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