Internals

Simulation tests (raftdb-sim)

How deterministic simulation checks the write-ahead log and the whole table against crashes, torn writes and lying disks, and what it does not model.

raftdb-sim runs the real storage code on a simulated disk, driven by a seeded random generator, and checks a contract after every crash. A seed gives the same run on every platform, so any failure is replayed exactly. The crate depends only on raftdb-wal; it has no unsafe code, no clock, no thread and no file. The approach follows the FoundationDB simulation study. D16 adds a rule: the simulator must find a planted bug within 1,000 schedules or it is not trusted.

Parts#

  • Rng: xoshiro256++ seeded through splitmix64, with below(n) and chance(num, den) (the idea of FoundationDB's BUGGIFY_WITH_PROB). Its output is pinned by a golden test.
  • Disk: a fixed-size zeroed disk with a live image (what reads see) and a durable image (what survives power loss). write marks 512-byte sectors dirty, sync makes them durable, and crash(rng) keeps each dirty sector whole, torn (a random prefix) or lost; reads then see the durable image.
  • run_schedule(seed, bug): 40 steps. Each writes a batch of 1 to 8 records in the real format and then, with 60 % probability, syncs and acknowledges, with 20 % crashes and recovers, or with 20 % continues unsynced. Half the schedules use one fixed record size, which makes a new record end exactly where a stale one did. After every recovery it checks the contract: every acknowledged record is present, and the recovered list is exactly a prefix of what was written. It returns a trace hash.
  • Bug::KeepStaleTail: a recovery that forgets to zero the bytes after the last valid record. The simulation must find it; this proves the checker can fail.

The whole table#

raftdb_sim::table runs the real Table and Wal (flush leader, buffers, cut-and-re-extend on open) on SimFile, a Disk behind the raftdb_wal::Storage trait. A schedule writes random puts and deletes over 12 keys and then commits, or "commits" while the disk lies about sync (written, not durable), or crashes before committing. The disk then crashes and the table is reopened. The contract: the recovered table equals the state after some prefix of the writes, in order, and that prefix contains every write whose commit returned while the disk really synced. The planted defect StorageBug::StaleGrow (shrinking a file keeps the cut bytes, so growing it again exposes them) must be found.

Results#

From the crate's record and the benchmark results log:

RunResult
Log-level, suite sim1,000,000 schedules, 0 violations; the planted bug found after 104 schedules; 5,042 schedules/s on one core
Whole table (earlier run)30,000 schedules, 0 violations; the planted bug found after 80 schedules

A first run once reported a violation on seed 15. It was a flaw in the test harness (a step of only absent-key deletes produced no log position, so earlier uncommitted writes were counted as acknowledged), not in the engine, and was fixed in the harness.

Raft#

The Raft core is simulated separately in the crate's own tests with message loss, duplication, reordering, partitions and crashes; see Raft and the control plane. That simulator has its own small generator and is not part of raftdb-sim.

Limits#

The simulated disk models sector tearing and lost sectors only: not bit rot, misdirected writes, or a drive that reorders flushes across sync. The simulation is single-threaded, so the group-commit threads of Wal are not exercised under a simulated scheduler. Time and network are not simulated in raftdb-sim.