| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 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 |
|
| 21 |
pub merged_packs: usize, |
| 22 |
} |
| 23 |
|
| 24 |
impl Repo { |
| 25 |
|
| 26 |
|
| 27 |
pub fn gc(&self, now: bool) -> Result<GcReport> { |
| 28 |
let keep = self.reachable()?; |
| 29 |
let grace = if now { |
| 30 |
std::time::Duration::ZERO |
| 31 |
} else { |
| 32 |
std::time::Duration::from_secs(self.config.gc_grace_days * 86_400) |
| 33 |
}; |
| 34 |
let pruned = self.store.prune(&keep, grace)?; |
| 35 |
|
| 36 |
|
| 37 |
let (packed, merged_packs) = self.store.repack_all(crate::store::REPACK_ZSTD_LEVEL)?; |
| 38 |
Ok(GcReport { |
| 39 |
packed, |
| 40 |
pruned, |
| 41 |
reachable: keep.len(), |
| 42 |
merged_packs, |
| 43 |
}) |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
pub fn reachable(&self) -> Result<HashSet<ObjectRef>> { |
| 48 |
let mut keep: HashSet<ObjectRef> = HashSet::new(); |
| 49 |
let mut states: Vec<ObjectRef> = Vec::new(); |
| 50 |
let mut changes: Vec<ChangeHash> = Vec::new(); |
| 51 |
let mut attests: Vec<ObjectRef> = Vec::new(); |
| 52 |
let mut policies: Vec<ObjectRef> = Vec::new(); |
| 53 |
|
| 54 |
|
| 55 |
let cutoff = Hlc::now() |
| 56 |
.wall_ms |
| 57 |
.saturating_sub(self.config.op_retention_days * 86_400_000); |
| 58 |
for (i, (r, op)) in self.op_log()?.into_iter().enumerate() { |
| 59 |
keep.insert(r); |
| 60 |
if i == 0 || op.hlc.wall_ms >= cutoff { |
| 61 |
states.extend(op.view.bookmarks.values().copied()); |
| 62 |
states.extend(op.view.remotes.values().flat_map(|b| b.values().copied())); |
| 63 |
changes.extend(op.view.workspaces.values().copied()); |
| 64 |
policies.extend(op.view.policy); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
for r in self.checkpoint_refs()? { |
| 69 |
if let Ok(bytes) = self.store.get_typed(&r, ObjectType::Checkpoint) { |
| 70 |
keep.insert(r); |
| 71 |
let cp = crate::objects::Checkpoint::decode(&bytes)?; |
| 72 |
keep.insert(cp.payload); |
| 73 |
if let Ok(b) = self.store.get_typed(&cp.payload, ObjectType::Blob) { |
| 74 |
keep.extend(Blob::decode(&b)?.chunks); |
| 75 |
} |
| 76 |
|
| 77 |
for g in self.checkpoint_graph_blobs(&cp).unwrap_or_default() { |
| 78 |
keep.insert(g); |
| 79 |
if let Ok(b) = self.store.get_typed(&g, ObjectType::Blob) { |
| 80 |
keep.extend(Blob::decode(&b)?.chunks); |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
let view_now = self.view()?; |
| 87 |
states.extend(view_now.tags.values().copied()); |
| 88 |
for name in view_now.tags.keys() { |
| 89 |
if let Some((r, _)) = self.tag_attest(name)? { |
| 90 |
attests.push(r); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
for name in self.view()?.bookmarks.keys() { |
| 95 |
if let Some((r, _)) = self.latest_ref_attest(name)? { |
| 96 |
attests.push(r); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
for st in states { |
| 102 |
if keep.insert(st) { |
| 103 |
match self.store.get_state(&st) { |
| 104 |
Ok(s) => changes.extend(s.heads), |
| 105 |
Err(StoreError::NotFound(_)) => continue, |
| 106 |
Err(e) => return Err(e.into()), |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
for p in policies { |
| 111 |
if keep.insert(p) { |
| 112 |
let bytes = match self.store.get_typed(&p, ObjectType::Policy) { |
| 113 |
Ok(b) => b, |
| 114 |
Err(StoreError::NotFound(_)) => continue, |
| 115 |
Err(e) => return Err(e.into()), |
| 116 |
}; |
| 117 |
let policy = crate::policy::Policy::decode(&bytes)?; |
| 118 |
for id in &policy.identities { |
| 119 |
keep.insert(id.hash()); |
| 120 |
} |
| 121 |
for sig in self.store.signatures(&p)? { |
| 122 |
keep.insert(sig.key); |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
let mut stack = changes; |
| 128 |
while let Some(h) = stack.pop() { |
| 129 |
if !keep.insert(h) { |
| 130 |
continue; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
let c = match self.store.get_change_parts(&h) { |
| 135 |
Ok((c, texts)) => { |
| 136 |
keep.extend(texts); |
| 137 |
c |
| 138 |
} |
| 139 |
Err(StoreError::NotFound(_)) => continue, |
| 140 |
Err(e) => return Err(e.into()), |
| 141 |
}; |
| 142 |
stack.extend(c.deps.iter().copied()); |
| 143 |
if !c.author.is_zero() { |
| 144 |
keep.insert(c.author); |
| 145 |
} |
| 146 |
for sig in self.store.signatures(&h)? { |
| 147 |
keep.insert(sig.key); |
| 148 |
} |
| 149 |
for e in &c.edits { |
| 150 |
if let Edit::Replace { old, new, .. } = e { |
| 151 |
for b in [old, new].into_iter().flatten() { |
| 152 |
if keep.insert(*b) { |
| 153 |
if let Ok(bytes) = self.store.get_typed(b, ObjectType::Blob) { |
| 154 |
keep.extend(Blob::decode(&bytes)?.chunks); |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
attests.extend(self.store.seals_for(&h)?); |
| 161 |
} |
| 162 |
self.reachable_receipts(&mut keep)?; |
| 163 |
for a in attests { |
| 164 |
if keep.insert(a) { |
| 165 |
for sig in self.store.signatures(&a)? { |
| 166 |
keep.insert(sig.key); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
Ok(keep) |
| 171 |
} |
| 172 |
} |