---- MODULE RaftTrace ---- \* A safety monitor over a recorded Raft run. It does not model the protocol's \* steps; it rebuilds what the journal shows and refuses the journal at the \* first entry that breaks one of Raft's safety properties: \* \* ElectionSafety at most one node leads a term. A node leads term t when \* it sends an `append` message of term t. \* LogMatching two logs that hold an entry of the same term at the same \* index are identical up to that index. Each node's log is \* rebuilt from its `Store.append` writes, and the property \* is checked on every write. \* LeaderCompleteness an entry committed before a term began is in the log of \* that term's leader. A commit's term is not in the \* journal, so the check is the part the journal settles: an \* entry some node applied before X's candidacy for term t \* (its `Store.save(X, t, X)`) was committed before t, and \* X's log must hold it whenever X leads t. \* StateMachineSafety 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, is never changed: what `Store.save` \* writes is what a node recovers after a crash, and a \* node that forgot it could vote twice in one term. \* \* Every other operation in a Raft run is listed in Unmodeled: it moves the \* trace on and changes nothing here. EXTENDS Integers, Sequences, TLC, TraceData Nodes == {"10.0.0.1", "10.0.0.2", "10.0.0.3"} Unmodeled == {"Time.Sim.now", "Time.Sim.sleep", "Net.Sim.recv", "Net.Sim.connect", "Net.Sim.accept", "Net.Sim.listen", "World.deliver", "World.partition", "World.random", "World.schedule", "World.crash", "World.restart", "Random.Sim.next_int", "Raft.Store.recover", "Raft.Machine.applied", "Raft.Rules.one_vote", "Raft.Rules.check_log", "Io.Stdout.println"} VARIABLES l, leaders, applied, logs, candidacies, saved TraceInit == /\ l = 1 /\ leaders = {} /\ applied = {} /\ logs = [n \in Nodes |-> << >>] /\ candidacies = {} /\ saved = [n \in Nodes |-> <<0, "">>] Min(a, b) == IF a < b THEN a ELSE b \* The message a send carries, decoded from its second argument. Sent(e) == e.argv[2].json Matching(a, b) == \A i \in 1..Min(Len(a), Len(b)) : a[i].term = b[i].term => \A j \in 1..i : a[j] = b[j] \* A save of (t, v) over what was saved before, (t0, v0). Durable(t0, v0, t, v) == t > t0 \/ (t = t0 /\ (v0 = "" \/ v0 = v)) \* X's log holds every entry applied before X's candidacy for term t. Complete(x, t) == \A c \in candidacies : (c[1] = x /\ c[2] = t) => \A a \in applied : a[3] < c[3] => (a[1] <= Len(logs[x]) /\ logs[x][a[1]].cmd = a[2]) MapEvent(e) == \/ /\ e.op = "Net.Sim.send" /\ e.argv[2].isJson /\ IF Sent(e).kind = "append" THEN /\ \A p \in leaders : p[1] = Sent(e).term => p[2] = Sent(e).from /\ Complete(Sent(e).from, Sent(e).term) /\ leaders' = leaders \cup {<>} ELSE leaders' = leaders /\ UNCHANGED <> \/ /\ e.op = "Raft.Store.append" /\ e.argv[1] \in Nodes /\ LET n == e.argv[1] next == SubSeq(logs[n], 1, e.argv[2]) \o e.argv[3] IN /\ \A m \in Nodes \ {n} : Matching(next, logs[m]) /\ logs' = [logs EXCEPT ![n] = next] /\ UNCHANGED <> \/ /\ 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]] = <>] /\ candidacies' = IF e.argv[3] = e.argv[1] THEN candidacies \cup {<>} ELSE candidacies /\ UNCHANGED <> \/ /\ e.op = "Raft.Machine.apply" /\ \A a \in applied : a[1] = e.argv[2] => a[2] = e.argv[3] /\ applied' = applied \cup {<>} /\ UNCHANGED <> \/ /\ e.op \in Unmodeled /\ UNCHANGED <> TraceStep == /\ l <= Len(Trace) /\ MapEvent(Trace[l]) /\ l' = l + 1 TraceNotAccepted == l <= Len(Trace) ====