diff --git a/changelog.d/+run-retry.bugfix.md b/changelog.d/+run-retry.bugfix.md new file mode 100644 index 0000000..89279c3 --- /dev/null +++ b/changelog.d/+run-retry.bugfix.md @@ -0,0 +1,2 @@ +`rfc9162-log`: A seal or compaction retried after its manifest append failed, or after a crash between storing the run file and logging the manifest, logs the run. +It found its own run file and refused every retry with "a run for that entry range is already stored"; a run file with other bytes for the range is still refused. diff --git a/crates/rfc9162-log/src/store.rs b/crates/rfc9162-log/src/store.rs index 63a0283..b534fb8 100644 --- a/crates/rfc9162-log/src/store.rs +++ b/crates/rfc9162-log/src/store.rs @@ -1100,8 +1100,14 @@ impl Store { ) -> Result { let bytes = Run::encode_blocks(&entries, &pairs)?; let path = run_path(&entries); - if !self.manifest_store().create_new(&path, &bytes)? { - return Err(Error::Tree("a run for that entry range is already stored")); + let store = self.manifest_store(); + // A seal or compaction whose manifest append failed, or was cut short + // by a crash, left its run file behind: the same bytes are that run, + // which only the manifest makes part of the log. + if !store.create_new(&path, &bytes)? && store.get(&path)?.as_deref() != Some(&bytes[..]) { + return Err(Error::Tree( + "another run for that entry range is already stored", + )); } let (key_lo, key_hi) = match (pairs.first(), pairs.last()) { (Some(first), Some(last)) => (first.0.clone(), last.0.clone()), diff --git a/crates/rfc9162-log/tests/store.rs b/crates/rfc9162-log/tests/store.rs index 5251e39..ec8afc8 100644 --- a/crates/rfc9162-log/tests/store.rs +++ b/crates/rfc9162-log/tests/store.rs @@ -452,3 +452,59 @@ fn recovery_counts_the_paths_moved_aside() { "{moved:?}" ); } + +/// A memory store whose next put of an entry bundle fails once armed. +#[derive(Clone)] +struct Flaky { + inner: MemoryStore, + armed: Arc, +} + +impl TileStore for Flaky { + fn get(&self, path: &str) -> std::io::Result>> { + self.inner.get(path) + } + fn put(&self, path: &str, bytes: &[u8]) -> std::io::Result<()> { + if path.starts_with("tile/entries/") + && self.armed.swap(false, std::sync::atomic::Ordering::SeqCst) + { + return Err(std::io::Error::other("injected put failure")); + } + self.inner.put(path, bytes) + } + fn create_new(&self, path: &str, bytes: &[u8]) -> std::io::Result { + self.inner.create_new(path, bytes) + } + fn delete(&self, path: &str) -> std::io::Result { + self.inner.delete(path) + } + fn rename(&self, from: &str, to: &str) -> std::io::Result<()> { + self.inner.rename(from, to) + } + fn list(&self, prefix: &str) -> std::io::Result> { + self.inner.list(prefix) + } +} + +#[test] +fn a_seal_whose_manifest_was_not_logged_is_retried() { + let flaky = Flaky { + inner: MemoryStore::new(), + armed: Arc::default(), + }; + let mut store = Store::open(flaky.clone(), fields()).expect("open"); + for i in 0..5 { + store.append(&entry(i)).expect("append"); + } + // The run file is stored, the bundle carrying its manifest is not. + flaky.armed.store(true, std::sync::atomic::Ordering::SeqCst); + assert!(matches!(store.seal(), Err(Error::Store(_)))); + drop(store); + + let mut store = Store::open(flaky.clone(), fields()).expect("reopen"); + let run = store.seal().expect("seal again"); + assert_eq!(run.entries, 0..=4); + assert_eq!(run.manifest_index, Some(5)); + let hits = store.find(b"module=m3").expect("find"); + assert_eq!(hits[0].run.as_ref().map(|r| r.index), Some(5)); +}