Skip to content

Commit 953ea94

Browse files
utilities for archiving snapshots (#3224)
# Description of Changes Adds utilities for marking and deleting snapshot directories that have been archived # API and ABI breaking changes <!-- If this is an API or ABI breaking change, please apply the corresponding GitHub label. --> None # Expected complexity level and risk <!-- How complicated do you think these changes are? Grade on a scale from 1 to 5, where 1 is a trivial change, and 5 is a deep-reaching and complex change. This complexity rating applies not only to the complexity apparent in the diff, but also to its interactions with existing and future code. If you answered more than a 2, explain what is complex about the PR, and what other components it interacts with in potentially concerning ways. --> 1 # Testing <!-- Describe any testing you've done, and any testing you'd like your reviewers to do, so that you're confident that all the changes work as expected! --> Testing will be handled by the patch that adds archival
1 parent 8d14efa commit 953ea94

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

crates/paths/src/server.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ path_type! {
206206
SnapshotDirPath: dir
207207
}
208208

209+
path_type! {
210+
/// An archived snapshot directory. `{data-dir}/replica/$replica_id/snapshots/$tx_offset.archived_snapshot`
211+
ArchivedSnapshotDirPath: dir
212+
}
213+
209214
impl SnapshotDirPath {
210215
pub fn snapshot_file(&self, tx_offset: u64) -> SnapshotFilePath {
211216
let file_name = format!("{tx_offset:0>20}.snapshot_bsatn");
@@ -221,6 +226,12 @@ impl SnapshotDirPath {
221226
fs::rename(self, invalid_path)
222227
}
223228

229+
pub fn rename_as_archived(&self) -> io::Result<ArchivedSnapshotDirPath> {
230+
let path = self.0.with_extension("archived_snapshot");
231+
fs::rename(self, &path)?;
232+
Ok(ArchivedSnapshotDirPath(path))
233+
}
234+
224235
pub fn tx_offset(&self) -> Option<u64> {
225236
self.0
226237
.file_stem()

crates/snapshot/src/lib.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use spacetimedb_fs_utils::{
3030
lockfile::{Lockfile, LockfileError},
3131
};
3232
use spacetimedb_lib::Identity;
33-
use spacetimedb_paths::server::{SnapshotDirPath, SnapshotFilePath, SnapshotsPath};
33+
use spacetimedb_paths::server::{ArchivedSnapshotDirPath, SnapshotDirPath, SnapshotFilePath, SnapshotsPath};
3434
use spacetimedb_paths::FromPathUnchecked;
3535
use spacetimedb_primitives::TableId;
3636
use spacetimedb_sats::{bsatn, de::Deserialize, ser::Serialize};
@@ -40,6 +40,7 @@ use spacetimedb_table::{
4040
page_pool::PagePool,
4141
table::Table,
4242
};
43+
use std::fs;
4344
use std::{
4445
collections::BTreeMap,
4546
collections::HashMap,
@@ -145,6 +146,9 @@ pub const SNAPSHOT_FILE_EXT: &str = "snapshot_bsatn";
145146
/// File extension of snapshots which have been marked invalid by [`SnapshotRepository::invalidate_newer_snapshots`].
146147
pub const INVALID_SNAPSHOT_DIR_EXT: &str = "invalid_snapshot";
147148

149+
/// File extension of snapshots which have been archived
150+
pub const ARCHIVED_SNAPSHOT_EXT: &str = "archived_snapshot";
151+
148152
#[derive(Clone, Serialize, Deserialize)]
149153
/// The hash and refcount of a single blob in the blob store.
150154
struct BlobEntry {
@@ -890,6 +894,27 @@ impl SnapshotRepository {
890894
}))
891895
}
892896

897+
/// Return an interator of [`ArchivedSnapshotDirPath`] for all the archived snapshot directories on disk
898+
pub fn all_archived_snapshots(&self) -> Result<impl Iterator<Item = ArchivedSnapshotDirPath>, SnapshotError> {
899+
Ok(self
900+
.root
901+
// Item = Result<DirEntry>
902+
.read_dir()?
903+
// Item = DirEntry
904+
.filter_map(Result::ok)
905+
// Item = PathBuf
906+
.map(|dirent| dirent.path())
907+
// Ignore entries not shaped like snapshot directories.
908+
.filter(|path| path.extension() == Some(OsStr::new(ARCHIVED_SNAPSHOT_EXT)))
909+
// Item = ArchivedSnapshotDirPath
910+
.map(ArchivedSnapshotDirPath::from_path_unchecked))
911+
}
912+
913+
/// Delete an archived snapshot from disk
914+
pub fn remove_archived_snapshot(path: &ArchivedSnapshotDirPath) -> Result<(), SnapshotError> {
915+
fs::remove_dir_all(path).map_err(SnapshotError::Io)
916+
}
917+
893918
/// Return the `TxOffset` of the highest-offset complete snapshot in the repository.
894919
///
895920
/// Does not verify that the snapshot of the returned `TxOffset` is valid and uncorrupted,

0 commit comments

Comments
 (0)