Skip to content

Commit

Permalink
Add APIs to query secondary cache capacity and usage for TieredCache (#…
Browse files Browse the repository at this point in the history
…12011)

Summary:
In `TieredCache`, the underlying compressed secondary cache is hidden from the user. So we need a way to query the capacity, as well as the portion of cache reservation charged to the compressed secondary cache.

Pull Request resolved: #12011

Test Plan: Update the unit tests

Reviewed By: akankshamahajan15

Differential Revision: D50651943

Pulled By: anand1976

fbshipit-source-id: 06d1cb5edb75a790c919bce718e2ff65f5908220
  • Loading branch information
anand1976 authored and facebook-github-bot committed Oct 25, 2023
1 parent 8ee009f commit 52be8f5
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 1 deletion.
7 changes: 7 additions & 0 deletions cache/compressed_secondary_cache_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1276,6 +1276,13 @@ TEST_P(CompressedSecCacheTestWithTiered, DynamicUpdateWithReservation) {
ASSERT_OK(sec_cache->GetCapacity(sec_capacity));
ASSERT_EQ(sec_capacity, (30 << 20));

ASSERT_OK(tiered_cache->GetSecondaryCacheCapacity(sec_capacity));
ASSERT_EQ(sec_capacity, 30 << 20);
size_t sec_usage;
ASSERT_OK(tiered_cache->GetSecondaryCachePinnedUsage(sec_usage));
EXPECT_PRED3(CacheUsageWithinBounds, sec_usage, 3 << 20,
GetPercent(3 << 20, 1));

ASSERT_OK(UpdateTieredCache(tiered_cache, -1, 0.39));
EXPECT_PRED3(CacheUsageWithinBounds, GetCache()->GetUsage(), (45 << 20),
GetPercent(45 << 20, 1));
Expand Down
23 changes: 23 additions & 0 deletions cache/secondary_cache_adapter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,29 @@ void CacheWithSecondaryAdapter::SetCapacity(size_t capacity) {
}
}

Status CacheWithSecondaryAdapter::GetSecondaryCacheCapacity(
size_t& size) const {
return secondary_cache_->GetCapacity(size);
}

Status CacheWithSecondaryAdapter::GetSecondaryCachePinnedUsage(
size_t& size) const {
Status s;
if (distribute_cache_res_) {
MutexLock m(&mutex_);
size_t capacity = 0;
s = secondary_cache_->GetCapacity(capacity);
if (s.ok()) {
size = capacity - pri_cache_res_->GetTotalMemoryUsed();
} else {
size = 0;
}
} else {
size = 0;
}
return s;
}

// Update the secondary/primary allocation ratio (remember, the primary
// capacity is the total memory budget when distribute_cache_res_ is true).
// When the ratio changes, we may accumulate some error in the calculations
Expand Down
6 changes: 5 additions & 1 deletion cache/secondary_cache_adapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ class CacheWithSecondaryAdapter : public CacheWrapper {

void SetCapacity(size_t capacity) override;

Status GetSecondaryCacheCapacity(size_t& size) const override;

Status GetSecondaryCachePinnedUsage(size_t& size) const override;

Status UpdateCacheReservationRatio(double ratio);

Status UpdateAdmissionPolicy(TieredAdmissionPolicy adm_policy);
Expand Down Expand Up @@ -81,7 +85,7 @@ class CacheWithSecondaryAdapter : public CacheWrapper {
// Fraction of a cache memory reservation to be assigned to the secondary
// cache
std::atomic<double> sec_cache_res_ratio_;
port::Mutex mutex_;
mutable port::Mutex mutex_;
#ifndef NDEBUG
bool ratio_changed_ = false;
#endif
Expand Down
10 changes: 10 additions & 0 deletions cache/sharded_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ size_t ShardedCacheBase::GetCapacity() const {
return capacity_;
}

Status ShardedCacheBase::GetSecondaryCacheCapacity(size_t& size) const {
size = 0;
return Status::OK();
}

Status ShardedCacheBase::GetSecondaryCachePinnedUsage(size_t& size) const {
size = 0;
return Status::OK();
}

bool ShardedCacheBase::HasStrictCapacityLimit() const {
MutexLock l(&config_mutex_);
return strict_capacity_limit_;
Expand Down
2 changes: 2 additions & 0 deletions cache/sharded_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ class ShardedCacheBase : public Cache {

bool HasStrictCapacityLimit() const override;
size_t GetCapacity() const override;
Status GetSecondaryCacheCapacity(size_t& size) const override;
Status GetSecondaryCachePinnedUsage(size_t& size) const override;

using Cache::GetUsage;
size_t GetUsage(Handle* handle) const override;
Expand Down
8 changes: 8 additions & 0 deletions include/rocksdb/advanced_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,14 @@ class Cache {
// Returns the helper for the specified entry.
virtual const CacheItemHelper* GetCacheItemHelper(Handle* handle) const = 0;

virtual Status GetSecondaryCacheCapacity(size_t& /*size*/) const {
return Status::NotSupported();
}

virtual Status GetSecondaryCachePinnedUsage(size_t& /*size*/) const {
return Status::NotSupported();
}

// Call this on shutdown if you want to speed it up. Cache will disown
// any underlying data and will not free it on delete. This call will leak
// memory - call this only if you're shutting down the process.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add new Cache APIs GetSecondaryCacheCapacity() and GetSecondaryCachePinnedUsage() to return the configured capacity, and cache reservation charged to the secondary cache.

0 comments on commit 52be8f5

Please sign in to comment.