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


##########
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:

Review Comment:
   fixed. it writes unhealthy on every tick while the check is stalled now, not 
just the first time.



##########
airflow-core/src/airflow/api/common/airflow_health.py:
##########
@@ -170,6 +224,85 @@ def _dag_processor_detailed_status(jobs: list[Job]) -> 
DetailedHealthStatus:
     return _coverage_status(expected, covered)
 
 
+@lru_cache
+def _configured_executor_keys() -> set[str]:
+    """
+    Return the key each configured executor that implements a health check 
would be recorded under.
+
+    Most executors don't override 
:meth:`~airflow.executors.base_executor.BaseExecutor.get_health`,
+    so this deliberately excludes them rather than counting every configured 
executor: an executor
+    that was never going to report anything isn't a gap in coverage, and a 
deployment where nothing
+    implements a health check should see no ``executor`` block at all in 
``/monitor/health``, not an
+    ``unhealthy`` one.
+
+    Also empty when ``executor_health_check_interval`` is 0: the scheduler 
records no ``ExecutorJob``
+    rows in that case either, so nothing would ever cover an expected key and 
``/monitor/health`` would
+    report the executor DOWN forever instead of omitting the block.
+
+    Cached: this calls 
:meth:`~airflow.executors.executor_loader.ExecutorLoader.import_executor_cls`,
+    which imports every configured executor class, and the configured 
executors can't change while the
+    process runs. Without caching, an API server would redo those imports -- 
and reproduce any import
+    failure's warning and traceback -- on every single call to 
``/monitor/health``. Team validation is
+    skipped (``validate_teams=False``), the same as ``connections.py`` does, 
so a database hiccup on
+    the first call can't get cached as "no executors".
+    """
+    if conf.getfloat("scheduler", "executor_health_check_interval", 
fallback=30.0) <= 0:

Review Comment:
   fixed, moved the getfloat call inside the try so a bad value doesn't crash 
the endpoint.



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