mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
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
This commit is contained in:
1 parent
999abb3467
commit
31633804de
2 files changed
+77
-7
No files matched your search
@@ -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);
|
||||
|
||||
+71
-2
@@ -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<Cluster> clusterStore = spy(AccountAwareStorageBackend.<Cluster>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<Cluster> 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<Cluster> 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<Cluster> 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");
|
||||
|
||||
Reference in new issue
Block a user