Skip to main content

Module wal

Module wal 

Source
Expand description

The write-ahead log, which exists to decouple the acknowledgement from the seal.

§Why this exists, given docs/architecture/ingest.md section 4 says “No WAL”

section 4’s argument is about recovery, and it is still correct: a block directory is renamed into place atomically, so there is no torn state and nothing for a log to replay. This log is not for recovery. It is for latency.

Before it, an export was acknowledged only after the block containing it was durably published, so under light load the caller waited out max_block_age — a measured p50 of 657 ms and p99 of 2,647 ms (section 11). The block is sealed on a timer because nothing else bounds how long a half-filled block sits there, and that timer became the caller’s latency. With the log in front, the seal triggers are unchanged and nobody is waiting on them: max_block_age goes back to being a statement about the shape of blocks on disk rather than a latency bound.

§The durability this buys, stated precisely

An append is acknowledged once write(2) has returned — the bytes are in the kernel’s page cache, not on the platter. That survives everything that kills the process: a panic under panic = "abort", SIGKILL, the OOM killer, a flusher that took the process down with it (section 1). It does not survive power loss or a kernel panic, because the page cache does not.

This is a deliberate choice and it is the reason there is no fsync on the acknowledgement path. On the machine section 11 was measured on, one F_FULLFSYNC costs 4,230 us, so a durably-fsynced ack could not have a p50 below 4.2 ms, let alone a p99 under 5 ms. The same trade is Kafka’s acks=1 and ClickHouse’s default. Wal::sync exists and is called on a timer by a background task, which bounds how much is exposed to a power cut to one sync interval — it is never called by an appender.

There is deliberately no userspace buffering. A BufWriter would batch the syscalls, but bytes sitting in a Vec in this process do not survive the process dying, which is exactly the failure this log is claiming to cover. That argument still holds and is the reason a buffer is not the fix below.

§The append rate is not the ceiling; the serialised section is

This paragraph used to finish the one above by pricing the syscall: one write(2) per export “at a few tens of microseconds”, against 188 appends per second at the thirty-two-connection plateau, and concluded that “three digits of appends per second is not a rate a syscall per append can be the ceiling of”. Measured, it is off by two orders of magnitude and it was the wrong quantity.

crate::diag times the wait and the hold separately. At the plateau wal.write means 2.1–2.6 ms, not tens of microseconds, and wal.held — the whole critical section — means 2.2–3.0 ms, of which the write is ~95%. At 250–330 appends/s that is 73–78% occupancy of one mutex shared by all three signals and every shard, and wal.lock_wait is 77–89% of the whole of submit in every configuration measured. The arithmetic above omitted hold time and the fact that the lock is global, which together are the entire effect.

Two consequences worth stating plainly. Mutex here resolves to the pthread backend, so a contended lock() parks the OS thread in the kernel — a parked tokio worker runs no other task and is not replaced, which is why wal.inflight_max comes out equal to the worker count exactly, and why a task that only sleeps 50 ms returns 110 ms late at 96 connections. And adding workers does not help: 12 to 48 moved throughput 2% and multiplied wal.lock_wait by six, because the ceiling is the serialised section rather than scheduling capacity.

§…but the serialised section is not the ceiling either

The paragraph above says where the time goes and it is right. It was then read as saying what the rate is, and that does not follow: a queue forms at whatever is slowest to acquire, which need not be what is slowest to finish. Both fixes it proposed were priced before either was believed.

One log per signal was built, measured against the binary it replaces, and rejected: nine paired passes at 4/32/96 connections across three sittings, records/s signs split at every shape. It moves wal.lock_wait — 0.63x at four connections, 0.795x at thirty-two — and gives the whole of it back in wal.write, 1.94x and 2.39x at thirty-two and ninety-six with all nine passes agreeing. The volume does not absorb a second appender: two concurrent writers at this frame size return 0.98x the aggregate bandwidth of one and three return 0.86x, measured with no Mira code in the loop. The diff is kept on the wal-per-signal branch rather than deleted.

Group commit needs no separate arm, because the envelope was measured directly: put the log on a RAM disk, change nothing else, and wal.write falls 89% to 0.245 ms, wal.held 83% to 0.395 ms and wal.lock_wait 93% to 1.245 ms — the serialised section effectively deleted — for 1.096x at thirty-two connections (3 of 3 passes) and 1.005x at ninety-six, signs split. A perfect log fix is worth ten percent at one shape and nothing at the other, and group commit writes the same bytes down the same fd.

What that run shows instead is where the queue re-forms once the log is free: submit.admit, 0.000–0.002 ms in the disk-backed dumps and ruled out there on that basis, becomes 44.05 ms of a 47.91 ms submit.total, 92%. Admission blocks when no flusher queue slot frees, so the constraint behind the log is the block seal and publish path — the same device, writing Arrow IPC.

