Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
AbnerZheng committed May 29, 2024
1 parent 9446d9f commit c94924b
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 20 deletions.
6 changes: 3 additions & 3 deletions mini-lsm-starter/src/bin/compaction-simulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ use mini_lsm_wrapper::key::KeyBytes;
use mini_lsm_wrapper::lsm_storage::LsmStorageState;
use mini_lsm_wrapper::mem_table::MemTable;
use mini_lsm_wrapper::table::SsTable;
use rand::SeedableRng;
use rand_chacha::{ChaCha12Rng, ChaCha8Rng};
use rand::Rng;
use rand_chacha::rand_core::SeedableRng;
use rand_chacha::ChaCha12Rng;

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
Expand Down Expand Up @@ -188,7 +189,6 @@ impl MockStorage {
}

fn generate_random_key_range() -> (KeyBytes, KeyBytes) {
use rand::Rng;
let mut rng = ChaCha12Rng::seed_from_u64(10086);
let begin: usize = rng.gen_range(0..(1 << 31));
let end: usize = begin + rng.gen_range((1 << 10)..(1 << 31));
Expand Down
4 changes: 2 additions & 2 deletions mini-lsm-starter/src/compact/leveled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,11 @@ impl LeveledCompactionController {
}
res
};
// find the right place to insert compacted SsTable
let first_key = snapshot.sstables[&output[0]].first_key();
if lower_level_kept.is_empty() {
snapshot.levels[*lower_level] = (*level_id, output.to_vec());
} else {
// find the right place to insert compacted SsTable
let first_key = snapshot.sstables[&output[0]].first_key();
let insert_idx = lower_level_kept
.iter()
.position(|sst_id| snapshot.sstables[sst_id].last_key() > first_key);
Expand Down
8 changes: 7 additions & 1 deletion mini-lsm-starter/src/lsm_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,13 @@ impl LsmStorageInner {
for record in records {
match record {
ManifestRecord::Flush(sst_id) => {
state.l0_sstables.insert(0, sst_id);
if compaction_controller.flush_to_l0() {
println!("flush memtable{sst_id} to l0");
state.l0_sstables.insert(0, sst_id);
} else {
println!("flush memtable{sst_id} to new tier");
state.levels.insert(0, (sst_id, vec![sst_id]));
}
let option = memtable_to_recover.pop();
assert_eq!(option, Some(sst_id), "failed to flush memtable");
sst_need_load.insert(sst_id);
Expand Down
1 change: 1 addition & 0 deletions mini-lsm-starter/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ mod week2_day3;
mod week2_day4;
mod week2_day5;
mod week2_day6;
mod week3_day1;
28 changes: 14 additions & 14 deletions mini-lsm-starter/src/tests/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,20 @@ where
// as_bytes(iter.key().for_testing_key_ref())
// );
assert!(iter.is_valid());
// assert_eq!(
// k,
// iter.key().for_testing_key_ref(),
// "expected key: {:?}, actual key: {:?}",
// k,
// as_bytes(iter.key().for_testing_key_ref()),
// );
// assert_eq!(
// v,
// iter.value(),
// "expected value: {:?}, actual value: {:?}",
// v,
// as_bytes(iter.value()),
// );
assert_eq!(
k,
iter.key().for_testing_key_ref(),
"expected key: {:?}, actual key: {:?}",
k,
as_bytes(iter.key().for_testing_key_ref()),
);
assert_eq!(
v,
iter.value(),
"expected value: {:?}, actual value: {:?}",
v,
as_bytes(iter.value()),
);
iter.next().unwrap();
}
assert!(!iter.is_valid());
Expand Down
3 changes: 3 additions & 0 deletions mini-lsm-starter/src/tests/week2_day5.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::thread::sleep;
use std::time::Duration;
use tempfile::tempdir;

use crate::{
Expand Down Expand Up @@ -62,6 +64,7 @@ fn test_integration(compaction_options: CompactionOptions) {
.force_freeze_memtable(&storage.inner.state_lock.lock())
.unwrap();
}
sleep(Duration::from_secs(1));
storage.close().unwrap();
// ensure all SSTs are flushed
assert!(storage.inner.state.read().memtable.is_empty());
Expand Down
56 changes: 56 additions & 0 deletions mini-lsm-starter/src/tests/week3_day1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::sync::Arc;

use bytes::Bytes;
use tempfile::tempdir;

use crate::key::KeySlice;
use crate::table::{FileObject, SsTable, SsTableBuilder, SsTableIterator};

use super::harness::{check_iter_result_by_key_and_ts, generate_sst_with_ts};

#[test]
#[ignore]
fn test_sst_build_multi_version_simple() {
let mut builder = SsTableBuilder::new(16);
builder.add(
KeySlice::for_testing_from_slice_with_ts(b"233", 233),
b"233333",
);
builder.add(
KeySlice::for_testing_from_slice_with_ts(b"233", 0),
b"2333333",
);
let dir = tempdir().unwrap();
builder.build_for_test(dir.path().join("1.sst")).unwrap();
}

fn generate_test_data() -> Vec<((Bytes, u64), Bytes)> {
(0..100)
.map(|id| {
(
(Bytes::from(format!("key{:05}", id / 5)), 5 - (id % 5)),
Bytes::from(format!("value{:05}", id)),
)
})
.collect()
}

#[test]
#[ignore]
fn test_sst_build_multi_version_hard() {
let dir = tempdir().unwrap();
let data = generate_test_data();
generate_sst_with_ts(1, dir.path().join("1.sst"), data.clone(), None);
let sst = Arc::new(
SsTable::open(
1,
None,
FileObject::open(&dir.path().join("1.sst")).unwrap(),
)
.unwrap(),
);
check_iter_result_by_key_and_ts(
&mut SsTableIterator::create_and_seek_to_first(sst).unwrap(),
data,
);
}

0 comments on commit c94924b

Please sign in to comment.