mirror of
https://github.com/facebook/rocksdb.git
synced 2024-11-29 09:36:17 +00:00
48589b961f
Summary: Updating the tiered cache (cache allocated using ```NewTieredCache()```) by calling ```SetCapacity()``` on it was not working properly. The initial creation would set the primary cache capacity to the combined primary and compressed secondary cache capacity. But ```SetCapacity()``` would just set the primary cache capacity, with no way to change the secondary cache capacity. Additionally, the API was confusing, since the primary and compressed secondary capacities would be specified separately during creation, but ```SetCapacity``` took the combined capacity. With this fix, the user always specifies the total budget and compressed secondary cache ratio on creation. Subsequently, `SetCapacity` will distribute the new capacity across the two caches by the same ratio. The `NewTieredCache` API has been changed to take the total cache capacity (inclusive of both the primary and the compressed secondary cache) and the ratio of total capacity to allocate to the compressed cache. These are specified in `TieredCacheOptions`. Any capacity specified in `LRUCacheOptions`, `HyperClockCacheOptions` and `CompressedSecondaryCacheOptions` is ignored. A new API, `UpdateTieredCache` is provided to dynamically update the total capacity, ratio of compressed cache, and admission policy. Tests: New unit tests Pull Request resolved: https://github.com/facebook/rocksdb/pull/11873 Reviewed By: akankshamahajan15 Differential Revision: D49562250 Pulled By: anand1976 fbshipit-source-id: 57033bc713b68d5da6292207765a6b3dbe539ddf
91 lines
3.1 KiB
C++
91 lines
3.1 KiB
C++
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
// 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).
|
|
|
|
#pragma once
|
|
|
|
#include "cache/cache_reservation_manager.h"
|
|
#include "rocksdb/secondary_cache.h"
|
|
|
|
namespace ROCKSDB_NAMESPACE {
|
|
|
|
class CacheWithSecondaryAdapter : public CacheWrapper {
|
|
public:
|
|
explicit CacheWithSecondaryAdapter(
|
|
std::shared_ptr<Cache> target,
|
|
std::shared_ptr<SecondaryCache> secondary_cache,
|
|
TieredAdmissionPolicy adm_policy = TieredAdmissionPolicy::kAdmPolicyAuto,
|
|
bool distribute_cache_res = false);
|
|
|
|
~CacheWithSecondaryAdapter() override;
|
|
|
|
Status Insert(
|
|
const Slice& key, ObjectPtr value, const CacheItemHelper* helper,
|
|
size_t charge, Handle** handle = nullptr,
|
|
Priority priority = Priority::LOW,
|
|
const Slice& compressed_value = Slice(),
|
|
CompressionType type = CompressionType::kNoCompression) override;
|
|
|
|
Handle* Lookup(const Slice& key, const CacheItemHelper* helper,
|
|
CreateContext* create_context,
|
|
Priority priority = Priority::LOW,
|
|
Statistics* stats = nullptr) override;
|
|
|
|
using Cache::Release;
|
|
bool Release(Handle* handle, bool erase_if_last_ref = false) override;
|
|
|
|
ObjectPtr Value(Handle* handle) override;
|
|
|
|
void StartAsyncLookup(AsyncLookupHandle& async_handle) override;
|
|
|
|
void WaitAll(AsyncLookupHandle* async_handles, size_t count) override;
|
|
|
|
std::string GetPrintableOptions() const override;
|
|
|
|
const char* Name() const override;
|
|
|
|
void SetCapacity(size_t capacity) override;
|
|
|
|
Status UpdateCacheReservationRatio(double ratio);
|
|
|
|
Status UpdateAdmissionPolicy(TieredAdmissionPolicy adm_policy);
|
|
|
|
Cache* TEST_GetCache() { return target_.get(); }
|
|
|
|
SecondaryCache* TEST_GetSecondaryCache() { return secondary_cache_.get(); }
|
|
|
|
private:
|
|
bool EvictionHandler(const Slice& key, Handle* handle, bool was_hit);
|
|
|
|
void StartAsyncLookupOnMySecondary(AsyncLookupHandle& async_handle);
|
|
|
|
Handle* Promote(
|
|
std::unique_ptr<SecondaryCacheResultHandle>&& secondary_handle,
|
|
const Slice& key, const CacheItemHelper* helper, Priority priority,
|
|
Statistics* stats, bool found_dummy_entry, bool kept_in_sec_cache);
|
|
|
|
bool ProcessDummyResult(Cache::Handle** handle, bool erase);
|
|
|
|
void CleanupCacheObject(ObjectPtr obj, const CacheItemHelper* helper);
|
|
|
|
std::shared_ptr<SecondaryCache> secondary_cache_;
|
|
TieredAdmissionPolicy adm_policy_;
|
|
// Whether to proportionally distribute cache memory reservations, i.e
|
|
// placeholder entries with null value and a non-zero charge, across
|
|
// the primary and secondary caches.
|
|
bool distribute_cache_res_;
|
|
// A cache reservation manager to keep track of secondary cache memory
|
|
// usage by reserving equivalent capacity against the primary cache
|
|
std::shared_ptr<ConcurrentCacheReservationManager> pri_cache_res_;
|
|
// Fraction of a cache memory reservation to be assigned to the secondary
|
|
// cache
|
|
double sec_cache_res_ratio_;
|
|
port::Mutex mutex_;
|
|
#ifndef NDEBUG
|
|
bool ratio_changed_ = false;
|
|
#endif
|
|
};
|
|
|
|
} // namespace ROCKSDB_NAMESPACE
|