Add a dedicated cache entry role for blobs (#10601)

Summary:
The patch adds a dedicated cache entry role for blob values and switches
to a registered deleter so that blobs show up as a separate bucket
(as opposed to "Misc") in the cache occupancy statistics, e.g.

```
Block cache entry stats(count,size,portion): DataBlock(133515,531.73 MB,13.6866%) BlobValue(1824855,3.10 GB,81.7071%) Misc(1,0.00 KB,0%)
```

Pull Request resolved: https://github.com/facebook/rocksdb/pull/10601

Test Plan: Ran `make check` and tested the cache occupancy statistics using `db_bench`.

Reviewed By: riversand963

Differential Revision: D39107915

Pulled By: ltamasi

fbshipit-source-id: 8446c3b190a41a144030df73f318eeda4398c125
This commit is contained in:
Levi Tamasi 2022-08-29 16:11:59 -07:00 committed by Facebook GitHub Bot
parent 72a3fb3424
commit 7818560194
6 changed files with 22 additions and 13 deletions

View File

@ -23,6 +23,7 @@ std::array<std::string, kNumCacheEntryRoles> kCacheEntryRoleToCamelString{{
"FilterConstruction", "FilterConstruction",
"BlockBasedTableReader", "BlockBasedTableReader",
"FileMetadata", "FileMetadata",
"BlobValue",
"BlobCache", "BlobCache",
"Misc", "Misc",
}}; }};
@ -39,6 +40,7 @@ std::array<std::string, kNumCacheEntryRoles> kCacheEntryRoleToHyphenString{{
"filter-construction", "filter-construction",
"block-based-table-reader", "block-based-table-reader",
"file-metadata", "file-metadata",
"blob-value",
"blob-cache", "blob-cache",
"misc", "misc",
}}; }};

View File

@ -7,6 +7,7 @@
#include <cassert> #include <cassert>
#include "cache/cache_entry_roles.h"
#include "cache/cache_helpers.h" #include "cache/cache_helpers.h"
#include "port/malloc.h" #include "port/malloc.h"
@ -62,13 +63,10 @@ Status BlobContents::SaveToCallback(void* from_obj, size_t from_offset,
return Status::OK(); return Status::OK();
} }
void BlobContents::DeleteCallback(const Slice& key, void* value) {
DeleteCacheEntry<BlobContents>(key, value);
}
Cache::CacheItemHelper* BlobContents::GetCacheItemHelper() { Cache::CacheItemHelper* BlobContents::GetCacheItemHelper() {
static Cache::CacheItemHelper cache_helper(&SizeCallback, &SaveToCallback, static Cache::CacheItemHelper cache_helper(
&DeleteCallback); &SizeCallback, &SaveToCallback,
GetCacheEntryDeleterForRole<BlobContents, CacheEntryRole::kBlobValue>());
return &cache_helper; return &cache_helper;
} }

View File

@ -40,8 +40,6 @@ class BlobContents {
static Status SaveToCallback(void* from_obj, size_t from_offset, static Status SaveToCallback(void* from_obj, size_t from_offset,
size_t length, void* out); size_t length, void* out);
static void DeleteCallback(const Slice& key, void* value);
static Cache::CacheItemHelper* GetCacheItemHelper(); static Cache::CacheItemHelper* GetCacheItemHelper();
static Status CreateCallback(const void* buf, size_t size, void** out_obj, static Status CreateCallback(const void* buf, size_t size, void** out_obj,

View File

@ -416,8 +416,12 @@ Status BlobFileBuilder::PutBlobIntoCacheIfNeeded(const Slice& blob,
std::unique_ptr<BlobContents> buf = std::unique_ptr<BlobContents> buf =
BlobContents::Create(std::move(allocation), blob.size()); BlobContents::Create(std::move(allocation), blob.size());
Cache::CacheItemHelper* const cache_item_helper =
BlobContents::GetCacheItemHelper();
assert(cache_item_helper);
s = blob_cache->Insert(key, buf.get(), buf->ApproximateMemoryUsage(), s = blob_cache->Insert(key, buf.get(), buf->ApproximateMemoryUsage(),
&BlobContents::DeleteCallback, cache_item_helper->del_cb,
nullptr /* cache_handle */, priority); nullptr /* cache_handle */, priority);
if (s.ok()) { if (s.ok()) {
RecordTick(statistics, BLOB_DB_CACHE_ADD); RecordTick(statistics, BLOB_DB_CACHE_ADD);

View File

@ -128,11 +128,15 @@ Status BlobSource::InsertEntryIntoCache(const Slice& key, BlobContents* value,
Cache::Priority priority) const { Cache::Priority priority) const {
Status s; Status s;
Cache::CacheItemHelper* const cache_item_helper =
BlobContents::GetCacheItemHelper();
assert(cache_item_helper);
if (lowest_used_cache_tier_ == CacheTier::kNonVolatileBlockTier) { if (lowest_used_cache_tier_ == CacheTier::kNonVolatileBlockTier) {
s = blob_cache_->Insert(key, value, BlobContents::GetCacheItemHelper(), s = blob_cache_->Insert(key, value, cache_item_helper, charge, cache_handle,
charge, cache_handle, priority); priority);
} else { } else {
s = blob_cache_->Insert(key, value, charge, &BlobContents::DeleteCallback, s = blob_cache_->Insert(key, value, charge, cache_item_helper->del_cb,
cache_handle, priority); cache_handle, priority);
} }

View File

@ -600,7 +600,10 @@ enum class CacheEntryRole {
kBlockBasedTableReader, kBlockBasedTableReader,
// FileMetadata's charge to account for its memory usage // FileMetadata's charge to account for its memory usage
kFileMetadata, kFileMetadata,
// Blob cache's charge to account for its memory usage // Blob value (when using the same cache as block cache and blob cache)
kBlobValue,
// Blob cache's charge to account for its memory usage (when using a
// separate block cache and blob cache)
kBlobCache, kBlobCache,
// Default bucket, for miscellaneous cache entries. Do not use for // Default bucket, for miscellaneous cache entries. Do not use for
// entries that could potentially add up to large usage. // entries that could potentially add up to large usage.