How a run becomes a verdict
Nothing here inspects the program's memory or instruments its code. The program runs as written, and the journal of what it did is the only thing checked. Five steps, each a section below:
- The program names every effect it can perform in its types.
- The world answers those effects, and makes every network and timing decision.
- The journal records each answer and each decision, in order.
- The monitor replays the journal against five safety properties.
- The verdict: admitted, or refused at a named entry.
1 The program
raft.nib is leader election and
log replication for three nodes, in 496 lines. Each node listens on its own
address, dials its peers, and feeds every message and a timer's ticks into one
mailbox; its main loop takes one event at a time. A function's type says which
effects it may perform, so the step that handles one event says it touches the
store, the network, randomness, the state machine and the voting rules, and
nothing else:
func handle :: n: Node, ev: Ev, now: Int -> Node ! Store, Net.Socket, Random.Source, Machine, Rules =raft.nib: the event handler's signature. Everything after ! is an effect.The effects are interfaces. What survives a crash (the current term, the
vote, the log) goes through Store, and every write is a recorded
operation made before the message it justifies is sent:
interface Store where
recover(node: String) -> Saved;
save(node: String, term: Int, voted: String) -> ();
append(node: String, keep: Int, entries: Vec<Entry>) -> ()raft.nib: the persistent state, as an interface.A wire binds each interface to an implementation. Swapping one
binding gives a program with a known bug and leaves the rest of the protocol
alone, so the checks have something to refuse. Forgetful binds a
store whose recover answers nothing, as a node that never synced
its writes would:
wire Main runs Raft.main where
Store = Disk, Machine = Kv, Rules = Strict,
Net.Socket = Net.Sim, Time.Clock = Time.Sim, Random.Source = Random.Sim,
Io.Print = Io.Stdout
wire Forgetful runs Raft.main where
Store = Ram, Machine = Kv, Rules = Strict,
Net.Socket = Net.Sim, Time.Clock = Time.Sim, Random.Source = Random.Sim,
Io.Print = Io.Stdoutraft.nib: two of the four wires. Mutant votes twice in a term; Careless ignores the candidate's log.effector Ram is Store where
unkeyed var logs :: Map<String, Vec<Entry>> = {};
recover(node) = Saved{ term: 0, voted: "", log: #[] }
save(node, term, voted) = ()
append(node, keep, entries) =
logs := Map.set(logs, node, Vec.append(Vec.take(Map.get_or(logs, node, #[]), keep), entries)); ()raft.nib: the store that forgets.2 The world
Net.Sim, Time.Sim and Random.Sim are
simulated implementations of the network, clock and randomness interfaces.
All three nodes are fibres in one process; virtual time moves only when every
fibre is waiting, so three seconds of cluster time take a fraction of a second.
A world file says what the world does:
{ "schedule": "random",
"net": { "delay_ms": [1, 40], "drop_permille": 50, "duplicate_permille": 20 },
"crashes": [ { "node": "10.0.0.1", "at_ms": 700, "restart_ms": 1200 },
{ "node": "10.0.0.2", "at_ms": 1500, "restart_ms": 1900 },
{ "node": "10.0.0.3", "at_ms": 2300, "restart_ms": 2600 } ] }worlds/crash.json: a random schedule, 1 to 40 ms delivery, 5% loss, 2% duplication, and a crash of each node.Every decision the world makes is a choice: when each message arrives (or whether it is lost), which ready fibre runs next, each random draw, each crash. A seed makes them; a world file can also state them outright. A crash kills every fibre of that host where it stands and closes its connections. Effector state survives, because it belongs to the wire, not to a fibre, so it is the program's disk. Restarting is the program's own work: a supervisor outside the node sees it killed and runs it again.
func keep :: me: String, stop: Int -> () ! Store, Net.Socket, Random.Source, Machine, Rules, Time.Clock, Io.Print =
let crashed =
try Switch.run { sw -> let _ = Fiber.await(fork(sw) { -> node(me, stop) }); false }
rescue HandleRefused(_) => true
end;
if crashed && @Time.Clock.now() < stop then keep(me, stop) else ()raft.nib: the supervisor. Its next listen waits until the world brings the host back.3 The journal
Every operation and every choice is a line of JSON, carrying the node that performed it and the answer it got. The hero run is 7,460 lines, 3,648 of them clock reads and sleeps. Messages are sent as JSON, so the checker can read a message's kind and term out of the bytes. Here is the crash, and the two writes the monitor compares in the refused run below (fields trimmed):
{"seq":2079,"kind":"choice","op":"World.crash","argv":["10.0.0.1",700],"node":"","outcome":{"value":null}}
{"seq":566,"kind":"operation","op":"Raft.Store.save","argv":["10.0.0.1",1,"10.0.0.3"],"node":"10.0.0.1:7000","outcome":{"value":null}}
{"seq":3071,"kind":"operation","op":"Raft.Store.save","argv":["10.0.0.1",1,""],"node":"10.0.0.1:7000","outcome":{"value":null}}
The same journal is also a replay: run the program against it, and each
recorded answer and choice is served back in order, so the run happens again
exactly, crashes included, with no world file needed. nib-tla
world turns a journal's choices back into a world file, so a failing
run can be edited and cut down.
4 The monitor
RaftTrace.tla is not a model
of Raft. It rebuilds what the journal shows (who led which term, each node's
log, what was applied and saved) and refuses the journal at the first entry
that breaks one of these:
- Election safety
- At most one node leads a term. A node leads term t when it sends an append of term t.
- Log matching
- Two logs holding an entry of the same term at the
same index agree up to that index, checked on every
Store.append. - Leader completeness
- An entry applied before a node stood for term t is in that node's log whenever it leads t.
- State machine safety
- No two nodes apply different commands at one index.
- Durability
- A node's saved term never goes back, and its vote in a term, once saved, never changes.
Durable(t0, v0, t, v) == t > t0 \/ (t = t0 /\ (v0 = "" \/ v0 = v))
\/ /\ e.op = "Raft.Store.save"
/\ e.argv[1] \in Nodes
/\ Durable(saved[e.argv[1]][1], saved[e.argv[1]][2], e.argv[2], e.argv[3])
/\ saved' = [saved EXCEPT ![e.argv[1]] = <<e.argv[2], e.argv[3]>>]
/\ candidacies' = IF e.argv[3] = e.argv[1]
THEN candidacies \cup {<<e.argv[1], e.argv[2], l>>}
ELSE candidacies
/\ UNCHANGED <<leaders, applied, logs>>RaftTrace.tla: Durability, and the step that checks it on each save.Any operation the monitor does not name is refused too, so a new effect in
the program cannot pass unexamined. nib-tla checks this with a
probe: it appends an operation nobody mapped and requires the monitor to
refuse it.
5 The verdict
The forgetful store, seed 1 of the same world. Node 10.0.0.1 voted for 10.0.0.3 in term 1 at 201 ms. It crashed, came back with nothing, and at 1213 ms heard of term 1 again and saved it with no vote: free to vote a second time in a term it has already voted in. The monitor refuses the run there, at entry 3071, before a second leader exists to show for it.
Dots mark the two saves: the vote, and the save that erased
it. The program's own end-of-run check, which compares what every node
applied, printed safe: applied 6 6 6.
That last line is the point. Of the 21 refused runs in the
table below, 15 printed safe: the nodes agreed at
the end, and the bug was invisible to "it ran and agreed with itself". On
every commit, CI runs nib-tla verify
areas/examples/raft-nib: one calm run, then each sweep below over a range of
seeds. The correct program must be admitted on every seed, and each broken
wire must be refused on at least one.
| wire | world | expected | result |
|---|---|---|---|
Main | worlds/calm.json | admitted | admitted, 5,498 entries |
Main | worlds/faults.json | admitted | 10 of 10 admitted |
Mutant | worlds/split.json | refused | 8 of 10 refused |
Careless | worlds/faults.json | refused | 8 of 10 refused |
Main | worlds/crash.json | admitted | 10 of 10 admitted |
Forgetful | worlds/crash.json | refused | 5 of 5 refused |
The whole check took 125 s on a laptop.
What nib provides
None of this is special to Raft. It follows from four decisions in the language and its runtime:
- Effects are in the types
- A function lists what it may do. So every way the program touches the world is a named operation with a signature, which is exactly what a journal can record and a monitor can name.
- Wires choose implementations
- The same program runs on real
sockets or on
Net.Sim, and a known-wrong variant is one binding away, with no mocks and no flags in the protocol code. - The scheduler has one choice point
- Fibres change hands only where one waits, and every such switch asks the world which ready fibre runs next. Under a seed that choice is random; in a journal it is recorded; in a replay it is served back.
- The journal is the run
- Operations are recorded when they complete, each with its node, fibre and message id, and chained by digest. The same file drives replay, the checker, and the world file that reproduces it.
What this does not show
- It checks the runs it is given: tens of seeds, not every interleaving. A bug that needs a schedule no seed draws goes unseen.
- The monitor checks safety only. A run in which no leader is ever elected is admitted.
- Leader completeness is checked only as far as the journal can settle it: commits are inferred from applies.
- Everything runs on the simulated world. Real sockets can be recorded, but not yet replayed with concurrent servers.
Run it yourself
From a checkout of the repository, with nibc and
nib-tla built:
nibc --run areas/examples/raft-nib/raft.nib --wire Main \
--world areas/examples/raft-nib/worlds/crash.json --journal-out run.jsonl
nib-tla check --spec areas/examples/raft-nib/RaftTrace.tla --journal run.jsonl
nib-tla verify areas/examples/raft-nib
explore.sh runs any wire under many seeds and keeps each
refused run with the world file that reproduces it. Every file named here is in the shared snapshot.