From 355fa94365b4cf8b92938bee0becb18bc4526d82 Mon Sep 17 00:00:00 2001 From: SherlockNoMad Date: Mon, 7 Dec 2015 10:51:08 -0800 Subject: [PATCH] EstimatedNumKeys Counter Inaccurate --- db/version_builder.cc | 3 ++- db/version_set.cc | 33 +++++++++++++++++++++++++-------- db/version_set.h | 11 +++++++++-- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/db/version_builder.cc b/db/version_builder.cc index 6717885b52..d6b1858a68 100644 --- a/db/version_builder.cc +++ b/db/version_builder.cc @@ -338,7 +338,8 @@ class VersionBuilder::Rep { void MaybeAddFile(VersionStorageInfo* vstorage, int level, FileMetaData* f) { if (levels_[level].deleted_files.count(f->fd.GetNumber()) > 0) { - // File is deleted: do nothing + // f is to-be-delected table file + vstorage->RemoveCurrentStats(f); } else { vstorage->AddFile(level, f, info_log_); } diff --git a/db/version_set.cc b/db/version_set.cc index 8d710f017d..ea8dde9eed 100644 --- a/db/version_set.cc +++ b/db/version_set.cc @@ -738,24 +738,24 @@ uint64_t VersionStorageInfo::GetEstimatedActiveKeys() const { // (2) keys are directly overwritten // (3) deletion on non-existing keys // (4) low number of samples - if (num_samples_ == 0) { + if (current_num_samples_ == 0) { return 0; } - if (accumulated_num_non_deletions_ <= accumulated_num_deletions_) { + if (current_num_non_deletions_ <= current_num_deletions_) { return 0; } - uint64_t est = accumulated_num_non_deletions_ - accumulated_num_deletions_; + uint64_t est = current_num_non_deletions_ - current_num_deletions_; uint64_t file_count = 0; for (int level = 0; level < num_levels_; ++level) { file_count += files_[level].size(); } - if (num_samples_ < file_count) { + if (current_num_samples_ < file_count) { // casting to avoid overflowing - return (est * static_cast(file_count) / num_samples_); + return (est * static_cast(file_count) / current_num_samples_); } else { return est; } @@ -826,7 +826,9 @@ VersionStorageInfo::VersionStorageInfo( accumulated_raw_value_size_(0), accumulated_num_non_deletions_(0), accumulated_num_deletions_(0), - num_samples_(0), + current_num_non_deletions_(0), + current_num_deletions_(0), + current_num_samples_(0), estimated_compaction_needed_bytes_(0), finalized_(false) { if (ref_vstorage != nullptr) { @@ -836,7 +838,9 @@ VersionStorageInfo::VersionStorageInfo( accumulated_num_non_deletions_ = ref_vstorage->accumulated_num_non_deletions_; accumulated_num_deletions_ = ref_vstorage->accumulated_num_deletions_; - num_samples_ = ref_vstorage->num_samples_; + current_num_non_deletions_ = ref_vstorage->current_num_non_deletions_; + current_num_deletions_ = ref_vstorage->current_num_deletions_; + current_num_samples_ = ref_vstorage->current_num_samples_; } } @@ -993,7 +997,20 @@ void VersionStorageInfo::UpdateAccumulatedStats(FileMetaData* file_meta) { accumulated_num_non_deletions_ += file_meta->num_entries - file_meta->num_deletions; accumulated_num_deletions_ += file_meta->num_deletions; - num_samples_++; + + current_num_non_deletions_ += + file_meta->num_entries - file_meta->num_deletions; + current_num_deletions_ += file_meta->num_deletions; + current_num_samples_++; +} + +void VersionStorageInfo::RemoveCurrentStats(FileMetaData* file_meta) { + if (file_meta->init_stats_from_file) { + current_num_non_deletions_ -= + file_meta->num_entries - file_meta->num_deletions; + current_num_deletions_ -= file_meta->num_deletions; + current_num_samples_--; + } } void Version::UpdateAccumulatedStats(bool update_stats) { diff --git a/db/version_set.h b/db/version_set.h index 0c0f3cc9dc..8d3d2c9c8e 100644 --- a/db/version_set.h +++ b/db/version_set.h @@ -111,6 +111,9 @@ class VersionStorageInfo { // Update the accumulated stats from a file-meta. void UpdateAccumulatedStats(FileMetaData* file_meta); + // Decrease the current stat form a to-be-delected file-meta + void RemoveCurrentStats(FileMetaData* file_meta); + void ComputeCompensatedSizes(); // Updates internal structures that keep track of compaction scores @@ -400,8 +403,12 @@ class VersionStorageInfo { uint64_t accumulated_num_non_deletions_; // total number of deletion entries uint64_t accumulated_num_deletions_; - // the number of samples - uint64_t num_samples_; + // current number of non_deletion entries + uint64_t current_num_non_deletions_; + // current number of delection entries + uint64_t current_num_deletions_; + // current number of file samples + uint64_t current_num_samples_; // Estimated bytes needed to be compacted until all levels' size is down to // target sizes. uint64_t estimated_compaction_needed_bytes_;