moonpool ~
GitHub →
$ the sim book

Aiming Storage Faults

Most of a Disk Is Nobody’s Business

Random storage faults are layout-blind. A corruption coin lands on whatever sector an operation touches, and a crash resolves every dirty sector alike. Most of a preallocated journal is zeros that nobody reads. A uniform fault mostly lands there, the run stays green, and the sixty-four bytes that recovery has to judge, the identifiers of the last batch, are never hit.

Moonpool keeps its fault coordinates simple: a file plus a flat sector offset. It does not know what a slot, a page or a header is. The format does. So the format tells the simulator where its bytes live, and three mechanisms use that map: an eligibility mask, a fault focus and a replicated fault pattern. A fourth, targeted injection, needs no map at all.

A Map the Format Draws

LayoutRegion in moonpool-core is plain data: one named byte range of one file.

pub struct LayoutRegion {
    pub path: String,         // as the format opens it
    pub bytes: Range<u64>,
    pub kind: &'static str,   // the format's own label: "slot", "header"...
    pub stripe: Option<u64>,  // the replicated record these bytes hold
}

kind lets a harness weigh kinds differently. stripe is the key every replica’s copy of a record shares (a Paxos slot, a page number), or None for bytes only this node has, such as a header. Nothing in it depends on a simulator, so a format lists its regions without a cfg(test).

moonpool-journal is one such format. Journal::regions() returns every region a fault could hit: each live position’s persist record and entry, striped by position, plus the two metainfo copies and every segment’s two header copies with no stripe. Journal::layout(position) returns the Layout of one position. The kinds are Layout::RECORD, Layout::ENTRY, Layout::META and Layout::HEADER. See A Crash-Aware Journal.

The Eligibility Mask

The coarsest control is a veto: (path, sector) -> bool. SimWorld::set_storage_eligibility_mask(mask) installs one. The random families and the damaging crash outcomes (Lost, LatentFault, Shorn) consult it. An ineligible sector falls back to the always-legal old-or-new resolution.

// Never damage sector 0 of any file: the superblock stays readable.
sim.set_storage_eligibility_mask(Arc::new(|_path: &str, sector: u64| sector != 0));

This is TigerBeetle’s ClusterFaultAtlas pattern, used by a harness that keeps its own ledger. Coins are rolled before the mask is consulted, so installing one never shifts the random stream.

Fault Focus

A mask says whether. A FaultFocus says how likely. It gives a weight to byte ranges of a process’s files and a background weight to every other sector of that disk. Every family rolled per sector or per operation (read and write corruption, read and write EIO, phantom writes, sync lies, and the damaging crash outcomes) has its probability multiplied by the weight of the sectors it would hit:

// Inside a process: aim this disk's faults at the journal's live regions.
let focus = FaultFocus::new()
    .background(0.1) // the zeros, barely
    .layout(&journal.regions(), |region| {
        if region.kind == Layout::RECORD { 8.0 } else { 4.0 }
    });
ctx.storage().focus_faults(focus)?;

spot(path, bytes, weight) adds one range by hand. Where spots overlap, the heaviest wins. A weight of 0 makes a sector immune. A weight never revives a family the configuration leaves at zero, and a misdirected write honours only the immunity. The rolls are drawn whatever the weights, so a focus changes which faults fire, never which draws decide them.

SimStorageProvider::focus_faults is reachable from process code and scopes the focus to that process’s disk. clear_fault_focus() removes it. A hand-driven test calls SimWorld::set_fault_focus(ip, focus). A focus survives its process’s crash and is cleared by a wipe of its storage.

Targeted Injection

A directed test does not want probabilities. It wants this sector broken, now. Four SimWorld methods are path-global, reaching every process’s file at that path:

Method Effect
corrupt_file(path, sectors) Deterministic damage on those sectors
fail_file_with_eio(path, sectors, target) Reads, writes or both (EioTarget) on those sectors fail with an I/O error
clear_file_eio(path, target) Removes the injection
corrupt_durable_out_of_band(path, sector) A deliberate barrier violation, to test the oracle itself

