public snapshot · areas/examples/raft-nib/ · system@main · commit da2bf624f03d · shared 2026-11-23T17:23:12Z· frozen: this page does not change when the repository does
all files
  1---- MODULE RaftTrace ----
  2\* A safety monitor over a recorded Raft run. It does not model the protocol's
  3\* steps; it rebuilds what the journal shows and refuses the journal at the
  4\* first entry that breaks one of Raft's safety properties:
  5\*
  6\*   ElectionSafety      at most one node leads a term. A node leads term t when
  7\*                       it sends an `append` message of term t.
  8\*   LogMatching         two logs that hold an entry of the same term at the same
  9\*                       index are identical up to that index. Each node's log is
 10\*                       rebuilt from its `Store.append` writes, and the property
 11\*                       is checked on every write.
 12\*   LeaderCompleteness  an entry committed before a term began is in the log of
 13\*                       that term's leader. A commit's term is not in the
 14\*                       journal, so the check is the part the journal settles: an
 15\*                       entry some node applied before X's candidacy for term t
 16\*                       (its `Store.save(X, t, X)`) was committed before t, and
 17\*                       X's log must hold it whenever X leads t.
 18\*   StateMachineSafety  no two nodes apply different commands at one index.
 19\*   Durability          a node's saved term never goes back, and its vote in a
 20\*                       term, once saved, is never changed: what `Store.save`
 21\*                       writes is what a node recovers after a crash, and a
 22\*                       node that forgot it could vote twice in one term.
 23\*
 24\* Every other operation in a Raft run is listed in Unmodeled: it moves the
 25\* trace on and changes nothing here.
 26EXTENDS Integers, Sequences, TLC, TraceData
 27
 28Nodes == {"10.0.0.1", "10.0.0.2", "10.0.0.3"}
 29
 30Unmodeled == {"Time.Sim.now", "Time.Sim.sleep", "Net.Sim.recv", "Net.Sim.connect",
 31              "Net.Sim.accept", "Net.Sim.listen", "World.deliver", "World.partition",
 32              "World.random", "World.schedule",
 33              "World.crash", "World.restart", "Random.Sim.next_int",
 34              "Raft.Store.recover", "Raft.Machine.applied", "Raft.Rules.one_vote",
 35              "Raft.Rules.check_log", "Io.Stdout.println"}
 36
 37VARIABLES l, leaders, applied, logs, candidacies, saved
 38
 39TraceInit ==
 40  /\ l = 1
 41  /\ leaders = {}
 42  /\ applied = {}
 43  /\ logs = [n \in Nodes |-> << >>]
 44  /\ candidacies = {}
 45  /\ saved = [n \in Nodes |-> <<0, "">>]
 46
 47Min(a, b) == IF a < b THEN a ELSE b
 48
 49\* The message a send carries, decoded from its second argument.
 50Sent(e) == e.argv[2].json
 51
 52Matching(a, b) ==
 53  \A i \in 1..Min(Len(a), Len(b)) :
 54    a[i].term = b[i].term => \A j \in 1..i : a[j] = b[j]
 55
 56\* A save of (t, v) over what was saved before, (t0, v0).
 57Durable(t0, v0, t, v) == t > t0 \/ (t = t0 /\ (v0 = "" \/ v0 = v))
 58
 59\* X's log holds every entry applied before X's candidacy for term t.
 60Complete(x, t) ==
 61  \A c \in candidacies :
 62    (c[1] = x /\ c[2] = t) =>
 63      \A a \in applied :
 64        a[3] < c[3] => (a[1] <= Len(logs[x]) /\ logs[x][a[1]].cmd = a[2])
 65
 66MapEvent(e) ==
 67  \/ /\ e.op = "Net.Sim.send"
 68     /\ e.argv[2].isJson
 69     /\ IF Sent(e).kind = "append"
 70          THEN /\ \A p \in leaders : p[1] = Sent(e).term => p[2] = Sent(e).from
 71               /\ Complete(Sent(e).from, Sent(e).term)
 72               /\ leaders' = leaders \cup {<<Sent(e).term, Sent(e).from>>}
 73          ELSE leaders' = leaders
 74     /\ UNCHANGED <<applied, logs, candidacies, saved>>
 75  \/ /\ e.op = "Raft.Store.append"
 76     /\ e.argv[1] \in Nodes
 77     /\ LET n == e.argv[1]
 78            next == SubSeq(logs[n], 1, e.argv[2]) \o e.argv[3]
 79        IN /\ \A m \in Nodes \ {n} : Matching(next, logs[m])
 80           /\ logs' = [logs EXCEPT ![n] = next]
 81     /\ UNCHANGED <<leaders, applied, candidacies, saved>>
 82  \/ /\ e.op = "Raft.Store.save"
 83     /\ e.argv[1] \in Nodes
 84     /\ Durable(saved[e.argv[1]][1], saved[e.argv[1]][2], e.argv[2], e.argv[3])
 85     /\ saved' = [saved EXCEPT ![e.argv[1]] = <<e.argv[2], e.argv[3]>>]
 86     /\ candidacies' = IF e.argv[3] = e.argv[1]
 87                         THEN candidacies \cup {<<e.argv[1], e.argv[2], l>>}
 88                         ELSE candidacies
 89     /\ UNCHANGED <<leaders, applied, logs>>
 90  \/ /\ e.op = "Raft.Machine.apply"
 91     /\ \A a \in applied : a[1] = e.argv[2] => a[2] = e.argv[3]
 92     /\ applied' = applied \cup {<<e.argv[2], e.argv[3], l>>}
 93     /\ UNCHANGED <<leaders, logs, candidacies, saved>>
 94  \/ /\ e.op \in Unmodeled
 95     /\ UNCHANGED <<leaders, applied, logs, candidacies, saved>>
 96
 97TraceStep ==
 98  /\ l <= Len(Trace)
 99  /\ MapEvent(Trace[l])
100  /\ l' = l + 1
101
102TraceNotAccepted == l <= Len(Trace)
103====