File: secondary_cache_adapter.h

package info (click to toggle)
rocksdb 9.11.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 46,252 kB
  • sloc: cpp: 503,390; java: 43,039; ansic: 9,834; python: 8,381; perl: 5,822; sh: 4,921; makefile: 2,386; asm: 550; xml: 342
file content (103 lines) | stat: -rw-r--r-- 3,895 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//  Copyright (c) Meta Platforms, Inc. and affiliates.
//  This source code is licensed under both the GPLv2 (found in the
//  COPYING file in the root directory) and Apache 2.0 License
//  (found in the LICENSE.Apache file in the root directory).

#pragma once

#include "cache/cache_reservation_manager.h"
#include "rocksdb/secondary_cache.h"

namespace ROCKSDB_NAMESPACE {

class CacheWithSecondaryAdapter : public CacheWrapper {
 public:
  explicit CacheWithSecondaryAdapter(
      std::shared_ptr<Cache> target,
      std::shared_ptr<SecondaryCache> secondary_cache,
      TieredAdmissionPolicy adm_policy = TieredAdmissionPolicy::kAdmPolicyAuto,
      bool distribute_cache_res = false);

  ~CacheWithSecondaryAdapter() override;

  Status Insert(
      const Slice& key, ObjectPtr value, const CacheItemHelper* helper,
      size_t charge, Handle** handle = nullptr,
      Priority priority = Priority::LOW,
      const Slice& compressed_value = Slice(),
      CompressionType type = CompressionType::kNoCompression) override;

  Handle* Lookup(const Slice& key, const CacheItemHelper* helper,
                 CreateContext* create_context,
                 Priority priority = Priority::LOW,
                 Statistics* stats = nullptr) override;

  using Cache::Release;
  bool Release(Handle* handle, bool erase_if_last_ref = false) override;

  ObjectPtr Value(Handle* handle) override;

  void StartAsyncLookup(AsyncLookupHandle& async_handle) override;

  void WaitAll(AsyncLookupHandle* async_handles, size_t count) override;

  std::string GetPrintableOptions() const override;

  const char* Name() const override;

  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);

  Cache* TEST_GetCache() { return target_.get(); }

  SecondaryCache* TEST_GetSecondaryCache() { return secondary_cache_.get(); }

 private:
  static constexpr size_t kReservationChunkSize = 1 << 20;

  bool EvictionHandler(const Slice& key, Handle* handle, bool was_hit);

  void StartAsyncLookupOnMySecondary(AsyncLookupHandle& async_handle);

  Handle* Promote(
      std::unique_ptr<SecondaryCacheResultHandle>&& secondary_handle,
      const Slice& key, const CacheItemHelper* helper, Priority priority,
      Statistics* stats, bool found_dummy_entry, bool kept_in_sec_cache);

  bool ProcessDummyResult(Cache::Handle** handle, bool erase);

  void CleanupCacheObject(ObjectPtr obj, const CacheItemHelper* helper);

  std::shared_ptr<SecondaryCache> secondary_cache_;
  TieredAdmissionPolicy adm_policy_;
  // Whether to proportionally distribute cache memory reservations, i.e
  // placeholder entries with null value and a non-zero charge, across
  // the primary and secondary caches.
  bool distribute_cache_res_;
  // A cache reservation manager to keep track of secondary cache memory
  // usage by reserving equivalent capacity against the primary cache
  std::shared_ptr<ConcurrentCacheReservationManager> pri_cache_res_;
  // Fraction of a cache memory reservation to be assigned to the secondary
  // cache
  double sec_cache_res_ratio_;
  // Mutex for use when managing cache memory reservations. Should not be used
  // for other purposes, as it may risk causing deadlocks.
  mutable port::Mutex cache_res_mutex_;
  // Total memory reserved by placeholder entriesin the cache
  size_t placeholder_usage_;
  // Total placeholoder memory charged to both the primary and secondary
  // caches. Will be <= placeholder_usage_.
  size_t reserved_usage_;
  // Amount of memory reserved in the secondary cache. This should be
  // reserved_usage_ * sec_cache_res_ratio_ in steady state.
  size_t sec_reserved_;
};

}  // namespace ROCKSDB_NAMESPACE