From 0de2b7fdd8618f844f5af606b79759e3bc56e9a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C5=A9=20Th=E1=BA=BF=20Huy?= <31652864+vuthehuyht@users.noreply.github.com> Date: Sat, 31 Oct 2026 19:45:26 +0700 Subject: [PATCH] fix(redshift): discard the final snapshot when DeleteCluster fails (#7072) * fix(redshift): discard the final snapshot when DeleteCluster fails DeleteCluster took the final snapshot before tearing the cluster down. When the teardown then failed, the cluster survived but the snapshot stayed behind, so retrying with the same FinalClusterSnapshotIdentifier failed with ClusterSnapshotAlreadyExists. Remove the snapshot when the cluster is still present after a failed removal. * fix(redshift): keep the final snapshot once container teardown has started The cleanup added for failed DeleteCluster also ran when containerManager.stop threw. That call removes the container and its volumes before closing the log stream, so a late failure leaves the cluster record behind with the database already gone, and the cleanup then deleted the only remaining copy. Limit the cleanup to the proxy-stop failure, which happens before anything is torn down, and cover both the kept-snapshot and the suppressed-cleanup-failure paths. --- .../services/redshift/RedshiftService.java | 31 +++++++++++-- .../RedshiftFinalSnapshotStorageTest.java | 45 +++++++++++++++++++ 2 files changed, 72 insertions(+), 4 deletions(-) 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());