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