]> git-server-git.apps.pok.os.sepia.ceph.com Git - rocksdb.git/commitdiff
Add a stat to count secondary cache hits (#8666)
authoranand76 <anand76@devvm4702.ftw0.facebook.com>
Tue, 17 Aug 2021 04:00:17 +0000 (21:00 -0700)
committerFacebook GitHub Bot <facebook-github-bot@users.noreply.github.com>
Tue, 17 Aug 2021 04:01:14 +0000 (21:01 -0700)
Summary:
Add a stat for secondary cache hits. The ```Cache::Lookup``` API had an unused ```stats``` parameter. This PR uses that to pass the pointer to a ```Statistics``` object that ```LRUCache``` uses to record the stat.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/8666

Test Plan: Update a unit test in lru_cache_test

Reviewed By: zhichao-cao

Differential Revision: D30353816

Pulled By: anand1976

fbshipit-source-id: 2046f78b460428877a26ffdd2bb914ae47dfbe77

HISTORY.md
cache/clock_cache.cc
cache/lru_cache.cc
cache/lru_cache.h
cache/lru_cache_test.cc
cache/sharded_cache.cc
cache/sharded_cache.h
include/rocksdb/statistics.h
java/rocksjni/portal.h
java/src/main/java/org/rocksdb/TickerType.java
monitoring/statistics.cc

index 8fe057683308954b02a3ac0c8274fedc262f46c4..a85ad6335052b02927f3356a51e3a333b7f5de7c 100644 (file)
@@ -17,6 +17,7 @@
 * Add a comment to suggest btrfs user to disable file preallocation by setting `options.allow_fallocate=false`.
 * Fast forward option in Trace replay changed to double type to allow replaying at a lower speed, by settings the value between 0 and 1. This option can be set via `ReplayOptions` in `Replayer::Replay()`, or via `--trace_replay_fast_forward` in db_bench.
 * Add property `LiveSstFilesSizeAtTemperature` to retrieve sst file size at different temperature.
+* Added a stat rocksdb.secondary.cache.hits
 
 ## Public API change
 * Added APIs to decode and replay trace file via Replayer class. Added `DB::NewDefaultReplayer()` to create a default Replayer instance. Added `TraceReader::Reset()` to restart reading a trace file. Created trace_record.h and utilities/replayer.h files to access decoded Trace records and replay them.
index a3cb7d2c3f9d309dfd3cd005e3de345ef858875f..3364e251234a1810329f2b22327a3619a4416584 100644 (file)
@@ -280,7 +280,8 @@ class ClockCacheShard final : public CacheShard {
   Cache::Handle* Lookup(const Slice& key, uint32_t hash,
                         const Cache::CacheItemHelper* /*helper*/,
                         const Cache::CreateCallback& /*create_cb*/,
-                        Cache::Priority /*priority*/, bool /*wait*/) override {
+                        Cache::Priority /*priority*/, bool /*wait*/,
+                        Statistics* /*stats*/) override {
     return Lookup(key, hash);
   }
   bool Release(Cache::Handle* handle, bool /*useful*/,
index db60828c024de2e050fa8cd1e0c10fe8856e3b0b..710a155c4c74c7d66532cb3435c956cf36d59225 100644 (file)
@@ -13,6 +13,7 @@
 #include <cstdint>
 #include <cstdio>
 
+#include "monitoring/statistics.h"
 #include "util/mutexlock.h"
 
 namespace ROCKSDB_NAMESPACE {
@@ -418,7 +419,7 @@ Cache::Handle* LRUCacheShard::Lookup(
     const Slice& key, uint32_t hash,
     const ShardedCache::CacheItemHelper* helper,
     const ShardedCache::CreateCallback& create_cb, Cache::Priority priority,
-    bool wait) {
+    bool wait, Statistics* stats) {
   LRUHandle* e = nullptr;
   {
     MutexLock l(&mutex_);
@@ -471,11 +472,16 @@ Cache::Handle* LRUCacheShard::Lookup(
           e->Unref();
           e->Free();
           e = nullptr;
+        } else {
+          RecordTick(stats, SECONDARY_CACHE_HITS);
         }
       } else {
         // If wait is false, we always return a handle and let the caller
         // release the handle after checking for success or failure
         e->SetIncomplete(true);
+        // This may be slightly inaccurate, if the lookup eventually fails.
+        // But the probability is very low.
+        RecordTick(stats, SECONDARY_CACHE_HITS);
       }
     }
   }
index 8ce0043de88b41a08cb2408f11d8a2847e53b750..7013c932840b5258ff11f711e024bde887697fc7 100644 (file)
@@ -319,10 +319,11 @@ class ALIGN_AS(CACHE_LINE_SIZE) LRUCacheShard final : public CacheShard {
   virtual Cache::Handle* Lookup(const Slice& key, uint32_t hash,
                                 const ShardedCache::CacheItemHelper* helper,
                                 const ShardedCache::CreateCallback& create_cb,
-                                ShardedCache::Priority priority,
-                                bool wait) override;
+                                ShardedCache::Priority priority, bool wait,
+                                Statistics* stats) override;
   virtual Cache::Handle* Lookup(const Slice& key, uint32_t hash) override {
-    return Lookup(key, hash, nullptr, nullptr, Cache::Priority::LOW, true);
+    return Lookup(key, hash, nullptr, nullptr, Cache::Priority::LOW, true,
+                  nullptr);
   }
   virtual bool Release(Cache::Handle* handle, bool /*useful*/,
                        bool force_erase) override {
index 404530fbe89195ddd1382cb12fcb5a887d5814c2..74409421c61e389e91c8ca9b2a1b76ec0a45a716 100644 (file)
@@ -468,6 +468,7 @@ TEST_F(LRUSecondaryCacheTest, BasicTest) {
       std::make_shared<TestSecondaryCache>(2048);
   opts.secondary_cache = secondary_cache;
   std::shared_ptr<Cache> cache = NewLRUCache(opts);
+  std::shared_ptr<Statistics> stats = CreateDBStatistics();
 
   Random rnd(301);
   std::string str1 = rnd.RandomString(1020);
@@ -476,22 +477,26 @@ TEST_F(LRUSecondaryCacheTest, BasicTest) {
                           str1.length()));
   std::string str2 = rnd.RandomString(1020);
   TestItem* item2 = new TestItem(str2.data(), str2.length());
-  // k2 should be demoted to NVM
+  // k1 should be demoted to NVM
   ASSERT_OK(cache->Insert("k2", item2, &LRUSecondaryCacheTest::helper_,
                           str2.length()));
 
   Cache::Handle* handle;
-  handle = cache->Lookup("k2", &LRUSecondaryCacheTest::helper_,
-                         test_item_creator, Cache::Priority::LOW, true);
+  handle =
+      cache->Lookup("k2", &LRUSecondaryCacheTest::helper_, test_item_creator,
+                    Cache::Priority::LOW, true, stats.get());
   ASSERT_NE(handle, nullptr);
   cache->Release(handle);
   // This lookup should promote k1 and demote k2
-  handle = cache->Lookup("k1", &LRUSecondaryCacheTest::helper_,
-                         test_item_creator, Cache::Priority::LOW, true);
+  handle =
+      cache->Lookup("k1", &LRUSecondaryCacheTest::helper_, test_item_creator,
+                    Cache::Priority::LOW, true, stats.get());
   ASSERT_NE(handle, nullptr);
   cache->Release(handle);
   ASSERT_EQ(secondary_cache->num_inserts(), 2u);
   ASSERT_EQ(secondary_cache->num_lookups(), 1u);
+  ASSERT_EQ(stats->getTickerCount(SECONDARY_CACHE_HITS),
+            secondary_cache->num_lookups());
 
   cache.reset();
   secondary_cache.reset();
index bf90ea3b10fb2fb8cbca5486aaf3acc1655ea9d6..d2876ce36283a2fe7d7c8eda9a12eb9afb549cf4 100644 (file)
@@ -83,10 +83,10 @@ Cache::Handle* ShardedCache::Lookup(const Slice& key,
                                     const CacheItemHelper* helper,
                                     const CreateCallback& create_cb,
                                     Priority priority, bool wait,
-                                    Statistics* /*stats*/) {
+                                    Statistics* stats) {
   uint32_t hash = HashSlice(key);
   return GetShard(Shard(hash))
-      ->Lookup(key, hash, helper, create_cb, priority, wait);
+      ->Lookup(key, hash, helper, create_cb, priority, wait, stats);
 }
 
 bool ShardedCache::IsReady(Handle* handle) {
index 3e2a20abac457bdcd6cdde6739d4fa495ec3cdf2..8e6f60d0318967debf0a838ab3c67d0ecca4619f 100644 (file)
@@ -34,7 +34,8 @@ class CacheShard {
   virtual Cache::Handle* Lookup(const Slice& key, uint32_t hash,
                                 const Cache::CacheItemHelper* helper,
                                 const Cache::CreateCallback& create_cb,
-                                Cache::Priority priority, bool wait) = 0;
+                                Cache::Priority priority, bool wait,
+                                Statistics* stats) = 0;
   virtual bool Release(Cache::Handle* handle, bool useful,
                        bool force_erase) = 0;
   virtual bool IsReady(Cache::Handle* handle) = 0;
index f5e83b36dd554a9172fc1281c0b675421c4a93f0..fa59397b130ba0006fe8d60f91a3beba36866dea 100644 (file)
@@ -389,6 +389,9 @@ enum Tickers : uint32_t {
   // Outdated bytes of data present on memtable at flush time.
   MEMTABLE_GARBAGE_BYTES_AT_FLUSH,
 
+  // Secondary cache statistics
+  SECONDARY_CACHE_HITS,
+
   TICKER_ENUM_MAX
 };
 
index c5f34a8a225fc87507638ac6fbcfc799a6e96bf6..5c9be330edc3d71ff93d55f05bce77d446d709c1 100644 (file)
@@ -5000,6 +5000,8 @@ class TickerTypeJni {
         return -0x1C;
       case ROCKSDB_NAMESPACE::Tickers::MEMTABLE_GARBAGE_BYTES_AT_FLUSH:
         return -0x1D;
+      case ROCKSDB_NAMESPACE::Tickers::SECONDARY_CACHE_HITS:
+        return -0x1E;
       case ROCKSDB_NAMESPACE::Tickers::TICKER_ENUM_MAX:
         // 0x5F for backwards compatibility on current minor version.
         return 0x5F;
@@ -5330,6 +5332,8 @@ class TickerTypeJni {
         return ROCKSDB_NAMESPACE::Tickers::MEMTABLE_PAYLOAD_BYTES_AT_FLUSH;
       case -0x1D:
         return ROCKSDB_NAMESPACE::Tickers::MEMTABLE_GARBAGE_BYTES_AT_FLUSH;
+      case -0x1E:
+        return ROCKSDB_NAMESPACE::Tickers::SECONDARY_CACHE_HITS;
       case 0x5F:
         // 0x5F for backwards compatibility on current minor version.
         return ROCKSDB_NAMESPACE::Tickers::TICKER_ENUM_MAX;
index 0d6cc5a92eb4f645770832c11e8aa25bf6c08000..1381a0a6d57bf8d2e6f1e9c7c1120494c8bd9462 100644 (file)
@@ -764,6 +764,11 @@ public enum TickerType {
      */
     MEMTABLE_GARBAGE_BYTES_AT_FLUSH((byte) -0x1D),
 
+    /**
+     * Number of secondary cache hits
+     */
+    SECONDARY_CACHE_HITS((byte) -0x1E),
+
     TICKER_ENUM_MAX((byte) 0x5F);
 
     private final byte value;
index 18d8eb160796cd505cb033df10ae4a3a67069293..7b30c7fcc082b73f986d6686b265f38d52c2b3d1 100644 (file)
@@ -205,6 +205,7 @@ const std::vector<std::pair<Tickers, std::string>> TickersNameMap = {
      "rocksdb.memtable.payload.bytes.at.flush"},
     {MEMTABLE_GARBAGE_BYTES_AT_FLUSH,
      "rocksdb.memtable.garbage.bytes.at.flush"},
+    {SECONDARY_CACHE_HITS, "rocksdb.secondary.cache.hits"},
 };
 
 const std::vector<std::pair<Histograms, std::string>> HistogramsNameMap = {