Skip to content

fix(dash,proxy): stop /metrics and /api/stats+/facets piling up under load - #108

Merged
OsherElhadad merged 2 commits into
mainfrom
fix/dashboard-stats-hang
Aug 26, 2026
Merged

OsherElhadad merged 2 commits into
mainfrom
fix/dashboard-stats-hang

Conversation

@OsherElhadad

Copy link
Copy Markdown
Collaborator

Summary

  • /metrics held a lock across the entire Prometheus render, so any slow render serialized every concurrent scraper behind it instead of serving the cached body. Confirmed live: it went unresponsive for over a minute under real production traffic before this fix.
  • /api/stats and /api/facets had no caching at all. Overview() alone measured ~25s under real production write load, and stats() layers several more queries on top — so every dashboard load, auto-refresh, or extra open tab independently paid that cost. Added a 5s TTL cache keyed per (principal, query) to both routes, mirroring the /metrics fix.
  • This was the cause of "the dashboard is loading for minutes" reported on the live deployment.

Test plan

  • go build ./...
  • go test ./dash/... — full suite passes
  • go test ./proxy/... — full suite passes
  • Reproduced the /metrics hang live (60s+ with no response), verified fixed after redeploy (instant when cached, ~7s cold render, never queued)
  • Measured Overview() directly against the live production DB (~25s under real write load), confirming the caching layer is the right mitigation
  • Already built, tested, and redeployed to the live host; monitoring since

… load

/metrics held a single mutex across the whole render, so once one render
was slow every concurrent scraper queued up behind it rather than getting
the cached body — measured live: over a minute with zero response while
production traffic was flowing. Now the lock only guards the cache read
and write; the render itself runs outside it.

/api/stats and /api/facets had no caching at all. Overview() alone
measured ~25s under real production write load, and stats() runs several
more queries on top of it — so a dashboard auto-refresh, or two tabs open
on the same window, each paid that cost independently. Added a 5s TTL
cache keyed per (principal, query) to both routes, mirroring the /metrics
fix.

This is what was behind "the dashboard is loading for minutes."

Signed-off-by: Osher-Elhadad <[email protected]>
…ache-key bug

The 5s cache from the previous commit was masking symptoms, not the cause, and
had a bug that made it inert for the dashboard's own default view. Dug into why
Overview() itself measured ~25s on the live corpus:

- CompactionResets ran a correlated subquery whose outer filter (tokens_before
  > 0) matches ~100% of rows, so it isn't a filter at all — it degenerates into
  65k+ separate subquery invocations. Adding a covering index
  (idx_requests_session_tb) didn't move the needle: the cost was never the
  lookup, it was per-invocation overhead paid 65k times. Rewrote it as a window
  function (LAG), one sort instead of 65k correlated calls. Measured on a copy
  of the production DB: ~25s -> ~3.3s for the whole of Overview().

- SelfRemovals (stats()'s DeclCreditFor) joined into tool_declarations
  (974k rows) with no index on the join column, so SQLite full-scanned it and
  probed an automatic temp index per row. Added idx_tooldecl_session. Measured:
  ~8-36s -> ~5.6-6s.

- cacheKey compared the raw query string, but the UI re-stamps a relative time
  range ("last 24h") with a fresh millisecond since/until on every call — so the
  cache from the previous commit never hit for the dashboard's default view,
  the most common one. Round since/until to the cache TTL bucket instead.

Net: a cold /api/stats load drops from 25-60s+ to roughly 9-10s, and repeat
loads within 5s (auto-refresh, a second tab) now actually hit the cache instead
of silently missing it every time.

Signed-off-by: Osher-Elhadad <[email protected]>
@OsherElhadad
OsherElhadad merged commit c863519 into main Aug 26, 2026
3 checks passed
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Aug 26, 2026
OsherElhadad added a commit that referenced this pull request Aug 26, 2026
/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]>
Co-authored-by: Osher-Elhadad <[email protected]>
OsherElhadad added a commit that referenced this pull request Aug 26, 2026
/api/components had no cache at all -- PR #108 added the 5s TTL jsonCache
to /api/stats and /api/facets but missed this one, so the dashboard's
most-read tab re-ran all five of its queries (Components,
DecomposeComponentSavedUSD, EstimateComponentSavedUSD) cold on every load.
Measured on a corpus matching production scale (70k requests x 7
components, 490k request_components rows): ~4.4s per load, every time --
a dashboard tab left auto-refreshing, or two managers on the same window,
each paid that cost independently, exactly the "piling up under load"
pattern #108 already fixed everywhere else.

Added componentsCache alongside statsCache/facetsCache and wrapped
a.components the same way a.stats already wraps Overview.

Added TestComponentsCached, which does not even compile against the
pre-fix code (no componentsCache field) and passes against the fix.

Signed-off-by: Osher-Elhadad <[email protected]>
Co-authored-by: Osher-Elhadad <[email protected]>
OsherElhadad pushed a commit to OsherElhadad/context-guru-1 that referenced this pull request Aug 26, 2026
Resolves overlap with rossoctl#107 (manager-controlled keep-alive strategies,
already on main) and rossoctl#108 (dash/api.go, dash/schema.go caching/index
fixes). The predictor pipeline in this branch was built as a superset
of rossoctl#107's base feature, so every conflict resolves by keeping this
branch's version (already contains rossoctl#107's content plus the predictor
gate on top); rossoctl#108's caching and index additions land untouched
alongside it.

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