Introduce BlobFileCache and add support for blob files to Get() (#7540)
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: a8a2a4f11d3d04d6082201b52184bc4d7b0857bamain
parent
fa2a8cda7b
commit
e8cb32ed67
@ -0,0 +1,114 @@ |
||||
// 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).
|
||||
|
||||
#pragma once |
||||
|
||||
#include <cassert> |
||||
|
||||
#include "rocksdb/cache.h" |
||||
#include "rocksdb/rocksdb_namespace.h" |
||||
|
||||
namespace ROCKSDB_NAMESPACE { |
||||
|
||||
// Returns the cached value given a cache handle.
|
||||
template <typename T> |
||||
T* GetFromCacheHandle(Cache* cache, Cache::Handle* handle) { |
||||
assert(cache); |
||||
assert(handle); |
||||
|
||||
return static_cast<T*>(cache->Value(handle)); |
||||
} |
||||
|
||||
// Simple generic deleter for Cache (to be used with Cache::Insert).
|
||||
template <typename T> |
||||
void DeleteCacheEntry(const Slice& /* key */, void* value) { |
||||
delete static_cast<T*>(value); |
||||
} |
||||
|
||||
// Turns a T* into a Slice so it can be used as a key with Cache.
|
||||
template <typename T> |
||||
Slice GetSlice(const T* t) { |
||||
return Slice(reinterpret_cast<const char*>(t), sizeof(T)); |
||||
} |
||||
|
||||
// Generic resource management object for cache handles that releases the handle
|
||||
// when destroyed. Has unique ownership of the handle, so copying it is not
|
||||
// allowed, while moving it transfers ownership.
|
||||
template <typename T> |
||||
class CacheHandleGuard { |
||||
public: |
||||
CacheHandleGuard() = default; |
||||
|
||||
CacheHandleGuard(Cache* cache, Cache::Handle* handle) |
||||
: cache_(cache), |
||||
handle_(handle), |
||||
value_(GetFromCacheHandle<T>(cache, handle)) { |
||||
assert(cache_ && handle_ && value_); |
||||
} |
||||
|
||||
CacheHandleGuard(const CacheHandleGuard&) = delete; |
||||
CacheHandleGuard& operator=(const CacheHandleGuard&) = delete; |
||||
|
||||
CacheHandleGuard(CacheHandleGuard&& rhs) noexcept |
||||
: cache_(rhs.cache_), handle_(rhs.handle_), value_(rhs.value_) { |
||||
assert((!cache_ && !handle_ && !value_) || (cache_ && handle_ && value_)); |
||||
|
||||
rhs.ResetFields(); |
||||
} |
||||
|
||||
CacheHandleGuard& operator=(CacheHandleGuard&& rhs) noexcept { |
||||
if (this == &rhs) { |
||||
return *this; |
||||
} |
||||
|
||||
ReleaseHandle(); |
||||
|
||||
cache_ = rhs.cache_; |
||||
handle_ = rhs.handle_; |
||||
value_ = rhs.value_; |
||||
|
||||
assert((!cache_ && !handle_ && !value_) || (cache_ && handle_ && value_)); |
||||
|
||||
rhs.ResetFields(); |
||||
|
||||
return *this; |
||||
} |
||||
|
||||
~CacheHandleGuard() { ReleaseHandle(); } |
||||
|
||||
bool IsEmpty() const { return !handle_; } |
||||
|
||||
Cache* GetCache() const { return cache_; } |
||||
Cache::Handle* GetCacheHandle() const { return handle_; } |
||||
T* GetValue() const { return value_; } |
||||
|
||||
void Reset() { |
||||
ReleaseHandle(); |
||||
ResetFields(); |
||||
} |
||||
|
||||
private: |
||||
void ReleaseHandle() { |
||||
if (IsEmpty()) { |
||||
return; |
||||
} |
||||
|
||||
assert(cache_); |
||||
cache_->Release(handle_); |
||||
} |
||||
|
||||
void ResetFields() { |
||||
cache_ = nullptr; |
||||
handle_ = nullptr; |
||||
value_ = nullptr; |
||||
} |
||||
|
||||
private: |
||||
Cache* cache_ = nullptr; |
||||
Cache::Handle* handle_ = nullptr; |
||||
T* value_ = nullptr; |
||||
}; |
||||
|
||||
} // namespace ROCKSDB_NAMESPACE
|
@ -0,0 +1,99 @@ |
||||
// 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
|
@ -0,0 +1,49 @@ |
||||
// 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).
|
||||
|
||||
#pragma once |
||||
|
||||
#include <cinttypes> |
||||
|
||||
#include "cache/cache_helpers.h" |
||||
#include "rocksdb/rocksdb_namespace.h" |
||||
#include "util/mutexlock.h" |
||||
|
||||
namespace ROCKSDB_NAMESPACE { |
||||
|
||||
class Cache; |
||||
struct ImmutableCFOptions; |
||||
struct FileOptions; |
||||
class HistogramImpl; |
||||
class Status; |
||||
class BlobFileReader; |
||||
class Slice; |
||||
|
||||
class BlobFileCache { |
||||
public: |
||||
BlobFileCache(Cache* cache, const ImmutableCFOptions* immutable_cf_options, |
||||
const FileOptions* file_options, uint32_t column_family_id, |
||||
HistogramImpl* blob_file_read_hist); |
||||
|
||||
BlobFileCache(const BlobFileCache&) = delete; |
||||
BlobFileCache& operator=(const BlobFileCache&) = delete; |
||||
|
||||
Status GetBlobFileReader(uint64_t blob_file_number, |
||||
CacheHandleGuard<BlobFileReader>* blob_file_reader); |
||||
|
||||
private: |
||||
Cache* cache_; |
||||
// Note: mutex_ below is used to guard against multiple threads racing to open
|
||||
// the same file.
|
||||
Striped<port::Mutex, Slice> mutex_; |
||||
const ImmutableCFOptions* immutable_cf_options_; |
||||
const FileOptions* file_options_; |
||||
uint32_t column_family_id_; |
||||
HistogramImpl* blob_file_read_hist_; |
||||
|
||||
static constexpr size_t kNumberOfMutexStripes = 1 << 7; |
||||
}; |
||||
|
||||
} // namespace ROCKSDB_NAMESPACE
|
@ -0,0 +1,268 @@ |
||||
// 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 <string> |
||||
|
||||
#include "db/blob/blob_log_format.h" |
||||
#include "db/blob/blob_log_writer.h" |
||||
#include "env/mock_env.h" |
||||
#include "file/filename.h" |
||||
#include "file/read_write_util.h" |
||||
#include "file/writable_file_writer.h" |
||||
#include "options/cf_options.h" |
||||
#include "rocksdb/cache.h" |
||||
#include "rocksdb/env.h" |
||||
#include "rocksdb/file_system.h" |
||||
#include "rocksdb/options.h" |
||||
#include "rocksdb/statistics.h" |
||||
#include "test_util/sync_point.h" |
||||
#include "test_util/testharness.h" |
||||
|
||||
namespace ROCKSDB_NAMESPACE { |
||||
|
||||
namespace { |
||||
|
||||
// Creates a test blob file with a single blob in it.
|
||||
void WriteBlobFile(uint32_t column_family_id, |
||||
const ImmutableCFOptions& immutable_cf_options, |
||||
uint64_t blob_file_number) { |
||||
assert(!immutable_cf_options.cf_paths.empty()); |
||||
|
||||
const std::string blob_file_path = BlobFileName( |
||||
immutable_cf_options.cf_paths.front().path, blob_file_number); |
||||
|
||||
std::unique_ptr<FSWritableFile> file; |
||||
ASSERT_OK(NewWritableFile(immutable_cf_options.fs, blob_file_path, &file, |
||||
FileOptions())); |
||||
|
||||
std::unique_ptr<WritableFileWriter> file_writer( |
||||
new WritableFileWriter(std::move(file), blob_file_path, FileOptions(), |
||||
immutable_cf_options.env)); |
||||
|
||||
constexpr Statistics* statistics = nullptr; |
||||
constexpr bool use_fsync = false; |
||||
|
||||
BlobLogWriter blob_log_writer(std::move(file_writer), |
||||
immutable_cf_options.env, statistics, |
||||
blob_file_number, use_fsync); |
||||
|
||||
constexpr bool has_ttl = false; |
||||
constexpr ExpirationRange expiration_range; |
||||
|
||||
BlobLogHeader header(column_family_id, kNoCompression, has_ttl, |
||||
expiration_range); |
||||
|
||||
ASSERT_OK(blob_log_writer.WriteHeader(header)); |
||||
|
||||
constexpr char key[] = "key"; |
||||
constexpr char blob[] = "blob"; |
||||
|
||||
std::string compressed_blob; |
||||
Slice blob_to_write; |
||||
|
||||
uint64_t key_offset = 0; |
||||
uint64_t blob_offset = 0; |
||||
|
||||
ASSERT_OK(blob_log_writer.AddRecord(key, blob, &key_offset, &blob_offset)); |
||||
|
||||
BlobLogFooter footer; |
||||
footer.blob_count = 1; |
||||
footer.expiration_range = expiration_range; |
||||
|
||||
std::string checksum_method; |
||||
std::string checksum_value; |
||||
|
||||
ASSERT_OK( |
||||
blob_log_writer.AppendFooter(footer, &checksum_method, &checksum_value)); |
||||
} |
||||
|
||||
} // anonymous namespace
|
||||
|
||||
class BlobFileCacheTest : public testing::Test { |
||||
protected: |
||||
BlobFileCacheTest() : mock_env_(Env::Default()) {} |
||||
|
||||
MockEnv mock_env_; |
||||
}; |
||||
|
||||
TEST_F(BlobFileCacheTest, GetBlobFileReader) { |
||||
Options options; |
||||
options.env = &mock_env_; |
||||
options.statistics = CreateDBStatistics(); |
||||
options.cf_paths.emplace_back( |
||||
test::PerThreadDBPath(&mock_env_, "BlobFileCacheTest_GetBlobFileReader"), |
||||
0); |
||||
options.enable_blob_files = true; |
||||
|
||||
constexpr uint32_t column_family_id = 1; |
||||
ImmutableCFOptions immutable_cf_options(options); |
||||
constexpr uint64_t blob_file_number = 123; |
||||
|
||||
WriteBlobFile(column_family_id, immutable_cf_options, blob_file_number); |
||||
|
||||
constexpr size_t capacity = 10; |
||||
std::shared_ptr<Cache> backing_cache = NewLRUCache(capacity); |
||||
|
||||
FileOptions file_options; |
||||
constexpr HistogramImpl* blob_file_read_hist = nullptr; |
||||
|
||||
BlobFileCache blob_file_cache(backing_cache.get(), &immutable_cf_options, |
||||
&file_options, column_family_id, |
||||
blob_file_read_hist); |
||||
|
||||
// First try: reader should be opened and put in cache
|
||||
CacheHandleGuard<BlobFileReader> first; |
||||
|
||||
ASSERT_OK(blob_file_cache.GetBlobFileReader(blob_file_number, &first)); |
||||
ASSERT_NE(first.GetValue(), nullptr); |
||||
ASSERT_EQ(options.statistics->getTickerCount(NO_FILE_OPENS), 1); |
||||
ASSERT_EQ(options.statistics->getTickerCount(NO_FILE_ERRORS), 0); |
||||
|
||||
// Second try: reader should be served from cache
|
||||
CacheHandleGuard<BlobFileReader> second; |
||||
|
||||
ASSERT_OK(blob_file_cache.GetBlobFileReader(blob_file_number, &second)); |
||||
ASSERT_NE(second.GetValue(), nullptr); |
||||
ASSERT_EQ(options.statistics->getTickerCount(NO_FILE_OPENS), 1); |
||||
ASSERT_EQ(options.statistics->getTickerCount(NO_FILE_ERRORS), 0); |
||||
|
||||
ASSERT_EQ(first.GetValue(), second.GetValue()); |
||||
} |
||||
|
||||
TEST_F(BlobFileCacheTest, GetBlobFileReader_Race) { |
||||
Options options; |
||||
options.env = &mock_env_; |
||||
options.statistics = CreateDBStatistics(); |
||||
options.cf_paths.emplace_back( |
||||
test::PerThreadDBPath(&mock_env_, |
||||
"BlobFileCacheTest_GetBlobFileReader_Race"), |
||||
0); |
||||
options.enable_blob_files = true; |
||||
|
||||
constexpr uint32_t column_family_id = 1; |
||||
ImmutableCFOptions immutable_cf_options(options); |
||||
constexpr uint64_t blob_file_number = 123; |
||||
|
||||
WriteBlobFile(column_family_id, immutable_cf_options, blob_file_number); |
||||
|
||||
constexpr size_t capacity = 10; |
||||
std::shared_ptr<Cache> backing_cache = NewLRUCache(capacity); |
||||
|
||||
FileOptions file_options; |
||||
constexpr HistogramImpl* blob_file_read_hist = nullptr; |
||||
|
||||
BlobFileCache blob_file_cache(backing_cache.get(), &immutable_cf_options, |
||||
&file_options, column_family_id, |
||||
blob_file_read_hist); |
||||
|
||||
CacheHandleGuard<BlobFileReader> first; |
||||
CacheHandleGuard<BlobFileReader> second; |
||||
|
||||
SyncPoint::GetInstance()->SetCallBack( |
||||
"BlobFileCache::GetBlobFileReader:DoubleCheck", [&](void* /* arg */) { |
||||
// Disabling sync points to prevent infinite recursion
|
||||
SyncPoint::GetInstance()->DisableProcessing(); |
||||
|
||||
ASSERT_OK(blob_file_cache.GetBlobFileReader(blob_file_number, &second)); |
||||
ASSERT_NE(second.GetValue(), nullptr); |
||||
ASSERT_EQ(options.statistics->getTickerCount(NO_FILE_OPENS), 1); |
||||
ASSERT_EQ(options.statistics->getTickerCount(NO_FILE_ERRORS), 0); |
||||
}); |
||||
SyncPoint::GetInstance()->EnableProcessing(); |
||||
|
||||
ASSERT_OK(blob_file_cache.GetBlobFileReader(blob_file_number, &first)); |
||||
ASSERT_NE(first.GetValue(), nullptr); |
||||
ASSERT_EQ(options.statistics->getTickerCount(NO_FILE_OPENS), 1); |
||||
ASSERT_EQ(options.statistics->getTickerCount(NO_FILE_ERRORS), 0); |
||||
|
||||
ASSERT_EQ(first.GetValue(), second.GetValue()); |
||||
|
||||
SyncPoint::GetInstance()->DisableProcessing(); |
||||
SyncPoint::GetInstance()->ClearAllCallBacks(); |
||||
} |
||||
|
||||
TEST_F(BlobFileCacheTest, GetBlobFileReader_IOError) { |
||||
Options options; |
||||
options.env = &mock_env_; |
||||
options.statistics = CreateDBStatistics(); |
||||
options.cf_paths.emplace_back( |
||||
test::PerThreadDBPath(&mock_env_, |
||||
"BlobFileCacheTest_GetBlobFileReader_IOError"), |
||||
0); |
||||
options.enable_blob_files = true; |
||||
|
||||
constexpr size_t capacity = 10; |
||||
std::shared_ptr<Cache> backing_cache = NewLRUCache(capacity); |
||||
|
||||
ImmutableCFOptions immutable_cf_options(options); |
||||
FileOptions file_options; |
||||
constexpr uint32_t column_family_id = 1; |
||||
constexpr HistogramImpl* blob_file_read_hist = nullptr; |
||||
|
||||
BlobFileCache blob_file_cache(backing_cache.get(), &immutable_cf_options, |
||||
&file_options, column_family_id, |
||||
blob_file_read_hist); |
||||
|
||||
// Note: there is no blob file with the below number
|
||||
constexpr uint64_t blob_file_number = 123; |
||||
|
||||
CacheHandleGuard<BlobFileReader> reader; |
||||
|
||||
ASSERT_TRUE( |
||||
blob_file_cache.GetBlobFileReader(blob_file_number, &reader).IsIOError()); |
||||
ASSERT_EQ(reader.GetValue(), nullptr); |
||||
ASSERT_EQ(options.statistics->getTickerCount(NO_FILE_OPENS), 1); |
||||
ASSERT_EQ(options.statistics->getTickerCount(NO_FILE_ERRORS), 1); |
||||
} |
||||
|
||||
TEST_F(BlobFileCacheTest, GetBlobFileReader_CacheFull) { |
||||
Options options; |
||||
options.env = &mock_env_; |
||||
options.statistics = CreateDBStatistics(); |
||||
options.cf_paths.emplace_back( |
||||
test::PerThreadDBPath(&mock_env_, |
||||
"BlobFileCacheTest_GetBlobFileReader_CacheFull"), |
||||
0); |
||||
options.enable_blob_files = true; |
||||
|
||||
constexpr uint32_t column_family_id = 1; |
||||
ImmutableCFOptions immutable_cf_options(options); |
||||
constexpr uint64_t blob_file_number = 123; |
||||
|
||||
WriteBlobFile(column_family_id, immutable_cf_options, blob_file_number); |
||||
|
||||
constexpr size_t capacity = 0; |
||||
constexpr int num_shard_bits = -1; // determined automatically
|
||||
constexpr bool strict_capacity_limit = true; |
||||
std::shared_ptr<Cache> backing_cache = |
||||
NewLRUCache(capacity, num_shard_bits, strict_capacity_limit); |
||||
|
||||
FileOptions file_options; |
||||
constexpr HistogramImpl* blob_file_read_hist = nullptr; |
||||
|
||||
BlobFileCache blob_file_cache(backing_cache.get(), &immutable_cf_options, |
||||
&file_options, column_family_id, |
||||
blob_file_read_hist); |
||||
|
||||
// Insert into cache should fail since it has zero capacity and
|
||||
// strict_capacity_limit is set
|
||||
CacheHandleGuard<BlobFileReader> reader; |
||||
|
||||
ASSERT_TRUE(blob_file_cache.GetBlobFileReader(blob_file_number, &reader) |
||||
.IsIncomplete()); |
||||
ASSERT_EQ(reader.GetValue(), nullptr); |
||||
ASSERT_EQ(options.statistics->getTickerCount(NO_FILE_OPENS), 1); |
||||
ASSERT_EQ(options.statistics->getTickerCount(NO_FILE_ERRORS), 1); |
||||
} |
||||
|
||||
} // namespace ROCKSDB_NAMESPACE
|
||||
|
||||
int main(int argc, char** argv) { |
||||
::testing::InitGoogleTest(&argc, argv); |
||||
return RUN_ALL_TESTS(); |
||||
} |
@ -0,0 +1,183 @@ |
||||
// 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_index.h" |
||||
#include "db/db_test_util.h" |
||||
#include "port/stack_trace.h" |
||||
#include "test_util/sync_point.h" |
||||
#include "utilities/fault_injection_env.h" |
||||
|
||||
namespace ROCKSDB_NAMESPACE { |
||||
|
||||
class DBBlobBasicTest : public DBTestBase { |
||||
protected: |
||||
DBBlobBasicTest() |
||||
: DBTestBase("/db_blob_basic_test", /* env_do_fsync */ false) {} |
||||
}; |
||||
|
||||
TEST_F(DBBlobBasicTest, GetBlob) { |
||||
Options options; |
||||
options.enable_blob_files = true; |
||||
options.min_blob_size = 0; |
||||
|
||||
Reopen(options); |
||||
|
||||
constexpr char key[] = "key"; |
||||
constexpr char blob_value[] = "blob_value"; |
||||
|
||||
ASSERT_OK(Put(key, blob_value)); |
||||
|
||||
ASSERT_OK(Flush()); |
||||
|
||||
ASSERT_EQ(Get(key), blob_value); |
||||
|
||||
// Try again with no I/O allowed. The table and the necessary blocks should
|
||||
// already be in their respective caches; however, the blob itself can only be
|
||||
// read from the blob file, so the read should return Incomplete.
|
||||
ReadOptions read_options; |
||||
read_options.read_tier = kBlockCacheTier; |
||||
|
||||
PinnableSlice result; |
||||
ASSERT_TRUE(db_->Get(read_options, db_->DefaultColumnFamily(), key, &result) |
||||
.IsIncomplete()); |
||||
} |
||||
|
||||
TEST_F(DBBlobBasicTest, GetBlob_CorruptIndex) { |
||||
Options options; |
||||
options.enable_blob_files = true; |
||||
options.min_blob_size = 0; |
||||
|
||||
Reopen(options); |
||||
|
||||
constexpr char key[] = "key"; |
||||
|
||||
// Fake a corrupt blob index.
|
||||
const std::string blob_index("foobar"); |
||||
|
||||
WriteBatch batch; |
||||
ASSERT_OK(WriteBatchInternal::PutBlobIndex(&batch, 0, key, blob_index)); |
||||
ASSERT_OK(db_->Write(WriteOptions(), &batch)); |
||||
|
||||
ASSERT_OK(Flush()); |
||||
|
||||
PinnableSlice result; |
||||
ASSERT_TRUE(db_->Get(ReadOptions(), db_->DefaultColumnFamily(), key, &result) |
||||
.IsCorruption()); |
||||
} |
||||
|
||||
TEST_F(DBBlobBasicTest, GetBlob_InlinedTTLIndex) { |
||||
constexpr uint64_t min_blob_size = 10; |
||||
|
||||
Options options; |
||||
options.enable_blob_files = true; |
||||
options.min_blob_size = min_blob_size; |
||||
|
||||
Reopen(options); |
||||
|
||||
constexpr char key[] = "key"; |
||||
constexpr char blob[] = "short"; |
||||
static_assert(sizeof(short) - 1 < min_blob_size, |
||||
"Blob too long to be inlined"); |
||||
|
||||
// Fake an inlined TTL blob index.
|
||||
std::string blob_index; |
||||
|
||||
constexpr uint64_t expiration = 1234567890; |
||||
|
||||
BlobIndex::EncodeInlinedTTL(&blob_index, expiration, blob); |
||||
|
||||
WriteBatch batch; |
||||
ASSERT_OK(WriteBatchInternal::PutBlobIndex(&batch, 0, key, blob_index)); |
||||
ASSERT_OK(db_->Write(WriteOptions(), &batch)); |
||||
|
||||
ASSERT_OK(Flush()); |
||||
|
||||
PinnableSlice result; |
||||
ASSERT_TRUE(db_->Get(ReadOptions(), db_->DefaultColumnFamily(), key, &result) |
||||
.IsCorruption()); |
||||
} |
||||
|
||||
TEST_F(DBBlobBasicTest, GetBlob_IndexWithInvalidFileNumber) { |
||||
Options options; |
||||
options.enable_blob_files = true; |
||||
options.min_blob_size = 0; |
||||
|
||||
Reopen(options); |
||||
|
||||
constexpr char key[] = "key"; |
||||
|
||||
// Fake a blob index referencing a non-existent blob file.
|
||||
std::string blob_index; |
||||
|
||||
constexpr uint64_t blob_file_number = 1000; |
||||
constexpr uint64_t offset = 1234; |
||||
constexpr uint64_t size = 5678; |
||||
|
||||
BlobIndex::EncodeBlob(&blob_index, blob_file_number, offset, size, |
||||
kNoCompression); |
||||
|
||||
WriteBatch batch; |
||||
ASSERT_OK(WriteBatchInternal::PutBlobIndex(&batch, 0, key, blob_index)); |
||||
ASSERT_OK(db_->Write(WriteOptions(), &batch)); |
||||
|
||||
ASSERT_OK(Flush()); |
||||
|
||||
PinnableSlice result; |
||||
ASSERT_TRUE(db_->Get(ReadOptions(), db_->DefaultColumnFamily(), key, &result) |
||||
.IsCorruption()); |
||||
} |
||||
|
||||
class DBBlobBasicIOErrorTest : public DBBlobBasicTest, |
||||
public testing::WithParamInterface<std::string> { |
||||
protected: |
||||
DBBlobBasicIOErrorTest() |
||||
: fault_injection_env_(Env::Default()), sync_point_(GetParam()) {} |
||||
~DBBlobBasicIOErrorTest() { Close(); } |
||||
|
||||
FaultInjectionTestEnv fault_injection_env_; |
||||
std::string sync_point_; |
||||
}; |
||||
|
||||
INSTANTIATE_TEST_CASE_P(DBBlobBasicTest, DBBlobBasicIOErrorTest, |
||||
::testing::ValuesIn(std::vector<std::string>{ |
||||
"BlobFileReader::OpenFile:NewRandomAccessFile", |
||||
"BlobFileReader::GetBlob:ReadFromFile"})); |
||||
|
||||
TEST_P(DBBlobBasicIOErrorTest, GetBlob_IOError) { |
||||
Options options; |
||||
options.env = &fault_injection_env_; |
||||
options.enable_blob_files = true; |
||||
options.min_blob_size = 0; |
||||
|
||||
Reopen(options); |
||||
|
||||
constexpr char key[] = "key"; |
||||
constexpr char blob_value[] = "blob_value"; |
||||
|
||||
ASSERT_OK(Put(key, blob_value)); |
||||
|
||||
ASSERT_OK(Flush()); |
||||
|
||||
SyncPoint::GetInstance()->SetCallBack(sync_point_, [this](void* /* arg */) { |
||||
fault_injection_env_.SetFilesystemActive(false, |
||||
Status::IOError(sync_point_)); |
||||
}); |
||||
SyncPoint::GetInstance()->EnableProcessing(); |
||||
|
||||
PinnableSlice result; |
||||
ASSERT_TRUE(db_->Get(ReadOptions(), db_->DefaultColumnFamily(), key, &result) |
||||
.IsIOError()); |
||||
|
||||
SyncPoint::GetInstance()->DisableProcessing(); |
||||
SyncPoint::GetInstance()->ClearAllCallBacks(); |
||||
} |
||||
|
||||
} // namespace ROCKSDB_NAMESPACE
|
||||
|
||||
int main(int argc, char** argv) { |
||||
ROCKSDB_NAMESPACE::port::InstallStackTraceHandler(); |
||||
::testing::InitGoogleTest(&argc, argv); |
||||
return RUN_ALL_TESTS(); |
||||
} |
Loading…
Reference in new issue