Repository navigation
fix(usage): cap the usage queue so a backlog cannot evict the Redis cache - #14193
HarshMN2345 wants to merge 4 commits into
Conversation
…ache The usage list and the document cache share one Redis. Official Compose runs that instance at 512mb with allkeys-lru, so a v1-stats-usage backlog fills it and evicts cache keys. Logical databases do not isolate maxmemory, so the publisher stops enqueueing once the pending list reaches _APP_STATS_USAGE_QUEUE_MAX (default 8192). Fixes #14084
Security rulesNo new WARNING or ERROR findings from security rules. 32 existing findings tracked in
|
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
8192 jobs is only a few seconds at the stats-usage drain rate, so a brief worker stall discarded metrics that still fit in Redis. 32768 jobs is about 64MB at the measured ~2KB payload, under the ~240k jobs that fill the 512MB instance. Only -1 removes the cap. Depth is checked again after the queue drains, and a Redis error on that read is logged and does not escape.
Rector's PHPUnit set rewrites count() comparisons on the published-event list. The Checks job runs that dry-run over tests/.
|
@hansi-codes review |
🟢 Tier S · Ready to merge
Adds a configurable pending-job cap to usage publishing, defaulting to 32,768, with rate-limited warnings when metrics are dropped and best-effort handling of depth-read failures. Documents and forwards the setting through Compose and adds unit coverage for limits, recovery, failures, and disabled statistics.
📂 Walkthrough · 5
✅ Fixed since the last review · 1
Reviewed |
Separate workers and MQTT call Usage::enqueue() with their own environment lists, so an operator's _APP_STATS_USAGE_QUEUE_MAX never reached them. Pass the variable through on MQTT and the webhooks, deletes, builds, jobs, functions, and messaging workers.
|
@hansi-codes review |
|
Superseded by #14112, which fixes the root cause without dropping usage jobs. |
What does this PR do?
Stops a
v1-stats-usagebacklog from evicting the document cache. Cache, locks, abuse, pub/sub, and every queue share one Redis. Official Compose runs that instance with--maxmemory 512mb --maxmemory-policy allkeys-lru. Each API request enqueues one usage job of about 2KB onutopia-queue.queue.v1-stats-usage. Under write load the list grows faster thanworker-stats-usagedrains it (about 1,000 jobs/s). In the report, 150,396 jobs occupied 391MB of the 512MB limit and Redis had evicted 7.2M keys.allkeys-lrucan also evict queue lists, which drops every job in them.This is maxmemory eviction on a shared instance, not a key collision. Queue keys (
utopia-queue.queue.*) and cache hashes do not overlap. Another Redis logical database would not help:maxmemoryand eviction are instance-wide.Usage::enqueue()refuses another job once the pending list reaches_APP_STATS_USAGE_QUEUE_MAX(default 32768, about 64MB). That is well under the roughly 240k jobs that fill the 512MB instance, and it still absorbs a short worker stall instead of dropping metrics after a few seconds. Further metrics are dropped, with a warning at most once a minute per process that includes the current depth and the limit. Publishing resumes as soon as the worker drains the list below the cap. A Redis error while reading depth is logged and does not escape the request. Usage writes are already best-effort.Set the variable to
-1to leave the list uncapped. Any other value that is not a positive integer, including0, keeps the default. The variable is documented inapp/config/variables.phpand forwarded, with no inline value, on every process that callsUsage::enqueue(): the API, realtime, MQTT, the combined worker, the scheduler, and the separate webhooks, deletes, builds, jobs, functions, and messaging workers. Services that extend the combined worker or scheduler inherit it. Containers that do not receive it still apply the code default.Fixes #14084
Test Plan
vendor/bin/phpunit tests/unit/Event/Publisher/UsageTest.php(14 tests): explicit and default caps,-1uncaps, other negatives /0/ non-numeric values keep the default, publishing resumes after the queue drains, a depth-read failure and a publish failure are swallowed, and disabled usage stats never touch the queue.vendor/bin/pint --testand PHPStan on the touched PHP files.tests/unit/Docker/Compose/GeneratorTest.phpstill parsesdocker-compose.yml.npx prettier --check docker-compose.yml.Related PRs and Issues
allkeys-lruinstance.Checklist