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 a65b739164..65ec401af6 100644 --- a/src/main/java/io/floci/aws/services/redshift/RedshiftService.java +++ b/src/main/java/io/floci/aws/services/redshift/RedshiftService.java @@ -3102,13 +3102,17 @@ public class RedshiftService implements ManagedSecretRotator { } public ClusterParameterGroup deleteClusterParameterGroup(String parameterGroupName) { - Optional groupOpt = parameterGroups.get(regionalKey(parameterGroupName)); - if (groupOpt.isEmpty()) { - throw new AwsException("ClusterParameterGroupNotFound", "Cluster parameter group " + parameterGroupName + " not found", 404); - } - ClusterParameterGroup group = groupOpt.get(); - parameterGroups.delete(regionalKey(parameterGroupName)); - parameterGroups.flush(); + // The read and the delete under the service's lock (mutate), which modifyClusterParameterGroup and + // resetClusterParameterGroup hold from their read to their write, so a delete can't land in between and be + // undone by their write; the event is emitted after it. + ClusterParameterGroup group = mutate(() -> { + ClusterParameterGroup found = parameterGroups.get(regionalKey(parameterGroupName)) + .orElseThrow(() -> new AwsException("ClusterParameterGroupNotFound", + "Cluster parameter group " + parameterGroupName + " not found", 404)); + parameterGroups.delete(regionalKey(parameterGroupName)); + parameterGroups.flush(); + return found; + }); emitEvent(RedshiftEventCatalog.PARAMETER_GROUP_DELETED, parameterGroupName, "Cluster parameter group '" + parameterGroupName + "' was deleted at " + eventTime() + "."); return group; @@ -3267,11 +3271,16 @@ public class RedshiftService implements ManagedSecretRotator { } public ClusterSubnetGroup deleteClusterSubnetGroup(String name) { - ClusterSubnetGroup group = subnetGroups.get(regionalKey(name)) - .orElseThrow(() -> new AwsException("ClusterSubnetGroupNotFoundFault", "Cluster subnet group " + name + " not found", 400)); - subnetGroups.delete(regionalKey(name)); - subnetGroups.flush(); - return group; + // The read and the delete under the service's lock (mutate), which modifyClusterSubnetGroup holds from its + // read to its write, so a delete can't land in between and be undone by that write. + return mutate(() -> { + ClusterSubnetGroup group = subnetGroups.get(regionalKey(name)) + .orElseThrow(() -> new AwsException("ClusterSubnetGroupNotFoundFault", + "Cluster subnet group " + name + " not found", 400)); + subnetGroups.delete(regionalKey(name)); + subnetGroups.flush(); + return group; + }); } // ── Snapshot Copy Grant Operations ─────────────────────────────────────── 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 29542c5e7a..50dfb42295 100644 --- a/src/test/java/io/floci/aws/services/redshift/RedshiftServiceTest.java +++ b/src/test/java/io/floci/aws/services/redshift/RedshiftServiceTest.java @@ -4206,7 +4206,7 @@ class RedshiftServiceTest { 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. */ + /** {@code backend}'s reads, writes and deletes, 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))); @@ -4214,9 +4214,43 @@ class RedshiftServiceTest { store.put(call.getArgument(0), call.getArgument(1)); return null; }).when(backend).put(anyString(), any()); + doAnswer(call -> { + store.delete(call.getArgument(0)); + return null; + }).when(backend).delete(anyString()); return store; } + /** + * A delete of a subnet group that a modify has read and is writing back waits for that write, then deletes it: + * the group stays deleted rather than being put back by the modify. + */ + @Test + void aDeleteOfASubnetGroupWaitsForItsModifyAndTheGroupStaysDeleted() throws Exception { + ParkingStore groups = backedByParkingStore(subnetGroupBackend); + groups.put(regionalKey("racy"), new ClusterSubnetGroup("racy", "d", "vpc-1", List.of("subnet-1"))); + groups.parkNextWrite(); + + assertOperationWaitsForParkedStore(groups, () -> service.modifyClusterSubnetGroup("racy", "changed", null), + () -> service.deleteClusterSubnetGroup("racy")); + + assertEquals(Set.of(), groups.keys()); + } + + /** As above, for a cluster parameter group. */ + @Test + void aDeleteOfAParameterGroupWaitsForItsModifyAndTheGroupStaysDeleted() throws Exception { + ParkingStore groups = backedByParkingStore(parameterGroupBackend); + groups.put(regionalKey("racy"), new ClusterParameterGroup("racy", "redshift-1.0", "d")); + groups.parkNextWrite(); + + assertOperationWaitsForParkedStore(groups, () -> service.modifyClusterParameterGroup("racy", + List.of(new Parameter("max_cursor_result_set_size", "1000"))), + () -> service.deleteClusterParameterGroup("racy")); + + assertEquals(Set.of(), groups.keys()); + } + /** * 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}.