OXYGENHUB
# ee06a6c557b7 oxygen / crates / oxy-core / src / repo / gc.rs
5.4 KiB · Rust · last changed Tags: immutable named states with tag attestations, in views and on the wire, policy-governed, exported to Git (spec 0.6h) 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
        // Tags: their states and attestations.
66
        let view_now = self.view()?;
67
        states.extend(view_now.tags.values().copied());
68
        for name in view_now.tags.keys() {
69
            if let Some((r, _)) = self.tag_attest(name)? {
70
                attests.push(r);
71
            }
72
        }
73
        // Reference attestations.
74
        for name in self.view()?.bookmarks.keys() {
75
            if let Some((r, _)) = self.latest_ref_attest(name)? {
76
                attests.push(r);
77
            }
78
        }
79
        for st in states {
80
            if keep.insert(st) {
81
                changes.extend(self.store.get_state(&st)?.heads);
82
            }
83
        }
84
        for p in policies {
85
            if keep.insert(p) {
86
                let policy = crate::policy::Policy::decode(&self.store.get_typed(&p, ObjectType::Policy)?)?;
87
                for id in &policy.identities {
88
                    keep.insert(id.hash());
89
                }
90
                for sig in self.store.signatures(&p)? {
91
                    keep.insert(sig.key);
92
                }
93
            }
94
        }
95
        // Changes: their closure, identities, blobs, chunks, seals.
96
        let mut stack = changes;
97
        while let Some(h) = stack.pop() {
98
            if !keep.insert(h) {
99
                continue;
100
            }
101
            // Beyond the shallow boundary there is nothing to keep.
102
            let c = match self.store.get_change(&h) {
103
                Ok(c) => c,
104
                Err(StoreError::NotFound(_)) => continue,
105
                Err(e) => return Err(e.into()),
106
            };
107
            stack.extend(c.deps.iter().copied());
108
            if !c.author.is_zero() {
109
                keep.insert(c.author);
110
            }
111
            for sig in self.store.signatures(&h)? {
112
                keep.insert(sig.key);
113
            }
114
            for e in &c.edits {
115
                if let Edit::Replace { old, new, .. } = e {
116
                    for b in [old, new].into_iter().flatten() {
117
                        if keep.insert(*b) {
118
                            if let Ok(bytes) = self.store.get_typed(b, ObjectType::Blob) {
119
                                keep.extend(Blob::decode(&bytes)?.chunks);
120
                            }
121
                        }
122
                    }
123
                }
124
            }
125
            attests.extend(self.store.seals_for(&h)?);
126
        }
127
        for a in attests {
128
            if keep.insert(a) {
129
                for sig in self.store.signatures(&a)? {
130
                    keep.insert(sig.key);
131
                }
132
            }
133
        }
134
        Ok(keep)
135
    }
136
}