fix(dynamodb): refuse a CreateTable that races another creator (#10035)

This commit is contained in:
XuJian authored and GitHub committed 2026-12-02 22:35:11 -04:00
1 parent b922f1be4b
commit 2f9cb8a2f5
2 files changed
+40 -1

No files matched your search

@@ -732,12 +732,21 @@ public class DynamoDbService {
lastCheck.run();
synchronized (capacityLock) {
// The check above is only a fast path: a creator that ran its whole validation while
// this one was still in it can have stored the table by now, so the name is decided
// here, next to the write that makes it visible.
if (tableStore.get(storageKey).isPresent()) {
throw new AwsException("ResourceInUseException",
"Table already exists: " + tableName, 400);
}
if (!"PAY_PER_REQUEST".equals(billingMode)) {
DynamoDbCapacityQuotas.requireNewTableWithinQuotas(table, reservedCapacity(region), region);
}
// The items go in before the table is visible, so a write another caller acknowledges
// against it can never be dropped by this call replacing the map afterwards.
itemsByTable.put(scopedItemsKey(storageKey), new ConcurrentSkipListMap<>());
tableStore.put(storageKey, table);
}
itemsByTable.put(scopedItemsKey(storageKey), new ConcurrentSkipListMap<>());
LOG.infov("Created table: {0} in region {1}", tableName, region);
return table;
}
@@ -5060,6 +5060,36 @@ class DynamoDbServiceTest {
assertEquals("ResourceInUseException", e.getErrorCode());
}
@Test
void createTable_secondCreatorWinsTheName_returnsResourceInUseException() {
// A rival creator that stores the table between this call's existence check and its store
// write is what a second client calling CreateTable at the same moment does.
TableDefinition[] rival = new TableDefinition[1];
AwsException refused = assertThrows(AwsException.class, () -> service.createTable("Users",
List.of(new KeySchemaElement("userId", "HASH")),
List.of(new AttributeDefinition("userId", "S")),
5L, 5L, List.of(), List.of(), List.of(), null, "us-east-1",
() -> rival[0] = createUsersTable("us-east-1")));
assertEquals("ResourceInUseException", refused.getErrorCode());
assertEquals(rival[0].getTableId(), service.describeTable("Users", "us-east-1").getTableId());
}
@Test
void createTable_secondCreatorWinsTheName_keepsTheAcknowledgedWrite() {
assertThrows(AwsException.class, () -> service.createTable("Users",
List.of(new KeySchemaElement("userId", "HASH")),
List.of(new AttributeDefinition("userId", "S")),
5L, 5L, List.of(), List.of(), List.of(), null, "us-east-1",
() -> {
createUsersTable("us-east-1");
service.putItem("Users", item("userId", "u1"), "us-east-1");
}));
assertNotNull(service.getItem("Users", item("userId", "u1"), "us-east-1"));
}
@Test
void exportTable_whileCreating_returnsTableNotFoundException() {
createUsersTableInCreating(service);