From bceb2dfe6abe13133620115be2b38a734271360d Mon Sep 17 00:00:00 2001 From: Changyu Bi Date: Fri, 4 Oct 2024 15:01:54 -0700 Subject: [PATCH] Introduce minimum compaction debt requirement for parallel compaction (#13054) Summary: a small CF can trigger parallel compaction that applies to the entire DB. This is because the bottommost file size of a small CF can be too small compared to l0 files when a l0->lbase compaction happens. We prevent this by requiring some minimum on the compaction debt. Pull Request resolved: https://github.com/facebook/rocksdb/pull/13054 Test Plan: updated unit test. Reviewed By: hx235 Differential Revision: D63861042 Pulled By: cbi42 fbshipit-source-id: 43bbf327988ef0ef912cd2fc700e3d096a8d2c18 --- db/column_family.cc | 6 +++++- db/column_family_test.cc | 20 ++++++++++++------- db/import_column_family_test.cc | 2 ++ .../behavior_changes/parallel-compaction.md | 1 + 4 files changed, 21 insertions(+), 8 deletions(-) create mode 100644 unreleased_history/behavior_changes/parallel-compaction.md diff --git a/db/column_family.cc b/db/column_family.cc index 8abad2941f..b5364ed15d 100644 --- a/db/column_family.cc +++ b/db/column_family.cc @@ -901,7 +901,11 @@ uint64_t GetPendingCompactionBytesForCompactionSpeedup( return slowdown_threshold; } - uint64_t size_threshold = bottommost_files_size / kBottommostSizeDivisor; + // Prevent a small CF from triggering parallel compactions for other CFs. + // Require compaction debt to be more than a full L0 to Lbase compaction. + const uint64_t kMinDebtSize = 2 * mutable_cf_options.max_bytes_for_level_base; + uint64_t size_threshold = + std::max(bottommost_files_size / kBottommostSizeDivisor, kMinDebtSize); return std::min(size_threshold, slowdown_threshold); } diff --git a/db/column_family_test.cc b/db/column_family_test.cc index d7751992b5..e7baa28ada 100644 --- a/db/column_family_test.cc +++ b/db/column_family_test.cc @@ -3012,19 +3012,25 @@ TEST_P(ColumnFamilyTest, CompactionSpeedupForCompactionDebt) { ASSERT_OK(db_->Flush(FlushOptions())); { - // 1MB debt is way bigger than bottommost data so definitely triggers - // speedup. VersionStorageInfo* vstorage = cfd->current()->storage_info(); - vstorage->TEST_set_estimated_compaction_needed_bytes(1048576 /* 1MB */, - dbmu); - RecalculateWriteStallConditions(cfd, mutable_cf_options); - ASSERT_EQ(6, dbfull()->TEST_BGCompactionsAllowed()); - // Eight bytes is way smaller than bottommost data so definitely does not // trigger speedup. vstorage->TEST_set_estimated_compaction_needed_bytes(8, dbmu); RecalculateWriteStallConditions(cfd, mutable_cf_options); ASSERT_EQ(1, dbfull()->TEST_BGCompactionsAllowed()); + + // 1MB is much larger than bottommost level size. However, since it's too + // small in terms of absolute size, it does not trigger parallel compaction + // in this case (see GetPendingCompactionBytesForCompactionSpeedup()). + vstorage->TEST_set_estimated_compaction_needed_bytes(1048576 /* 1MB */, + dbmu); + RecalculateWriteStallConditions(cfd, mutable_cf_options); + ASSERT_EQ(1, dbfull()->TEST_BGCompactionsAllowed()); + + vstorage->TEST_set_estimated_compaction_needed_bytes( + 2 * mutable_cf_options.max_bytes_for_level_base, dbmu); + RecalculateWriteStallConditions(cfd, mutable_cf_options); + ASSERT_EQ(6, dbfull()->TEST_BGCompactionsAllowed()); } } diff --git a/db/import_column_family_test.cc b/db/import_column_family_test.cc index 89fdbb7e30..c659ba6aed 100644 --- a/db/import_column_family_test.cc +++ b/db/import_column_family_test.cc @@ -951,6 +951,8 @@ TEST_F(ImportColumnFamilyTest, AssignEpochNumberToMultipleCF) { Options options = CurrentOptions(); options.level_compaction_dynamic_level_bytes = true; options.max_background_jobs = 8; + // Always allow parallel compaction + options.soft_pending_compaction_bytes_limit = 10; env_->SetBackgroundThreads(2, Env::LOW); env_->SetBackgroundThreads(0, Env::BOTTOM); CreateAndReopenWithCF({"CF1", "CF2"}, options); diff --git a/unreleased_history/behavior_changes/parallel-compaction.md b/unreleased_history/behavior_changes/parallel-compaction.md new file mode 100644 index 0000000000..87f5bb76e6 --- /dev/null +++ b/unreleased_history/behavior_changes/parallel-compaction.md @@ -0,0 +1 @@ +* Fix an issue in level compaction where a small CF with small compaction debt can cause the DB to allow parallel compactions. (#13054) \ No newline at end of file