Repository navigation
index tasks by the worker and type known at event time - #10988
Conversation
State.tasks_by_worker and State.tasks_by_type were only updated when a task was first created. With task_send_sent_event the creating event is the client's task-sent, so the task was filed under the client hostname and never under the worker that ran it, and a task whose first event carried no name never reached tasks_by_type. Update the indexes whenever a worker event or the task name is seen, and move the task if a later event comes from a different worker. Also stop the callable tasks_by_worker() raising AttributeError for a task that has no worker yet.
MehrazRumman
left a comment
There was a problem hiding this comment.
Traced State.event() with the change, and the fix is right. The worker index was only written on the event that created the task. With task_send_sent_event=True, that's the client's task-sent, so the task was filed under the client's hostname and never under the worker. A task whose first event had no name never reached tasks_by_type either. Moving the worker index to "every worker event, keyed by that event's hostname" makes it agree with the callable tasks_by_worker(hostname), which already went by task.worker. The task.worker is not None guard in _tasks_by_worker() also fixes a real AttributeError for tasks seen only through task-sent.
Two notes inline: one on the hot path, and one behaviour change to be aware of.
CI hasn't run on this yet (no check runs on ff802a9); it probably needs a maintainer to approve the workflow run.
WeakSet.add() allocates a new weakref even when the item is already in the set, so adding the task to the tasks_by_type and tasks_by_worker indexes on every task event costs two allocations on the hot path for no benefit. Only update them when the task name or the worker changed. Document that the worker index follows the last processed worker event (like task.worker), not the one with the latest clock, and cover it with tests.
…o fix/events-state-task-indexes
|
Thanks @MehrazRumman, can you please review the fixes!? |
MehrazRumman
left a comment
There was a problem hiding this comment.
Re-reviewed 6f25f9c. 6662246 covers both points from my last review, and I have nothing more on the code.
- The indexes are only touched when something changed.
prev_workerandprev_nameare read beforetask.workeris set and beforetask.event(), so a worker event for the sameWorkerobject, or a repeated name, no longer allocates a weakref.test_task_indexes_only_updated_when_changedcovers that. - The comment now says the worker index follows the last worker event processed, as
task.workerdoes, andtest_tasks_by_worker_follows_last_processed_eventpins that down. test_tasks_by_worker_worker_replaced_in_statecovers a case my suggestion got wrong. I saidget_worker()returns the sameWorkerper hostname, but afterworker-offlineor an LRU eviction theWorkeris created again.is notthen sees a new object, and the task is discarded from and added back to the same hostname's set, so it stays indexed. Thanks for testing it.
I traced the other orders as well. If task-sent comes first, the task gets its type there and its worker on the first worker event. If task-sent comes after a worker event, the worker index isn't touched.
Two things outside the code:
- The description still describes ff802a9. It says the index
add"now runs on every task event ... it could be guarded", and lists 4 new tests. Both changed in 6662246, so it would be worth updating before a squash merge. - GitHub Actions hasn't run on this branch. Every workflow on 6f25f9c shows
action_required, so a maintainer needs to approve the run. pre-commit.ci and the Read the Docs build pass. I didn't run the tests here.
|
thanks for the re-review and for tracing the other orders @MehrazRumman, I updated the description to match 6662246. the workflow run on 6f25f9c still needs a maintainer's approval before CI can start. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #10988 +/- ##
==========================================
+ Coverage 91.41% 91.42% +0.01%
==========================================
Files 153 153
Lines 22040 22044 +4
Branches 2743 2745 +2
==========================================
+ Hits 20148 20154 +6
+ Misses 1589 1588 -1
+ Partials 303 302 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
|
unit test builds are failing |
|
Thanks for the heads-up @auvipy. The failing tests are the Redis chord tests, which broke inside fakeredis itself ( |
|
@phanky1 can you cross check? |
Fixes #10987
State.tasks_by_workerandState.tasks_by_typewere only updated when a task was first created (if task_created). The creating event is not always the worker's: withtask_send_sent_event=Trueit is the client'stask-sent, so the task was filed under the client hostname and never under the worker that ran it, and a task whose first event carried no name never reachedtasks_by_type.Changes in
State.event(celery/events/state.py):tasks_by_typeis filled whenever the task has a name (adding to a set is idempotent).tasks_by_workeris filled on every worker (non-task-sent) event under that event's hostname. If the task previously belonged to a different worker (a retried task), it is removed from the old worker's set.tasks_by_worker(hostname)skips tasks that have no worker yet instead of raisingAttributeError.Tests: 7 new tests in
t/unit/events/test_state.py(test_tasks_by_worker_with_task_sent_event,test_tasks_by_worker_task_sent_only,test_tasks_by_worker_first_event_has_no_name,test_tasks_by_worker_task_moves_to_new_worker,test_tasks_by_worker_follows_last_processed_event,test_tasks_by_worker_worker_replaced_in_state,test_task_indexes_only_updated_when_changed). They fail onmainand pass with the change; the existingtasks_by_*tests pass untouched. (Keep the "Not run:" sentence from the old description.)Trade-offs: the two index updates run only when the task name or its worker changed (
prev_name/prev_workerare read beforetask.event()), so the hot path allocates no weakref when nothing changed. The worker index follows the last worker event processed, astask.workerdoes. A task seen only viatask-sentis in notasks_by_workerset, matching the callable which has always usedtask.worker. The discard on worker change goes slightly beyond the minimal fix and can be dropped if you prefer a smaller diff.