To damage one copy of one record on one replica, aim at one owner and exact bytes:

Method Effect
corrupt_process_file_bytes(ip, path, bytes) Durably changes exactly those bytes of that process’s file. Each byte is XORed with a nonzero mask of its offset, so no randomness is drawn. The durability oracle is re-stamped, so a later crash does not report a lost synced write.
fail_process_file_with_eio(ip, path, sectors, target) EIO on one owner’s file only
clear_process_file_eio(ip, path, target) Removes it
process_file_durable_bytes(ip, path) That file’s durable image, to find the bytes to aim at

A scripted FaultInjector holds the same four on FaultContext: durable_bytes, corrupt_bytes, fail_file_with_eio and clear_file_eio, each taking the process IP as a string. Reading the durable bytes takes no simulated time, rolls no fault coin and draws no randomness, so aiming never perturbs the run:

// Inside a FaultInjector: damage three bytes of the first replica only.
let first = &ctx.process_ips()[0];
let image = ctx.durable_bytes(first, "data")?;
let record = find_last_record(&image); // the format's own parsing
ctx.corrupt_bytes(first, "data", record.start..record.start + 3)?;

The other replicas’ files at "data" are untouched. corrupt_bytes refuses a range past the durable end with StorageError::OutOfRange, and a missing file with NotFound. The damage is exactly those bytes: a record that shares a sector with them but none of the bytes keeps its clean copy, both for the shard gate that judges the fault and for the copy count at the end of the run (Shards::clean_copies).

Replicated Fault Patterns

A replicated system survives disk damage only while some replica keeps a clean copy of every record. Random faults drawn independently per disk eventually hit every copy of one record, and a valid run becomes unwinnable. A harness could keep a ledger. Moonpool prevents it by construction, because it knows the topology:

SimulationBuilder::new()
    .cluster(LocalityConfig::new(1, 3, 1..=2, 1), make_node)
    .enable_chaos([Chaos::Storage(ChaosMode::Swarm)])
    .replicated_storage_faults(ReplicatedFaults::new(DomainLevel::Zone).group("node"))

Each process publishes what its disk holds with ctx.storage().publish_layout(&regions). The stripe of each region is the record’s key, shared by every replica’s copy. A ReplicatedFaults covers one process group (.group(name), the processes’ Process::name), or the whole topology without one. Call replicated_storage_faults once per replicated group, so acceptors and matchmakers each draw their own pattern. Processes outside every covered group keep plain storage chaos.

A moonpool_journal::Journal publishes its own regions in a shard with Journal::publish_layout(Some(shard)), its positions as stripes. A store whose replicas write different records at the same position (a registry each replica fills in its own order) calls Journal::publish_layout_whole instead: no region has a stripe, so damage anywhere counts against the whole copy.

Such a store may lose its copy to a crash that tears its last batch without a damaged sector, when the format refuses the torn batch. The gate let that crash land, so it still counts the copy clean. The harness then calls SimStorageProvider::lose_replica_past_gate, and the gate counts the copy lost until the replica heals. A process that keeps several stores, one per shard, may give up one torn store and keep the others. The harness then calls SimStorageProvider::lose_shard_past_gate(shard): the gate counts that one copy lost until it catches up, and the replica’s copies of other shards stay clean. A copy with no striped region in its shard (a registry published whole) stands or falls whole, so the gate does not wait for the harness there: a crash that rolls back or loses a dirty sector of such a copy counts it lost at once. That is also the only case where the gate turns a crash into a kill that keeps the page cache: a dirty sector of a striped region needs no downgrade, because the crash model keeps it old or new while its stripe cannot afford damage, and every other unsynced sector of the process still rolls back. A crash the gate let through counts as pending until it lands, so the second victim of a domain or tag reboot is judged as if the first had torn its whole copies. SimContext::crash(RebootKind::CrashAndWipe), a process wiping itself, is the code’s own decision and skips the gate. When the store next opens the copy and its own check passes, the code under test calls moonpool_buggify::shard::intact(shard), and the copy is clean again. A copy the store refuses stays lost. A failed background writeback (fsyncgate) can drop unsynced bytes as a crash does, so it asks the gate too: the gate refuses it when the bytes it would drop or strand belong to a copy the shard cannot lose, and counts Downgrade::WritebackRefused. A crash that loses an unsynced directory entry asks the gate the same way: the gate keeps the entry when its file holds a copy the shard cannot lose, and counts Downgrade::DirEntryLossRefused. An entry loss the gate lets land counts the copies of that file lost until the store opens them intact.

