Diffinery

homeflare/celldpublic

celld/docs/fencing.md
231 lines · 11.4 KBRaw History
1# Ownership and fencing
2
3celld makes two claims. One node owns a cell at a time, and a write is
4durable before celld acknowledges it. This page gives the mechanism
5behind each claim, so you can check the argument against the code.
6
7A short version first. The ownership records use conditional writes.
8The bucket replication path uses plain writes, because the epoch in the key
9is the fence. After a bucket proof, celld re-reads the ownership record and
10acknowledges the write only if the record still names this node. Every
11follower must fsync the write for a fleet proof. During a cold activation,
12celld recovers the prior node log before it reads the bucket. These paths
13stop a stale node from acknowledging a write that the fleet does not store.
14
15## The ownership record
16
17Each cell has one ownership record in the bucket. The record names the
18owner node's session and carries a fencing epoch. A node acquires a
19cell with a conditional write: a create when no record exists, and a
20compare-and-swap on the previous record when one does. The bucket
21accepts one such write, so two nodes cannot acquire the same cell.
22
23Every activation advances the epoch. A takeover advances it, and a
24local wake advances it too. Each owner therefore replicates under a
25fresh epoch, and an epoch never has two writers.
26
27## Replication and the epoch prefix
28
29The replicator copies the SQLite data of each cell to the bucket under an
30epoch prefix: `cells/<cell>/ltx/e<epoch>/`. These segment writes are plain,
31unconditional PUTs. The tiering path can first combine segments from many
32cells into a node bundle, and it later drains each segment into its per-cell
33prefix.
34
35The epoch in the key is the fence, so the data path needs no conditional
36write. A node that lost ownership can continue to write, but its writes land
37in a superseded prefix. A restore selects the current lineage, so the stale
38node cannot corrupt the data of the new owner.
39
40The prefix protects the current owner from stale writes. The acknowledgement
41rule and the takeover recovery gate protect the durability promise.
42
43## The acknowledgement rule (RPO=0)
44
45A gate holds each write response until a durability proof covers the write.
46After a bucket proof, celld reads the ownership record one time. celld
47acknowledges only if the record still names this node at this epoch. A
48partitioned node can commit locally and replicate into its superseded prefix.
49The ownership read then shows the new owner, so celld does not acknowledge the
50write. The check reads the record instead of comparing a clock, so a paused
51process or a skewed clock cannot pass it.
52
53A fleet proof does not require this read. Every member of the follower
54ensemble must fsync the write. A takeover seals the prior node-log session
55before it restores, so the stale owner cannot complete another fleet proof.
56
57## The takeover recovery gate
58
59The default fleet mode can acknowledge a write after all members of a node
60log ensemble store the write. The bucket upload can complete later. Each
61process session therefore creates a conditional node log record before its
62first fleet-durable acknowledgement.
63
64A cold activation checks the log records of the prior owner before it reads
65the bucket. An absent record proves that the session never acknowledged past
66the bucket, and a sealed record proves that recovery completed. An open or
67recovering record makes the activation run recovery.
68
69Recovery uses compare-and-swap writes to fence the record. It seals the
70reachable followers, gathers their retained segments and bundles, and uploads
71the missing segments into the per-cell prefixes. Recovery then marks the log
72record as sealed with another compare-and-swap write. The activation cannot
73restore until this sequence completes.
74
75## Full-prefix restore
76
77celld no longer writes an epoch seal object. A restore selects the newest
78epoch prefix that contains LTX data, and it reads the full contiguous chain
79from transaction zero. A legacy `e<epoch>.seal.json` object does not limit
80this chain.
81
82A fenced node can append an unacknowledged tail to an older prefix. A later
83restore can expose that tail if it selects the prefix. This result does not
84violate the acknowledgement contract, because a failed or absent
85acknowledgement does not prove that the write is absent.
86
87A node-log recovery or a bundle drain can add an acknowledged follower tail
88after an earlier restore. A fixed restore cut stops at the transaction of that
89earlier restore, therefore it hides the recovered tail and loses acknowledged
90data. celld does not use a fixed restore cut. The full-prefix rule reads the
91complete chain, so a later restore returns the recovered tail and the
92acknowledgement contract holds.
93
94## Self-fencing
95
96Each node holds a node lease in the bucket. The lease carries an expiry, and
97the node renews the lease after one third of the lease lifetime. The
98`CELLD_TTL_MS` variable sets that lifetime, and the default is 10000 ms. A
99renewal that does not reach the bucket does not fence the node, because the
100node retries while the published expiry has not passed.
101
102A node that cannot reach the bucket cannot renew its lease, and it cannot
103replicate. Such a node must not own cells, so it fences itself when its
104published expiry passes. It stops each active cell, and it fails every
105request that it has not completed. A node whose lease record another writer
106replaced or removed fences at once, because that record proves the authority
107moved.
108
109The fence writes nothing to the bucket. Each peer already reads the lease
110record as dead or as replaced, therefore a peer can acquire the cells through
111the ownership records. The handover comes from the lease record itself, not
112from a release that the fenced node performs.
113
114A request is safe even before the fence runs. celld compares the current time
115against the published expiry each time it routes a request, so a node with a
116lapsed lease refuses the request. The fence stops the process, and the
117dispatch check keeps one owner per cell.
118
119A fenced node logs a line that starts with `SELF-FENCE:`, and then the process
120stops with the exit code 3. The line names the cause, because celld reports
121other internal failures with the same prefix and the same exit code. The
122fenced state is terminal, so the process cannot take a new lease, and only a
123restart returns the node to the fleet. A restart uses the same cold activation
124path that a peer failure already uses, therefore celld needs no second
125recovery procedure.
126
127You must run celld under a supervisor that restarts the process, such as
128systemd, Docker with a restart policy, or Kubernetes. A node without such a
129supervisor does not return after a fence, and the fleet loses that capacity
130until an operator intervenes. The supervisor must restart the process without
131an attempt limit, and it must wait at least one lease lifetime between
132attempts. A node that cannot acquire a lease at startup retries and does not
133exit, so a repeated fence needs a node that acquires a lease and then loses
134it, and the wait keeps that cycle slow enough to observe.
135
136The failure of a node is a normal input, not a recovery procedure; the
137[testing page](testing.md) shows the kill tests that exercise this path.
138
139## What the bucket must provide
140
141celld needs three properties from the object store:
142
143- A conditional create. The create must fail when the object already
144 exists.
145- A conditional overwrite. The write must fail when the object changed
146 after the read.
147- Read-after-write consistency. A read after a successful write must
148 return that write.
149
150The bucket adapter treats `AlreadyExists` and `Precondition` as clean
151conditional-write rejections. Azure uses `Precondition` for an HTTP 412
152response. The adapter keeps every other error ambiguous. A caller must
153reconcile an ambiguous error because the write can have changed the object.
154
155On an S3-compatible bucket, celld sends the `If-None-Match: *` and
156`If-Match` headers, and the condition compares the etag. Amazon S3,
157Cloudflare R2, and Tigris document these operations. celld's release
158tests run against Cloudflare R2, and the AWS S3 path uses the same
159client and the same headers.
160
161A `gs://` bucket selects Google Cloud Storage. celld then uses the
162Cloud Storage XML API with the `x-goog-if-generation-match`
163precondition and OAuth credentials, and the condition compares the
164object generation. celld does not send the S3 request dialect to
165Cloud Storage, because Cloud Storage does not apply `If-Match` to a
166PUT.
167
168An `az://` bucket selects Azure Blob Storage, where the NAME is the
169container. celld then sends the same `If-None-Match: *` and `If-Match`
170headers, and the condition compares the etag, because Put Blob applies
171both headers. Microsoft documents these operations.
172
173The qualified stores are Amazon S3, Cloudflare R2, Tigris, Google Cloud
174Storage, and Azure Blob Storage.
175
176Azure was qualified on 2026-08-18, against the shipped revision. The
177four-step matrix passes under each credential family celld supports —
178an account key, a VM managed identity, and an AKS workload identity —
179and a node restores a cell from an Azure replica alone after its local
180state is destroyed. An LTX file above the 5 MiB threshold, where celld
181switches to block-blob multipart, uploads and reads back byte-for-byte;
182Azure's own committed block list shows the parts.
183
184What that qualification does not cover: a managed identity on Azure App
185Service or Azure Container Apps (see [limitations](limitations.md)),
186per-blob write rate limits shaping lease cadence under sustained load,
187and multi-node contention. Each run was single-node, and its conditions
188are recorded with it.
189
190Some S3-compatible stores do not qualify. MinIO (the community
191edition), Backblaze B2, Hetzner Object Storage, and DigitalOcean
192Spaces do not implement the required conditional writes. celld is not
193correct on such a store: two nodes can then own one cell. A store can
194also accept the conditional headers and not apply the condition, and
195that store fails late and silently.
196
197## The storage test
198
199No object store publishes the answer, therefore celld asks the store
200directly. The command `celld diagnose` sends four conditional writes to
201your bucket, and it reports the result:
202
203```
204ok bucket conditional write (create, reject-create, update, reject-stale)
205```
206
207Two of the four writes must fail. A create over an existing object must
208fail, and an update that carries a stale token must fail. A store that
209applies either write cannot fence a cell, so celld names the store as
210the fault and the command exits with an error.
211
212Each node runs the same test one time at startup. A node that finds a
213broken store stops, because a node that serves on such a store can
214share a cell with a second owner. The value `0` in the
215`CELLD_STORAGE_PROBE` variable disables the startup test.
216
217The test writes one small object, and then it deletes the object. The
218object uses the `probe/` prefix. celld reserves that prefix together
219with `cells/`, `nodes/`, `node-cells/`, `fleet/`, `deploy/`,
220`deploy-blobs/`, `wake/` and `telemetry/`, so an application must not
221write under any of them. celld deletes objects under some of them. A
222process that stops during the test does not delete the object. The
223object is small, and celld never reads it.
224
225The test writes, so a credential that cannot write fails it. An
226operator who diagnoses with a read-only credential runs
227`celld diagnose --read-only`, and celld then skips the test.
228
229celld does not require a ranged read today, so a store that ignores the
230`Range` header can still run a fleet. A later compaction level can hold
231large snapshots, and such a level can need a ranged read again.