volatile_tier_impl.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright (c) 2013, Facebook, Inc. All rights reserved.
  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. #ifndef ROCKSDB_LITE
  8. #include <atomic>
  9. #include <limits>
  10. #include <sstream>
  11. #include <string>
  12. #include <vector>
  13. #include "rocksdb/cache.h"
  14. #include "utilities/persistent_cache/hash_table.h"
  15. #include "utilities/persistent_cache/hash_table_evictable.h"
  16. #include "utilities/persistent_cache/persistent_cache_tier.h"
  17. // VolatileCacheTier
  18. //
  19. // This file provides persistent cache tier implementation for caching
  20. // key/values in RAM.
  21. //
  22. // key/values
  23. // |
  24. // V
  25. // +-------------------+
  26. // | VolatileCacheTier | Store in an evictable hash table
  27. // +-------------------+
  28. // |
  29. // V
  30. // on eviction
  31. // pushed to next tier
  32. //
  33. // The implementation is designed to be concurrent. The evictable hash table
  34. // implementation is not concurrent at this point though.
  35. //
  36. // The eviction algorithm is LRU
  37. namespace ROCKSDB_NAMESPACE {
  38. class VolatileCacheTier : public PersistentCacheTier {
  39. public:
  40. explicit VolatileCacheTier(
  41. const bool is_compressed = true,
  42. const size_t max_size = std::numeric_limits<size_t>::max())
  43. : is_compressed_(is_compressed), max_size_(max_size) {}
  44. virtual ~VolatileCacheTier();
  45. // insert to cache
  46. Status Insert(const Slice& page_key, const char* data,
  47. const size_t size) override;
  48. // lookup key in cache
  49. Status Lookup(const Slice& page_key, std::unique_ptr<char[]>* data,
  50. size_t* size) override;
  51. // is compressed cache ?
  52. bool IsCompressed() override { return is_compressed_; }
  53. // erase key from cache
  54. bool Erase(const Slice& key) override;
  55. std::string GetPrintableOptions() const override {
  56. return "VolatileCacheTier";
  57. }
  58. // Expose stats as map
  59. PersistentCache::StatsType Stats() override;
  60. private:
  61. //
  62. // Cache data abstraction
  63. //
  64. struct CacheData : LRUElement<CacheData> {
  65. explicit CacheData(CacheData&& rhs) ROCKSDB_NOEXCEPT
  66. : key(std::move(rhs.key)),
  67. value(std::move(rhs.value)) {}
  68. explicit CacheData(const std::string& _key, const std::string& _value = "")
  69. : key(_key), value(_value) {}
  70. virtual ~CacheData() {}
  71. const std::string key;
  72. const std::string value;
  73. };
  74. static void DeleteCacheData(CacheData* data);
  75. //
  76. // Index and LRU definition
  77. //
  78. struct CacheDataHash {
  79. uint64_t operator()(const CacheData* obj) const {
  80. assert(obj);
  81. return std::hash<std::string>()(obj->key);
  82. }
  83. };
  84. struct CacheDataEqual {
  85. bool operator()(const CacheData* lhs, const CacheData* rhs) const {
  86. assert(lhs);
  87. assert(rhs);
  88. return lhs->key == rhs->key;
  89. }
  90. };
  91. struct Statistics {
  92. std::atomic<uint64_t> cache_misses_{0};
  93. std::atomic<uint64_t> cache_hits_{0};
  94. std::atomic<uint64_t> cache_inserts_{0};
  95. std::atomic<uint64_t> cache_evicts_{0};
  96. double CacheHitPct() const {
  97. auto lookups = cache_hits_ + cache_misses_;
  98. return lookups ? 100 * cache_hits_ / static_cast<double>(lookups) : 0.0;
  99. }
  100. double CacheMissPct() const {
  101. auto lookups = cache_hits_ + cache_misses_;
  102. return lookups ? 100 * cache_misses_ / static_cast<double>(lookups) : 0.0;
  103. }
  104. };
  105. typedef EvictableHashTable<CacheData, CacheDataHash, CacheDataEqual>
  106. IndexType;
  107. // Evict LRU tail
  108. bool Evict();
  109. const bool is_compressed_ = true; // does it store compressed data
  110. IndexType index_; // in-memory cache
  111. std::atomic<uint64_t> max_size_{0}; // Maximum size of the cache
  112. std::atomic<uint64_t> size_{0}; // Size of the cache
  113. Statistics stats_;
  114. };
  115. } // namespace ROCKSDB_NAMESPACE
  116. #endif