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
This commit is contained in:
TiM authored and GitHub committed 2026-04-13 11:07:49 -05:00
1 parent 702faf58e1
commit eb6749870b
5 files changed
+90 -3

No files matched your search

@@ -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();
}
@@ -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<String, ElastiCacheContainerHandle> 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<ElastiCacheContainerHandle> 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()) {
@@ -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<String, RdsContainerHandle> 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<String> envVars = buildEnvVars(engine, masterUsername, masterPassword, dbName);
List<String> 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<RdsContainerHandle> 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()) {