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