Skip to content

fix(dash): stop /metrics hanging on an unchecked WAL backlog - #115

Merged
OsherElhadad merged 1 commit into
mainfrom
fix/metrics-per-tenant-hang
Aug 26, 2026
Merged

OsherElhadad merged 1 commit into
mainfrom
fix/metrics-per-tenant-hang

Conversation

@OsherElhadad

Copy link
Copy Markdown
Collaborator

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 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 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/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 / ~160MB, the
same order of magnitude observed on cg.db-wal): 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 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():

  • Once in Open(), so a restart pays down whatever backlog it inherited
    before serving any traffic, instead of deferring that cost to the first
    request or commit that happens to land on it.
  • Once every janitorPass (every pruneInterval, unconditionally — not only
    when Prune found something to delete), so the WAL never re-accumulates to
    this size while the process stays up between reclaim() runs.

Test plan

  • go build ./... and go vet ./... clean
  • go test ./... passes across the whole repo
  • Added TestOpenCheckpointsInheritedWAL and
    TestJanitorPassCheckpointsWAL — both fail against the pre-fix code
    (WAL stays large) and pass against the fix (checkpointed to near zero)
  • Built the proxy binary in an isolated worktree, pointed it at a
    synthetic (never production) database bloated to production scale,
    and confirmed with curl -m 15:
    - before this fix: http_code=000 size=0 after the full 15s timeout
    - after this fix: http_code=200 in well under a second

/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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant