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


##########
airflow-core/newsfragments/76410.significant.rst:
##########
@@ -0,0 +1,18 @@
+Added scaffolding for per-executor health reporting

Review Comment:
   removed it.



##########
airflow-core/src/airflow/api/common/airflow_health.py:
##########
@@ -170,6 +223,76 @@ 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.
+
+    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".
+    """
+    from airflow.executors.base_executor import BaseExecutor
+
+    try:
+        names = ExecutorLoader.get_executor_names(validate_teams=False)
+    except Exception:
+        # A health probe must not fail on malformed executor config; callers 
fall back to liveness only.
+        log.warning("Could not read the executor configuration", exc_info=True)
+        return set()
+
+    keys = set()
+    for name in names:
+        try:
+            executor_cls, _ = ExecutorLoader.import_executor_cls(name)
+        except Exception:
+            log.warning("Could not import configured executor %s", 
name.module_path, exc_info=True)
+            continue
+        if executor_cls.get_health is not BaseExecutor.get_health:

Review Comment:
   done, added BaseExecutor.overrides_get_health() and both places use it now.



##########
airflow-core/tests/unit/api/common/test_airflow_health.py:
##########
@@ -662,4 +668,299 @@ def 
test_get_airflow_health_mixed_triggerers_include_team_names(self, testing_te
                 "team_names": [testing_team.name],
             },
         ]
-        assert health_status["triggerer"]["latest_triggerer_heartbeat"] == 
alive_heartbeat.isoformat()
+
+
+# -- Executor health: no configured executor overrides get_health() by 
default, so these tests patch
+# -- _configured_executor_keys directly rather than relying on a real executor 
class, exactly as the
+# -- bundle/team partition tests above patch _configured_bundle_teams.
+
+
+def _executor(
+    *,
+    alive: bool,
+    healthy: bool | None = True,
+    team_name: str | None = None,
+    executor: str | None = None,
+    owner_job_id: int = 1,
+) -> MagicMock:
+    job = _mock_job(
+        hostname=f"host-{owner_job_id}",
+        heartbeat=datetime(2024, 2, 1),
+        alive=alive,
+        team_names=[team_name] if team_name else [],
+    )
+    job.executor_class = executor or f"executor-{team_name}-{'alive' if alive 
else 'stale'}"
+    job.owner_job_id = owner_job_id
+    if healthy is None:
+        job.health_check_result = HealthCheckResult.UNKNOWN
+    else:
+        job.health_check_result = HealthCheckResult.HEALTHY if healthy else 
HealthCheckResult.UNHEALTHY
+    return job
+
+
+def _patch_executor_keys(keys: set[str]):
+    return 
patch("airflow.api.common.airflow_health._configured_executor_keys", 
return_value=keys)
+
+
+def _patch_live_scheduler_ids(ids: set[int]):
+    """Patch which owning-scheduler ids count as alive, standing in for a real 
SchedulerJob query."""
+    return patch("airflow.api.common.airflow_health._live_scheduler_job_ids", 
return_value=ids)
+
+
+class _HealthCheckingExecutor(BaseExecutor):
+    """A stand-in for a concrete executor (e.g. Celery) that has implemented 
get_health()."""
+
+    def get_health(self, timeout: float) -> bool | None:
+        return True
+
+
+class TestConfiguredExecutorKeys:
+    """``_configured_executor_keys`` only counts an executor that actually 
implements get_health()."""
+
+    @pytest.fixture(autouse=True)
+    def clear_executor_caches(self):
+        _configured_executor_keys.cache_clear()
+        _configured_executor_names.cache_clear()
+        yield
+        _configured_executor_keys.cache_clear()
+        _configured_executor_names.cache_clear()
+
+    def test_empty_when_no_executor_overrides_get_health(self):
+        names = 
[ExecutorName(module_path="airflow.executors.local_executor.LocalExecutor", 
alias="Local")]
+        with 
patch("airflow.api.common.airflow_health.ExecutorLoader.get_executor_names", 
return_value=names):

Review Comment:
   switched those to the decorator.



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