Repository navigation
Report how many tasks are parked behind concurrency limits - #618
Conversation
❌ 1 Tests Failed:
View the top 1 failed test(s) by shortest run time
To view more test analytics, go to the Test Analytics Dashboard |
chrisguidry
left a comment
There was a problem hiding this comment.
Thanks for this, the gap is real and the index is a good way to avoid a SCAN. The script changes look right to me and they are cheap: one SADD or SREM per park, release, scavenge, or cancel. My one concern is what the heartbeat does with the index.
The heartbeat's cost grows with the number of backlogged keys, times the number of workers
parked_depth runs SMEMBERS and then one XLEN per indexed stream, in every worker, on every heartbeat (2 seconds by default). With a single ConcurrencyLimit("task") that is one XLEN, which is nothing. With a per-tenant limit it is one XLEN per tenant that has a backlog. At 10,000 backlogged tenants and 100 workers, that is about 500,000 XLENs per second and 100 SMEMBERS replies of 10,000 names every 2 seconds, and all 100 workers compute the same number. In pydocket the XLENs also go through redis.pipeline(), which defaults to MULTI/EXEC, so Redis runs each batch of 10,000 without serving anyone else.
Per-tenant limits with deep backlogs are where this gauge is most useful, so I'd like the heartbeat's cost not to depend on how many keys are backlogged.
Alternatives
1. Keep a running total, corrected by per-stream counts. Make the index a hash of stream -> parked count in place of the set, and add a total at its own key. Each script already ends by reading XLEN waiters_stream. At that point it would:
local now = redis.call('XLEN', waiters_stream)
local before = tonumber(redis.call('HGET', waiter_index, waiters_stream) or 0)
if now == 0 then
redis.call('HDEL', waiter_index, waiters_stream)
else
redis.call('HSET', waiter_index, waiters_stream, now)
end
redis.call('INCRBY', parked_total, now - before)The heartbeat becomes one GET. This answers the objection to a plain counter: the scripts never increment or decrement by one, they write the stream's real length and move the total by the difference, so a count that is wrong for a key is right again the next time any script runs on that key. The cost is three O(1) commands per script call.
What it does not fix on its own: a worker on an earlier release that drains a stream leaves that stream's count in the hash, and if nothing ever parks on that key again, the total stays high. That needs option 2's pass as a slow reconciler, or we accept it as a rollout-only error and say so.
2. Keep this PR's set, but count once for the fleet. One worker at a time takes a short lease, the way the redelivery sweep does with {docket}:leases:redelivery-sweep, walks the index with SSCAN in batches outside a transaction, and writes the sum to a key. Every heartbeat reads that key with one GET. The scripts stay as they are in this PR. The cost drops from (backlogged keys x workers) per heartbeat to (backlogged keys) per lease interval, and the interval can be longer than a heartbeat. The gauge is as stale as the interval.
3. Both. Option 1 for the number every heartbeat reads, and option 2's leased pass at a long interval (a minute or more) to rebuild the hash and the total from the stream lengths. This is the most code, and it is the only one that is O(1) per heartbeat and also recovers from any drift, including mixed releases.
My preference is option 1 now, with the reconciling pass as a follow-up if mixed-release drift turns out to matter. Option 2 is the smallest change from what is here, if you'd rather not touch the scripts again.
Two smaller things in the same code
- The heartbeat removes a drained stream's name with a plain
SREMafter itsXLEN, and a task can park on that stream in between. On a busy key withmax_concurrent=1the stream goes between 0 and 1 entries constantly, so this is not only a mixed-release case. Options 1 and 3 remove thatSREMfrom the heartbeat entirely. With option 2, the leased pass can do the check and the removal in one small script. - If
parked_depthraises, the heartbeat skips settingdocket_queue_depthanddocket_schedule_depthfor that beat, in both pydocket and docket-rs. Setting those two before counting the parked tasks keeps the existing gauges independent of the new one.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2a546dd3b2
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| local indexed = redis.call('ZCARD', waiter_index) | ||
| if indexed > 0 then |
There was a problem hiding this comment.
Discover waiter streams created by older workers
During a rolling upgrade, an older worker can create a new :waiters stream without adding it to waiter_index; because this script only examines streams already present in that index, every new worker reports docket_parked_depth as zero for that backlog until a new-version acquire/release/scavenge happens to touch the same limit. If the safeguard and holder are also handled by old workers, the undercount can last for the waiter's entire lifetime, contradicting the documented claim that each worker reports waiters across every limit; add a migration/discovery path for previously unindexed streams or otherwise account for this mixed-version case.
AGENTS.md reference: AGENTS.md:L95-L103
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
No change. The case is real: a task that an earlier release parks on a stream this release has not recorded is not counted until a script from this release runs on that stream. Finding such a stream needs a keyspace SCAN, which this change avoids. The undercount ends with the rollout. Once every worker runs this release, the next release, safeguard, cancel, or park on that limit records the stream. The PR description lists this under rollout caveats.
chrisguidry
left a comment
There was a problem hiding this comment.
This addresses everything from my earlier review, and I'm approving it. The scripts keep the total, the heartbeat's cost no longer depends on the number of backlogged keys, the SREM race is gone, and both heartbeats report the queue and schedule depths before they read the parked depth. I agree with leaving the mixed-release undercount as a rollout caveat.
I ran 3,000 tasks through a 20-slot worker against Redis on this branch and on its merge-base, two rounds each: tasks with no limit, tasks behind one ConcurrencyLimit(2) key, and tasks spread over 500 keys. Throughput is the same within run-to-run noise in all three (about 2,100-2,400, 450-500, and 1,170-1,280 tasks per second).
The branch conflicts with main in loq.toml, on the worker.py and docket.py baselines. Since that needs a push anyway, here are four small things I'd like in the same push. None of them changes the design.
- pydocket has no test that a failed parked-depth read leaves the other two gauges reported. docket-rs has
a_heartbeat_fails_when_it_cannot_read_the_parked_total. In pydocket, moving theparked_depthcall aboveQUEUE_DEPTH.setwould still pass the suite. - docket-rs has no test that sums two waiter streams. pydocket has
test_depth_sums_every_waiter_stream; every docket-rs test uses one concurrency key. - The two heartbeats advance the cursor differently after a failure. docket-rs adds
PARKED_DEPTH_BATCHafter every beat. pydocket adds it only after a successful read, so a batch that fails every time is retried on every beat and the gauge stops updating. Please make pydocket match docket-rs. parked_depth.luareturns the stored total unchanged when the index is empty. If the total is ever non-zero while the index is empty (one key deleted or evicted without the other), the gauge overcounts and no pass corrects it. Treating the total as 0 whenZCARDreturns 0 fixes that.
Two notes that need no change here: the comment on waiter_index_key says the indexes: prefix keeps the keys clear of task keys, but a task key can start with indexes: (the same is true of leases:); and UNRELEASED_METRICS in the conformance driver will need emptying after the next pydocket release.
2a546dd to
5569e7a
Compare
A task that cannot get a ConcurrencyLimit slot leaves the main stream for a per-key waiter stream, so docket_queue_depth and docket_schedule_depth count none of the backlog behind a limit. The new docket_parked_depth gauge reports it on each heartbeat, in pydocket and docket-rs. The concurrency scripts keep a set of the waiter streams that hold a task, under the docket's prefix. The heartbeat sums the length of each stream in the set, so the value comes from the streams themselves and corrects itself. A single counter was the alternative; one missed decrement would have left it wrong for good. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The Rust coverage gate takes each function's best-covered copy, and no one test binary reached all three error branches of parked_depth. The integration tests now reach each of them. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The heartbeat ran one XLEN for each backlogged concurrency key, in every worker, so its cost grew with the backlog the gauge exists to show. The index is now a hash of each waiter stream's length, and the scripts move a total by the difference each time they run on a stream. The heartbeat reads the total with one GET, after it reports the other depths. The scripts write the stream's real length, not an increment, so a wrong count for a stream is right again after any script runs on it. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
On a slow machine the worker took its first task more than the 200 ms redelivery timeout after the test seeded the slot. The slot was stale by then, so the task took it and never parked. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A worker on an earlier release can drain a waiter stream without running a script that keeps the count. If nothing parked on that stream again, its count stayed in the total for good. The index is now a sorted set of waiter streams, scored by their counts, so that it can be read by rank. parked_depth.lua replaces the heartbeat's GET. It compares 16 of the indexed streams with their counts, corrects the total, and returns it. Each worker advances its cursor by 16 on each heartbeat. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
pydocket advanced its parked-depth cursor only after a successful read, so a batch that failed every time was retried on every beat and the gauge stopped updating; it now advances on every beat, as docket-rs does. parked_depth.lua returned the stored total when the index was empty, so a total left behind by an evicted or deleted index overcounted for good; an empty index now means a total of zero. Each implementation gains the test the other already had: pydocket checks that a failed parked-depth read leaves the queue and schedule depths reported, and docket-rs checks that the depth sums two waiter streams. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5569e7a to
17336d7
Compare
The problem. A task that cannot get a
ConcurrencyLimitslot leaves the main stream for a per-key waiter stream.docket_queue_depthanddocket_schedule_depthdo not count it. A docket with thousands of tasks waiting behind a limit reports a near-empty queue.The fix. A new gauge,
docket_parked_depth, reports the number of tasks on waiter streams, in pydocket and docket-rs.The concurrency scripts keep the number. A sorted set at
{docket}:indexes:concurrency-waitersholds each waiter stream, scored by its length, and{docket}:indexes:concurrency-waiters:totalholds the sum. Each script ends by writing its stream's real length and moving the total by the difference.Each heartbeat runs one new script,
parked_depth.lua, after it reports the other two depths. The script compares 16 of the indexed streams with their counts, corrects the total, and returns it. Each worker advances its cursor by 16 on each heartbeat. The cost of a heartbeat does not depend on the number of backlogged keys.That pass corrects a count that a worker on an earlier release left wrong. It replaces the leased pass from the review: neither in-memory engine has
HSCAN, and a sorted set reads by rank with commands they both have. For the same reason the scripts useGETandSETfor the total, notINCRBY.The conformance driver checks the gauge for presence only, and only for working-tree implementations, because 0.29.1 does not emit it.
Rollout caveats
no-op: workers on an earlier release ignore both keys and run unchangedwatch: docket_parked_depth — it can be wrong for a stream while two releases run together, until a script or a heartbeat pass reaches that streamwatch: docket_parked_depth — it does not count a task that an earlier release parks on a stream this release has not recorded, until a script from this release runs on that streamSession context
A local run showed 185 parked tasks while the stream held 3 to 5 entries. The first version summed
XLENover a set of waiter streams in every heartbeat. Review found that cost grows with backlogged keys times workers, so the scripts keep the total now.🤖 Generated with Claude Code