| 1 | //! The per-cell ownership decision, reified sans-IO. |
| 2 | //! |
| 3 | //! A cell's authority lives in one write-once-then-CAS record, |
| 4 | //! `cells/<cell>/own.json` = `{ node, epoch }`. Acquiring it is the |
| 5 | //! Probing→Claiming logic `ownership.rs` performs: read the record, and — for a |
| 6 | //! record owned by someone else — read that owner's node lease to see if it is |
| 7 | //! still alive, then either take over at `epoch+1` or defer. Only the GETs and |
| 8 | //! the CAS are I/O; the branching (epoch math, take-over-vs-advance-vs-defer) |
| 9 | //! is a pure function extracted here, so production's async `acquire_cell` and |
| 10 | //! a deterministic executor drive the identical decision. |
| 11 | |
| 12 | /// The core's view of an ownership record. The serde wire type |
| 13 | /// (`ownership::Own`) maps to this at the I/O edge; the core carries no |
| 14 | /// serialization concern. An empty `node` is the relinquished/unowned marker. |
| 15 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 16 | pub struct Owner { |
| 17 | pub node: String, |
| 18 | pub epoch: u64, |
| 19 | } |
| 20 | |
| 21 | /// One step of acquisition. `NeedLiveness` models the two-GET shape: the caller |
| 22 | /// performs the owner's node-lease GET, then calls `acquire` again with |
| 23 | /// `owner_live: Some(..)`. |
| 24 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 25 | pub enum Acquire { |
| 26 | /// No record (or it vanished) — nothing to acquire here. |
| 27 | GiveUp, |
| 28 | /// Owned by another node whose liveness is unknown — go read its lease. |
| 29 | NeedLiveness, |
| 30 | /// Owned by a live node — back off. |
| 31 | Defer, |
| 32 | /// Write ownership to me at `epoch` via if-match. `takeover` seizes a dead |
| 33 | /// owner's cell (vs. advancing my own epoch); it only colors the activity |
| 34 | /// log, but the two must not be conflated. |
| 35 | Take { epoch: u64, takeover: bool }, |
| 36 | } |
| 37 | |
| 38 | /// The acquisition decision. `owner` is the observed record (`None` = absent); |
| 39 | /// `owner_live` is `None` until the owner's lease has been read. |
| 40 | pub fn acquire(owner: Option<&Owner>, me: &str, owner_live: Option<bool>) -> Acquire { |
| 41 | let Some(o) = owner else { |
| 42 | return Acquire::GiveUp; |
| 43 | }; |
| 44 | // Reusing a node id (restart) still advances the epoch, so a superseded |
| 45 | // lineage's late writes can never land in ours. |
| 46 | if o.node == me { |
| 47 | return Acquire::Take { epoch: o.epoch + 1, takeover: false }; |
| 48 | } |
| 49 | match owner_live { |
| 50 | None => Acquire::NeedLiveness, |
| 51 | Some(true) => Acquire::Defer, |
| 52 | Some(false) => Acquire::Take { epoch: o.epoch + 1, takeover: true }, |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /// The record to write when relinquishing: node cleared, epoch retained so the |
| 57 | /// next owner must advance it (deleting would reset the fence sequence to one). |
| 58 | /// `None` if the on-disk owner no longer matches — someone else moved, and it |
| 59 | /// is not ours to release. |
| 60 | pub fn relinquish(owner: Option<&Owner>, me: &str, my_epoch: u64) -> Option<Owner> { |
| 61 | match owner { |
| 62 | Some(o) if o.node == me && o.epoch == my_epoch => { |
| 63 | Some(Owner { node: String::new(), epoch: my_epoch }) |
| 64 | } |
| 65 | _ => None, |
| 66 | } |
| 67 | } |