mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -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
+36
-7
@@ -1,6 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.elasticache;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.elasticache.container.ElastiCacheContainerHandle;
|
||||
@@ -12,6 +13,8 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
@@ -119,9 +122,10 @@ class ElastiCacheServiceTest {
|
||||
assertThrows(RuntimeException.class,
|
||||
() -> service.createReplicationGroup("grp", "test", AuthMode.PASSWORD, null));
|
||||
|
||||
// Rollback stopped the proxy and the already-started container.
|
||||
// Rollback stops by the exact handle, not a fresh by-id lookup.
|
||||
verify(proxyManager).stopProxy("grp");
|
||||
verify(containerManager).stop(handle);
|
||||
verify(containerManager, never()).stopByGroupId(anyString());
|
||||
|
||||
// The reserved proxy port was released: a subsequent successful create reuses the base port
|
||||
// instead of skipping to the next one (which is what a leak would cause).
|
||||
@@ -134,18 +138,16 @@ class ElastiCacheServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedContainerStartReleasesPortWithoutTouchingProxyOrContainer() {
|
||||
// Container start blows up before any handle exists.
|
||||
doThrow(new RuntimeException("container boom"))
|
||||
void failedContainerStartupCleansUpContainerByIdAndReleasesPort() {
|
||||
// Models a readiness timeout: start() throws without ever returning a handle.
|
||||
doThrow(new RuntimeException("readiness boom"))
|
||||
.when(containerManager).start(eq("grp"), anyString());
|
||||
|
||||
// The original failure must propagate to the caller (we clean up, then rethrow).
|
||||
assertThrows(RuntimeException.class,
|
||||
() -> service.createReplicationGroup("grp", "test", AuthMode.PASSWORD, null));
|
||||
|
||||
// No container started and no proxy started, so rollback must not call either stop.
|
||||
verify(proxyManager, never()).stopProxy(anyString());
|
||||
verify(containerManager, never()).stop(any());
|
||||
verify(containerManager).stopByGroupId("grp");
|
||||
|
||||
// The reserved proxy port was still released: a subsequent successful create reuses the base port.
|
||||
when(containerManager.start(anyString(), anyString()))
|
||||
@@ -155,4 +157,31 @@ class ElastiCacheServiceTest {
|
||||
assertEquals(16379, recovered.getProxyPort(),
|
||||
"Port from the failed create must be released so the next group reuses it");
|
||||
}
|
||||
|
||||
@Test
|
||||
void concurrentCreateForSameGroupIdIsRejectedWhileFirstIsProvisioning() throws InterruptedException {
|
||||
CountDownLatch startedLatch = new CountDownLatch(1);
|
||||
CountDownLatch releaseLatch = new CountDownLatch(1);
|
||||
when(containerManager.start(anyString(), anyString())).thenAnswer(inv -> {
|
||||
startedLatch.countDown();
|
||||
assertTrue(releaseLatch.await(5, TimeUnit.SECONDS), "test timed out waiting for release");
|
||||
return new ElastiCacheContainerHandle("cid", "grp", "localhost", 6379);
|
||||
});
|
||||
|
||||
Thread firstRequest = new Thread(() ->
|
||||
service.createReplicationGroup("grp", "test", AuthMode.PASSWORD, null));
|
||||
firstRequest.start();
|
||||
assertTrue(startedLatch.await(5, TimeUnit.SECONDS), "first request never reached container start");
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> service.createReplicationGroup("grp", "test", AuthMode.PASSWORD, null));
|
||||
assertEquals("ReplicationGroupAlreadyExistsFault", ex.jsonType());
|
||||
verify(containerManager, never()).stop(any());
|
||||
verify(containerManager, never()).stopByGroupId(anyString());
|
||||
|
||||
releaseLatch.countDown();
|
||||
firstRequest.join(5000);
|
||||
|
||||
assertEquals("grp", service.getReplicationGroup("grp").getReplicationGroupId());
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package io.github.hectorvent.floci.services.elasticache.container;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerDetector;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
class ElastiCacheContainerManagerTest {
|
||||
|
||||
@Test
|
||||
void stopByGroupIdRemovesByDeterministicNameWhenNothingRegistered() {
|
||||
ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
|
||||
ElastiCacheContainerManager manager = new ElastiCacheContainerManager(
|
||||
mock(ContainerBuilder.class), lifecycleManager, mock(ContainerLogStreamer.class),
|
||||
mock(ContainerDetector.class), mock(EmulatorConfig.class), mock(RegionResolver.class));
|
||||
|
||||
// No container was ever registered for this id (e.g. it failed before registration).
|
||||
// Rollback must still fall back to the deterministic name so nothing is orphaned.
|
||||
manager.stopByGroupId("my-group");
|
||||
|
||||
verify(lifecycleManager).removeIfExists("floci-valkey-my-group");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user