feat: add configurable GFS retention per orbit

This commit is contained in:
2026-11-10 11:16:03 +07:00
parent 749c4cc828
commit 30a1a9b8f0
14 changed files with 665 additions and 86 deletions

View File

@@ -8,7 +8,9 @@ use std::fs::{self, File};
use std::io::{Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use tessera_core::decay::{evaluate_decay, DecayPolicy, SnapshotRef};
use tessera_core::decay::{
evaluate_decay, evaluate_gfs, DecayPolicy, RetentionPolicy, SnapshotRef,
};
use tessera_core::manifest::SnapshotManifest;
use uuid::Uuid;
@@ -129,7 +131,7 @@ impl Vault {
mut manifest: SnapshotManifest,
outpost_id: Uuid,
restic_id: &str,
) -> Result<Option<Uuid>> {
) -> Result<Option<(Uuid, bool)>> {
restic::validate_id(restic_id)?;
if !self.outpost_repository_configured(manifest.orbit_id)? {
return Ok(None);
@@ -143,7 +145,7 @@ impl Vault {
if dir.join(format!("{id}.json.enc")).try_exists()?
|| dir.join(format!("{id}.expired")).try_exists()?
{
return Ok(Some(id));
return Ok(Some((id, false)));
}
let repo = self.outpost_repository(manifest.orbit_id, config);
let summary = repo.snapshot_summary(restic_id)?;
@@ -161,7 +163,7 @@ impl Vault {
},
false,
)?;
Ok(Some(id))
Ok(Some((id, true)))
}
pub fn snapshot_files(
@@ -546,30 +548,41 @@ impl Vault {
pub fn prune_with_decay(&mut self, preset: &str) -> Result<Vec<Uuid>> {
let snaps = self.list_snapshots()?;
self.prune_snapshots(preset, snaps.into_iter())
self.prune_snapshots(preset, None, snaps.into_iter())
}
pub fn prune_orbit_with_decay(&mut self, orbit_id: Uuid, preset: &str) -> Result<Vec<Uuid>> {
pub fn prune_orbit_with_decay(
&mut self,
orbit_id: Uuid,
preset: &str,
retention: Option<&RetentionPolicy>,
) -> Result<Vec<Uuid>> {
let snaps = self.list_snapshots()?;
self.prune_snapshots(
preset,
snaps.into_iter().filter(|s| s.manifest.orbit_id == orbit_id),
retention,
snaps
.into_iter()
.filter(|s| s.manifest.orbit_id == orbit_id),
)
}
fn prune_snapshots(
&mut self,
preset: &str,
retention: Option<&RetentionPolicy>,
snapshots: impl Iterator<Item = StoredSnapshot>,
) -> Result<Vec<Uuid>> {
let policy = DecayPolicy::from_preset(preset);
let refs = snapshots
.map(|s| SnapshotRef {
id: s.id,
taken_at: s.taken_at,
})
.collect();
let to_forget = evaluate_decay(&policy, refs);
let to_forget = match retention {
Some(policy) => evaluate_gfs(policy, refs),
None => evaluate_decay(&DecayPolicy::from_preset(preset), refs),
};
self.forget(&to_forget)?;
Ok(to_forget)
}
@@ -736,7 +749,7 @@ mod tests {
.unwrap();
}
let pruned = vault
.prune_orbit_with_decay(orbit_id, "month-of-weeklies")
.prune_orbit_with_decay(orbit_id, "month-of-weeklies", None)
.unwrap();
assert_eq!(pruned.len(), 1);
assert!(vault