diff --git a/db/db_impl.cc b/db/db_impl.cc index 06ea1543b..85fbc112d 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -2443,6 +2443,10 @@ Status DBImpl::SetOptions(ColumnFamilyHandle* column_family, s = cfd->SetOptions(options_map); if (s.ok()) { new_options = *cfd->GetLatestMutableCFOptions(); + // Append new version to recompute compaction score. + VersionEdit dummy_edit; + versions_->LogAndApply(cfd, new_options, &dummy_edit, &mutex_, + directories_.GetDbDir()); // Trigger possible flush/compactions. This has to be before we persist // options to file, otherwise there will be a deadlock with writer // thread. diff --git a/db/db_options_test.cc b/db/db_options_test.cc index a164b29b9..cd6c7f71f 100644 --- a/db/db_options_test.cc +++ b/db/db_options_test.cc @@ -76,6 +76,15 @@ TEST_F(DBOptionsTest, GetLatestOptions) { GetMutableCFOptionsMap(dbfull()->GetOptions(handles_[1]))); } +TEST_F(DBOptionsTest, SetOptionsAndReopen) { + Random rnd(1044); + auto rand_opts = GetRandomizedMutableCFOptionsMap(&rnd); + ASSERT_OK(dbfull()->SetOptions(rand_opts)); + // Verify if DB can be reopen after setting options. + Options options; + ASSERT_OK(TryReopen(options)); +} + TEST_F(DBOptionsTest, EnableAutoCompactionAndTriggerStall) { const std::string kValue(1024, 'v'); for (int method_type = 0; method_type < 2; method_type++) { @@ -179,6 +188,24 @@ TEST_F(DBOptionsTest, EnableAutoCompactionAndTriggerStall) { } } +TEST_F(DBOptionsTest, SetOptionsMayTriggerCompaction) { + Options options; + options.create_if_missing = true; + options.level0_file_num_compaction_trigger = 1000; + Reopen(options); + for (int i = 0; i < 3; i++) { + // Need to insert two keys to avoid trivial move. + ASSERT_OK(Put("foo", ToString(i))); + ASSERT_OK(Put("bar", ToString(i))); + Flush(); + } + ASSERT_EQ("3", FilesPerLevel()); + ASSERT_OK( + dbfull()->SetOptions({{"level0_file_num_compaction_trigger", "3"}})); + dbfull()->TEST_WaitForCompact(); + ASSERT_EQ("0,1", FilesPerLevel()); +} + #endif // ROCKSDB_LITE } // namespace rocksdb