Skip to content

Commit

Permalink
docs: add recovery mode for leveled compaction (#86)
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Chi <iskyzh@gmail.com>
  • Loading branch information
skyzh authored Jul 3, 2024
1 parent 77e15ef commit dd333ca
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
4 changes: 4 additions & 0 deletions mini-lsm-book/src/week2-05-manifest.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ src/lsm_storage.rs

Now, you may modify the `open` function to recover the engine state from the manifest file. To recover it, you will need to first generate the list of SSTs you will need to load. You can do this by calling `apply_compaction_result` and recover SST ids in the LSM state. After that, you may iterate the state and load all SSTs (update the sstables hash map). During the process, you will need to compute the maximum SST id and update the `next_sst_id` field. After that, you may create a new memtable using that id and increment the id by one.

If you have implemented leveled compaction, you might have sorted the SSTs every time you apply the compaction result. However, with manifest recover, your sorting logic will be broken, because during the recovery process, you cannot know the start key and the end key of each of the SST. To resolve this, you will need to read the `in_recovery` flag of the `apply_compaction_result` function. During the recovery process, you should not attempt to retrieve the first key of the SST. After the LSM state is recovered and all SSTs are opened, you can do a sorting at the end of the recovery process.

Optionally, you may include the start key and the end key of each of the SSTs in the manifest. This strategy is used in RocksDB/BadgerDB, so that you do not need to distinguish the recovery mode and the normal mode during the compaction apply process.

You may use the mini-lsm-cli to test your implementation.

```
Expand Down
22 changes: 12 additions & 10 deletions mini-lsm-mvcc/src/lsm_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,16 +400,18 @@ impl LsmStorageInner {

next_sst_id += 1;

// Sort SSTs on each level
for (_id, ssts) in &mut state.levels {
ssts.sort_by(|x, y| {
state
.sstables
.get(x)
.unwrap()
.first_key()
.cmp(state.sstables.get(y).unwrap().first_key())
})
// Sort SSTs on each level (only for leveled compaction)
if let CompactionController::Leveled(_) = &compaction_controller {
for (_id, ssts) in &mut state.levels {
ssts.sort_by(|x, y| {
state
.sstables
.get(x)
.unwrap()
.first_key()
.cmp(state.sstables.get(y).unwrap().first_key())
})
}
}

// recover memtables
Expand Down
22 changes: 12 additions & 10 deletions mini-lsm/src/lsm_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,16 +395,18 @@ impl LsmStorageInner {

next_sst_id += 1;

// Sort SSTs on each level
for (_id, ssts) in &mut state.levels {
ssts.sort_by(|x, y| {
state
.sstables
.get(x)
.unwrap()
.first_key()
.cmp(state.sstables.get(y).unwrap().first_key())
})
// Sort SSTs on each level (only for leveled compaction)
if let CompactionController::Leveled(_) = &compaction_controller {
for (_id, ssts) in &mut state.levels {
ssts.sort_by(|x, y| {
state
.sstables
.get(x)
.unwrap()
.first_key()
.cmp(state.sstables.get(y).unwrap().first_key())
})
}
}

// recover memtables
Expand Down

0 comments on commit dd333ca

Please sign in to comment.