diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftService.java b/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftService.java index 672b7d1602..b0b76d9202 100644 --- a/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftService.java +++ b/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftService.java @@ -955,21 +955,44 @@ public class RedshiftService implements ManagedSecretRotator { createSnapshot(finalSnapshotIdentifier, identifier, retentionPeriod); } - Cluster removed = mutate(() -> removeCluster(identifier, cluster)); + Cluster removed = mutate(() -> { + try { + stopProxyBeforeRemoval(identifier, cluster); + } catch (RuntimeException e) { + if (!skipSnapshot) { + discardUnusedFinalSnapshot(finalSnapshotIdentifier, e); + } + throw e; + } + return removeCluster(identifier, cluster); + }); emitEvent(RedshiftEventCatalog.CLUSTER_DELETED, identifier, "Amazon Redshift cluster '" + identifier + "' has been deleted at " + eventTime() + ". A final snapshot was " + (skipSnapshot ? "not " : "") + "saved."); return removed; } - private Cluster removeCluster(String identifier, Cluster cluster) { - // Tear down the auth proxy and return its port before stopping the container. - // If it fails, abort deletion so the metadata remains and the user can retry. + // Only valid before the container is touched: the database is intact, the caller will retry with the + // same final snapshot identifier, and a leftover snapshot would turn that retry into + // ClusterSnapshotAlreadyExists. Once the container teardown starts the snapshot may be the only copy. + private void discardUnusedFinalSnapshot(String finalSnapshotIdentifier, RuntimeException deletionFailure) { + try { + removeSnapshot(finalSnapshotIdentifier); + } catch (RuntimeException cleanupFailure) { + deletionFailure.addSuppressed(cleanupFailure); + } + } + + // Tear down the auth proxy and return its port before stopping the container. + // If it fails, abort deletion so the metadata remains and the user can retry. + private void stopProxyBeforeRemoval(String identifier, Cluster cluster) { if (!stopProxyAndReleasePortSafely(identifier, cluster.getProxyPort())) { throw new AwsException("InternalFailure", "Failed to stop auth proxy; cluster " + identifier + " was not deleted", 500); } + } + private Cluster removeCluster(String identifier, Cluster cluster) { containerManager.stop(clusters.accountId(), identifier); proxyManager.forgetSpectrumRuntime(relayKey(clusters.accountId(), identifier)); clusters.delete(identifier); diff --git a/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftFinalSnapshotStorageTest.java b/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftFinalSnapshotStorageTest.java index 32cc6235d5..25b68a5aa2 100644 --- a/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftFinalSnapshotStorageTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftFinalSnapshotStorageTest.java @@ -98,6 +98,51 @@ class RedshiftFinalSnapshotStorageTest { verifyNoInteractions(fixture.proxy()); } + @Test + void deleteClusterRetryAfterProxyStopFailureReusesFinalSnapshotIdentifier() { + Fixture fixture = fixture(); + seedCluster(fixture); + doThrow(new IllegalStateException("proxy stop failed")).doNothing() + .when(fixture.proxy()).stopProxy(anyString()); + + assertThrows(AwsException.class, () -> delete(fixture)); + assertEquals("available", fixture.service().describeClusters("final-cluster").getFirst().getClusterStatus()); + + assertDoesNotThrow(() -> delete(fixture)); + assertTrue(fixture.service().describeClusters(null).isEmpty()); + } + + @Test + void deleteClusterKeepsFinalSnapshotWhenContainerTeardownFails() { + Fixture fixture = fixture(); + seedCluster(fixture); + doThrow(new IllegalStateException("container removal failed")) + .when(fixture.manager()).stop(anyString(), anyString()); + + assertThrows(IllegalStateException.class, () -> delete(fixture)); + + assertTrue(fixture.service().getSnapshot("final-backup").isPresent()); + assertTrue(Files.exists(directory.resolve("redshift-dumps").resolve(ACCOUNT).resolve("final-backup.sql"))); + } + + @Test + void deleteClusterAttachesFinalSnapshotCleanupFailureToTheProxyStopFailure() { + Fixture fixture = fixture(); + seedCluster(fixture); + IllegalStateException proxyFailure = new IllegalStateException("proxy stop failed"); + doAnswer(invocation -> { + fixture.service().deleteSnapshot("final-backup"); + throw proxyFailure; + }).when(fixture.proxy()).stopProxy(anyString()); + + AwsException failure = assertThrows(AwsException.class, () -> delete(fixture)); + + assertEquals("InternalFailure", failure.getErrorCode()); + assertEquals(1, failure.getSuppressed().length); + assertInstanceOf(AwsException.class, failure.getSuppressed()[0]); + assertEquals("ClusterSnapshotNotFound", ((AwsException) failure.getSuppressed()[0]).getErrorCode()); + } + private Fixture fixture() { EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS); when(config.storage().persistentPath()).thenReturn(directory.toString());