mirror of
https://github.com/facebook/rocksdb.git
synced 2024-11-26 16:30:56 +00:00
feb06e83b2
Summary: Defined the abstract interface for a secondary cache in include/rocksdb/secondary_cache.h, and updated LRUCacheOptions to take a std::shared_ptr<SecondaryCache>. An item is initially inserted into the LRU (primary) cache. When it ages out and evicted from memory, its inserted into the secondary cache. On a LRU cache miss and successful lookup in the secondary cache, the item is promoted to the LRU cache. Only support synchronous lookup currently. The secondary cache would be used to implement a persistent (flash cache) or compressed cache. Tests: Results from cache_bench and db_bench don't show any regression due to these changes. cache_bench results before and after this change - Command ```./cache_bench -ops_per_thread=10000000 -threads=1``` Before ```Complete in 40.688 s; QPS = 245774``` ```Complete in 40.486 s; QPS = 246996``` ```Complete in 42.019 s; QPS = 237989``` After ```Complete in 40.672 s; QPS = 245869``` ```Complete in 44.622 s; QPS = 224107``` ```Complete in 42.445 s; QPS = 235599``` db_bench results before this change, and with this change + https://github.com/facebook/rocksdb/issues/8213 and https://github.com/facebook/rocksdb/issues/8191 - Commands ```./db_bench --benchmarks="fillseq,compact" -num=30000000 -key_size=32 -value_size=256 -use_direct_io_for_flush_and_compaction=true -db=/home/anand76/nvm_cache/db -partition_index_and_filters=true``` ```./db_bench -db=/home/anand76/nvm_cache/db -use_existing_db=true -benchmarks=readrandom -num=30000000 -key_size=32 -value_size=256 -use_direct_reads=true -cache_size=1073741824 -cache_numshardbits=6 -cache_index_and_filter_blocks=true -read_random_exp_range=17 -statistics -partition_index_and_filters=true -threads=16 -duration=300``` Before ``` DB path: [/home/anand76/nvm_cache/db] readrandom : 80.702 micros/op 198104 ops/sec; 54.4 MB/s (3708999 of 3708999 found) ``` ``` DB path: [/home/anand76/nvm_cache/db] readrandom : 87.124 micros/op 183625 ops/sec; 50.4 MB/s (3439999 of 3439999 found) ``` After ``` DB path: [/home/anand76/nvm_cache/db] readrandom : 77.653 micros/op 206025 ops/sec; 56.6 MB/s (3866999 of 3866999 found) ``` ``` DB path: [/home/anand76/nvm_cache/db] readrandom : 84.962 micros/op 188299 ops/sec; 51.7 MB/s (3535999 of 3535999 found) ``` Pull Request resolved: https://github.com/facebook/rocksdb/pull/8271 Reviewed By: zhichao-cao Differential Revision: D28357511 Pulled By: anand1976 fbshipit-source-id: d1cfa236f00e649a18c53328be10a8062a4b6da2
136 lines
6 KiB
C++
136 lines
6 KiB
C++
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
// (found in the LICENSE.Apache file in the root directory).
|
|
//
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
#pragma once
|
|
|
|
#include <atomic>
|
|
#include <string>
|
|
|
|
#include "port/port.h"
|
|
#include "rocksdb/cache.h"
|
|
|
|
namespace ROCKSDB_NAMESPACE {
|
|
|
|
// Single cache shard interface.
|
|
class CacheShard {
|
|
public:
|
|
CacheShard() = default;
|
|
virtual ~CacheShard() = default;
|
|
|
|
using DeleterFn = Cache::DeleterFn;
|
|
virtual Status Insert(const Slice& key, uint32_t hash, void* value,
|
|
size_t charge, DeleterFn deleter,
|
|
Cache::Handle** handle, Cache::Priority priority) = 0;
|
|
virtual Status Insert(const Slice& key, uint32_t hash, void* value,
|
|
const Cache::CacheItemHelper* helper, size_t charge,
|
|
Cache::Handle** handle, Cache::Priority priority) = 0;
|
|
virtual Cache::Handle* Lookup(const Slice& key, uint32_t hash) = 0;
|
|
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;
|
|
virtual bool Release(Cache::Handle* handle, bool useful,
|
|
bool force_erase) = 0;
|
|
virtual bool IsReady(Cache::Handle* handle) = 0;
|
|
virtual void Wait(Cache::Handle* handle) = 0;
|
|
virtual bool Ref(Cache::Handle* handle) = 0;
|
|
virtual bool Release(Cache::Handle* handle, bool force_erase) = 0;
|
|
virtual void Erase(const Slice& key, uint32_t hash) = 0;
|
|
virtual void SetCapacity(size_t capacity) = 0;
|
|
virtual void SetStrictCapacityLimit(bool strict_capacity_limit) = 0;
|
|
virtual size_t GetUsage() const = 0;
|
|
virtual size_t GetPinnedUsage() const = 0;
|
|
// Handles iterating over roughly `average_entries_per_lock` entries, using
|
|
// `state` to somehow record where it last ended up. Caller initially uses
|
|
// *state == 0 and implementation sets *state = UINT32_MAX to indicate
|
|
// completion.
|
|
virtual void ApplyToSomeEntries(
|
|
const std::function<void(const Slice& key, void* value, size_t charge,
|
|
DeleterFn deleter)>& callback,
|
|
uint32_t average_entries_per_lock, uint32_t* state) = 0;
|
|
virtual void EraseUnRefEntries() = 0;
|
|
virtual std::string GetPrintableOptions() const { return ""; }
|
|
void set_metadata_charge_policy(
|
|
CacheMetadataChargePolicy metadata_charge_policy) {
|
|
metadata_charge_policy_ = metadata_charge_policy;
|
|
}
|
|
|
|
protected:
|
|
CacheMetadataChargePolicy metadata_charge_policy_ = kDontChargeCacheMetadata;
|
|
};
|
|
|
|
// Generic cache interface which shards cache by hash of keys. 2^num_shard_bits
|
|
// shards will be created, with capacity split evenly to each of the shards.
|
|
// Keys are sharded by the highest num_shard_bits bits of hash value.
|
|
class ShardedCache : public Cache {
|
|
public:
|
|
ShardedCache(size_t capacity, int num_shard_bits, bool strict_capacity_limit,
|
|
std::shared_ptr<MemoryAllocator> memory_allocator = nullptr);
|
|
virtual ~ShardedCache() = default;
|
|
virtual const char* Name() const override = 0;
|
|
virtual CacheShard* GetShard(uint32_t shard) = 0;
|
|
virtual const CacheShard* GetShard(uint32_t shard) const = 0;
|
|
virtual void* Value(Handle* handle) override = 0;
|
|
virtual size_t GetCharge(Handle* handle) const override = 0;
|
|
virtual void WaitAll(std::vector<Handle*>& handles) override = 0;
|
|
|
|
virtual uint32_t GetHash(Handle* handle) const = 0;
|
|
virtual void DisownData() override = 0;
|
|
|
|
virtual void SetCapacity(size_t capacity) override;
|
|
virtual void SetStrictCapacityLimit(bool strict_capacity_limit) override;
|
|
|
|
virtual Status Insert(const Slice& key, void* value, size_t charge,
|
|
DeleterFn deleter, Handle** handle,
|
|
Priority priority) override;
|
|
virtual Status Insert(const Slice& key, void* value,
|
|
const CacheItemHelper* helper, size_t chargge,
|
|
Handle** handle = nullptr,
|
|
Priority priority = Priority::LOW) override;
|
|
virtual Handle* Lookup(const Slice& key, Statistics* stats) override;
|
|
virtual Handle* Lookup(const Slice& key, const CacheItemHelper* helper,
|
|
const CreateCallback& create_cb, Priority priority,
|
|
bool wait, Statistics* stats = nullptr) override;
|
|
virtual bool Release(Handle* handle, bool useful,
|
|
bool force_erase = false) override;
|
|
virtual bool IsReady(Handle* handle) override;
|
|
virtual void Wait(Handle* handle) override;
|
|
virtual bool Ref(Handle* handle) override;
|
|
virtual bool Release(Handle* handle, bool force_erase = false) override;
|
|
virtual void Erase(const Slice& key) override;
|
|
virtual uint64_t NewId() override;
|
|
virtual size_t GetCapacity() const override;
|
|
virtual bool HasStrictCapacityLimit() const override;
|
|
virtual size_t GetUsage() const override;
|
|
virtual size_t GetUsage(Handle* handle) const override;
|
|
virtual size_t GetPinnedUsage() const override;
|
|
virtual void ApplyToAllEntries(
|
|
const std::function<void(const Slice& key, void* value, size_t charge,
|
|
DeleterFn deleter)>& callback,
|
|
const ApplyToAllEntriesOptions& opts) override;
|
|
virtual void EraseUnRefEntries() override;
|
|
virtual std::string GetPrintableOptions() const override;
|
|
|
|
int GetNumShardBits() const;
|
|
uint32_t GetNumShards() const;
|
|
|
|
private:
|
|
inline uint32_t Shard(uint32_t hash) { return hash & shard_mask_; }
|
|
|
|
const uint32_t shard_mask_;
|
|
mutable port::Mutex capacity_mutex_;
|
|
size_t capacity_;
|
|
bool strict_capacity_limit_;
|
|
std::atomic<uint64_t> last_id_;
|
|
};
|
|
|
|
extern int GetDefaultCacheShardBits(size_t capacity);
|
|
|
|
} // namespace ROCKSDB_NAMESPACE
|