Skip to content

Commit

Permalink
Implement secondary cache admission policy to allow all evicted blocks (
Browse files Browse the repository at this point in the history
#12599)

Summary:
Add a secondary cache admission policy to admit all blocks evicted from the block cache.

Pull Request resolved: #12599

Reviewed By: pdillinger

Differential Revision: D56891760

Pulled By: anand1976

fbshipit-source-id: 193c98c055aa3477f4e3a78e5d3daef27a5eacf4
  • Loading branch information
anand1976 authored and facebook-github-bot committed May 2, 2024
1 parent 6349da6 commit 6cc7ad1
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 4 deletions.
9 changes: 6 additions & 3 deletions cache/secondary_cache_adapter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,14 @@ bool CacheWithSecondaryAdapter::EvictionHandler(const Slice& key,
auto obj = target_->Value(handle);
// Ignore dummy entry
if (obj != kDummyObj) {
bool hit = false;
bool force = false;
if (adm_policy_ == TieredAdmissionPolicy::kAdmPolicyAllowCacheHits) {
hit = was_hit;
force = was_hit;
} else if (adm_policy_ == TieredAdmissionPolicy::kAdmPolicyAllowAll) {
force = true;
}
// Spill into secondary cache.
secondary_cache_->Insert(key, obj, helper, hit).PermitUncheckedError();
secondary_cache_->Insert(key, obj, helper, force).PermitUncheckedError();
}
}
// Never takes ownership of obj
Expand Down Expand Up @@ -661,6 +663,7 @@ std::shared_ptr<Cache> NewTieredCache(const TieredCacheOptions& _opts) {
break;
case TieredAdmissionPolicy::kAdmPolicyPlaceholder:
case TieredAdmissionPolicy::kAdmPolicyAllowCacheHits:
case TieredAdmissionPolicy::kAdmPolicyAllowAll:
if (opts.nvm_sec_cache) {
valid_adm_policy = false;
}
Expand Down
60 changes: 59 additions & 1 deletion cache/tiered_secondary_cache_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -816,11 +816,69 @@ TEST_P(DBTieredAdmPolicyTest, CompressedOnlyTest) {
Destroy(options);
}

TEST_P(DBTieredAdmPolicyTest, CompressedCacheAdmission) {
if (!LZ4_Supported()) {
ROCKSDB_GTEST_SKIP("This test requires LZ4 support.");
return;
}

BlockBasedTableOptions table_options;
// We want a block cache of size 5KB, and a compressed secondary cache of
// size 5KB. However, we specify a block cache size of 256KB here in order
// to take into account the cache reservation in the block cache on
// behalf of the compressed cache. The unit of cache reservation is 256KB.
// The effective block cache capacity will be calculated as 256 + 5 = 261KB,
// and 256KB will be reserved for the compressed cache, leaving 10KB for
// the primary block cache. We only have to worry about this here because
// the cache size is so small.
table_options.block_cache = NewCache(256 * 1024, 5 * 1024, 0, GetParam());
table_options.block_size = 4 * 1024;
table_options.cache_index_and_filter_blocks = false;
Options options = GetDefaultOptions();
options.create_if_missing = true;
options.table_factory.reset(NewBlockBasedTableFactory(table_options));

size_t comp_cache_usage = compressed_secondary_cache()->TEST_GetUsage();
// Disable paranoid_file_checks so that flush will not read back the newly
// written file
options.paranoid_file_checks = false;
DestroyAndReopen(options);
Random rnd(301);
const int N = 256;
for (int i = 0; i < N; i++) {
std::string p_v;
test::CompressibleString(&rnd, 0.5, 1007, &p_v);
ASSERT_OK(Put(Key(i), p_v));
}

ASSERT_OK(Flush());

// The second Get (for 5) will evict the data block loaded by the first
// Get, which will be admitted into the compressed secondary cache only
// for the kAdmPolicyAllowAll policy
std::string v = Get(Key(0));
ASSERT_EQ(1007, v.size());

v = Get(Key(5));
ASSERT_EQ(1007, v.size());

if (GetParam() == TieredAdmissionPolicy::kAdmPolicyAllowAll) {
ASSERT_GT(compressed_secondary_cache()->TEST_GetUsage(),
comp_cache_usage + 128);
} else {
ASSERT_LT(compressed_secondary_cache()->TEST_GetUsage(),
comp_cache_usage + 128);
}

Destroy(options);
}

INSTANTIATE_TEST_CASE_P(
DBTieredAdmPolicyTest, DBTieredAdmPolicyTest,
::testing::Values(TieredAdmissionPolicy::kAdmPolicyAuto,
TieredAdmissionPolicy::kAdmPolicyPlaceholder,
TieredAdmissionPolicy::kAdmPolicyAllowCacheHits));
TieredAdmissionPolicy::kAdmPolicyAllowCacheHits,
TieredAdmissionPolicy::kAdmPolicyAllowAll));

} // namespace ROCKSDB_NAMESPACE

Expand Down
5 changes: 5 additions & 0 deletions include/rocksdb/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,11 @@ enum TieredAdmissionPolicy {
// compressed secondary, and a compressed local flash (non-volatile) cache.
// Each tier is managed as an independent queue.
kAdmPolicyThreeQueue,
// Allow all blocks evicted from the primary block cache into the secondary
// cache. This may increase CPU overhead due to more blocks being admitted
// and compressed, but may increase the compressed secondary cache hit rate
// for some workloads
kAdmPolicyAllowAll,
kAdmPolicyMax,
};

Expand Down
2 changes: 2 additions & 0 deletions tools/db_bench_tool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1299,6 +1299,8 @@ static enum ROCKSDB_NAMESPACE::TieredAdmissionPolicy StringToAdmissionPolicy(
return ROCKSDB_NAMESPACE::kAdmPolicyAllowCacheHits;
} else if (!strcasecmp(policy, "three_queue")) {
return ROCKSDB_NAMESPACE::kAdmPolicyThreeQueue;
} else if (!strcasecmp(policy, "allow_all")) {
return ROCKSDB_NAMESPACE::kAdmPolicyAllowAll;
} else {
fprintf(stderr, "Cannot parse admission policy %s\n", policy);
exit(1);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add a kAdmPolicyAllowAll option to TieredAdmissionPolicy that admits all blocks evicted from the primary block cache into the compressed secondary cache.

0 comments on commit 6cc7ad1

Please sign in to comment.