fix: restore safety copies no longer compete with production captures in retention
A safety copy of a restore target was stored in the application's orbit and weighed by the same retention as captures, so the newest copy (the target's) could push the production capture that had just been restored out of the keep set, whatever the rule (GFS, count or age). Safety copies are now separated: they have their own per-target rule (newest three plus anything under seven days) and never count for or against captures. Proven red on the old behaviour with a real refresh. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -111,6 +111,43 @@ pub fn evaluate_capture_retention(
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// A restore safety copy, for its own retention.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SafetyRef {
|
||||
pub id: Uuid,
|
||||
pub taken_at: DateTime<Utc>,
|
||||
pub target: String,
|
||||
}
|
||||
|
||||
/// Safety copies kept per restore target regardless of age.
|
||||
pub const SAFETY_KEEP_PER_TARGET: usize = 3;
|
||||
/// Safety copies younger than this are always kept, however many there are.
|
||||
pub const SAFETY_KEEP_DAYS: i64 = 7;
|
||||
|
||||
/// Which safety copies to forget. Each target keeps its newest few and everything from the last week, so the
|
||||
/// newest copy of a target is never forgotten here, and a burst of restores cannot erase the way back from
|
||||
/// the previous one. (A copy that a restore still depends on is protected separately, by the caller.)
|
||||
pub fn evaluate_safety_retention(snapshots: Vec<SafetyRef>, now: DateTime<Utc>) -> Vec<Uuid> {
|
||||
let mut targets: std::collections::BTreeMap<String, Vec<SafetyRef>> = Default::default();
|
||||
for snapshot in snapshots {
|
||||
targets
|
||||
.entry(snapshot.target.clone())
|
||||
.or_default()
|
||||
.push(snapshot);
|
||||
}
|
||||
let mut forget = Vec::new();
|
||||
for (_, mut copies) in targets {
|
||||
copies.sort_unstable_by_key(|s| std::cmp::Reverse((s.taken_at, s.id)));
|
||||
for (index, copy) in copies.into_iter().enumerate() {
|
||||
let recent = now.signed_duration_since(copy.taken_at).num_days() < SAFETY_KEEP_DAYS;
|
||||
if index >= SAFETY_KEEP_PER_TARGET && !recent {
|
||||
forget.push(copy.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
forget
|
||||
}
|
||||
|
||||
/// Keep the newest snapshot in each of the most recent occupied UTC periods.
|
||||
pub fn evaluate_gfs(policy: &RetentionPolicy, mut snapshots: Vec<SnapshotRef>) -> Vec<Uuid> {
|
||||
if !policy.has_tiers() {
|
||||
@@ -333,4 +370,75 @@ mod tests {
|
||||
let forget = evaluate_decay(&policy, snaps);
|
||||
assert_eq!(forget.len(), 3);
|
||||
}
|
||||
|
||||
fn safety(day: u32, target: &str) -> SafetyRef {
|
||||
SafetyRef {
|
||||
id: Uuid::new_v4(),
|
||||
taken_at: Utc.with_ymd_and_hms(2026, 11, day, 4, 0, 0).unwrap(),
|
||||
target: target.into(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn safety_snapshots_keep_the_newest_few_per_target_and_anything_recent() {
|
||||
let now = Utc.with_ymd_and_hms(2026, 11, 30, 12, 0, 0).unwrap();
|
||||
let old: Vec<_> = (1..=6).map(|d| safety(d, "staging")).collect(); // all older than a week
|
||||
let recent = safety(28, "staging");
|
||||
let mut all = old.clone();
|
||||
all.push(recent.clone());
|
||||
let forgotten = evaluate_safety_retention(all, now);
|
||||
// newest three overall (recent, day 6, day 5) stay; days 1..=4 go
|
||||
assert_eq!(forgotten.len(), 4);
|
||||
assert!(!forgotten.contains(&recent.id));
|
||||
assert!(!forgotten.contains(&old[5].id) && !forgotten.contains(&old[4].id));
|
||||
assert!(old[..4].iter().all(|s| forgotten.contains(&s.id)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_recent_safety_snapshot_is_never_forgotten_even_beyond_the_count() {
|
||||
let now = Utc.with_ymd_and_hms(2026, 11, 30, 12, 0, 0).unwrap();
|
||||
let burst: Vec<_> = (24..=29).map(|d| safety(d, "staging")).collect(); // six in the last week
|
||||
assert!(evaluate_safety_retention(burst, now).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn targets_are_counted_separately_and_each_keeps_at_least_its_newest() {
|
||||
let now = Utc.with_ymd_and_hms(2027, 6, 1, 0, 0, 0).unwrap(); // everything is very old
|
||||
let all = vec![safety(1, "staging"), safety(2, "staging"), safety(1, "dev")];
|
||||
let forgotten = evaluate_safety_retention(all.clone(), now);
|
||||
assert!(
|
||||
forgotten.is_empty(),
|
||||
"three or fewer per target are always kept"
|
||||
);
|
||||
let many: Vec<_> = (1..=5)
|
||||
.map(|d| safety(d, "staging"))
|
||||
.chain([safety(1, "dev")])
|
||||
.collect();
|
||||
let forgotten = evaluate_safety_retention(many.clone(), now);
|
||||
assert_eq!(forgotten.len(), 2);
|
||||
assert!(
|
||||
!forgotten.contains(&many[5].id),
|
||||
"the only snapshot of another target stays"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_manifest_knows_whether_it_is_a_restore_safety_copy() {
|
||||
let mut manifest = crate::manifest::SnapshotManifest {
|
||||
snapshot_id: Uuid::nil(),
|
||||
orbit_id: Uuid::nil(),
|
||||
orbit_name: "x".into(),
|
||||
labels: vec!["postgres".into()],
|
||||
provider: "postgres".into(),
|
||||
paths: vec![],
|
||||
restore_hints: serde_json::json!({"job_id": "j"}),
|
||||
};
|
||||
assert!(!manifest.is_restore_safety());
|
||||
manifest.labels = vec!["restore-safety".into()];
|
||||
assert!(manifest.is_restore_safety());
|
||||
manifest.labels = vec![];
|
||||
manifest.restore_hints = serde_json::json!({"safety_for": "job", "target_id": "t"});
|
||||
assert!(manifest.is_restore_safety());
|
||||
assert_eq!(manifest.safety_target().as_deref(), Some("t"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,23 @@ pub struct SnapshotManifest {
|
||||
pub restore_hints: serde_json::Value,
|
||||
}
|
||||
|
||||
impl SnapshotManifest {
|
||||
/// A copy of a restore target's database taken before it was overwritten. It belongs to the target, not
|
||||
/// to the application's backup history: retention must never weigh it against real captures.
|
||||
pub fn is_restore_safety(&self) -> bool {
|
||||
self.labels.iter().any(|label| label == "restore-safety")
|
||||
|| self.restore_hints.get("safety_for").is_some()
|
||||
}
|
||||
|
||||
/// The restore target a safety copy was taken from.
|
||||
pub fn safety_target(&self) -> Option<String> {
|
||||
self.restore_hints
|
||||
.get("target_id")
|
||||
.and_then(|id| id.as_str())
|
||||
.map(str::to_string)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn manifest_hash(manifest: &SnapshotManifest) -> String {
|
||||
let json = serde_json::to_vec(manifest).expect("manifest serializes");
|
||||
let digest = Sha256::digest(&json);
|
||||
|
||||
Reference in New Issue
Block a user