Coverage Report

Created: 2024-07-27 06:53

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