fix: keep startup fast regardless of history size - #18
Merged
Merged
Conversation
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.
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.
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:
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.