OXYGENHUB
# 21b2aec279ff oxygen / crates / oxy-core / src / repo / gc.rs
5.7 KiB · Rust · last changed gc: tolerate historical views naming objects pruned under an earlier, shorter op-retention 1 month ago source blame history raw
1
//! Garbage collection (spec §21): pack loose objects, then delete objects
2
//! nothing reachable refers to.
3
//!
4
//! Roots: the current view, the views of every operation newer than
5
//! `core.op-retention`, reference attestations, and the policy. Operation
6
//! objects are always kept so the log stays walkable, but an operation older
7
//! than the retention window no longer keeps its states alive, so undoing
8
//! that far back may fail once its objects are gone.
9
10
use super::*;
11
use crate::store::{PackReport, PruneReport};
12
use std::collections::HashSet;
13
14
#[derive(Debug, Clone, Default, PartialEq, Eq)]
15
pub struct GcReport {
16
    pub packed: PackReport,
17
    pub pruned: PruneReport,
18
    pub reachable: usize,
19
    /// Packs folded into one because there were more than `MAX_PACKS`.
20
    pub merged_packs: usize,
21
}
22
23
impl Repo {
24
    /// Pack loose objects and prune unreachable ones older than the grace
25
    /// period (`now` ignores the grace period).
26
    pub fn gc(&self, now: bool) -> Result<GcReport> {
27
        let packed = self.store.pack_loose()?;
28
        let keep = self.reachable()?;
29
        let grace = if now { std::time::Duration::ZERO } else { std::time::Duration::from_secs(self.config.gc_grace_days * 86_400) };
30
        let pruned = self.store.prune(&keep, grace)?;
31
        let merged_packs = self.store.merge_packs()?;
32
        Ok(GcReport { packed, pruned, reachable: keep.len(), merged_packs })
33
    }
34
35
    /// Every object that must survive garbage collection.
36
    pub fn reachable(&self) -> Result<HashSet<ObjectRef>> {
37
        let mut keep: HashSet<ObjectRef> = HashSet::new();
38
        let mut states: Vec<ObjectRef> = Vec::new();
39
        let mut changes: Vec<ChangeHash> = Vec::new();
40
        let mut attests: Vec<ObjectRef> = Vec::new();
41
        let mut policies: Vec<ObjectRef> = Vec::new();
42
43
        // Operations: always kept; views within retention are roots.
44
        let cutoff = Hlc::now().wall_ms.saturating_sub(self.config.op_retention_days * 86_400_000);
45
        for (i, (r, op)) in self.op_log()?.into_iter().enumerate() {
46
            keep.insert(r);
47
            if i == 0 || op.hlc.wall_ms >= cutoff {
48
                states.extend(op.view.bookmarks.values().copied());
49
                states.extend(op.view.remotes.values().flat_map(|b| b.values().copied()));
50
                changes.extend(op.view.workspaces.values().copied());
51
                policies.extend(op.view.policy);
52
            }
53
        }
54
        // Checkpoints: the object, its payload blob, and the blob's chunks.
55
        for r in self.checkpoint_refs()? {
56
            if let Ok(bytes) = self.store.get_typed(&r, ObjectType::Checkpoint) {
57
                keep.insert(r);
58
                let cp = crate::objects::Checkpoint::decode(&bytes)?;
59
                keep.insert(cp.payload);
60
                if let Ok(b) = self.store.get_typed(&cp.payload, ObjectType::Blob) {
61
                    keep.extend(Blob::decode(&b)?.chunks);
62
                }
63
            }
64
        }
65
        // Reference attestations.
66
        for name in self.view()?.bookmarks.keys() {
67
            if let Some((r, _)) = self.latest_ref_attest(name)? {
68
                attests.push(r);
69
            }
70
        }
71
        // A historical view may name objects pruned under an earlier, shorter
72
        // retention; there is nothing to keep for those.
73
        for st in states {
74
            if keep.insert(st) {
75
                match self.store.get_state(&st) {
76
                    Ok(s) => changes.extend(s.heads),
77
                    Err(StoreError::NotFound(_)) => continue,
78
                    Err(e) => return Err(e.into()),
79
                }
80
            }
81
        }
82
        for p in policies {
83
            if keep.insert(p) {
84
                let bytes = match self.store.get_typed(&p, ObjectType::Policy) {
85
                    Ok(b) => b,
86
                    Err(StoreError::NotFound(_)) => continue,
87
                    Err(e) => return Err(e.into()),
88
                };
89
                let policy = crate::policy::Policy::decode(&bytes)?;
90
                for id in &policy.identities {
91
                    keep.insert(id.hash());
92
                }
93
                for sig in self.store.signatures(&p)? {
94
                    keep.insert(sig.key);
95
                }
96
            }
97
        }
98
        // Changes: their closure, identities, blobs, chunks, seals.
99
        let mut stack = changes;
100
        while let Some(h) = stack.pop() {
101
            if !keep.insert(h) {
102
                continue;
103
            }
104
            // Beyond the shallow boundary, or pruned under an earlier
105
            // retention, there is nothing to keep.
106
            let c = match self.store.get_change(&h) {
107
                Ok(c) => c,
108
                Err(StoreError::NotFound(_)) => continue,
109
                Err(e) => return Err(e.into()),
110
            };
111
            stack.extend(c.deps.iter().copied());
112
            if !c.author.is_zero() {
113
                keep.insert(c.author);
114
            }
115
            for sig in self.store.signatures(&h)? {
116
                keep.insert(sig.key);
117
            }
118
            for e in &c.edits {
119
                if let Edit::Replace { old, new, .. } = e {
120
                    for b in [old, new].into_iter().flatten() {
121
                        if keep.insert(*b) {
122
                            if let Ok(bytes) = self.store.get_typed(b, ObjectType::Blob) {
123
                                keep.extend(Blob::decode(&bytes)?.chunks);
124
                            }
125
                        }
126
                    }
127
                }
128
            }
129
            attests.extend(self.store.seals_for(&h)?);
130
        }
131
        for a in attests {
132
            if keep.insert(a) {
133
                for sig in self.store.signatures(&a)? {
134
                    keep.insert(sig.key);
135
                }
136
            }
137
        }
138
        Ok(keep)
139
    }
140
}