Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog.d/+run-retry.bugfix.md
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 8 additions & 2 deletions crates/rfc9162-log/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1100,8 +1100,14 @@ impl<S: TileStore> Store<S> {
) -> Result<RunManifest, Error> {
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()),
Expand Down
56 changes: 56 additions & 0 deletions crates/rfc9162-log/tests/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::sync::atomic::AtomicBool>,
}

impl TileStore for Flaky {
fn get(&self, path: &str) -> std::io::Result<Option<Vec<u8>>> {
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<bool> {
self.inner.create_new(path, bytes)
}
fn delete(&self, path: &str) -> std::io::Result<bool> {
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<Vec<String>> {
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));
}
Loading