Skip to content

Commit 54a9cb4

Browse files
committed
Create DB properties for internal table cache
1 parent f7ef236 commit 54a9cb4

File tree

4 files changed

+92
-13
lines changed

4 files changed

+92
-13
lines changed

db/db_properties_test.cc

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,8 @@ void GetExpectedTableProperties(
245245
const int kDeletionCount = kTableCount * kDeletionsPerTable;
246246
const int kMergeCount = kTableCount * kMergeOperandsPerTable;
247247
const int kRangeDeletionCount = kTableCount * kRangeDeletionsPerTable;
248-
const int kKeyCount = kPutCount + kDeletionCount + kMergeCount + kRangeDeletionCount;
248+
const int kKeyCount =
249+
kPutCount + kDeletionCount + kMergeCount + kRangeDeletionCount;
249250
const int kAvgSuccessorSize = kKeySize / 5;
250251
const int kEncodingSavePerKey = kKeySize / 4;
251252
expected_tp->raw_key_size = kKeyCount * (kKeySize + 8);
@@ -256,7 +257,8 @@ void GetExpectedTableProperties(
256257
expected_tp->num_merge_operands = kMergeCount;
257258
expected_tp->num_range_deletions = kRangeDeletionCount;
258259
expected_tp->num_data_blocks =
259-
kTableCount * (kKeysPerTable * (kKeySize - kEncodingSavePerKey + kValueSize)) /
260+
kTableCount *
261+
(kKeysPerTable * (kKeySize - kEncodingSavePerKey + kValueSize)) /
260262
kBlockSize;
261263
expected_tp->data_size =
262264
kTableCount * (kKeysPerTable * (kKeySize + 8 + kValueSize));
@@ -1090,7 +1092,8 @@ class CountingUserTblPropCollector : public TablePropertiesCollector {
10901092
std::string encoded;
10911093
PutVarint32(&encoded, count_);
10921094
*properties = UserCollectedProperties{
1093-
{"CountingUserTblPropCollector", message_}, {"Count", encoded},
1095+
{"CountingUserTblPropCollector", message_},
1096+
{"Count", encoded},
10941097
};
10951098
return Status::OK();
10961099
}
@@ -1713,6 +1716,46 @@ TEST_F(DBPropertiesTest, BlockCacheProperties) {
17131716
ASSERT_EQ(0, value);
17141717
}
17151718

1719+
TEST_F(DBPropertiesTest, TableCacheProperties) {
1720+
Options options;
1721+
uint64_t value, new_value;
1722+
1723+
options.env = CurrentOptions().env;
1724+
1725+
Reopen(options);
1726+
1727+
//
1728+
// test table_cache access is "live"
1729+
// TableCacheCapacity originally comes from DBOptions::max_open_files
1730+
// and can vary by system. Get its current value.
1731+
ASSERT_TRUE(db_->GetIntProperty(DB::Properties::kTableCacheCapacity, &value));
1732+
1733+
// now, change max_open_files to prove we are really accessing the value of
1734+
// interest
1735+
new_value = value / 2;
1736+
std::unordered_map<std::string, std::string> new_options;
1737+
new_options.insert(std::pair<std::string, std::string>(
1738+
"max_open_files", std::to_string(new_value)));
1739+
ASSERT_OK(db_->SetDBOptions(new_options));
1740+
1741+
// did the value we are reading update. NOTE: rocksdb internally reduces
1742+
// the value we pass by 10.
1743+
ASSERT_TRUE(db_->GetIntProperty(DB::Properties::kTableCacheCapacity, &value));
1744+
ASSERT_EQ(new_value - 10, value);
1745+
1746+
//
1747+
// TableCacheUsage is a count of open .sst files. Force the creation of a
1748+
// a new table file. First add a record via Put(). Then force that
1749+
// record from write buffer to new .sst via Flush(). New .sst
1750+
// automatically opens and gets position in table cache ... raising usage
1751+
// count
1752+
ASSERT_TRUE(db_->GetIntProperty(DB::Properties::kTableCacheUsage, &value));
1753+
ASSERT_OK(Put("foo", "v1"));
1754+
ASSERT_OK(db_->Flush(FlushOptions()));
1755+
ASSERT_TRUE(
1756+
db_->GetIntProperty(DB::Properties::kTableCacheUsage, &new_value));
1757+
ASSERT_EQ(new_value, value + 1);
1758+
}
17161759
#endif // ROCKSDB_LITE
17171760
} // namespace ROCKSDB_NAMESPACE
17181761

db/internal_stats.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ static const std::string estimate_oldest_key_time = "estimate-oldest-key-time";
260260
static const std::string block_cache_capacity = "block-cache-capacity";
261261
static const std::string block_cache_usage = "block-cache-usage";
262262
static const std::string block_cache_pinned_usage = "block-cache-pinned-usage";
263+
static const std::string tablecache_capacity = "table-cache-capacity";
264+
static const std::string tablecache_usage = "table-cache-usage";
263265
static const std::string options_statistics = "options-statistics";
264266

265267
const std::string DB::Properties::kNumFilesAtLevelPrefix =
@@ -348,6 +350,10 @@ const std::string DB::Properties::kBlockCacheUsage =
348350
rocksdb_prefix + block_cache_usage;
349351
const std::string DB::Properties::kBlockCachePinnedUsage =
350352
rocksdb_prefix + block_cache_pinned_usage;
353+
const std::string DB::Properties::kTableCacheCapacity =
354+
rocksdb_prefix + tablecache_capacity;
355+
const std::string DB::Properties::kTableCacheUsage =
356+
rocksdb_prefix + tablecache_usage;
351357
const std::string DB::Properties::kOptionsStatistics =
352358
rocksdb_prefix + options_statistics;
353359

@@ -487,6 +493,12 @@ const std::unordered_map<std::string, DBPropertyInfo>
487493
{DB::Properties::kBlockCachePinnedUsage,
488494
{false, nullptr, &InternalStats::HandleBlockCachePinnedUsage, nullptr,
489495
nullptr}},
496+
{DB::Properties::kTableCacheCapacity,
497+
{false, nullptr, &InternalStats::HandleTableCacheCapacity, nullptr,
498+
nullptr}},
499+
{DB::Properties::kTableCacheUsage,
500+
{false, nullptr, &InternalStats::HandleTableCacheUsage, nullptr,
501+
nullptr}},
490502
{DB::Properties::kOptionsStatistics,
491503
{false, nullptr, nullptr, nullptr,
492504
&DBImpl::GetPropertyHandleOptionsStatistics}},
@@ -987,6 +999,18 @@ bool InternalStats::HandleBlockCachePinnedUsage(uint64_t* value, DBImpl* /*db*/,
987999
return true;
9881000
}
9891001

1002+
bool InternalStats::HandleTableCacheCapacity(uint64_t* value, DBImpl* db,
1003+
Version* /*version*/) {
1004+
*value = static_cast<uint64_t>(db->table_cache_->GetCapacity());
1005+
return true;
1006+
}
1007+
1008+
bool InternalStats::HandleTableCacheUsage(uint64_t* value, DBImpl* db,
1009+
Version* /*version*/) {
1010+
*value = static_cast<uint64_t>(db->table_cache_->GetUsage());
1011+
return true;
1012+
}
1013+
9901014
void InternalStats::DumpDBStats(std::string* value) {
9911015
char buf[1000];
9921016
// DB-level stats, only available from default column family

db/internal_stats.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ class InternalStats {
296296
this->num_dropped_records += c.num_dropped_records;
297297
this->count += c.count;
298298
int num_of_reasons = static_cast<int>(CompactionReason::kNumOfReasons);
299-
for (int i = 0; i< num_of_reasons; i++) {
299+
for (int i = 0; i < num_of_reasons; i++) {
300300
counts[i] += c.counts[i];
301301
}
302302
}
@@ -437,8 +437,8 @@ class InternalStats {
437437
struct CFStatsSnapshot {
438438
// ColumnFamily-level stats
439439
CompactionStats comp_stats;
440-
uint64_t ingest_bytes_flush; // Bytes written to L0 (Flush)
441-
uint64_t stall_count; // Stall count
440+
uint64_t ingest_bytes_flush; // Bytes written to L0 (Flush)
441+
uint64_t stall_count; // Stall count
442442
// Stats from compaction jobs - bytes written, bytes read, duration.
443443
uint64_t compact_bytes_write;
444444
uint64_t compact_bytes_read;
@@ -480,10 +480,10 @@ class InternalStats {
480480

481481
struct DBStatsSnapshot {
482482
// DB-level stats
483-
uint64_t ingest_bytes; // Bytes written by user
484-
uint64_t wal_bytes; // Bytes written to WAL
485-
uint64_t wal_synced; // Number of times WAL is synced
486-
uint64_t write_with_wal; // Number of writes that request WAL
483+
uint64_t ingest_bytes; // Bytes written by user
484+
uint64_t wal_bytes; // Bytes written to WAL
485+
uint64_t wal_synced; // Number of times WAL is synced
486+
uint64_t write_with_wal; // Number of writes that request WAL
487487
// These count the number of writes processed by the calling thread or
488488
// another thread.
489489
uint64_t write_other;
@@ -594,6 +594,8 @@ class InternalStats {
594594
bool HandleBlockCacheUsage(uint64_t* value, DBImpl* db, Version* version);
595595
bool HandleBlockCachePinnedUsage(uint64_t* value, DBImpl* db,
596596
Version* version);
597+
bool HandleTableCacheCapacity(uint64_t* value, DBImpl* db, Version* version);
598+
bool HandleTableCacheUsage(uint64_t* value, DBImpl* db, Version* version);
597599
// Total number of background errors encountered. Every time a flush task
598600
// or compaction task fails, this counter is incremented. The failure can
599601
// be caused by any possible reason, including file system errors, out of
@@ -697,13 +699,14 @@ class InternalStats {
697699
return false;
698700
}
699701

700-
bool GetIntProperty(const DBPropertyInfo& /*property_info*/, uint64_t* /*value*/,
701-
DBImpl* /*db*/) const {
702+
bool GetIntProperty(const DBPropertyInfo& /*property_info*/,
703+
uint64_t* /*value*/, DBImpl* /*db*/) const {
702704
return false;
703705
}
704706

705707
bool GetIntPropertyOutOfMutex(const DBPropertyInfo& /*property_info*/,
706-
Version* /*version*/, uint64_t* /*value*/) const {
708+
Version* /*version*/,
709+
uint64_t* /*value*/) const {
707710
return false;
708711
}
709712
};

include/rocksdb/db.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,13 @@ class DB {
923923
// entries being pinned.
924924
static const std::string kBlockCachePinnedUsage;
925925

926+
// "rocksdb.table-cache-capacity" - returns table cache capacity.
927+
static const std::string kTableCacheCapacity;
928+
929+
// "rocksdb.table-cache-usage" - returns the memory size for the entries
930+
// residing in table cache.
931+
static const std::string kTableCacheUsage;
932+
926933
// "rocksdb.options-statistics" - returns multi-line string
927934
// of options.statistics
928935
static const std::string kOptionsStatistics;
@@ -986,6 +993,8 @@ class DB {
986993
// "rocksdb.block-cache-capacity"
987994
// "rocksdb.block-cache-usage"
988995
// "rocksdb.block-cache-pinned-usage"
996+
// "rocksdb.table-cache-capacity"
997+
// "rocksdb.table-cache-usage"
989998
virtual bool GetIntProperty(ColumnFamilyHandle* column_family,
990999
const Slice& property, uint64_t* value) = 0;
9911000
virtual bool GetIntProperty(const Slice& property, uint64_t* value) {

0 commit comments

Comments
 (0)