JoelEmmanuelCloud commented on code in PR #76410:
URL: https://github.com/apache/airflow/pull/76410#discussion_r4686493049


##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -456,6 +498,191 @@ def __init__(
     def heartbeat_callback(self, *, session: Session = NEW_SESSION) -> None:
         stats.incr("scheduler_heartbeat", 1, 1)
 
+    def _check_executor_health(self) -> None:
+        """
+        Kick off a health check for each reporting executor, in the background.
+
+        Called on its own timer (``[scheduler] 
executor_health_check_interval``), independent of the
+        scheduling loop and of each executor's own 
:meth:`~airflow.executors.base_executor.BaseExecutor.heartbeat`.
+        A new check for a given executor is skipped while its previous check 
is still running, so a
+        slow or hung dependency can only ever delay that executor's own health 
being updated, never
+        pile up work or block this (or any other) scheduler tick.
+
+        ``get_health()`` is passed a timeout, but an implementation that 
ignores it can still leave its
+        check thread running forever. Once a running check is a few intervals 
old, it's reported as
+        stalled: the scheduler records UNHEALTHY itself, since a check that 
hasn't answered isn't
+        still-healthy, and logs and emits a metric so the stall is visible. If 
the stuck call does
+        eventually return, its result overwrites this one.
+        """
+        for executor in self.executors:
+            key = _executor_health_key(executor)
+            if key not in self._executor_health_keys:
+                continue
+            thread, started = self._executor_health_check_threads.get(key, 
(None, 0.0))
+            if thread is not None and thread.is_alive():
+                stall_after = (
+                    self._executor_health_check_interval * 
self._executor_health_check_stall_after_intervals
+                )
+                if time.monotonic() - started > stall_after and key not in 
self._stalled_health_checks:
+                    self._stalled_health_checks.add(key)
+                    self.log.warning("Health check for executor %s has stopped 
responding", key)
+                    stats.incr(
+                        "scheduler.executor_health_check.stalled",
+                        tags=prune_dict(
+                            {"executor": type(executor).__name__, "team_name": 
executor.team_name}
+                        ),
+                    )
+                    try:
+                        self._record_executor_health_result(key, 
HealthCheckResult.UNHEALTHY)
+                    except Exception:
+                        self.log.exception("Failed to record health check 
result for executor %s", key)
+                else:
+                    self.log.debug(
+                        "Skipping health check for executor %s, the previous 
check hasn't finished", key
+                    )
+                continue
+            self._stalled_health_checks.discard(key)
+            thread = threading.Thread(
+                target=self._run_executor_health_check,
+                args=(executor, key),
+                name=f"executor-health-check-{key}",
+                daemon=True,
+            )
+            thread.start()
+            self._executor_health_check_threads[key] = (thread, 
time.monotonic())
+
+    def _join_executor_health_check_threads(self) -> None:
+        """
+        Wait for any in-flight executor health check before this scheduler's 
ExecutorJob rows are closed.
+
+        A check still running at that point can no longer revive a row -- only 
the main thread ever
+        creates one -- but joining first still keeps it from writing a result 
onto a row the scheduler
+        has already closed. Bounded by the same timeout passed to 
``get_health()``, so an implementation
+        that ignores it can delay shutdown but not hang it.
+        """
+        for thread, _ in self._executor_health_check_threads.values():
+            thread.join(timeout=self._executor_health_check_timeout)

Review Comment:
   fixed, the threads now share one deadline instead of each getting the full 
timeout.



-- 
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]

Reply via email to