Skip to content

Commit 2dda7a0

Browse files
ajkrfacebook-github-bot
authored andcommitted
Detect compaction pressure at lower debt ratios (#12236)
Summary: This PR significantly reduces the compaction pressure threshold introduced in facebook/rocksdb#12130 by a factor of 64x. The original number was too high to trigger in scenarios where compaction parallelism was needed. Pull Request resolved: facebook/rocksdb#12236 Reviewed By: cbi42 Differential Revision: D52765685 Pulled By: ajkr fbshipit-source-id: 8298e966933b485de24f63165a00e672cb9db6c4
1 parent 21d5a8f commit 2dda7a0

File tree

4 files changed

+48
-23
lines changed

4 files changed

+48
-23
lines changed

db/column_family.cc

+2-3
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ uint64_t GetPendingCompactionBytesForCompactionSpeedup(
870870
const VersionStorageInfo* vstorage) {
871871
// Compaction debt relatively large compared to the stable (bottommost) data
872872
// size indicates compaction fell behind.
873-
const uint64_t kBottommostSizeMultiplier = 8;
873+
const uint64_t kBottommostSizeDivisor = 8;
874874
// Meaningful progress toward the slowdown trigger is another good indication.
875875
const uint64_t kSlowdownTriggerDivisor = 4;
876876

@@ -890,8 +890,7 @@ uint64_t GetPendingCompactionBytesForCompactionSpeedup(
890890
return slowdown_threshold;
891891
}
892892

893-
uint64_t size_threshold =
894-
MultiplyCheckOverflow(bottommost_files_size, kBottommostSizeMultiplier);
893+
uint64_t size_threshold = bottommost_files_size / kBottommostSizeDivisor;
895894
return std::min(size_threshold, slowdown_threshold);
896895
}
897896
} // anonymous namespace

db/db_compaction_test.cc

