fix(opensearch,mwaa): remove containers on explicit delete when shutdown retention is on (#4225)

keepRunningOnShutdown made stopDomain and stopEnvironment return early, so
DeleteDomain and DeleteEnvironment left their containers running. Only the
shutdown hook now honours the setting, matching how MWAA already guards its
shutdown path.
This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-09-23 10:17:48 -04:00
1 parent 4b3923712a
commit 2c3332c7cf
7 files changed
+52 -19

No files matched your search

@@ -319,10 +319,6 @@ public class MwaaEnvironmentManager {
/** Stops and removes both containers and all three named volumes for the given environment. */
public void stopEnvironment(Environment environment) {
if (config.services().mwaa().keepRunningOnShutdown()) {
LOG.infov("Leaving MWAA containers for environment {0} running", environment.getName());
return;
}
String name = environment.getName();
if (environment.getAirflowContainerId() != null) {
lifecycleManager.stopAndRemove(environment.getAirflowContainerId(), null);
@@ -207,15 +207,9 @@ public class OpenSearchDomainManager {
if (domain.getContainerId() == null) {
return;
}
if (config.services().opensearch().keepRunningOnShutdown()) {
LOG.infov("Leaving OpenSearch container for domain {0} running", domain.getDomainName());
return;
}
lifecycleManager.stopAndRemove(domain.getContainerId(), null);
// The container no longer holds the binding, so the reservation must go with
// it. Repeated create/delete would otherwise exhaust the configured range.
// The keep-running early return above deliberately keeps the reservation:
// the surviving container still owns the binding.
if (domain.getHostPort() != null) {
portAllocator.release(domain.getHostPort());
domain.setHostPort(null);
@@ -76,7 +76,7 @@ public class OpenSearchService implements ResourceProvider {
@PreDestroy
public void shutdown() {
poller.shutdownNow();
if (!config.services().opensearch().mock()) {
if (!config.services().opensearch().mock() && !config.services().opensearch().keepRunningOnShutdown()) {
for (Domain domain : allDomains()) {
domainManager.stopDomain(domain);
}
@@ -43,6 +43,7 @@ class MwaaEnvironmentManagerTest {
private ContainerLifecycleManager lifecycleManager;
private ContainerDetector containerDetector;
private MwaaEnvironmentManager manager;
private EmulatorConfig.MwaaServiceConfig mwaaConfig;
@BeforeEach
void setUp() {
@@ -51,7 +52,7 @@ class MwaaEnvironmentManagerTest {
when(dockerConfig.logMaxSize()).thenReturn("10m");
when(dockerConfig.logMaxFile()).thenReturn("3");
EmulatorConfig.MwaaServiceConfig mwaaConfig = Mockito.mock(EmulatorConfig.MwaaServiceConfig.class);
mwaaConfig = Mockito.mock(EmulatorConfig.MwaaServiceConfig.class);
when(mwaaConfig.dockerNetwork()).thenReturn(Optional.empty());
when(mwaaConfig.defaultPostgresImage()).thenReturn("postgres:16-alpine");
@@ -504,4 +505,23 @@ class MwaaEnvironmentManagerTest {
assertFalse(manager.hasAnyContainerExited(environmentWithContainers("db-container-id", "airflow-container-id")));
}
}
/**
* Explicit DeleteEnvironment tears down through {@code stopEnvironment}. The shutdown
* retention option is decided by {@link MwaaService#shutdown()} and must not turn a Delete
* into metadata removal that leaves the containers running.
*/
@Test
void stopEnvironmentRemovesContainersEvenWhenShutdownRetentionIsEnabled() {
when(mwaaConfig.keepRunningOnShutdown()).thenReturn(true);
Environment environment = new Environment();
environment.setName("deleted-env");
environment.setAirflowContainerId("airflow-container-id");
environment.setDbContainerId("db-container-id");
manager.stopEnvironment(environment);
Mockito.verify(lifecycleManager).stopAndRemove("airflow-container-id", null);
Mockito.verify(lifecycleManager).stopAndRemove("db-container-id", null);
}
}
@@ -551,6 +551,7 @@ class MwaaServiceTest {
private MwaaEnvironmentManager environmentManager;
private MwaaProxyManager proxyManager;
private MwaaService realModeService;
private boolean keepRunningOnShutdown;
@BeforeEach
void setUpRealMode() {
@@ -571,6 +572,7 @@ class MwaaServiceTest {
case "proxyBasePort" -> 8700;
case "proxyMaxPort" -> 8799;
case "dagSyncIntervalSeconds" -> 30;
case "keepRunningOnShutdown" -> keepRunningOnShutdown;
default -> defaultValue(method);
});
EmulatorConfig.ServicesConfig servicesConfig = proxy(EmulatorConfig.ServicesConfig.class,
@@ -611,6 +613,16 @@ class MwaaServiceTest {
verify(portAllocator).release(8701);
}
@Test
void shutdownLeavesEnvironmentContainersRunningWhenRetentionIsEnabled() {
keepRunningOnShutdown = true;
realModeService.createEnvironment("kept-env", createRequest("arn:aws:s3:::my-bucket", "dags"));
realModeService.shutdown();
verify(environmentManager, Mockito.never()).stopEnvironment(any());
}
@Test
void proxyStartupFailureRollsBackTheAllocatedPortAndAnyStartedContainers() {
// Containers start fine; only the proxy bind fails (e.g. the port got taken out from
@@ -177,11 +177,12 @@ class OpenSearchDomainManagerTest {
}
/**
* Keep-running shutdown leaves the container holding its Docker binding, so
* the reservation has to survive with it.
* Explicit DeleteDomain tears down through {@code stopDomain}. The shutdown retention
* option is decided by {@link OpenSearchService#shutdown()}, so it must not leave a deleted
* domain's container running or its port reserved.
*/
@Test
void stopDomainKeepsTheReservationWhenTheContainerKeepsRunning() {
void stopDomainRemovesTheContainerEvenWhenShutdownRetentionIsEnabled() {
EmulatorConfig config = mock(EmulatorConfig.class);
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
EmulatorConfig.OpenSearchServiceConfig opensearch = mock(EmulatorConfig.OpenSearchServiceConfig.class);
@@ -197,14 +198,13 @@ class OpenSearchDomainManagerTest {
mock(RegionResolver.class));
Domain domain = new Domain();
domain.setDomainName("keep-running");
domain.setDomainName("deleted-domain");
domain.setContainerId("container-id");
domain.setHostPort(9401);
manager.stopDomain(domain);
verify(lifecycleManager, never()).stopAndRemove(anyString(), any());
verify(portAllocator, never()).release(9401);
assertEquals(Integer.valueOf(9401), domain.getHostPort());
verify(lifecycleManager).stopAndRemove("container-id", null);
verify(portAllocator).release(9401);
}
}
@@ -98,4 +98,15 @@ class OpenSearchServiceTest {
assertFalse(service.describeDomain("ready-recovery").isProcessing());
verify(domainManager, atLeast(2)).isReady(any());
}
@Test
void shutdownLeavesDomainContainersRunningWhenRetentionIsEnabled() {
when(domainManager.tryStartDomain(any())).thenReturn(true);
when(osConfig.keepRunningOnShutdown()).thenReturn(true);
service.createDomain("kept-domain", "OpenSearch_2.11", null, null, null, "us-east-1");
service.shutdown();
verify(domainManager, Mockito.never()).stopDomain(any());
}
}