diff --git a/mini-lsm-book/src/week2-06-wal.md b/mini-lsm-book/src/week2-06-wal.md index f82331a5..fb4ebc99 100644 --- a/mini-lsm-book/src/week2-06-wal.md +++ b/mini-lsm-book/src/week2-06-wal.md @@ -55,7 +55,7 @@ If WAL is enabled, you will need to recover the memtables based on WALs when loa cargo run --bin mini-lsm-cli -- --enable-wal ``` -Remember to recover the correct `next_sst_id` from the state, which should be `max{memtable id, sst id}` + 1. In your `close` function, you should not flush memtables to SSTs if `enable_wal` is set to true, as WAL itself provides persistency. +Remember to recover the correct `next_sst_id` from the state, which should be `max{memtable id, sst id}` + 1. In your `close` function, you should not flush memtables to SSTs if `enable_wal` is set to true, as WAL itself provides persistency. You should wait until all compaction and flush threads to exit before closing the database. ## Test Your Understanding diff --git a/mini-lsm-mvcc/src/lsm_storage.rs b/mini-lsm-mvcc/src/lsm_storage.rs index 886c5148..0504cda7 100644 --- a/mini-lsm-mvcc/src/lsm_storage.rs +++ b/mini-lsm-mvcc/src/lsm_storage.rs @@ -188,12 +188,6 @@ impl MiniLsm { self.compaction_notifier.send(()).ok(); self.flush_notifier.send(()).ok(); - if self.inner.options.enable_wal { - self.inner.sync()?; - self.inner.sync_dir()?; - return Ok(()); - } - let mut compaction_thread = self.compaction_thread.lock(); if let Some(compaction_thread) = compaction_thread.take() { compaction_thread @@ -207,6 +201,12 @@ impl MiniLsm { .map_err(|e| anyhow::anyhow!("{:?}", e))?; } + if self.inner.options.enable_wal { + self.inner.sync()?; + self.inner.sync_dir()?; + return Ok(()); + } + // create memtable and skip updating manifest if !self.inner.state.read().memtable.is_empty() { self.inner diff --git a/mini-lsm/src/lsm_storage.rs b/mini-lsm/src/lsm_storage.rs index d16223f3..738fdbde 100644 --- a/mini-lsm/src/lsm_storage.rs +++ b/mini-lsm/src/lsm_storage.rs @@ -188,12 +188,6 @@ impl MiniLsm { self.compaction_notifier.send(()).ok(); self.flush_notifier.send(()).ok(); - if self.inner.options.enable_wal { - self.inner.sync()?; - self.inner.sync_dir()?; - return Ok(()); - } - let mut compaction_thread = self.compaction_thread.lock(); if let Some(compaction_thread) = compaction_thread.take() { compaction_thread @@ -207,6 +201,12 @@ impl MiniLsm { .map_err(|e| anyhow::anyhow!("{:?}", e))?; } + if self.inner.options.enable_wal { + self.inner.sync()?; + self.inner.sync_dir()?; + return Ok(()); + } + // create memtable and skip updating manifest if !self.inner.state.read().memtable.is_empty() { self.inner diff --git a/mini-lsm/src/tests/harness.rs b/mini-lsm/src/tests/harness.rs index 30afa80c..d55b32a9 100644 --- a/mini-lsm/src/tests/harness.rs +++ b/mini-lsm/src/tests/harness.rs @@ -401,12 +401,13 @@ pub fn check_compaction_ratio(storage: Arc) { } pub fn dump_files_in_dir(path: impl AsRef) { + println!("--- DIR DUMP ---"); for f in path.as_ref().read_dir().unwrap() { let f = f.unwrap(); + print!("{}", f.path().display()); println!( - "{}, size={:.3}KB", - f.path().display(), + ", size={:.3}KB", f.metadata().unwrap().size() as f64 / 1024.0 - ) + ); } }