fix(mwaa): also detect a crashed sibling Postgres container, not just Airflow (#3454)

hasAirflowContainerExited only inspected the Airflow container. If the
sibling Postgres container dies instead (out of memory, its volume
filling up), Airflow keeps running but never reports metadatabase
healthy, so the environment sat at CREATING exactly as before this
existed, the same failure mode #3446 fixed for the Airflow container
alone.

Generalized into hasAnyContainerExited, backed by a shared per-container
helper, checking both Environment.getDbContainerId() and
getAirflowContainerId(). checkReadiness and its log messages updated to
match; behavior for a lone Airflow-container failure is unchanged.

Follow-up from hectorvent's review on #3446.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-09-12 00:20:38 -05:00
1 parent 0a45d2e131
commit fe076212c2
5 files changed
+70 -41

No files matched your search

@@ -365,8 +365,8 @@ class MwaaEnvironmentManagerTest {
}
}
/** 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). */
/** Crash detection the readiness poller relies on to stop waiting on dead containers, verified
* against a mocked DockerClient (mirrors BatchDockerRunnerTest's deep-stub style). */
@Nested
class ContainerExitDetection {
@@ -378,40 +378,60 @@ class MwaaEnvironmentManagerTest {
when(lifecycleManager.getDockerClient()).thenReturn(dockerClient);
}
private Environment environmentWithContainerId(String containerId) {
private Environment environmentWithContainers(String dbContainerId, String airflowContainerId) {
Environment environment = new Environment();
environment.setName("my-env");
environment.setAirflowContainerId(containerId);
environment.setDbContainerId(dbContainerId);
environment.setAirflowContainerId(airflowContainerId);
return environment;
}
@Test
void reportsNotExitedWhileTheContainerIsStillRunning() {
void reportsNotExitedWhileBothContainersAreStillRunning() {
when(dockerClient.inspectContainerCmd("db-container-id").exec().getState().getRunning())
.thenReturn(true);
when(dockerClient.inspectContainerCmd("airflow-container-id").exec().getState().getRunning())
.thenReturn(true);
assertFalse(manager.hasAirflowContainerExited(environmentWithContainerId("airflow-container-id")));
assertFalse(manager.hasAnyContainerExited(environmentWithContainers("db-container-id", "airflow-container-id")));
}
@Test
void reportsExitedOnceTheContainerHasStopped() {
void reportsExitedOnceTheAirflowContainerHasStopped() {
when(dockerClient.inspectContainerCmd("db-container-id").exec().getState().getRunning())
.thenReturn(true);
when(dockerClient.inspectContainerCmd("airflow-container-id").exec().getState().getRunning())
.thenReturn(false);
assertTrue(manager.hasAirflowContainerExited(environmentWithContainerId("airflow-container-id")));
assertTrue(manager.hasAnyContainerExited(environmentWithContainers("db-container-id", "airflow-container-id")));
}
@Test
void reportsExitedWhenTheContainerHasBeenRemoved() {
void reportsExitedOnceTheSiblingPostgresContainerHasStopped() {
// Airflow itself keeps running when Postgres dies underneath it, it just never reports
// metadatabase healthy, so this must be detected independently of the Airflow
// container's own running state.
when(dockerClient.inspectContainerCmd("db-container-id").exec().getState().getRunning())
.thenReturn(false);
when(dockerClient.inspectContainerCmd("airflow-container-id").exec().getState().getRunning())
.thenReturn(true);
assertTrue(manager.hasAnyContainerExited(environmentWithContainers("db-container-id", "airflow-container-id")));
}
@Test
void reportsExitedWhenEitherContainerHasBeenRemoved() {
when(dockerClient.inspectContainerCmd("db-container-id").exec().getState().getRunning())
.thenReturn(true);
when(dockerClient.inspectContainerCmd("airflow-container-id").exec())
.thenThrow(new NotFoundException("no such container"));
assertTrue(manager.hasAirflowContainerExited(environmentWithContainerId("airflow-container-id")));
assertTrue(manager.hasAnyContainerExited(environmentWithContainers("db-container-id", "airflow-container-id")));
}
@Test
void reportsNotExitedWhenNoContainerHasBeenCreatedYet() {
assertFalse(manager.hasAirflowContainerExited(new Environment()));
void reportsNotExitedWhenNeitherContainerHasBeenCreatedYet() {
assertFalse(manager.hasAnyContainerExited(new Environment()));
}
@Test
@@ -420,10 +440,12 @@ class MwaaEnvironmentManagerTest {
// 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("db-container-id").exec().getState().getRunning())
.thenReturn(true);
when(dockerClient.inspectContainerCmd("airflow-container-id").exec())
.thenThrow(new RuntimeException("docker daemon connection reset"));
assertFalse(manager.hasAirflowContainerExited(environmentWithContainerId("airflow-container-id")));
assertFalse(manager.hasAnyContainerExited(environmentWithContainers("db-container-id", "airflow-container-id")));
}
}
}
@@ -590,7 +590,7 @@ class MwaaServiceTest {
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);
when(environmentManager.hasAnyContainerExited(environment)).thenReturn(false);
realModeService.checkReadiness(environment);
@@ -598,14 +598,14 @@ class MwaaServiceTest {
}
@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.
void checkReadinessMarksCreateFailedWhenAContainerHasExited() {
// e.g. a startup script or `airflow db migrate` failed after docker start returned, or
// the sibling Postgres container died; without this, the environment would poll dead
// containers 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);
when(environmentManager.hasAnyContainerExited(environment)).thenReturn(true);
realModeService.checkReadiness(environment);
@@ -622,7 +622,7 @@ class MwaaServiceTest {
assertEquals(EnvironmentStatus.AVAILABLE, environment.getStatus());
verify(environmentManager, Mockito.never()).isReady(any());
verify(environmentManager, Mockito.never()).hasAirflowContainerExited(any());
verify(environmentManager, Mockito.never()).hasAnyContainerExited(any());
}
@Test
@@ -643,12 +643,12 @@ class MwaaServiceTest {
@Test
void checkReadinessDoesNotOverwriteAStatusChangedConcurrentlyWhileWaitingOnContainerExitCheck() {
// Same race, on the hasAirflowContainerExited() branch: without the re-check, this would
// Same race, on the hasAnyContainerExited() 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 -> {
when(environmentManager.hasAnyContainerExited(environment)).thenAnswer(invocation -> {
environment.setStatus(EnvironmentStatus.DELETING);
return true;
});