Diffinery

homeflare/celldpublic

celld/crates/logic/epoch_gc.rs
98 lines · 4.2 KBRaw History
1// Copyright 2026 Deno Land Inc. Apache-2.0 license.
2
3//! Which superseded LTX epoch prefixes an owner may delete.
4//!
5//! Every activation opens a new epoch prefix, and a restore reads only the
6//! epochs of its chain: it walks down from the newest epoch and stops at the
7//! first span that opens at TXID 1, the chain's base. Every epoch below the
8//! base is never read again, yet nothing removed it, so a bucket grew with
9//! database size times activations (denoland/celld#240).
10//!
11//! The decision is pure; the owner supplies the facts by I/O. The chain is the
12//! one the restore code builds (`EpochChain::spans`), over every listed epoch
13//! including the owner's own. A second walk written here could disagree with
14//! the restore walk, and the disagreement deletes a base: a fenced owner's
15//! late snapshot in an intermediate epoch opens at TXID 1 yet sits outside the
16//! chain, so "the newest epoch that opens at TXID 1" is not the base.
17//!
18//! The owner deletes nothing until the chain's newest span is its own epoch.
19//! That proves its own opener (a TXID-1 snapshot, or a paged activation's
20//! marker) is listed, so a successor that claims the cell later lists it too
21//! and restores from a base at or above this one. The caller must read
22//! `own.json` after this point and before the delete; that order, not a
23//! conditional write, is the fence. Every uncertain fact keeps the epoch: a
24//! leak costs bytes, a wrong delete makes a cell unrestorable.
25
26pub type Epoch = u64;
27
28/// One epoch prefix under a stream, with the upload time of its newest object.
29#[derive(Debug, Clone, Copy, PartialEq, Eq)]
30pub struct ListedEpoch {
31 pub epoch: Epoch,
32 pub newest_ms: u64,
33}
34
35/// What an owner does after activation.
36#[derive(Debug, Clone, PartialEq, Eq)]
37pub struct Plan {
38 /// The chain's base. Rows for epochs below it are covered by the base's
39 /// image, so bundle GC and recovery may treat them as retired.
40 pub retired_below: Epoch,
41 /// Whether the stream's recorded mark must be raised to `retired_below`
42 /// before any delete.
43 pub record: bool,
44 /// Epoch prefixes to delete, ascending.
45 pub delete: Vec<Epoch>,
46}
47
48/// Plans one stream's epoch GC, or `None` when nothing may be decided yet.
49///
50/// `chain` holds the restore chain's epochs, oldest first. `recorded` is the
51/// stream's current retired mark. `pinned_reads` is true while the owner's
52/// own activation still reads objects by the chain it built at activation:
53/// a paged cell faults pages from those objects until its background fill
54/// completes. The chain can gain a later base in the meantime (a fenced
55/// owner's late snapshot that ends exactly at the cut links in above the old
56/// base), and deleting below the new base would remove objects the fill
57/// still reads. An epoch is deleted only when it is below the
58/// base, is neither the owner's epoch nor the one before it, and its newest
59/// object is at least `grace_ms` old. The previous-epoch rule is a margin
60/// against a lagging listing, not the safety argument.
61pub fn plan(
62 own_epoch: Epoch,
63 chain: &[Epoch],
64 listed: &[ListedEpoch],
65 recorded: Option<Epoch>,
66 pinned_reads: bool,
67 now_ms: u64,
68 grace_ms: u64,
69) -> Option<Plan> {
70 if pinned_reads || chain.last() != Some(&own_epoch) {
71 return None;
72 }
73 let base = *chain.first()?;
74 // A recorded mark above the base means an earlier owner saw a higher
75 // base. The base never falls under a consistent listing, so this owner's
76 // view is stale; it decides nothing.
77 if recorded.is_some_and(|mark| mark > base) {
78 return None;
79 }
80 let delete = listed
81 .iter()
82 .filter(|l| l.epoch < base && l.epoch + 1 < own_epoch)
83 .filter(|l| now_ms.saturating_sub(l.newest_ms) >= grace_ms)
84 .map(|l| l.epoch)
85 .collect();
86 Some(Plan {
87 retired_below: base,
88 record: recorded != Some(base),
89 delete,
90 })
91}
92
93/// Is a node-log row for `epoch` covered by a stream's retired mark? The mark
94/// is read per stream by its exact name; a facet nests under its root's
95/// path, and a prefix match would apply the root's mark to the facet.
96pub fn retired(epoch: Epoch, recorded: Option<Epoch>) -> bool {
97 recorded.is_some_and(|mark| epoch < mark)
98}