jscheffl commented on code in PR #66400:
URL: https://github.com/apache/airflow/pull/66400#discussion_r3191239038
##########
providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/executors/kubernetes_executor.py:
##########
@@ -681,22 +681,114 @@ def adopt_launched_task(
del tis_to_flush_by_key[ti_key]
self.running.add(ti_key)
+ def _alive_other_scheduler_job_ids(self) -> set[int]:
+ """
+ Return job IDs of every SchedulerJob that is currently alive —
excluding self.
+
+ "Alive" means ``Job.state == RUNNING`` AND its ``latest_heartbeat`` is
+ within ``[scheduler] scheduler_health_check_threshold``.
+
+ Used by ``_adopt_completed_pods`` to scope cross-scheduler pod
+ adoption to pods owned by no-longer-alive schedulers (#66396).
+ With a single scheduler the returned set is always empty — the
+ original "exclude self only" behavior is preserved. With multiple
+ schedulers each one only adopts pods whose owning scheduler is gone,
+ eliminating the relabel-thrash that PR #61839 introduced.
+
+ Returns an empty set on any DB error so the caller falls back to
+ the pre-#61839 "exclude self only" selector — a transient DB issue
+ must not break completed-pod cleanup.
+ """
+ if TYPE_CHECKING:
+ assert self.scheduler_job_id
+
+ try:
+ self_id = int(self.scheduler_job_id)
+ except (TypeError, ValueError):
+ # Tests sometimes set scheduler_job_id to a non-numeric string.
+ # In production it's always Job.id (int), but be defensive.
+ return set()
+
+ try:
+ from datetime import timedelta
+
+ from sqlalchemy import select
+
+ from airflow.jobs.job import Job
+ from airflow.utils import timezone
+ from airflow.utils.session import create_session
+ from airflow.utils.state import JobState
+
+ timeout = conf.getint("scheduler",
"scheduler_health_check_threshold")
+ cutoff = timezone.utcnow() - timedelta(seconds=timeout)
+ with create_session() as session:
+ # Iterate the scalar cursor straight into the set so we never
+ # materialize an intermediate list — keeps the memory
+ # footprint flat regardless of how many sibling schedulers
+ # are alive (per @jscheffl review on #66400).
Review Comment:
I think for this my name does not need to be carried in comments :-D
```suggestion
# are alive
```
--
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]