fix(mwaa): mark CREATE_FAILED when the Airflow container exits before becoming ready (#3446)

docker start only launches the entrypoint and returns immediately, so a
startup script or `airflow db migrate` failing partway through never
surfaced anywhere: the readiness poller only checked isReady() and had no
path back to a failure status, so the environment reported CREATING
forever instead of CREATE_FAILED.

MwaaEnvironmentManager.hasAirflowContainerExited inspects the container's
running state (docker-removed counts as exited too), and the readiness
poller (extracted into checkReadiness for direct testability) now marks
CREATE_FAILED when isReady() is false and the container has exited.

Two things this needed to stay safe under concurrency and partial
failure, both of which isReady() already handles and this had to match:

- isReady()/hasAirflowContainerExited() are blocking Docker/HTTP calls,
  wide enough for a concurrent DeleteEnvironment (which sets DELETING on
  the same Environment instance before tearing down its containers) to
  have moved the environment past CREATING in the meantime. checkReadiness
  re-checks the status immediately before writing, in both branches, so it
  can't resurrect a just-deleted environment into storage with a status
  decided from stale, pre-delete information.
- hasAirflowContainerExited only treats NotFoundException (the container
  is actually gone) as exited; any other Docker error is inconclusive and
  must not propagate, since the readiness poller shares one loop across
  every CREATING environment and an uncaught exception from one
  environment's check would otherwise skip every other environment for
  that poll tick, the same failure mode isReady()'s own catch-all already
  avoids.

Fixes #3437
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-09-11 22:38:06 -04:00
1 parent f482a2d15e
commit 2a682a6875
5 files changed
+213 -7

No files matched your search

@@ -12,6 +12,7 @@ import io.github.hectorvent.floci.core.common.docker.LaunchedContainerAwsEnv;
import io.github.hectorvent.floci.services.mwaa.model.Environment;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.command.InspectContainerResponse;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.ContainerNetwork;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.api.model.StreamType;
@@ -262,6 +263,36 @@ public class MwaaEnvironmentManager {
}
}
/**
* True when the Airflow container was created but is no longer running, for example because a
* startup script or {@code airflow db migrate} failed after {@code docker start} returned.
* {@code docker start} only launches the entrypoint and returns immediately, so nothing else
* observes a failure like that; {@link MwaaService}'s readiness poller uses this to stop
* waiting on a container that will never answer {@code /health}, instead of leaving the
* environment at CREATING forever.
*/
public boolean hasAirflowContainerExited(Environment environment) {
String containerId = environment.getAirflowContainerId();
if (containerId == null) {
return false;
}
try {
InspectContainerResponse inspect = lifecycleManager.getDockerClient().inspectContainerCmd(containerId).exec();
return !Boolean.TRUE.equals(inspect.getState().getRunning());
} catch (NotFoundException e) {
return true;
} catch (Exception e) {
// Inconclusive (a transient Docker daemon issue, not "the container is gone"): treat as
// still running, like isReady()'s own catch-all does for a failed /health call, so one
// bad inspect doesn't wrongly fail this environment or, since the readiness poller
// shares one loop across every CREATING environment, escape and starve every other
// environment's check for this poll tick.
LOG.warnv("Could not inspect Airflow container {0} for environment {1}: {2}",
containerId, environment.getName(), e.getMessage());
return false;
}
}
/** Crude but dependency-free check for {@code "<section>":{"status":"healthy"...}} in the /health JSON. */
static boolean healthySection(String body, String section) {
int idx = body.indexOf("\"" + section + "\"");
@@ -432,12 +432,7 @@ public class MwaaService implements TagHandler {
readinessPoller.scheduleAtFixedRate(() -> {
try {
for (Environment environment : allEnvironments()) {
if (environment.getStatus() == EnvironmentStatus.CREATING
&& environmentManager.isReady(environment)) {
LOG.infov("MWAA environment {0} is now AVAILABLE", environment.getName());
environment.setStatus(EnvironmentStatus.AVAILABLE);
putEnvironment(environment);
}
checkReadiness(environment);
}
} catch (Exception e) {
LOG.error("Error in MWAA readiness poller", e);
@@ -445,6 +440,39 @@ public class MwaaService implements TagHandler {
}, 2, 3, TimeUnit.SECONDS);
}
// Package-private (not private) so MwaaServiceTest can exercise a single poll pass directly,
// without waiting on the scheduled executor.
void checkReadiness(Environment environment) {
if (environment.getStatus() != EnvironmentStatus.CREATING) {
return;
}
boolean ready = environmentManager.isReady(environment);
boolean exited = !ready && environmentManager.hasAirflowContainerExited(environment);
// isReady()/hasAirflowContainerExited() are blocking Docker/HTTP calls, long enough for a
// concurrent DeleteEnvironment (which sets DELETING on this same Environment instance
// before tearing down its containers) to have moved this environment past CREATING in the
// meantime. Re-checking right before writing avoids resurrecting a just-deleted environment
// into storage with a status decided from stale, pre-delete information.
if (environment.getStatus() != EnvironmentStatus.CREATING) {
return;
}
if (ready) {
LOG.infov("MWAA environment {0} is now AVAILABLE", environment.getName());
environment.setStatus(EnvironmentStatus.AVAILABLE);
putEnvironment(environment);
} else if (exited) {
// docker start returns as soon as the entrypoint launches, so a script or migration
// failing partway through never surfaces here on its own; without this check the
// environment would poll a dead container and report CREATING forever instead of the
// CREATE_FAILED a real failure should be.
LOG.errorv("MWAA environment {0}''s Airflow container exited before becoming ready; "
+ "marking CREATE_FAILED", environment.getName());
environment.setStatus(EnvironmentStatus.CREATE_FAILED);
putEnvironment(environment);
}
}
private void startDagSyncPoller() {
int intervalSeconds = Math.max(1, config.services().mwaa().dagSyncIntervalSeconds());
dagSyncPoller.scheduleAtFixedRate(() -> {