From 43b16baaa6bf5959cac5ece8d406c580c97b4ce6 Mon Sep 17 00:00:00 2001 From: Nikolai Golub Date: Fri, 6 Nov 2026 17:38:30 +0100 Subject: [PATCH] rollback: record only the deltas still live in the manifest `writeout_start` returns the live range the manifest is written with, before `writeout_end` prunes the log. It returned a range that still covered pruned deltas, and a reopen loaded them back: - After pruning the oldest delta it returned the pre-prune start, so a reopened log held one delta more than `max_rollback_log_len`. Pruning only dropped one delta per sync, so the log never shrank back. - After a truncate it returned `min(seglog start, pending_truncate)`. When the truncate removed every delta, `pending_truncate` points at the last pruned delta, so the range revived it. A rollback after reopen then succeeded and rewound one commit past the rollback window. With `max_rollback_log_len(1)`: commit 1, 2, 3, `rollback(1)` succeeds and a second fails; after a reopen `rollback(1)` succeeded and restored the state after commit 1. Derive the range from the deltas still in memory, which is what stays live, and use (0, 0) when none do. Prune until the log fits its length, so a log opened with a smaller length shrinks on the next commit. Writing the pre-prune start was also unsafe on its own: `writeout_end` deletes the segments before the new start, which the manifest still pointed into until the next sync. Found by the concurrent_rollback fuzz target. Co-Authored-By: Claude Opus 5.5 --- nomt/src/rollback/mod.rs | 41 +++++++++++----- nomt/src/rollback/tests.rs | 46 ++++++++++++++++-- nomt/tests/rollback.rs | 97 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 169 insertions(+), 15 deletions(-) diff --git a/nomt/src/rollback/mod.rs b/nomt/src/rollback/mod.rs index 83f47c9df6..b61946e99e 100644 --- a/nomt/src/rollback/mod.rs +++ b/nomt/src/rollback/mod.rs @@ -84,6 +84,10 @@ impl InMemory { self.log.pop_front() } + fn oldest_record_id(&self) -> Option { + self.log.front().map(|(record_id, _)| *record_id) + } + // Returns the total number of deltas, including the staged one. fn total_len(&self) -> usize { self.log.len() @@ -227,29 +231,42 @@ impl Rollback { let pending_truncate = in_memory.pending_truncate.take(); + // The manifest is written with the range returned here before `writeout_end` prunes the + // log, so the range must be what stays live after pruning: the deltas still in memory. + // Recording more would let a reopen load deltas that were pruned or truncated away. + // NOTE: for now, if there is a pending truncate, we ignore everything else. if let Some(pending_truncate) = pending_truncate { - let rollback_start_live = std::cmp::min(seglog.live_range().0 .0, pending_truncate); + // A truncate that removed every delta empties the log. `pending_truncate` then points + // before the oldest live delta, at one that was pruned earlier. + let (rollback_start_live, rollback_end_live) = match in_memory.oldest_record_id() { + Some(oldest) => (oldest.0, pending_truncate), + None => (0, 0), + }; return WriteoutData { rollback_start_live, - rollback_end_live: pending_truncate, + rollback_end_live, prune_to_new_start_live: None, - prune_to_new_end_live: Some(pending_truncate), + prune_to_new_end_live: Some(rollback_end_live), }; } - let prune_to_new_start_live = if in_memory.total_len() > self.shared.max_rollback_log_len { - Some(in_memory.pop_oldest().unwrap().0.next().0) - } else { - None - }; + let mut pruned = false; + while in_memory.total_len() > self.shared.max_rollback_log_len { + in_memory.pop_oldest(); + pruned = true; + } - let (rollback_start_live, rollback_end_live) = seglog.live_range(); + let (_, end_live) = seglog.live_range(); + let (rollback_start_live, rollback_end_live) = match in_memory.oldest_record_id() { + Some(oldest) => (oldest.0, end_live.0), + None => (0, 0), + }; WriteoutData { - rollback_start_live: rollback_start_live.0, - rollback_end_live: rollback_end_live.0, - prune_to_new_start_live, + rollback_start_live, + rollback_end_live, + prune_to_new_start_live: pruned.then_some(rollback_start_live), prune_to_new_end_live: None, } } diff --git a/nomt/src/rollback/tests.rs b/nomt/src/rollback/tests.rs index 4a7cde9c17..274bfd9fa6 100644 --- a/nomt/src/rollback/tests.rs +++ b/nomt/src/rollback/tests.rs @@ -316,9 +316,9 @@ fn rollback_writeout_start_and_end() { rollback.commit(delta).unwrap(); } - // expected prune of oldest delta + // expected prune of oldest delta. The manifest gets the range left after pruning. let wa = rollback.writeout_start(); - assert_eq!(wa.rollback_start_live, 1); + assert_eq!(wa.rollback_start_live, 2); assert_eq!(wa.rollback_end_live, 101); assert_eq!(wa.prune_to_new_start_live, Some(2)); assert_eq!(wa.prune_to_new_end_live, None); @@ -336,7 +336,7 @@ fn rollback_writeout_start_and_end() { rollback.commit(delta).unwrap(); let wa = rollback.writeout_start(); - assert_eq!(wa.rollback_start_live, 2); + assert_eq!(wa.rollback_start_live, 3); assert_eq!(wa.rollback_end_live, 102); assert_eq!(wa.prune_to_new_start_live, Some(3)); assert_eq!(wa.prune_to_new_end_live, None); @@ -364,3 +364,43 @@ fn rollback_writeout_start_and_end() { assert_eq!(rollback_start_live, 3.into()); assert_eq!(rollback_end_live, 97.into()); } + +#[test] +fn rollback_writeout_truncate_to_empty_after_prune() { + let temp_dir = tempfile::tempdir().unwrap(); + let db_dir_path = temp_dir.path().join("db"); + std::fs::create_dir_all(&db_dir_path).unwrap(); + let db_dir_fd = OpenOptions::new() + .read(true) + .open(db_dir_path.clone()) + .unwrap(); + let store = MockStore::new(); + + let rollback = Rollback::read(1, db_dir_path, Arc::new(db_dir_fd), 0, 0).unwrap(); + for expected_start in [1, 2, 3] { + let builder = rollback.delta_builder_inner(store.async_reader()); + rollback + .commit(builder.finalize(vec![], |_, _| {})) + .unwrap(); + let wa = rollback.writeout_start(); + assert_eq!( + (wa.rollback_start_live, wa.rollback_end_live), + (expected_start, expected_start) + ); + rollback + .writeout_end(wa.prune_to_new_start_live, wa.prune_to_new_end_live) + .unwrap(); + } + + // Truncating the only live delta empties the log rather than reviving delta 2, which was + // pruned. + rollback.truncate(1).unwrap().unwrap(); + let wa = rollback.writeout_start(); + assert_eq!((wa.rollback_start_live, wa.rollback_end_live), (0, 0)); + assert_eq!(wa.prune_to_new_end_live, Some(0)); + rollback + .writeout_end(wa.prune_to_new_start_live, wa.prune_to_new_end_live) + .unwrap(); + assert_eq!(rollback.seglog().live_range(), (0.into(), 0.into())); + assert!(rollback.truncate(1).unwrap().is_none()); +} diff --git a/nomt/tests/rollback.rs b/nomt/tests/rollback.rs index a3216ca530..3c53fc2955 100644 --- a/nomt/tests/rollback.rs +++ b/nomt/tests/rollback.rs @@ -494,3 +494,100 @@ fn test_rollback_read_then_write() { nomt.rollback(1).unwrap(); assert_eq!(nomt.read(key).unwrap(), None); } + +/// A NOMT with rollback enabled and the given rollback log length, in `path`. +fn open_with_log_len(path: &std::path::Path, max_rollback_log_len: u32) -> Nomt { + let mut o = Options::new(); + o.path(path); + o.bitbox_seed([0; 16]); + o.hashtable_buckets(4096); + o.io_workers(1); + o.rollback(true); + o.max_rollback_log_len(max_rollback_log_len); + Nomt::open(o).unwrap() +} + +/// Commit `i` writes key `i`, so the state after commit `n` is keys `1..=n`. +fn commit_key(nomt: &Nomt, i: u8) { + let session = nomt.begin_session(SessionParams::default()); + let key = [i; 32]; + session.warm_up(key); + session + .finish(vec![(key, KeyReadWrite::Write(Some(vec![i])))]) + .unwrap() + .commit(nomt) + .unwrap(); +} + +fn assert_state_after_commit(nomt: &Nomt, n: u8, max: u8) { + for i in 1..=max { + let expected = (i <= n).then(|| vec![i]); + assert_eq!( + nomt.read([i; 32]).unwrap(), + expected, + "key {i}, state after commit {n}" + ); + } +} + +#[test] +fn test_rollback_log_len_holds_across_reopen() { + for log_len in 1..=3u8 { + let commits = log_len + 2; + + // Rolling back the whole log, then reopening, must not bring back a delta that was pruned. + let dir = tempfile::tempdir().unwrap(); + let nomt = open_with_log_len(dir.path(), log_len as u32); + for i in 1..=commits { + commit_key(&nomt, i); + } + nomt.rollback(log_len as usize).unwrap(); + assert!(nomt.rollback(1).is_err()); + drop(nomt); + let nomt = open_with_log_len(dir.path(), log_len as u32); + assert!( + nomt.rollback(1).is_err(), + "log_len {log_len}: rolled back past the log after reopen" + ); + assert_state_after_commit(&nomt, commits - log_len, commits); + drop(nomt); + + // A reopened log holds exactly the last `log_len` deltas, not also the last pruned one. + let dir = tempfile::tempdir().unwrap(); + let nomt = open_with_log_len(dir.path(), log_len as u32); + for i in 1..=commits { + commit_key(&nomt, i); + } + drop(nomt); + let nomt = open_with_log_len(dir.path(), log_len as u32); + assert!( + nomt.rollback(log_len as usize + 1).is_err(), + "log_len {log_len}: reopened log holds a pruned delta" + ); + nomt.rollback(log_len as usize).unwrap(); + assert_state_after_commit(&nomt, commits - log_len, commits); + assert!(nomt.rollback(1).is_err()); + } +} + +#[test] +fn test_rollback_log_shrinks_to_smaller_len_on_reopen() { + let dir = tempfile::tempdir().unwrap(); + let nomt = open_with_log_len(dir.path(), 4); + for i in 1..=4 { + commit_key(&nomt, i); + } + drop(nomt); + + // The next commit prunes the log down to the new length, not just by one delta. + let nomt = open_with_log_len(dir.path(), 1); + commit_key(&nomt, 5); + assert!(nomt.rollback(2).is_err(), "log holds more than its length"); + nomt.rollback(1).unwrap(); + assert_state_after_commit(&nomt, 4, 5); + drop(nomt); + + let nomt = open_with_log_len(dir.path(), 1); + assert!(nomt.rollback(1).is_err()); + assert_state_after_commit(&nomt, 4, 5); +}