Skip to content

Commit

Permalink
feat(rolldb): allow optionally overlap of WAL over immutable chain (#419
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jmhrpr authored Mar 8, 2024
1 parent 00086aa commit 87ff44b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
10 changes: 8 additions & 2 deletions pallas-rolldb/src/wal/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,15 @@ pub struct Store {
pub tip_change: Arc<tokio::sync::Notify>,
wal_seq: u64,
k_param: u64,
immutable_overlap: u64,
}

impl Store {
pub fn open(path: impl AsRef<Path>, k_param: u64) -> Result<Self, Error> {
pub fn open(
path: impl AsRef<Path>,
k_param: u64,
immutable_overlap: Option<u64>,
) -> Result<Self, Error> {
let mut opts = Options::default();
opts.create_if_missing(true);
opts.create_missing_column_families(true);
Expand All @@ -184,6 +189,7 @@ impl Store {
tip_change: Arc::new(tokio::sync::Notify::new()),
wal_seq,
k_param,
immutable_overlap: immutable_overlap.unwrap_or(0),
};

Ok(out)
Expand Down Expand Up @@ -378,7 +384,7 @@ impl Store {
// get the number of slots that have passed since the wal point
let slot_delta = tip - value.slot().unwrap_or(0);

if slot_delta <= self.k_param {
if slot_delta <= self.k_param + self.immutable_overlap {
break;
} else {
WalKV::stage_delete(&self.db, wal_key, &mut batch);
Expand Down
2 changes: 1 addition & 1 deletion pallas-rolldb/src/wal/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ mod tests {
#[tokio::test]
async fn test_stream_waiting() {
let path = tempfile::tempdir().unwrap().into_path();
let mut db = Store::open(path.clone(), 30).unwrap();
let mut db = Store::open(path.clone(), 30, None).unwrap();

for i in 0..=100 {
let (slot, hash, body) = dummy_block(i * 10);
Expand Down
36 changes: 35 additions & 1 deletion pallas-rolldb/src/wal/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@ use super::{BlockBody, BlockHash, BlockSlot, Store};

fn with_tmp_db<T>(k_param: u64, op: fn(store: Store) -> T) {
let path = tempfile::tempdir().unwrap().into_path();
let store = Store::open(path.clone(), k_param).unwrap();
let store = Store::open(path.clone(), k_param, None).unwrap();

op(store);

Store::destroy(path).unwrap();
}

fn with_tmp_db_overlap<T>(k_param: u64, overlap: u64, op: fn(store: Store) -> T) {
let path = tempfile::tempdir().unwrap().into_path();
let store = Store::open(path.clone(), k_param, Some(overlap)).unwrap();

op(store);

Expand Down Expand Up @@ -109,6 +118,8 @@ fn test_prune_linear() {
db.roll_forward(slot, hash, body).unwrap();
}

// db contains slots: 0, 10, ..., 980, 990
// this should prune slots less than (990 - 30) = 960
db.prune_wal().unwrap();

let mut wal = db.crawl_after(None);
Expand All @@ -122,6 +133,29 @@ fn test_prune_linear() {
});
}

#[test]
fn test_prune_linear_with_overlap() {
with_tmp_db_overlap(30, 20, |mut db| {
for i in 0..100 {
let (slot, hash, body) = dummy_block(i * 10);
db.roll_forward(slot, hash, body).unwrap();
}

// db contains slots: 0, 10, ..., 980, 990
// this should prune slots less than (990 - 30 - 20) = 940
db.prune_wal().unwrap();

let mut wal = db.crawl_after(None);

for i in 94..100 {
let (_, val) = wal.next().unwrap().unwrap();
assert_eq!(val.slot().unwrap(), i * 10);
}

assert!(wal.next().is_none());
});
}

#[test]
fn test_prune_with_rollback() {
with_tmp_db(30, |mut db| {
Expand Down

0 comments on commit 87ff44b

Please sign in to comment.