fix(redshift): keep two creates of one parameter or subnet group name from both succeeding (#8605)

* fix(redshift): keep two creates of one parameter or subnet group name from both succeeding
* fix(redshift): refuse a taken subnet group name before asking EC2 about its subnets
This commit is contained in:
mrfelek authored and GitHub committed 2026-11-16 14:24:05 -04:00
1 parent faf2287433
commit 43363c0f8d
2 files changed
+76 -12

No files matched your search

@@ -2894,16 +2894,21 @@ public class RedshiftService implements ManagedSecretRotator {
if (DEFAULT_PARAMETER_GROUP_NAME.equals(parameterGroupName)) {
throw new AwsException("InvalidParameterValue", "The default parameter group name is reserved.", 400);
}
if (parameterGroups.get(regionalKey(parameterGroupName)).isPresent()) {
throw new AwsException("ClusterParameterGroupAlreadyExists", "Cluster parameter group " + parameterGroupName + " already exists", 400);
}
ClusterParameterGroup group = new ClusterParameterGroup(parameterGroupName, parameterGroupFamily, description);
if (tags != null) {
group.setTags(new LinkedHashMap<>(tags));
}
parameterGroups.put(regionalKey(parameterGroupName), group);
parameterGroups.flush();
// The free-name check and the write under the service's lock (mutate), so two creates of one name can't both
// pass the check; the event is emitted after it.
mutate(() -> {
if (parameterGroups.get(regionalKey(parameterGroupName)).isPresent()) {
throw new AwsException("ClusterParameterGroupAlreadyExists",
"Cluster parameter group " + parameterGroupName + " already exists", 400);
}
parameterGroups.put(regionalKey(parameterGroupName), group);
parameterGroups.flush();
return group;
});
emitEvent(RedshiftEventCatalog.PARAMETER_GROUP_CREATED, parameterGroupName,
"Cluster parameter group '" + parameterGroupName + "' was created.");
return group;
@@ -3193,17 +3198,28 @@ public class RedshiftService implements ManagedSecretRotator {
public ClusterSubnetGroup createClusterSubnetGroup(String name, String description, String vpcId,
List<String> subnetIds, Map<String, String> tags) {
if (subnetGroups.get(regionalKey(name)).isPresent()) {
throw new AwsException("ClusterSubnetGroupAlreadyExists", "Cluster subnet group " + name + " already exists", 400);
}
// A name taken already is refused before EC2 is asked about the subnets, as before.
requireFreeSubnetGroupName(name);
ClusterSubnetGroup group = new ClusterSubnetGroup(name, description, vpcId, subnetIds);
if (tags != null) {
group.setTags(new LinkedHashMap<>(tags));
}
resolveSubnets(group);
subnetGroups.put(regionalKey(name), group);
subnetGroups.flush();
return group;
// The free-name check again, with the write, under the service's lock (mutate), so two creates of one name
// can't both pass it; EC2 is asked about the subnets before it.
return mutate(() -> {
requireFreeSubnetGroupName(name);
subnetGroups.put(regionalKey(name), group);
subnetGroups.flush();
return group;
});
}
private void requireFreeSubnetGroupName(String name) {
if (subnetGroups.get(regionalKey(name)).isPresent()) {
throw new AwsException("ClusterSubnetGroupAlreadyExists",
"Cluster subnet group " + name + " already exists", 400);
}
}
/**
@@ -40,6 +40,7 @@ import io.floci.aws.services.redshiftserverless.model.Namespace;
import io.floci.aws.services.s3.S3Service;
import io.floci.aws.services.secretsmanager.SecretsManagerService;
import io.floci.aws.services.secretsmanager.model.Secret;
import io.floci.aws.testing.ParkingStore;
import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.MultivaluedMap;
import org.junit.jupiter.api.BeforeEach;
@@ -75,6 +76,7 @@ import java.util.stream.Collectors;
import java.util.stream.Stream;
import static io.floci.aws.testing.InlineMocks.inlineMock;
import static io.floci.aws.testing.ParkingStoreAssertions.assertOperationWaitsForParkedStore;
import static io.floci.aws.testing.ThreadStates.awaitBlockedOrDone;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -4180,6 +4182,52 @@ class RedshiftServiceTest {
service.createClusterSubnetGroup("existing", "d", "vpc-1", List.of("subnet-1")));
}
/** Two creates of one subnet group name at once: the second waits for the first, then finds the name taken. */
@Test
void twoCreatesOfOneSubnetGroupNameCanNotBothSucceed() throws Exception {
ParkingStore<ClusterSubnetGroup> groups = backedByParkingStore(subnetGroupBackend);
assertOnlyTheFirstCreateSucceeds(groups,
() -> service.createClusterSubnetGroup("racy", "d", "vpc-1", List.of("subnet-1")),
"ClusterSubnetGroupAlreadyExists");
assertEquals(Set.of(regionalKey("racy")), groups.keys());
}
/** As above, for a cluster parameter group. */
@Test
void twoCreatesOfOneParameterGroupNameCanNotBothSucceed() throws Exception {
ParkingStore<ClusterParameterGroup> groups = backedByParkingStore(parameterGroupBackend);
assertOnlyTheFirstCreateSucceeds(groups,
() -> service.createClusterParameterGroup("racy", "redshift-1.0", "d"),
"ClusterParameterGroupAlreadyExists");
assertEquals(Set.of(regionalKey("racy")), groups.keys());
}
/** {@code backend}'s reads and writes, served by a store a test can hold inside its next write. */
private static <V> ParkingStore<V> backedByParkingStore(AccountAwareStorageBackend<V> backend) {
ParkingStore<V> store = new ParkingStore<>();
when(backend.get(anyString())).thenAnswer(call -> store.get(call.getArgument(0)));
doAnswer(call -> {
store.put(call.getArgument(0), call.getArgument(1));
return null;
}).when(backend).put(anyString(), any());
return store;
}
/**
* Holds the first {@code create} inside its write, after its check that the name is free, and runs a second one:
* the second must wait for the first, then answer {@code alreadyExists}.
*/
private static void assertOnlyTheFirstCreateSucceeds(ParkingStore<?> store, Runnable create, String alreadyExists)
throws Exception {
store.parkNextWrite();
assertOperationWaitsForParkedStore(store, create,
() -> assertEquals(alreadyExists, assertThrows(AwsException.class, create::run).getErrorCode()));
}
@Test
void describeClusterSubnetGroups() {
ClusterSubnetGroup group = new ClusterSubnetGroup("my-group", "d", "vpc-1", List.of("subnet-1"));