moonpool ~
GitHub →
$ techniques

Storage faults

A crash decides the fate of each sector written after the last sync.

The problem

A write goes to the page cache first. Only a sync puts it on the disk. When the power goes off between the two, a real disk can keep the new bytes, keep the old bytes, lose the sector, or keep a half-written sector. Code that trusts a write before its sync loses data, but only on the day of a crash.

The technique

The simulated disk keeps each sector that was written and not synced. At a crash, it decides the fate of each one: kept old, kept new, lost, latent (corrupt bytes on each read) or shorn (half old, half new). A sector that a sync reported durable must not change. The simulator checks this after each crash: this is the lost-synced-write oracle. If a synced sector changed, the run fails as a bug of the disk model, unless the test armed the family of disks that lie about a sync.

The simulator reports each fate in a storage_crash_sectors event. It is read-only: it draws nothing and does not change the run.

The live run

Each server writes version v of the register to sector v − 1 of its log, then syncs it. Crash damage is on and the other storage faults are off. Step to 154 ms: server 10.0.1.1 crashes after it wrote sector 1 and before the sync. The crash loses the sector. The server did not answer that write, so no client counts on it. When a server boots, it reads its log and keeps the newest record that passes its check word.

the log of each server, one sector per version

The code

The write path of a server. The record is in the page cache after write_at and on the disk after sync_all. The hint! between them names the moment where a reboot hurts most, and attrition can strike there.

if request.version > version {
    let sector = request.version - 1;
    let mut record = [0; SECTOR];
    record[..24].copy_from_slice(&encode_state(request.version, request.value));
    let written = file
        .write_at(sector * SECTOR as u64, &record)
        .await
        .map_err(|e| io_error(&e))?;
    if written < SECTOR {
        return Err(SimulationError::InvalidState(format!(
            "register file: short write of {written} bytes"
        )));
    }
    tracing::info!(sector, "log_written");
    // The record is in the page cache, not on the disk.
    hint!("register: record written, not synced").await;
    file.sync_all().await.map_err(|e| io_error(&e))?;
    tracing::info!(sector, "log_synced");
    (version, value) = (request.version, request.value);

In the Guide

All the techniques