Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compaction): correct level0_file_num_compaction_trigger in simple leveled compaction #112

Merged
merged 2 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions mini-lsm-mvcc/src/compact/simple_leveled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ impl SimpleLeveledCompactionController {
level_sizes.push(files.len());
}

// check level0_file_num_compaction_trigger for compaction of L0 to L1
if snapshot.l0_sstables.len() >= self.options.level0_file_num_compaction_trigger {
println!(
"compaction triggered at level 0 because L0 has {} SSTs >= {}",
snapshot.l0_sstables.len(),
self.options.level0_file_num_compaction_trigger
);
return Some(SimpleLeveledCompactionTask {
upper_level: None,
upper_level_sst_ids: snapshot.l0_sstables.clone(),
lower_level: 1,
lower_level_sst_ids: snapshot.levels[0].1.clone(),
is_lower_level_bottom_level: false,
});
}

for i in 0..self.options.max_levels {
if i == 0
&& snapshot.l0_sstables.len() < self.options.level0_file_num_compaction_trigger
Expand Down
35 changes: 23 additions & 12 deletions mini-lsm/src/compact/simple_leveled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,34 @@ impl SimpleLeveledCompactionController {
&self,
snapshot: &LsmStorageState,
) -> Option<SimpleLeveledCompactionTask> {
if self.options.max_levels == 0 {
return None;
}

let mut level_sizes = Vec::new();
level_sizes.push(snapshot.l0_sstables.len());
for (_, files) in &snapshot.levels {
level_sizes.push(files.len());
}

for i in 0..self.options.max_levels {
if i == 0
&& snapshot.l0_sstables.len() < self.options.level0_file_num_compaction_trigger
{
continue;
}
// check level0_file_num_compaction_trigger for compaction of L0 to L1
if snapshot.l0_sstables.len() >= self.options.level0_file_num_compaction_trigger {
println!(
"compaction triggered at level 0 because L0 has {} SSTs >= {}",
snapshot.l0_sstables.len(),
self.options.level0_file_num_compaction_trigger
);
return Some(SimpleLeveledCompactionTask {
upper_level: None,
upper_level_sst_ids: snapshot.l0_sstables.clone(),
lower_level: 1,
lower_level_sst_ids: snapshot.levels[0].1.clone(),
is_lower_level_bottom_level: false,
});
}

// check size_ratio_percent for compaction of other levels (>= L1)
for i in 1..self.options.max_levels {
let lower_level = i + 1;
let size_ratio = level_sizes[lower_level] as f64 / level_sizes[i] as f64;
if size_ratio < self.options.size_ratio_percent as f64 / 100.0 {
Expand All @@ -58,12 +73,8 @@ impl SimpleLeveledCompactionController {
i, lower_level, size_ratio
);
return Some(SimpleLeveledCompactionTask {
upper_level: if i == 0 { None } else { Some(i) },
upper_level_sst_ids: if i == 0 {
snapshot.l0_sstables.clone()
} else {
snapshot.levels[i - 1].1.clone()
},
upper_level: Some(i),
upper_level_sst_ids: snapshot.levels[i - 1].1.clone(),
lower_level,
lower_level_sst_ids: snapshot.levels[lower_level - 1].1.clone(),
is_lower_level_bottom_level: lower_level == self.options.max_levels,
Expand Down