mirror of
https://github.com/facebook/rocksdb.git
synced 2024-11-27 02:44:18 +00:00
269478ee46
Summary: This PR implements support for a three tier cache - primary block cache, compressed secondary cache, and a nvm (local flash) secondary cache. This allows more effective utilization of the nvm cache, and minimizes the number of reads from local flash by caching compressed blocks in the compressed secondary cache. The basic design is as follows - 1. A new secondary cache implementation, ```TieredSecondaryCache```, is introduced. It keeps the compressed and nvm secondary caches and manages the movement of blocks between them and the primary block cache. To setup a three tier cache, we allocate a ```CacheWithSecondaryAdapter```, with a ```TieredSecondaryCache``` instance as the secondary cache. 2. The table reader passes both the uncompressed and compressed block to ```FullTypedCacheInterface::InsertFull```, allowing the block cache to optionally store the compressed block. 3. When there's a miss, the block object is constructed and inserted in the primary cache, and the compressed block is inserted into the nvm cache by calling ```InsertSaved```. This avoids the overhead of recompressing the block, as well as avoiding putting more memory pressure on the compressed secondary cache. 4. When there's a hit in the nvm cache, we attempt to insert the block in the compressed secondary cache and the primary cache, subject to the admission policy of those caches (i.e admit on second access). Blocks/items evicted from any tier are simply discarded. We can easily implement additional admission policies if desired. Todo (In a subsequent PR): 1. Add to db_bench and run benchmarks 2. Add to db_stress Pull Request resolved: https://github.com/facebook/rocksdb/pull/11812 Reviewed By: pdillinger Differential Revision: D49461842 Pulled By: anand1976 fbshipit-source-id: b40ac1330ef7cd8c12efa0a3ca75128e602e3a0b
98 lines
3.4 KiB
C++
98 lines
3.4 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).
|
|
|
|
#include "test_util/secondary_cache_test_util.h"
|
|
|
|
#include <array>
|
|
|
|
namespace ROCKSDB_NAMESPACE {
|
|
|
|
namespace secondary_cache_test_util {
|
|
|
|
namespace {
|
|
using TestItem = WithCacheType::TestItem;
|
|
|
|
size_t SizeCallback(Cache::ObjectPtr obj) {
|
|
return static_cast<TestItem*>(obj)->Size();
|
|
}
|
|
|
|
Status SaveToCallback(Cache::ObjectPtr from_obj, size_t from_offset,
|
|
size_t length, char* out) {
|
|
auto item = static_cast<TestItem*>(from_obj);
|
|
const char* buf = item->Buf();
|
|
EXPECT_EQ(length, item->Size());
|
|
EXPECT_EQ(from_offset, 0);
|
|
memcpy(out, buf, length);
|
|
return Status::OK();
|
|
}
|
|
|
|
void DeletionCallback(Cache::ObjectPtr obj, MemoryAllocator* /*alloc*/) {
|
|
delete static_cast<TestItem*>(obj);
|
|
}
|
|
|
|
Status SaveToCallbackFail(Cache::ObjectPtr /*obj*/, size_t /*offset*/,
|
|
size_t /*size*/, char* /*out*/) {
|
|
return Status::NotSupported();
|
|
}
|
|
|
|
Status CreateCallback(const Slice& data, CompressionType /*type*/,
|
|
CacheTier /*source*/, Cache::CreateContext* context,
|
|
MemoryAllocator* /*allocator*/, Cache::ObjectPtr* out_obj,
|
|
size_t* out_charge) {
|
|
auto t = static_cast<TestCreateContext*>(context);
|
|
if (t->fail_create_) {
|
|
return Status::NotSupported();
|
|
}
|
|
*out_obj = new TestItem(data.data(), data.size());
|
|
*out_charge = data.size();
|
|
return Status::OK();
|
|
}
|
|
|
|
// If helpers without_secondary are provided, returns helpers with secondary
|
|
// support. If not provided, returns helpers without secondary support.
|
|
auto GenerateHelpersByRole(
|
|
const std::array<Cache::CacheItemHelper, kNumCacheEntryRoles>*
|
|
without_secondary,
|
|
bool fail) {
|
|
std::array<Cache::CacheItemHelper, kNumCacheEntryRoles> a;
|
|
for (uint32_t i = 0; i < kNumCacheEntryRoles; ++i) {
|
|
if (without_secondary) {
|
|
a[i] =
|
|
Cache::CacheItemHelper{static_cast<CacheEntryRole>(i),
|
|
&DeletionCallback,
|
|
&SizeCallback,
|
|
fail ? &SaveToCallbackFail : &SaveToCallback,
|
|
&CreateCallback,
|
|
&(*without_secondary)[i]};
|
|
} else {
|
|
a[i] = Cache::CacheItemHelper{static_cast<CacheEntryRole>(i),
|
|
&DeletionCallback};
|
|
}
|
|
}
|
|
return a;
|
|
}
|
|
} // namespace
|
|
|
|
const Cache::CacheItemHelper* WithCacheType::GetHelper(
|
|
CacheEntryRole r, bool secondary_compatible, bool fail) {
|
|
static const std::array<Cache::CacheItemHelper, kNumCacheEntryRoles>
|
|
without_secondary = GenerateHelpersByRole(nullptr, false);
|
|
static const std::array<Cache::CacheItemHelper, kNumCacheEntryRoles>
|
|
with_secondary = GenerateHelpersByRole(&without_secondary, false);
|
|
static const std::array<Cache::CacheItemHelper, kNumCacheEntryRoles>
|
|
with_secondary_fail = GenerateHelpersByRole(&without_secondary, true);
|
|
return &(fail ? with_secondary_fail
|
|
: secondary_compatible ? with_secondary
|
|
: without_secondary)[static_cast<int>(r)];
|
|
}
|
|
|
|
const Cache::CacheItemHelper* WithCacheType::GetHelperFail(CacheEntryRole r) {
|
|
return GetHelper(r, true, true);
|
|
}
|
|
|
|
} // namespace secondary_cache_test_util
|
|
|
|
} // namespace ROCKSDB_NAMESPACE
|