From 31633804de470d35b02ab814a5b356830a238d2b Mon Sep 17 00:00:00 2001 From: Niels Kootstra <545768+nkootstra@users.noreply.github.com> Date: Tue, 3 Nov 2026 21:56:07 +0100 Subject: [PATCH] fix(memorydb): check for an existing cluster after claiming its name, so a create that just ended is seen (#7441) * fix(memorydb): check for an existing cluster after claiming its name, so a create that just ended is seen * test(memorydb): wait for both racing creates to end before counting their outcomes * test(memorydb): build the racing creates' spec with the existing cluster helper --- .../services/memorydb/MemoryDbService.java | 11 +-- .../memorydb/MemoryDbServiceTest.java | 73 ++++++++++++++++++- 2 files changed, 77 insertions(+), 7 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/memorydb/MemoryDbService.java b/src/main/java/io/github/hectorvent/floci/services/memorydb/MemoryDbService.java index 2481ed8c85..bfb287e5f1 100644 --- a/src/main/java/io/github/hectorvent/floci/services/memorydb/MemoryDbService.java +++ b/src/main/java/io/github/hectorvent/floci/services/memorydb/MemoryDbService.java @@ -101,18 +101,19 @@ public class MemoryDbService { throw new AwsException("InvalidParameterValueException", "ClusterName is required.", 400); } String resourceKey = key(region, name); - if (clusters.get(resourceKey).isPresent() || legacyGet(clusters, name, region).isPresent()) { - throw new AwsException("ClusterAlreadyExistsFault", - "Cluster with specified name already exists.", 400); - } // Claim the name for the whole provisioning attempt so a concurrent create can't race - // ahead and be stopped by this request's handle-less rollback fallback. + // ahead and be stopped by this request's handle-less rollback fallback. The existence + // check follows the claim, so a create that ended before it is seen. if (!provisioningClusterNames.add(resourceKey)) { throw new AwsException("ClusterAlreadyExistsFault", "Cluster " + name + " is already being created.", 400); } try { + if (clusters.get(resourceKey).isPresent() || legacyGet(clusters, name, region).isPresent()) { + throw new AwsException("ClusterAlreadyExistsFault", + "Cluster with specified name already exists.", 400); + } String aclName = spec.getAclName(); if (aclName == null || aclName.isBlank()) { throw new AwsException("InvalidParameterValueException", "ACLName is required.", 400); diff --git a/src/test/java/io/github/hectorvent/floci/services/memorydb/MemoryDbServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/memorydb/MemoryDbServiceTest.java index 444cc4dd97..4be235f642 100644 --- a/src/test/java/io/github/hectorvent/floci/services/memorydb/MemoryDbServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/memorydb/MemoryDbServiceTest.java @@ -21,11 +21,15 @@ import org.junit.jupiter.api.Test; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; @@ -33,10 +37,13 @@ import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doAnswer; 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.spy; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; @@ -48,6 +55,8 @@ class MemoryDbServiceTest { private MemoryDbProxyManager proxyManager; private SigV4Validator sigV4Validator; private EmulatorConfig.MemoryDbServiceConfig mdbConfig; + private EmulatorConfig config; + private RegionResolver regionResolver; @BeforeEach void setUp() { @@ -55,8 +64,8 @@ class MemoryDbServiceTest { proxyManager = mock(MemoryDbProxyManager.class); sigV4Validator = mock(SigV4Validator.class); StorageFactory storageFactory = mock(StorageFactory.class); - EmulatorConfig config = mock(EmulatorConfig.class); - RegionResolver regionResolver = mock(RegionResolver.class); + config = mock(EmulatorConfig.class); + regionResolver = mock(RegionResolver.class); EmulatorConfig.ServicesConfig servicesConfig = mock(EmulatorConfig.ServicesConfig.class); mdbConfig = mock(EmulatorConfig.MemoryDbServiceConfig.class); @@ -464,6 +473,66 @@ class MemoryDbServiceTest { assertEquals(ClusterStatus.AVAILABLE, service.getCluster("c1").getStatus()); } + /** + * A create that checked the name before another create of it finished must not go on to start + * a second cluster once the first has released its claim: exactly one of the two creates + * succeeds and the other is refused, whichever order they end up in. + */ + @Test + void createRacingAFinishingCreateOfTheSameNameStartsOneCluster() throws Exception { + AccountAwareStorageBackend clusterStore = spy(AccountAwareStorageBackend.inMemory("000000000000")); + StorageFactory storageFactory = mock(StorageFactory.class); + when(storageFactory.create(anyString(), anyString(), any())).thenAnswer(inv -> + "memorydb-clusters.json".equals(inv.getArgument(1)) + ? clusterStore + : AccountAwareStorageBackend.inMemory("000000000000")); + MemoryDbService racedService = new MemoryDbService(containerManager, proxyManager, sigV4Validator, + storageFactory, config, regionResolver); + CountDownLatch slowCreateLookedUp = new CountDownLatch(1); + CountDownLatch otherCreateDone = new CountDownLatch(1); + AtomicBoolean heldOnce = new AtomicBoolean(); + doAnswer(inv -> { + Object found = inv.callRealMethod(); + if ("slow-create".equals(Thread.currentThread().getName()) && heldOnce.compareAndSet(false, true)) { + slowCreateLookedUp.countDown(); + assertTrue(otherCreateDone.await(5, TimeUnit.SECONDS), "the other create never ended"); + } + return found; + }).when(clusterStore).get(anyString()); + + CompletableFuture slow = new CompletableFuture<>(); + Thread slowCreate = new Thread(() -> { + try { + slow.complete(racedService.createCluster(cluster("raced", "open-access"), "us-east-1")); + } catch (RuntimeException e) { + slow.completeExceptionally(e); + } + }, "slow-create"); + slowCreate.start(); + assertTrue(slowCreateLookedUp.await(5, TimeUnit.SECONDS), "the slow create never looked the name up"); + CompletableFuture other = new CompletableFuture<>(); + try { + other.complete(racedService.createCluster(cluster("raced", "open-access"), "us-east-1")); + } catch (RuntimeException e) { + other.completeExceptionally(e); + } finally { + otherCreateDone.countDown(); + } + + int succeeded = 0; + for (CompletableFuture outcome : List.of(slow, other)) { + try { + outcome.get(5, TimeUnit.SECONDS); + succeeded++; + } catch (ExecutionException refused) { + AwsException cause = assertInstanceOf(AwsException.class, refused.getCause()); + assertEquals("ClusterAlreadyExistsFault", cause.jsonType()); + } + } + assertEquals(1, succeeded, "exactly one create of the name succeeds"); + verify(containerManager, times(1)).tryStart(anyString(), anyString()); + } + @Test void sameNamesAreScopedToRegion() { User firstUser = passwordUser("shared-user", "one");