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

@@ -13,6 +13,7 @@ import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer;
import io.github.hectorvent.floci.services.mwaa.model.Environment;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.CopyArchiveToContainerCmd;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.Mount;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
@@ -363,4 +364,66 @@ class MwaaEnvironmentManagerTest {
verify(lifecycleManager, never()).startCreated(anyString(), any());
}
}
/** Crash detection the readiness poller relies on to stop waiting on a dead container,
* verified against a mocked DockerClient (mirrors BatchDockerRunnerTest's deep-stub style). */
@Nested
class ContainerExitDetection {
private DockerClient dockerClient;
@BeforeEach
void setUpDockerClient() {
dockerClient = Mockito.mock(DockerClient.class, Mockito.RETURNS_DEEP_STUBS);
when(lifecycleManager.getDockerClient()).thenReturn(dockerClient);
}
private Environment environmentWithContainerId(String containerId) {
Environment environment = new Environment();
environment.setName("my-env");
environment.setAirflowContainerId(containerId);
return environment;
}
@Test
void reportsNotExitedWhileTheContainerIsStillRunning() {
when(dockerClient.inspectContainerCmd("airflow-container-id").exec().getState().getRunning())
.thenReturn(true);
assertFalse(manager.hasAirflowContainerExited(environmentWithContainerId("airflow-container-id")));
}
@Test
void reportsExitedOnceTheContainerHasStopped() {
when(dockerClient.inspectContainerCmd("airflow-container-id").exec().getState().getRunning())
.thenReturn(false);
assertTrue(manager.hasAirflowContainerExited(environmentWithContainerId("airflow-container-id")));
}
@Test
void reportsExitedWhenTheContainerHasBeenRemoved() {
when(dockerClient.inspectContainerCmd("airflow-container-id").exec())
.thenThrow(new NotFoundException("no such container"));
assertTrue(manager.hasAirflowContainerExited(environmentWithContainerId("airflow-container-id")));
}
@Test
void reportsNotExitedWhenNoContainerHasBeenCreatedYet() {
assertFalse(manager.hasAirflowContainerExited(new Environment()));
}
@Test
void reportsNotExitedWhenTheDockerDaemonInspectFailsForAnUnrelatedReason() {
// Anything other than NotFoundException is inconclusive, not "the container is gone" -
// must not propagate, since the readiness poller shares one loop across every CREATING
// environment and a thrown exception here would starve every other environment's check
// for this poll tick, not just this one's.
when(dockerClient.inspectContainerCmd("airflow-container-id").exec())
.thenThrow(new RuntimeException("docker daemon connection reset"));
assertFalse(manager.hasAirflowContainerExited(environmentWithContainerId("airflow-container-id")));
}
}
}
@@ -540,5 +540,89 @@ class MwaaServiceTest {
verify(environmentManager).stopEnvironment(environment);
verify(proxyManager).stopProxy(MwaaService.environmentIdentity(environment));
}
@Test
void checkReadinessMarksEnvironmentAvailableOnceReady() {
Environment environment = realModeService.createEnvironment("ready-env",
createRequest("arn:aws:s3:::my-bucket", "dags"));
when(environmentManager.isReady(environment)).thenReturn(true);
realModeService.checkReadiness(environment);
assertEquals(EnvironmentStatus.AVAILABLE, environment.getStatus());
}
@Test
void checkReadinessLeavesEnvironmentCreatingWhileContainerIsStillStartingUp() {
Environment environment = realModeService.createEnvironment("starting-env",
createRequest("arn:aws:s3:::my-bucket", "dags"));
when(environmentManager.isReady(environment)).thenReturn(false);
when(environmentManager.hasAirflowContainerExited(environment)).thenReturn(false);
realModeService.checkReadiness(environment);
assertEquals(EnvironmentStatus.CREATING, environment.getStatus());
}
@Test
void checkReadinessMarksCreateFailedWhenTheAirflowContainerHasExited() {
// e.g. a startup script or `airflow db migrate` failed after docker start returned;
// without this, the environment would poll a dead container forever and never leave
// CREATING.
Environment environment = realModeService.createEnvironment("crashed-env",
createRequest("arn:aws:s3:::my-bucket", "dags"));
when(environmentManager.isReady(environment)).thenReturn(false);
when(environmentManager.hasAirflowContainerExited(environment)).thenReturn(true);
realModeService.checkReadiness(environment);
assertEquals(EnvironmentStatus.CREATE_FAILED, environment.getStatus());
}
@Test
void checkReadinessIgnoresEnvironmentsNotInCreatingStatus() {
Environment environment = realModeService.createEnvironment("available-env",
createRequest("arn:aws:s3:::my-bucket", "dags"));
environment.setStatus(EnvironmentStatus.AVAILABLE);
realModeService.checkReadiness(environment);
assertEquals(EnvironmentStatus.AVAILABLE, environment.getStatus());
verify(environmentManager, Mockito.never()).isReady(any());
verify(environmentManager, Mockito.never()).hasAirflowContainerExited(any());
}
@Test
void checkReadinessDoesNotOverwriteAStatusChangedConcurrentlyWhileWaitingOnIsReady() {
// isReady() is a blocking HTTP call; simulate a concurrent DeleteEnvironment moving the
// same Environment instance to DELETING while that call is in flight.
Environment environment = realModeService.createEnvironment("deleted-mid-check-env",
createRequest("arn:aws:s3:::my-bucket", "dags"));
when(environmentManager.isReady(environment)).thenAnswer(invocation -> {
environment.setStatus(EnvironmentStatus.DELETING);
return true;
});
realModeService.checkReadiness(environment);
assertEquals(EnvironmentStatus.DELETING, environment.getStatus());
}
@Test
void checkReadinessDoesNotOverwriteAStatusChangedConcurrentlyWhileWaitingOnContainerExitCheck() {
// Same race, on the hasAirflowContainerExited() branch: without the re-check, this would
// resurrect a just-deleted environment into storage as CREATE_FAILED.
Environment environment = realModeService.createEnvironment("deleted-mid-crash-check-env",
createRequest("arn:aws:s3:::my-bucket", "dags"));
when(environmentManager.isReady(environment)).thenReturn(false);
when(environmentManager.hasAirflowContainerExited(environment)).thenAnswer(invocation -> {
environment.setStatus(EnvironmentStatus.DELETING);
return true;
});
realModeService.checkReadiness(environment);
assertEquals(EnvironmentStatus.DELETING, environment.getStatus());
}
}
}