From fbbf0d1456f8d872d100f7cbfceb9f9b89249664 Mon Sep 17 00:00:00 2001 From: Siying Dong Date: Tue, 14 Jan 2014 11:04:27 -0800 Subject: [PATCH] Pre-calculate whether to slow down for too many level 0 files Summary: Currently in DBImpl::MakeRoomForWrite(), we do "versions_->NumLevelFiles(0) >= options_.level0_slowdown_writes_trigger" to check whether the writer thread needs to slow down. However, versions_->NumLevelFiles(0) is slightly more expensive than we expected. By caching the result of the comparison when installing a new version, we can avoid this function call every time. Test Plan: make all check Manually trigger this behavior by applying universal compaction style and make sure inserts are made slow after there are certain number of files. Reviewers: haobo, kailiu, igor Reviewed By: kailiu CC: nkg-, leveldb Differential Revision: https://reviews.facebook.net/D15141 --- db/db_impl.cc | 3 +-- db/version_set.cc | 4 ++++ db/version_set.h | 8 ++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/db/db_impl.cc b/db/db_impl.cc index 12e07868f..ed5853336 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -3166,8 +3166,7 @@ Status DBImpl::MakeRoomForWrite(bool force, break; } else if ( allow_delay && - versions_->NumLevelFiles(0) >= - options_.level0_slowdown_writes_trigger) { + versions_->NeedSlowdownForNumLevel0Files()) { // We are getting close to hitting a hard limit on the number of // L0 files. Rather than delaying a single write by several // seconds when we hit the hard limit, start delaying each diff --git a/db/version_set.cc b/db/version_set.cc index 46cdfaa61..7a1f5cbf8 100644 --- a/db/version_set.cc +++ b/db/version_set.cc @@ -1148,6 +1148,7 @@ VersionSet::VersionSet(const std::string& dbname, num_levels_(options_->num_levels), dummy_versions_(this), current_(nullptr), + need_slowdown_for_num_level0_files(false), compactions_in_progress_(options_->num_levels), current_version_number_(0), last_observed_manifest_size_(0), @@ -1199,6 +1200,9 @@ void VersionSet::AppendVersion(Version* v) { current_->Unref(); } current_ = v; + need_slowdown_for_num_level0_files = + (options_->level0_slowdown_writes_trigger >= 0 && current_ != nullptr && + NumLevelFiles(0) >= options_->level0_slowdown_writes_trigger); v->Ref(); // Append to linked list diff --git a/db/version_set.h b/db/version_set.h index 75b529942..85ff2ff36 100644 --- a/db/version_set.h +++ b/db/version_set.h @@ -250,6 +250,12 @@ class VersionSet { // Return the current version. Version* current() const { return current_; } + // A Flag indicating whether write needs to slowdown because of there are + // too many number of level0 files. + bool NeedSlowdownForNumLevel0Files() const { + return need_slowdown_for_num_level0_files; + } + // Return the current manifest file number uint64_t ManifestFileNumber() const { return manifest_file_number_; } @@ -489,6 +495,8 @@ class VersionSet { Version dummy_versions_; // Head of circular doubly-linked list of versions. Version* current_; // == dummy_versions_.prev_ + bool need_slowdown_for_num_level0_files; + // Per-level key at which the next compaction at that level should start. // Either an empty string, or a valid InternalKey. std::string* compact_pointer_;