Formal specifications (TLA+)
This directory holds TLA+ models of the sw-block protocols where a bug would lose or corrupt data: volume and snapshot lifecycle, epoch fencing and takeover, local WAL crash recovery, the cut (HEAD publish) pipeline, the control-plane authority store, RF=2 failover, and GC timing.
The models check designs, not the Go code. Each one is small, each abstracts a lot, and each is mapped to the code by hand (see Limits). They have already paid for themselves: several of the counterexamples below were turned into fixes (#152, #156, #158, #159).
Code references are path:line at main 5c7c1a7f unless a row says
otherwise. Comments inside the .tla files cite lines at the commit named in
each file's header; this README is the current map.
TLA+ and TLC in one paragraph
TLA+ describes a system as a
state machine: an initial state (Init) and a relation (Next) that says
which steps may follow any state. Concurrency is modelled as interleaving, so
every order of remote calls, crashes, lost replies and timeouts that the spec
allows is a possible behaviour. TLC is the explicit-state model checker:
for a configuration (cfg/*.cfg) that fixes small values for the constants
(2 volumes, 4 writes, ...), it enumerates every reachable state and checks
the invariants in each. A pass means "no violation exists within these
bounds"; a violation comes with the shortest trace that reaches it. SANY
is the parser and semantic checker that ships in the same jar.
Running
Tools. Java 11 or newer (the runs below used Java 17 and 21) and
tla2tools.jar v1.7.4 (TLC2 version 2.19):
curl -fLO https://github.com/tlaplus/tlaplus/releases/download/v1.7.4/tla2tools.jar
echo "936a262061c914694dfd669a543be24573c45d5aa0ff20a8b96b23d01e050e88 tla2tools.jar" | sha256sum -c -
Parse only (seconds; useful after an edit):
cd docs/spec
java -cp /path/to/tla2tools.jar tla2sany.SANY WorkspaceTakeover.tla
Model-check one configuration. run.sh picks the spec from the config
name prefix:
cd docs/spec
TLA2TOOLS=/path/to/tla2tools.jar ./run.sh Fix_token
# equivalent raw command:
java -XX:+UseParallelGC -Xmx4g -cp /path/to/tla2tools.jar tlc2.TLC \
-config cfg/Fix_token.cfg -deadlock -workers auto -metadir states/Fix_token \
ChunkLifecycleFixed.tla
-deadlockturns deadlock checking off. Several models have a process that loops forever (GC), and bounded models stop at their bounds.-workers autouses every core; 8 workers is a good default on a laptop.- Memory: most configs fit in
-Xmx4g. The large ones (Fix_P1,Fix_P2_aba,W4_drift_heads, anything_bigor_large) want-Xmx8gor more and a fast disk forstates/. The_largeconfigs ran for tens of minutes on 30 workers. ChunkLifecycle.tlaandChunkLifecycleFixed.tlaare PlusCal. After editing the algorithm, re-translate withjava -cp tla2tools.jar pcal.trans -nocfg ChunkLifecycleFixed.tla.- For a quick random search of a config too large to finish, add
-simulate -depth 400(not exhaustive).
Reading results. Model checking completed. No error has been found. is
a pass for that config. Invariant X is violated prints the trace.
Two kinds of config are expected to violate:
*_red_*,RED_*,Bug_*: a guard is removed (a constant set to the "unsafe" value) or the pre-fix design is used. The violation shows that the guard or fix is load-bearing.*_reach_*,W_*invariants: reachability witnesses ("this good state is never reached"). Their violation shows that the model can actually do the thing (delete, take over, publish), so a pass elsewhere is not vacuous.
Files
| Spec | Configs | Kind |
|---|---|---|
ChunkLifecycleFixed.tla |
cfg/Fix_* |
current design (PlusCal) |
ChunkLifecycle.tla |
cfg/Bug_* |
historical: the pre-fix design of #152, kept for its counterexamples |
WorkspaceTakeover.tla |
cfg/W4_*, cfg/RED_* |
|
WALRecovery.tla |
cfg/S3_wal_*, cfg/S3v*, cfg/S3r_*, cfg/S3m_*, cfg/S3f_* |
|
SmartWALRecovery.tla |
cfg/S3_sw_*, cfg/S3_sw2_* |
|
WALReplica.tla |
cfg/S3_rep_* |
|
CutCapture.tla |
cfg/S4_* |
|
CutPipeline.tla |
cfg/S4b_* |
|
ControlPlane.tla |
cfg/S5_* |
|
ReplicationFailover.tla |
cfg/S6_* |
design-level |
RetentionGCTiming.tla |
cfg/S9_* |
|
RetentionGCMulti.tla |
cfg/S9b_* |
Each .cfg names its spec on its first line.
Specs at a glance
State counts are distinct states for exhaustive passes. "N-step trace" is the length of TLC's shortest counterexample.
| # | Spec | What it models | Key invariants | Results | Code (at 5c7c1a7f) |
|---|---|---|---|---|---|
| S1 | ChunkLifecycleFixed (+ historical ChunkLifecycle) |
Chunk HEAD, capture intents, snapshots (name record + attempt path), fork claims, tombstones, GC mark+sweep, and volume delete with a blockmaster deletion reservation. Actors: host, 0-2 snapshot callers, a fork, explicit DeleteSnapshot callers, 1-3 volume deleters with reruns, CreateVolume, GC. Filer semantics: conditional write on HEAD gen, exclusive create, conditional update/delete, uncancellable late create. | GCSafe, SnapOnlyExplicitDelete, CommitClean, NoPublishAfterCommit, AckedChunksSurvive, NoIdReuse, GiveBackReleases, temporal HeadMonotone |
Fixed design, all pass: Fix_P1 51,375,412; Fix_P2_aba 46,245,848; Fix_mix_small 29,194,420; Fix_token 41,256; Fix_create 100. Fix_mix no violation in 138M states (20-min limit, incomplete). All Fix_reach_* reached. Historical (pre-fix) counterexamples: Bug_P1_mark (18-step), Bug_P1_giveback (23), Bug_P1_data (25: GC reclaims an acked snapshot's chunk), Bug_P2_aba (22), Bug_token (16), Bug_create (7). |
core/frontend/chunkstore/volume_delete.go:106 DeleteVolume (incarnation loop :150), :269 markHeadDeleting, :400 snapshotsBlock, :314 revertHeadDeleting, :347 commitHeadPurging; catalog.go:824 CreateSnapshot (gate :863), :943 claimName, :771 createResolved, :1058 DeleteSnapshot, :1157 Fork, :1337 claimForkParent, :709 AbortSnapshot, :1438 AbortFork; retention.go:448 markDeleting, :570 liveClaims; gc.go:108 Collector.run; chunk_remote.go:404 CASHead refuses delete states; core/host/master/services.go:53 CreateVolume (holds workspaceMu :64), :132 MarkVolumeDeleting; core/lifecycle/deletion_reservations.go:111 Admit; cmd/sw-block/chunk_volume_delete.go:32 --request-id |
| S2 | WorkspaceTakeover |
A workspace volume moving from host A (epoch 1) to host B by a clean move (detach, revoke, ACK, assign) or a forced move (lease expiry). Blockmaster clock, host clock lag up to Drift, lossy and reordered messages, master restart, each host's cut as one remote call per step, takeover CAS, GC intent expiry. Code pinned at 5c293821. |
NoDualServing, NoOldCasAfterTakeover, NoLateChunkReachable, GCSafe, NoLiveIntentExpired, CleanNoLoss, ForcedLossBounded, SeedMatchesTakeover, temporal HeadMonotone |
Pass: W4_clean 6,048,496; W4_forced 22,619,644; W4_drift_heads 56,242,345; W4_clean_large 590,137,925 (depth 70). W4_forced_large: no violation in 910,907,693 states (time limit, incomplete). RED: RED_early_assign NoDualServing (12-step); RED_gc_ageonly GCSafe (12); RED_epoch NoOldCasAfterTakeover (22-24); RED_lsn HeadMonotone (24). RED_epoch_storeonly/_hostonly pass (438,406 / 827,500): either epoch check alone suffices. RED_lsn_storeonly passes (28,389,024). Findings: W4_drift NoDualServing (13-step, fixed by #158, see below); W4_forced_exact ForcedLossExact (16-step, benign, open). |
core/frontend/chunkstore/chunk_remote.go:390 CASHead (epoch guard :391, gen/epoch/LSN guard :409, conditional update :420); core/host/volume/workspace_lease.go:57 permits, :70 Refresh (anchor :73, accept :117); core/authority/workspace.go:95 RenewWorkspaceLease, :222 BeginWorkspaceRevocation, :237 AckWorkspaceRevocation, :309 prepareWorkspaceMoveLocked (forced-move margin :316-320); core/host/master/workspace_service.go:109 MoveWorkspace (HEAD read :116); core/authority/authority.go:251 reloadFromStore (deadline reset :261); core/frontend/durable/provider_chunk.go:289 PrepareRestore, :309 ClaimChunkHead; core/storage/chunk_restore.go:14; core/storage/chunk_layer.go:680 captureLocked (admit :695, lsn_behind :800, PutIntent :853, re-admit :911, supersededBy :916, CAS :926, settle :972); core/frontend/chunkstore/gc.go:221 intent-expiry epoch rule |
| S3 | WALRecovery, SmartWALRecovery, WALReplica |
Local WAL crash recovery. walstore: a ring of K pages, 1-2 page records, page cache written back in any order, Sync, flusher checkpoint, crash at any point with optionally one corrupted record page; recovery is a pure function of the durable image, with a selectable Rule. smartwal: in-place extent write plus a metadata ring slot, written back in any order, torn slots. Replica: RF=2 classification and promotion eligibility, BASE jump. |
I1 every synced write is recovered (or superseded) or recovery fails closed; I2 a crash without corruption never reports corruption; ReplayLastWriter; I0Prefix; I1Frontier; NoForgedReplay; I3 the promoted node has every acked write |
walstore, current main (rule rootLww, reuse bounded by the persisted tail): S3r_rootLww_I1_crashonly/_I2/_replay pass (849,003); with an in-memory rebuild jump S3r_rebuild_rootLww_* pass (1,460,772). Pre-fix code: S3_wal_current_I1_crashonly I1 violated (synced write lost after a plain crash, #156); S3v_code_lww_I1_crashonly I1 violated (LWW replay alone is not enough, #159). Open: one corrupted synced record S3v1_root_lww_I1 violated; Sync-persisted watermark S3m_* pass (1,320,984 / 3,955,400); S3f_* NoForgedReplay violated (confirmed on main, see Open items). smartwal, current: S3_sw_current_I2 violated (false wal_corrupt), _I1, _I1frontier violated; proposal S3_sw2_ordered_marker_repair_I2 pass (65,401), _I1slot pass (345,084). Replica: S3_rep_c1_I2 pass (29,196), S3_rep_c1_g2_I2 pass (12,712), S3_rep_c1_g1_I3 pass (74,527); RED S3_rep_c2_I2, S3_rep_c1_g2jump_I2, S3_rep_nogate_I3 violated. |
core/storage/recovery.go:43 recoverWAL (head==tail scan from its position :63-73, defensive scan :74-90, a range stops at its first bad entry :141, :154, hole warning :437), LWW replay :193 via core/storage/dirty_map.go:56 putAtIfNewer; core/storage/wal_writer.go:41 durableTail, :70 spaceUsed, :443 tailPersisted; core/storage/walstore.go:390 checkpoint release, :933 Sync, :1065 AdvanceFrontier, :1167 ApplyEntry; core/storage/smartwal/store.go:297 writeAt (extent :304, then ring :313), :345 Sync; smartwal/recovery.go:47 runRecoveryCounting (fail closed :99-100); smartwal/ring.go:45 appendAt, :69 scanSlots; core/replication/volume.go:680 RebaseShipLSN |
| S4 | CutCapture, CutPipeline |
Which chunks the next cut must upload: the dirty bitmap vs a per-chunk last-write-LSN map, across RF=1 restart, crash mid-cut and RF=2 failover; dirty state persisted before a checkpoint passes WAL entries. Then the pipeline: precopy tagged with a per-chunk mutation generation, two-level manifest segments, root CAS, and reconciling after a restart without a bound bitmap. |
HeadImage (HEAD is the exact image of the lineage at HEAD.lsn), HeadLeDurable, SegConsistent |
Pass: S4_rf1_bitmap 47,670, S4_rf1_lsnmap 46,934, S4_rf2_bitmapHash 7,166,376, S4_rf2_lsnmap 8,226,004, S4_rf2_lsnmap_base 901,390; S4b_design 69,645, S4b_design_big 1,515,837. RED: S4_rf2_bitmap (failover without hashMode, 7-step), S4_rf2_lsnmap_localCut (6), S4_rf1_*_noPersist (7), S4_rf1_bitmap_clearEarly (8); S4b_red_noGen, S4b_red_cutWhileRec HeadImage; S4b_red_segReuse SegConsistent. |
core/storage/chunk_layer.go:356 BeforeCheckpointAdvance, :299 OnExtentWrite, :95 hashMode (set on attach :184-230), :111 chunkMut, :324 noteMutation, :762 and :1435 staged upload reused only if its generation is unchanged, :702-704 cut refused while reconciling, :877-881 PrepareManifest, :1542 finishCut; core/frontend/chunkstore/manifest.go:101 segment reused only if its chunk refs are unchanged |
| S5 | ControlPlane |
Blockmaster authority store: one volume record {epoch, holder, move journal}, two retried move requests, durable Puts that succeed, fail, or land with the reply lost, and restarts. |
UniqueEpoch (issued epochs strictly increase), Idempotent (one epoch per request id), JournalComplete |
S5_code pass (298). RED: S5_red_noUncertain UniqueEpoch violated (5-step: a lost-reply Put, then a reservation from stale memory puts the old assignment back). S5_red_noReserve passes (the reservation step is not needed for these invariants). |
core/authority/authority.go:436 applyWithWorkspaceMove (uncertain refusal :442-444, Put :564, mark uncertain :568); core/authority/workspace.go:309 prepareWorkspaceMoveLocked (expected line :313, resume a reserved entry :324-333, reserve max+1 :344-355), journal replay :192-219, persist and mark uncertain :276-288 |
| S6 | ReplicationFailover |
RF=2: P ships to R, R acks what is durable, the client is acked when both are durable; promotion of R at a new epoch with an evidence gate; R refuses old-epoch shipping; cuts and snapshots cover only the replicated-durable frontier; an in-flight snapshot create from the old primary may still land after the failover. Design-level. | AckedSurvive, HeadOnLineage, SnapOnLineage, NoStaleAck |
S6_design pass (52,897), S6_design_big (598,353). RED: S6_red_localCut HeadOnLineage; S6_red_noFence AckedSurvive. S6_red_noEvidence passes: with synchronous RF=2 acks the gate is redundant (it matters for quorum or async shipping, not modelled). |
core/transport/replica.go:362 replica rejects a stale-lineage ship entry; core/replication/volume.go:680 RebaseShipLSN; takeover CAS as in S2 |
| S9 | RetentionGCTiming, RetentionGCMulti |
GC timing with an explicit clock: a copy operation with deadline D, marks of duration up to MD, deletion only after two marks at least Grace apart; then marks across two volumes with a fork and a snapshot delete. | NoLiveDelete |
Single op: S9_rule_G4 (G > D + MD) pass (10,798; _big 43,780); G = D passes (S9_red_G2_noguard, 11,953). RED: S9_red_G1_noguard, S9_red_G1_D3 (G < D) NoLiveDelete violated. Two volumes: S9b_G1_MD2, _G2_MD2, _G2_MD3, _G3_MD1 pass (5,743-9,079). |
core/frontend/chunkstore/gc.go:89 grace > operation deadline, :280 mark-duration margin, :306-318 two-mark rule, :214-221 intent expiry; catalog.go:896-905 intent deadline re-checked before publish; gc_copy.go:143, :177 a deleting attempt stays a root until grace |
Limits
- Design, not code. Each action is a hand-written abstraction of one
remote call or one local decision, mapped to code by the table above. A
pass says the design is safe under the modelled assumptions; it does not
say the Go code implements that design. Line mappings were re-checked
against
5c7c1a7f, but they rot as code moves. - Small scopes. 2 volumes, 2-3 chunks, 2 snapshot names, 3-6 writes or LSNs, a 4-page WAL, 1-2 restarts, clocks up to 7-16 ticks. A pass is exhaustive for that config only. Some large configs stopped at a time limit; they are marked incomplete above.
- Abstractions. Data is "the LSN of the write it holds" (no bytes); a CRC is data identity; one corrupted record at most; GC mark+sweep is atomic in S1 and S2 (S9 models its timing); time in S1 is replaced by a "keys read by an in-flight operation are roots" assumption; S1 has no epoch (S2 covers it) and no crash (so fsync-durability bugs are out of scope); some reads are merged with the conditional write they guard, which can only hide interleavings, never invent them.
- Not modelled. RF=3 quorum, catch-up and rebuild, wall-clock jumps,
master HA, an unknown-outcome HEAD CAS (
resolvePending), the S2 forced move's skew margin added in #158, liveness under fairness. - Model errors happen. Three early drafts of these specs produced spurious violations that were traced to the model, not the code. Every violation listed here was checked against the code.
Findings that led to code fixes
| PR | Finding | Spec / configs |
|---|---|---|
| #152 | Volume delete (as first proposed): (1) the snapshot cascade's back-out could mark another caller's published snapshot deleting while the delete was still provisional, so a later GC could reclaim an acknowledged snapshot's only chunk; (2) a claim check and commit not bound to one deletion incarnation allowed an ABA across three concurrent deleters; (3) a rerun with a fresh token left the first reservation held; (4) CreateVolume released its lock between the reservation check and the create. Fixed by refusing while any snapshot name exists, binding check, commit and revert to one incarnation, using --request-id as the token, and keeping the lock. |
S1 Bug_P1_*, Bug_P2_aba, Bug_token, Bug_create (violated); Fix_* (pass) |
| #156 | walstore recovery replayed in physical scan order. After a wrap, a newer record could be scanned before an older one for the same LBA and lose to it, so an acknowledged write was lost after a plain crash. Fixed by last-writer-wins replay by LSN. | S3 S3_wal_current_I1_crashonly |
| #159 | With LWW replay alone, the writer could reuse WAL space released only in memory; a torn unsynced record written there could stop the scan before a synced record that the on-disk superblock still retained. Fixed by reusing only space the persisted tail released, and by scanning an empty-superblock window from its position. | S3 S3v_code_lww_I1_crashonly (violated); S3r_rootLww_*, S3r_rebuild_rootLww_* (pass) |
| #158 | The workspace lease had no clock-skew margin: with a host clock one tick slow, the old and new host could both serve after a forced move (HEAD stayed safe because the old host's CAS fails on the epoch). Fixed by a skew margin: a forced move waits for lease expiry plus the margin (core/authority/workspace.go:316-320), and the lease validity reported to the host excludes half of it (core/host/master/workspace_service.go:46-53). |
S2 W4_drift (violated against the pre-fix design; the margin is not yet in the model) |
The S2 epoch rule for GC intent expiry (gc.go:221), the S5 "uncertain
after a failed Put" guard, and the S4 generation and reconciling guards were
already in the code; their RED variants confirm they are load-bearing and
are good candidates for regression tests.
Open items
These are model-level results on the current design. They are not confirmed bugs in production paths unless stated.
- Corrupted synced WAL records need a format change (walstore). If a
synced record's bytes are corrupted, the current recovery stops that range
at the bad record and continues; data after it in the range is dropped and
recovery reports success (a hole warning is logged since #155). An
in-record durable watermark alone is not enough (a corrupt last synced
record leaves no valid witness). The configs that pass (
S3m_*) need Sync to persist the watermark and any frontier jump out of band (for example in the superblock) before acknowledging, plus recovery that decodes every record position and replays by LSN. - Forged replay (confirmed, open). Recovery accepts any well-formed
record. A guest payload can contain bytes that decode as a valid WAL
record (CRC32 is not keyed). If a newer, shorter record overwrites only the
start of an old multi-block record, the scan continues into the old
payload and replays the forged record. Every rule modelled, including the
current one, violates
NoForgedReplay(S3f_*).- Reproduced on main. A 3-block batch write carries an encoded WAL entry (LSN 999, LBA 7) in its guest data. After a newer 1-block record wraps over the batch's start, recovery decodes the embedded entry: LBA 7 reads the forged data, and the frontier jumps to 999. This also happens after a clean close and reopen.
- Effect. It stays inside the guest's own volume: a write at a chosen LBA, or a bogus LSN that moves the frontier.
- Planned fix. An interim fix persists the WAL head on Close and adds a clean-shutdown flag. The full fix comes with the format change: bind each record to its WAL incarnation and lap, for example with an epoch or nonce in the header.
- SmartWAL.
- The extent write and the ring slot have no ordering barrier
(
smartwal/store.go:304,:313), and recovery fails closed on any CRC mismatch (smartwal/recovery.go:99). After a plain crash an unsynced write can therefore make the volume refuse to start withwal_corrupt(S3_sw_current_I2): an availability problem, not data loss. - There is no per-block CRC once a slot has been recycled, so extent
corruption of such an LBA is not detected, and corrupting the only record
of a synced write lowers the recovered frontier silently
(
S3_sw_current_I1,_I1frontier; a documented limitation). - A proposal passes
I2and slot-corruptionI1: make the record durable before the data write, persist gap markers for frontier jumps, and have recovery durably settle every torn tail it accepts (S3_sw2_ordered_marker_repair_*). Its frontier still needs an out-of-band durable watermark (item 1). - The positional torn-slot rule of an earlier revision of #155 (089d2d9)
could report
wal_corruptafter a plain crash (S3_sw2_pr155o_I2); the merged #155 only counts torn slots, so this does not apply to main.
- The extent write and the ring slot have no ordering barrier
(
- Design notes (no safety impact). A forced move's journal records the
HEAD read before the publisher lock (
workspace_service.go:116), so it can over-report the loss window (W4_forced_exact). A refused snapshot attempt can leave an unpublished name record that blocks later volume deletes untilDeleteSnapshotsettles it (Fix_reach_W_NoHiddenNameBlock;catalog.go:861).
Parse check
All 12 specs parse and pass SANY's semantic check with tla2tools.jar v1.7.4
(java -cp tla2tools.jar tla2sany.SANY <file>), checked on 2026-11-16:
ChunkLifecycle, ChunkLifecycleFixed, ControlPlane, CutCapture,
CutPipeline, ReplicationFailover, RetentionGCMulti, RetentionGCTiming,
SmartWALRecovery, WALRecovery, WALReplica, WorkspaceTakeover. The state counts above come from earlier
TLC runs of these specs; comment-only edits were made since.