A deterministic discrete-event simulator for consensus algorithms.
This is the primary way algorithms are tested. Everything that could vary
between runs -- time, randomness, message delay, message loss, crash points --
is drawn from an explicit seed and threaded through pure functions, so a
failing run is fully reproducible from (seed, config) and can be replayed,
forked, or stepped one tick at a time.
The simulator is itself a value. advance takes a sim and returns a sim; no
atoms, no threads, no wall clock. That is what makes shrinking work: a
test.check failure hands you a seed, and the seed hands you the exact history.
A tick is the unit. Timers are armed for a number of ticks, message delays are a number of ticks. Nothing consults a real clock, so a timeout-dependent bug reproduces identically on a fast machine, a slow machine, and in a browser.
Map iteration order is not guaranteed to be stable across platforms, so anywhere order could affect the result -- delivering to peers, breaking ties -- we sort explicitly. Where a collection is drawn from, we sort before sampling. Violating this would make a run irreproducible in exactly the cases that matter most.
A deterministic discrete-event simulator for consensus algorithms. This is the primary way algorithms are tested. Everything that could vary between runs -- time, randomness, message delay, message loss, crash points -- is drawn from an explicit seed and threaded through pure functions, so a failing run is fully reproducible from `(seed, config)` and can be replayed, forked, or stepped one tick at a time. The simulator is itself a value. `advance` takes a sim and returns a sim; no atoms, no threads, no wall clock. That is what makes shrinking work: a test.check failure hands you a seed, and the seed hands you the exact history. ## Time is logical A tick is the unit. Timers are armed for a number of ticks, message delays are a number of ticks. Nothing consults a real clock, so a timeout-dependent bug reproduces identically on a fast machine, a slow machine, and in a browser. ## Determinism rules Map iteration order is not guaranteed to be stable across platforms, so anywhere order could affect the result -- delivering to peers, breaking ties -- we sort explicitly. Where a collection is drawn from, we sort before sampling. Violating this would make a run irreproducible in exactly the cases that matter most.
(add-peer sim peer-id impl config)Add a peer that did not exist when the simulator was built.
:peer-ids is who is REACHABLE -- who can be sent a message and who gets a
tick. It is NOT who counts toward a quorum: that is each node's own
:validators, which an algorithm may now change through a decided value (see
urd.consensus.reconfig). Before set changes existed the two were always
equal, and make still derives one from the other for convenience, which
made them easy to conflate.
Keeping them apart is what lets a set change be simulated honestly:
Neither is expressible while the two are one list. It is the same distinction as discovery-versus-membership in a peer-to-peer transport: reachability is not authority.
config is merged into the node's init config and stored as its
:node-config, so a later restart rebuilds it the same way. A joiner is
normally given :validators reflecting the set it is joining, and whatever
state-sync register the algorithm uses to start above genesis.
Add a peer that did not exist when the simulator was built.
## Why this is separate from the validator set
`:peer-ids` is who is REACHABLE -- who can be sent a message and who gets a
tick. It is NOT who counts toward a quorum: that is each node's own
`:validators`, which an algorithm may now change through a decided value (see
`urd.consensus.reconfig`). Before set changes existed the two were always
equal, and `make` still derives one from the other for convenience, which
made them easy to conflate.
Keeping them apart is what lets a set change be simulated honestly:
- A joining validator must be REACHABLE BEFORE it counts. The cluster has
to be able to replicate to it while it is still not a voter, or it can
never catch up enough to become one.
- A removed validator stays REACHABLE AFTER it stops counting. It keeps
receiving messages and must not be able to influence a quorum.
Neither is expressible while the two are one list. It is the same distinction
as discovery-versus-membership in a peer-to-peer transport: reachability is
not authority.
`config` is merged into the node's `init` config and stored as its
`:node-config`, so a later `restart` rebuilds it the same way. A joiner is
normally given `:validators` reflecting the set it is joining, and whatever
state-sync register the algorithm uses to start above genesis.(advance sim)Advance the simulation by one tick.
Advance the simulation by one tick.
(agreement? sim)Do all live nodes agree on their committed prefix?
The core safety property: no two nodes commit different values at the same position. Nodes may be at different lengths -- that is lag, not disagreement.
Do all live nodes agree on their committed prefix? The core safety property: no two nodes commit different values at the same position. Nodes may be at different lengths -- that is lag, not disagreement.
(bandwidth-drops sim)Messages the per-tick budget refused, if one was configured.
Messages the per-tick budget refused, if one was configured.
(bytes-sent sim)Notional bytes each peer put on the wire over the whole run.
Notional bytes each peer put on the wire over the whole run.
(crash sim peer-id)Crash a node: it stops stepping, its in-memory state is lost, and any write that had not reached the platter is lost with it.
complete-due-persists lands every completed write in :landed --
durable? true and durable? false alike -- under a comment asserting the
write is now on disk and survives a crash. restart then replayed all of it.
So a non-fsynced write ALWAYS survived a simulated crash, and no crash test in
this repo could tell buffered state from durable state.
That is the entire distinction persist-then's durable? flag exists to
draw, and the simulator was structurally blind to it: an algorithm could
release a binding commitment against a write that never reached disk, and the
whole suite would still pass. Three Raft findings live in exactly that gap --
a follower acking MsgAppResp off an entry-less buffered write, a leader
consuming its own vote and its own append the same way -- and none of them
could be reproduced here, not because they were absent but because the oracle
could not represent them.
POSIX-shaped and no more: an fsync flushes everything written before it. So a
buffered write that PRECEDES a durable one survives on its coat-tails, and
only the buffered tail after the last fsync is lost. That distinction is not
a detail to round off -- etcd-raft's MustSync is built on it, deliberately
leaving a commit-index change unsynced because a later sync will carry it.
:writes keeps every write ever ISSUED and is untouched, so a test can still
see what was attempted; :landed is narrowed to what a restart may see.
Crash a node: it stops stepping, its in-memory state is lost, and any write that had not reached the platter is lost with it. ## Buffered writes did NOT used to be lost, and that blinded every crash test `complete-due-persists` lands every completed write in `:landed` -- `durable? true` and `durable? false` alike -- under a comment asserting the write is now on disk and survives a crash. `restart` then replayed all of it. So a non-fsynced write ALWAYS survived a simulated crash, and no crash test in this repo could tell buffered state from durable state. That is the entire distinction `persist-then`'s `durable?` flag exists to draw, and the simulator was structurally blind to it: an algorithm could release a binding commitment against a write that never reached disk, and the whole suite would still pass. Three Raft findings live in exactly that gap -- a follower acking `MsgAppResp` off an entry-less buffered write, a leader consuming its own vote and its own append the same way -- and none of them could be reproduced here, not because they were absent but because the oracle could not represent them. ## The model POSIX-shaped and no more: an fsync flushes everything written before it. So a buffered write that PRECEDES a durable one survives on its coat-tails, and only the buffered tail after the last fsync is lost. That distinction is not a detail to round off -- etcd-raft's `MustSync` is built on it, deliberately leaving a commit-index change unsynced because a later sync will carry it. `:writes` keeps every write ever ISSUED and is untouched, so a test can still see what was attempted; `:landed` is narrowed to what a restart may see.
(estimate-size x)(estimate-size x depth)A portable, deterministic size estimate for a message, in notional bytes.
Structural rather than (count (pr-str x)), and that is deliberate: printing
is not guaranteed to agree between Clojure and ClojureScript for doubles or
for records, and a simulator whose accounting differs between platforms is
worse than one with no accounting. The absolute numbers mean nothing; what
has to be true is that a bigger message costs more and that both platforms
agree by how much.
Depth-bounded, because a message is attacker-supplied data in several of these algorithms and a cyclic or absurdly nested one must not hang the simulator.
A portable, deterministic size estimate for a message, in notional bytes. Structural rather than `(count (pr-str x))`, and that is deliberate: printing is not guaranteed to agree between Clojure and ClojureScript for doubles or for records, and a simulator whose accounting differs between platforms is worse than one with no accounting. The absolute numbers mean nothing; what has to be true is that a bigger message costs more and that both platforms agree by how much. Depth-bounded, because a message is attacker-supplied data in several of these algorithms and a cyclic or absurdly nested one must not hang the simulator.
(inject sim peer-id input)Feed an input straight into a node, bypassing the network.
This is the class of input that neither arrives over the network nor answers
an effect: a client request, a newly mined block, an operator command. Needed
by any algorithm that reacts to one, since request-value is node-INITIATED
and offers no way to hand a proposal to a running node from outside.
Feed an input straight into a node, bypassing the network. This is the class of input that neither arrives over the network nor answers an effect: a client request, a newly mined block, an operator command. Needed by any algorithm that reacts to one, since `request-value` is node-INITIATED and offers no way to hand a proposal to a running node from outside.
(inject-all sim input)Inject the same input into every live node.
Inject the same input into every live node.
(make {:keys [seed nodes node-config config] :or {seed 1 config {}}})Build a simulator.
nodes is {peer-id node-impl}. Each node's init runs immediately and its
effects are interpreted, so a node can persist hard state or arm a timer
before the first tick.
Build a simulator.
`nodes` is {peer-id node-impl}. Each node's `init` runs immediately and its
effects are interpreted, so a node can persist hard state or arm a timer
before the first tick.(partition-network sim parts)Split the network. parts is a collection of peer-id sets; messages only
flow within a part.
Split the network. `parts` is a collection of peer-id sets; messages only flow within a part.
(peak-tick-bytes sim)The most any peer sent in a SINGLE tick. A storm is a spike, so this is the measure that shows one; a total averages it away.
The most any peer sent in a SINGLE tick. A storm is a spike, so this is the measure that shows one; a total averages it away.
(quiet? sim)No messages in flight, no timers armed, nothing waiting on a write.
No messages in flight, no timers armed, nothing waiting on a write.
(restart sim peer-id)Restart a crashed node from its DURABLE state only.
In-memory state is discarded and init runs again, so anything the algorithm
failed to persist is genuinely lost -- which is the point. A node that keeps
its term in memory but not on disk will fail here, exactly as it would in
production.
:restored carries two views of the same thing, because algorithms differ in
what they need. :durable is the merge of every landed write, which suits an
algorithm whose durable state is a small record. :writes is the ORDERED log
of landed writes, which an algorithm with an append-only log must replay --
merging cannot express truncate-from, and a later append at a lower index
has to overwrite rather than union.
Restart a crashed node from its DURABLE state only. In-memory state is discarded and `init` runs again, so anything the algorithm failed to persist is genuinely lost -- which is the point. A node that keeps its term in memory but not on disk will fail here, exactly as it would in production. `:restored` carries two views of the same thing, because algorithms differ in what they need. `:durable` is the merge of every landed write, which suits an algorithm whose durable state is a small record. `:writes` is the ORDERED log of landed writes, which an algorithm with an append-only log must replay -- merging cannot express `truncate-from`, and a later append at a lower index has to overwrite rather than union.
(run sim max-ticks)(run sim max-ticks pred)Advance until pred holds or max-ticks elapse.
Returns the sim. Check (:tick sim) against max-ticks to tell convergence
from exhaustion.
Advance until `pred` holds or `max-ticks` elapse. Returns the sim. Check `(:tick sim)` against max-ticks to tell convergence from exhaustion.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |