Skip to content

Commit

Permalink
Catch situation where current snapshot is being deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
issackelly authored and alexjg committed Dec 8, 2023
1 parent 19d9388 commit 7ecbdf0
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/fs_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,33 @@ impl FsStore {
// Write the snapshot
let output_chunk_name = SavedChunkName::new_snapshot(doc.get_heads());
let chunk = doc.save();
write_chunk(&self.root, &paths, &chunk, output_chunk_name, &self.tmpdir)?;
write_chunk(
&self.root,
&paths,
&chunk,
output_chunk_name.clone(),
&self.tmpdir,
)?;

// Remove all the old data
for incremental in chunks.incrementals.keys() {
let path = paths.chunk_path(&self.root, incremental);
std::fs::remove_file(&path)
.map_err(|e| Error(ErrorKind::DeleteChunk(path, e)))?;
}
let just_wrote = paths.chunk_path(&self.root, &output_chunk_name);
for snapshot in chunks.snapshots.keys() {
let path = paths.chunk_path(&self.root, snapshot);

if path == just_wrote {
tracing::error!(
?path,
"Somehow trying to delete the same path we just wrote to. Not today \
Satan"
);
continue;
}

std::fs::remove_file(&path)
.map_err(|e| Error(ErrorKind::DeleteChunk(path, e)))?;
}
Expand Down Expand Up @@ -261,6 +278,7 @@ fn write_chunk(
// Move the temporary file into a snapshot in the document data directory
// with a name based on the hash of the heads of the document
let output_path = paths.chunk_path(root, &name);
tracing::trace!(?temp_save_path, ?output_path, "renaming chunk file");
std::fs::rename(&temp_save_path, &output_path)
.map_err(|e| Error(ErrorKind::RenameTempFile(temp_save_path, output_path, e)))?;

Expand Down Expand Up @@ -327,13 +345,13 @@ impl DocIdPaths {
}
}

#[derive(Debug, Hash, PartialEq, Eq)]
#[derive(Debug, Hash, PartialEq, Eq, Clone)]
enum ChunkType {
Snapshot,
Incremental,
}

#[derive(Debug, Hash, PartialEq, Eq)]
#[derive(Debug, Hash, PartialEq, Eq, Clone)]
struct SavedChunkName {
hash: Vec<u8>,
chunk_type: ChunkType,
Expand Down Expand Up @@ -449,7 +467,7 @@ impl Chunks {
// Could be a concurrent process compacting, not an error
tracing::warn!(
missing_chunk_path=%path.display(),
"chunk file disappeared while reading chunks",
"chunk file disappeared while reading chunks; ignoring",
);
continue;
}
Expand Down

0 comments on commit 7ecbdf0

Please sign in to comment.