ponytail: one global log with one mutex. The ceiling that matters is not this file’s: wal.lock_wait is the largest term in submit and still worth ~10% at most, so the upgrade path here is bytes — the re-encode below is 790 KiB a frame and compressing or eliding it is the only lever with room — and the upgrade path for ingest is the flusher, not the log. Do not re-propose group commit or a log per signal without a number that contradicts the two paragraphs above; see docs/architecture/performance.md section 11.

§The frame is the OTLP request, in its canonical protobuf encoding

Not the bytes off the wire. Mira has three ways in — gRPC, protobuf over HTTP, and KYAML over HTTP — and only one of them still has bytes by the time anything could log them: tonic decodes before the handler is called, and a KYAML body is not protobuf at all. So the frame body is encode_to_vec of the decoded request, which normalises all three transports to one format with one decoder on the replay side.

That costs a re-encode. Measured on an 8,192-record log export (1.29 MiB): encode 1.49 ms at 864 MiB/s, against the decode already in the path at 5.29 ms and 244 MiB/s. section 11 measured the whole engine at 190.6 MiB/s, a 6.8 ms budget for that export, so the log adds about 22%. The alternative — a custom tonic Codec to keep the wire bytes — buys that 22% back for a codec Mira then owns forever, which is the wrong side of principle 1’s trade until something measures it as the bottleneck.

Replay feeds the decoded frame through the same ingest::{logs,traces, metrics} the network path calls, so there is no second decode path to write, to test or to keep in step with the first.

┌────────┬─────┬────────┬─────┬─────────┬────────┬──────────┬────────┐
│ magic  │ ver │ signal │ pad │ seq     │ len    │ body     │ crc32  │
│ 4 B    │ 2 B │ 1 B    │ 1 B │ 8 B     │ 4 B    │ len B    │ 4 B    │
└────────┴─────┴────────┴─────┴─────────┴────────┴──────────┴────────┘
 └──────────────── covered by the CRC ──────────────────────┘

The CRC covers the header as well as the body, so a corrupted length is caught by the checksum rather than by whatever it would otherwise index into. That is the same lesson as the block footer: a length read out of a file is attacker-controlled-equivalent, and validating it against the file’s real extent is not optional.

§Recovery, and why there is still no manifest

Replay needs exactly one fact — which frames are already inside a published block — and the block directory carries it, so principle 4 survives intact. Block names gain a fifth field, wal_hi: an exclusive watermark, meaning every sequence of that signal below it is inside some published block. It is not this block’s own maximum, because sibling shards are filling their own blocks from the same log — it is what Wal::watermark_for answers, the lowest still-unpublished sequence this block does not hold, or one past everything the log has handed out when this block holds them all. Boot recovery therefore stays what section 4 says it is: a readdir, the same one the read path already does, with no extra I/O and no metadata store to keep consistent. Each signal keeps its own watermark, and that costs nothing because an OTLP export belongs to exactly one signal — /v1/logs, /v1/traces and /v1/metrics are three endpoints.

§What this breaks, and what pays for it

Read-your-writes. section 11 notes it was free, and it was free because the ack waited for the publish — the same rename made the data durable and visible at once. A caller acked here and querying immediately would not see its data until the block seals, which is a latency the log was built to remove.

The repair is the open-block query surface: crate::query::search_open takes the flusher’s in-progress builder as a snapshot and scans it alongside the sealed blocks, so read-your-writes holds with the log on or off. It is not free — that snapshot is the one allocation the read path makes — and section 7.6 is why it is paid there rather than in the scan.

Structs§

Frame 🔒
FrameReader 🔒
Reads frames out of one segment, stopping at the first that is not whole.
Inner 🔒
Replayed
What a Wal::replay did, for the line it gets logged on.
Wal
An append-only log of OTLP export bodies.

Enums§

Signal
Which signal a frame belongs to.

Constants§

CRC_LEN 🔒
HEADER_LEN 🔒
MAGIC 🔒
MIRAWAL0, truncated. Present so a stray file in the WAL directory is rejected by name rather than parsed as a frame.
MAX_FRAME_BYTES 🔒
The largest frame that will be written or believed on read.
SEGMENT_BYTES 🔒
Roll to a new segment past this size. Segments are the unit of deletion, so this trades how much dead log is retained past its watermark against how many files the directory holds. At section 11’s measured 190.6 MiB/s a segment is about a third of a second of ingest.
WAL_VERSION
Bumped when the frame layout changes. A reader that does not recognise a version refuses the segment rather than guessing at its shape, which is the same rule the block format follows.

Functions§

lock_node 🔒
Take this node’s writer lock under dir, or refuse to start.
read_exact_or_eof 🔒
true if the buffer was filled, false on a clean EOF before any byte.

Type Aliases§

Watermarks
One past the last published sequence, per signal, indexed by Signal::index. Assembled from the block directory listing at boot; see the module docs.