Skip to content

Commit

Permalink
fix(compaction): correct level0_file_num_compaction_trigger in simple…
Browse files Browse the repository at this point in the history
… leveled compaction (#112)

* fix(compaction): correct level0_file_num_compaction_trigger in simple leveled compaction

Signed-off-by: zztaki <[email protected]>

* minor nit + sync to mvcc version

Signed-off-by: Alex Chi <[email protected]>

---------

Signed-off-by: zztaki <[email protected]>
Signed-off-by: Alex Chi <[email protected]>
Co-authored-by: Alex Chi <[email protected]>
  • Loading branch information
zztaki and skyzh authored Dec 29, 2024
1 parent 0a91335 commit cc1b174
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
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

0 comments on commit cc1b174

Please sign in to comment.