charged_cache.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright (c) Meta Platforms, Inc. and affiliates.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #include "cache/charged_cache.h"
  6. #include "cache/cache_reservation_manager.h"
  7. namespace ROCKSDB_NAMESPACE {
  8. ChargedCache::ChargedCache(std::shared_ptr<Cache> cache,
  9. std::shared_ptr<Cache> block_cache)
  10. : CacheWrapper(cache),
  11. cache_res_mgr_(std::make_shared<ConcurrentCacheReservationManager>(
  12. std::make_shared<
  13. CacheReservationManagerImpl<CacheEntryRole::kBlobCache>>(
  14. block_cache))) {}
  15. Status ChargedCache::Insert(const Slice& key, ObjectPtr obj,
  16. const CacheItemHelper* helper, size_t charge,
  17. Handle** handle, Priority priority,
  18. const Slice& compressed_val, CompressionType type) {
  19. Status s = target_->Insert(key, obj, helper, charge, handle, priority,
  20. compressed_val, type);
  21. if (s.ok()) {
  22. // Insert may cause the cache entry eviction if the cache is full. So we
  23. // directly call the reservation manager to update the total memory used
  24. // in the cache.
  25. assert(cache_res_mgr_);
  26. cache_res_mgr_->UpdateCacheReservation(target_->GetUsage())
  27. .PermitUncheckedError();
  28. }
  29. return s;
  30. }
  31. Cache::Handle* ChargedCache::Lookup(const Slice& key,
  32. const CacheItemHelper* helper,
  33. CreateContext* create_context,
  34. Priority priority, Statistics* stats) {
  35. auto handle = target_->Lookup(key, helper, create_context, priority, stats);
  36. // Lookup may promote the KV pair from the secondary cache to the primary
  37. // cache. So we directly call the reservation manager to update the total
  38. // memory used in the cache.
  39. if (helper && helper->create_cb) {
  40. assert(cache_res_mgr_);
  41. cache_res_mgr_->UpdateCacheReservation(target_->GetUsage())
  42. .PermitUncheckedError();
  43. }
  44. return handle;
  45. }
  46. void ChargedCache::WaitAll(AsyncLookupHandle* async_handles, size_t count) {
  47. target_->WaitAll(async_handles, count);
  48. // In case of any promotions. Although some could finish by return of
  49. // StartAsyncLookup, Wait/WaitAll will generally be used, so simpler to
  50. // update here.
  51. assert(cache_res_mgr_);
  52. cache_res_mgr_->UpdateCacheReservation(target_->GetUsage())
  53. .PermitUncheckedError();
  54. }
  55. bool ChargedCache::Release(Cache::Handle* handle, bool useful,
  56. bool erase_if_last_ref) {
  57. size_t memory_used_delta = target_->GetUsage(handle);
  58. bool erased = target_->Release(handle, useful, erase_if_last_ref);
  59. if (erased) {
  60. assert(cache_res_mgr_);
  61. cache_res_mgr_
  62. ->UpdateCacheReservation(memory_used_delta, /* increase */ false)
  63. .PermitUncheckedError();
  64. }
  65. return erased;
  66. }
  67. bool ChargedCache::Release(Cache::Handle* handle, bool erase_if_last_ref) {
  68. size_t memory_used_delta = target_->GetUsage(handle);
  69. bool erased = target_->Release(handle, erase_if_last_ref);
  70. if (erased) {
  71. assert(cache_res_mgr_);
  72. cache_res_mgr_
  73. ->UpdateCacheReservation(memory_used_delta, /* increase */ false)
  74. .PermitUncheckedError();
  75. }
  76. return erased;
  77. }
  78. void ChargedCache::Erase(const Slice& key) {
  79. target_->Erase(key);
  80. assert(cache_res_mgr_);
  81. cache_res_mgr_->UpdateCacheReservation(target_->GetUsage())
  82. .PermitUncheckedError();
  83. }
  84. void ChargedCache::EraseUnRefEntries() {
  85. target_->EraseUnRefEntries();
  86. assert(cache_res_mgr_);
  87. cache_res_mgr_->UpdateCacheReservation(target_->GetUsage())
  88. .PermitUncheckedError();
  89. }
  90. void ChargedCache::SetCapacity(size_t capacity) {
  91. target_->SetCapacity(capacity);
  92. // SetCapacity can result in evictions when the cache capacity is decreased,
  93. // so we would want to update the cache reservation here as well.
  94. assert(cache_res_mgr_);
  95. cache_res_mgr_->UpdateCacheReservation(target_->GetUsage())
  96. .PermitUncheckedError();
  97. }
  98. } // namespace ROCKSDB_NAMESPACE