Raft consensus in MoonBit.
moon add moonbitstack/moonraft// Drive a five-node cluster through the deterministic simulator.
let cluster = @raft.Cluster::new(["a", "b", "c", "d", "e"], seed=1)
let leader = cluster.run_until_leader(200) // elect a leader
let _ = cluster.propose(b"set x = 1") // replicate a command
let _ = cluster.run_until_committed(2, 200) // wait for commit
cluster.crash(leader.unwrap()) // inject a fault
let _ = cluster.run_until_leader(400) // a new leader takes over
assert_true(cluster.one_leader_per_term()) // safety still holds
assert_true(cluster.committed_agrees())git clone https://github.com/moonbitstack/moonraft && cd moonraft
moon run src/cmd/examplecluster of 5 nodes, seed 1
elected leader: c
committed 'set x = 1' on a majority
crashed the leader
new leader: a
one leader per term : true
committed prefixes agree : true
safety invariants hold : true# The high-level, deterministic Cluster simulator — the easiest place to start
moon run src/examples/00-helloworld # a minimal system: elect a leader
moon run src/examples/01-replicate # propose commands, confirm every node commits
moon run src/examples/02-fault-tolerance # crash, partition, drop packets — safety holds
moon run src/examples/03-operations # progress, leadership transfer, compaction
moon run src/examples/04-kvstore # a complete replicated key-value store on a StateMachine
# Membership and the two roles a node can play
moon run src/examples/05-membership # add and remove voters (ConfChange) at runtime
moon run src/examples/06-learner # a non-voting learner, then promote it to a voter
# Fault handling and log management
moon run src/examples/07-checkquorum # check-quorum: a leader steps down when it loses its majority
moon run src/examples/08-snapshot # compact the log; a restarted node catches up from a snapshot
# The message-driven RawNode — the API you use to deploy Raft on a real transport
moon run src/examples/09-rawnode # the RawNode tick → Ready → Advance loop, by hand
moon run src/examples/10-storage # persist Raft state to MemoryStorage and recover it
moon run src/examples/11-readindex # a linearizable (ReadOnlySafe) read, confirmed by quorummoon run --release src/cmd/benchmark| Benchmark | ns/op |
|---|---|
| BenchmarkStatus | ~250 |
| BenchmarkRawNode (propose → persist → commit → apply) | ~5300 |
| BenchmarkMajorityConfig_CommittedIndex | ~970 |
moon build --target wasm --release src/examples/browser
cp _build/wasm/release/build/examples/browser/browser.wasm docs/moonraft.wasm
python3 -m http.server 8099 --directory docs # then open http://localhost:8099/| Package | Responsibility |
|---|---|
| quorum/ | Majority and joint-configuration vote counting |
| tracker/ | Per-follower Progress (Probe / Replicate / Snapshot) and the Inflights window |
| raftpb/ | On-the-wire types: Entry, Message, RPCs, HardState, Snapshot, ConfState, entry sizing |
| confchange/ | Configurations, joint consensus, ConfChange and the config Changer |
| storage/ | MemoryStorage, the write-ahead log, and the LogStore / RaftStorage traits |
| log/ | RaftLog, the unstable tail, term lookup and bounded slices |
| core/ | The consensus engine: RaftNode / Node step & dispatch, election, replication, snapshots, ReadIndex, leader lease, check-quorum, RawNode / Ready, Config, and the simulator |
fn demo_run(ids : Array[String], seed : UInt64, elect_ticks : Int, commit_ticks : Int, reelect_ticks : Int) -> DemoReportlet no_limit : UInt64