mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(elasticache): clean up container by id on readiness-timeout rollback (#2090)
* fix(elasticache): clean up container by id on readiness-timeout rollback start() registers the container in activeContainers before its waitForBackendReady() probe, and that probe throws on timeout. When it does, start() propagates without ever returning a handle, so the service's rollback saw handle == null, skipped the container stop, and left it running (only reaped at shutdown). A readiness timeout is the most likely provisioning failure, not an edge case. Add an idempotent stopByGroupId(groupId) that looks the container up in activeContainers, falling back to the deterministic container name for one that failed before registration. Call it unconditionally from rollbackReplicationGroup instead of gating containerManager.stop(handle) on handle != null. Mirrors the same fix already merged for Neptune in #1742. * fix(elasticache): stop by handle when known, only fall back to stopByGroupId Greptile flagged (PR #2090 review): rollbackReplicationGroup called containerManager.stopByGroupId(groupId) unconditionally, even when the handle from this request's own start() was already known. If a concurrent CreateReplicationGroup for the same group id raced ahead and overwrote the activeContainers entry before this rollback ran, stopByGroupId's fresh lookup would fetch and stop the other request's container instead of this one's. Restore the precise containerManager.stop(handle) path whenever handle is non-null (the common case — every failure after the container itself started successfully) and only use stopByGroupId as the by-id fallback for the one case that actually needs it: a readiness timeout, where start() never returns a handle at all. * fix(elasticache): guard against concurrent creates for the same group id Reviewer follow-up on PR #2090: the handle-less rollback path (a readiness timeout, where start() never returns a handle) still cleans up by looking up whatever containerManager currently has registered for the group id. If a second CreateReplicationGroup for that same id raced in and started its own container before the first request's rollback ran, that lookup would fetch and stop the second request's container instead of the first's — the existing "already exists" check only guards against a *persisted* group, not one still being provisioned, so nothing previously prevented two concurrent creates for the same not-yet-existing id from both proceeding. Add a provisioningGroupIds guard: claim the id before provisioning starts and hold it until createReplicationGroup returns or throws (rollback included), rejecting a concurrent second create for the same id with the same ReplicationGroupAlreadyExistsFault used for an already-persisted group. This closes the race at its root rather than narrowing it — a second request for the same id can never reach containerManager.start() while the first is still in flight, so the by-id rollback fallback can never observe a container it doesn't own. Added a concurrency test using real threads + latches to prove a second create for an in-flight id is rejected immediately rather than racing ahead. * refactor(elasticache): trim comments to one-liners
This commit is contained in:
1 parent
11d527547a
commit
91b6a40bc7
4 files changed
+129
-35
No files matched your search
+41
-27
@@ -39,6 +39,7 @@ public class ElastiCacheService {
|
||||
private final ElastiCacheProxyManager proxyManager;
|
||||
private final EmulatorConfig config;
|
||||
private final Set<Integer> usedPorts = ConcurrentHashMap.newKeySet();
|
||||
private final Set<String> provisioningGroupIds = ConcurrentHashMap.newKeySet();
|
||||
|
||||
@Inject
|
||||
public ElastiCacheService(ElastiCacheContainerManager containerManager,
|
||||
@@ -60,38 +61,48 @@ public class ElastiCacheService {
|
||||
throw new AwsException("ReplicationGroupAlreadyExistsFault",
|
||||
"Replication group " + groupId + " already exists.", 400);
|
||||
}
|
||||
// Claim the id for the whole provisioning attempt so a concurrent create can't race
|
||||
// ahead and be stopped by this request's handle-less rollback fallback.
|
||||
if (!provisioningGroupIds.add(groupId)) {
|
||||
throw new AwsException("ReplicationGroupAlreadyExistsFault",
|
||||
"Replication group " + groupId + " is already being created.", 400);
|
||||
}
|
||||
|
||||
int proxyPort = allocateProxyPort();
|
||||
String image = config.services().elasticache().defaultImage();
|
||||
|
||||
LOG.infov("Creating replication group {0} with authMode={1} on proxy port {2}",
|
||||
groupId, authMode, String.valueOf(proxyPort));
|
||||
|
||||
ElastiCacheContainerHandle handle = null;
|
||||
try {
|
||||
handle = containerManager.start(groupId, image);
|
||||
int proxyPort = allocateProxyPort();
|
||||
String image = config.services().elasticache().defaultImage();
|
||||
|
||||
String endpointHost = resolveEndpointHost();
|
||||
Endpoint endpoint = new Endpoint(endpointHost, proxyPort);
|
||||
ReplicationGroup group = new ReplicationGroup(
|
||||
groupId, description, ReplicationGroupStatus.AVAILABLE,
|
||||
authMode, endpoint, Instant.now(), proxyPort);
|
||||
group.setContainerId(handle.getContainerId());
|
||||
group.setContainerHost(handle.getHost());
|
||||
group.setContainerPort(handle.getPort());
|
||||
group.setAuthToken(authToken);
|
||||
LOG.infov("Creating replication group {0} with authMode={1} on proxy port {2}",
|
||||
groupId, authMode, String.valueOf(proxyPort));
|
||||
|
||||
proxyManager.startProxy(groupId, authMode, proxyPort,
|
||||
handle.getHost(), handle.getPort(),
|
||||
(username, password) -> validatePassword(groupId, username, password));
|
||||
ElastiCacheContainerHandle handle = null;
|
||||
try {
|
||||
handle = containerManager.start(groupId, image);
|
||||
|
||||
groups.put(groupId, group);
|
||||
LOG.infov("Replication group {0} created, endpoint={1}:{2}", groupId, endpointHost, String.valueOf(proxyPort));
|
||||
return group;
|
||||
} catch (RuntimeException e) {
|
||||
LOG.warnv("Replication group {0} provisioning failed, rolling back: {1}", groupId, e.getMessage());
|
||||
rollbackReplicationGroup(groupId, handle, proxyPort);
|
||||
throw e;
|
||||
String endpointHost = resolveEndpointHost();
|
||||
Endpoint endpoint = new Endpoint(endpointHost, proxyPort);
|
||||
ReplicationGroup group = new ReplicationGroup(
|
||||
groupId, description, ReplicationGroupStatus.AVAILABLE,
|
||||
authMode, endpoint, Instant.now(), proxyPort);
|
||||
group.setContainerId(handle.getContainerId());
|
||||
group.setContainerHost(handle.getHost());
|
||||
group.setContainerPort(handle.getPort());
|
||||
group.setAuthToken(authToken);
|
||||
|
||||
proxyManager.startProxy(groupId, authMode, proxyPort,
|
||||
handle.getHost(), handle.getPort(),
|
||||
(username, password) -> validatePassword(groupId, username, password));
|
||||
|
||||
groups.put(groupId, group);
|
||||
LOG.infov("Replication group {0} created, endpoint={1}:{2}", groupId, endpointHost, String.valueOf(proxyPort));
|
||||
return group;
|
||||
} catch (RuntimeException e) {
|
||||
LOG.warnv("Replication group {0} provisioning failed, rolling back: {1}", groupId, e.getMessage());
|
||||
rollbackReplicationGroup(groupId, handle, proxyPort);
|
||||
throw e;
|
||||
}
|
||||
} finally {
|
||||
provisioningGroupIds.remove(groupId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,6 +117,9 @@ public class ElastiCacheService {
|
||||
try {
|
||||
if (handle != null) {
|
||||
containerManager.stop(handle);
|
||||
} else {
|
||||
// No handle: a readiness timeout throws before start() can return one.
|
||||
containerManager.stopByGroupId(groupId);
|
||||
}
|
||||
} catch (RuntimeException e) {
|
||||
LOG.warnv("Error stopping container for replication group {0}: {1}", groupId, e.getMessage());
|
||||
|
||||
+23
-1
@@ -72,7 +72,7 @@ public class ElastiCacheContainerManager {
|
||||
public ElastiCacheContainerHandle start(String groupId, String image) {
|
||||
LOG.infov("Starting ElastiCache backend container for group: {0}", groupId);
|
||||
|
||||
String containerName = ContainerStorageHelper.resourceName(config, "valkey", null, groupId);
|
||||
String containerName = containerName(groupId);
|
||||
|
||||
// Remove any stale container with the same name
|
||||
lifecycleManager.removeIfExists(containerName);
|
||||
@@ -184,6 +184,28 @@ public class ElastiCacheContainerManager {
|
||||
lifecycleManager.stopAndRemove(handle.getContainerId(), handle.getLogStream());
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops and removes the backend container for a group by id, if one exists.
|
||||
* Used by the service's provisioning rollback: {@link #start} registers the container in
|
||||
* {@code activeContainers} <em>before</em> {@link #waitForBackendReady}, so a readiness
|
||||
* timeout throws without ever returning the handle to the caller. In that case rollback
|
||||
* can't go through {@link #stop} (it has no handle), so it cleans up by id instead. Falls
|
||||
* back to the deterministic container name to catch a container that failed before it was
|
||||
* registered. Idempotent — a no-op when nothing is running for the id.
|
||||
*/
|
||||
public void stopByGroupId(String groupId) {
|
||||
ElastiCacheContainerHandle handle = activeContainers.get(groupId);
|
||||
if (handle != null) {
|
||||
stop(handle);
|
||||
return;
|
||||
}
|
||||
lifecycleManager.removeIfExists(containerName(groupId));
|
||||
}
|
||||
|
||||
private String containerName(String groupId) {
|
||||
return ContainerStorageHelper.resourceName(config, "valkey", null, groupId);
|
||||
}
|
||||
|
||||
public void stopAll() {
|
||||
List<ElastiCacheContainerHandle> handles = new ArrayList<>(activeContainers.values());
|
||||
if (!handles.isEmpty()) {
|
||||
|
||||
Reference in new issue
Block a user