Diffinery

homeflare/celldpublic

celld/docs/fencing.md
107 lines · 5.0 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 replication stream uses plain writes, because the epoch in the key
9is the fence. The acknowledgement path re-reads the ownership record,
10so a stale node cannot make a promise that the fleet does not keep.
11
12## The ownership record
13
14Each cell has one ownership record in the bucket. The record names the
15owner node's session and carries a fencing epoch. A node acquires a
16cell with a conditional write: a create when no record exists, and a
17compare-and-swap on the previous record when one does. The bucket
18accepts one such write, so two nodes cannot acquire the same cell.
19
20Every activation advances the epoch. A takeover advances it, and a
21local wake advances it too. Each owner therefore replicates under a
22fresh epoch, and an epoch never has two writers.
23
24## Replication and the epoch prefix
25
26The replicator copies the SQLite data of each cell to the bucket under
27an epoch prefix: `cells/<cell>/ltx/e<epoch>/`. These segment writes
28are plain, unconditional PUTs. That is intentional: the fence is the
29epoch in the key, not a condition on the request. A node that lost its
30ownership can continue to write, but its writes land in a superseded
31prefix. A restore selects the current lineage, so the stale node
32cannot corrupt the data of the new owner, and the data path pays no
33conditional-write cost.
34
35The prefix protects the data. It does not, alone, protect the promise
36to the client. The next two mechanisms close that gap.
37
38## The acknowledgement rule (RPO=0)
39
40A gate holds the response of each write until the replicator proves
41that the write is in the bucket. After the proof, celld reads the
42ownership record one time, and celld acknowledges only if the record
43still names this node at this epoch. A partitioned node can commit
44locally and replicate into its own superseded prefix, but its
45ownership read shows the new owner, so the client never receives an
46acknowledgement for a write that the surviving lineage does not
47contain. The check is a read of the record, not a clock comparison, so
48a paused process or a skewed clock cannot pass it.
49
50## The epoch seal
51
52A restore selects the newest epoch prefix that contains data. A fenced
53node can append to that prefix after the takeover, because the segment
54writes are unconditional. Without a further rule, a later restore
55could read that appended tail: writes that no client saw acknowledged.
56
57The seal closes this hole. The first activation that restores from an
58epoch writes a seal object (`e<epoch>.seal.json`) with a conditional
59create, and the seal fixes the highest transaction that any restore of
60that epoch can read. The conditional create means that the first
61restorer wins, so every later restore reads the same cut. Every
62acknowledged write sits at or below the seal, because the
63acknowledgement required an ownership read, and the takeover preceded
64the seal. The later writes of the fenced node sit above the seal, so
65they never return. If the seal write fails, the activation fails,
66because a restore of an unsealed prefix reopens the hole.
67
68## Self-fencing
69
70A node that cannot reach the bucket cannot renew its lease, and it
71cannot replicate. Such a node must not own cells, so it fences itself:
72it stops the writes and releases its residency. A different node can
73then acquire the cells through the ownership records. The failure of a
74node is a normal input, not a recovery procedure; the [testing
75page](testing.md) shows the kill tests that exercise this path.
76
77## What the bucket must provide
78
79celld 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
88On an S3-compatible bucket, celld sends the `If-None-Match: *` and
89`If-Match` headers, and the condition compares the etag. Amazon S3,
90Cloudflare R2, and Tigris document these operations. celld's release
91tests run against Cloudflare R2, and the AWS S3 path uses the same
92client and the same headers.
93
94A `gs://` bucket selects Google Cloud Storage. celld then uses the
95Cloud Storage XML API with the `x-goog-if-generation-match`
96precondition and OAuth credentials, and the condition compares the
97object generation. celld does not send the S3 request dialect to
98Cloud Storage, because Cloud Storage does not apply `If-Match` to a
99PUT.
100
101Some S3-compatible stores do not qualify. MinIO (the community
102edition), Backblaze B2, Hetzner Object Storage, and DigitalOcean
103Spaces do not implement the required conditional writes. celld is not
104correct on such a store: two nodes can then own one cell. A store can
105also accept the conditional headers and not apply the condition, and
106that store fails late and silently, so test a store before you trust
107a fleet to it.