From 43363c0f8defa3e36ec5c06c7b99ac8a5a3bec14 Mon Sep 17 00:00:00 2001 From: mrfelek <17274927+mrfelek@users.noreply.github.com> Date: Mon, 16 Nov 2026 19:24:05 +0100 Subject: [PATCH] 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 --- .../services/redshift/RedshiftService.java | 40 +++++++++++----- .../redshift/RedshiftServiceTest.java | 48 +++++++++++++++++++ 2 files changed, 76 insertions(+), 12 deletions(-) diff --git a/src/main/java/io/floci/aws/services/redshift/RedshiftService.java b/src/main/java/io/floci/aws/services/redshift/RedshiftService.java index 37f5dca412..a65b739164 100644 --- a/src/main/java/io/floci/aws/services/redshift/RedshiftService.java +++ b/src/main/java/io/floci/aws/services/redshift/RedshiftService.java @@ -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 subnetIds, Map 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); + } } /** diff --git a/src/test/java/io/floci/aws/services/redshift/RedshiftServiceTest.java b/src/test/java/io/floci/aws/services/redshift/RedshiftServiceTest.java index bd4bc95354..29542c5e7a 100644 --- a/src/test/java/io/floci/aws/services/redshift/RedshiftServiceTest.java +++ b/src/test/java/io/floci/aws/services/redshift/RedshiftServiceTest.java @@ -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 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 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 ParkingStore backedByParkingStore(AccountAwareStorageBackend backend) { + ParkingStore 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"));