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