| 1 | //! `celld-logic`: the sans-IO coordination core — a pure library the celld |
| 2 | //! binary drives in production. |
| 3 | //! |
| 4 | //! Coordination logic is a pure function of `(state, event) -> effects`: no |
| 5 | //! async, no I/O, no clock reads, no rng, no locks. All nondeterminism is |
| 6 | //! confined to the PORTS below — `Clock`, `Rand`, `ObjectStore`, `Replicator`, |
| 7 | //! `CellRuntime`. Production supplies tokio + real S3 + a Litestream subprocess |
| 8 | //! behind them; because the core reads only the ports, an executor that |
| 9 | //! supplies seeded, deterministic models can replay it exactly. That |
| 10 | //! replayability is the whole point. |
| 11 | //! |
| 12 | //! Grown one subsystem at a time. Landed: the pure `wake`/`pressure` transitions, |
| 13 | //! the node-lease `lease` decision, and the per-cell `cell` acquisition. The |
| 14 | //! full ownership lifecycle (`on_event`) reifies `ownership.rs` as the single |
| 15 | //! source of the coordination decision — see the design pass in the doc. |
| 16 | #![allow(dead_code)] // interface module: ports and variants wire in per subsystem |
| 17 | |
| 18 | pub mod alarm; |
| 19 | pub mod cache; |
| 20 | pub mod cell; |
| 21 | pub mod dead_node_reconciliation; |
| 22 | pub mod evict; |
| 23 | pub mod lease; |
| 24 | pub mod lifecycle; |
| 25 | pub mod peer; |
| 26 | pub mod pressure; |
| 27 | pub mod restore; |
| 28 | pub mod routing; |
| 29 | pub mod schedule; |
| 30 | pub mod sqlite; |
| 31 | pub mod wake; |
| 32 | |
| 33 | /// S3 entity tag. Opaque to the core; equality is the only operation it needs. |
| 34 | pub type Etag = String; |
| 35 | |
| 36 | /// Milliseconds. Signed to carry wake's `-1 = no alarm` sentinel and to make |
| 37 | /// monotonic deltas subtractable without underflow ceremony. |
| 38 | pub type Ms = i64; |
| 39 | |
| 40 | /// Why a conditional store op did not take effect — the core branches on this |
| 41 | /// exactly as `ownership.rs` branches on a 412 versus a network error. |
| 42 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 43 | pub enum StoreError { |
| 44 | /// 412: an `if-none-match` / `if-match` precondition failed — someone else |
| 45 | /// moved first. A *correct* refusal, not a fault. |
| 46 | Precondition, |
| 47 | /// Network / timeout / 5xx: the outcome is unknown. Retry; never assume it |
| 48 | /// failed (an assumed-failed PUT that actually landed is a lost fence). |
| 49 | Transient, |
| 50 | } |
| 51 | |
| 52 | pub type StoreResult<T> = Result<T, StoreError>; |
| 53 | |
| 54 | /// Compare-and-swap object store — the widest port. Every `aws_sdk_s3` call in |
| 55 | /// `ownership.rs` / `wake.rs` is one of these, with etag/412 semantics any |
| 56 | /// executor must reproduce exactly. Sync because the core is single-threaded; |
| 57 | /// production's async executor performs the same operations off-core. |
| 58 | pub trait ObjectStore { |
| 59 | /// Body + etag, or `None` if absent. |
| 60 | fn get(&mut self, key: &str) -> StoreResult<Option<(Vec<u8>, Etag)>>; |
| 61 | /// `if-none-match: *` — create iff absent. `Err(Precondition)` if present. |
| 62 | fn put_if_absent(&mut self, key: &str, body: &[u8]) -> StoreResult<Etag>; |
| 63 | /// `if-match: etag` — replace iff unchanged. `Err(Precondition)` on drift. |
| 64 | fn put_if_match( |
| 65 | &mut self, |
| 66 | key: &str, |
| 67 | etag: &Etag, |
| 68 | body: &[u8], |
| 69 | ) -> StoreResult<Etag>; |
| 70 | /// Unconditional PUT (wake entries need no CAS). |
| 71 | fn put(&mut self, key: &str, body: &[u8]) -> StoreResult<Etag>; |
| 72 | /// Keys under `prefix`, lexicographically ascending — S3 list order, which |
| 73 | /// the wake scan relies on to stop at the first future bucket. |
| 74 | fn list(&mut self, prefix: &str) -> StoreResult<Vec<(String, Etag)>>; |
| 75 | /// Unconditional DELETE; deleting an absent key is not an error. |
| 76 | fn delete(&mut self, key: &str) -> StoreResult<()>; |
| 77 | } |
| 78 | |
| 79 | /// Time, split by trust. Wall is `SystemTime`: advisory, skewable, used only |
| 80 | /// for lease-expiry hints. Mono is `Instant`: the fence is monotonic by design |
| 81 | /// (`ownership.rs`), so a backward wall step can never suppress it. |
| 82 | pub trait Clock { |
| 83 | fn wall_ms(&self) -> Ms; |
| 84 | fn mono_ms(&self) -> Ms; |
| 85 | } |
| 86 | |
| 87 | /// Seeded randomness. The load-bearing draw is the node-session id |
| 88 | /// (`main.rs`, `OsRng` in production); alarm-generation ids route here too so a |
| 89 | /// seeded executor can replay them. |
| 90 | pub trait Rand { |
| 91 | fn next_u64(&mut self) -> u64; |
| 92 | } |
| 93 | |
| 94 | /// Durability, kept a subprocess in production (Litestream). Pinned here; wired |
| 95 | /// in step 4. Behind this port an executor can apply replication lag, torn-write, |
| 96 | /// and stale-restore faults — proving coordination correctness given a |
| 97 | /// durability oracle. |
| 98 | pub trait Replicator { |
| 99 | /// Begin replicating this cell at `epoch` (a new owner restores then serves). |
| 100 | fn activate(&mut self, cell: &str, epoch: u64); |
| 101 | /// Stop replicating (hibernate or release). |
| 102 | fn hibernate(&mut self, cell: &str); |
| 103 | /// Highest epoch whose writes are durably in the bucket — the lag oracle a |
| 104 | /// stale-restore fault perturbs. |
| 105 | fn epoch_replicated(&self, cell: &str) -> Option<u64>; |
| 106 | } |
| 107 | |
| 108 | /// The V8 isolate, kept OUTSIDE the core (cells become scripted event sources). |
| 109 | /// The boundary already exists as the `CellJob` channel plus the |
| 110 | /// `next_alarm_ms` atomic, so this is minimal surgery; wired in step 5. |
| 111 | pub trait CellRuntime { |
| 112 | fn dispatch(&mut self, cell: &str, job: CellJob); |
| 113 | } |
| 114 | |
| 115 | /// Placeholder for the cell-runtime job vocabulary (fleshed out in step 5). |
| 116 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 117 | pub enum CellJob { |
| 118 | Fetch, |
| 119 | Alarm { gen: u64 }, |
| 120 | } |