perf(host-service): save the usage index to disk so a restart resumes
KI
19d ago+338+338+012345678901234567890123456789-3-3-0123456789
Part 3 of 3, stacked on the incremental-reads PR. Split from #9500.
Summary
- Saves the usage index's parse state and SQLite watermarks to
$SUPERSET_HOME_DIR/usage/usage-cache.db, so a host restart (or an idle-reaped usage worker) resumes instead of rescanning. On a heavy machine that is 0.3 s instead of 11 s.
How It Works
SqliteStateStoreadds disk saves to the in-memory store. It writes at most every 5 minutes (and after the first scan), in one transaction, only at the end of a scan, so any saved row is a consistent point to resume from.- The cache is keyed by the host-service version and a format number, so each release starts a new one.
- A file SQLite reports as corrupt is rebuilt. A locked file (another host-service shares it) falls back to memory and is never deleted. A deleted key cannot reload from its old row.
- The directory is owner-only, because cached session titles come from prompts.
Testing
usage-store.node-test.ts: reopen, version reset, eviction, corrupt rebuild, leaving a locked file alone, write throttle, and a deleted key staying deleted. Added totest:integration:terminal.- On real data, a fresh process resuming from the cache gives the same per-agent totals and labels as an uncached scan.
Risks / Rollback
- Data on disk: about 50 MB on a heavy machine, plus its WAL. It holds prompt-derived session titles and workspace paths in an owner-only directory, with no expiry beyond the per-release reset.
- Shared file: two host-services of different versions sharing the cache would keep resetting it. That is slower, not wrong.
- Wrong numbers: a cache bug would persist until the next release, and the leaderboard publishes these numbers.
- Rollback: revert. Older builds ignore the file, and it can be deleted safely.
Summary by cubic
Saves the usage index's parse state and SQLite watermarks to $SUPERSET_HOME_DIR/usage/usage-cache.db, so a host restart or an idle-reaped usage worker resumes instead of rescanning. On a heavy machine that's 0.3 s instead of 11 s.
Things to weigh:
- Writes happen at most every 5 minutes and only at the end of a scan, in one transaction, so any saved row is a consistent state to resume from.
- The cache is keyed by host-service version and a format number; each release starts a new one, so a cache bug would persist until the next release.
- A corrupt file is rebuilt; a locked file (another host-service using it) falls back to memory and is never deleted; a deleted key can't reload from its old row.
- A corrupted or locked file falls back to memory, leaving the shared file untouched.
- The directory is owner-only because cached session titles come from prompts; disk usage is about 50 MB plus WAL on a heavy machine, with no expiry beyond the per-release reset.
- Rollback is a revert: older builds ignore the file, and it can be deleted safely.