This is an automated email from the ASF dual-hosted git repository.

maplefu pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/kvrocks.git


The following commit(s) were added to refs/heads/unstable by this push:
     new 02a62400 Add support of using hyper clock cache as the block cache 
(#2031)
02a62400 is described below

commit 02a62400e29f4dd21a685296098a8a1eb7e3424b
Author: jxlikar <[email protected]>
AuthorDate: Wed Jan 24 17:02:23 2024 +0800

    Add support of using hyper clock cache as the block cache (#2031)
    
    Co-authored-by: mwish <[email protected]>
    Co-authored-by: hulk <[email protected]>
---
 kvrocks.conf           | 11 +++++++++++
 src/config/config.cc   | 11 +++++++++++
 src/config/config.h    |  3 +++
 src/storage/storage.cc | 33 +++++++++++++++++++++++++++++++--
 src/storage/storage.h  | 11 +++++++++++
 5 files changed, 67 insertions(+), 2 deletions(-)

diff --git a/kvrocks.conf b/kvrocks.conf
index 2ea4f519..defd9985 100644
--- a/kvrocks.conf
+++ b/kvrocks.conf
@@ -569,6 +569,17 @@ migrate-sequence-gap 10000
 # Default: 4096MB
 rocksdb.block_cache_size 4096
 
+# Specify the type of cache used in the block cache.
+# Accept value: "lru", "hcc"
+# "lru" stands for the cache with the LRU(Least Recently Used) replacement 
policy.
+#
+# "hcc" stands for the Hyper Clock Cache, a lock-free cache alternative
+# that offers much improved CPU efficiency vs. LRU cache under high parallel
+# load or high contention.
+#
+# default lru
+rocksdb.block_cache_type lru
+
 # A global cache for table-level rows in RocksDB. If almost always point
 # lookups, enlarging row cache may improve read performance. Otherwise,
 # if we enlarge this value, we can lessen metadata/subkey block cache size.
diff --git a/src/config/config.cc b/src/config/config.cc
index 5ea8c617..7a944f6c 100644
--- a/src/config/config.cc
+++ b/src/config/config.cc
@@ -74,6 +74,15 @@ const std::vector<ConfigEnum<rocksdb::CompressionType>> 
compression_types{[] {
   return res;
 }()};
 
+const std::vector<ConfigEnum<BlockCacheType>> cache_types{[] {
+  std::vector<ConfigEnum<BlockCacheType>> res;
+  res.reserve(engine::CacheOptions.size());
+  for (const auto &e : engine::CacheOptions) {
+    res.push_back({e.name, e.type});
+  }
+  return res;
+}()};
+
 std::string TrimRocksDbPrefix(std::string s) {
   if (strncasecmp(s.data(), "rocksdb.", 8) != 0) return s;
   return s.substr(8, s.size() - 8);
@@ -191,6 +200,8 @@ Config::Config() {
       {"rocksdb.stats_dump_period_sec", false, new 
IntField(&rocks_db.stats_dump_period_sec, 0, 0, INT_MAX)},
       {"rocksdb.cache_index_and_filter_blocks", true, new 
YesNoField(&rocks_db.cache_index_and_filter_blocks, true)},
       {"rocksdb.block_cache_size", true, new 
IntField(&rocks_db.block_cache_size, 0, 0, INT_MAX)},
+      {"rocksdb.block_cache_type", true,
+       new EnumField<BlockCacheType>(&rocks_db.block_cache_type, cache_types, 
BlockCacheType::kCacheTypeLRU)},
       {"rocksdb.subkey_block_cache_size", true, new 
IntField(&rocks_db.subkey_block_cache_size, 2048, 0, INT_MAX)},
       {"rocksdb.metadata_block_cache_size", true, new 
IntField(&rocks_db.metadata_block_cache_size, 2048, 0, INT_MAX)},
       {"rocksdb.share_metadata_and_subkey_block_cache", true,
diff --git a/src/config/config.h b/src/config/config.h
index 46e260bc..e69ad5e5 100644
--- a/src/config/config.h
+++ b/src/config/config.h
@@ -54,6 +54,8 @@ constexpr const uint32_t kDefaultPort = 6666;
 
 constexpr const char *kDefaultNamespace = "__namespace";
 
+enum class BlockCacheType { kCacheTypeLRU = 0, kCacheTypeHCC };
+
 struct CompactionCheckerRange {
  public:
   int start;
@@ -169,6 +171,7 @@ struct Config {
     int block_size;
     bool cache_index_and_filter_blocks;
     int block_cache_size;
+    BlockCacheType block_cache_type;
     int metadata_block_cache_size;
     int subkey_block_cache_size;
     bool share_metadata_and_subkey_block_cache;
diff --git a/src/storage/storage.cc b/src/storage/storage.cc
index a600813f..75f6fb6d 100644
--- a/src/storage/storage.cc
+++ b/src/storage/storage.cc
@@ -42,6 +42,7 @@
 #include "event_util.h"
 #include "redis_db.h"
 #include "redis_metadata.h"
+#include "rocksdb/cache.h"
 #include "rocksdb_crc32c.h"
 #include "server/server.h"
 #include "table_properties_collector.h"
@@ -52,6 +53,23 @@ namespace engine {
 
 constexpr const char *kReplicationIdKey = "replication_id_";
 
+// used in creating rocksdb::LRUCache, set `num_shard_bits` to -1 means let 
rocksdb choose a good default shard count
+// based on the capacity and the implementation.
+constexpr int kRocksdbLRUAutoAdjustShardBits = -1;
+
+// used as the default argument for `strict_capacity_limit` in creating 
rocksdb::Cache.
+constexpr bool kRocksdbCacheStrictCapacityLimit = false;
+
+// used as the default argument for `high_pri_pool_ratio` in creating block 
cache.
+constexpr double kRocksdbLRUBlockCacheHighPriPoolRatio = 0.75;
+
+// used as the default argument for `high_pri_pool_ratio` in creating row 
cache.
+constexpr double kRocksdbLRURowCacheHighPriPoolRatio = 0.5;
+
+// used in creating rocksdb::HyperClockCache, set`estimated_entry_charge` to 0 
means let rocksdb dynamically and
+// automacally adjust the table size for the cache.
+constexpr size_t kRockdbHCCAutoAdjustCharge = 0;
+
 const int64_t kIORateLimitMaxMb = 1024000;
 
 using rocksdb::Slice;
@@ -152,9 +170,12 @@ rocksdb::Options Storage::InitRocksDBOptions() {
       options.compression_per_level[i] = config_->rocks_db.compression;
     }
   }
+
   if (config_->rocks_db.row_cache_size) {
-    options.row_cache = rocksdb::NewLRUCache(config_->rocks_db.row_cache_size 
* MiB);
+    options.row_cache = rocksdb::NewLRUCache(config_->rocks_db.row_cache_size 
* MiB, kRocksdbLRUAutoAdjustShardBits,
+                                             kRocksdbCacheStrictCapacityLimit, 
kRocksdbLRURowCacheHighPriPoolRatio);
   }
+
   options.enable_pipelined_write = config_->rocks_db.enable_pipelined_write;
   options.target_file_size_base = config_->rocks_db.target_file_size_base * 
MiB;
   options.max_manifest_file_size = 64 * MiB;
@@ -256,7 +277,15 @@ Status Storage::Open(DBOpenMode mode) {
     }
   }
 
-  std::shared_ptr<rocksdb::Cache> shared_block_cache = 
rocksdb::NewLRUCache(block_cache_size, -1, false, 0.75);
+  std::shared_ptr<rocksdb::Cache> shared_block_cache;
+
+  if (config_->rocks_db.block_cache_type == BlockCacheType::kCacheTypeLRU) {
+    shared_block_cache = rocksdb::NewLRUCache(block_cache_size, 
kRocksdbLRUAutoAdjustShardBits,
+                                              
kRocksdbCacheStrictCapacityLimit, kRocksdbLRUBlockCacheHighPriPoolRatio);
+  } else {
+    rocksdb::HyperClockCacheOptions hcc_cache_options(block_cache_size, 
kRockdbHCCAutoAdjustCharge);
+    shared_block_cache = hcc_cache_options.MakeSharedCache();
+  }
 
   rocksdb::BlockBasedTableOptions metadata_table_opts = InitTableOptions();
   metadata_table_opts.block_cache = shared_block_cache;
diff --git a/src/storage/storage.h b/src/storage/storage.h
index 7e37d82f..6114c7fc 100644
--- a/src/storage/storage.h
+++ b/src/storage/storage.h
@@ -94,6 +94,17 @@ inline const std::vector<CompressionOption> 
CompressionOptions = {
     {rocksdb::kZSTD, "zstd", "kZSTD"},
 };
 
+struct CacheOption {
+  BlockCacheType type;
+  const std::string name;
+  const std::string val;
+};
+
+inline const std::vector<CacheOption> CacheOptions = {
+    {BlockCacheType::kCacheTypeLRU, "lru", "kCacheTypeLRU"},
+    {BlockCacheType::kCacheTypeHCC, "hcc", "kCacheTypeHCC"},
+};
+
 enum class StatType {
   CompactionCount,
   FlushCount,

Reply via email to