mirror of
https://github.com/facebook/rocksdb.git
synced 2026-07-07 14:47:40 +08:00
a004c2d850
Summary:
Add EXPERIMENTAL embedded blob SST support for SstFileWriter through
OpenWithEmbeddedBlobs(). Eligible large values are written as same-file blob
records inline in a block-based SST as values are added (interleaved with data
blocks), while table entries store same-file BlobIndex references that readers
resolve for Get, MultiGet, and iteration, including mixed embedded and
non-embedded wide-column values.
Embedded-blob handling is folded directly into BlockBasedTableBuilder rather than
living in SstFileWriter or a separate table-builder wrapper: SstFileWriter only
selects the mode (via TableBuilderOptions::embedded_blob_options), and the
builder writes blob records inline using its own file writer and running offset.
This is enabled by disabling index-value delta encoding for these SSTs — delta
encoding reconstructs a data block's offset from the previous block and so
requires byte-contiguous data blocks, which interleaved blob records would break.
With full (non-delta) index values, blob records can sit between data blocks, so
no entry buffering/replay is needed. To keep inline blob appends correctly
ordered with data-block writes, these SSTs use single-threaded (non-parallel)
compression. The mode is the only entry point today but the placement keeps it
open to generalization beyond SstFileWriter; regardless, this experimental
feature is expected only to have niche applications.
(Previous revisions of this change allowed delta-encoded index blocks by
putting all blobs at the beginning of the file, but that was a more awkward
and memory-hungry implementation due to buffering all the data blocks before
writing.)
The on-disk record format (SimpleGen2Blob: payload bytes followed by a 5-byte
trailer of a compression marker plus a builtin checksum that is context-modified
by the record's absolute file offset) lives in db/blob/blob_gen2_format.{h,cc},
which now owns both the read (ReadAndVerifySimpleGen2BlobRecord) and write
(WriteSimpleGen2BlobRecord) sides so the format is defined in one place. This is
expected to be reused for upcoming "blog file" support.
Readers need no record-layout metadata: same-file blob resolution is purely
absolute-offset keyed, and the per-record offset-modified checksum (plus a cheap
"record fits within the file" bound) is the corruption guard. The reader's only
embedded-blob metadata is presence: a best-effort auxiliary table property
(blob count and payload-byte totals, for diagnostics) whose mere presence signals
that the SST contains embedded blobs.
Reads route through the column family's BlobSource when a DB is attached, so
embedded payloads are served from / inserted into the blob value cache and
recorded in BLOB_DB_* statistics; the cache key is derived from the
SimpleGen2Blob offset scheme (the same scheme block-based SST blocks use), so
embedded blob records stay collision-free with data blocks even when the blob
cache and block cache are the same cache. Non-DB openers (SstFileReader,
sst_dump, repair, ingestion prevalidation) have no BlobSource and fall back to a
direct, uncached read.
Same-file BlobIndex references use blob file number 0 as the marker. That value
also serves as the invalid blob-file-number sentinel in broader metadata code,
but the meanings do not conflict when used carefully: only the embedded-SST
reader/writer path interprets 0 as same-file, while generic file-metadata paths
continue to reject it as invalid. Using 1 would be worse because legacy
"stackable" BlobDB can use low blob file numbers, including 1, so reserving it
would collide with real blob files.
Compression options remain in the public API as placeholders, but embedded blob
compression support is deferred. Integrating compression with
BlockBasedTableBuilder while avoiding copied CompressAndVerifyBlock-style logic
is tricky enough to deserve a separate, focused PR.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/14851
Test Plan:
- Added basic feature coverage to the crash test.
- Added BlobIndexTest.SameFileBlobIndex and BlobGarbageMeterTest.SameFileBlobIndex
coverage for same-file BlobIndex encoding, display, recognition, and ignoring
same-file references in blob-garbage accounting.
- Extended FileMetaDataTest.UpdateBoundariesBlobIndex to preserve the generic
zero-file-number corruption check while keeping same-file embedded blob
semantics at the table-reader/writer layers.
- SstFileReader embedded blob coverage: round-trip Get, MultiGet, and iterator
reads; format_version gating; ignored placeholder compression options; the
2048-byte default min_blob_size; wide-column mixed embedded/non-embedded
values; and early append-error surfacing.
- Added an interleaved-layout test (small block_size with alternating
small/large values) asserting the SST property index_value_is_delta_encoded==0,
more than one data block, and that blob records are interspersed with data
blocks (not a strict front prefix), with all values read back correctly via
Get and iteration; replaces the old "ignored bytes before/after the blob record
prefix" test.
- Added an embedded-record corruption test: flipping a byte inside a blob
record's payload yields Corruption on read with verify_checksums (the
offset-keyed record checksum is the guard now that the range pre-check is gone).
- Exercised the cached read path through BlobSource, including blob-cache
hit/miss behavior and the shared blob_cache == block_cache configuration, in
db_blob_index_test.
- Normal-path CPU regression check: release (DEBUG_LEVEL=0) db_bench on a
non-embedded DB comparing this change vs upstream main, 3 interleaved reps of
fillseq, fillrandom, readrandom, and readseq (5M keys, value_size=100,
compression none, DB on /dev/shm). All deltas were within run-to-run noise
(~1%), i.e. no measurable regression from adding the embedded-mode branch to
the builder hot path.
Reviewed By: xingbowang
Differential Revision: D108564468
Pulled By: pdillinger
fbshipit-source-id: 5f01ffb1d40c6fd5b82d2451ec3342abb5040ca6
203 lines
8.7 KiB
C++
203 lines
8.7 KiB
C++
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
// (found in the LICENSE.Apache file in the root directory).
|
|
|
|
#pragma once
|
|
|
|
#include <cinttypes>
|
|
#include <memory>
|
|
|
|
#include "cache/cache_key.h"
|
|
#include "cache/typed_cache.h"
|
|
#include "db/blob/blob_contents.h"
|
|
#include "db/blob/blob_file_cache.h"
|
|
#include "db/blob/blob_read_request.h"
|
|
#include "rocksdb/cache.h"
|
|
#include "rocksdb/rocksdb_namespace.h"
|
|
#include "table/block_based/cachable_entry.h"
|
|
#include "util/autovector.h"
|
|
|
|
namespace ROCKSDB_NAMESPACE {
|
|
|
|
struct ImmutableOptions;
|
|
struct MutableCFOptions;
|
|
class Status;
|
|
class FilePrefetchBuffer;
|
|
class Slice;
|
|
class RandomAccessFileReader;
|
|
enum ChecksumType : char;
|
|
|
|
// BlobSource is a class that provides universal access to blobs, regardless of
|
|
// whether they are in the blob cache, secondary cache, or (remote) storage.
|
|
// Depending on user settings, it always fetch blobs from multi-tier cache and
|
|
// storage with minimal cost.
|
|
class BlobSource {
|
|
public:
|
|
// NOTE: db_id, db_session_id, and blob_file_cache are saved by reference or
|
|
// pointer.
|
|
BlobSource(const ImmutableOptions& immutable_options,
|
|
const MutableCFOptions& mutable_cf_options,
|
|
const std::string& db_id, const std::string& db_session_id,
|
|
BlobFileCache* blob_file_cache);
|
|
|
|
BlobSource(const BlobSource&) = delete;
|
|
BlobSource& operator=(const BlobSource&) = delete;
|
|
|
|
~BlobSource();
|
|
|
|
// Read a blob from the underlying cache or one blob file.
|
|
//
|
|
// If successful, returns ok and sets "*value" to the newly retrieved
|
|
// uncompressed blob. If there was an error while fetching the blob, sets
|
|
// "*value" to empty and returns a non-ok status.
|
|
//
|
|
// Note: For consistency, whether the blob is found in the cache or on disk,
|
|
// sets "*bytes_read" to the size of on-disk (possibly compressed) blob
|
|
// record.
|
|
Status GetBlob(const ReadOptions& read_options, const Slice& user_key,
|
|
uint64_t file_number, uint64_t offset, uint64_t file_size,
|
|
uint64_t value_size, CompressionType compression_type,
|
|
FilePrefetchBuffer* prefetch_buffer, PinnableSlice* value,
|
|
uint64_t* bytes_read);
|
|
|
|
// Reads a SimpleGen2Blob payload (see db/blob/blob_gen2_format.h) through the
|
|
// blob value cache and BLOB_DB_* statistics. This is the counterpart to
|
|
// GetBlob() for the second-generation blob record format, which is read
|
|
// directly from a RandomAccessFileReader rather than from a traditional blob
|
|
// file.
|
|
//
|
|
// The cache key is derived from the SimpleGen2Blob format itself, not chosen
|
|
// by the caller: it is GetSimpleGen2BlobCacheKey(base_cache_key,
|
|
// record_offset), the same offset scheme block-based SST blocks use. The
|
|
// caller supplies only its file's `base_cache_key` (db_id / db_session_id /
|
|
// file_number). This keeps blob records collision-free with the file's data
|
|
// blocks even when the blob cache and block cache are the same cache.
|
|
//
|
|
// `file`, `record_offset`, `payload_size`, `checksum_type`,
|
|
// `base_context_checksum`, and `expected_compression` are the inputs to the
|
|
// SimpleGen2Blob reader used on a cache miss (see
|
|
// ReadAndVerifySimpleGen2BlobRecord). The on-disk record size (payload +
|
|
// trailer) is reported via `*bytes_read` (when non-null) and the
|
|
// BLOB_DB_BLOB_FILE_BYTES_READ / blob_read_byte counters, consistently on
|
|
// both cache hits and misses.
|
|
//
|
|
// On a cache hit, pins the cached value into `*value` (no copy). On a miss,
|
|
// reads + verifies the record into a cache-allocator buffer, records the
|
|
// per-read stats, inserts it into the cache (when configured and fill_cache
|
|
// is set), and pins it into `*value`. If blob_cache_ is not configured, the
|
|
// record is still read and read stats recorded, just without a cache
|
|
// lookup/insert.
|
|
Status GetSimpleGen2Blob(const ReadOptions& read_options,
|
|
const OffsetableCacheKey& base_cache_key,
|
|
RandomAccessFileReader* file, uint64_t record_offset,
|
|
uint64_t payload_size, ChecksumType checksum_type,
|
|
uint32_t base_context_checksum,
|
|
CompressionType expected_compression,
|
|
PinnableSlice* value, uint64_t* bytes_read);
|
|
|
|
// Read multiple blobs from the underlying cache or blob file(s).
|
|
//
|
|
// If successful, returns ok and sets "result" in the elements of "blob_reqs"
|
|
// to the newly retrieved uncompressed blobs. If there was an error while
|
|
// fetching one of blobs, sets its "result" to empty and sets its
|
|
// corresponding "status" to a non-ok status.
|
|
//
|
|
// Note:
|
|
// - The main difference between this function and MultiGetBlobFromOneFile is
|
|
// that this function can read multiple blobs from multiple blob files.
|
|
//
|
|
// - For consistency, whether the blob is found in the cache or on disk, sets
|
|
// "*bytes_read" to the total size of on-disk (possibly compressed) blob
|
|
// records.
|
|
void MultiGetBlob(const ReadOptions& read_options,
|
|
autovector<BlobFileReadRequests>& blob_reqs,
|
|
uint64_t* bytes_read);
|
|
|
|
// Read multiple blobs from the underlying cache or one blob file.
|
|
//
|
|
// If successful, returns ok and sets "result" in the elements of "blob_reqs"
|
|
// to the newly retrieved uncompressed blobs. If there was an error while
|
|
// fetching one of blobs, sets its "result" to empty and sets its
|
|
// corresponding "status" to a non-ok status.
|
|
//
|
|
// Note:
|
|
// - The main difference between this function and MultiGetBlob is that this
|
|
// function is only used for the case where the demanded blobs are stored in
|
|
// one blob file. MultiGetBlob will call this function multiple times if the
|
|
// demanded blobs are stored in multiple blob files.
|
|
//
|
|
// - For consistency, whether the blob is found in the cache or on disk, sets
|
|
// "*bytes_read" to the total size of on-disk (possibly compressed) blob
|
|
// records.
|
|
void MultiGetBlobFromOneFile(const ReadOptions& read_options,
|
|
uint64_t file_number, uint64_t file_size,
|
|
autovector<BlobReadRequest>& blob_reqs,
|
|
uint64_t* bytes_read);
|
|
|
|
inline Status GetBlobFileReader(
|
|
const ReadOptions& read_options, uint64_t blob_file_number,
|
|
CacheHandleGuard<BlobFileReader>* blob_file_reader) {
|
|
return blob_file_cache_->GetBlobFileReader(read_options, blob_file_number,
|
|
blob_file_reader);
|
|
}
|
|
|
|
inline Cache* GetBlobCache() const { return blob_cache_.get(); }
|
|
|
|
bool TEST_BlobInCache(uint64_t file_number, uint64_t file_size,
|
|
uint64_t offset, size_t* charge = nullptr) const;
|
|
|
|
// For TypedSharedCacheInterface
|
|
void Create(BlobContents** out, const char* buf, size_t size,
|
|
MemoryAllocator* alloc);
|
|
|
|
using SharedCacheInterface =
|
|
FullTypedSharedCacheInterface<BlobContents, BlobContentsCreator>;
|
|
using TypedHandle = SharedCacheInterface::TypedHandle;
|
|
|
|
private:
|
|
Status GetBlobFromCache(const Slice& cache_key,
|
|
CacheHandleGuard<BlobContents>* cached_blob) const;
|
|
|
|
Status PutBlobIntoCache(const Slice& cache_key,
|
|
std::unique_ptr<BlobContents>* blob,
|
|
CacheHandleGuard<BlobContents>* cached_blob) const;
|
|
|
|
static void PinCachedBlob(CacheHandleGuard<BlobContents>* cached_blob,
|
|
PinnableSlice* value);
|
|
|
|
static void PinOwnedBlob(std::unique_ptr<BlobContents>* owned_blob,
|
|
PinnableSlice* value);
|
|
|
|
TypedHandle* GetEntryFromCache(const Slice& key) const;
|
|
|
|
Status InsertEntryIntoCache(const Slice& key, BlobContents* value,
|
|
TypedHandle** cache_handle,
|
|
Cache::Priority priority) const;
|
|
|
|
inline CacheKey GetCacheKey(uint64_t file_number, uint64_t /*file_size*/,
|
|
uint64_t offset) const {
|
|
OffsetableCacheKey base_cache_key(db_id_, db_session_id_, file_number);
|
|
return base_cache_key.WithOffset(offset);
|
|
}
|
|
|
|
const std::string& db_id_;
|
|
const std::string& db_session_id_;
|
|
|
|
Statistics* statistics_;
|
|
|
|
// A cache to store blob file reader.
|
|
BlobFileCache* blob_file_cache_;
|
|
|
|
// A cache to store uncompressed blobs.
|
|
mutable SharedCacheInterface blob_cache_;
|
|
|
|
// The control option of how the cache tiers will be used. Currently rocksdb
|
|
// support block/blob cache (volatile tier) and secondary cache (this tier
|
|
// isn't strictly speaking a non-volatile tier since the compressed cache in
|
|
// this tier is in volatile memory).
|
|
const CacheTier lowest_used_cache_tier_;
|
|
};
|
|
|
|
} // namespace ROCKSDB_NAMESPACE
|