From eb6749870bddf52fc8f2cbb13b3806b912e31f3a Mon Sep 17 00:00:00 2001 From: TiM Date: Tue, 14 Apr 2026 04:07:49 +1200 Subject: [PATCH] fix(elasticache,rds): prevent orphaned Docker containers on shutdown and restart (#398) ElastiCache and RDS container managers used deterministic container names without conflict handling, causing 409 errors when restarting after an interrupted run. Containers were also never cleaned up on shutdown. - Force-remove stale containers by name before creating new ones - Track active containers in ConcurrentHashMap with stopAll() for shutdown - Add container cleanup to EmulatorLifecycle.onStop() (proxies first, then containers) - Add @AfterAll safety net to ElastiCacheIntegrationTest --- .../floci/lifecycle/EmulatorLifecycle.java | 16 +++++++++++-- .../ElastiCacheContainerManager.java | 23 ++++++++++++++++++ .../rds/container/RdsContainerManager.java | 23 ++++++++++++++++++ .../lifecycle/EmulatorLifecycleTest.java | 7 +++++- .../ElastiCacheIntegrationTest.java | 24 +++++++++++++++++++ 5 files changed, 90 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycle.java b/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycle.java index 72be5cdac0..fb8dcdd99d 100644 --- a/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycle.java +++ b/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycle.java @@ -5,7 +5,9 @@ import io.github.hectorvent.floci.core.common.ServiceRegistry; import io.github.hectorvent.floci.core.storage.StorageFactory; import io.github.hectorvent.floci.lifecycle.inithook.InitializationHook; import io.github.hectorvent.floci.lifecycle.inithook.InitializationHooksRunner; +import io.github.hectorvent.floci.services.elasticache.container.ElastiCacheContainerManager; import io.github.hectorvent.floci.services.elasticache.proxy.ElastiCacheProxyManager; +import io.github.hectorvent.floci.services.rds.container.RdsContainerManager; import io.github.hectorvent.floci.services.rds.proxy.RdsProxyManager; import io.quarkus.runtime.Quarkus; import io.quarkus.runtime.ShutdownEvent; @@ -28,18 +30,26 @@ public class EmulatorLifecycle { private final StorageFactory storageFactory; private final ServiceRegistry serviceRegistry; private final EmulatorConfig config; + private final ElastiCacheContainerManager elastiCacheContainerManager; private final ElastiCacheProxyManager elastiCacheProxyManager; + private final RdsContainerManager rdsContainerManager; private final RdsProxyManager rdsProxyManager; private final InitializationHooksRunner initializationHooksRunner; @Inject public EmulatorLifecycle(StorageFactory storageFactory, ServiceRegistry serviceRegistry, - EmulatorConfig config, ElastiCacheProxyManager elastiCacheProxyManager, - RdsProxyManager rdsProxyManager, InitializationHooksRunner initializationHooksRunner) { + EmulatorConfig config, + ElastiCacheContainerManager elastiCacheContainerManager, + ElastiCacheProxyManager elastiCacheProxyManager, + RdsContainerManager rdsContainerManager, + RdsProxyManager rdsProxyManager, + InitializationHooksRunner initializationHooksRunner) { this.storageFactory = storageFactory; this.serviceRegistry = serviceRegistry; this.config = config; + this.elastiCacheContainerManager = elastiCacheContainerManager; this.elastiCacheProxyManager = elastiCacheProxyManager; + this.rdsContainerManager = rdsContainerManager; this.rdsProxyManager = rdsProxyManager; this.initializationHooksRunner = initializationHooksRunner; } @@ -86,6 +96,8 @@ public class EmulatorLifecycle { } finally { elastiCacheProxyManager.stopAll(); rdsProxyManager.stopAll(); + elastiCacheContainerManager.stopAll(); + rdsContainerManager.stopAll(); storageFactory.shutdownAll(); } diff --git a/src/main/java/io/github/hectorvent/floci/services/elasticache/container/ElastiCacheContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/elasticache/container/ElastiCacheContainerManager.java index d844e62539..9f17525e7f 100644 --- a/src/main/java/io/github/hectorvent/floci/services/elasticache/container/ElastiCacheContainerManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/elasticache/container/ElastiCacheContainerManager.java @@ -22,9 +22,11 @@ import java.net.ServerSocket; import java.nio.charset.StandardCharsets; import java.time.LocalDate; import java.time.format.DateTimeFormatter; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; /** * Manages backend Docker container lifecycle for ElastiCache replication groups. @@ -44,6 +46,7 @@ public class ElastiCacheContainerManager { private final EmulatorConfig config; private final CloudWatchLogsService cloudWatchLogsService; private final RegionResolver regionResolver; + private final Map activeContainers = new ConcurrentHashMap<>(); @Inject public ElastiCacheContainerManager(DockerClient dockerClient, @@ -76,6 +79,14 @@ public class ElastiCacheContainerManager { } }); + // Remove any stale container with the same name (from a previous interrupted run) + try { + dockerClient.removeContainerCmd(containerName).withForce(true).exec(); + LOG.infov("Removed stale container {0} before creating new one", containerName); + } catch (com.github.dockerjava.api.exception.NotFoundException ignored) { + // No existing container — normal path + } + CreateContainerResponse container = dockerClient.createContainerCmd(image) .withName(containerName) .withEnv(List.of("VALKEY_EXTRA_FLAGS=--loglevel verbose")) @@ -128,6 +139,7 @@ public class ElastiCacheContainerManager { ElastiCacheContainerHandle handle = new ElastiCacheContainerHandle( containerId, groupId, backendHost, backendPort); + activeContainers.put(groupId, handle); String shortId = containerId.length() >= 8 ? containerId.substring(0, 8) : containerId; attachLogStream(handle, groupId, containerId, shortId); @@ -139,6 +151,7 @@ public class ElastiCacheContainerManager { if (handle == null) { return; } + activeContainers.remove(handle.getGroupId()); LOG.infov("Stopping ElastiCache container {0}", handle.getContainerId()); if (handle.getLogStream() != null) { @@ -160,6 +173,16 @@ public class ElastiCacheContainerManager { } } + public void stopAll() { + List handles = new ArrayList<>(activeContainers.values()); + if (!handles.isEmpty()) { + LOG.infov("Stopping {0} ElastiCache container(s) on shutdown", handles.size()); + } + for (ElastiCacheContainerHandle handle : handles) { + stop(handle); + } + } + private HostConfig buildHostConfig() { HostConfig hostConfig = HostConfig.newHostConfig(); if (!containerDetector.isRunningInContainer()) { diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java index 60fd1b30d0..c800ac2064 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java @@ -23,9 +23,11 @@ import java.net.ServerSocket; import java.nio.charset.StandardCharsets; import java.time.LocalDate; import java.time.format.DateTimeFormatter; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; /** * Manages backend Docker container lifecycle for RDS DB instances and clusters. @@ -43,6 +45,7 @@ public class RdsContainerManager { private final EmulatorConfig config; private final CloudWatchLogsService cloudWatchLogsService; private final RegionResolver regionResolver; + private final Map activeContainers = new ConcurrentHashMap<>(); @Inject public RdsContainerManager(DockerClient dockerClient, @@ -79,6 +82,14 @@ public class RdsContainerManager { } }); + // Remove any stale container with the same name (from a previous interrupted run) + try { + dockerClient.removeContainerCmd(containerName).withForce(true).exec(); + LOG.infov("Removed stale container {0} before creating new one", containerName); + } catch (com.github.dockerjava.api.exception.NotFoundException ignored) { + // No existing container — normal path + } + List envVars = buildEnvVars(engine, masterUsername, masterPassword, dbName); List cmd = buildContainerCmd(engine); @@ -135,6 +146,7 @@ public class RdsContainerManager { LOG.infov("RDS backend for instance {0}: {1}:{2}", instanceId, backendHost, backendPort); RdsContainerHandle handle = new RdsContainerHandle(containerId, instanceId, backendHost, backendPort); + activeContainers.put(instanceId, handle); String shortId = containerId.length() >= 8 ? containerId.substring(0, 8) : containerId; attachLogStream(handle, instanceId, containerId, shortId); return handle; @@ -144,6 +156,7 @@ public class RdsContainerManager { if (handle == null) { return; } + activeContainers.remove(handle.getInstanceId()); LOG.infov("Stopping RDS container {0}", handle.getContainerId()); if (handle.getLogStream() != null) { @@ -163,6 +176,16 @@ public class RdsContainerManager { } } + public void stopAll() { + List handles = new ArrayList<>(activeContainers.values()); + if (!handles.isEmpty()) { + LOG.infov("Stopping {0} RDS container(s) on shutdown", handles.size()); + } + for (RdsContainerHandle handle : handles) { + stop(handle); + } + } + private HostConfig buildHostConfig(int enginePort) { HostConfig hostConfig = HostConfig.newHostConfig(); if (!containerDetector.isRunningInContainer()) { diff --git a/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycleTest.java b/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycleTest.java index 61b572d372..c624ac47cb 100644 --- a/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycleTest.java +++ b/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycleTest.java @@ -5,7 +5,9 @@ import io.github.hectorvent.floci.core.common.ServiceRegistry; import io.github.hectorvent.floci.core.storage.StorageFactory; import io.github.hectorvent.floci.lifecycle.inithook.InitializationHook; import io.github.hectorvent.floci.lifecycle.inithook.InitializationHooksRunner; +import io.github.hectorvent.floci.services.elasticache.container.ElastiCacheContainerManager; import io.github.hectorvent.floci.services.elasticache.proxy.ElastiCacheProxyManager; +import io.github.hectorvent.floci.services.rds.container.RdsContainerManager; import io.github.hectorvent.floci.services.rds.proxy.RdsProxyManager; import io.quarkus.runtime.StartupEvent; import org.junit.jupiter.api.BeforeEach; @@ -29,7 +31,9 @@ class EmulatorLifecycleTest { @Mock private ServiceRegistry serviceRegistry; @Mock private EmulatorConfig config; @Mock private EmulatorConfig.StorageConfig storageConfig; + @Mock private ElastiCacheContainerManager elastiCacheContainerManager; @Mock private ElastiCacheProxyManager elastiCacheProxyManager; + @Mock private RdsContainerManager rdsContainerManager; @Mock private RdsProxyManager rdsProxyManager; @Mock private InitializationHooksRunner initializationHooksRunner; @@ -42,7 +46,8 @@ class EmulatorLifecycleTest { when(storageConfig.persistentPath()).thenReturn("/app/data"); emulatorLifecycle = new EmulatorLifecycle( storageFactory, serviceRegistry, config, - elastiCacheProxyManager, rdsProxyManager, initializationHooksRunner); + elastiCacheContainerManager, elastiCacheProxyManager, + rdsContainerManager, rdsProxyManager, initializationHooksRunner); } @Test diff --git a/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheIntegrationTest.java index 510f41d5cd..f959b84dcc 100644 --- a/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheIntegrationTest.java @@ -1,6 +1,7 @@ package io.github.hectorvent.floci.services.elasticache; import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Assumptions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.MethodOrderer; @@ -9,6 +10,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; import java.io.IOException; +import java.util.List; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; @@ -43,6 +45,28 @@ class ElastiCacheIntegrationTest { Assumptions.assumeTrue(isDockerAvailable(), "Docker daemon must be available for ElastiCache integration tests"); } + @AfterAll + static void cleanup() { + // Best-effort cleanup of any resources created during tests. + // Prevents orphaned containers/state if a test fails mid-way. + for (String groupId : List.of(CROSS_GROUP_ID, GROUP_ID, GROUP_ID + "-reused")) { + try { + given() + .formParam("Action", "DeleteReplicationGroup") + .formParam("ReplicationGroupId", groupId) + .header("Authorization", AUTH_HEADER) + .post("/"); + } catch (Exception ignored) {} + } + try { + given() + .formParam("Action", "DeleteUser") + .formParam("UserId", USER_ID) + .header("Authorization", AUTH_HEADER) + .post("/"); + } catch (Exception ignored) {} + } + @Test @Order(1) void createReplicationGroup() {