mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(rds): retain incomplete cleanup for retry (#7339)
* fix(rds): retain incomplete cleanup for retry * fix(rds): reuse retained backup dump on delete retry * refactor(rds): simplify retry ownership and test isolation
This commit is contained in:
1 parent
cffad85ac1
commit
1b4f99cb8a
8 files changed
+732
-56
No files matched your search
@@ -1783,11 +1783,17 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
DbInstanceAutomatedBackup backup = activeInstanceBackupOf(instance, region, now);
|
||||
backup.setStatus(DbInstanceAutomatedBackup.RETAINED);
|
||||
backup.setInstance(instance);
|
||||
String dump = saveBackupData(instance.getEngine(), instance.getContainerId(), instance.getMasterUsername(),
|
||||
"DB instance " + instance.getDbInstanceIdentifier(), "InvalidDBInstanceState");
|
||||
if (dump != null) {
|
||||
snapshotData.put(instanceBackupDataKey(region, instance.getDbiResourceId()), dump);
|
||||
String dataKey = instanceBackupDataKey(region, instance.getDbiResourceId());
|
||||
// A delete retry may no longer have the container that produced the first dump.
|
||||
if (instance.getStatus() == DbInstanceStatus.DELETING && snapshotData.get(dataKey).isPresent()) {
|
||||
backup.setDataSaved(true);
|
||||
} else {
|
||||
String dump = saveBackupData(instance.getEngine(), instance.getContainerId(), instance.getMasterUsername(),
|
||||
"DB instance " + instance.getDbInstanceIdentifier(), "InvalidDBInstanceState");
|
||||
if (dump != null) {
|
||||
snapshotData.put(dataKey, dump);
|
||||
backup.setDataSaved(true);
|
||||
}
|
||||
}
|
||||
return backup;
|
||||
}
|
||||
@@ -1803,11 +1809,16 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
backup.setEarliestTime(cluster.getCreatedAt() != null ? cluster.getCreatedAt() : now);
|
||||
backup.setLatestTime(now);
|
||||
backup.setBackupRetentionPeriod(CLUSTER_BACKUP_RETENTION_DAYS);
|
||||
String dump = saveBackupData(cluster.getEngine(), cluster.getContainerId(), cluster.getMasterUsername(),
|
||||
"DB cluster " + cluster.getDbClusterIdentifier(), "InvalidDBClusterStateFault");
|
||||
if (dump != null) {
|
||||
snapshotData.put(clusterBackupDataKey(region, cluster.getDbClusterResourceId()), dump);
|
||||
String dataKey = clusterBackupDataKey(region, cluster.getDbClusterResourceId());
|
||||
if (cluster.getStatus() == DbInstanceStatus.DELETING && snapshotData.get(dataKey).isPresent()) {
|
||||
backup.setDataSaved(true);
|
||||
} else {
|
||||
String dump = saveBackupData(cluster.getEngine(), cluster.getContainerId(), cluster.getMasterUsername(),
|
||||
"DB cluster " + cluster.getDbClusterIdentifier(), "InvalidDBClusterStateFault");
|
||||
if (dump != null) {
|
||||
snapshotData.put(dataKey, dump);
|
||||
backup.setDataSaved(true);
|
||||
}
|
||||
}
|
||||
return backup;
|
||||
}
|
||||
@@ -3313,6 +3324,69 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
private String matchingManagedSecretArn(String secretName, Secret.Tag ownerTag, String region) {
|
||||
try {
|
||||
Secret secret = secretsManagerService.describeSecret(secretName, region);
|
||||
if (secret != null && secretName.equals(secret.getName())
|
||||
&& MANAGED_SECRET_OWNING_SERVICE.equals(secret.getOwningService())
|
||||
&& secret.getTags() != null && secret.getTags().contains(ownerTag)) {
|
||||
return secret.getArn();
|
||||
}
|
||||
} catch (AwsException failure) {
|
||||
if (!"ResourceNotFoundException".equals(failure.getErrorCode())) {
|
||||
throw failure;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void cleanupFailedCreateManagedSecret(String secretArn, String region) {
|
||||
if (secretArn == null || secretsManagerService == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
secretsManagerService.deleteSecret(secretArn, null, true, region);
|
||||
} catch (AwsException failure) {
|
||||
if (!"ResourceNotFoundException".equals(failure.getErrorCode())) {
|
||||
throw failure;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void cleanupFailedCreateManagedSecret(DbInstance instance, String region) {
|
||||
if (secretsManagerService == null) {
|
||||
throw new IllegalStateException("Secrets Manager is required to retry RDS managed secret cleanup");
|
||||
}
|
||||
if (instance.isManagedSecretCleanupRequired() && instance.getMasterUserSecretArn() == null) {
|
||||
instance.setMasterUserSecretArn(matchingManagedSecretArn(
|
||||
"rds!" + instance.getDbiResourceId(),
|
||||
new Secret.Tag("aws:rds:primaryDBInstanceArn", instance.getDbInstanceArn()), region));
|
||||
}
|
||||
if (instance.getMasterUserSecretArn() != null) {
|
||||
instance.setManagedSecretCleanupRequired(true);
|
||||
cleanupFailedCreateManagedSecret(instance.getMasterUserSecretArn(), region);
|
||||
instance.setMasterUserSecretArn(null);
|
||||
}
|
||||
instance.setManagedSecretCleanupRequired(false);
|
||||
}
|
||||
|
||||
private void cleanupFailedCreateManagedSecret(DbCluster cluster, String region) {
|
||||
if (secretsManagerService == null) {
|
||||
throw new IllegalStateException("Secrets Manager is required to retry RDS managed secret cleanup");
|
||||
}
|
||||
if (cluster.isManagedSecretCleanupRequired() && cluster.getMasterUserSecretArn() == null) {
|
||||
cluster.setMasterUserSecretArn(matchingManagedSecretArn(
|
||||
"rds!" + cluster.getDbClusterResourceId(),
|
||||
new Secret.Tag("aws:rds:primaryDBClusterArn", cluster.getDbClusterArn()), region));
|
||||
}
|
||||
if (cluster.getMasterUserSecretArn() != null) {
|
||||
cluster.setManagedSecretCleanupRequired(true);
|
||||
cleanupFailedCreateManagedSecret(cluster.getMasterUserSecretArn(), region);
|
||||
cluster.setMasterUserSecretArn(null);
|
||||
}
|
||||
cluster.setManagedSecretCleanupRequired(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies a new KMS key to a cluster's existing managed master user secret. AWS re-encrypts the
|
||||
* secret in place on {@code ModifyDBCluster}; silently keeping the old key would make the call
|
||||
@@ -4846,17 +4920,20 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
detachReadReplicaLinksBeforeDelete(instance);
|
||||
|
||||
boolean mock = config.services().rds().mock();
|
||||
if (!mock) {
|
||||
if (!mock || instance.isBackendCleanupRequired()) {
|
||||
proxyManager.stopProxy(rdsResourceRelayKey(instance.getDbInstanceArn(), id));
|
||||
}
|
||||
|
||||
if (clusterId == null || clusterId.isBlank()) {
|
||||
// Standalone, so stop its container and clean up its Docker volume. Neither exists in
|
||||
// mock mode, and an instance with no container and no reachable daemon has nothing
|
||||
// Docker could clean up, so its delete stays pure metadata.
|
||||
if (!mock && backendCleanupPossible(
|
||||
instance.getContainerId(), instance.getDbInstanceArn())) {
|
||||
if (instance.getContainerId() != null) {
|
||||
// Standalone, so stop its container and clean up its Docker volume. Current mock mode
|
||||
// skips an ordinary backend, but a persisted cleanup flag still owns a prior backend.
|
||||
// Without either, deletion stays pure metadata.
|
||||
if (instance.isBackendCleanupRequired() || (!mock && backendCleanupPossible(
|
||||
instance.getContainerId(), instance.getDbInstanceArn()))) {
|
||||
if (instance.isBackendCleanupRequired()) {
|
||||
containerManager.stopForDelete(
|
||||
instance.getDbInstanceArn(), resolvedInstanceDockerVolumeName(instance));
|
||||
} else if (instance.getContainerId() != null) {
|
||||
containerManager.stop(buildHandle(instance));
|
||||
} else {
|
||||
containerManager.stopByRuntimeId(instance.getDbInstanceArn());
|
||||
@@ -4865,8 +4942,16 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
instance.getDbInstanceArn(),
|
||||
resolvedInstanceStorageResourceId(instance),
|
||||
resolvedInstanceDockerVolumeName(instance));
|
||||
if (instance.isBackendCleanupRequired()) {
|
||||
instance.setBackendCleanupRequired(false);
|
||||
putInstanceForScope(currentAccountId(), effectiveRegion, id, instance);
|
||||
}
|
||||
}
|
||||
if (instance.isManagedSecretCleanupRequired()) {
|
||||
cleanupFailedCreateManagedSecret(instance, effectiveRegion);
|
||||
} else {
|
||||
detachManagedMasterUserSecret(instance.getMasterUserSecretArn(), effectiveRegion);
|
||||
}
|
||||
detachManagedMasterUserSecret(instance.getMasterUserSecretArn(), effectiveRegion);
|
||||
} else {
|
||||
// Cluster member — remove from cluster's member list
|
||||
DbCluster cluster = findClusterForScope(
|
||||
@@ -4880,8 +4965,19 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
deleteInstanceForScope(currentAccountId(), effectiveRegion, id);
|
||||
} catch (RuntimeException | Error failure) {
|
||||
try {
|
||||
putInstanceForScope(currentAccountId(), effectiveRegion, id, instance);
|
||||
} catch (RuntimeException | Error restoreFailure) {
|
||||
if (restoreFailure != failure) {
|
||||
failure.addSuppressed(restoreFailure);
|
||||
}
|
||||
}
|
||||
throw failure;
|
||||
}
|
||||
releaseProxyPort(instance.getProxyPort());
|
||||
deleteInstanceForScope(currentAccountId(), effectiveRegion, id);
|
||||
reportedMemberRelayFailures.remove(instance.getDbInstanceArn());
|
||||
retainReplicatedBackups(instance);
|
||||
if (retained != null) {
|
||||
@@ -5766,15 +5862,23 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
: retainedClusterBackupOf(cluster, effectiveRegion);
|
||||
detachFromGlobalClusterBeforeDelete(cluster);
|
||||
|
||||
detachManagedMasterUserSecret(cluster, effectiveRegion);
|
||||
if (cluster.isManagedSecretCleanupRequired()) {
|
||||
cleanupFailedCreateManagedSecret(cluster, effectiveRegion);
|
||||
} else {
|
||||
detachManagedMasterUserSecret(cluster, effectiveRegion);
|
||||
}
|
||||
|
||||
cluster.setStatus(DbInstanceStatus.DELETING);
|
||||
putClusterForScope(currentAccountId(), effectiveRegion, id, cluster);
|
||||
|
||||
if (!config.services().rds().mock()) {
|
||||
if (!config.services().rds().mock() || cluster.isBackendCleanupRequired()) {
|
||||
proxyManager.stopProxy(rdsResourceRelayKey(cluster.getDbClusterArn(), id));
|
||||
if (backendCleanupPossible(cluster.getContainerId(), cluster.getDbClusterArn())) {
|
||||
if (cluster.getContainerId() != null) {
|
||||
if (cluster.isBackendCleanupRequired()
|
||||
|| backendCleanupPossible(cluster.getContainerId(), cluster.getDbClusterArn())) {
|
||||
if (cluster.isBackendCleanupRequired()) {
|
||||
containerManager.stopForDelete(
|
||||
cluster.getDbClusterArn(), resolvedClusterDockerVolumeName(cluster));
|
||||
} else if (cluster.getContainerId() != null) {
|
||||
containerManager.stop(buildClusterHandle(cluster));
|
||||
} else {
|
||||
containerManager.stopByRuntimeId(cluster.getDbClusterArn());
|
||||
@@ -5783,11 +5887,26 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
cluster.getDbClusterArn(),
|
||||
resolvedClusterStorageResourceId(cluster),
|
||||
resolvedClusterDockerVolumeName(cluster));
|
||||
if (cluster.isBackendCleanupRequired()) {
|
||||
cluster.setBackendCleanupRequired(false);
|
||||
putClusterForScope(currentAccountId(), effectiveRegion, id, cluster);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
deleteClusterForScope(currentAccountId(), effectiveRegion, id);
|
||||
} catch (RuntimeException | Error failure) {
|
||||
try {
|
||||
putClusterForScope(currentAccountId(), effectiveRegion, id, cluster);
|
||||
} catch (RuntimeException | Error restoreFailure) {
|
||||
if (restoreFailure != failure) {
|
||||
failure.addSuppressed(restoreFailure);
|
||||
}
|
||||
}
|
||||
throw failure;
|
||||
}
|
||||
releaseProxyPort(cluster.getProxyPort());
|
||||
deleteClusterForScope(currentAccountId(), effectiveRegion, id);
|
||||
// Custom endpoints belong to their cluster and go with it.
|
||||
clusterEndpointsOf(effectiveRegion, id).forEach(endpoint -> clusterEndpoints.delete(
|
||||
clusterEndpointKey(effectiveRegion, endpoint.getDbClusterEndpointIdentifier())));
|
||||
@@ -10444,21 +10563,17 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
}
|
||||
|
||||
/**
|
||||
* Reserves every persisted endpoint port for its owner before any restore allocates one, so a
|
||||
* resource restored earlier without a port cannot take the port of one restored later. When
|
||||
* two records persisted the same port, the first one seen keeps it.
|
||||
* Reserves every persisted endpoint port, including a deleting resource's retained listener,
|
||||
* before any restore allocates one. When two records persisted the same port, the first one
|
||||
* seen keeps it.
|
||||
*/
|
||||
private Map<Integer, String> reservePersistedProxyPorts() {
|
||||
Map<Integer, String> owners = new HashMap<>();
|
||||
for (DbCluster cluster : allClusters()) {
|
||||
if (cluster.getStatus() != DbInstanceStatus.DELETING) {
|
||||
reservePersistedProxyPort(owners, cluster.getDbClusterArn(), cluster.getProxyPort());
|
||||
}
|
||||
reservePersistedProxyPort(owners, cluster.getDbClusterArn(), cluster.getProxyPort());
|
||||
}
|
||||
for (DbInstance instance : allInstances()) {
|
||||
if (instance.getStatus() != DbInstanceStatus.DELETING) {
|
||||
reservePersistedProxyPort(owners, instance.getDbInstanceArn(), instance.getProxyPort());
|
||||
}
|
||||
reservePersistedProxyPort(owners, instance.getDbInstanceArn(), instance.getProxyPort());
|
||||
}
|
||||
for (DbProxy proxy : allProxies()) {
|
||||
reservePersistedProxyPort(owners, proxy.getDbProxyArn(), proxy.getProxyPort());
|
||||
|
||||
+47
-1
@@ -90,6 +90,7 @@ public class RdsContainerManager implements RdsBackendGate, RdsBackendEndpointRe
|
||||
private final Map<String, String> activeStorageOwners = new ConcurrentHashMap<>();
|
||||
private final Set<String> claimedRuntimes = ConcurrentHashMap.newKeySet();
|
||||
private final Set<String> cleanupPending = ConcurrentHashMap.newKeySet();
|
||||
private final Set<String> preCreateCleanupPending = ConcurrentHashMap.newKeySet();
|
||||
private final Set<String> parameterGroupRefusals = ConcurrentHashMap.newKeySet();
|
||||
private volatile boolean dockerUnavailableLogged;
|
||||
// Auto-pause state of every running container, by runtime id. The timer thread only keeps
|
||||
@@ -260,7 +261,8 @@ public class RdsContainerManager implements RdsBackendGate, RdsBackendEndpointRe
|
||||
|
||||
private void discardNeverCreatedIdentity(String runtimeId, String containerName) {
|
||||
RdsContainerHandle retained = activeContainers.get(runtimeId);
|
||||
if (retained == null || retained.getHost() != null
|
||||
if (retained == null || !preCreateCleanupPending.contains(runtimeId)
|
||||
|| retained.getHost() != null
|
||||
|| !retained.getContainerId().equals(containerName)) {
|
||||
return;
|
||||
}
|
||||
@@ -350,6 +352,7 @@ public class RdsContainerManager implements RdsBackendGate, RdsBackendEndpointRe
|
||||
}
|
||||
|
||||
String cleanupContainerId = containerName;
|
||||
boolean createAttempted = false;
|
||||
RdsContainerHandle handle = null;
|
||||
try {
|
||||
// Remove any stale container with the same name only after storage ownership is
|
||||
@@ -406,6 +409,7 @@ public class RdsContainerManager implements RdsBackendGate, RdsBackendEndpointRe
|
||||
// The fixed name remains a valid cleanup identity if create succeeds but its response
|
||||
// is lost before Docker returns the generated ID.
|
||||
cleanupContainerId = containerName;
|
||||
createAttempted = true;
|
||||
String createdContainerId = lifecycleManager.create(spec);
|
||||
cleanupContainerId = createdContainerId;
|
||||
if (needsMasterGrant(engine, masterUsername)) {
|
||||
@@ -468,6 +472,9 @@ public class RdsContainerManager implements RdsBackendGate, RdsBackendEndpointRe
|
||||
null, 0, storageKey, containerKey);
|
||||
activeContainers.put(effectiveRuntimeId, retained);
|
||||
cleanupPending.add(effectiveRuntimeId);
|
||||
if (!createAttempted) {
|
||||
preCreateCleanupPending.add(effectiveRuntimeId);
|
||||
}
|
||||
LOG.errorv(cleanupFailure,
|
||||
"Failed to clean up RDS container {0}; retaining storage ownership for {1}",
|
||||
cleanupContainerId, effectiveRuntimeId);
|
||||
@@ -547,6 +554,44 @@ public class RdsContainerManager implements RdsBackendGate, RdsBackendEndpointRe
|
||||
}
|
||||
}
|
||||
|
||||
/** Removes a failed creation's container even after its transient Docker handle is lost. */
|
||||
public void stopForDelete(String runtimeId, String dockerVolumeName) {
|
||||
stopByRuntimeId(runtimeId);
|
||||
String exactVolumeName = requireSafeStorageComponent(dockerVolumeName, "Docker volume name");
|
||||
String currentName = ContainerStorageHelper.dockerName(config, exactVolumeName);
|
||||
String legacyName = ContainerStorageHelper.legacyDockerName(config, exactVolumeName);
|
||||
String currentKey = "container:" + currentName;
|
||||
String legacyKey = legacyName.equals(currentName) ? null : "container:" + legacyName;
|
||||
if (!claimedRuntimes.add(runtimeId)) {
|
||||
throw new IllegalStateException("RDS runtime " + runtimeId + " already has an active container");
|
||||
}
|
||||
boolean cleanupPendingForRetry = false;
|
||||
String cleanupName = currentName;
|
||||
try {
|
||||
claimStorage(currentKey, runtimeId);
|
||||
if (legacyKey != null) {
|
||||
claimStorage(legacyKey, runtimeId);
|
||||
}
|
||||
try {
|
||||
lifecycleManager.removeIfExistsStrict(currentName);
|
||||
if (legacyKey != null) {
|
||||
cleanupName = legacyName;
|
||||
lifecycleManager.removeIfExistsStrict(legacyName);
|
||||
}
|
||||
} catch (RuntimeException | Error e) {
|
||||
activeContainers.put(runtimeId, new RdsContainerHandle(
|
||||
cleanupName, runtimeId, runtimeId, null, 0, currentKey, legacyKey));
|
||||
cleanupPending.add(runtimeId);
|
||||
cleanupPendingForRetry = true;
|
||||
throw e;
|
||||
}
|
||||
} finally {
|
||||
if (!cleanupPendingForRetry) {
|
||||
releaseOwnership(currentKey, legacyKey, runtimeId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Probes a backing RDS container. UNKNOWN means the Docker probe failed and the container's
|
||||
* state is not known, which is not the same as the container being gone.
|
||||
@@ -1341,6 +1386,7 @@ public class RdsContainerManager implements RdsBackendGate, RdsBackendEndpointRe
|
||||
}
|
||||
claimedRuntimes.remove(runtimeId);
|
||||
cleanupPending.remove(runtimeId);
|
||||
preCreateCleanupPending.remove(runtimeId);
|
||||
}
|
||||
|
||||
private static String requireSafeStorageComponent(String value, String label) {
|
||||
|
||||
@@ -51,6 +51,8 @@ public class DbCluster {
|
||||
private String dockerVolumeName;
|
||||
private String volumeId;
|
||||
private String containerStorageResourceId;
|
||||
private boolean backendCleanupRequired;
|
||||
private boolean managedSecretCleanupRequired;
|
||||
|
||||
// Aurora Serverless v2 scaling (ACUs). Null when the cluster is not Serverless v2.
|
||||
private Double serverlessV2MinCapacity;
|
||||
@@ -214,6 +216,16 @@ public class DbCluster {
|
||||
this.containerStorageResourceId = containerStorageResourceId;
|
||||
}
|
||||
|
||||
public boolean isBackendCleanupRequired() { return backendCleanupRequired; }
|
||||
public void setBackendCleanupRequired(boolean backendCleanupRequired) {
|
||||
this.backendCleanupRequired = backendCleanupRequired;
|
||||
}
|
||||
|
||||
public boolean isManagedSecretCleanupRequired() { return managedSecretCleanupRequired; }
|
||||
public void setManagedSecretCleanupRequired(boolean managedSecretCleanupRequired) {
|
||||
this.managedSecretCleanupRequired = managedSecretCleanupRequired;
|
||||
}
|
||||
|
||||
public String getContainerId() { return containerId; }
|
||||
public void setContainerId(String containerId) { this.containerId = containerId; }
|
||||
|
||||
|
||||
@@ -89,6 +89,8 @@ public class DbInstance {
|
||||
private String dockerVolumeName;
|
||||
private String volumeId;
|
||||
private String containerStorageResourceId;
|
||||
private boolean backendCleanupRequired;
|
||||
private boolean managedSecretCleanupRequired;
|
||||
|
||||
private transient String containerId;
|
||||
private transient String containerHost;
|
||||
@@ -295,6 +297,16 @@ public class DbInstance {
|
||||
this.containerStorageResourceId = containerStorageResourceId;
|
||||
}
|
||||
|
||||
public boolean isBackendCleanupRequired() { return backendCleanupRequired; }
|
||||
public void setBackendCleanupRequired(boolean backendCleanupRequired) {
|
||||
this.backendCleanupRequired = backendCleanupRequired;
|
||||
}
|
||||
|
||||
public boolean isManagedSecretCleanupRequired() { return managedSecretCleanupRequired; }
|
||||
public void setManagedSecretCleanupRequired(boolean managedSecretCleanupRequired) {
|
||||
this.managedSecretCleanupRequired = managedSecretCleanupRequired;
|
||||
}
|
||||
|
||||
public String getContainerId() { return containerId; }
|
||||
public void setContainerId(String containerId) { this.containerId = containerId; }
|
||||
|
||||
|
||||
@@ -10,6 +10,8 @@ import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
@@ -27,6 +29,7 @@ public class RdsProxyManager {
|
||||
private final RdsBackendGate backendGate;
|
||||
private final RdsContainerManager containerManager;
|
||||
private final ConcurrentHashMap<String, RdsAuthProxy> proxies = new ConcurrentHashMap<>();
|
||||
private final ConcurrentHashMap<String, RdsAuthProxy> pendingFailedStarts = new ConcurrentHashMap<>();
|
||||
|
||||
@Inject
|
||||
public RdsProxyManager(RdsSigV4Validator sigV4Validator, RdsProxyTlsCertificates tlsCertificates,
|
||||
@@ -89,6 +92,14 @@ public class RdsProxyManager {
|
||||
String masterUsername, String masterPassword, String dbName,
|
||||
RdsAuthProxy.MasterPasswordCheck passwordValidator,
|
||||
RdsProxyBinding binding, RdsAuthProxy.EndpointKind endpointKind) {
|
||||
if (instanceId == null) {
|
||||
throw new IllegalArgumentException("RDS proxy instance ID is required");
|
||||
}
|
||||
RdsAuthProxy pending = pendingFailedStarts.get(instanceId);
|
||||
if (pending != null) {
|
||||
pending.stop();
|
||||
pendingFailedStarts.remove(instanceId, pending);
|
||||
}
|
||||
tlsCertificates.ensureHost(advertisedHost);
|
||||
EmulatorConfig.RdsServiceConfig rdsConfig = config.services().rds();
|
||||
RdsAuthProxy proxy = new RdsAuthProxy(
|
||||
@@ -103,14 +114,17 @@ public class RdsProxyManager {
|
||||
RuntimeException failure = new RuntimeException(
|
||||
"Failed to start RDS proxy for instance " + instanceId
|
||||
+ " on port " + proxyPort, e);
|
||||
cleanupFailedStart(proxy, failure);
|
||||
cleanupFailedStart(instanceId, proxy, failure);
|
||||
throw failure;
|
||||
} catch (RuntimeException e) {
|
||||
RuntimeException failure = new RuntimeException(
|
||||
"Failed to register RDS proxy for instance " + instanceId
|
||||
+ " on port " + proxyPort, e);
|
||||
cleanupFailedStart(proxy, failure);
|
||||
cleanupFailedStart(instanceId, proxy, failure);
|
||||
throw failure;
|
||||
} catch (Error e) {
|
||||
cleanupFailedStart(instanceId, proxy, e);
|
||||
throw e;
|
||||
}
|
||||
RdsAuthProxy previous;
|
||||
try {
|
||||
@@ -119,17 +133,20 @@ public class RdsProxyManager {
|
||||
RuntimeException failure = new RuntimeException(
|
||||
"Failed to register RDS proxy for instance " + instanceId
|
||||
+ " on port " + proxyPort, e);
|
||||
cleanupFailedStart(proxy, failure);
|
||||
cleanupFailedStart(instanceId, proxy, failure);
|
||||
throw failure;
|
||||
} catch (Error e) {
|
||||
cleanupFailedStart(instanceId, proxy, e);
|
||||
throw e;
|
||||
}
|
||||
if (previous != null) {
|
||||
try {
|
||||
previous.stop();
|
||||
} catch (RuntimeException e) {
|
||||
} catch (RuntimeException | Error e) {
|
||||
proxies.put(instanceId, previous);
|
||||
RuntimeException failure = new RuntimeException(
|
||||
"Failed to replace RDS proxy for instance " + instanceId, e);
|
||||
cleanupFailedStart(proxy, failure);
|
||||
cleanupFailedStart(instanceId, proxy, failure);
|
||||
throw failure;
|
||||
}
|
||||
}
|
||||
@@ -152,20 +169,45 @@ public class RdsProxyManager {
|
||||
}
|
||||
|
||||
public synchronized void stopProxy(String instanceId) {
|
||||
Throwable failure = null;
|
||||
RdsAuthProxy pending = pendingFailedStarts.get(instanceId);
|
||||
if (pending != null) {
|
||||
try {
|
||||
pending.stop();
|
||||
pendingFailedStarts.remove(instanceId, pending);
|
||||
} catch (RuntimeException | Error e) {
|
||||
failure = e;
|
||||
}
|
||||
}
|
||||
RdsAuthProxy proxy = proxies.get(instanceId);
|
||||
if (proxy != null) {
|
||||
proxy.stop();
|
||||
proxies.remove(instanceId, proxy);
|
||||
LOG.infov("Stopped RDS proxy for instance {0}", instanceId);
|
||||
try {
|
||||
proxy.stop();
|
||||
proxies.remove(instanceId, proxy);
|
||||
LOG.infov("Stopped RDS proxy for instance {0}", instanceId);
|
||||
} catch (RuntimeException | Error e) {
|
||||
if (failure == null) {
|
||||
failure = e;
|
||||
} else if (failure != e) {
|
||||
failure.addSuppressed(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (failure instanceof RuntimeException runtimeFailure) {
|
||||
throw runtimeFailure;
|
||||
}
|
||||
if (failure instanceof Error error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void stopAll() {
|
||||
proxies.forEach((instanceId, proxy) -> {
|
||||
Set<String> instanceIds = new HashSet<>(proxies.keySet());
|
||||
instanceIds.addAll(pendingFailedStarts.keySet());
|
||||
instanceIds.forEach(instanceId -> {
|
||||
try {
|
||||
proxy.stop();
|
||||
proxies.remove(instanceId, proxy);
|
||||
} catch (RuntimeException e) {
|
||||
stopProxy(instanceId);
|
||||
} catch (RuntimeException | Error e) {
|
||||
LOG.warnv(e, "Failed to stop RDS proxy for instance {0} during shutdown",
|
||||
instanceId);
|
||||
}
|
||||
@@ -173,11 +215,14 @@ public class RdsProxyManager {
|
||||
LOG.info("Stopped all RDS proxies");
|
||||
}
|
||||
|
||||
private void cleanupFailedStart(RdsAuthProxy proxy, RuntimeException failure) {
|
||||
private void cleanupFailedStart(String instanceId, RdsAuthProxy proxy, Throwable failure) {
|
||||
try {
|
||||
proxy.stop();
|
||||
} catch (RuntimeException cleanupFailure) {
|
||||
failure.addSuppressed(cleanupFailure);
|
||||
} catch (RuntimeException | Error cleanupFailure) {
|
||||
if (cleanupFailure != failure) {
|
||||
failure.addSuppressed(cleanupFailure);
|
||||
}
|
||||
pendingFailedStarts.put(instanceId, proxy);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user