mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(rds): roll back failed cluster member publication (#7408)
* fix(rds): roll back failed cluster member publication * fix(rds): release port after failed member writes * fix(rds): preserve ports when persistent deletion fails * fix(rds): keep rollback scoped to instance cleanup * fix(rds): retain port after uncertain WAL rollback
This commit is contained in:
1 parent
61d2a92373
commit
b8fd3da5f0
2 files changed
+789
-57
No files matched your search
@@ -74,6 +74,7 @@ import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.UncheckedIOException;
|
||||
import java.security.SecureRandom;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
@@ -907,7 +908,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
} catch (RuntimeException | Error failure) {
|
||||
synchronized (instance) {
|
||||
rollbackFailedStandaloneInstanceCreate(failure, instance, accountId,
|
||||
effectiveRegion, false, false, mock);
|
||||
effectiveRegion, false, false, false, mock);
|
||||
}
|
||||
throw failure;
|
||||
}
|
||||
@@ -954,40 +955,37 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
}
|
||||
}
|
||||
}
|
||||
List<String> originalMembers = currentCluster == null
|
||||
? null : new ArrayList<>(currentCluster.getDbClusterMembers());
|
||||
String originalWriter = currentCluster == null
|
||||
? null : currentCluster.getClusterWriterIdentifier();
|
||||
boolean instanceWriteStarted = false;
|
||||
boolean proxyStartAttempted = false;
|
||||
boolean memberMutationStarted = false;
|
||||
try {
|
||||
if (manageInstancePassword) {
|
||||
attachManagedMasterUserSecret(
|
||||
instance, effectiveRegion, masterUserSecretKmsKeyId, effectiveSecretKmsKeyId);
|
||||
}
|
||||
instanceWriteStarted = true;
|
||||
putInstanceForScope(accountId, effectiveRegion, id, instance);
|
||||
|
||||
if (!mock && hasBackend(backendHost, backendPort)) {
|
||||
final String instanceRegion = regionFromArn(instance.getDbInstanceArn());
|
||||
InstanceBackendIdentity backendIdentity = instanceBackendIdentity(instance);
|
||||
try {
|
||||
proxyStartAttempted = true;
|
||||
proxyManager.startProxy(rdsResourceRelayKey(instance.getDbInstanceArn(), id),
|
||||
backendIdentity.runtimeId(), engine, iamEnabled, proxyPort,
|
||||
backendHost, backendPort, instance.getEndpoint().address(),
|
||||
instance.getMasterUsername(), instance.getMasterPassword(), dbName,
|
||||
(user, pw) -> validateDbPasswordForScope(
|
||||
accountId, instanceRegion, id, user, pw),
|
||||
proxyBinding(engine, instance.getEndpoint().address(), instance.getEndpoint().port(),
|
||||
instanceRegion, accountId, backendIdentity.dbUserResourceId()));
|
||||
} catch (RuntimeException | Error failure) {
|
||||
if (!isBlank(dbClusterIdentifier)) {
|
||||
try {
|
||||
deleteInstanceForScope(accountId, effectiveRegion, id);
|
||||
} catch (RuntimeException | Error rollbackFailure) {
|
||||
addSuppressedUnlessSelf(failure, rollbackFailure);
|
||||
}
|
||||
}
|
||||
throw failure;
|
||||
}
|
||||
proxyStartAttempted = true;
|
||||
proxyManager.startProxy(rdsResourceRelayKey(instance.getDbInstanceArn(), id),
|
||||
backendIdentity.runtimeId(), engine, iamEnabled, proxyPort,
|
||||
backendHost, backendPort, instance.getEndpoint().address(),
|
||||
instance.getMasterUsername(), instance.getMasterPassword(), dbName,
|
||||
(user, pw) -> validateDbPasswordForScope(
|
||||
accountId, instanceRegion, id, user, pw),
|
||||
proxyBinding(engine, instance.getEndpoint().address(), instance.getEndpoint().port(),
|
||||
instanceRegion, accountId, backendIdentity.dbUserResourceId()));
|
||||
}
|
||||
|
||||
if (currentCluster != null) {
|
||||
memberMutationStarted = true;
|
||||
currentCluster.getDbClusterMembers().add(id);
|
||||
if (currentCluster.resolveWriterIdentifier() == null
|
||||
|| !currentCluster.getDbClusterMembers().contains(currentCluster.getClusterWriterIdentifier())) {
|
||||
@@ -1001,7 +999,12 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
} catch (RuntimeException | Error failure) {
|
||||
if (isBlank(dbClusterIdentifier)) {
|
||||
rollbackFailedStandaloneInstanceCreate(failure, instance, accountId,
|
||||
effectiveRegion, proxyStartAttempted, manageInstancePassword, mock);
|
||||
effectiveRegion, instanceWriteStarted, proxyStartAttempted,
|
||||
manageInstancePassword, mock);
|
||||
} else if (currentCluster != null) {
|
||||
rollbackFailedClusterMemberCreate(failure, instance, currentCluster,
|
||||
originalMembers, originalWriter, accountId, effectiveRegion,
|
||||
instanceWriteStarted, proxyStartAttempted, memberMutationStarted);
|
||||
}
|
||||
throw failure;
|
||||
}
|
||||
@@ -1013,32 +1016,132 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
|
||||
private void rollbackFailedStandaloneInstanceCreate(
|
||||
Throwable failure, DbInstance instance, String accountId, String region,
|
||||
boolean proxyStartAttempted, boolean managedPassword, boolean mock) {
|
||||
String id = instance.getDbInstanceIdentifier();
|
||||
instance.setStatus(DbInstanceStatus.DELETING);
|
||||
instance.setDeletionProtection(false);
|
||||
boolean instanceWriteStarted, boolean proxyStartAttempted,
|
||||
boolean managedPassword, boolean mock) {
|
||||
instance.setBackendCleanupRequired(!mock && instance.getDockerVolumeName() != null);
|
||||
instance.setManagedSecretCleanupRequired(managedPassword
|
||||
&& (instance.getMasterUserSecretArn() != null
|
||||
|| instance.isManagedSecretCleanupRequired()));
|
||||
try {
|
||||
putInstanceForScope(accountId, region, id, instance);
|
||||
} catch (RuntimeException | Error persistenceFailure) {
|
||||
addSuppressedUnlessSelf(failure, persistenceFailure);
|
||||
return;
|
||||
rollbackFailedInstanceCreate(failure, instance, accountId, region, true, instanceWriteStarted,
|
||||
proxyStartAttempted, null, managedPassword, mock);
|
||||
}
|
||||
|
||||
private void rollbackFailedClusterMemberCreate(
|
||||
Throwable failure, DbInstance instance, DbCluster cluster,
|
||||
List<String> originalMembers, String originalWriter, String accountId, String region,
|
||||
boolean instanceWriteStarted, boolean proxyStartAttempted, boolean memberMutationStarted) {
|
||||
rollbackFailedInstanceCreate(failure, instance, accountId, region,
|
||||
instanceWriteStarted, instanceWriteStarted, proxyStartAttempted,
|
||||
new ClusterMemberRollback(cluster, originalMembers, originalWriter, memberMutationStarted),
|
||||
false, false);
|
||||
}
|
||||
|
||||
private record ClusterMemberRollback(DbCluster cluster, List<String> originalMembers,
|
||||
String originalWriter, boolean memberMutationStarted) {}
|
||||
|
||||
private void rollbackFailedInstanceCreate(
|
||||
Throwable failure, DbInstance instance, String accountId, String region,
|
||||
boolean tombstoneWriteNeeded, boolean instanceWriteStarted, boolean proxyStartAttempted,
|
||||
ClusterMemberRollback clusterRollback, boolean managedPassword, boolean mock) {
|
||||
String id = instance.getDbInstanceIdentifier();
|
||||
int proxyPort = instance.getProxyPort();
|
||||
if (tombstoneWriteNeeded) {
|
||||
instance.setStatus(DbInstanceStatus.DELETING);
|
||||
instance.setDeletionProtection(false);
|
||||
try {
|
||||
putInstanceForScope(accountId, region, id, instance);
|
||||
} catch (RuntimeException | Error rollbackFailure) {
|
||||
addSuppressedUnlessSelf(failure, rollbackFailure);
|
||||
if (!proxyStartAttempted
|
||||
&& (clusterRollback == null || !clusterRollback.memberMutationStarted())
|
||||
&& !(instanceWriteStarted && hasFailedWalRollback(failure))
|
||||
&& !hasFailedWalRollback(rollbackFailure)) {
|
||||
try {
|
||||
if (!scopedKeyExists(instances, accountId, dbResourceKey(region, id))) {
|
||||
if (clusterRollback != null || cleanupFailedStandaloneResources(
|
||||
failure, instance, region, managedPassword, mock)) {
|
||||
releaseProxyPort(proxyPort);
|
||||
}
|
||||
}
|
||||
} catch (RuntimeException | Error verificationFailure) {
|
||||
addSuppressedUnlessSelf(failure, verificationFailure);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
boolean cleanupComplete = true;
|
||||
if (clusterRollback != null && clusterRollback.memberMutationStarted()) {
|
||||
try {
|
||||
DbCluster cluster = clusterRollback.cluster();
|
||||
cluster.getDbClusterMembers().clear();
|
||||
cluster.getDbClusterMembers().addAll(clusterRollback.originalMembers());
|
||||
cluster.setClusterWriterIdentifier(clusterRollback.originalWriter());
|
||||
putClusterForScope(accountId, region, cluster.getDbClusterIdentifier(), cluster);
|
||||
} catch (RuntimeException | Error rollbackFailure) {
|
||||
cleanupComplete = false;
|
||||
addSuppressedUnlessSelf(failure, rollbackFailure);
|
||||
}
|
||||
}
|
||||
|
||||
boolean proxyStopped = !proxyStartAttempted;
|
||||
if (proxyStartAttempted) {
|
||||
try {
|
||||
proxyManager.stopProxy(rdsResourceRelayKey(instance.getDbInstanceArn(), id));
|
||||
proxyStopped = true;
|
||||
} catch (RuntimeException | Error cleanupFailure) {
|
||||
cleanupComplete = false;
|
||||
addSuppressedUnlessSelf(failure, cleanupFailure);
|
||||
} catch (RuntimeException | Error rollbackFailure) {
|
||||
addSuppressedUnlessSelf(failure, rollbackFailure);
|
||||
}
|
||||
}
|
||||
|
||||
if (clusterRollback == null) {
|
||||
cleanupComplete = cleanupFailedStandaloneResources(failure, instance, region, managedPassword, mock);
|
||||
}
|
||||
|
||||
boolean instanceRemoved = !tombstoneWriteNeeded;
|
||||
if (tombstoneWriteNeeded && proxyStopped && cleanupComplete) {
|
||||
try {
|
||||
deleteInstanceForScope(accountId, region, id);
|
||||
} catch (RuntimeException | Error rollbackFailure) {
|
||||
addSuppressedUnlessSelf(failure, rollbackFailure);
|
||||
}
|
||||
try {
|
||||
instanceRemoved = !scopedKeyExists(instances, accountId, dbResourceKey(region, id));
|
||||
} catch (RuntimeException | Error verificationFailure) {
|
||||
addSuppressedUnlessSelf(failure, verificationFailure);
|
||||
}
|
||||
}
|
||||
boolean releasePort = proxyStopped && instanceRemoved;
|
||||
if (!instanceRemoved) {
|
||||
DbEndpoint originalEndpoint = instance.getEndpoint();
|
||||
if (proxyStopped) {
|
||||
instance.setProxyPort(0);
|
||||
instance.setEndpoint(null);
|
||||
}
|
||||
try {
|
||||
putInstanceForScope(accountId, region, id, instance);
|
||||
releasePort = proxyStopped;
|
||||
} catch (RuntimeException | Error rollbackFailure) {
|
||||
instance.setProxyPort(proxyPort);
|
||||
instance.setEndpoint(originalEndpoint);
|
||||
addSuppressedUnlessSelf(failure, rollbackFailure);
|
||||
}
|
||||
}
|
||||
if (releasePort) {
|
||||
releaseProxyPort(proxyPort);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean hasFailedWalRollback(Throwable failure) {
|
||||
// WalStorage adds suppressed failures only when recovery of an appended record fails.
|
||||
return failure instanceof UncheckedIOException ioFailure
|
||||
&& ioFailure.getCause().getSuppressed().length > 0;
|
||||
}
|
||||
|
||||
private boolean cleanupFailedStandaloneResources(
|
||||
Throwable failure, DbInstance instance, String region, boolean managedPassword, boolean mock) {
|
||||
boolean cleanupComplete = true;
|
||||
if (!mock && instance.getDockerVolumeName() != null) {
|
||||
try {
|
||||
if (instance.getContainerId() != null) {
|
||||
@@ -1066,29 +1169,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
addSuppressedUnlessSelf(failure, cleanupFailure);
|
||||
}
|
||||
}
|
||||
if (proxyStopped) {
|
||||
releaseProxyPort(instance.getProxyPort());
|
||||
instance.setProxyPort(0);
|
||||
instance.setEndpoint(null);
|
||||
}
|
||||
if (!cleanupComplete) {
|
||||
try {
|
||||
putInstanceForScope(accountId, region, id, instance);
|
||||
} catch (RuntimeException | Error persistenceFailure) {
|
||||
addSuppressedUnlessSelf(failure, persistenceFailure);
|
||||
}
|
||||
return;
|
||||
}
|
||||
try {
|
||||
deleteInstanceForScope(accountId, region, id);
|
||||
} catch (RuntimeException | Error deletionFailure) {
|
||||
addSuppressedUnlessSelf(failure, deletionFailure);
|
||||
try {
|
||||
putInstanceForScope(accountId, region, id, instance);
|
||||
} catch (RuntimeException | Error persistenceFailure) {
|
||||
addSuppressedUnlessSelf(failure, persistenceFailure);
|
||||
}
|
||||
}
|
||||
return cleanupComplete;
|
||||
}
|
||||
|
||||
public DbSnapshot createDbSnapshot(String snapshotId, String instanceId) {
|
||||
|
||||
@@ -13,6 +13,7 @@ import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.core.storage.PersistentStorage;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.core.storage.WalStorage;
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2Service;
|
||||
import io.github.hectorvent.floci.services.ec2.model.SecurityGroup;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Subnet;
|
||||
@@ -69,10 +70,15 @@ import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.ArgumentMatchers;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.net.ServerSocket;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
@@ -1218,6 +1224,157 @@ class RdsServiceTest {
|
||||
assertThrows(AwsException.class, () -> service.getDbInstance("failed-instance"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedInitialStandaloneWritesWithoutRecordReleasePort() {
|
||||
when(rdsConfig.proxyMaxPort()).thenReturn(7000);
|
||||
String instanceArn = "arn:aws:rds:us-east-1:123456789012:db:failed-instance";
|
||||
InMemoryStorage<String, DbInstance> instances = spy(new InMemoryStorage<>());
|
||||
RdsService service = newService(containerManager, proxyManager, instances,
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>());
|
||||
IllegalStateException initialFailure = new IllegalStateException("initial write failed before insert");
|
||||
IllegalStateException tombstoneFailure = new IllegalStateException("tombstone write failed before insert");
|
||||
doThrow(initialFailure).doThrow(tombstoneFailure).doCallRealMethod()
|
||||
.when(instances).put(eq("us-east-1::failed-instance"), any());
|
||||
|
||||
assertSame(initialFailure, assertThrows(IllegalStateException.class, () ->
|
||||
service.createDbInstance("failed-instance", "postgres", "13", "admin", "secret",
|
||||
"app", "db.t3.micro", 20, false, null, null, null)));
|
||||
assertTrue(List.of(initialFailure.getSuppressed()).contains(tombstoneFailure));
|
||||
assertTrue(instances.get("us-east-1::failed-instance").isEmpty());
|
||||
ArgumentCaptor<String> volumeName = ArgumentCaptor.forClass(String.class);
|
||||
verify(containerManager).tryStart(eq(instanceArn), eq("failed-instance"), any(), volumeName.capture(),
|
||||
any(), any(), any(), any(), any(), any());
|
||||
verify(containerManager).stop(ArgumentMatchers.argThat(
|
||||
handle -> "cont-id".equals(handle.getContainerId())));
|
||||
verify(containerManager).removeVolume(eq(instanceArn), any(), eq(volumeName.getValue()));
|
||||
|
||||
DbInstance retried = service.createDbInstance("another-instance", "postgres", "13",
|
||||
"admin", "secret", "app", "db.t3.micro", 20, false, null, null, null);
|
||||
assertEquals(7000, retried.getProxyPort());
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedContainerBeforeInstanceWriteDoesNotReservePortForUnrelatedIoFailure() {
|
||||
when(rdsConfig.proxyMaxPort()).thenReturn(7000);
|
||||
InMemoryStorage<String, DbInstance> instances = spy(new InMemoryStorage<>());
|
||||
RdsService service = newService(containerManager, proxyManager, instances,
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>());
|
||||
IOException cause = new IOException("container failed before instance write");
|
||||
cause.addSuppressed(new IOException("container cleanup also failed"));
|
||||
UncheckedIOException startupFailure = new UncheckedIOException(cause);
|
||||
UncheckedIOException tombstoneFailure = new UncheckedIOException(
|
||||
"Failed to inspect WAL before append", new IOException("WAL path is not a regular file"));
|
||||
doThrow(startupFailure).doReturn(new RdsContainerHandle("cont-id", "id", "localhost", 5432))
|
||||
.when(containerManager).tryStart(any(), any(), any(), any(), any(), any(), any(), any(), any(), any());
|
||||
doThrow(tombstoneFailure).doCallRealMethod()
|
||||
.when(instances).put(eq("us-east-1::failed-instance"), any());
|
||||
|
||||
assertSame(startupFailure, assertThrows(UncheckedIOException.class, () ->
|
||||
service.createDbInstance("failed-instance", "postgres", "13", "admin", "secret",
|
||||
"app", "db.t3.micro", 20, false, null, null, null)));
|
||||
assertTrue(List.of(startupFailure.getSuppressed()).contains(tombstoneFailure));
|
||||
assertTrue(instances.get("us-east-1::failed-instance").isEmpty());
|
||||
DbInstance retried = service.createDbInstance("failed-instance", "postgres", "13",
|
||||
"admin", "secret", "app", "db.t3.micro", 20, false, null, null, null);
|
||||
assertEquals(7000, retried.getProxyPort());
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedInitialStandaloneWritesKeepPortWhenBackendCleanupFails() {
|
||||
when(rdsConfig.proxyMaxPort()).thenReturn(7000);
|
||||
InMemoryStorage<String, DbInstance> instances = spy(new InMemoryStorage<>());
|
||||
RdsService service = newService(containerManager, proxyManager, instances,
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>());
|
||||
IllegalStateException initialFailure = new IllegalStateException("initial write failed before insert");
|
||||
IllegalStateException tombstoneFailure = new IllegalStateException("tombstone write failed before insert");
|
||||
IllegalStateException cleanupFailure = new IllegalStateException("volume removal failed");
|
||||
doThrow(initialFailure).doThrow(tombstoneFailure)
|
||||
.when(instances).put(eq("us-east-1::failed-instance"), any());
|
||||
doThrow(cleanupFailure).when(containerManager).removeVolume(any(), any(), any());
|
||||
|
||||
assertSame(initialFailure, assertThrows(IllegalStateException.class, () ->
|
||||
service.createDbInstance("failed-instance", "postgres", "13", "admin", "secret",
|
||||
"app", "db.t3.micro", 20, false, null, null, null)));
|
||||
assertTrue(List.of(initialFailure.getSuppressed()).contains(tombstoneFailure));
|
||||
assertTrue(List.of(initialFailure.getSuppressed()).contains(cleanupFailure));
|
||||
assertTrue(instances.get("us-east-1::failed-instance").isEmpty());
|
||||
verify(containerManager).stop(ArgumentMatchers.argThat(
|
||||
handle -> "cont-id".equals(handle.getContainerId())));
|
||||
assertEquals("InsufficientDBInstanceCapacity", assertThrows(AwsException.class, () ->
|
||||
service.createDbInstance("another-instance", "postgres", "13", "admin", "secret",
|
||||
"app", "db.t3.micro", 20, false, null, null, null)).getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedStandalonePortClearWriteKeepsPersistedPort(@TempDir Path tempDir) throws Exception {
|
||||
when(rdsConfig.proxyMaxPort()).thenReturn(7000);
|
||||
Path file = tempDir.resolve("standalone-port-owner.json");
|
||||
TypeReference<Map<String, DbInstance>> type = new TypeReference<>() {};
|
||||
PersistentStorage<String, DbInstance> instances = spy(new PersistentStorage<>(file, type));
|
||||
RdsService service = newService(containerManager, proxyManager, instances,
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>());
|
||||
IllegalStateException startupFailure = new IllegalStateException("proxy start failed");
|
||||
IllegalStateException cleanupFailure = new IllegalStateException("backend cleanup failed");
|
||||
IllegalStateException portWriteFailure = new IllegalStateException("port clearing write failed");
|
||||
doThrow(startupFailure).doNothing().when(proxyManager).startProxy(
|
||||
any(), any(), any(), anyBoolean(), anyInt(), any(), anyInt(),
|
||||
any(), any(), any(), any(), any(), any());
|
||||
doThrow(cleanupFailure).when(containerManager).removeVolume(any(), any(), any());
|
||||
doCallRealMethod().doCallRealMethod().doThrow(portWriteFailure)
|
||||
.when(instances).put(eq("us-east-1::failed-instance"), any());
|
||||
|
||||
assertSame(startupFailure, assertThrows(IllegalStateException.class, () ->
|
||||
service.createDbInstance("failed-instance", "postgres", "13", "admin", "secret",
|
||||
"app", "db.t3.micro", 20, false, null, null, null)));
|
||||
assertTrue(List.of(startupFailure.getSuppressed()).contains(cleanupFailure));
|
||||
assertTrue(List.of(startupFailure.getSuppressed()).contains(portWriteFailure));
|
||||
PersistentStorage<String, DbInstance> reloaded = new PersistentStorage<>(file, type);
|
||||
reloaded.load();
|
||||
DbInstance persisted = reloaded.get("us-east-1::failed-instance").orElseThrow();
|
||||
assertEquals(DbInstanceStatus.DELETING, persisted.getStatus());
|
||||
assertEquals(7000, persisted.getProxyPort());
|
||||
assertEquals("InsufficientDBInstanceCapacity", assertThrows(AwsException.class, () ->
|
||||
service.createDbInstance("another-instance", "postgres", "13", "admin", "secret",
|
||||
"app", "db.t3.micro", 20, false, null, null, null)).getErrorCode());
|
||||
DbInstance retained = service.getDbInstance("failed-instance");
|
||||
assertEquals(7000, retained.getProxyPort());
|
||||
assertEquals(7000, retained.getEndpoint().port());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {true, false})
|
||||
void failedStandaloneRollbackDeleteReusesPortOnlyAfterRecordIsCleared(boolean failBeforeDelete) {
|
||||
when(rdsConfig.proxyMaxPort()).thenReturn(7000);
|
||||
InMemoryStorage<String, DbInstance> instances = spy(new InMemoryStorage<>());
|
||||
RdsService service = newService(containerManager, proxyManager, instances,
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>());
|
||||
IllegalStateException startupFailure = new IllegalStateException("proxy start failed");
|
||||
IllegalStateException deletionFailure = new IllegalStateException("rollback delete failed");
|
||||
doThrow(startupFailure).doNothing().when(proxyManager).startProxy(
|
||||
any(), any(), any(), anyBoolean(), anyInt(), any(), anyInt(),
|
||||
any(), any(), any(), any(), any(), any());
|
||||
doAnswer(invocation -> {
|
||||
if (!failBeforeDelete) {
|
||||
invocation.callRealMethod();
|
||||
}
|
||||
throw deletionFailure;
|
||||
}).when(instances).delete("us-east-1::failed-instance");
|
||||
|
||||
assertSame(startupFailure, assertThrows(IllegalStateException.class, () ->
|
||||
service.createDbInstance("failed-instance", "postgres", "13", "admin", "secret",
|
||||
"app", "db.t3.micro", 20, false, null, null, null)));
|
||||
assertTrue(List.of(startupFailure.getSuppressed()).contains(deletionFailure));
|
||||
if (failBeforeDelete) {
|
||||
DbInstance retained = instances.get("us-east-1::failed-instance").orElseThrow();
|
||||
assertEquals(DbInstanceStatus.DELETING, retained.getStatus());
|
||||
assertEquals(0, retained.getProxyPort());
|
||||
} else {
|
||||
assertTrue(instances.get("us-east-1::failed-instance").isEmpty());
|
||||
}
|
||||
assertEquals(7000, service.createDbInstance("another-instance", "postgres", "13",
|
||||
"admin", "secret", "app", "db.t3.micro", 20, false, null, null, null).getProxyPort());
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedInstanceRollbackKeepsPortOwnedUntilCleanupFinishes() throws Exception {
|
||||
when(rdsConfig.proxyMaxPort()).thenReturn(7000);
|
||||
@@ -1621,6 +1778,500 @@ class RdsServiceTest {
|
||||
List.of(), List.of(), List.of(), null, null).backups().getFirst().isDataSaved());
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedClusterMemberPublicationRestoresMembershipAndPort() {
|
||||
when(rdsConfig.proxyMaxPort()).thenReturn(7002);
|
||||
InMemoryStorage<String, DbCluster> clusters = spy(new InMemoryStorage<>());
|
||||
InMemoryStorage<String, DbInstance> instances = new InMemoryStorage<>();
|
||||
RdsService service = newService(containerManager, proxyManager, instances,
|
||||
clusters, new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>());
|
||||
DbCluster cluster = service.createDbCluster("member-cluster", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", false, null);
|
||||
service.createDbInstance("writer-member", "aurora-postgresql", "16.3",
|
||||
null, null, "dbname", "db.serverless", 0, false,
|
||||
null, null, "member-cluster");
|
||||
List<String> originalMembers = List.copyOf(cluster.getDbClusterMembers());
|
||||
String originalWriter = cluster.getClusterWriterIdentifier();
|
||||
clearInvocations(containerManager, proxyManager);
|
||||
|
||||
IllegalStateException failure = new IllegalStateException("member publication wrote then failed");
|
||||
doAnswer(invocation -> {
|
||||
invocation.callRealMethod();
|
||||
throw failure;
|
||||
}).doCallRealMethod().when(clusters).put(eq("us-east-1::member-cluster"), any());
|
||||
|
||||
assertSame(failure, assertThrows(IllegalStateException.class, () ->
|
||||
service.createDbInstance("failed-member", "aurora-postgresql", "16.3",
|
||||
null, null, "dbname", "db.serverless", 0, false,
|
||||
null, null, "member-cluster")));
|
||||
assertTrue(instances.get("us-east-1::failed-member").isEmpty());
|
||||
assertEquals(originalMembers, cluster.getDbClusterMembers());
|
||||
assertEquals(originalWriter, cluster.getClusterWriterIdentifier());
|
||||
assertEquals(originalMembers, service.getDbCluster("member-cluster").getDbClusterMembers());
|
||||
verify(proxyManager).stopProxy(
|
||||
"rds-resource:arn:aws:rds:us-east-1:123456789012:db:failed-member");
|
||||
verify(containerManager, never()).stop(any());
|
||||
verify(containerManager, never()).stopByRuntimeId(any());
|
||||
verify(containerManager, never()).removeVolume(any(), any(), any());
|
||||
|
||||
DbInstance retried = service.createDbInstance("failed-member", "aurora-postgresql", "16.3",
|
||||
null, null, "dbname", "db.serverless", 0, false,
|
||||
null, null, "member-cluster");
|
||||
assertEquals(7002, retried.getProxyPort());
|
||||
assertEquals(List.of("writer-member", "failed-member"), cluster.getDbClusterMembers());
|
||||
assertEquals(originalWriter, cluster.getClusterWriterIdentifier());
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedFinalClusterMemberWriteRestoresMembership() {
|
||||
InMemoryStorage<String, DbCluster> clusters = new InMemoryStorage<>();
|
||||
InMemoryStorage<String, DbInstance> instances = spy(new InMemoryStorage<>());
|
||||
RdsService service = newService(containerManager, proxyManager, instances,
|
||||
clusters, new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>());
|
||||
DbCluster cluster = service.createDbCluster("member-cluster", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", false, null);
|
||||
IllegalStateException failure = new IllegalStateException("final member write failed");
|
||||
doCallRealMethod().doAnswer(invocation -> {
|
||||
invocation.callRealMethod();
|
||||
throw failure;
|
||||
}).doCallRealMethod().when(instances).put(eq("us-east-1::failed-member"), any());
|
||||
|
||||
assertSame(failure, assertThrows(IllegalStateException.class, () ->
|
||||
service.createDbInstance("failed-member", "aurora-postgresql", "16.3",
|
||||
null, null, "dbname", "db.serverless", 0, false,
|
||||
null, null, "member-cluster")));
|
||||
assertTrue(instances.get("us-east-1::failed-member").isEmpty());
|
||||
assertTrue(cluster.getDbClusterMembers().isEmpty());
|
||||
assertNull(cluster.getClusterWriterIdentifier());
|
||||
verify(proxyManager).stopProxy(
|
||||
"rds-resource:arn:aws:rds:us-east-1:123456789012:db:failed-member");
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedClusterMemberWritePersistsTombstoneBeforeStoppingProxy(@TempDir Path tempDir) throws Exception {
|
||||
Path instanceFile = tempDir.resolve("member-instance.json");
|
||||
TypeReference<Map<String, DbInstance>> instanceType = new TypeReference<>() {};
|
||||
PersistentStorage<String, DbInstance> instances = spy(new PersistentStorage<>(instanceFile, instanceType));
|
||||
RdsService service = newService(containerManager, proxyManager, instances,
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>());
|
||||
service.createDbCluster("member-cluster", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", false, null);
|
||||
IllegalStateException failure = new IllegalStateException("final member write failed");
|
||||
doCallRealMethod().doAnswer(invocation -> {
|
||||
invocation.callRealMethod();
|
||||
throw failure;
|
||||
}).doCallRealMethod().when(instances).put(eq("us-east-1::failed-member"), any());
|
||||
List<DbInstanceStatus> statusesAtProxyStop = new ArrayList<>();
|
||||
String relayKey = "rds-resource:arn:aws:rds:us-east-1:123456789012:db:failed-member";
|
||||
doAnswer(invocation -> {
|
||||
PersistentStorage<String, DbInstance> reloaded = new PersistentStorage<>(instanceFile, instanceType);
|
||||
reloaded.load();
|
||||
statusesAtProxyStop.add(reloaded.get("us-east-1::failed-member").orElseThrow().getStatus());
|
||||
return null;
|
||||
}).when(proxyManager).stopProxy(relayKey);
|
||||
|
||||
assertSame(failure, assertThrows(IllegalStateException.class, () ->
|
||||
service.createDbInstance("failed-member", "aurora-postgresql", "16.3",
|
||||
null, null, "dbname", "db.serverless", 0, false,
|
||||
null, null, "member-cluster")));
|
||||
assertEquals(List.of(DbInstanceStatus.DELETING), statusesAtProxyStop);
|
||||
verify(proxyManager).stopProxy(relayKey);
|
||||
assertThrows(AwsException.class, () -> service.getDbInstance("failed-member"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedClusterMemberTombstoneWriteKeepsRelayAndPort(@TempDir Path tempDir) throws Exception {
|
||||
when(rdsConfig.proxyMaxPort()).thenReturn(7001);
|
||||
Path instanceFile = tempDir.resolve("member-instance.json");
|
||||
TypeReference<Map<String, DbInstance>> instanceType = new TypeReference<>() {};
|
||||
PersistentStorage<String, DbInstance> instances = spy(new PersistentStorage<>(instanceFile, instanceType));
|
||||
RdsService service = newService(containerManager, proxyManager, instances,
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>());
|
||||
service.createDbCluster("member-cluster", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", false, null);
|
||||
IllegalStateException startupFailure = new IllegalStateException("member proxy start failed");
|
||||
IllegalStateException writeFailure = new IllegalStateException("tombstone write failed");
|
||||
doCallRealMethod().doThrow(writeFailure)
|
||||
.when(instances).put(eq("us-east-1::failed-member"), any());
|
||||
doThrow(startupFailure).when(proxyManager).startProxy(
|
||||
any(), any(), any(), anyBoolean(), anyInt(), any(), anyInt(),
|
||||
any(), any(), any(), any(), any(), any());
|
||||
|
||||
assertSame(startupFailure, assertThrows(IllegalStateException.class, () ->
|
||||
service.createDbInstance("failed-member", "aurora-postgresql", "16.3",
|
||||
null, null, "dbname", "db.serverless", 0, false,
|
||||
null, null, "member-cluster")));
|
||||
assertTrue(List.of(startupFailure.getSuppressed()).contains(writeFailure));
|
||||
PersistentStorage<String, DbInstance> reloaded = new PersistentStorage<>(instanceFile, instanceType);
|
||||
reloaded.load();
|
||||
assertEquals(DbInstanceStatus.CREATING,
|
||||
reloaded.get("us-east-1::failed-member").orElseThrow().getStatus());
|
||||
verify(proxyManager, never()).stopProxy(
|
||||
"rds-resource:arn:aws:rds:us-east-1:123456789012:db:failed-member");
|
||||
assertEquals("InsufficientDBInstanceCapacity", assertThrows(AwsException.class, () ->
|
||||
service.createDbInstance("other-member", "aurora-postgresql", "16.3",
|
||||
null, null, "dbname", "db.serverless", 0, false,
|
||||
null, null, "member-cluster")).getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedInitialClusterMemberWriteWithoutRecordReleasesPort() {
|
||||
when(rdsConfig.proxyMaxPort()).thenReturn(7001);
|
||||
InMemoryStorage<String, DbInstance> instances = spy(new InMemoryStorage<>());
|
||||
RdsService service = newService(containerManager, proxyManager, instances,
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>());
|
||||
DbCluster cluster = service.createDbCluster("member-cluster", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", false, null);
|
||||
IllegalStateException initialFailure = new IllegalStateException("initial write failed before insert");
|
||||
IllegalStateException tombstoneFailure = new IllegalStateException("tombstone write failed before insert");
|
||||
doThrow(initialFailure).doThrow(tombstoneFailure).doCallRealMethod()
|
||||
.when(instances).put(eq("us-east-1::failed-member"), any());
|
||||
|
||||
assertSame(initialFailure, assertThrows(IllegalStateException.class, () ->
|
||||
service.createDbInstance("failed-member", "aurora-postgresql", "16.3",
|
||||
null, null, "dbname", "db.serverless", 0, false,
|
||||
null, null, "member-cluster")));
|
||||
assertTrue(List.of(initialFailure.getSuppressed()).contains(tombstoneFailure));
|
||||
assertTrue(instances.get("us-east-1::failed-member").isEmpty());
|
||||
assertTrue(cluster.getDbClusterMembers().isEmpty());
|
||||
|
||||
DbInstance retried = service.createDbInstance("failed-member", "aurora-postgresql", "16.3",
|
||||
null, null, "dbname", "db.serverless", 0, false,
|
||||
null, null, "member-cluster");
|
||||
assertEquals(7001, retried.getProxyPort());
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedBufferedWalRollbackKeepsRecoveredMemberPort(@TempDir Path tempDir) throws Exception {
|
||||
when(rdsConfig.proxyMaxPort()).thenReturn(7001);
|
||||
Path snapshot = tempDir.resolve("member-instances.json");
|
||||
Path log = tempDir.resolve("member-instances.wal");
|
||||
Path retainedLog = tempDir.resolve("retained-member-instances.wal");
|
||||
TypeReference<Map<String, DbInstance>> type = new TypeReference<>() {};
|
||||
WalStorage<String, DbInstance> wal = new WalStorage<>(snapshot, log, type, Long.MAX_VALUE);
|
||||
wal.load();
|
||||
try {
|
||||
AccountAwareStorageBackend<DbInstance> instances =
|
||||
new AccountAwareStorageBackend<>(wal, null, "123456789012");
|
||||
RdsService service = newService(containerManager, proxyManager, instances,
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>());
|
||||
service.createDbCluster("member-cluster", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", false, null);
|
||||
|
||||
Field writerField = WalStorage.class.getDeclaredField("walWriter");
|
||||
writerField.setAccessible(true);
|
||||
((DataOutputStream) writerField.get(wal)).close();
|
||||
OutputStream append = Files.newOutputStream(log, StandardOpenOption.APPEND);
|
||||
writerField.set(wal, new DataOutputStream(append) {
|
||||
@Override
|
||||
public void flush() throws IOException {
|
||||
append.flush();
|
||||
append.close();
|
||||
Files.move(log, retainedLog);
|
||||
Files.createDirectory(log);
|
||||
throw new IOException("flush failed after complete WAL append");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
append.close();
|
||||
}
|
||||
});
|
||||
|
||||
UncheckedIOException failure = assertThrows(UncheckedIOException.class, () ->
|
||||
service.createDbInstance("failed-member", "aurora-postgresql", "16.3",
|
||||
null, null, "dbname", "db.serverless", 0, false,
|
||||
null, null, "member-cluster"));
|
||||
assertEquals(1, failure.getCause().getSuppressed().length);
|
||||
assertTrue(instances.getForAccount("123456789012", "us-east-1::failed-member").isEmpty());
|
||||
assertEquals("InsufficientDBInstanceCapacity", assertThrows(AwsException.class, () ->
|
||||
service.createDbInstance("other-member", "aurora-postgresql", "16.3",
|
||||
null, null, "dbname", "db.serverless", 0, false,
|
||||
null, null, "member-cluster")).getErrorCode());
|
||||
|
||||
Files.delete(log);
|
||||
Files.move(retainedLog, log);
|
||||
WalStorage<String, DbInstance> reloaded = new WalStorage<>(snapshot, log, type, Long.MAX_VALUE);
|
||||
reloaded.load();
|
||||
try {
|
||||
DbInstance recovered = reloaded.get("123456789012/us-east-1::failed-member").orElseThrow();
|
||||
assertEquals(7001, recovered.getProxyPort());
|
||||
assertEquals(DbInstanceStatus.CREATING, recovered.getStatus());
|
||||
} finally {
|
||||
reloaded.shutdown();
|
||||
}
|
||||
} finally {
|
||||
wal.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedPreAppendWalInspectionWithoutRecordReleasesMemberPort() {
|
||||
when(rdsConfig.proxyMaxPort()).thenReturn(7001);
|
||||
InMemoryStorage<String, DbInstance> instances = spy(new InMemoryStorage<>());
|
||||
RdsService service = newService(containerManager, proxyManager, instances,
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>());
|
||||
service.createDbCluster("member-cluster", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", false, null);
|
||||
UncheckedIOException initialFailure = new UncheckedIOException(
|
||||
"Failed to inspect WAL before append", new IOException("WAL path is not a regular file"));
|
||||
UncheckedIOException tombstoneFailure = new UncheckedIOException(
|
||||
"Failed to inspect WAL before append", new IOException("WAL path is not a regular file"));
|
||||
doThrow(initialFailure).doThrow(tombstoneFailure).doCallRealMethod()
|
||||
.when(instances).put(eq("us-east-1::failed-member"), any());
|
||||
|
||||
assertSame(initialFailure, assertThrows(UncheckedIOException.class, () ->
|
||||
service.createDbInstance("failed-member", "aurora-postgresql", "16.3",
|
||||
null, null, "dbname", "db.serverless", 0, false,
|
||||
null, null, "member-cluster")));
|
||||
assertTrue(List.of(initialFailure.getSuppressed()).contains(tombstoneFailure));
|
||||
assertTrue(instances.get("us-east-1::failed-member").isEmpty());
|
||||
DbInstance retried = service.createDbInstance("failed-member", "aurora-postgresql", "16.3",
|
||||
null, null, "dbname", "db.serverless", 0, false,
|
||||
null, null, "member-cluster");
|
||||
assertEquals(7001, retried.getProxyPort());
|
||||
}
|
||||
|
||||
@Test
|
||||
void unrelatedStorageWriteFailureDoesNotBlockMemberRollback(@TempDir Path tempDir) throws Exception {
|
||||
when(rdsConfig.proxyMaxPort()).thenReturn(7001);
|
||||
Path file = tempDir.resolve("member-instances.json");
|
||||
Path blockedTempFile = tempDir.resolve("member-instances.json.tmp");
|
||||
TypeReference<Map<String, DbInstance>> type = new TypeReference<>() {};
|
||||
PersistentStorage<String, DbInstance> instances = spy(new PersistentStorage<>(file, type));
|
||||
RdsService service = newService(containerManager, proxyManager, instances,
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>());
|
||||
DbCluster cluster = service.createDbCluster("member-cluster", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", false, null);
|
||||
IllegalStateException initialFailure = new IllegalStateException("initial member write failed");
|
||||
CountDownLatch tombstoneWritten = new CountDownLatch(1);
|
||||
CountDownLatch resumeRollback = new CountDownLatch(1);
|
||||
doThrow(initialFailure).doAnswer(invocation -> {
|
||||
invocation.callRealMethod();
|
||||
tombstoneWritten.countDown();
|
||||
assertTrue(resumeRollback.await(5, TimeUnit.SECONDS));
|
||||
return null;
|
||||
}).when(instances).put(eq("us-east-1::failed-member"), any());
|
||||
ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||
try {
|
||||
Future<Throwable> failedCreate = executor.submit(() -> assertThrows(IllegalStateException.class, () ->
|
||||
service.createDbInstance("failed-member", "aurora-postgresql", "16.3",
|
||||
null, null, "dbname", "db.serverless", 0, false,
|
||||
null, null, "member-cluster")));
|
||||
assertTrue(tombstoneWritten.await(5, TimeUnit.SECONDS));
|
||||
Files.createDirectory(blockedTempFile);
|
||||
instances.delete("unrelated-instance");
|
||||
Files.delete(blockedTempFile);
|
||||
resumeRollback.countDown();
|
||||
assertSame(initialFailure, failedCreate.get(5, TimeUnit.SECONDS));
|
||||
} finally {
|
||||
resumeRollback.countDown();
|
||||
executor.shutdownNow();
|
||||
Files.deleteIfExists(blockedTempFile);
|
||||
}
|
||||
assertTrue(instances.get("us-east-1::failed-member").isEmpty());
|
||||
assertTrue(cluster.getDbClusterMembers().isEmpty());
|
||||
PersistentStorage<String, DbInstance> reloaded = new PersistentStorage<>(file, type);
|
||||
reloaded.load();
|
||||
assertTrue(reloaded.get("us-east-1::failed-member").isEmpty());
|
||||
DbInstance retried = service.createDbInstance("failed-member", "aurora-postgresql", "16.3",
|
||||
null, null, "dbname", "db.serverless", 0, false,
|
||||
null, null, "member-cluster");
|
||||
assertEquals(7001, retried.getProxyPort());
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedInitialClusterMemberWriteWithPublishedRecordKeepsPort() {
|
||||
when(rdsConfig.proxyMaxPort()).thenReturn(7001);
|
||||
InMemoryStorage<String, DbInstance> instances = spy(new InMemoryStorage<>());
|
||||
RdsService service = newService(containerManager, proxyManager, instances,
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>());
|
||||
service.createDbCluster("member-cluster", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", false, null);
|
||||
IllegalStateException initialFailure = new IllegalStateException("initial write published then failed");
|
||||
IllegalStateException tombstoneFailure = new IllegalStateException("tombstone write failed");
|
||||
doAnswer(invocation -> {
|
||||
invocation.callRealMethod();
|
||||
throw initialFailure;
|
||||
}).doThrow(tombstoneFailure).doCallRealMethod()
|
||||
.when(instances).put(eq("us-east-1::failed-member"), any());
|
||||
|
||||
assertSame(initialFailure, assertThrows(IllegalStateException.class, () ->
|
||||
service.createDbInstance("failed-member", "aurora-postgresql", "16.3",
|
||||
null, null, "dbname", "db.serverless", 0, false,
|
||||
null, null, "member-cluster")));
|
||||
assertEquals(7001, service.getDbInstance("failed-member").getProxyPort());
|
||||
assertEquals("InsufficientDBInstanceCapacity", assertThrows(AwsException.class, () ->
|
||||
service.createDbInstance("other-member", "aurora-postgresql", "16.3",
|
||||
null, null, "dbname", "db.serverless", 0, false,
|
||||
null, null, "member-cluster")).getErrorCode());
|
||||
|
||||
service.deleteDbInstance("failed-member");
|
||||
DbInstance retried = service.createDbInstance("other-member", "aurora-postgresql", "16.3",
|
||||
null, null, "dbname", "db.serverless", 0, false,
|
||||
null, null, "member-cluster");
|
||||
assertEquals(7001, retried.getProxyPort());
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedClusterMembershipRestoreRetainsMemberForDeleteRetry(@TempDir Path tempDir) throws Exception {
|
||||
Path file = tempDir.resolve("member-cluster.json");
|
||||
TypeReference<Map<String, DbCluster>> type = new TypeReference<>() {};
|
||||
PersistentStorage<String, DbCluster> clusters = spy(new PersistentStorage<>(file, type));
|
||||
Path instanceFile = tempDir.resolve("member-instance.json");
|
||||
TypeReference<Map<String, DbInstance>> instanceType = new TypeReference<>() {};
|
||||
PersistentStorage<String, DbInstance> instances = new PersistentStorage<>(instanceFile, instanceType);
|
||||
RdsService service = newService(containerManager, proxyManager, instances,
|
||||
clusters, new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>());
|
||||
DbCluster cluster = service.createDbCluster("member-cluster", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", false, null);
|
||||
IllegalStateException publicationFailure = new IllegalStateException("cluster write failed");
|
||||
IllegalStateException restoreFailure = new IllegalStateException("cluster restore failed");
|
||||
doAnswer(invocation -> {
|
||||
invocation.callRealMethod();
|
||||
throw publicationFailure;
|
||||
}).doThrow(restoreFailure).doCallRealMethod()
|
||||
.when(clusters).put(eq("us-east-1::member-cluster"), any());
|
||||
|
||||
assertSame(publicationFailure, assertThrows(IllegalStateException.class, () ->
|
||||
service.createDbInstance("failed-member", "aurora-postgresql", "16.3",
|
||||
null, null, "dbname", "db.serverless", 0, false,
|
||||
null, null, "member-cluster")));
|
||||
DbInstance retained = service.getDbInstance("failed-member");
|
||||
assertEquals(DbInstanceStatus.DELETING, retained.getStatus());
|
||||
assertEquals(0, retained.getProxyPort());
|
||||
assertTrue(List.of(publicationFailure.getSuppressed()).contains(restoreFailure));
|
||||
assertTrue(cluster.getDbClusterMembers().isEmpty());
|
||||
PersistentStorage<String, DbInstance> reloadedInstances =
|
||||
new PersistentStorage<>(instanceFile, instanceType);
|
||||
reloadedInstances.load();
|
||||
assertEquals(DbInstanceStatus.DELETING,
|
||||
reloadedInstances.get("us-east-1::failed-member").orElseThrow().getStatus());
|
||||
PersistentStorage<String, DbCluster> reloaded = new PersistentStorage<>(file, type);
|
||||
reloaded.load();
|
||||
assertEquals(List.of("failed-member"),
|
||||
reloaded.get("us-east-1::member-cluster").orElseThrow().getDbClusterMembers());
|
||||
|
||||
service.deleteDbInstance("failed-member");
|
||||
assertThrows(AwsException.class, () -> service.getDbInstance("failed-member"));
|
||||
reloadedInstances.load();
|
||||
assertTrue(reloadedInstances.get("us-east-1::failed-member").isEmpty());
|
||||
reloaded.load();
|
||||
assertTrue(reloaded.get("us-east-1::member-cluster").orElseThrow().getDbClusterMembers().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedClusterMemberFallbackWriteKeepsPortForDeleteRetry() {
|
||||
when(rdsConfig.proxyMaxPort()).thenReturn(7001);
|
||||
InMemoryStorage<String, DbCluster> clusters = spy(new InMemoryStorage<>());
|
||||
InMemoryStorage<String, DbInstance> instances = spy(new InMemoryStorage<>());
|
||||
RdsService service = newService(containerManager, proxyManager, instances,
|
||||
clusters, new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>());
|
||||
service.createDbCluster("member-cluster", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", false, null);
|
||||
IllegalStateException publicationFailure = new IllegalStateException("cluster write failed");
|
||||
IllegalStateException restoreFailure = new IllegalStateException("cluster restore failed");
|
||||
IllegalStateException fallbackFailure = new IllegalStateException("fallback write failed");
|
||||
doAnswer(invocation -> {
|
||||
invocation.callRealMethod();
|
||||
throw publicationFailure;
|
||||
}).doThrow(restoreFailure).doCallRealMethod()
|
||||
.when(clusters).put(eq("us-east-1::member-cluster"), any());
|
||||
doCallRealMethod().doCallRealMethod().doThrow(fallbackFailure).doCallRealMethod()
|
||||
.when(instances).put(eq("us-east-1::failed-member"), any());
|
||||
|
||||
assertSame(publicationFailure, assertThrows(IllegalStateException.class, () ->
|
||||
service.createDbInstance("failed-member", "aurora-postgresql", "16.3",
|
||||
null, null, "dbname", "db.serverless", 0, false,
|
||||
null, null, "member-cluster")));
|
||||
DbInstance retained = service.getDbInstance("failed-member");
|
||||
assertEquals(DbInstanceStatus.DELETING, retained.getStatus());
|
||||
assertEquals(7001, retained.getProxyPort());
|
||||
assertEquals(7001, retained.getEndpoint().port());
|
||||
assertTrue(List.of(publicationFailure.getSuppressed()).contains(restoreFailure));
|
||||
assertTrue(List.of(publicationFailure.getSuppressed()).contains(fallbackFailure));
|
||||
|
||||
service.deleteDbInstance("failed-member");
|
||||
DbInstance replacement = service.createDbInstance("other-member", "aurora-postgresql", "16.3",
|
||||
null, null, "dbname", "db.serverless", 0, false,
|
||||
null, null, "member-cluster");
|
||||
assertEquals(7001, replacement.getProxyPort());
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedClusterMemberProxyCleanupKeepsPortUntilDeleteRetry() {
|
||||
when(rdsConfig.proxyMaxPort()).thenReturn(7001);
|
||||
InMemoryStorage<String, DbCluster> clusters = spy(new InMemoryStorage<>());
|
||||
InMemoryStorage<String, DbInstance> instances = new InMemoryStorage<>();
|
||||
RdsService service = newService(containerManager, proxyManager, instances,
|
||||
clusters, new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>());
|
||||
DbCluster cluster = service.createDbCluster("member-cluster", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", false, null);
|
||||
IllegalStateException publicationFailure = new IllegalStateException("cluster write failed");
|
||||
doAnswer(invocation -> {
|
||||
invocation.callRealMethod();
|
||||
throw publicationFailure;
|
||||
}).doCallRealMethod().when(clusters).put(eq("us-east-1::member-cluster"), any());
|
||||
String relayKey = "rds-resource:arn:aws:rds:us-east-1:123456789012:db:failed-member";
|
||||
IllegalStateException cleanupFailure = new IllegalStateException("proxy stop failed");
|
||||
doThrow(cleanupFailure).doNothing().when(proxyManager).stopProxy(relayKey);
|
||||
|
||||
assertSame(publicationFailure, assertThrows(IllegalStateException.class, () ->
|
||||
service.createDbInstance("failed-member", "aurora-postgresql", "16.3",
|
||||
null, null, "dbname", "db.serverless", 0, false,
|
||||
null, null, "member-cluster")));
|
||||
DbInstance retained = service.getDbInstance("failed-member");
|
||||
assertEquals(DbInstanceStatus.DELETING, retained.getStatus());
|
||||
assertEquals(7001, retained.getProxyPort());
|
||||
assertEquals(List.of(), cluster.getDbClusterMembers());
|
||||
assertTrue(List.of(publicationFailure.getSuppressed()).contains(cleanupFailure));
|
||||
assertEquals("InsufficientDBInstanceCapacity", assertThrows(AwsException.class, () ->
|
||||
service.createDbInstance("other-member", "aurora-postgresql", "16.3",
|
||||
null, null, "dbname", "db.serverless", 0, false,
|
||||
null, null, "member-cluster")).getErrorCode());
|
||||
|
||||
service.deleteDbInstance("failed-member");
|
||||
DbInstance replacement = service.createDbInstance("other-member", "aurora-postgresql", "16.3",
|
||||
null, null, "dbname", "db.serverless", 0, false,
|
||||
null, null, "member-cluster");
|
||||
assertEquals(7001, replacement.getProxyPort());
|
||||
verify(containerManager, never()).stop(any());
|
||||
verify(containerManager, never()).stopByRuntimeId(any());
|
||||
verify(containerManager, never()).removeVolume(any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedClusterMemberProxyStartStopsRelayAndReusesPort() {
|
||||
when(rdsConfig.proxyMaxPort()).thenReturn(7001);
|
||||
RdsService service = newService(containerManager, proxyManager,
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>());
|
||||
DbCluster cluster = service.createDbCluster("member-cluster", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", false, null);
|
||||
clearInvocations(containerManager, proxyManager);
|
||||
IllegalStateException failure = new IllegalStateException("member proxy start failed");
|
||||
doThrow(failure).doNothing().when(proxyManager).startProxy(
|
||||
any(), any(), any(), anyBoolean(), anyInt(), any(), anyInt(),
|
||||
any(), any(), any(), any(), any(), any());
|
||||
|
||||
assertSame(failure, assertThrows(IllegalStateException.class, () ->
|
||||
service.createDbInstance("failed-member", "aurora-postgresql", "16.3",
|
||||
null, null, "dbname", "db.serverless", 0, false,
|
||||
null, null, "member-cluster")));
|
||||
assertThrows(AwsException.class, () -> service.getDbInstance("failed-member"));
|
||||
assertTrue(cluster.getDbClusterMembers().isEmpty());
|
||||
verify(proxyManager).stopProxy(
|
||||
"rds-resource:arn:aws:rds:us-east-1:123456789012:db:failed-member");
|
||||
verify(containerManager, never()).stop(any());
|
||||
verify(containerManager, never()).removeVolume(any(), any(), any());
|
||||
|
||||
DbInstance retried = service.createDbInstance("failed-member", "aurora-postgresql", "16.3",
|
||||
null, null, "dbname", "db.serverless", 0, false,
|
||||
null, null, "member-cluster");
|
||||
assertEquals(7001, retried.getProxyPort());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void persistedInstanceTombstoneRetriesNamedBackendCleanup(boolean mockOnRestart, @TempDir Path tempDir)
|
||||
|
||||
Reference in new issue
Block a user