vincbeck commented on code in PR #76410:
URL: https://github.com/apache/airflow/pull/76410#discussion_r4685744639
##########
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:
A stalled check can end up reported as healthy. The stall is recorded as
UNHEALTHY only once (`key not in self._stalled_health_checks`), and no new
check starts while the thread is stuck. Example: the scheduler loop pauses past
`scheduler_health_check_threshold`, `adopt_or_reset_orphaned_tasks` fails the
`ExecutorJob` row, and `_touch_executor_heartbeats` inserts a new UNKNOWN row.
Nothing writes UNHEALTHY on that row again, and `_executor_detailed_status`
drops UNKNOWN keys from `expected`, so `/monitor/health` reports the executor
healthy for as long as the hang lasts. Fix: record UNHEALTHY on every tick
while the check is stalled, not only the first time.
##########
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:
nit: the threads are joined one after the other, each with its own timeout,
so shutdown can wait 15s per hung check, including a check already marked as
stalled. Can you use one deadline for all of them?
##########
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:
A bad `executor_health_check_interval` value (e.g. `30s`) makes
`/monitor/health` return a 500 on every call, so the metadatabase and scheduler
status are lost too. `conf.getfloat` runs outside the `try` below, and
`lru_cache` does not cache the exception. Can you move it inside the `try`?
--
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]