mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
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.
This commit is contained in:
1 parent
3ed65caf07
commit
0de2b7fdd8
2 files changed
+72
-4
No files matched your search
@@ -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);
|
||||
|
||||
+45
@@ -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());
|
||||
|
||||
Reference in new issue
Block a user