From e622bee18a724d89e6df4609852a0e54f7f56c62 Mon Sep 17 00:00:00 2001 From: Alex Chi Z Date: Wed, 17 Jan 2024 16:23:58 +0800 Subject: [PATCH] check compaction converge for simple leveled Signed-off-by: Alex Chi Z --- mini-lsm-starter/src/block/iterator.rs | 3 ++- mini-lsm-starter/src/table/builder.rs | 6 ++++-- mini-lsm-starter/src/table/iterator.rs | 3 ++- mini-lsm-week-1/src/block/builder.rs | 2 +- mini-lsm-week-1/src/table.rs | 3 ++- mini-lsm/src/bin/compaction_simulator.rs | 5 ++++- 6 files changed, 15 insertions(+), 7 deletions(-) diff --git a/mini-lsm-starter/src/block/iterator.rs b/mini-lsm-starter/src/block/iterator.rs index 77a832f..728dbc8 100644 --- a/mini-lsm-starter/src/block/iterator.rs +++ b/mini-lsm-starter/src/block/iterator.rs @@ -64,7 +64,8 @@ impl BlockIterator { } /// Seek to the first key that >= `key`. - /// Note: You should assume the key-value pairs in the block are sorted when being added by callers. + /// Note: You should assume the key-value pairs in the block are sorted when being added by + /// callers. pub fn seek_to_key(&mut self, key: &[u8]) { unimplemented!() } diff --git a/mini-lsm-starter/src/table/builder.rs b/mini-lsm-starter/src/table/builder.rs index b89dab8..35a47d6 100644 --- a/mini-lsm-starter/src/table/builder.rs +++ b/mini-lsm-starter/src/table/builder.rs @@ -22,13 +22,15 @@ impl SsTableBuilder { } /// Adds a key-value pair to SSTable. - /// Note: You should split a new block when the current block is full.(`std::mem::replace` may be of help here) + /// Note: You should split a new block when the current block is full.(`std::mem::replace` may + /// be of help here) pub fn add(&mut self, key: &[u8], value: &[u8]) { unimplemented!() } /// Get the estimated size of the SSTable. - /// Since the data blocks contain much more data than meta blocks, just return the size of data blocks here. + /// Since the data blocks contain much more data than meta blocks, just return the size of data + /// blocks here. pub fn estimated_size(&self) -> usize { unimplemented!() } diff --git a/mini-lsm-starter/src/table/iterator.rs b/mini-lsm-starter/src/table/iterator.rs index 7c7ae4d..e9da435 100644 --- a/mini-lsm-starter/src/table/iterator.rs +++ b/mini-lsm-starter/src/table/iterator.rs @@ -28,7 +28,8 @@ impl SsTableIterator { } /// Seek to the first key-value pair which >= `key`. - /// Note: You probably want to review the handout for detailed explanation when implementing this function. + /// Note: You probably want to review the handout for detailed explanation when implementing + /// this function. pub fn seek_to_key(&mut self, key: &[u8]) -> Result<()> { unimplemented!() } diff --git a/mini-lsm-week-1/src/block/builder.rs b/mini-lsm-week-1/src/block/builder.rs index 1e73188..f69ec43 100644 --- a/mini-lsm-week-1/src/block/builder.rs +++ b/mini-lsm-week-1/src/block/builder.rs @@ -24,7 +24,7 @@ impl BlockBuilder { fn estimated_size(&self) -> usize { SIZEOF_U16 /* number of key-value pairs in the block */ + self.offsets.len() * SIZEOF_U16 /* offsets */ + self.data.len() - /* key-value pairs */ + // key-value pairs } /// Adds a key-value pair to the block. Returns false when the block is full. diff --git a/mini-lsm-week-1/src/table.rs b/mini-lsm-week-1/src/table.rs index 05a3be6..831c65b 100644 --- a/mini-lsm-week-1/src/table.rs +++ b/mini-lsm-week-1/src/table.rs @@ -33,7 +33,8 @@ impl BlockMeta { // The size of actual key estimated_size += meta.first_key.len(); } - // Reserve the space to improve performance, especially when the size of incoming data is large + // Reserve the space to improve performance, especially when the size of incoming data is + // large buf.reserve(estimated_size); let original_len = buf.len(); for meta in block_meta { diff --git a/mini-lsm/src/bin/compaction_simulator.rs b/mini-lsm/src/bin/compaction_simulator.rs index 48ee217..0eda300 100644 --- a/mini-lsm/src/bin/compaction_simulator.rs +++ b/mini-lsm/src/bin/compaction_simulator.rs @@ -160,7 +160,6 @@ fn main() { } let mut num_compactions = 0; while let Some(task) = controller.generate_compaction_task(&storage.snapshot) { - num_compactions += 1; println!("--- Compaction Task ---"); let mut sst_ids = Vec::new(); for file in task @@ -194,6 +193,10 @@ fn main() { } else { storage.dump_original_id(true); } + num_compactions += 1; + if num_compactions >= max_levels * 2 { + panic!("compaction does not converge?"); + } } if num_compactions == 0 { println!("no compaction triggered");