+34-20
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class CompactionStatsCollector : public EventListener {
7676
class DBCompactionTest : public DBTestBase {
7777
public:
7878
DBCompactionTest()
79-
: DBTestBase("db_compaction_test", /*env_do_fsync=*/true) {}
79+
: DBTestBase("db_compaction_test", /*env_do_fsync=*/false) {}
8080

8181
protected:
8282
/*
@@ -121,7 +121,7 @@ class DBCompactionTestWithParam
121121
public testing::WithParamInterface<std::tuple<uint32_t, bool>> {
122122
public:
123123
DBCompactionTestWithParam()
124-
: DBTestBase("db_compaction_test", /*env_do_fsync=*/true) {
124+
: DBTestBase("db_compaction_test", /*env_do_fsync=*/false) {
125125
max_subcompactions_ = std::get<0>(GetParam());
126126
exclusive_manual_compaction_ = std::get<1>(GetParam());
127127
}
@@ -140,7 +140,7 @@ class DBCompactionTestWithBottommostParam
140140
std::tuple<BottommostLevelCompaction, bool>> {
141141
public:
142142
DBCompactionTestWithBottommostParam()
143-
: DBTestBase("db_compaction_test", /*env_do_fsync=*/true) {
143+
: DBTestBase("db_compaction_test", /*env_do_fsync=*/false) {
144144
bottommost_level_compaction_ = std::get<0>(GetParam());
145145
}
146146

@@ -160,7 +160,7 @@ class DBCompactionWaitForCompactTest
160160
std::tuple<bool, bool, bool, std::chrono::microseconds>> {
161161
public:
162162
DBCompactionWaitForCompactTest()
163-
: DBTestBase("db_compaction_test", /*env_do_fsync=*/true) {
163+
: DBTestBase("db_compaction_test", /*env_do_fsync=*/false) {
164164
abort_on_pause_ = std::get<0>(GetParam());
165165
flush_ = std::get<1>(GetParam());
166166
close_db_ = std::get<2>(GetParam());
@@ -845,6 +845,20 @@ TEST_F(DBCompactionTest, BGCompactionsAllowed) {
845845
options.memtable_factory.reset(
846846
test::NewSpecialSkipListFactory(kNumKeysPerFile));
847847

848+
CreateAndReopenWithCF({"one", "two", "three"}, options);
849+
850+
Random rnd(301);
851+
for (int cf = 0; cf < 4; cf++) {
852+
// Make a trivial L1 for L0 to compact into. L2 will be large so debt ratio
853+
// will not cause compaction pressure.
854+
ASSERT_OK(Put(cf, Key(0), rnd.RandomString(102400)));
855+
ASSERT_OK(Flush(cf));
856+
MoveFilesToLevel(2, cf);
857+
ASSERT_OK(Put(cf, Key(0), ""));
858+
ASSERT_OK(Flush(cf));
859+
MoveFilesToLevel(1, cf);
860+
}
861+
848862
// Block all threads in thread pool.
849863
const size_t kTotalTasks = 4;
850864
env_->SetBackgroundThreads(4, Env::LOW);
@@ -855,9 +869,6 @@ TEST_F(DBCompactionTest, BGCompactionsAllowed) {
855869
sleeping_tasks[i].WaitUntilSleeping();
856870
}
857871

858-
CreateAndReopenWithCF({"one", "two", "three"}, options);
859-
860-
Random rnd(301);
861872
for (int cf = 0; cf < 4; cf++) {
862873
for (int num = 0; num < options.level0_file_num_compaction_trigger; num++) {
863874
for (int i = 0; i < kNumKeysPerFile; i++) {
@@ -6150,7 +6161,7 @@ class CompactionPriTest : public DBTestBase,
61506161
public testing::WithParamInterface<uint32_t> {
61516162
public:
61526163
CompactionPriTest()
6153-
: DBTestBase("compaction_pri_test", /*env_do_fsync=*/true) {
6164+
: DBTestBase("compaction_pri_test", /*env_do_fsync=*/false) {
61546165
compaction_pri_ = GetParam();
61556166
}
61566167

@@ -6270,26 +6281,30 @@ TEST_F(DBCompactionTest, PersistRoundRobinCompactCursor) {
62706281

62716282
TEST_P(RoundRobinSubcompactionsAgainstPressureToken, PressureTokenTest) {
62726283
const int kKeysPerBuffer = 100;
6284+
const int kNumSubcompactions = 2;
6285+
const int kFilesPerLevel = 50;
62736286
Options options = CurrentOptions();
6274-
options.num_levels = 4;
6287+
options.num_levels = 3;
62756288
options.max_bytes_for_level_multiplier = 2;
62766289
options.level0_file_num_compaction_trigger = 4;
62776290
options.target_file_size_base = kKeysPerBuffer * 1024;
62786291
options.compaction_pri = CompactionPri::kRoundRobin;
6279-
options.max_bytes_for_level_base = 8 * kKeysPerBuffer * 1024;
6292+
// Target size is chosen so that filling the level with `kFilesPerLevel` files
6293+
// will make it oversized by `kNumSubcompactions` files.
6294+
options.max_bytes_for_level_base =
6295+
(kFilesPerLevel - kNumSubcompactions) * kKeysPerBuffer * 1024;
62806296
options.disable_auto_compactions = true;
6281-
// Setup 7 threads but limited subcompactions so that
6282-
// RoundRobin requires extra compactions from reserved threads
6297+
// Setup `kNumSubcompactions` threads but limited subcompactions so
6298+
// that RoundRobin requires extra compactions from reserved threads
62836299
options.max_subcompactions = 1;
6284-
options.max_background_compactions = 7;
6300+
options.max_background_compactions = kNumSubcompactions;
62856301
options.max_compaction_bytes = 100000000;
62866302
DestroyAndReopen(options);
6287-
env_->SetBackgroundThreads(7, Env::LOW);
6303+
env_->SetBackgroundThreads(kNumSubcompactions, Env::LOW);
62886304

62896305
Random rnd(301);
6290-
const std::vector<int> files_per_level = {0, 15, 25};
62916306
for (int lvl = 2; lvl > 0; lvl--) {
6292-
for (int i = 0; i < files_per_level[lvl]; i++) {
6307+
for (int i = 0; i < kFilesPerLevel; i++) {
62936308
for (int j = 0; j < kKeysPerBuffer; j++) {
62946309
// Add (lvl-1) to ensure nearly equivallent number of files
62956310
// in L2 are overlapped with fils selected to compact from
@@ -6300,9 +6315,8 @@ TEST_P(RoundRobinSubcompactionsAgainstPressureToken, PressureTokenTest) {
63006315
ASSERT_OK(Flush());
63016316
}
63026317
MoveFilesToLevel(lvl);
6303-
ASSERT_EQ(files_per_level[lvl], NumTableFilesAtLevel(lvl, 0));
6318+
ASSERT_EQ(kFilesPerLevel, NumTableFilesAtLevel(lvl, 0));
63046319
}
6305-
// 15 files in L1; 25 files in L2
63066320

63076321
// This is a variable for making sure the following callback is called
63086322
// and the assertions in it are indeed excuted.
@@ -6311,10 +6325,10 @@ TEST_P(RoundRobinSubcompactionsAgainstPressureToken, PressureTokenTest) {
63116325
"CompactionJob::GenSubcompactionBoundaries:0", [&](void* arg) {
63126326
uint64_t num_planned_subcompactions = *(static_cast<uint64_t*>(arg));
63136327
if (grab_pressure_token_) {
6314-
// 7 files are selected for round-robin under auto
6328+
// `kNumSubcompactions` files are selected for round-robin under auto
63156329
// compaction. The number of planned subcompaction is restricted by
63166330
// the limited number of max_background_compactions
6317-
ASSERT_EQ(num_planned_subcompactions, 7);
6331+
ASSERT_EQ(num_planned_subcompactions, kNumSubcompactions);
63186332
} else {
63196333
ASSERT_EQ(num_planned_subcompactions, 1);
63206334
}

db/db_test2.cc

+11
Original file line numberDiff line numberDiff line change
@@ -3860,6 +3860,17 @@ TEST_F(DBTest2, LowPriWrite) {
38603860
int64_t* rate_bytes_per_sec = static_cast<int64_t*>(arg);
38613861
ASSERT_EQ(1024 * 1024, *rate_bytes_per_sec);
38623862
});
3863+
3864+
// Make a trivial L5 for L0 to compact into. L6 will be large so debt ratio
3865+
// will not cause compaction pressure.
3866+
Random rnd(301);
3867+
ASSERT_OK(Put("", rnd.RandomString(102400)));
3868+
ASSERT_OK(Flush());
3869+
MoveFilesToLevel(6);
3870+
ASSERT_OK(Put("", ""));
3871+
ASSERT_OK(Flush());
3872+
MoveFilesToLevel(5);
3873+
38633874
// Block compaction
38643875
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency({
38653876
{"DBTest.LowPriWrite:0", "DBImpl::BGWorkCompaction"},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Reduced the compaction debt ratio trigger for scheduling parallel compactions

0 commit comments

Comments
 (0)