From fbb73a4ac3485da1924cec8b5995a28581ef3f23 Mon Sep 17 00:00:00 2001 From: Dhruba Borthakur Date: Tue, 20 Nov 2012 15:45:41 -0800 Subject: [PATCH] Support to disable background compactions on a database. Summary: This option is needed for fast bulk uploads. The goal is to load all the data into files in L0 without any interference from background compactions. Test Plan: make clean check Reviewers: sheki Reviewed By: sheki CC: leveldb Differential Revision: https://reviews.facebook.net/D6849 --- db/db_bench.cc | 7 +++++++ db/db_impl.cc | 4 ++-- include/leveldb/options.h | 4 ++++ util/options.cc | 5 ++++- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/db/db_bench.cc b/db/db_bench.cc index e3fd3e910c..96bb44d67d 100644 --- a/db/db_bench.cc +++ b/db/db_bench.cc @@ -218,6 +218,9 @@ static int FLAGS_max_grandparent_overlap_factor; // Run read only benchmarks. static bool FLAGS_read_only = false; +// Do not auto trigger compactions +static bool FLAGS_disable_auto_compactions = false; + extern bool useOsBuffer; extern bool useFsReadAhead; extern bool useMmapRead; @@ -974,6 +977,7 @@ class Benchmark { options.table_cache_numshardbits = FLAGS_table_cache_numshardbits; options.max_grandparent_overlap_factor = FLAGS_max_grandparent_overlap_factor; + options.disable_auto_compactions = FLAGS_disable_auto_compactions; Status s; if(FLAGS_read_only) { s = DB::OpenForReadOnly(options, FLAGS_db, &db_); @@ -1424,6 +1428,9 @@ int main(int argc, char** argv) { } else if (sscanf(argv[i], "--max_grandparent_overlap_factor=%d%c", &n, &junk) == 1) { FLAGS_max_grandparent_overlap_factor = n; + } else if (sscanf(argv[i], "--disable_auto_compactions=%d%c", + &n, &junk) == 1 && (n == 0 || n ==1)) { + FLAGS_disable_auto_compactions = n; } else { fprintf(stderr, "Invalid flag '%s'\n", argv[i]); exit(1); diff --git a/db/db_impl.cc b/db/db_impl.cc index 6898b8ce87..141a7407cb 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -1012,7 +1012,7 @@ Status DBImpl::BackgroundCompaction(bool* madeProgress, } } - Compaction* c; + Compaction* c = NULL; bool is_manual = (manual_compaction_ != NULL) && (manual_compaction_->in_progress == false); InternalKey manual_end; @@ -1031,7 +1031,7 @@ Status DBImpl::BackgroundCompaction(bool* madeProgress, (m->begin ? m->begin->DebugString().c_str() : "(begin)"), (m->end ? m->end->DebugString().c_str() : "(end)"), (m->done ? "(end)" : manual_end.DebugString().c_str())); - } else { + } else if (!options_.disable_auto_compactions) { c = versions_->PickCompaction(); } diff --git a/include/leveldb/options.h b/include/leveldb/options.h index a76e6b3db9..2ae8c31bbe 100644 --- a/include/leveldb/options.h +++ b/include/leveldb/options.h @@ -330,6 +330,10 @@ struct Options { bool (*CompactionFilter)(void* compaction_filter_args, int level, const Slice& key, const Slice& existing_value, Slice** new_value); + + // Disable automatic compactions. Manual compactions can still + // be issued on this database. + bool disable_auto_compactions; }; // Options that control read operations diff --git a/util/options.cc b/util/options.cc index 591e6bf9a5..4907c45af4 100644 --- a/util/options.cc +++ b/util/options.cc @@ -51,7 +51,8 @@ Options::Options() no_block_cache(false), table_cache_numshardbits(4), compaction_filter_args(NULL), - CompactionFilter(NULL) { + CompactionFilter(NULL), + disable_auto_compactions(false) { } void @@ -134,6 +135,8 @@ Options::Dump( compaction_filter_args); Log(log," Options.CompactionFilter: %p", CompactionFilter); + Log(log," Options.disable_auto_compactions: %d", + disable_auto_compactions); } // Options::Dump } // namespace leveldb