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