Expand description
Record framing for the log-first storage substrate. This is the
write-ahead log: crate::log_store appends
these framed records to segment files, and that durable append is the
commit’s ack boundary (not the buffered redb materialization). See the
manual’s docs/manual/architecture/storage-engine.md.
Every record (an expanded op-set, optionally paired with its originating
CommitBatch — encode_ops_record) is wrapped in a fixed
header with magic, format version, shard id, LSN, payload length, and a
CRC32C-style checksum. This lets replay/export/CDC distinguish valid old
data, valid new data, partial new data, and corrupted data instead of
hoping the filesystem write was atomic.
Greenfield: only the framed format is read or written. There is no legacy
unframed format to migrate. An in-memory store (no WAL file) instead keeps
a row-based command journal — the one remaining user of the bare
__log__/<lsn> row format.
Structs§
- Chain
Assembler - Reconstructs a (possibly chained) commit from consecutive framed WAL
records, fed one at a time in physical log order. The overwhelmingly
common case — a
BARE/WITH_COMMANDrecord — reconstructs immediately. A chained commit’sCHAIN_CONTINUEfragments are buffered until the matchingCHAIN_FINALis fed, at which point the whole op-set is returned — in full, exactly once, never partially. This is the one reconstruction implementation crash recovery and replication apply both use, so there is never a second, potentially drifting copy of this logic. - Checkpoint
- A published durability checkpoint: the durable LSN (header), the GC floor, and the wall-clock time the checkpoint was published. The record’s frame checksum makes a torn/partial checkpoint fail verification on load.
- Snapshot
Manifest - Manifest header for a framed snapshot stream (W6, D9/P17). Written as the
first framed chunk; consumers validate
magic/format_versionbefore reading tenant chunks.tenants_crccovers the concatenated tenant chunks, so a torn/corrupted stream is rejected at restore time. - Tenant
Chunk - One tenant’s framed backup chunk: scope (
TenantIdbytes) + JSONL payload. - WalHeader
- A decoded logical WAL record header.
- WalSegment
Manifest - Manifest describing one exported WAL segment range. Written first in a segment stream so a consumer can validate the range before reading records.
Enums§
- Chain
Feed - Result of feeding one record to a
ChainAssembler. - WalDecode
Error - Error returned when a logical WAL record fails to decode or verify.
Constants§
- SEGMENT_
MAGIC - Magic identifying a WAL segment manifest.
- SNAPSHOT_
MAGIC - Framed snapshot export magic: distinct from the WAL segment magic so a restore can reject a mismatched artifact type immediately.
- WAL_
FORMAT_ VERSION - Current logical WAL framing format version.
Functions§
- decode_
checkpoint - Decode a checkpoint from a framed WAL record.
- decode_
last_ applied - Decode the
LAST_APPLIEDwatermark row. - decode_
logical_ body - Decode a
ChainFeed::Completelogical body into its op-set and optional command. Kept separate fromChainAssembler::feed(seeChainFeed::Complete’s doc) so framing/chain-structure completeness and content validity are never conflated. - decode_
op_ set - Decode an op-set encoded by
encode_op_set. - decode_
ops_ record - Decode a framed op-set record, verifying the frame checksum. The returned
header carries the routing shard and LSN; the
CommitBatchisSomeonly for a record encoded with one (every real client commit through the normal publish path — seeencode_ops_record). - decode_
tenant_ chunk - Decode a tenant chunk body (the inverse of
encode_tenant_chunk). - decode_
wal_ record - Decode and verify a framed record; returns the header and payload.
- encode_
chained_ ops_ record - Split
ops(optionally paired withcommand_payload) into one or more framed WAL records at(shard_id, lsn), each a complete, independently checksummed frame whose total length never exceedsmax_record_bytes. All returned records share one LSN; only the last is tagged [OPS_RECORD_KIND_CHAIN_FINAL] (or, when everything already fits in one record — overwhelmingly the common case — the single returned record is byte-identical toencode_ops_record’s ordinaryBARE/WITH_COMMANDoutput). When chaining is needed, the logical body (op-set plus optional command, laid out exactly likeWITH_COMMAND’s own format) is cut at arbitrary raw byte boundaries — never aligned to op or command boundaries — which is what lets a command payload of any size be chained too, not just the op-set: nothing is decoded fragment-by-fragment, only once every fragment of the chain is reassembled (seeChainAssembler). - encode_
checkpoint - Encode a checkpoint as a framed WAL record (LSN carried in the header, floor + timestamp in the payload).
- encode_
last_ applied - Encode the
LAST_APPLIEDwatermark row (a framed record with an empty payload carries the LSN in the header). - encode_
manifest - Encode a manifest as JSON (used as the first framed chunk of a stream).
- encode_
op_ set - Encode an expanded op-set as a compact binary WAL payload: an op count
followed by
(tag, key, [value])entries. This is the log-first WAL’s record content (decision B): the log stores state transitions, so recovery applies them directly and needs no expander. - encode_
ops_ record - Encode an expanded op-set as a framed WAL record at
(shard_id, lsn), optionally paired with the serializedCommitBatchthat produced it (carrying the command in the WAL record is what makes the command journal —commit_keyper LSN — fully derivable from the WAL, closing the__log__redb row that used to be the only source of it).command_payloadis the batch pre-serialized by the caller (already computed once for the outcome-payload row), not re-serialized here. - encode_
tenant_ chunk - Encode a tenant chunk: 4-byte tenant length, tenant bytes, JSONL bytes. The whole chunk is written as one framed chunk (CRC-verified on read).
- encode_
wal_ record - Encode one logical record into a framed, checksummed byte vector.
- now_ms
- Wall-clock now in milliseconds since the Unix epoch (used for retention lease deadlines and checkpoint publication timestamps).
- op_
set_ encoded_ len - Exact byte length
encode_op_setwould produce forops, computed without allocating — used for cheap pre-commit size checks (deciding whether a commit needs chaining, or exceeds the configured transaction ceiling entirely) without building the buffer just to measure it. - read_
framed_ chunk - Read one framed chunk written by
write_framed_chunk. - read_
snapshot - Read one framed snapshot stream, verifying the manifest (magic + version)
and every tenant chunk CRC. Returns the manifest and decoded tenant chunks.
Truncated or corrupted input is rejected: if the stream ends before
tenant_countchunks are read, or a chunk fails its CRC, this errors. - read_
snapshot_ manifest - Read + validate only the snapshot manifest (magic/version), so a caller can stream the tenant chunks one at a time instead of materializing them all.
- read_
wal_ segment - Read one WAL segment written by
write_wal_segment, verifying the manifest and every record checksum. Returns(manifest, records). - write_
framed_ chunk - Framed-stream writer for export/backup: emits
{len}{crc}{bytes}chunks so a consumer can detect truncation/corruption without reading the whole artifact. (P17 foundations.) - write_
snapshot - Write one framed snapshot stream: manifest chunk, then one framed chunk per tenant (each independently checksummed; the manifest carries a CRC over all tenant chunks). Returns the manifest.
- write_
wal_ segment - Write one full WAL segment: manifest chunk, then one framed chunk per record (each chunk independently checksummed; the manifest also carries a CRC over all records). Returns the manifest.