Skip to content

fix: keep startup fast regardless of history size - #18

Merged
masseselsev merged 1 commit into
masterfrom
fix/fast-metric-backfill
Sep 18, 2026
Merged

masseselsev merged 1 commit into
masterfrom
fix/fast-metric-backfill

Conversation

@masseselsev

Copy link
Copy Markdown
Owner

After the 0.3.34 upgrade the router's MikroMan took >25 min to serve anything: the one-off bucket backfill blocks startup, and its window predicate (a strftime/cast expression) is not index-sargable, so every day-chunk rescans the whole raw table.

Two changes:

  • backfill moved to a background goroutine (HTTP listener no longer waits for it; the raw-fallback in the chart handlers already covers 'buckets not built yet'); retention's first pass is chained after it in the same goroutine to avoid the prune/backfill race.
  • sargable text bounds added in front of the epoch cast; the cast remains the exact predicate, so selection semantics are unchanged (bucket/raw equivalence tests pass).

Measured on a synthetic 518k-row month: chunk scan 153ms -> 88ms in RAM; the effect on flash-backed SQLite I/O is the real point.

The one-off metric-bucket backfill ran on the startup path before the HTTP listener, so an upgraded install with 30 days of raw samples served no requests until it finished — over 25 minutes on a router-class device. It now runs in a background goroutine; chart handlers keep reading raw tables until each range's buckets exist, so the service answers within seconds of a restart. The retention loop's first pass waits for the backfill to finish in the same goroutine: pruning raw rows while the backfill consumes them would leave an unrecoverable hole in the middle of the history.

The bucket window predicate also gained sargable text bounds (timestamp >= datetime(?, 'unixepoch')) in front of the epoch cast: the cast alone is not index-usable and forced a full-table scan per backfill chunk. The cast stays as the exact predicate; the text bounds only prune through the timestamp index, so row selection is unchanged.
@masseselsev
masseselsev merged commit 6fea0b2 into master Sep 18, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant