secondary_cache_adapter.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #pragma once
  6. #include "cache/cache_reservation_manager.h"
  7. #include "rocksdb/secondary_cache.h"
  8. namespace ROCKSDB_NAMESPACE {
  9. class CacheWithSecondaryAdapter : public CacheWrapper {
  10. public:
  11. explicit CacheWithSecondaryAdapter(
  12. std::shared_ptr<Cache> target,
  13. std::shared_ptr<SecondaryCache> secondary_cache,
  14. TieredAdmissionPolicy adm_policy = TieredAdmissionPolicy::kAdmPolicyAuto,
  15. bool distribute_cache_res = false);
  16. ~CacheWithSecondaryAdapter() override;
  17. Status Insert(
  18. const Slice& key, ObjectPtr value, const CacheItemHelper* helper,
  19. size_t charge, Handle** handle = nullptr,
  20. Priority priority = Priority::LOW,
  21. const Slice& compressed_value = Slice(),
  22. CompressionType type = CompressionType::kNoCompression) override;
  23. Handle* Lookup(const Slice& key, const CacheItemHelper* helper,
  24. CreateContext* create_context,
  25. Priority priority = Priority::LOW,
  26. Statistics* stats = nullptr) override;
  27. using Cache::Release;
  28. bool Release(Handle* handle, bool erase_if_last_ref = false) override;
  29. ObjectPtr Value(Handle* handle) override;
  30. void StartAsyncLookup(AsyncLookupHandle& async_handle) override;
  31. void WaitAll(AsyncLookupHandle* async_handles, size_t count) override;
  32. std::string GetPrintableOptions() const override;
  33. const char* Name() const override;
  34. void SetCapacity(size_t capacity) override;
  35. Status GetSecondaryCacheCapacity(size_t& size) const override;
  36. Status GetSecondaryCachePinnedUsage(size_t& size) const override;
  37. Status UpdateCacheReservationRatio(double ratio);
  38. Status UpdateAdmissionPolicy(TieredAdmissionPolicy adm_policy);
  39. Cache* TEST_GetCache() { return target_.get(); }
  40. SecondaryCache* TEST_GetSecondaryCache() { return secondary_cache_.get(); }
  41. private:
  42. static constexpr size_t kReservationChunkSize = 1 << 20;
  43. bool EvictionHandler(const Slice& key, Handle* handle, bool was_hit);
  44. void StartAsyncLookupOnMySecondary(AsyncLookupHandle& async_handle);
  45. Handle* Promote(
  46. std::unique_ptr<SecondaryCacheResultHandle>&& secondary_handle,
  47. const Slice& key, const CacheItemHelper* helper, Priority priority,
  48. Statistics* stats, bool found_dummy_entry, bool kept_in_sec_cache);
  49. bool ProcessDummyResult(Cache::Handle** handle, bool erase);
  50. void CleanupCacheObject(ObjectPtr obj, const CacheItemHelper* helper);
  51. std::shared_ptr<SecondaryCache> secondary_cache_;
  52. TieredAdmissionPolicy adm_policy_;
  53. // Whether to proportionally distribute cache memory reservations, i.e
  54. // placeholder entries with null value and a non-zero charge, across
  55. // the primary and secondary caches.
  56. bool distribute_cache_res_;
  57. // A cache reservation manager to keep track of secondary cache memory
  58. // usage by reserving equivalent capacity against the primary cache
  59. std::shared_ptr<ConcurrentCacheReservationManager> pri_cache_res_;
  60. // Fraction of a cache memory reservation to be assigned to the secondary
  61. // cache
  62. double sec_cache_res_ratio_;
  63. // Mutex for use when managing cache memory reservations. Should not be used
  64. // for other purposes, as it may risk causing deadlocks.
  65. mutable port::Mutex cache_res_mutex_;
  66. // Total memory reserved by placeholder entriesin the cache
  67. size_t placeholder_usage_;
  68. // Total placeholoder memory charged to both the primary and secondary
  69. // caches. Will be <= placeholder_usage_.
  70. size_t reserved_usage_;
  71. // Amount of memory reserved in the secondary cache. This should be
  72. // reserved_usage_ * sec_cache_res_ratio_ in steady state.
  73. size_t sec_reserved_;
  74. };
  75. } // namespace ROCKSDB_NAMESPACE