Skip to main content

Module journal

Module journal 

Available on crate feature tokio only.
Expand description

File-based persistence for an MQTT session.

This module is a thin I/O driver: it owns the journal file handle and shuttles bytes between the file and the sans-io types from crate::session::journal. All framing, CRC validation, op serialisation, and snapshot logic live there.

tokio would dispatch each individual async filesystem call to its blocking thread pool anyway, so each logical operation here runs its whole sequence of std::fs syscalls inside one [tokio::task::spawn_blocking] call rather than bouncing back to the runtime between syscalls.

Β§File layout

A journal file starts with an 8-byte header naming the format version, followed by CRC-framed records of session operations. On startup the file is replayed frame-by-frame via crate::session::journal::Reader. If the tail is corrupt or truncated, the file is truncated to the last valid frame boundary.

If the header is missing or unsupported (including a pre-header journal), the file cannot be interpreted at all: it is renamed aside as a .corrupt-<unix millis>-<seq> backup and a fresh session is returned. A frame that decodes to malformed ops is a different problem. It is an encoding or decoding bug, not on-disk corruption. It is reported as a load error instead of being silently discarded.

Compaction writes a fresh header and snapshot to a temp file and atomically renames it over the live journal.

ModulesΒ§

backup πŸ”’
Backing up unreadable journal files as .corrupt-* siblings and pruning old backups.
file πŸ”’
Low-level journal file primitives: the append handle, write-rollback guard, and the fsync/truncate/temp-path helpers built on std::fs.
read πŸ”’
Loading a session by replaying a journal file frame by frame.

StructsΒ§

Writer πŸ”’
Append-only writer for a journal file with compaction support.

EnumsΒ§

CompactOutcome πŸ”’
Result of Writer::compact’s blocking work, distinguishing whether the rename that makes the new file live was reached: that is the point past which the writer’s old handle is stale and must be replaced (or, on failure to do so, discarded) rather than reused.

FunctionsΒ§

blocking πŸ”’
Runs a blocking filesystem closure on tokio’s blocking pool, flattening a task-join failure (only possible on panic) into an I/O error.