Diffinery

homeflare/celldpublic

celld/docs/testing.md
191 lines · 10.3 KBRaw History
1# Testing
2
3celld makes three important promises:
4
5- An acknowledged write is durable.
6- A cell has one writer at a time. It never has two.
7- Code written for Cloudflare Workers and Durable Objects operates the
8 same on celld.
9
10We try to break each promise at the layer where a failure shows most
11clearly. We test the API contract with differential execution against the
12Cloudflare runtime. We check the coordination protocol against an
13exhaustively model-checked specification, and we test it with
14deterministic simulation. We test the full system with fault injection
15on live fleets.
16
17## Conformance: two runtimes, one output
18
19We test the compatibility promise differentially. We run each Workers and
20Durable Objects program twice: once on workerd, the runtime binary that
21Cloudflare operates in production, and once on celld, on identical bytes.
22The two outputs must be equal. This shows two facts at once: the program
23is real Cloudflare code, because workerd accepts it, and celld obeys the
24contract, because the outputs are equal. A test cannot agree with our own
25runtime by accident.
26
27The corpus only grows. When celld gets a new API surface, we add fixtures
28for that surface, and the fixtures must give equal output on the two
29engines. We also port test suites from workerd itself: the Durable
30Objects contract, the web-platform globals, and the upstream Web Platform
31Tests. Before a release, we also replay scenarios through the full
32`celld` binary in each deployment mode: storage, SQL, alarms, streams,
33WebSockets, and lifecycle.
34
35## Specification: exhaustive at small size
36
37The coordination protocol is also specified in TLA+. Heyang Zhou wrote
38the specifications against celld v0.1.0, and his model checking found
39four bugs and a split-brain that lost an acknowledged write. All are
40fixed. None had surfaced in our own review or testing.
41
42Where simulation samples schedules, the checker enumerates them: at a
43small configuration it visits every reachable state. The model grants
44the implementation a linearizable object store and perfect shared
45clocks, so a violation it finds needs no clock skew and no storage
46anomaly to occur. The invariants are the first two promises at the top
47of this page: one writer for each epoch, and no acknowledged write
48lost. The fencing argument itself — that a stale owner's late writes
49cannot cost an acknowledged write, because the epoch in the key keeps
50its lineage apart — is checked, not asserted.
51
52Every configuration carries a pinned expected verdict, and most of the
53verdicts are failures: each failing configuration models a bug the
54protocol once had, or a deliberately broken checker, and the model
55must produce the counterexample. A configuration that stops failing
56has lost its tooth. One tooth had already fallen out when the
57specifications arrived; we repaired it.
58
59Some specifications model a proposed protocol before it is built. When
60the fence on write acknowledgments was redesigned, the design-stage
61check produced an eight-state counterexample against the version it
62replaced: a dormant cell resumes at its old epoch while its release is
63in flight, acknowledges a write, and the takeover that follows
64restores without it. The fix shipped; the counterexample stays as a
65pinned failure.
66
67The checker has also removed code: celld once sealed a cell's durable
68history at restore, and the verdicts showed the seal defended only the
69return of a write that was never acknowledged — an outcome celld does
70not promise to prevent — while its permanent cut could turn a
71recoverable ordering slip into a permanent loss. The seal is gone.
72
73The specifications are a hand-synced snapshot, deliberately not in
74continuous integration: a silently stale gate is worse than none. In
75practice their updates have landed in the same changes that moved the
76protocol, and a delta ledger records what the model does not yet
77describe, including places where the model is weaker than the code
78rather than wrong. Simulation remains the per-commit ratchet; the
79model is its exhaustive small-configuration complement.
80
81## Simulation: the protocol under adversarial schedules
82
83The dangerous bugs live in the coordination: a crash during an ownership
84handoff, a lease renewal that races a takeover, an alarm that fires
85against a partially restored cell. These windows are nanoseconds wide and
86open rarely, so a test cannot wait for them.
87
88Thus the coordination protocol is a
89[pure decision core](https://github.com/denoland/celld/tree/main/crates/logic)
90with no I/O of its own: the clock, the randomness, and the object store
91are interfaces, and a simulator drives the core. The simulated store
92injects latency, compare-and-swap races, and lost responses; the clocks
93drift apart; a node can crash at each await point. Scripted adversaries
94play the cells: a handler that never returns, a write stream that stops
95halfway. V8 stays out of the simulation, because V8 is not deterministic.
96
97A seeded scheduler drives each run, so a failure is not a fluke: the seed
98replays it exactly, every time, and we keep the seed until the bug is
99dead. We examine each property for safety (two writers in one epoch, a
100lost acknowledged write, an expired lease that comes back) and for
101liveness (each armed alarm fires, and ownership settles on one node after
102a crash). A property must survive tens of thousands of seeds, and the
103core protocols have run through millions of different schedules.
104
105Simulation has a known failure mode: the checker that cannot fail. Thus
106we also test the checkers: we run deliberately broken variants of the
107protocol against the properties, and the properties must find the damage.
108A suite that stays green against a broken protocol is a broken suite.
109
110## Live fleets: what simulation cannot see
111
112Simulation cannot see the real S3 tail latency, the real kernel and
113filesystem behavior, or V8 under memory pressure. The third layer is
114therefore a permanent fleet lab: standard VMs from standard providers,
115and a real bucket. The workloads rotate: chat rooms under many WebSocket
116connections, working sets that shift across tens of thousands of cells
117(each cell has a unique checksum), deployment cutovers under load, and
118runs that fill the nodes to the memory limit. The lab qualifies each
119release, and between releases it pushes the density and the fault
120coverage further. Each run makes an archived evidence bundle: the
121configuration, the verification sweeps, the node journals, the kernel
122logs, and the phase timings. We keep a red run with the same care as a
123green run, because a failure that the harness caught is a result, not a
124retry.
125
126We inject the faults between verification passes. A pass fetches each
127cell through different nodes and compares the durable state exactly: the
128status, the body, and the full message ledger. Each run therefore has a
129clean picture before the fault and after it. A cell can be unavailable
130for a short time while its ownership moves, but its committed state must
131stay complete, and a live node must serve that state again.
132
133The scenarios attack every seam that we know. We stop a node with
134`SIGKILL` in the middle of a write stream and delete its local database,
135so the recovery can only come from the bucket: every acknowledged write
136comes back, because the output gate held each response until the write
137was durable. We freeze an owner node, write to its cells through other
138nodes, and unfreeze it: the node sees that its lease moved and refuses to
139serve the old state, and each write from the other nodes lands exactly
140one time. One epoch has at most one writer. We cut a node off from the
141bucket, and it fences itself, because a node that cannot replicate must
142not own cells. We throttle the bucket, so it answers each request with a
143429: the engine slows to the write rate of the store and does not amplify
144the throttle, because a node that knows its replicated position does not
145ask a slow store for extra listings. We stop a full host at the provider
146level in the middle of a workload: its cells move to the other nodes, and
147when the host returns, it joins again with no duplicate residency. Across
148every run of every scenario, no acknowledged write was lost and no
149committed state was damaged; the verification sweeps show zero body
150faults, zero status faults, and zero lost messages.
151
152## A few numbers we trust
153
154Each number includes the condition of its measurement. A number without
155its conditions has no value.
156
157- **The epoch fence holds under contention.** Five hundred claimants
158 tried at the same time to own the same cells: 5,500 attempts, one
159 writer for each epoch, zero violations.
160- **A warm resident request is local.** A request to a resident cell does
161 zero bucket operations and returns in p50 ~1.1 ms and p99 ~7 ms (a
162 fixed-host measurement). Only a cold activation touches object storage.
163- **A durable write costs one bucket round trip.** The response waits
164 until the write is in the bucket; that is the meaning of RPO=0, and the
165 storage round trip is therefore the minimum latency for one write.
166 Concurrent writes to one cell join one shared upload, so the throughput
167 of one cell is not one round trip for each write.
168- **A restore is normal work.** Placement handles the restore of an
169 inactive cell as ordinary work, and not as an emergency procedure. The
170 measured restore times come from the retired external replicator, so this
171 page gives no number until a fleet run measures `celld-ltx`.
172- **Ten small nodes held real scale.** A fleet of ten nodes, each with 4
173 vCPU and 8 GB, held 10,000 resident cells and 20,000 concurrent
174 WebSocket connections. We stopped two of the ten nodes, and the data of
175 every cell was available again on another node in ~11 s at the tail
176 (with reserve headroom).
177
178## The failure edges we intend to find
179
180We must also know where a policy stops, so we record the edges instead of
181tuning them away. The clearest edge is the reserve headroom: a fleet that
182is full to its resident limit has no space for the cells of a lost node,
183so a failure of more than one node at the limit degrades the service,
184and a fleet with headroom does not. We measure both sides of that line,
185the good restoration with reserve and the red case without, because this
186measurement is part of the work.
187
188If you find an edge that we did not find, tell us: a schedule that breaks
189a promise, a fault that we did not inject, a number that you cannot
190reproduce. That is exactly the bug report that we want:
191[github.com/denoland/celld/issues](https://github.com/denoland/celld/issues).