Each seed draws one FaultPattern per group over the failure domains at the chosen DomainLevel (Machine, Zone or Datacenter):

FaultPattern Where damage may land
Minority { domains } Only the processes of a few domains, anywhere on their disks. TigerBeetle’s minority corruption.
Striped { domains, local } Every domain, but stripe s only in the domain it rotates to. TigerBeetle’s helical corruption, keyed by record instead of file offset, because a replicated log writes the same record at different offsets on each replica.
Rolling { domains } One domain at a time, anywhere on its disks. The turn moves on once the damage is repaired.
Spared The group has fewer than two domains, so no process in it takes damage.

tolerance(n) (default 1) is how many domains may hold damage to one record at once. It is clamped so one domain always keeps every record. Under a stripe rotation, node-local bytes (stripe None, or bytes never published) are damaged only in the tolerance - 1 local domains, and those domains may damage every stripe. A format may answer a damaged header by giving up the whole replica, so node-local damage counts against every stripe. At the default tolerance no node-local byte is damaged under this pattern.

A rolling turn is repaired when every damaged sector has been rewritten, truncated away or deleted, or is no longer inside a region its process publishes. A domain holding no damage passes the turn as soon as another domain’s disk is up for a fault. A domain that never repairs holds the turn, and a run stuck on one turn is the evidence. SimWorld::fault_turn(ip) names the domains holding the turn, and SimStorageProvider::fault_pattern() tells a process which pattern its group drew. SimWorld::draw_replicated_faults(config, &localities) draws a pattern for a hand-driven test.

A wiped disk holds no copy of any record, so the patterns count it as damage. A process in the wipe ledger, wiped and not yet healed, puts its domain among the damaged ones for every record. Damage then lands only where the record’s damaged domains, with the lost one, stay within tolerance: in the lost domain itself at the default tolerance, or in the marked domains of a minority that already holds it. A rolling turn holds on a window with a lost disk, as it holds on damage. Without this, a wipe in one domain and a read fault in another leave a record with no clean copy, and a correct replication layer cannot repair it. The other order is covered too. A wipe loses every copy its process holds, so a wipe drawn by an attrition regime while another domain of the group holds damage becomes a crash: that wipe would leave a damaged record with no clean copy. The check reads the disks, so damage that has been repaired no longer holds it back. A scripted wipe (FaultContext::reboot, SimContext::crash) is the harness’s own decision and is never downgraded, so a script that wipes after damage can still pass tolerance. Two wipes in two domains are what a wipe budget bounds, not the pattern.

A wipe heals as the ledger says: by WipeLedger::heal, or after the WipeHeal::After bound of the attrition regime that wiped it. A wipe no budgeted regime decided heals only by the signal. A harness that never signals keeps the wiped domain lost for the rest of the run, and at tolerance 1 its group then takes damage only there. The run records a replica_still_lost sim fault, with the process’s ip, for each such process when it shuts down, so the narrower coverage shows in the timeline.

Failure domains come from the .cluster() topology. A group registered with .processes() has none and is always spared. The pattern shapes the same families a focus weighs. It is a weight of zero where damage may not land, and it composes with a focus and the mask. Disk failure, wipes and lost directory entries are outside it: they take a whole disk, or only unsynced state.

Which One to Reach For

You want Use
A sector that must never break The eligibility mask
Random faults where recovery has to look FaultFocus with the format’s LayoutRegions
One record on one replica, broken now FaultContext::corrupt_bytes after durable_bytes
A cluster that stays recoverable under random damage replicated_storage_faults with publish_layout

All four compose. The pattern and the mask veto first, the focus weighs what remains, and a targeted injection ignores all three because the test owns its budget.