| da652d63 Andy McCall ↶ |
1 |
|
| ba160f60 Andy McCall ↶ |
2 |
|
| 3 |
|
| 4 |
|
| b689779e Andy McCall ↶ |
5 |
|
| 6 |
|
| fc1a466a Andy McCall ↶ |
7 |
|
| ba160f60 Andy McCall ↶ |
8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
use super::*; |
| 13 |
use crate::store::{PackReport, PruneReport}; |
| 14 |
use std::collections::HashSet; |
| 15 |
|
| 16 |
#[derive(Debug, Clone, Default, PartialEq, Eq)] |
| 17 |
pub struct GcReport { |
| 18 |
pub packed: PackReport, |
| 19 |
pub pruned: PruneReport, |
| 20 |
pub reachable: usize, |
| da652d63 Andy McCall ↶ |
21 |
|
| 135212de Andy McCall ↶ |
22 |
pub merged_packs: usize, |
| ba160f60 Andy McCall ↶ |
23 |
} |
| 24 |
|
| 25 |
impl Repo { |
| da652d63 Andy McCall ↶ |
26 |
|
| 27 |
|
| ba160f60 Andy McCall ↶ |
28 |
pub fn gc(&self, now: bool) -> Result<GcReport> { |
| 9ae4d15a Andy McCall ↶ |
29 |
self.gc_with(now, &mut |_| {}) |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
pub fn gc_with(&self, now: bool, progress: crate::store::Progress) -> Result<GcReport> { |
| 9d580f21 Andy McCall ↶ |
35 |
self.gc_keeping(now, &[], progress) |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
pub fn gc_keeping( |
| 43 |
&self, |
| 44 |
now: bool, |
| 45 |
extra: &[ObjectRef], |
| 46 |
progress: crate::store::Progress, |
| 47 |
) -> Result<GcReport> { |
| 9ae4d15a Andy McCall ↶ |
48 |
progress("finding what is reachable"); |
| 9d580f21 Andy McCall ↶ |
49 |
let mut keep = self.reachable()?; |
| 50 |
for r in extra { |
| 51 |
if !self.store.has(r) || !keep.insert(*r) { |
| 52 |
continue; |
| 53 |
} |
| 54 |
if let Ok(body) = self.store.get_typed(r, ObjectType::Blob) { |
| 55 |
if let Ok(blob) = Blob::decode(&body) { |
| 56 |
keep.extend(blob.chunks); |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 75167672 Andy McCall |
60 |
let grace = if now { |
| 61 |
std::time::Duration::ZERO |
| 62 |
} else { |
| 63 |
std::time::Duration::from_secs(self.config.gc_grace_days * 86_400) |
| 64 |
}; |
| 9ae4d15a Andy McCall ↶ |
65 |
let pruned = self.store.prune_with(&keep, grace, progress)?; |
| da652d63 Andy McCall ↶ |
66 |
|
| 67 |
|
| 9ae4d15a Andy McCall ↶ |
68 |
let (packed, merged_packs) = self |
| 69 |
.store |
| 70 |
.repack_all_with(crate::store::REPACK_ZSTD_LEVEL, progress)?; |
| 75167672 Andy McCall |
71 |
Ok(GcReport { |
| 72 |
packed, |
| 73 |
pruned, |
| 74 |
reachable: keep.len(), |
| 75 |
merged_packs, |
| 76 |
}) |
| ba160f60 Andy McCall ↶ |
77 |
} |
| 78 |
|
| 79 |
|
| 80 |
pub fn reachable(&self) -> Result<HashSet<ObjectRef>> { |
| 81 |
let mut keep: HashSet<ObjectRef> = HashSet::new(); |
| 82 |
let mut states: Vec<ObjectRef> = Vec::new(); |
| 83 |
let mut changes: Vec<ChangeHash> = Vec::new(); |
| 84 |
let mut attests: Vec<ObjectRef> = Vec::new(); |
| 85 |
let mut policies: Vec<ObjectRef> = Vec::new(); |
| 86 |
|
| b689779e Andy McCall ↶ |
87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
let now_ms = Hlc::now().wall_ms; |
| 92 |
let cutoff = now_ms.saturating_sub(self.config.op_retention_days * 86_400_000); |
| 93 |
let wc_cutoff = now_ms |
| 94 |
.saturating_sub(self.config.snapshot_retention_days * 86_400_000) |
| 95 |
.max(cutoff); |
| ba160f60 Andy McCall ↶ |
96 |
for (i, (r, op)) in self.op_log()?.into_iter().enumerate() { |
| 97 |
keep.insert(r); |
| 98 |
if i == 0 || op.hlc.wall_ms >= cutoff { |
| 99 |
states.extend(op.view.bookmarks.values().copied()); |
| 100 |
states.extend(op.view.remotes.values().flat_map(|b| b.values().copied())); |
| 101 |
policies.extend(op.view.policy); |
| b689779e Andy McCall ↶ |
102 |
} |
| 103 |
if i == 0 || (self.config.snapshot_retention_days > 0 && op.hlc.wall_ms >= wc_cutoff) { |
| 104 |
changes.extend(op.view.workspaces.values().copied()); |
| ba160f60 Andy McCall ↶ |
105 |
} |
| 106 |
} |
| 252e8cb2 Andy McCall ↶ |
107 |
|
| 108 |
for r in self.checkpoint_refs()? { |
| 109 |
if let Ok(bytes) = self.store.get_typed(&r, ObjectType::Checkpoint) { |
| 110 |
keep.insert(r); |
| 111 |
let cp = crate::objects::Checkpoint::decode(&bytes)?; |
| 112 |
keep.insert(cp.payload); |
| 113 |
if let Ok(b) = self.store.get_typed(&cp.payload, ObjectType::Blob) { |
| 114 |
keep.extend(Blob::decode(&b)?.chunks); |
| 115 |
} |
| afaba5f1 Andy McCall ↶ |
116 |
|
| 117 |
for g in self.checkpoint_graph_blobs(&cp).unwrap_or_default() { |
| 118 |
keep.insert(g); |
| 119 |
if let Ok(b) = self.store.get_typed(&g, ObjectType::Blob) { |
| 120 |
keep.extend(Blob::decode(&b)?.chunks); |
| 121 |
} |
| 122 |
} |
| 252e8cb2 Andy McCall ↶ |
123 |
} |
| 124 |
} |
| 132b884b Andy McCall ↶ |
125 |
|
| 126 |
let view_now = self.view()?; |
| 127 |
states.extend(view_now.tags.values().copied()); |
| 128 |
for name in view_now.tags.keys() { |
| 129 |
if let Some((r, _)) = self.tag_attest(name)? { |
| 130 |
attests.push(r); |
| 131 |
} |
| 132 |
} |
| ba160f60 Andy McCall ↶ |
133 |
|
| 134 |
for name in self.view()?.bookmarks.keys() { |
| 135 |
if let Some((r, _)) = self.latest_ref_attest(name)? { |
| f53cbe7a Andy McCall ↶ |
136 |
|
| 137 |
if let Some(acc) = self.acceptance_of(&r)? { |
| 138 |
attests.push(acc.attest.hash()); |
| 139 |
} |
| ba160f60 Andy McCall ↶ |
140 |
attests.push(r); |
| 141 |
} |
| 142 |
} |
| 21b2aec2 Andy McCall ↶ |
143 |
|
| 144 |
|
| ba160f60 Andy McCall ↶ |
145 |
for st in states { |
| 146 |
if keep.insert(st) { |
| 21b2aec2 Andy McCall ↶ |
147 |
match self.store.get_state(&st) { |
| 148 |
Ok(s) => changes.extend(s.heads), |
| 149 |
Err(StoreError::NotFound(_)) => continue, |
| 150 |
Err(e) => return Err(e.into()), |
| 151 |
} |
| ba160f60 Andy McCall ↶ |
152 |
} |
| 153 |
} |
| 154 |
for p in policies { |
| 155 |
if keep.insert(p) { |
| 21b2aec2 Andy McCall ↶ |
156 |
let bytes = match self.store.get_typed(&p, ObjectType::Policy) { |
| 157 |
Ok(b) => b, |
| 158 |
Err(StoreError::NotFound(_)) => continue, |
| 159 |
Err(e) => return Err(e.into()), |
| 160 |
}; |
| 161 |
let policy = crate::policy::Policy::decode(&bytes)?; |
| ba160f60 Andy McCall ↶ |
162 |
for id in &policy.identities { |
| 163 |
keep.insert(id.hash()); |
| 164 |
} |
| 165 |
for sig in self.store.signatures(&p)? { |
| 166 |
keep.insert(sig.key); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
let mut stack = changes; |
| 172 |
while let Some(h) = stack.pop() { |
| 173 |
if !keep.insert(h) { |
| 174 |
continue; |
| 175 |
} |
| 21b2aec2 Andy McCall ↶ |
176 |
|
| 177 |
|
| 6f8aeb99 Andy McCall ↶ |
178 |
let c = match self.store.get_change_parts(&h) { |
| 179 |
Ok((c, texts)) => { |
| 180 |
keep.extend(texts); |
| 181 |
c |
| 182 |
} |
| 2863456a Andy McCall ↶ |
183 |
Err(StoreError::NotFound(_)) => continue, |
| 184 |
Err(e) => return Err(e.into()), |
| 185 |
}; |
| ba160f60 Andy McCall ↶ |
186 |
stack.extend(c.deps.iter().copied()); |
| 187 |
if !c.author.is_zero() { |
| 188 |
keep.insert(c.author); |
| 189 |
} |
| 190 |
for sig in self.store.signatures(&h)? { |
| f53cbe7a Andy McCall ↶ |
191 |
self.keep_signer(&mut keep, sig.key)?; |
| ba160f60 Andy McCall ↶ |
192 |
} |
| 193 |
for e in &c.edits { |
| 194 |
if let Edit::Replace { old, new, .. } = e { |
| 195 |
for b in [old, new].into_iter().flatten() { |
| 196 |
if keep.insert(*b) { |
| 197 |
if let Ok(bytes) = self.store.get_typed(b, ObjectType::Blob) { |
| 198 |
keep.extend(Blob::decode(&bytes)?.chunks); |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
attests.extend(self.store.seals_for(&h)?); |
| 205 |
} |
| fc1a466a Andy McCall ↶ |
206 |
self.reachable_receipts(&mut keep)?; |
| 3a4b3e01 Andy McCall ↶ |
207 |
self.reachable_approvals(&mut keep)?; |
| ba160f60 Andy McCall ↶ |
208 |
for a in attests { |
| 209 |
if keep.insert(a) { |
| 210 |
for sig in self.store.signatures(&a)? { |
| f53cbe7a Andy McCall ↶ |
211 |
self.keep_signer(&mut keep, sig.key)?; |
| ba160f60 Andy McCall ↶ |
212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
Ok(keep) |
| f53cbe7a Andy McCall ↶ |
216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
pub(crate) fn keep_signer(&self, keep: &mut HashSet<ObjectRef>, key: ObjectRef) -> Result<()> { |
| 221 |
keep.insert(key); |
| 222 |
if let Some(c) = self.store.certificate_of(&key)? { |
| 223 |
keep.insert(c); |
| 224 |
if let Ok(cert) = self.store.get_certificate(&c) { |
| 225 |
keep.insert(cert.cert.issuer); |
| 226 |
} |
| 227 |
} |
| 228 |
Ok(()) |
| ba160f60 Andy McCall ↶ |
229 |
} |
| 230 |
} |