From b13bdfa500821587411fb72a91cebf9ebafac5cc Mon Sep 17 00:00:00 2001
From: Igor Canadi <icanadi@fb.com>
Date: Fri, 24 Jan 2014 14:57:52 -0800
Subject: [PATCH] Add a call DisownData() to Cache, which should speed up
 shutdown

Summary: On a shutdown, freeing memory takes a long time. If we're shutting down, we don't really care about memory leaks. I added a call to Cache that will avoid freeing all objects in cache.

Test Plan:
I created a script to test the speedup and demonstrate how to use the call: https://phabricator.fb.com/P3864368

Clean shutdown took 7.2 seconds, while fast and dirty one took 6.3 seconds. Unfortunately, the speedup is not that big, but should be bigger with bigger block_cache. I have set up the capacity to 80GB, but the script filled up only ~7GB.

Reviewers: dhruba, haobo, MarkCallaghan, xjin

Reviewed By: dhruba

CC: leveldb

Differential Revision: https://reviews.facebook.net/D15069
---
 include/rocksdb/cache.h | 9 +++++++++
 util/cache.cc           | 3 +++
 2 files changed, 12 insertions(+)

diff --git a/include/rocksdb/cache.h b/include/rocksdb/cache.h
index 3e0e5c1cd..7d58e1546 100644
--- a/include/rocksdb/cache.h
+++ b/include/rocksdb/cache.h
@@ -104,6 +104,15 @@ class Cache {
   // returns the maximum configured capacity of the cache
   virtual size_t GetCapacity() = 0;
 
+  // Call this on shutdown if you want to speed it up. Cache will disown
+  // any underlying data and will not free it on delete. This call will leak
+  // memory - call this only if you're shutting down the process.
+  // Any attempts of using cache after this call will fail terribly.
+  // Always delete the DB object before calling this method!
+  virtual void DisownData() {
+    // default implementation is noop
+  };
+
  private:
   void LRU_Remove(Handle* e);
   void LRU_Append(Handle* e);
diff --git a/util/cache.cc b/util/cache.cc
index 8fa03626b..4707eac94 100644
--- a/util/cache.cc
+++ b/util/cache.cc
@@ -409,6 +409,9 @@ class ShardedLRUCache : public Cache {
   virtual size_t GetCapacity() {
     return capacity_;
   }
+  virtual void DisownData() {
+    shard_ = nullptr;
+  }
 };
 
 }  // end anonymous namespace