Diffinery

homeflare/celldpublic

celld/crates/logic/src/restore.rs
38 lines · 2.1 KBRaw History
1//! Restore-source durability predicates, reified sans-IO. Activation restores a
2//! cell's SQLite from the newest SAFE source — a local hibernation snapshot, a
3//! local crash epoch, or the replicated bucket. Which source is a durability
4//! decision: pick a stale one over a newer replica and the cell serves lost
5//! writes; pick a needlessly-remote one and pay dozens of sequential round trips
6//! (measured: 46, 0 local reuses in 910 activations, before the previous-epoch
7//! lookup). The AVAILABILITY of each source is I/O (file checks, S3 LIST); these
8//! predicates are the pure choices among them, and the safety fences within.
9
10pub type Epoch = u64;
11
12/// May a hibernation snapshot from the PREVIOUS epoch be reused? Ordinary idle
13/// hibernation is followed by an epoch advance, so its cache sits under
14/// `epoch - 1` — safe to reuse only when we did NOT take the cell over from
15/// another node. A takeover means someone else may have written the cell while
16/// we slept, so their newer durable state, not our stale snapshot, is
17/// authoritative. Epoch 1 has no previous epoch.
18pub fn previous_epoch_reusable(epoch: Epoch, took_over: bool) -> bool {
19 epoch > 1 && !took_over
20}
21
22/// Restore the LOCAL crash epoch instead of the replicated bucket? Yes when
23/// there is no replica, or the local epoch is at least as new as the newest
24/// replicated one — a local restore is cheap, and a same-or-newer local epoch
25/// carries every durable write the replica holds. A replica STRICTLY newer than
26/// the local epoch wins: it may hold writes this node never saw.
27pub fn local_epoch_wins(local_epoch: Epoch, remote_epoch: Option<Epoch>) -> bool {
28 remote_epoch.is_none_or(|remote| local_epoch >= remote)
29}
30
31/// May a discovered local epoch be recovered during activation of `activating`?
32/// Only when STRICTLY before it. Recovering an epoch `>= activating` would
33/// reopen the ownership the activation just advanced past, letting two processes
34/// open the same db — the strict `<` is the fence, not an optimization.
35pub fn recoverable(candidate: Epoch, activating: Epoch) -> bool {
36 candidate < activating
37}
38