mirror of
https://github.com/facebook/rocksdb.git
synced 2024-11-26 16:30:56 +00:00
e8cb32ed67
Summary: The patch adds blob file support to the `Get` API by extending `Version` so that whenever a blob reference is read from a file, the blob is retrieved from the corresponding blob file and passed back to the caller. (This is assuming the blob reference is valid and the blob file is actually part of the given `Version`.) It also introduces a cache of `BlobFileReader`s called `BlobFileCache` that enables sharing `BlobFileReader`s between callers. `BlobFileCache` uses the same backing cache as `TableCache`, so `max_open_files` (if specified) limits the total number of open (table + blob) files. TODO: proactively open/cache blob files and pin the cache handles of the readers in the metadata objects similarly to what `VersionBuilder::LoadTableHandlers` does for table files. Pull Request resolved: https://github.com/facebook/rocksdb/pull/7540 Test Plan: `make check` Reviewed By: riversand963 Differential Revision: D24260219 Pulled By: ltamasi fbshipit-source-id: a8a2a4f11d3d04d6082201b52184bc4d7b0857ba
100 lines
2.8 KiB
C++
100 lines
2.8 KiB
C++
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
// 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).
|
|
|
|
#include "db/blob/blob_file_cache.h"
|
|
|
|
#include <cassert>
|
|
#include <memory>
|
|
|
|
#include "db/blob/blob_file_reader.h"
|
|
#include "options/cf_options.h"
|
|
#include "rocksdb/cache.h"
|
|
#include "rocksdb/slice.h"
|
|
#include "test_util/sync_point.h"
|
|
#include "util/hash.h"
|
|
|
|
namespace ROCKSDB_NAMESPACE {
|
|
|
|
BlobFileCache::BlobFileCache(Cache* cache,
|
|
const ImmutableCFOptions* immutable_cf_options,
|
|
const FileOptions* file_options,
|
|
uint32_t column_family_id,
|
|
HistogramImpl* blob_file_read_hist)
|
|
: cache_(cache),
|
|
mutex_(kNumberOfMutexStripes, GetSliceNPHash64),
|
|
immutable_cf_options_(immutable_cf_options),
|
|
file_options_(file_options),
|
|
column_family_id_(column_family_id),
|
|
blob_file_read_hist_(blob_file_read_hist) {
|
|
assert(cache_);
|
|
assert(immutable_cf_options_);
|
|
assert(file_options_);
|
|
}
|
|
|
|
Status BlobFileCache::GetBlobFileReader(
|
|
uint64_t blob_file_number,
|
|
CacheHandleGuard<BlobFileReader>* blob_file_reader) {
|
|
assert(blob_file_reader);
|
|
assert(blob_file_reader->IsEmpty());
|
|
|
|
const Slice key = GetSlice(&blob_file_number);
|
|
|
|
assert(cache_);
|
|
|
|
Cache::Handle* handle = cache_->Lookup(key);
|
|
if (handle) {
|
|
*blob_file_reader = CacheHandleGuard<BlobFileReader>(cache_, handle);
|
|
return Status::OK();
|
|
}
|
|
|
|
TEST_SYNC_POINT("BlobFileCache::GetBlobFileReader:DoubleCheck");
|
|
|
|
// Check again while holding mutex
|
|
MutexLock lock(mutex_.get(key));
|
|
|
|
handle = cache_->Lookup(key);
|
|
if (handle) {
|
|
*blob_file_reader = CacheHandleGuard<BlobFileReader>(cache_, handle);
|
|
return Status::OK();
|
|
}
|
|
|
|
assert(immutable_cf_options_);
|
|
Statistics* const statistics = immutable_cf_options_->statistics;
|
|
|
|
RecordTick(statistics, NO_FILE_OPENS);
|
|
|
|
std::unique_ptr<BlobFileReader> reader;
|
|
|
|
{
|
|
assert(file_options_);
|
|
const Status s = BlobFileReader::Create(
|
|
*immutable_cf_options_, *file_options_, column_family_id_,
|
|
blob_file_read_hist_, blob_file_number, &reader);
|
|
if (!s.ok()) {
|
|
RecordTick(statistics, NO_FILE_ERRORS);
|
|
return s;
|
|
}
|
|
}
|
|
|
|
{
|
|
constexpr size_t charge = 1;
|
|
|
|
const Status s = cache_->Insert(key, reader.get(), charge,
|
|
&DeleteCacheEntry<BlobFileReader>, &handle);
|
|
if (!s.ok()) {
|
|
RecordTick(statistics, NO_FILE_ERRORS);
|
|
return s;
|
|
}
|
|
}
|
|
|
|
reader.release();
|
|
|
|
*blob_file_reader = CacheHandleGuard<BlobFileReader>(cache_, handle);
|
|
|
|
return Status::OK();
|
|
}
|
|
|
|
} // namespace ROCKSDB_NAMESPACE
|