mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(rds): global clusters and FailoverDBCluster (#3819)
CreateGlobalCluster creates an Aurora global database, empty or with an existing Aurora cluster as its primary, in which case engine, version, database name and encryption come from that cluster and may not be given. CreateDBCluster with GlobalClusterIdentifier joins a cluster: the first becomes the primary, every later one a secondary in a region that holds no member yet, with the primary's credentials and database name and a point-in-time copy of its data through the pg_dumpall mechanism that snapshots and read replicas use. DescribeGlobalClusters answers the real records for RDS, with IsWriter and Readers per member; DocumentDB and Neptune keep the shared empty answer. SwitchoverGlobalCluster and FailoverGlobalCluster promote the named secondary and demote the current primary to a secondary, the topology AWS keeps on switchover and restores after a managed failover. RemoveFromGlobalCluster detaches a member into a standalone cluster and the primary goes last; DeleteGlobalCluster needs an empty, unprotected global cluster; ModifyGlobalCluster renames, protects or upgrades, with members following. DBCluster now carries the writer role: the first instance is the writer, IsClusterWriter reports it instead of true for every member, deleting the writer promotes a remaining member, and FailoverDBCluster moves the role to the named reader or the first one; a cluster without a reader is refused with InvalidDBClusterStateFault. Closes #3778
This commit is contained in:
1 parent
22b0627245
commit
54a5cf3e9c
9 files changed
+1516
-9
No files matched your search
@@ -5,7 +5,6 @@ import io.github.hectorvent.floci.core.common.AwsErrorMessages;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.AwsNamespaces;
|
||||
import io.github.hectorvent.floci.core.common.AwsQueryResponse;
|
||||
import io.github.hectorvent.floci.core.common.RdsFamilyQuerySupport;
|
||||
import io.github.hectorvent.floci.core.common.XmlBuilder;
|
||||
import io.github.hectorvent.floci.services.rds.model.DatabaseEngine;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbCluster;
|
||||
@@ -22,6 +21,8 @@ import io.github.hectorvent.floci.services.rds.model.DbProxyAuth;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbProxyTarget;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbProxyTargetGroup;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbSubnetGroup;
|
||||
import io.github.hectorvent.floci.services.rds.model.GlobalCluster;
|
||||
import io.github.hectorvent.floci.services.rds.model.GlobalClusterMember;
|
||||
import io.github.hectorvent.floci.services.rds.model.OptionGroup;
|
||||
import io.github.hectorvent.floci.services.rds.model.OptionGroupOption;
|
||||
import io.github.hectorvent.floci.services.rds.model.RdsEvent;
|
||||
@@ -122,6 +123,13 @@ public class RdsQueryHandler {
|
||||
case "DescribeDBProxyTargets" -> handleDescribeDbProxyTargets(params, region);
|
||||
case "DescribeDBClusterSnapshots" -> handleDescribeDbClusterSnapshots(params);
|
||||
case "DescribeGlobalClusters" -> handleDescribeGlobalClusters(params);
|
||||
case "CreateGlobalCluster" -> handleCreateGlobalCluster(params, region);
|
||||
case "ModifyGlobalCluster" -> handleModifyGlobalCluster(params);
|
||||
case "DeleteGlobalCluster" -> handleDeleteGlobalCluster(params);
|
||||
case "RemoveFromGlobalCluster" -> handleRemoveFromGlobalCluster(params, region);
|
||||
case "FailoverGlobalCluster" -> handleFailoverGlobalCluster(params, region);
|
||||
case "SwitchoverGlobalCluster" -> handleSwitchoverGlobalCluster(params, region);
|
||||
case "FailoverDBCluster" -> handleFailoverDbCluster(params, region);
|
||||
case "AddTagsToResource" -> handleAddTagsToResource(params, region);
|
||||
case "ListTagsForResource" -> handleListTagsForResource(params, region);
|
||||
case "RemoveTagsFromResource" -> handleRemoveTagsFromResource(params, region);
|
||||
@@ -682,11 +690,18 @@ public class RdsQueryHandler {
|
||||
Double serverlessV2Max = parseDoubleParam(params, "ServerlessV2ScalingConfiguration.MaxCapacity");
|
||||
Integer serverlessV2SecondsUntilAutoPause = parseIntegerParam(
|
||||
params, "ServerlessV2ScalingConfiguration.SecondsUntilAutoPause");
|
||||
DbCluster cluster = service.createDbCluster(id, engine, engineVersion, masterUsername,
|
||||
masterPassword, databaseName, iamEnabled, paramGroupName,
|
||||
dbSubnetGroupName, availabilityZone, multiAz, region,
|
||||
serverlessV2Min, serverlessV2Max, serverlessV2SecondsUntilAutoPause,
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId, engineMode, storageEncrypted);
|
||||
String globalClusterIdentifier = params.getFirst("GlobalClusterIdentifier");
|
||||
DbCluster cluster = globalClusterIdentifier != null && !globalClusterIdentifier.isBlank()
|
||||
? service.createDbClusterInGlobalCluster(globalClusterIdentifier, id, engine,
|
||||
params.getFirst("EngineVersion"), masterUsername, masterPassword, databaseName,
|
||||
iamEnabled, paramGroupName, dbSubnetGroupName, availabilityZone, multiAz, region,
|
||||
serverlessV2Min, serverlessV2Max, serverlessV2SecondsUntilAutoPause,
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId, engineMode, storageEncrypted)
|
||||
: service.createDbCluster(id, engine, engineVersion, masterUsername,
|
||||
masterPassword, databaseName, iamEnabled, paramGroupName,
|
||||
dbSubnetGroupName, availabilityZone, multiAz, region,
|
||||
serverlessV2Min, serverlessV2Max, serverlessV2SecondsUntilAutoPause,
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId, engineMode, storageEncrypted);
|
||||
String result = dbClusterXml(cluster);
|
||||
return Response.ok(AwsQueryResponse.envelope("CreateDBCluster", AwsNamespaces.RDS, result)).build();
|
||||
} catch (AwsException e) {
|
||||
@@ -1651,8 +1666,207 @@ public class RdsQueryHandler {
|
||||
return xml.build();
|
||||
}
|
||||
|
||||
// ── Global clusters ───────────────────────────────────────────────────────
|
||||
|
||||
private Response handleDescribeGlobalClusters(MultivaluedMap<String, String> params) {
|
||||
return RdsFamilyQuerySupport.handleDescribeGlobalClusters(LOG, params);
|
||||
String maxRecords = params.getFirst("MaxRecords");
|
||||
if (maxRecords != null && !maxRecords.isBlank()) {
|
||||
int max = -1;
|
||||
try {
|
||||
max = Integer.parseInt(maxRecords.trim());
|
||||
} catch (NumberFormatException e) {
|
||||
LOG.debugv("Non-numeric MaxRecords {0} on DescribeGlobalClusters", maxRecords);
|
||||
}
|
||||
if (max < 20 || max > 100) {
|
||||
return AwsQueryResponse.error("InvalidParameterValue",
|
||||
"Invalid value " + maxRecords + " for MaxRecords. Must be between 20 and 100",
|
||||
AwsNamespaces.RDS, 400);
|
||||
}
|
||||
}
|
||||
try {
|
||||
// AWS resolves the identifier before it looks at the marker, so a bad marker with an
|
||||
// unknown identifier answers not found.
|
||||
String identifier = params.getFirst("GlobalClusterIdentifier");
|
||||
List<GlobalCluster> result = identifier != null && !identifier.isBlank()
|
||||
? List.of(service.describeGlobalCluster(identifier))
|
||||
: service.listGlobalClusters();
|
||||
String marker = params.getFirst("Marker");
|
||||
if (marker != null && !marker.isBlank()) {
|
||||
return AwsQueryResponse.error("InvalidParameterValue", "The request token is invalid.", AwsNamespaces.RDS, 400);
|
||||
}
|
||||
XmlBuilder xml = new XmlBuilder().start("GlobalClusters");
|
||||
for (GlobalCluster global : result) {
|
||||
xml.raw(globalClusterXml(global, "GlobalClusterMember"));
|
||||
}
|
||||
xml.end("GlobalClusters");
|
||||
return Response.ok(AwsQueryResponse.envelope("DescribeGlobalClusters", AwsNamespaces.RDS, xml.build())).build();
|
||||
} catch (AwsException e) {
|
||||
return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.RDS, e.getHttpStatus());
|
||||
}
|
||||
}
|
||||
|
||||
private Response handleCreateGlobalCluster(MultivaluedMap<String, String> params, String region) {
|
||||
String id = params.getFirst("GlobalClusterIdentifier");
|
||||
if (id == null || id.isBlank()) {
|
||||
return AwsQueryResponse.error("InvalidParameterValue", "GlobalClusterIdentifier is required.", AwsNamespaces.RDS, 400);
|
||||
}
|
||||
try {
|
||||
GlobalCluster global = service.createGlobalCluster(id,
|
||||
params.getFirst("SourceDBClusterIdentifier"),
|
||||
params.getFirst("Engine"),
|
||||
params.getFirst("EngineVersion"),
|
||||
params.getFirst("DatabaseName"),
|
||||
parseOptionalBoolean(params, "StorageEncrypted"),
|
||||
parseOptionalBoolean(params, "DeletionProtection"),
|
||||
parseTags(params), region);
|
||||
return Response.ok(AwsQueryResponse.envelope("CreateGlobalCluster", AwsNamespaces.RDS,
|
||||
globalClusterXml(global, "GlobalCluster"))).build();
|
||||
} catch (AwsException e) {
|
||||
return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.RDS, e.getHttpStatus());
|
||||
}
|
||||
}
|
||||
|
||||
private Response handleModifyGlobalCluster(MultivaluedMap<String, String> params) {
|
||||
String id = params.getFirst("GlobalClusterIdentifier");
|
||||
if (id == null || id.isBlank()) {
|
||||
return AwsQueryResponse.error("InvalidParameterValue", "GlobalClusterIdentifier is required.", AwsNamespaces.RDS, 400);
|
||||
}
|
||||
try {
|
||||
GlobalCluster global = service.modifyGlobalCluster(id,
|
||||
params.getFirst("NewGlobalClusterIdentifier"),
|
||||
parseOptionalBoolean(params, "DeletionProtection"),
|
||||
params.getFirst("EngineVersion"),
|
||||
parseOptionalBoolean(params, "AllowMajorVersionUpgrade"));
|
||||
return Response.ok(AwsQueryResponse.envelope("ModifyGlobalCluster", AwsNamespaces.RDS,
|
||||
globalClusterXml(global, "GlobalCluster"))).build();
|
||||
} catch (AwsException e) {
|
||||
return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.RDS, e.getHttpStatus());
|
||||
}
|
||||
}
|
||||
|
||||
private Response handleDeleteGlobalCluster(MultivaluedMap<String, String> params) {
|
||||
String id = params.getFirst("GlobalClusterIdentifier");
|
||||
if (id == null || id.isBlank()) {
|
||||
return AwsQueryResponse.error("InvalidParameterValue", "GlobalClusterIdentifier is required.", AwsNamespaces.RDS, 400);
|
||||
}
|
||||
try {
|
||||
GlobalCluster global = service.deleteGlobalCluster(id);
|
||||
return Response.ok(AwsQueryResponse.envelope("DeleteGlobalCluster", AwsNamespaces.RDS,
|
||||
globalClusterXml(global, "GlobalCluster"))).build();
|
||||
} catch (AwsException e) {
|
||||
return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.RDS, e.getHttpStatus());
|
||||
}
|
||||
}
|
||||
|
||||
private Response handleRemoveFromGlobalCluster(MultivaluedMap<String, String> params, String region) {
|
||||
String id = params.getFirst("GlobalClusterIdentifier");
|
||||
String clusterId = params.getFirst("DbClusterIdentifier");
|
||||
if (id == null || id.isBlank()) {
|
||||
return AwsQueryResponse.error("InvalidParameterValue", "GlobalClusterIdentifier is required.", AwsNamespaces.RDS, 400);
|
||||
}
|
||||
if (clusterId == null || clusterId.isBlank()) {
|
||||
return AwsQueryResponse.error("InvalidParameterValue", "DbClusterIdentifier is required.", AwsNamespaces.RDS, 400);
|
||||
}
|
||||
try {
|
||||
GlobalCluster global = service.removeFromGlobalCluster(id, clusterId, region);
|
||||
return Response.ok(AwsQueryResponse.envelope("RemoveFromGlobalCluster", AwsNamespaces.RDS,
|
||||
globalClusterXml(global, "GlobalCluster"))).build();
|
||||
} catch (AwsException e) {
|
||||
return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.RDS, e.getHttpStatus());
|
||||
}
|
||||
}
|
||||
|
||||
private Response handleFailoverGlobalCluster(MultivaluedMap<String, String> params, String region) {
|
||||
String id = params.getFirst("GlobalClusterIdentifier");
|
||||
String target = params.getFirst("TargetDbClusterIdentifier");
|
||||
if (id == null || id.isBlank()) {
|
||||
return AwsQueryResponse.error("InvalidParameterValue", "GlobalClusterIdentifier is required.", AwsNamespaces.RDS, 400);
|
||||
}
|
||||
if (target == null || target.isBlank()) {
|
||||
return AwsQueryResponse.error("InvalidParameterValue", "TargetDbClusterIdentifier is required.", AwsNamespaces.RDS, 400);
|
||||
}
|
||||
try {
|
||||
GlobalCluster global = service.failoverGlobalCluster(id, target,
|
||||
parseOptionalBoolean(params, "AllowDataLoss"),
|
||||
parseOptionalBoolean(params, "Switchover"), region);
|
||||
return Response.ok(AwsQueryResponse.envelope("FailoverGlobalCluster", AwsNamespaces.RDS,
|
||||
globalClusterXml(global, "GlobalCluster"))).build();
|
||||
} catch (AwsException e) {
|
||||
return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.RDS, e.getHttpStatus());
|
||||
}
|
||||
}
|
||||
|
||||
private Response handleSwitchoverGlobalCluster(MultivaluedMap<String, String> params, String region) {
|
||||
String id = params.getFirst("GlobalClusterIdentifier");
|
||||
String target = params.getFirst("TargetDbClusterIdentifier");
|
||||
if (id == null || id.isBlank()) {
|
||||
return AwsQueryResponse.error("InvalidParameterValue", "GlobalClusterIdentifier is required.", AwsNamespaces.RDS, 400);
|
||||
}
|
||||
if (target == null || target.isBlank()) {
|
||||
return AwsQueryResponse.error("InvalidParameterValue", "TargetDbClusterIdentifier is required.", AwsNamespaces.RDS, 400);
|
||||
}
|
||||
try {
|
||||
GlobalCluster global = service.switchoverGlobalCluster(id, target, region);
|
||||
return Response.ok(AwsQueryResponse.envelope("SwitchoverGlobalCluster", AwsNamespaces.RDS,
|
||||
globalClusterXml(global, "GlobalCluster"))).build();
|
||||
} catch (AwsException e) {
|
||||
return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.RDS, e.getHttpStatus());
|
||||
}
|
||||
}
|
||||
|
||||
private Response handleFailoverDbCluster(MultivaluedMap<String, String> params, String region) {
|
||||
String id = params.getFirst("DBClusterIdentifier");
|
||||
if (id == null || id.isBlank()) {
|
||||
return AwsQueryResponse.error("InvalidParameterValue", "DBClusterIdentifier is required.", AwsNamespaces.RDS, 400);
|
||||
}
|
||||
try {
|
||||
DbCluster cluster = service.failoverDbCluster(id, params.getFirst("TargetDBInstanceIdentifier"), region);
|
||||
return Response.ok(AwsQueryResponse.envelope("FailoverDBCluster", AwsNamespaces.RDS,
|
||||
dbClusterXml(cluster))).build();
|
||||
} catch (AwsException e) {
|
||||
return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.RDS, e.getHttpStatus());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The GlobalCluster shape. The list element of DescribeGlobalClusters is named
|
||||
* GlobalClusterMember by the service model, the same name as the entries of GlobalClusterMembers.
|
||||
*/
|
||||
private static String globalClusterXml(GlobalCluster g, String elementName) {
|
||||
XmlBuilder xml = new XmlBuilder().start(elementName)
|
||||
.elem("GlobalClusterIdentifier", g.getGlobalClusterIdentifier())
|
||||
.elem("GlobalClusterResourceId", g.getGlobalClusterResourceId())
|
||||
.elem("GlobalClusterArn", g.getGlobalClusterArn())
|
||||
.elem("Status", g.getStatus())
|
||||
.elem("Engine", g.getEngine())
|
||||
.elem("EngineVersion", g.getEngineVersion());
|
||||
if (g.getDatabaseName() != null && !g.getDatabaseName().isBlank()) {
|
||||
xml.elem("DatabaseName", g.getDatabaseName());
|
||||
}
|
||||
xml.elem("StorageEncrypted", g.isStorageEncrypted())
|
||||
.elem("DeletionProtection", g.isDeletionProtection());
|
||||
List<String> readerArns = g.getMembers().stream()
|
||||
.filter(member -> !member.isWriter())
|
||||
.map(GlobalClusterMember::getDbClusterArn)
|
||||
.toList();
|
||||
xml.start("GlobalClusterMembers");
|
||||
for (GlobalClusterMember member : g.getMembers()) {
|
||||
xml.start("GlobalClusterMember")
|
||||
.elem("DBClusterArn", member.getDbClusterArn());
|
||||
xml.start("Readers");
|
||||
if (member.isWriter()) {
|
||||
readerArns.forEach(arn -> xml.elem("member", arn));
|
||||
}
|
||||
xml.end("Readers");
|
||||
xml.elem("IsWriter", member.isWriter())
|
||||
.elem("SynchronizationStatus", "connected")
|
||||
.end("GlobalClusterMember");
|
||||
}
|
||||
xml.end("GlobalClusterMembers");
|
||||
xml.start("TagList");
|
||||
writeTags(xml, g.getTags());
|
||||
xml.end("TagList");
|
||||
return xml.end(elementName).build();
|
||||
}
|
||||
|
||||
private Response handleDescribeDbClusterSnapshots(MultivaluedMap<String, String> params) {
|
||||
@@ -1952,12 +2166,16 @@ public class RdsQueryHandler {
|
||||
}
|
||||
xml.end("ServerlessV2ScalingConfiguration");
|
||||
}
|
||||
if (c.getGlobalClusterIdentifier() != null && !c.getGlobalClusterIdentifier().isBlank()) {
|
||||
xml.elem("GlobalClusterIdentifier", c.getGlobalClusterIdentifier());
|
||||
}
|
||||
xml.start("DBClusterMembers");
|
||||
if (c.getDbClusterMembers() != null) {
|
||||
String writer = c.resolveWriterIdentifier();
|
||||
for (String memberId : c.getDbClusterMembers()) {
|
||||
xml.start("member")
|
||||
.elem("DBInstanceIdentifier", memberId)
|
||||
.elem("IsClusterWriter", true)
|
||||
.elem("IsClusterWriter", memberId.equals(writer))
|
||||
.end("member");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,6 +41,8 @@ import io.github.hectorvent.floci.services.rds.model.RdsEvent;
|
||||
import io.github.hectorvent.floci.services.rds.model.ReadReplicaRequest;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbSnapshot;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbSubnetGroup;
|
||||
import io.github.hectorvent.floci.services.rds.model.GlobalCluster;
|
||||
import io.github.hectorvent.floci.services.rds.model.GlobalClusterMember;
|
||||
import io.github.hectorvent.floci.services.rds.model.OptionGroup;
|
||||
import io.github.hectorvent.floci.services.rds.model.OptionGroupOption;
|
||||
import io.github.hectorvent.floci.services.rds.proxy.RdsProxyManager;
|
||||
@@ -145,6 +147,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
private final StorageBackend<String, DbProxy> proxies;
|
||||
private final StorageBackend<String, DbProxyTargetGroup> proxyTargetGroups;
|
||||
private final StorageBackend<String, DbSnapshot> snapshots;
|
||||
private final StorageBackend<String, GlobalCluster> globalClusters;
|
||||
private final StorageBackend<String, String> snapshotData;
|
||||
private StorageBackend<String, RdsEvent> events = new InMemoryStorage<>();
|
||||
private final RdsContainerManager containerManager;
|
||||
@@ -224,6 +227,8 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
new TypeReference<Map<String, DbProxy>>() {});
|
||||
this.proxyTargetGroups = storageFactory.create("rds", "rds-proxy-target-groups.json",
|
||||
new TypeReference<Map<String, DbProxyTargetGroup>>() {});
|
||||
this.globalClusters = storageFactory.create("rds", "rds-global-clusters.json",
|
||||
new TypeReference<Map<String, GlobalCluster>>() {});
|
||||
this.snapshots = storageFactory.create("rds", "rds-snapshots.json",
|
||||
new TypeReference<Map<String, DbSnapshot>>() {});
|
||||
this.snapshotData = storageFactory.create("rds", "rds-snapshot-data.json",
|
||||
@@ -361,6 +366,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
this.proxies = proxies;
|
||||
this.proxyTargetGroups = proxyTargetGroups;
|
||||
this.taggingService = taggingService;
|
||||
this.globalClusters = new io.github.hectorvent.floci.core.storage.InMemoryStorage<>();
|
||||
this.snapshots = new io.github.hectorvent.floci.core.storage.InMemoryStorage<>();
|
||||
this.snapshotData = new io.github.hectorvent.floci.core.storage.InMemoryStorage<>();
|
||||
}
|
||||
@@ -751,6 +757,10 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
currentAccountId(), effectiveRegion, dbClusterIdentifier);
|
||||
if (cluster != null) {
|
||||
cluster.getDbClusterMembers().add(id);
|
||||
if (cluster.resolveWriterIdentifier() == null
|
||||
|| !cluster.getDbClusterMembers().contains(cluster.getClusterWriterIdentifier())) {
|
||||
cluster.setClusterWriterIdentifier(cluster.resolveWriterIdentifier());
|
||||
}
|
||||
putClusterForScope(currentAccountId(), effectiveRegion,
|
||||
dbClusterIdentifier, cluster);
|
||||
}
|
||||
@@ -2301,6 +2311,8 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
currentAccountId(), effectiveRegion, clusterId);
|
||||
if (cluster != null) {
|
||||
cluster.getDbClusterMembers().remove(id);
|
||||
// Losing the writer promotes a remaining member, as Aurora fails over on its own.
|
||||
cluster.setClusterWriterIdentifier(cluster.resolveWriterIdentifier());
|
||||
putClusterForScope(currentAccountId(), effectiveRegion, clusterId, cluster);
|
||||
}
|
||||
}
|
||||
@@ -2785,6 +2797,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
throw new AwsException("InvalidDBClusterStateFault",
|
||||
"DB cluster " + id + " is registered with a DB proxy target group.", 400);
|
||||
}
|
||||
detachFromGlobalClusterBeforeDelete(cluster);
|
||||
|
||||
detachManagedMasterUserSecret(cluster, effectiveRegion);
|
||||
|
||||
@@ -2811,6 +2824,546 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
LOG.infov("DB cluster {0} deleted", id);
|
||||
}
|
||||
|
||||
// ── Global clusters (Aurora global databases) ─────────────────────────────
|
||||
|
||||
private static final Set<String> GLOBAL_CLUSTER_ENGINES = Set.of("aurora-mysql", "aurora-postgresql");
|
||||
|
||||
/**
|
||||
* Creates an Aurora global database, empty or with an existing cluster (named by ARN or, in
|
||||
* the request Region, by identifier) as its primary. With a source, engine, version, database
|
||||
* name and encryption come from that cluster and may not be given, as the API reference
|
||||
* states.
|
||||
*/
|
||||
public synchronized GlobalCluster createGlobalCluster(String id, String sourceDbClusterIdentifier,
|
||||
String engine, String engineVersion,
|
||||
String databaseName, Boolean storageEncrypted,
|
||||
Boolean deletionProtection,
|
||||
Map<String, String> tags, String region) {
|
||||
if (id == null || id.isBlank()) {
|
||||
throw new AwsException("InvalidParameterValue", "GlobalClusterIdentifier is required.", 400);
|
||||
}
|
||||
String accountId = currentAccountId();
|
||||
if (findGlobalCluster(accountId, id) != null) {
|
||||
throw new AwsException("GlobalClusterAlreadyExistsFault",
|
||||
"Global cluster " + id + " already exists.", 400);
|
||||
}
|
||||
GlobalCluster global = new GlobalCluster();
|
||||
global.setGlobalClusterIdentifier(id.toLowerCase(Locale.ROOT));
|
||||
global.setGlobalClusterResourceId("cluster-" + java.util.UUID.randomUUID().toString()
|
||||
.replace("-", "").substring(0, 24).toUpperCase());
|
||||
global.setGlobalClusterArn(globalClusterArn(accountId, global.getGlobalClusterIdentifier()));
|
||||
global.setStatus("available");
|
||||
global.setDeletionProtection(Boolean.TRUE.equals(deletionProtection));
|
||||
global.setTags(tags);
|
||||
global.setCreatedAt(Instant.now());
|
||||
|
||||
if (sourceDbClusterIdentifier != null && !sourceDbClusterIdentifier.isBlank()) {
|
||||
if (hasText(engine) || hasText(engineVersion) || hasText(databaseName)
|
||||
|| storageEncrypted != null) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
"Engine, EngineVersion, DatabaseName and StorageEncrypted can't be specified "
|
||||
+ "when SourceDBClusterIdentifier is specified; the global cluster uses the "
|
||||
+ "values of the source DB cluster.", 400);
|
||||
}
|
||||
DbCluster source = resolveClusterReference(sourceDbClusterIdentifier, region);
|
||||
requireGlobalClusterEngine(source.getEngineIdentifier(), source.getDbClusterIdentifier());
|
||||
if (source.getGlobalClusterIdentifier() != null) {
|
||||
throw new AwsException("InvalidDBClusterStateFault",
|
||||
"DB cluster " + source.getDbClusterIdentifier() + " is already a member of "
|
||||
+ "global cluster " + source.getGlobalClusterIdentifier() + ".", 400);
|
||||
}
|
||||
global.setEngine(source.getEngineIdentifier());
|
||||
global.setEngineVersion(source.getEngineVersion());
|
||||
global.setDatabaseName(source.getDatabaseName());
|
||||
global.setStorageEncrypted(source.isStorageEncrypted());
|
||||
global.getMembers().add(new GlobalClusterMember(source.getDbClusterArn(), true));
|
||||
source.setGlobalClusterIdentifier(global.getGlobalClusterIdentifier());
|
||||
putClusterForScope(accountId, regionFromArn(source.getDbClusterArn()),
|
||||
source.getDbClusterIdentifier(), source);
|
||||
} else {
|
||||
if (!hasText(engine)) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
"Engine must be specified when SourceDBClusterIdentifier is not.", 400);
|
||||
}
|
||||
requireGlobalClusterEngine(engine, null);
|
||||
global.setEngine(engine.toLowerCase(Locale.ROOT));
|
||||
global.setEngineVersion(hasText(engineVersion)
|
||||
? engineVersion : defaultGlobalEngineVersion(global.getEngine()));
|
||||
global.setDatabaseName(hasText(databaseName) ? databaseName : null);
|
||||
global.setStorageEncrypted(Boolean.TRUE.equals(storageEncrypted));
|
||||
}
|
||||
putGlobalCluster(accountId, global);
|
||||
LOG.infov("Global cluster {0} created, engine={1}", global.getGlobalClusterIdentifier(), global.getEngine());
|
||||
return global;
|
||||
}
|
||||
|
||||
/**
|
||||
* CreateDBCluster with GlobalClusterIdentifier: the first cluster becomes the primary, every
|
||||
* later one a secondary in a Region that has neither the primary nor another secondary. A
|
||||
* secondary takes credentials, database name, version and encryption from the primary and
|
||||
* may not be given its own, and its database is initialised from a dump of the primary the
|
||||
* way a read replica is.
|
||||
*/
|
||||
public DbCluster createDbClusterInGlobalCluster(String globalClusterIdentifier, String id,
|
||||
String engineParam, String engineVersion,
|
||||
String masterUsername, String masterPassword,
|
||||
String databaseName, boolean iamEnabled,
|
||||
String paramGroupName, String dbSubnetGroupName,
|
||||
String availabilityZone, boolean multiAz, String region,
|
||||
Double serverlessV2MinCapacity, Double serverlessV2MaxCapacity,
|
||||
Integer serverlessV2SecondsUntilAutoPause,
|
||||
boolean manageMasterUserPassword, String masterUserSecretKmsKeyId,
|
||||
String engineMode, boolean storageEncrypted) {
|
||||
String effectiveRegion = effectiveRegion(region);
|
||||
String accountId = currentAccountId();
|
||||
GlobalCluster global = requireGlobalCluster(accountId, globalClusterIdentifier);
|
||||
String engineName = hasText(engineParam) ? engineParam.toLowerCase(Locale.ROOT) : global.getEngine();
|
||||
if (!engineName.equals(global.getEngine())) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
"Engine " + engineName + " does not match engine " + global.getEngine()
|
||||
+ " of global cluster " + global.getGlobalClusterIdentifier() + ".", 400);
|
||||
}
|
||||
DbCluster primary = global.findPrimary()
|
||||
.map(member -> findClusterByArn(accountId, member.getDbClusterArn()))
|
||||
.orElse(null);
|
||||
DbCluster cluster;
|
||||
if (primary == null) {
|
||||
cluster = createDbCluster(id, engineName, hasText(engineVersion) ? engineVersion : global.getEngineVersion(),
|
||||
masterUsername, masterPassword,
|
||||
hasText(databaseName) ? databaseName : global.getDatabaseName(), iamEnabled,
|
||||
paramGroupName, dbSubnetGroupName, availabilityZone, multiAz, effectiveRegion,
|
||||
serverlessV2MinCapacity, serverlessV2MaxCapacity, serverlessV2SecondsUntilAutoPause,
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId, engineMode,
|
||||
storageEncrypted || global.isStorageEncrypted());
|
||||
} else {
|
||||
if (hasText(masterUsername) || hasText(masterPassword) || manageMasterUserPassword) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
"Cannot specify user name for cross region replication cluster", 400);
|
||||
}
|
||||
if (hasText(databaseName)) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
"Cannot specify database name for cross region replication cluster", 400);
|
||||
}
|
||||
String primaryRegion = regionFromArn(primary.getDbClusterArn());
|
||||
if (primaryRegion.equals(effectiveRegion)) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
"A secondary cluster must be in a different Region than the primary cluster "
|
||||
+ primary.getDbClusterIdentifier() + " (" + primaryRegion + ").", 400);
|
||||
}
|
||||
for (GlobalClusterMember member : global.getMembers()) {
|
||||
if (regionFromArn(member.getDbClusterArn()).equals(effectiveRegion)) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
"Global cluster " + global.getGlobalClusterIdentifier()
|
||||
+ " already has a cluster in " + effectiveRegion + ".", 400);
|
||||
}
|
||||
}
|
||||
if (primary.getEngine() != DatabaseEngine.POSTGRES) {
|
||||
// The point-in-time copy of the primary is pg_dumpall based, the line
|
||||
// CreateDBSnapshot and CreateDBInstanceReadReplica draw as well.
|
||||
throw new AwsException("InvalidDBClusterStateFault",
|
||||
"Adding a secondary cluster is not supported for engine "
|
||||
+ primary.getEngineIdentifier() + ".", 400);
|
||||
}
|
||||
cluster = createDbCluster(id, engineName, primary.getEngineVersion(),
|
||||
primary.getMasterUsername(), primary.getMasterPassword(),
|
||||
primary.getDatabaseName(), iamEnabled, paramGroupName, dbSubnetGroupName,
|
||||
availabilityZone, multiAz, effectiveRegion, serverlessV2MinCapacity,
|
||||
serverlessV2MaxCapacity, serverlessV2SecondsUntilAutoPause, false, null,
|
||||
engineMode, primary.isStorageEncrypted());
|
||||
}
|
||||
try {
|
||||
attachToGlobalCluster(accountId, global.getGlobalClusterIdentifier(), cluster, primary == null);
|
||||
if (primary != null && !config.services().rds().mock()
|
||||
&& primary.getContainerId() != null && cluster.getContainerId() != null) {
|
||||
String sqlDump = containerManager.createPostgresSnapshot(
|
||||
primary.getContainerId(), primary.getMasterUsername());
|
||||
containerManager.restorePostgresSnapshot(
|
||||
cluster.getContainerId(), cluster.getMasterUsername(), sqlDump);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
try {
|
||||
deleteDbCluster(id, effectiveRegion);
|
||||
} catch (RuntimeException cleanupError) {
|
||||
e.addSuppressed(cleanupError);
|
||||
}
|
||||
if (e instanceof AwsException aws) {
|
||||
throw aws;
|
||||
}
|
||||
AwsException failure = new AwsException("InvalidDBClusterStateFault",
|
||||
"Failed to initialise secondary cluster " + id + " from the primary: " + e.getMessage(), 400);
|
||||
failure.initCause(e);
|
||||
throw failure;
|
||||
}
|
||||
return getDbCluster(id, effectiveRegion);
|
||||
}
|
||||
|
||||
public synchronized GlobalCluster describeGlobalCluster(String id) {
|
||||
return requireGlobalCluster(currentAccountId(), id);
|
||||
}
|
||||
|
||||
public synchronized List<GlobalCluster> listGlobalClusters() {
|
||||
String accountId = currentAccountId();
|
||||
List<GlobalCluster> result = new ArrayList<>();
|
||||
if (globalClusters instanceof AccountAwareStorageBackend<GlobalCluster> aware) {
|
||||
result.addAll(aware.scanForAccount(accountId, k -> true));
|
||||
} else {
|
||||
for (GlobalCluster candidate : globalClusters.scan(k -> true)) {
|
||||
if (accountId.equals(accountIdFromArn(candidate.getGlobalClusterArn()))) {
|
||||
result.add(candidate);
|
||||
}
|
||||
}
|
||||
}
|
||||
result.sort(Comparator.comparing(GlobalCluster::getGlobalClusterIdentifier));
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renames the global cluster, toggles deletion protection or upgrades the engine version,
|
||||
* which member clusters follow. A rename is reflected on every member.
|
||||
*/
|
||||
public synchronized GlobalCluster modifyGlobalCluster(String id, String newGlobalClusterIdentifier,
|
||||
Boolean deletionProtection, String engineVersion,
|
||||
Boolean allowMajorVersionUpgrade) {
|
||||
String accountId = currentAccountId();
|
||||
GlobalCluster global = requireGlobalCluster(accountId, id);
|
||||
if (hasText(engineVersion) && !engineVersion.equals(global.getEngineVersion())
|
||||
&& !sameMajorVersion(global.getEngineVersion(), engineVersion)
|
||||
&& !Boolean.TRUE.equals(allowMajorVersionUpgrade)) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
"The AllowMajorVersionUpgrade flag must be present when upgrading to a new major version.", 400);
|
||||
}
|
||||
if (hasText(newGlobalClusterIdentifier)
|
||||
&& !newGlobalClusterIdentifier.equalsIgnoreCase(global.getGlobalClusterIdentifier())) {
|
||||
String newId = newGlobalClusterIdentifier.toLowerCase(Locale.ROOT);
|
||||
if (findGlobalCluster(accountId, newId) != null) {
|
||||
throw new AwsException("GlobalClusterAlreadyExistsFault",
|
||||
"Global cluster " + newId + " already exists.", 400);
|
||||
}
|
||||
deleteGlobalClusterRecord(accountId, global.getGlobalClusterIdentifier());
|
||||
global.setGlobalClusterIdentifier(newId);
|
||||
global.setGlobalClusterArn(globalClusterArn(accountId, newId));
|
||||
for (GlobalClusterMember member : global.getMembers()) {
|
||||
DbCluster cluster = findClusterByArn(accountId, member.getDbClusterArn());
|
||||
if (cluster != null) {
|
||||
cluster.setGlobalClusterIdentifier(newId);
|
||||
putClusterForScope(accountId, regionFromArn(cluster.getDbClusterArn()),
|
||||
cluster.getDbClusterIdentifier(), cluster);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (deletionProtection != null) {
|
||||
global.setDeletionProtection(deletionProtection);
|
||||
}
|
||||
if (hasText(engineVersion)) {
|
||||
global.setEngineVersion(engineVersion);
|
||||
for (GlobalClusterMember member : global.getMembers()) {
|
||||
DbCluster cluster = findClusterByArn(accountId, member.getDbClusterArn());
|
||||
if (cluster != null) {
|
||||
cluster.setEngineVersion(engineVersion);
|
||||
putClusterForScope(accountId, regionFromArn(cluster.getDbClusterArn()),
|
||||
cluster.getDbClusterIdentifier(), cluster);
|
||||
}
|
||||
}
|
||||
}
|
||||
putGlobalCluster(accountId, global);
|
||||
return global;
|
||||
}
|
||||
|
||||
/** Deletes an empty global cluster; one with members or deletion protection is refused. */
|
||||
public synchronized GlobalCluster deleteGlobalCluster(String id) {
|
||||
String accountId = currentAccountId();
|
||||
GlobalCluster global = requireGlobalCluster(accountId, id);
|
||||
if (global.isDeletionProtection()) {
|
||||
throw new AwsException("InvalidGlobalClusterStateFault",
|
||||
"Cannot delete protected Global Cluster, please disable deletion protection and try again.", 400);
|
||||
}
|
||||
if (!global.getMembers().isEmpty()) {
|
||||
throw new AwsException("InvalidGlobalClusterStateFault",
|
||||
"Global cluster " + global.getGlobalClusterIdentifier() + " still has "
|
||||
+ global.getMembers().size() + " DB cluster(s) attached. Remove them first.", 400);
|
||||
}
|
||||
deleteGlobalClusterRecord(accountId, global.getGlobalClusterIdentifier());
|
||||
global.setStatus("deleting");
|
||||
LOG.infov("Global cluster {0} deleted", global.getGlobalClusterIdentifier());
|
||||
return global;
|
||||
}
|
||||
|
||||
/**
|
||||
* Detaches a member: it becomes a standalone cluster with read-write capability. The primary
|
||||
* can only be removed once every secondary has been.
|
||||
*/
|
||||
public synchronized GlobalCluster removeFromGlobalCluster(String id, String dbClusterIdentifier, String region) {
|
||||
String accountId = currentAccountId();
|
||||
GlobalCluster global = requireGlobalCluster(accountId, id);
|
||||
DbCluster cluster = resolveClusterReference(dbClusterIdentifier, region);
|
||||
GlobalClusterMember member = global.findMember(cluster.getDbClusterArn())
|
||||
.orElseThrow(() -> new AwsException("DBClusterNotFoundFault",
|
||||
"DB cluster " + cluster.getDbClusterIdentifier() + " is not a member of global cluster "
|
||||
+ global.getGlobalClusterIdentifier() + ".", 404));
|
||||
if (member.isWriter() && global.getMembers().size() > 1) {
|
||||
throw new AwsException("InvalidGlobalClusterStateFault",
|
||||
"DB cluster " + cluster.getDbClusterIdentifier() + " is the primary cluster of global "
|
||||
+ "cluster " + global.getGlobalClusterIdentifier()
|
||||
+ "; remove all secondary clusters before removing the primary.", 400);
|
||||
}
|
||||
global.getMembers().remove(member);
|
||||
putGlobalCluster(accountId, global);
|
||||
cluster.setGlobalClusterIdentifier(null);
|
||||
putClusterForScope(accountId, regionFromArn(cluster.getDbClusterArn()),
|
||||
cluster.getDbClusterIdentifier(), cluster);
|
||||
return global;
|
||||
}
|
||||
|
||||
/**
|
||||
* Promotes the named secondary to primary and demotes the current primary to a secondary, the
|
||||
* topology AWS keeps for a switchover and restores after a managed failover once the old
|
||||
* primary Region is healthy again, which here it always is.
|
||||
*/
|
||||
public synchronized GlobalCluster failoverGlobalCluster(String id, String targetDbClusterIdentifier,
|
||||
Boolean allowDataLoss, Boolean switchover,
|
||||
String region) {
|
||||
if (Boolean.TRUE.equals(allowDataLoss) && switchover != null) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
"AllowDataLoss and Switchover can't be specified together.", 400);
|
||||
}
|
||||
return promoteGlobalClusterMember(id, targetDbClusterIdentifier, region);
|
||||
}
|
||||
|
||||
public synchronized GlobalCluster switchoverGlobalCluster(String id, String targetDbClusterIdentifier,
|
||||
String region) {
|
||||
return promoteGlobalClusterMember(id, targetDbClusterIdentifier, region);
|
||||
}
|
||||
|
||||
private GlobalCluster promoteGlobalClusterMember(String id, String targetDbClusterIdentifier, String region) {
|
||||
String accountId = currentAccountId();
|
||||
GlobalCluster global = requireGlobalCluster(accountId, id);
|
||||
if (targetDbClusterIdentifier == null || targetDbClusterIdentifier.isBlank()) {
|
||||
throw new AwsException("InvalidParameterValue", "TargetDbClusterIdentifier is required.", 400);
|
||||
}
|
||||
DbCluster target = resolveClusterReference(targetDbClusterIdentifier, region);
|
||||
GlobalClusterMember targetMember = global.findMember(target.getDbClusterArn())
|
||||
.orElseThrow(() -> new AwsException("DBClusterNotFoundFault",
|
||||
"DB cluster " + target.getDbClusterIdentifier() + " is not a member of global cluster "
|
||||
+ global.getGlobalClusterIdentifier() + ".", 404));
|
||||
if (global.getMembers().size() < 2) {
|
||||
throw new AwsException("InvalidGlobalClusterStateFault",
|
||||
"Global cluster " + global.getGlobalClusterIdentifier()
|
||||
+ " has no secondary cluster to promote.", 400);
|
||||
}
|
||||
if (targetMember.isWriter()) {
|
||||
throw new AwsException("InvalidDBClusterStateFault",
|
||||
"DB cluster " + target.getDbClusterIdentifier() + " is already the primary cluster of "
|
||||
+ "global cluster " + global.getGlobalClusterIdentifier() + ".", 400);
|
||||
}
|
||||
for (GlobalClusterMember member : global.getMembers()) {
|
||||
member.setWriter(member == targetMember);
|
||||
}
|
||||
putGlobalCluster(accountId, global);
|
||||
LOG.infov("Global cluster {0}: {1} is now the primary cluster",
|
||||
global.getGlobalClusterIdentifier(), target.getDbClusterIdentifier());
|
||||
return global;
|
||||
}
|
||||
|
||||
/**
|
||||
* Forces a failover inside a DB cluster: the named member, or the first reader when none is
|
||||
* named, becomes the writer. A cluster without a reader has nothing to fail over to.
|
||||
*/
|
||||
public synchronized DbCluster failoverDbCluster(String id, String targetDbInstanceIdentifier, String region) {
|
||||
String effectiveRegion = effectiveRegion(region);
|
||||
DbCluster cluster = getDbCluster(id, effectiveRegion);
|
||||
String writer = cluster.resolveWriterIdentifier();
|
||||
List<String> readers = cluster.getDbClusterMembers().stream()
|
||||
.filter(member -> !member.equalsIgnoreCase(writer))
|
||||
.toList();
|
||||
if (readers.isEmpty()) {
|
||||
throw new AwsException("InvalidDBClusterStateFault",
|
||||
"DB cluster " + id + " has no reader instance to fail over to.", 400);
|
||||
}
|
||||
String target = readers.get(0);
|
||||
if (hasText(targetDbInstanceIdentifier)) {
|
||||
target = cluster.getDbClusterMembers().stream()
|
||||
.filter(member -> member.equalsIgnoreCase(targetDbInstanceIdentifier))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new AwsException("InvalidDBInstanceState",
|
||||
"DB instance " + targetDbInstanceIdentifier + " is not a member of DB cluster "
|
||||
+ id + ".", 400));
|
||||
if (target.equalsIgnoreCase(writer)) {
|
||||
throw new AwsException("InvalidDBInstanceState",
|
||||
"DB instance " + target + " is already the writer of DB cluster " + id + ".", 400);
|
||||
}
|
||||
}
|
||||
cluster.setClusterWriterIdentifier(target);
|
||||
putClusterForScope(currentAccountId(), effectiveRegion, id, cluster);
|
||||
LOG.infov("DB cluster {0}: {1} is now the writer", id, target);
|
||||
return cluster;
|
||||
}
|
||||
|
||||
/**
|
||||
* The membership checks run again here, under the lock, because the cluster's container was
|
||||
* started outside it: two concurrent joins that both passed the early checks would otherwise
|
||||
* both attach, leaving two primaries or two secondaries in one Region. A join that lost the
|
||||
* race fails, and the caller deletes the cluster it created.
|
||||
*/
|
||||
private void attachToGlobalCluster(String accountId, String globalClusterIdentifier,
|
||||
DbCluster cluster, boolean asPrimary) {
|
||||
synchronized (this) {
|
||||
GlobalCluster global = requireGlobalCluster(accountId, globalClusterIdentifier);
|
||||
String region = regionFromArn(cluster.getDbClusterArn());
|
||||
if (asPrimary && global.findPrimary().isPresent()) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
"Global cluster " + global.getGlobalClusterIdentifier()
|
||||
+ " already has a primary cluster.", 400);
|
||||
}
|
||||
if (!asPrimary && global.findPrimary().isEmpty()) {
|
||||
throw new AwsException("InvalidGlobalClusterStateFault",
|
||||
"Global cluster " + global.getGlobalClusterIdentifier()
|
||||
+ " has no primary cluster to replicate from.", 400);
|
||||
}
|
||||
for (GlobalClusterMember member : global.getMembers()) {
|
||||
if (regionFromArn(member.getDbClusterArn()).equals(region)) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
"Global cluster " + global.getGlobalClusterIdentifier()
|
||||
+ " already has a cluster in " + region + ".", 400);
|
||||
}
|
||||
}
|
||||
global.getMembers().add(new GlobalClusterMember(cluster.getDbClusterArn(), asPrimary));
|
||||
putGlobalCluster(accountId, global);
|
||||
cluster.setGlobalClusterIdentifier(global.getGlobalClusterIdentifier());
|
||||
putClusterForScope(accountId, regionFromArn(cluster.getDbClusterArn()),
|
||||
cluster.getDbClusterIdentifier(), cluster);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A member cluster leaves its global cluster when deleted, except the primary while
|
||||
* secondaries remain, which AWS refuses.
|
||||
*/
|
||||
private void detachFromGlobalClusterBeforeDelete(DbCluster cluster) {
|
||||
if (cluster.getGlobalClusterIdentifier() == null) {
|
||||
return;
|
||||
}
|
||||
String accountId = currentAccountId();
|
||||
GlobalCluster global = findGlobalCluster(accountId, cluster.getGlobalClusterIdentifier());
|
||||
if (global == null) {
|
||||
cluster.setGlobalClusterIdentifier(null);
|
||||
return;
|
||||
}
|
||||
GlobalClusterMember member = global.findMember(cluster.getDbClusterArn()).orElse(null);
|
||||
if (member == null) {
|
||||
cluster.setGlobalClusterIdentifier(null);
|
||||
return;
|
||||
}
|
||||
if (member.isWriter() && global.getMembers().size() > 1) {
|
||||
throw new AwsException("InvalidDBClusterStateFault",
|
||||
"DB cluster " + cluster.getDbClusterIdentifier() + " is the primary cluster of global "
|
||||
+ "cluster " + global.getGlobalClusterIdentifier()
|
||||
+ "; remove all secondary clusters before deleting it.", 400);
|
||||
}
|
||||
global.getMembers().remove(member);
|
||||
putGlobalCluster(accountId, global);
|
||||
cluster.setGlobalClusterIdentifier(null);
|
||||
}
|
||||
|
||||
private GlobalCluster requireGlobalCluster(String accountId, String id) {
|
||||
if (id == null || id.isBlank()) {
|
||||
throw new AwsException("InvalidParameterValue", "GlobalClusterIdentifier is required.", 400);
|
||||
}
|
||||
GlobalCluster global = findGlobalCluster(accountId, id);
|
||||
if (global == null) {
|
||||
throw new AwsException("GlobalClusterNotFoundFault",
|
||||
"Global cluster '" + id + "' not found", 404);
|
||||
}
|
||||
return global;
|
||||
}
|
||||
|
||||
private GlobalCluster findGlobalCluster(String accountId, String id) {
|
||||
String key = globalClusterKey(id);
|
||||
if (globalClusters instanceof AccountAwareStorageBackend<GlobalCluster> aware) {
|
||||
return aware.getForAccount(accountId, key).orElse(null);
|
||||
}
|
||||
return globalClusters.get(key)
|
||||
.filter(global -> accountId.equals(accountIdFromArn(global.getGlobalClusterArn())))
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
private void putGlobalCluster(String accountId, GlobalCluster global) {
|
||||
String key = globalClusterKey(global.getGlobalClusterIdentifier());
|
||||
if (globalClusters instanceof AccountAwareStorageBackend<GlobalCluster> aware) {
|
||||
aware.putForAccount(accountId, key, global);
|
||||
} else {
|
||||
globalClusters.put(key, global);
|
||||
}
|
||||
}
|
||||
|
||||
private void deleteGlobalClusterRecord(String accountId, String id) {
|
||||
String key = globalClusterKey(id);
|
||||
if (globalClusters instanceof AccountAwareStorageBackend<GlobalCluster> aware) {
|
||||
aware.deleteForAccount(accountId, key);
|
||||
} else {
|
||||
globalClusters.delete(key);
|
||||
}
|
||||
}
|
||||
|
||||
private static String globalClusterKey(String id) {
|
||||
return "global:" + id.toLowerCase(Locale.ROOT);
|
||||
}
|
||||
|
||||
private static String globalClusterArn(String accountId, String id) {
|
||||
return "arn:aws:rds::" + accountId + ":global-cluster:" + id;
|
||||
}
|
||||
|
||||
private static void requireGlobalClusterEngine(String engine, String clusterId) {
|
||||
String name = engine == null ? "" : engine.toLowerCase(Locale.ROOT);
|
||||
if (!GLOBAL_CLUSTER_ENGINES.contains(name)) {
|
||||
throw new AwsException("InvalidParameterValue",
|
||||
(clusterId != null ? "DB cluster " + clusterId + " runs engine " + name + "; only "
|
||||
: "Engine " + name + " is not valid for a global cluster; only ")
|
||||
+ "aurora-mysql and aurora-postgresql are supported.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static String defaultGlobalEngineVersion(String engine) {
|
||||
return "aurora-mysql".equals(engine) ? "8.0.mysql_aurora.3.05.2" : "16.3";
|
||||
}
|
||||
|
||||
private static boolean sameMajorVersion(String current, String requested) {
|
||||
if (current == null || requested == null) {
|
||||
return true;
|
||||
}
|
||||
return current.split("\\.")[0].equals(requested.split("\\.")[0]);
|
||||
}
|
||||
|
||||
private static boolean hasText(String value) {
|
||||
return value != null && !value.isBlank();
|
||||
}
|
||||
|
||||
/** A DB cluster named by ARN (any Region of the account) or by identifier in the request Region. */
|
||||
private DbCluster resolveClusterReference(String reference, String region) {
|
||||
String accountId = currentAccountId();
|
||||
if (reference == null || reference.isBlank()) {
|
||||
throw new AwsException("InvalidParameterValue", "DBClusterIdentifier is required.", 400);
|
||||
}
|
||||
if (reference.startsWith("arn:")) {
|
||||
DbCluster cluster = findClusterByArn(accountId, reference);
|
||||
if (cluster == null) {
|
||||
throw new AwsException("DBClusterNotFoundFault", "DB cluster " + reference + " not found.", 404);
|
||||
}
|
||||
return cluster;
|
||||
}
|
||||
return getDbCluster(reference, effectiveRegion(region));
|
||||
}
|
||||
|
||||
private DbCluster findClusterByArn(String accountId, String arn) {
|
||||
for (DbCluster candidate : clusters.scan(k -> true)) {
|
||||
if (arn.equalsIgnoreCase(candidate.getDbClusterArn())
|
||||
&& accountId.equals(accountIdFromArn(candidate.getDbClusterArn()))) {
|
||||
return findClusterForScope(accountId, regionFromArn(candidate.getDbClusterArn()),
|
||||
candidate.getDbClusterIdentifier());
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// ── DB Proxies (AWS::RDS::DBProxy) ──────────────────────────────────────────
|
||||
|
||||
/**
|
||||
|
||||
@@ -23,6 +23,11 @@ public class DbCluster {
|
||||
private DbEndpoint readerEndpoint;
|
||||
private boolean iamDatabaseAuthenticationEnabled;
|
||||
private List<String> dbClusterMembers = new ArrayList<>();
|
||||
// The member that holds the writer role. Null on a cluster persisted before the role was
|
||||
// modelled, in which case the first member is the writer, as it always was reported.
|
||||
private String clusterWriterIdentifier;
|
||||
// Set while the cluster is the primary or a secondary of a global cluster.
|
||||
private String globalClusterIdentifier;
|
||||
private String parameterGroupName;
|
||||
private String dbSubnetGroupName;
|
||||
private String vpcId;
|
||||
@@ -115,6 +120,20 @@ public class DbCluster {
|
||||
public List<String> getDbClusterMembers() { return dbClusterMembers; }
|
||||
public void setDbClusterMembers(List<String> dbClusterMembers) { this.dbClusterMembers = dbClusterMembers; }
|
||||
|
||||
public String getClusterWriterIdentifier() { return clusterWriterIdentifier; }
|
||||
public void setClusterWriterIdentifier(String clusterWriterIdentifier) { this.clusterWriterIdentifier = clusterWriterIdentifier; }
|
||||
|
||||
/** The member DescribeDBClusters reports as the writer; null only when there are no members. */
|
||||
public String resolveWriterIdentifier() {
|
||||
if (clusterWriterIdentifier != null && dbClusterMembers.contains(clusterWriterIdentifier)) {
|
||||
return clusterWriterIdentifier;
|
||||
}
|
||||
return dbClusterMembers.isEmpty() ? null : dbClusterMembers.get(0);
|
||||
}
|
||||
|
||||
public String getGlobalClusterIdentifier() { return globalClusterIdentifier; }
|
||||
public void setGlobalClusterIdentifier(String globalClusterIdentifier) { this.globalClusterIdentifier = globalClusterIdentifier; }
|
||||
|
||||
public String getParameterGroupName() { return parameterGroupName; }
|
||||
public void setParameterGroupName(String parameterGroupName) { this.parameterGroupName = parameterGroupName; }
|
||||
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
package io.github.hectorvent.floci.services.rds.model;
|
||||
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* An Aurora global database: an account-wide record (its ARN carries no Region) holding one
|
||||
* primary cluster and any number of secondaries, each in its own Region.
|
||||
*/
|
||||
@RegisterForReflection
|
||||
public class GlobalCluster {
|
||||
|
||||
private String globalClusterIdentifier;
|
||||
private String globalClusterResourceId;
|
||||
private String globalClusterArn;
|
||||
private String status;
|
||||
private String engine;
|
||||
private String engineVersion;
|
||||
private String databaseName;
|
||||
private boolean storageEncrypted;
|
||||
private boolean deletionProtection;
|
||||
private List<GlobalClusterMember> members = new ArrayList<>();
|
||||
private Map<String, String> tags = new LinkedHashMap<>();
|
||||
private Instant createdAt;
|
||||
|
||||
public GlobalCluster() {}
|
||||
|
||||
public String getGlobalClusterIdentifier() { return globalClusterIdentifier; }
|
||||
public void setGlobalClusterIdentifier(String globalClusterIdentifier) { this.globalClusterIdentifier = globalClusterIdentifier; }
|
||||
|
||||
public String getGlobalClusterResourceId() { return globalClusterResourceId; }
|
||||
public void setGlobalClusterResourceId(String globalClusterResourceId) { this.globalClusterResourceId = globalClusterResourceId; }
|
||||
|
||||
public String getGlobalClusterArn() { return globalClusterArn; }
|
||||
public void setGlobalClusterArn(String globalClusterArn) { this.globalClusterArn = globalClusterArn; }
|
||||
|
||||
public String getStatus() { return status; }
|
||||
public void setStatus(String status) { this.status = status; }
|
||||
|
||||
public String getEngine() { return engine; }
|
||||
public void setEngine(String engine) { this.engine = engine; }
|
||||
|
||||
public String getEngineVersion() { return engineVersion; }
|
||||
public void setEngineVersion(String engineVersion) { this.engineVersion = engineVersion; }
|
||||
|
||||
public String getDatabaseName() { return databaseName; }
|
||||
public void setDatabaseName(String databaseName) { this.databaseName = databaseName; }
|
||||
|
||||
public boolean isStorageEncrypted() { return storageEncrypted; }
|
||||
public void setStorageEncrypted(boolean storageEncrypted) { this.storageEncrypted = storageEncrypted; }
|
||||
|
||||
public boolean isDeletionProtection() { return deletionProtection; }
|
||||
public void setDeletionProtection(boolean deletionProtection) { this.deletionProtection = deletionProtection; }
|
||||
|
||||
public List<GlobalClusterMember> getMembers() { return members; }
|
||||
public void setMembers(List<GlobalClusterMember> members) {
|
||||
this.members = members != null ? new ArrayList<>(members) : new ArrayList<>();
|
||||
}
|
||||
|
||||
public Map<String, String> getTags() { return tags; }
|
||||
public void setTags(Map<String, String> tags) {
|
||||
this.tags = tags != null ? new LinkedHashMap<>(tags) : new LinkedHashMap<>();
|
||||
}
|
||||
|
||||
public Instant getCreatedAt() { return createdAt; }
|
||||
public void setCreatedAt(Instant createdAt) { this.createdAt = createdAt; }
|
||||
|
||||
// Helpers, not bean properties: the persisted form must carry only settable properties.
|
||||
|
||||
public Optional<GlobalClusterMember> findPrimary() {
|
||||
return members.stream().filter(GlobalClusterMember::isWriter).findFirst();
|
||||
}
|
||||
|
||||
public Optional<GlobalClusterMember> findMember(String dbClusterArn) {
|
||||
return members.stream()
|
||||
.filter(member -> member.getDbClusterArn().equalsIgnoreCase(dbClusterArn))
|
||||
.findFirst();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package io.github.hectorvent.floci.services.rds.model;
|
||||
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
/** One DB cluster of an Aurora global database: the primary (writer) or a secondary. */
|
||||
@RegisterForReflection
|
||||
public class GlobalClusterMember {
|
||||
|
||||
private String dbClusterArn;
|
||||
private boolean writer;
|
||||
|
||||
public GlobalClusterMember() {}
|
||||
|
||||
public GlobalClusterMember(String dbClusterArn, boolean writer) {
|
||||
this.dbClusterArn = dbClusterArn;
|
||||
this.writer = writer;
|
||||
}
|
||||
|
||||
public String getDbClusterArn() { return dbClusterArn; }
|
||||
public void setDbClusterArn(String dbClusterArn) { this.dbClusterArn = dbClusterArn; }
|
||||
|
||||
public boolean isWriter() { return writer; }
|
||||
public void setWriter(boolean writer) { this.writer = writer; }
|
||||
}
|
||||
Reference in new issue
Block a user