Repository navigation
Arm one rate limit timer per bucket, not one per waiting task - #11075
Conversation
Consumer._schedule_bucket_request() armed a new timer each time a request found no token in its bucket. Each of these timers re-arms itself when it fires and the bucket still has requests, so each request that waited started a chain of timers that lived until the bucket was empty. On a busy queue the bucket is never empty, so the number of timers grew with the number of requests received, and so did the CPU time used for each task. Record the pending timer of each bucket, and arm a timer only for a bucket that has none. The timer callback forgets the timer before it moves the requests, so the next timer can be armed. A token in the callback arguments makes a timer that is no longer the recorded one stop, so two chains cannot serve one bucket if two threads arm a timer at the same time. on_close() empties the buckets, so it now also cancels their timers and forgets them. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Looks good. I ran it against the reproduction from #5927 and a few cases around it, and found nothing to change.
On main the first seconds give 20 and 16 tasks, then 1 per second. Here every second has 19 or 20. The reconnect row is the one I looked at hardest, since a bucket still marked as armed after its timer is gone would never move again. The tests cover the change well. The 8 new ones and the changed One thing that isn't from this PR, in case it matters for your gevent workers: with the gevent pool I got 10 tasks per second for both a Setup of the runs
|
ChristianMurphy
left a comment
There was a problem hiding this comment.
Approving. My runs are in my comment above (or below in the new ui), and nothing has changed since 542639b.
|
Thank you for testing this so thoroughly, @ChristianMurphy. The reconnect and rate-change cases, and the planted mistakes, are a good extra check that the tests cover what matters. Thanks for the gevent note too. That cap is the same on |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #11075 +/- ##
=======================================
Coverage 91.41% 91.42%
=======================================
Files 153 153
Lines 22040 22057 +17
Branches 2743 2747 +4
=======================================
+ Hits 20148 20165 +17
Misses 1589 1589
Partials 303 303
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
|
seems the unit test coverage needs to be increased |
auvipy
left a comment
There was a problem hiding this comment.
should we also consider smoke or integration tests for this changes?
on_close() cancels the rate limit timers only if the consumer has a timer. Add a test for a consumer without a timer. The test checks that on_close() still forgets the timers. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The test queues more rate-limited tasks than the worker runs in 10 seconds, then reads the number of pending rate limit timers on the worker each second with a new inspect command. One bucket waits for tokens, so the count must stay at 1 or less. It runs on the prefork and gevent pools, with a RabbitMQ broker. Without the fix for celery#5927 the count grows with the tasks received. Co-Authored-By: Claude Opus 5.5 <[email protected]>
I'd already added them to another branch, I'll pull them in here. |
I saw the message but was confused as it said this branch has higher coverage than the base! I've add another tests to test the uncovered code. |
Note: Before submitting this pull request, please review our contributing
guidelines & [Anti AI spamming Guidelines] (#10932). Before opening an AI based PR make sure you asked
in the discussion forum or in the related open issues first.
Description
Fixes #5927
I reported this bug in discussion #11070, with the cause and this fix.
@ChristianMurphy's analysis in #5927 (comment) is extra evidence for the same cause. With
rate_limit="20/s", 200 tasks took 161 s, where the limit allows about 10 s. It also shows a second effect in kombu:Hub.fire_timers()fires at most 10 timers in each loop, then returns a 1 second poll timeout. So the growing number of timers also lowers the task rate.This PR fixes the Celery part only. It does not change kombu. #10972 changes the same part of
consumer.pyfor #8119, so one of the two PRs will need a rebase.Consumer._schedule_bucket_request()armed a new timer each time a request found no token in its bucket. Each of these timers re-arms itself while the bucket has requests. So each received task that waited started a chain of timers that lived until the bucket was empty. On a busy queue the bucket is never empty, so the timers, and the CPU used for each task, grew with the number of tasks received. #11070 and #5927 have the details. With the gevent pool each timer is also a greenlet, so the CPU used by the worker grows over time: we saw one worker go from about 0.3 to about 0.85 of a core in about 35 minutes.Changes in
celery/worker/consumer/consumer.py:Consumer._limit_timersrecords the pending timer of each bucket as(token, entry)._schedule_bucket_request()arms a timer only for a bucket that has none._on_bucket_wakeup(bucket, token). It forgets the recorded timer, then calls_schedule_bucket_request(), which arms the next timer if requests still wait._on_bucket_wakeup()does nothing. This stops a second chain if two threads arm a timer at the same moment (thetimer2thread and the consumer, with the solo or threads pool).on_close()empties the buckets withclear_pending(). It now also cancels their timers throughself.timer.cancel(entry)and forgets them. Otherwise a bucket whose timer was cancelled would stay marked as armed, and its requests would never move.The order of the requests and the rate do not change.
Tests in
t/unit/worker/test_consumer.py:TokenBucketand a realkombuTimergive 1 timer entry.on_close(), a bucket gets a timer again.on_close()cancels and forgets the timers, also whentimer.cancel()raises.on_close()forgets the timers when the consumer has no timer.test_schedule_bucket_requestnow expects the_on_bucket_wakeupcallback. The fake consumers intest_consumer.py,test_control.pyandtest_eta_query_task.pythat callConsumer.on_close()now set_limit_timers = {}. Without the fix, 9 tests intest_consumer.pyfail.Before and after, with an in-process script (in-memory broker, solo pool, 5 tasks a second, 10 seconds):
mainA pytest-celery test (RabbitMQ broker, Redis backend, Celery 5.7.0 in the worker container, 5 tasks a second) counts the pending rate limit timers in the worker each second for 15 seconds:
A smoke test,
t/smoke/tests/test_rate_limit.py, does the same check. For it, this PR also adds a rate-limited task int/smoke/tasks.pyand an inspect command int/integration/tasks.py. The inspect command counts the pending rate limit timers. The test runs on the prefork and gevent pools and passes with this fix. Without the fix it fails: in 10 seconds the timers grow from 21 to 55 (prefork) and from 9 to 66 (gevent).pytest t/unit: 5229 passed, 98 skipped, 2 xfailed.pre-commit runon the changed files passes.I used Claude Code to write this change. I reviewed and tested it, and I can explain each part.
🤖 Generated with Claude Code