Light, fluffy, and always free - The AWS Local Emulator alternative
fix(elasticache): roll back proxy port and container on failed provisioning (#1618)
Ahmed khaled8ba1e92713cb
diff --git a/src/main/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheService.java b/src/main/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheService.javaindex 0c8ff625a0..eee8d55ea4 100644--- a/src/main/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheService.java+++ b/src/main/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheService.java@@ -67,25 +67,51 @@ public class ElastiCacheService {LOG.infov("Creating replication group {0} with authMode={1} on proxy port {2}",groupId, authMode, String.valueOf(proxyPort));- ElastiCacheContainerHandle handle = containerManager.start(groupId, image);-- 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));+ ElastiCacheContainerHandle handle = null;+ try {+ handle = containerManager.start(groupId, image);++ 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;+ }+ }- groups.put(groupId, group);- LOG.infov("Replication group {0} created, endpoint={1}:{2}", groupId, endpointHost, String.valueOf(proxyPort));- return group;+ private void rollbackReplicationGroup(String groupId, ElastiCacheContainerHandle handle, int proxyPort) {+ try {+ if (handle != null) {+ proxyManager.stopProxy(groupId);+ }+ } catch (RuntimeException e) {+ LOG.warnv("Error stopping proxy for replication group {0}: {1}", groupId, e.getMessage());+ }+ try {+ if (handle != null) {+ containerManager.stop(handle);+ }+ } catch (RuntimeException e) {+ LOG.warnv("Error stopping container for replication group {0}: {1}", groupId, e.getMessage());+ } finally {+ releaseProxyPort(proxyPort);+ }}public ReplicationGroup getReplicationGroup(String groupId) {diff --git a/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheServiceTest.javaindex 830c771e3a..3e40fd552f 100644--- a/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheServiceTest.java@@ -6,29 +6,38 @@ import io.github.hectorvent.floci.core.storage.StorageFactory;import io.github.hectorvent.floci.services.elasticache.container.ElastiCacheContainerHandle;import io.github.hectorvent.floci.services.elasticache.container.ElastiCacheContainerManager;import io.github.hectorvent.floci.services.elasticache.model.AuthMode;+import io.github.hectorvent.floci.services.elasticache.model.ReplicationGroup;import io.github.hectorvent.floci.services.elasticache.proxy.ElastiCacheProxyManager;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;import java.util.List;+import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertFalse;+import static org.junit.jupiter.api.Assertions.assertThrows;import static org.junit.jupiter.api.Assertions.assertTrue;import static org.mockito.ArgumentMatchers.any;import static org.mockito.ArgumentMatchers.anyInt;import static org.mockito.ArgumentMatchers.anyString;+import static org.mockito.ArgumentMatchers.eq;import static org.mockito.Mockito.doNothing;+import static org.mockito.Mockito.doThrow;import static org.mockito.Mockito.mock;+import static org.mockito.Mockito.never;+import static org.mockito.Mockito.verify;import static org.mockito.Mockito.when;class ElastiCacheServiceTest {private ElastiCacheService service;+ private ElastiCacheContainerManager containerManager;+ private ElastiCacheProxyManager proxyManager;@BeforeEachvoid setUp() {- ElastiCacheContainerManager containerManager = mock(ElastiCacheContainerManager.class);- ElastiCacheProxyManager proxyManager = mock(ElastiCacheProxyManager.class);+ containerManager = mock(ElastiCacheContainerManager.class);+ proxyManager = mock(ElastiCacheProxyManager.class);StorageFactory storageFactory = mock(StorageFactory.class);EmulatorConfig config = mock(EmulatorConfig.class);@@ -95,4 +104,55 @@ class ElastiCacheServiceTest {// Single-arg AUTH with wrong password should failassertFalse(service.validatePassword("grp", null, "wrong-token"));}++ @Test+ void failedProvisioningRollsBackContainerAndReleasesProxyPort() {+ ElastiCacheContainerHandle handle =+ new ElastiCacheContainerHandle("cid", "grp", "localhost", 6379);+ when(containerManager.start(anyString(), anyString())).thenReturn(handle);++ // Proxy startup blows up after the port is reserved and the container is started.+ doThrow(new RuntimeException("proxy boom"))+ .when(proxyManager).startProxy(eq("grp"), any(), anyInt(), anyString(), anyInt(), any());++ // The original failure must propagate to the caller (we clean up, then rethrow).+ assertThrows(RuntimeException.class,+ () -> service.createReplicationGroup("grp", "test", AuthMode.PASSWORD, null));++ // Rollback stopped the proxy and the already-started container.+ verify(proxyManager).stopProxy("grp");+ verify(containerManager).stop(handle);++ // 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).+ doNothing().when(proxyManager)+ .startProxy(anyString(), any(), anyInt(), anyString(), anyInt(), any());+ ReplicationGroup recovered =+ service.createReplicationGroup("grp2", "test", AuthMode.PASSWORD, null);+ assertEquals(16379, recovered.getProxyPort(),+ "Port from the failed create must be released so the next group reuses it");+ }++ @Test+ void failedContainerStartReleasesPortWithoutTouchingProxyOrContainer() {+ // Container start blows up before any handle exists.+ doThrow(new RuntimeException("container 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());++ // The reserved proxy port was still released: a subsequent successful create reuses the base port.+ when(containerManager.start(anyString(), anyString()))+ .thenReturn(new ElastiCacheContainerHandle("cid", "grp2", "localhost", 6379));+ ReplicationGroup recovered =+ service.createReplicationGroup("grp2", "test", AuthMode.PASSWORD, null);+ assertEquals(16379, recovered.getProxyPort(),+ "Port from the failed create must be released so the next group reuses it");+ }}