From dd333ca5a7c1684357f335c304f885b287fbf8e0 Mon Sep 17 00:00:00 2001 From: Alex Chi Z Date: Tue, 2 Jul 2024 20:36:07 -0400 Subject: [PATCH] docs: add recovery mode for leveled compaction (#86) Signed-off-by: Alex Chi --- mini-lsm-book/src/week2-05-manifest.md | 4 ++++ mini-lsm-mvcc/src/lsm_storage.rs | 22 ++++++++++++---------- mini-lsm/src/lsm_storage.rs | 22 ++++++++++++---------- 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/mini-lsm-book/src/week2-05-manifest.md b/mini-lsm-book/src/week2-05-manifest.md index a4c4b7ac..d7fb51da 100644 --- a/mini-lsm-book/src/week2-05-manifest.md +++ b/mini-lsm-book/src/week2-05-manifest.md @@ -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. ``` diff --git a/mini-lsm-mvcc/src/lsm_storage.rs b/mini-lsm-mvcc/src/lsm_storage.rs index ab32e7b5..a136f480 100644 --- a/mini-lsm-mvcc/src/lsm_storage.rs +++ b/mini-lsm-mvcc/src/lsm_storage.rs @@ -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 diff --git a/mini-lsm/src/lsm_storage.rs b/mini-lsm/src/lsm_storage.rs index 6b69e69b..1162a783 100644 --- a/mini-lsm/src/lsm_storage.rs +++ b/mini-lsm/src/lsm_storage.rs @@ -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