From: Adam Kupczyk Date: Tue, 1 Jul 2025 11:23:28 +0000 (+0000) Subject: kv/KeyValueDB: New estimate_range_size function X-Git-Tag: v21.0.1~9^2~3 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=77af5b004e96161e719048cb73ad39e3115895cd;p=ceph.git kv/KeyValueDB: New estimate_range_size function Taking estimate_prefix_size to another level. Makes possible detailed inspection of db size. Used primarily for bisecting key range. Signed-off-by: Adam Kupczyk --- diff --git a/src/kv/KeyValueDB.h b/src/kv/KeyValueDB.h index 68f07394eaa..0583107cf50 100644 --- a/src/kv/KeyValueDB.h +++ b/src/kv/KeyValueDB.h @@ -410,6 +410,13 @@ public: const std::string& key_prefix) { return 0; } + /// estimate space utilization for a specified range (in bytes) + virtual int64_t estimate_range_size( + const std::string& prefix, + const std::string& key_from, + const std::string& key_to) { + return 0; + }; /// compact the underlying store virtual void compact() {} diff --git a/src/kv/RocksDBStore.cc b/src/kv/RocksDBStore.cc index 97830201269..a51968b7036 100644 --- a/src/kv/RocksDBStore.cc +++ b/src/kv/RocksDBStore.cc @@ -1433,6 +1433,36 @@ int64_t RocksDBStore::estimate_prefix_size(const string& prefix, return size; } +int64_t RocksDBStore::estimate_range_size( + const string& prefix, + const string& key_from, + const string& key_to) +{ + // The default mode is to derive estimates based on + // sst files alone (INCLUDE_FILES). + // This gives an irritating result when a batch of keys is + // just commited but estimate keeps showing 0. + rocksdb::DB::SizeApproximationFlags flags( + rocksdb::DB::SizeApproximationFlags::INCLUDE_FILES | + rocksdb::DB::SizeApproximationFlags::INCLUDE_MEMTABLES); + uint64_t size = 0; + auto p_iter = cf_handles.find(prefix); + if (p_iter != cf_handles.end()) { + for (const auto cf : p_iter->second.handles) { + uint64_t s = 0; + rocksdb::Range r(key_from, key_to); + db->GetApproximateSizes(cf, &r, 1, &s, flags); + size += s; + } + } else { + string start = combine_strings(prefix , key_from); + string limit = combine_strings(prefix , key_to); + rocksdb::Range r(start, limit); + db->GetApproximateSizes(default_cf, &r, 1, &size, flags); + } + return size; +} + void RocksDBStore::get_statistics(Formatter *f) { if (!cct->_conf->rocksdb_perf) { diff --git a/src/kv/RocksDBStore.h b/src/kv/RocksDBStore.h index c0cab4cfbcf..a32cc71b0c8 100644 --- a/src/kv/RocksDBStore.h +++ b/src/kv/RocksDBStore.h @@ -291,6 +291,9 @@ public: int64_t estimate_prefix_size(const std::string& prefix, const std::string& key_prefix) override; + int64_t estimate_range_size(const std::string& prefix, + const std::string& key_from, + const std::string& key_to) override; struct RocksWBHandler; class RocksDBTransactionImpl : public KeyValueDB::TransactionImpl { public: