dabla commented on code in PR #77549:
URL: https://github.com/apache/airflow/pull/77549#discussion_r4686370229
##########
airflow-core/src/airflow/executors/base_executor.py:
##########
@@ -339,7 +339,18 @@ def __init__(self, parallelism: int = PARALLELISM,
team_name: str | None = None)
)
self.running: set[WorkloadKey] = set()
self.event_buffer: dict[WorkloadKey, EventBufferValueType] = {}
+ # Only ``_register_task`` adds entries and only
``_forget_unreferenced_tasks`` removes them: the
+ # reverse index and the sweep rely on it.
self._task_coordinates: dict[TaskInstanceUuid, TaskInstanceKey] = {}
+ # Reverse index of ``_task_coordinates``, so events keyed by
coordinates resolve without a scan.
+ # Tuples: coordinates almost always have a single attempt, and a set
costs 4x the memory.
+ self._coordinate_task_ids: dict[TaskInstanceKey,
tuple[TaskInstanceUuid, ...]] = {}
+ # Identities registered since the last sweep, and the container keys
it saw (see
+ # ``_forget_unreferenced_tasks``).
+ self._unswept_task_ids: set[TaskInstanceUuid] = set()
+ self._keys_at_last_sweep: tuple[set[WorkloadKey], ...] = (set(),
set(), set())
+ # Identities forgotten by the sweep of the drain in progress in
``_drain_events_with_task_ids``.
+ self._task_coordinates_forgotten_by_drain: dict[TaskInstanceUuid,
TaskInstanceKey] | None = None
Review Comment:
**[nit]** The identity bookkeeping is now a group of five fields and three
methods with a life of its own inside `BaseExecutor`.
`_task_coordinates`, `_coordinate_task_ids`, `_unswept_task_ids`,
`_keys_at_last_sweep` and `_task_coordinates_forgotten_by_drain` are only read
and written by `_register_task`, `_discard_coordinate_owner` and
`_forget_unreferenced_tasks`, and the invariant between them ("only
`_register_task` adds, only the sweep removes") is kept by a comment. That is
the shape [Extract Class](https://refactoring.com/catalog/extractClass.html)
describes: a small registry object holding the two maps and the sweep state,
with `register(ti)`, `sweep(running, task_queue, event_buffer)` returning what
it forgot, and `owners(coordinates)`. Two things would fall out of it: the
sweep could hand the forgotten identities back as a return value, so
`_drain_events_with_task_ids` no longer needs to pass them through an instance
attribute set around the call to `get_event_buffer()`, and the randomized test
could drive the registry directly instead of porting the old logic onto a
`BaseExecutor` subclass.
Not for this PR if it is going to be backported to 3.4, where the smaller
diff is worth more. Fine as a follow-up on `main`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]