| 1 | # Ownership and fencing |
| 2 | |
| 3 | celld makes two claims. One node owns a cell at a time, and a write is |
| 4 | durable before celld acknowledges it. This page gives the mechanism |
| 5 | behind each claim, so you can check the argument against the code. |
| 6 | |
| 7 | A short version first. The ownership records use conditional writes. |
| 8 | The replication stream uses plain writes, because the epoch in the key |
| 9 | is the fence. The acknowledgement path re-reads the ownership record, |
| 10 | so a stale node cannot make a promise that the fleet does not keep. |
| 11 | |
| 12 | ## The ownership record |
| 13 | |
| 14 | Each cell has one ownership record in the bucket. The record names the |
| 15 | owner node's session and carries a fencing epoch. A node acquires a |
| 16 | cell with a conditional write: a create when no record exists, and a |
| 17 | compare-and-swap on the previous record when one does. The bucket |
| 18 | accepts one such write, so two nodes cannot acquire the same cell. |
| 19 | |
| 20 | Every activation advances the epoch. A takeover advances it, and a |
| 21 | local wake advances it too. Each owner therefore replicates under a |
| 22 | fresh epoch, and an epoch never has two writers. |
| 23 | |
| 24 | ## Replication and the epoch prefix |
| 25 | |
| 26 | The replicator copies the SQLite data of each cell to the bucket under |
| 27 | an epoch prefix: `cells/<cell>/ltx/e<epoch>/`. These segment writes |
| 28 | are plain, unconditional PUTs. That is intentional: the fence is the |
| 29 | epoch in the key, not a condition on the request. A node that lost its |
| 30 | ownership can continue to write, but its writes land in a superseded |
| 31 | prefix. A restore selects the current lineage, so the stale node |
| 32 | cannot corrupt the data of the new owner, and the data path pays no |
| 33 | conditional-write cost. |
| 34 | |
| 35 | The prefix protects the data. It does not, alone, protect the promise |
| 36 | to the client. The next two mechanisms close that gap. |
| 37 | |
| 38 | ## The acknowledgement rule (RPO=0) |
| 39 | |
| 40 | A gate holds the response of each write until the replicator proves |
| 41 | that the write is in the bucket. After the proof, celld reads the |
| 42 | ownership record one time, and celld acknowledges only if the record |
| 43 | still names this node at this epoch. A partitioned node can commit |
| 44 | locally and replicate into its own superseded prefix, but its |
| 45 | ownership read shows the new owner, so the client never receives an |
| 46 | acknowledgement for a write that the surviving lineage does not |
| 47 | contain. The check is a read of the record, not a clock comparison, so |
| 48 | a paused process or a skewed clock cannot pass it. |
| 49 | |
| 50 | ## The epoch seal |
| 51 | |
| 52 | A restore selects the newest epoch prefix that contains data. A fenced |
| 53 | node can append to that prefix after the takeover, because the segment |
| 54 | writes are unconditional. Without a further rule, a later restore |
| 55 | could read that appended tail: writes that no client saw acknowledged. |
| 56 | |
| 57 | The seal closes this hole. The first activation that restores from an |
| 58 | epoch writes a seal object (`e<epoch>.seal.json`) with a conditional |
| 59 | create, and the seal fixes the highest transaction that any restore of |
| 60 | that epoch can read. The conditional create means that the first |
| 61 | restorer wins, so every later restore reads the same cut. Every |
| 62 | acknowledged write sits at or below the seal, because the |
| 63 | acknowledgement required an ownership read, and the takeover preceded |
| 64 | the seal. The later writes of the fenced node sit above the seal, so |
| 65 | they never return. If the seal write fails, the activation fails, |
| 66 | because a restore of an unsealed prefix reopens the hole. |
| 67 | |
| 68 | ## Self-fencing |
| 69 | |
| 70 | A node that cannot reach the bucket cannot renew its lease, and it |
| 71 | cannot replicate. Such a node must not own cells, so it fences itself: |
| 72 | it stops the writes and releases its residency. A different node can |
| 73 | then acquire the cells through the ownership records. The failure of a |
| 74 | node is a normal input, not a recovery procedure; the [testing |
| 75 | page](testing.md) shows the kill tests that exercise this path. |
| 76 | |
| 77 | ## What the bucket must provide |
| 78 | |
| 79 | celld needs three properties from the object store: |
| 80 | |
| 81 | - A conditional create. The create must fail when the object already |
| 82 | exists. |
| 83 | - A conditional overwrite. The write must fail when the object changed |
| 84 | after the read. |
| 85 | - Read-after-write consistency. A read after a successful write must |
| 86 | return that write. |
| 87 | |
| 88 | On an S3-compatible bucket, celld sends the `If-None-Match: *` and |
| 89 | `If-Match` headers, and the condition compares the etag. Amazon S3, |
| 90 | Cloudflare R2, and Tigris document these operations. celld's release |
| 91 | tests run against Cloudflare R2, and the AWS S3 path uses the same |
| 92 | client and the same headers. |
| 93 | |
| 94 | A `gs://` bucket selects Google Cloud Storage. celld then uses the |
| 95 | Cloud Storage XML API with the `x-goog-if-generation-match` |
| 96 | precondition and OAuth credentials, and the condition compares the |
| 97 | object generation. celld does not send the S3 request dialect to |
| 98 | Cloud Storage, because Cloud Storage does not apply `If-Match` to a |
| 99 | PUT. |
| 100 | |
| 101 | Some S3-compatible stores do not qualify. MinIO (the community |
| 102 | edition), Backblaze B2, Hetzner Object Storage, and DigitalOcean |
| 103 | Spaces do not implement the required conditional writes. celld is not |
| 104 | correct on such a store: two nodes can then own one cell. A store can |
| 105 | also accept the conditional headers and not apply the condition, and |
| 106 | that store fails late and silently. |
| 107 | |
| 108 | ## The storage test |
| 109 | |
| 110 | No object store publishes the answer, therefore celld asks the store |
| 111 | directly. The command `celld diagnose` sends four conditional writes to |
| 112 | your bucket, and it reports the result: |
| 113 | |
| 114 | ``` |
| 115 | ok bucket conditional write (create, reject-create, update, reject-stale) |
| 116 | ``` |
| 117 | |
| 118 | Two of the four writes must fail. A create over an existing object must |
| 119 | fail, and an update that carries a stale token must fail. A store that |
| 120 | applies either write cannot fence a cell, so celld names the store as |
| 121 | the fault and the command exits with an error. |
| 122 | |
| 123 | Each node runs the same test one time at startup. A node that finds a |
| 124 | broken store stops, because a node that serves on such a store can |
| 125 | share a cell with a second owner. The value `0` in the |
| 126 | `CELLD_STORAGE_PROBE` variable disables the startup test. |
| 127 | |
| 128 | The test writes one small object, and then it deletes the object. The |
| 129 | object uses the `probe/` prefix. celld reserves that prefix together |
| 130 | with `cells/`, `nodes/`, `node-cells/`, `fleet/`, `deploy/`, |
| 131 | `deploy-blobs/`, `wake/` and `telemetry/`, so an application must not |
| 132 | write under any of them. celld deletes objects under some of them. A |
| 133 | process that stops during the test does not delete the object. The |
| 134 | object is small, and celld never reads it. |
| 135 | |
| 136 | The test writes, so a credential that cannot write fails it. An |
| 137 | operator who diagnoses with a read-only credential runs |
| 138 | `celld diagnose --read-only`, and celld then skips the test. |
| 139 | |
| 140 | celld does not require a ranged read today, so a store that ignores the |
| 141 | `Range` header can still run a fleet. A later compaction level can hold |
| 142 | large snapshots, and such a level can need a ranged read again. |