fix(opensearch): refuse a concurrent CreateDomain of a name already being created (#7435)

This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-11-03 15:06:54 -04:00
1 parent 7a7fef93d9
commit 5b955981fb
2 files changed
+85 -13

No files matched your search

@@ -35,6 +35,7 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@@ -58,6 +59,9 @@ public class OpenSearchService implements ResourceProvider {
private final RegionResolver regionResolver;
private final OpenSearchDomainManager domainManager;
private final ScheduledExecutorService poller = Executors.newSingleThreadScheduledExecutor();
// The account and storage key of each domain a createDomain is creating, from its existence
// check to its save, so a concurrent create of the same name is refused and starts no container.
private final Set<String> creating = ConcurrentHashMap.newKeySet();
@Inject
public OpenSearchService(StorageFactory storageFactory, EmulatorConfig config,
@@ -125,11 +129,6 @@ public class OpenSearchService implements ResourceProvider {
validateOptions(options);
String accountId = regionResolver.getAccountId();
if (findDomain(accountId, region, domainName).isPresent()) {
throw new AwsException("ResourceAlreadyExistsException",
"Domain with name " + domainName + " already exists.", 409);
}
Domain domain = new Domain();
domain.setDomainName(domainName);
domain.setDomainId(accountId + "/" + domainName);
@@ -159,18 +158,28 @@ public class OpenSearchService implements ResourceProvider {
}
applyDomainOptions(domain, options);
if (config.services().opensearch().mock()) {
domain.setProcessing(false);
domain.setEndpoint(synthesizedEndpoint(domain, region));
} else {
domain.setProcessing(true);
if (!domainManager.tryStartDomain(domain)) {
String creatingKey = accountId + "/" + storageKey(region, domainName);
if (!creating.add(creatingKey)) {
throw domainAlreadyExists(domainName);
}
try {
if (findDomain(accountId, region, domainName).isPresent()) {
throw domainAlreadyExists(domainName);
}
if (config.services().opensearch().mock()) {
domain.setProcessing(false);
domain.setEndpoint(synthesizedEndpoint(domain, region));
} else {
domain.setProcessing(true);
if (!domainManager.tryStartDomain(domain)) {
domain.setProcessing(false);
domain.setEndpoint(synthesizedEndpoint(domain, region));
}
}
domainStore.putForAccount(accountId, storageKey(region, domainName), domain);
} finally {
creating.remove(creatingKey);
}
domainStore.putForAccount(accountId, storageKey(region, domainName), domain);
LOG.infov("Created OpenSearch domain: {0}", domainName);
return domain;
}
@@ -401,6 +410,11 @@ public class OpenSearchService implements ResourceProvider {
}
}
private static AwsException domainAlreadyExists(String domainName) {
return new AwsException("ResourceAlreadyExistsException",
"Domain with name " + domainName + " already exists.", 409);
}
private static String storageKey(String region, String domainName) {
return region + "::" + domainName;
}
@@ -13,9 +13,15 @@ import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import java.util.List;
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.AtomicInteger;
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;
@@ -70,6 +76,58 @@ class OpenSearchServiceTest {
verify(domainManager).tryStartDomain(any());
}
/**
* Two concurrent creates of one name start one container: the second is refused as the name
* already exists, rather than starting a second container whose domain overwrites the first
* and leaves the first container running with nothing to stop it.
*/
@Test
void concurrentCreatesOfOneNameStartOneContainerAndRefuseTheSecond() throws Exception {
CountDownLatch firstStarting = new CountDownLatch(1);
CountDownLatch releaseFirst = new CountDownLatch(1);
CountDownLatch secondStarting = new CountDownLatch(1);
AtomicInteger starts = new AtomicInteger();
when(domainManager.tryStartDomain(any())).thenAnswer(invocation -> {
if (starts.incrementAndGet() == 1) {
firstStarting.countDown();
assertTrue(releaseFirst.await(10, TimeUnit.SECONDS), "the test never released the first start");
} else {
secondStarting.countDown();
}
return true;
});
CompletableFuture<Domain> first = CompletableFuture.supplyAsync(() -> service.createDomain(
"raced-domain", "OpenSearch_2.11", null, null, null, US_EAST_1));
assertTrue(firstStarting.await(10, TimeUnit.SECONDS), "the first create never started its container");
CompletableFuture<Domain> second = CompletableFuture.supplyAsync(() -> service.createDomain(
"raced-domain", "OpenSearch_2.11", null, null, null, US_EAST_1));
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(10);
while (secondStarting.getCount() > 0 && !second.isDone() && System.nanoTime() < deadline) {
Thread.sleep(10);
}
releaseFirst.countDown();
first.get(10, TimeUnit.SECONDS);
ExecutionException refused = assertThrows(ExecutionException.class, () -> second.get(10, TimeUnit.SECONDS));
AwsException cause = assertInstanceOf(AwsException.class, refused.getCause());
assertEquals("ResourceAlreadyExistsException", cause.getErrorCode());
assertEquals(1, starts.get());
}
@Test
void createDomainWhoseContainerFailedToStartCanBeRetried() {
when(domainManager.tryStartDomain(any()))
.thenThrow(new IllegalStateException("container failed to start"))
.thenReturn(true);
assertThrows(IllegalStateException.class, () -> service.createDomain(
"retried-domain", "OpenSearch_2.11", null, null, null, US_EAST_1));
Domain domain = service.createDomain("retried-domain", "OpenSearch_2.11", null, null, null, US_EAST_1);
assertEquals("retried-domain", domain.getDomainName());
}
/**
* The volume name and the ownership marker are persisted at creation, so deletion
* acts on what was stamped instead of recomputing ownership from whatever