secondary_cache_test_util.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 <gtest/gtest.h>
  7. #include <functional>
  8. #include "rocksdb/advanced_cache.h"
  9. namespace ROCKSDB_NAMESPACE {
  10. namespace secondary_cache_test_util {
  11. struct TestCreateContext : public Cache::CreateContext {
  12. void SetFailCreate(bool fail) { fail_create_ = fail; }
  13. bool fail_create_ = false;
  14. };
  15. class WithCacheType : public TestCreateContext {
  16. public:
  17. WithCacheType() {}
  18. virtual ~WithCacheType() {}
  19. class TestItem {
  20. public:
  21. TestItem(const char* buf, size_t size) : buf_(new char[size]), size_(size) {
  22. memcpy(buf_.get(), buf, size);
  23. }
  24. ~TestItem() = default;
  25. char* Buf() { return buf_.get(); }
  26. [[nodiscard]] size_t Size() const { return size_; }
  27. std::string ToString() { return std::string(Buf(), Size()); }
  28. private:
  29. std::unique_ptr<char[]> buf_;
  30. size_t size_;
  31. };
  32. static constexpr auto kLRU = "lru";
  33. static constexpr auto kFixedHyperClock = "fixed_hyper_clock";
  34. static constexpr auto kAutoHyperClock = "auto_hyper_clock";
  35. // For options other than capacity
  36. size_t estimated_value_size_ = 1;
  37. virtual const std::string& Type() const = 0;
  38. static bool IsHyperClock(const std::string& type) {
  39. return type == kFixedHyperClock || type == kAutoHyperClock;
  40. }
  41. bool IsHyperClock() const { return IsHyperClock(Type()); }
  42. std::shared_ptr<Cache> NewCache(
  43. size_t capacity,
  44. std::function<void(ShardedCacheOptions&)> modify_opts_fn = {}) {
  45. const auto& type = Type();
  46. if (type == kLRU) {
  47. LRUCacheOptions lru_opts;
  48. lru_opts.capacity = capacity;
  49. lru_opts.hash_seed = 0; // deterministic tests
  50. if (modify_opts_fn) {
  51. modify_opts_fn(lru_opts);
  52. }
  53. return lru_opts.MakeSharedCache();
  54. }
  55. if (IsHyperClock(type)) {
  56. HyperClockCacheOptions hc_opts{
  57. capacity, type == kFixedHyperClock ? estimated_value_size_ : 0};
  58. hc_opts.min_avg_entry_charge =
  59. std::max(size_t{1}, estimated_value_size_ / 2);
  60. hc_opts.hash_seed = 0; // deterministic tests
  61. if (modify_opts_fn) {
  62. modify_opts_fn(hc_opts);
  63. }
  64. return hc_opts.MakeSharedCache();
  65. }
  66. assert(false);
  67. return nullptr;
  68. }
  69. std::shared_ptr<Cache> NewCache(
  70. size_t capacity, int num_shard_bits, bool strict_capacity_limit,
  71. CacheMetadataChargePolicy charge_policy = kDontChargeCacheMetadata) {
  72. return NewCache(capacity, [=](ShardedCacheOptions& opts) {
  73. opts.num_shard_bits = num_shard_bits;
  74. opts.strict_capacity_limit = strict_capacity_limit;
  75. opts.metadata_charge_policy = charge_policy;
  76. });
  77. }
  78. std::shared_ptr<Cache> NewCache(
  79. size_t capacity, int num_shard_bits, bool strict_capacity_limit,
  80. std::shared_ptr<SecondaryCache> secondary_cache) {
  81. return NewCache(capacity, [=](ShardedCacheOptions& opts) {
  82. opts.num_shard_bits = num_shard_bits;
  83. opts.strict_capacity_limit = strict_capacity_limit;
  84. opts.metadata_charge_policy = kDontChargeCacheMetadata;
  85. opts.secondary_cache = secondary_cache;
  86. });
  87. }
  88. static const Cache::CacheItemHelper* GetHelper(
  89. CacheEntryRole r = CacheEntryRole::kDataBlock,
  90. bool secondary_compatible = true, bool fail = false);
  91. static const Cache::CacheItemHelper* GetHelperFail(
  92. CacheEntryRole r = CacheEntryRole::kDataBlock);
  93. };
  94. class WithCacheTypeParam : public WithCacheType,
  95. public testing::WithParamInterface<std::string> {
  96. const std::string& Type() const override { return GetParam(); }
  97. };
  98. constexpr auto kLRU = WithCacheType::kLRU;
  99. constexpr auto kFixedHyperClock = WithCacheType::kFixedHyperClock;
  100. constexpr auto kAutoHyperClock = WithCacheType::kAutoHyperClock;
  101. inline auto GetTestingCacheTypes() {
  102. return testing::Values(std::string(kLRU), std::string(kFixedHyperClock),
  103. std::string(kAutoHyperClock));
  104. }
  105. } // namespace secondary_cache_test_util
  106. } // namespace ROCKSDB_NAMESPACE