Repository navigation
fix(dash): stop /metrics hanging on an unchecked WAL backlog - #115
Merged
Merged
Conversation
/metrics was hanging for 15s+ with zero bytes returned, reproducing cold on a fresh restart with no other traffic. PR #108 fixed the promCache mutex and two slow queries, but a third mechanism was still live: dash/store.go only ever checkpoints the WAL from reclaim(), which only runs as a side effect of Prune() actually deleting rows on the byte-budget path -- a deployment inside its retention budget (this one) never takes it, so nothing ever checkpoints the WAL during ordinary operation. Left alone, the WAL grows until SQLite's own default wal_autocheckpoint (1000 pages) forces an inline checkpoint on an ordinary writer commit, paying off the WHOLE accumulated backlog inside that one commit -- modernc.org/sqlite gives no non-blocking WAL reads across it, so every reader sharing that *sql.DB (including /metrics's per-tenant query) blocks for the same span. Measured against a WAL bloated to the live scale (~40k pages): that single commit took 5.5s, and a concurrent reader blocked for 5.2s. A restart inherits whatever WAL the previous process left on disk (SQLite's close-time auto-checkpoint never got a chance to run against a systemd SIGTERM/SIGKILL), so the same cost lands again on the very first write after a cold start -- reproduced end-to-end with a real binary and curl: a fresh process opening a synthetically bloated WAL (matching production's ~150k-row scale) returned zero bytes after a 15s timeout, before this fix, and answered in under a second after it. Two checkpoints close the gap, both best-effort like the existing one in reclaim(): once in Open(), so a restart pays down whatever backlog it inherited before serving any traffic instead of deferring it to the first request or commit that happens to land on it; and once every janitorPass (dash/janitor.go), unconditionally, so the WAL never re-accumulates to this size while the process is up between reclaim() runs. Added TestOpenCheckpointsInheritedWAL and TestJanitorPassCheckpointsWAL, which fail against the pre-fix code (WAL stays large) and pass against the fix (checkpointed to near zero). Signed-off-by: Osher-Elhadad <[email protected]>
3 tasks done
This was referenced Nov 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
/metrics has been hanging for 15s+ with zero bytes returned, reproducing cold
on a fresh restart with no other traffic. PR #108 fixed the promCache mutex
and two slow queries (
CompactionResets,SelfRemovals), but a thirdmechanism was still live:
dash/store.goonly ever checkpoints the WAL fromreclaim(), which onlyruns as a side effect of
Prune()actually deleting rows on the byte-budgetpath. A deployment comfortably inside its retention budget — this one —
never takes that path, so nothing checkpoints the WAL during ordinary
operation.
Left alone, the WAL grows until SQLite's own default
wal_autocheckpoint(1000 pages) forces an inline checkpoint on an ordinary writer commit, paying
off the whole accumulated backlog inside that one commit.
modernc.org/sqlitegives no non-blocking WAL reads across it, so every reader sharing that
*sql.DB— including /metrics's per-tenant query — blocks for the same span.Measured against a WAL bloated to the live scale (~40k pages / ~160MB, the
same order of magnitude observed on
cg.db-wal): that single commit took5.5s, and a concurrent reader blocked for 5.2s.
A restart inherits whatever WAL the previous process left on disk (SQLite's
close-time auto-checkpoint never gets a chance to run against a systemd
SIGTERM/SIGKILL), so the same cost lands again on the very first write after
a cold start — which is why this reproduced on two separate fresh restarts
with zero concurrent traffic. Reproduced end-to-end with a real binary and
curl: a fresh process opening a synthetically bloated WAL (matching
production's ~150k-row scale) returned zero bytes after a 15s timeout before
this fix, and answered in under a second after it.
Fix
Two checkpoints close the gap, both best-effort like the existing one in
reclaim():Open(), so a restart pays down whatever backlog it inheritedbefore serving any traffic, instead of deferring that cost to the first
request or commit that happens to land on it.
janitorPass(everypruneInterval, unconditionally — not onlywhen
Prunefound something to delete), so the WAL never re-accumulates tothis size while the process stays up between
reclaim()runs.Test plan
go build ./...andgo vet ./...cleango test ./...passes across the whole repoTestOpenCheckpointsInheritedWALandTestJanitorPassCheckpointsWAL— both fail against the pre-fix code(WAL stays large) and pass against the fix (checkpointed to near zero)
synthetic (never production) database bloated to production scale,
and confirmed with
curl -m 15:- before this fix:
http_code=000 size=0after the full 15s timeout- after this fix:
http_code=200in well under a second