When the power fails¶
The bench host has recorded the drive's endurance run for 37 minutes when its power fails. It was in the middle of writing a block of frames from the CAN bus. This chapter answers three questions: what the crash cost, how a reader reads the file left behind, and how the recorder goes on. The closed recording should be the same as if the power had never failed.
In short, the crash costs about the last second of data. A reader reads everything before the damaged block. A resume cuts off the damaged end and lets the recorder write it again. The rest of the chapter shows why each answer holds. It assumes Tape concepts, and it uses the drive's recording, a motor controller on a test bench, as its example throughout.
A file's bytes tell its state¶
A tape file is never closed by accident, and its bytes show whether it was. writer::create makes the file
capture.wwt under its final name and takes a lock on it. Only TapeWriter::close ends the piece with a
close and a tail, and syncs the file.
A program that finds capture.wwt on disk reads its end to learn whether a writer closed it. The lock tells whether
a writer still works on it. The kernel holds the lock for the writer's process, so a crash releases it and leaves
nothing behind. While the lock is held, a second writer gets FsError::Locked and stops.
What the crash costs¶
An operating system keeps written bytes in memory for a while before it puts them on the disk. A power failure loses whatever it has not put there yet. The writer bounds that loss with its sync policy. By default it asks the system to put its bytes on the disk at least once a second. The drive's recorder used the default, so it loses at most about the last second of the run.
SyncPolicy |
The writer syncs | A crash loses at most |
|---|---|---|
SyncPolicy::Interval, 1 s by default |
when blocks wait and the last sync is that long ago | about that long of data |
SyncPolicy::EveryBlocks |
after every n blocks | n blocks |
SyncPolicy::None |
only at the close | everything the system has not written yet |
The bytes on the disk end in a torn block, the block the writer was in the middle of. It ends early or fails its CRC-32C. Everything synced before it is whole, because the writer only appends and never rewrites a byte. A sync policy that writes more often loses less, and each sync costs a wait for the disk. A recorder of a slow, precious stream syncs after every block; one of a fast stream keeps the default.
How a reader finds the end¶
A closed piece ends with a tail, and a reader starts there. The tail leads it to the final index, which lists every block. An unclosed piece has neither, so the reader needs another way to its blocks. The writer provides one while it writes. Every 4 MiB it places a checkpoint: an index block that lists the blocks since the previous checkpoint and points back at it.
ww_tape_figures from the drive's recording at 1/1000 of its data, cut at minute
37. Its checkpoints come every 3 KiB instead of every 4 MiB.The reader looks backwards from the end of the bytes for the newest checkpoint, which lies at most about 4 MiB back. It then follows the chain of checkpoints back to the start, and so lists every block before the newest checkpoint without reading them. Only the blocks after the newest checkpoint need a scan, one by one, and the scan stops at the torn block. The scan therefore covers at most about 4 MiB, however long the recording is.
The drive's file reads like this after the crash. PieceInfo::cut says where the good blocks end:
let bytes = std::fs::read(dir.join("capture.wwt"))?;
let reader = TapeReader::open(&bytes[..])?;
let piece = &reader.pieces()[0];
assert!(piece.tail.is_none()); // never closed
let end = piece.cut.expect("where the good blocks end"); // the torn block's offset
Everything before that offset reads as it would in a closed file: the definitions, the log lines, the CAN frames, the summaries written so far. The piece has no content hash to check, because the close record that would hold it was never written. Its missing tail is how a reader tells that the file was not closed.
Resuming the file¶
When the recorder starts again, it calls writer::resume with the path and the options it wrote the file with.
The resume reads the file the way a reader does. It keeps every byte up to the end of the last good block
and cuts off the rest, the torn end. It then replays the kept blocks, so that the writer knows again what it
wrote. That covers its index, its hashes, and which definitions and state items lie close behind.
let (mut writer, resumed) = ww_tape_fs::writer::resume(&path, options)?;
assert_eq!(resumed.kept_bytes, end); // the torn block is cut off
let lost = writer.next_seq(); // the first block the file lacks
for (_, block) in recorder.sent.iter().filter(|(seq, _)| *seq >= lost) {
writer.write_block(&recorder.defs, block)?;
}
writer.close()?; // capture.wwt now ends in a close and a tail
The recorder gives its calls again from the first block the file lacks, which TapeWriter::next_seq names. The
drive's recorder keeps every block it sends, so it sends the lost ones again. A recorder that relays a device's
stream can instead ask the device for the blocks from that point on.
ww_tape_figures from the same scaled recording, and
it checks that the bytes are equal.The resumed file ends with the same bytes as a file written without the crash, and so with the same content hash. That holds because what the writer adds depends only on the bytes before it, the calls and the options. It needs no clock and no memory of what happened before the crash. The test behind this chapter's code cuts the drive's file, resumes it, and compares it with one written whole. The crate's own tests cut pieces at every block boundary.
Puts that go with a lost block¶
Before a block of data, the writer puts the definitions and state items the block needs, when no copy lies close
behind. Such puts belong with the block after them, and the writer marks them with the flag FLAG_WITH_NEXT for
that reason. When the crash tears the block, the resume cuts off its puts too.
The recorder then gives the call for the block again, and the writer writes the same puts and the same block. A put that finds its item close behind writes nothing, so the recorder never needs to know which puts reached the disk. It simply puts again what it uses, as it did before the crash.
When a resume refuses¶
A resume cuts off what a crash leaves and nothing more. A crash leaves a block that stops short, or zeros from a sector boundary to the end. A disk writes a 512-byte sector whole or not at all, so nothing else is torn. Anything else past the good blocks is damage: the resume refuses and leaves the bytes as they are. These cases refuse:
| Refusal | Why |
|---|---|
ResumeError::Damaged |
the bytes to cut hold damage, not a torn end: a whole block that fails its check |
ResumeError::GoodBlockAfterCut |
a good block lies past where the reader stopped, so cutting would lose it |
ResumeError::LookSpent |
the look for good blocks past that point ran out before the end of the bytes |
InputError::Layout, InputError::Producer |
the options name another layout or producer than the file |
The second case is the one to know. Suppose a sector of the disk went bad in the drive's file a few blocks before the
end, and then the power failed. The reader's scan stops at the bad sector, but good blocks follow it, so cutting there
would lose them. The resume names the first good block and refuses. The file still reads past the damage with
Recovery::Resync, as When bytes go bad explains.
A crash after the close needs no new data. A closed piece goes on after its close, and a close with nothing written after it adds no byte. A crash in the middle of the close opens the piece again, and the next close writes the end anew. The format reference tells a torn end from damage byte by byte and lists the bounds of the look.
Resuming a chain¶
The drive's run is long, so the recorder writes it as a chain of pieces in a directory, 00000000.wwt,
00000001.wwt and so on. The power failed while it wrote piece 2. On restart the recorder calls
chain::resume with the directory, the owner id, the chain number and its options.
The chain reads pieces 0 and 1 back from their tails and reopens 00000002.wwt as a single file resumes.
The recorder then goes on from the first block the chain lacks, exactly as before. Two crashes at a piece boundary
resume as well. One comes between closing a piece and creating the next, the other inside the head of the next.
| How the last piece ended | A piece per file | All pieces in one file |
|---|---|---|
| unclosed | resumed | resumed |
| closed naming a next piece: the crash came during a cut | the next piece starts | the next piece starts |
| closed, naming no next piece | written on after its close | written on after its close |
| its head is damaged | refused | refused |
A chain therefore gains a piece only at a cut. A recorder that opens the chain again goes on in its last piece.
Trying crashes in a test¶
A crash is easy to rehearse, because a resume works on any sink. A MemSink keeps a piece in memory,
and a test writes a piece there and copies a prefix of its bytes into a new sink. It resumes that sink with
TapeWriter::resume_on and gives the lost calls again. Repeat that for every offset of a small piece, and compare
each result with the piece written whole. The crate tests its resumes this way, and a
producer can test its own.
TapeReader.pub fn create(
path: &Path,
policy: SyncPolicy,
) -> Result<TapeWriter<FileSink>, WriteError<FsError>> {
let sink = FileSink::create(path).map_err(sink_err("creating a writer"))?;
Ok(TapeWriter::create_on(sink, policy))
}
A new file at path, created exclusively under its final name; fails if path is there (FileSink::create).
pub fn close(self) -> Result<Closed, WriteError<S::Error>> {
self.close_as(CloseReason::Closed)
}
Close the piece (CloseReason::Closed) and sync it.
Locked(PathBuf),
Another writer holds the file: one writer per file (ww_tape::sink).
TapeWriter.pub cut: Option<u64>,
Where the bytes stopped being good blocks, when a scan stopped before the end of the piece's stretch: a cut last block, damage, something that isn't a block, or a block of another session where no session may start (stale data behind the end of data).
can0 records, about a thousand per second.pub fn resume(
path: &Path,
options: WriterOptions,
) -> Result<(TapeWriter<FileSink>, Resumed), WriteError<FsError>>
Reopen the piece path to write on, see TapeWriter::resume_on.
pub fn next_seq(&self) -> u64 {
self.next_seq
}
Seq the writer gives the next block it places itself (Self::write_chunk, Self::defer_block): one after the last counted block written.
Resync,
Salvage: report a bad block and read on at the next block magic one byte after its start (FileError::Bad); no seq check, but a data block with the session and seq of one handed out before (of the last RESYNC_SEQS_MAX) and other bytes is a conflict (FileError::Conflict): it comes only in the report, which names both (a copy with the same bytes is no conflict).
pub fn resume(
dir: &Path,
owner: OwnerId,
chain: u64,
options: ChainOptions,
) -> Result<(PieceChain<FilePieces>, Resumed), WriteError<FsError>>
Reopen chain chain of owner in dir (with PieceTapes::One the file) after a crash, see
[PieceChain::resume_on]: from the lowest piece file there, past pieces removed on purpose.
pub struct MemSink<K = u64> {
tape: MemPiece<K>,
/// Where the piece being written starts in the bytes
base: u64,
/// The tape holds pieces back to back ([PieceStore::create_whole], [PieceStore::open_whole])
whole: bool,
}
pub fn resume_on(
mut sink: S,
options: WriterOptions,
) -> Result<(Self, Resumed), WriteError<S::Error>>
Reopen the unclosed piece in sink (cut off by a crash, or cut in its end) to go on writing it, see the
module docs; a closed piece is written on after its last close. options are the ones it was written
with.
TapeSink.pub enum SyncPolicy {
/// Only at the end: a crash may lose everything the OS hadn't written yet.
None,
/// After every N blocks.
EveryBlocks(u32),
/// When blocks are waiting and the last sync is this long ago, at a block or at [TapeWriter::tick].
Interval(Duration),
}
When a TapeWriter syncs the data to the disk, besides at TapeWriter::close.
Interval(Duration),
When blocks are waiting and the last sync is this long ago, at a block or at TapeWriter::tick.
EveryBlocks(u32),
After every N blocks.
None,
Only at the end: a crash may lose everything the OS hadn't written yet.
Damaged { offset: u64 },
The bytes a resume would cut hold a whole record at offset that fails its check, or a tail that doesn't end
the close before it: damage, not a torn end. Only an end the bytes stop inside of, or blank (zero) bytes, is
cut.
GoodBlockAfterCut { offset: u64 },
The resume would cut off a good block of the piece at offset, behind bytes the reader didn't take: more
than a torn end.
LookSpent { offset: u64 },
The resume looked for good blocks past where the reader stopped and ran out of its look
(RESUME_LOOK_MAX) or of the places it reads whole
(RESUME_PROBES_MAX) at offset, before the end of the bytes: it can't tell the
bytes from there on hold no good block, so it cuts nothing.
Layout { described: Layout, given: Layout },
The piece to resume describes another layout than the one given in the
WriterOptions, or a layout set after the first block.
Producer { described: String, given: String },
The piece to resume names another producer than the one given in the
WriterOptions.