mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -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
+290
@@ -0,0 +1,290 @@
|
||||
package io.github.hectorvent.floci.services.rds;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static io.restassured.http.ContentType.URLENC;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
|
||||
/**
|
||||
* The Aurora global database lifecycle over the Query protocol: an empty global cluster, a
|
||||
* primary and a secondary joined through CreateDBCluster, describe, switchover and failover,
|
||||
* removal in the documented order, and FailoverDBCluster inside a cluster.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class RdsGlobalClusterIntegrationTest {
|
||||
|
||||
private static final String GLOBAL = "gdb-test";
|
||||
private static final String PRIMARY = "gdb-test-primary";
|
||||
private static final String SECONDARY = "gdb-test-secondary";
|
||||
private static final String PRIMARY_ARN = "arn:aws:rds:us-east-1:000000000000:cluster:" + PRIMARY;
|
||||
private static final String SECONDARY_ARN = "arn:aws:rds:eu-west-1:000000000000:cluster:" + SECONDARY;
|
||||
|
||||
private static RequestSpecification query(String action, String region) {
|
||||
return given().header("Authorization",
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260615/" + region + "/rds/aws4_request, "
|
||||
+ "SignedHeaders=content-type;host, Signature=test")
|
||||
.contentType(URLENC)
|
||||
.formParam("Action", action)
|
||||
.formParam("Version", "2014-10-31");
|
||||
}
|
||||
|
||||
private static RequestSpecification query(String action) {
|
||||
return query(action, "us-east-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createGlobalClusterThenPrimaryThenSecondary() {
|
||||
query("CreateGlobalCluster")
|
||||
.formParam("GlobalClusterIdentifier", GLOBAL)
|
||||
.formParam("Engine", "aurora-postgresql")
|
||||
.formParam("EngineVersion", "16.3")
|
||||
.formParam("DatabaseName", "appdb")
|
||||
.formParam("Tags.Tag.1.Key", "team")
|
||||
.formParam("Tags.Tag.1.Value", "data")
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<CreateGlobalClusterResult>"))
|
||||
.body(containsString("<GlobalClusterIdentifier>" + GLOBAL + "</GlobalClusterIdentifier>"))
|
||||
.body(containsString("<GlobalClusterArn>arn:aws:rds::000000000000:global-cluster:" + GLOBAL + "</GlobalClusterArn>"))
|
||||
.body(containsString("<Status>available</Status>"))
|
||||
.body(containsString("<Engine>aurora-postgresql</Engine>"))
|
||||
.body(containsString("<GlobalClusterMembers></GlobalClusterMembers>"))
|
||||
.body(containsString("<Value>data</Value>"));
|
||||
|
||||
query("CreateGlobalCluster")
|
||||
.formParam("GlobalClusterIdentifier", GLOBAL)
|
||||
.formParam("Engine", "aurora-postgresql")
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body(containsString("<Code>GlobalClusterAlreadyExistsFault</Code>"));
|
||||
|
||||
query("CreateDBCluster")
|
||||
.formParam("DBClusterIdentifier", PRIMARY)
|
||||
.formParam("Engine", "aurora-postgresql")
|
||||
.formParam("EngineVersion", "16.3")
|
||||
.formParam("MasterUsername", "admin")
|
||||
.formParam("MasterUserPassword", "password123")
|
||||
.formParam("GlobalClusterIdentifier", GLOBAL)
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<GlobalClusterIdentifier>" + GLOBAL + "</GlobalClusterIdentifier>"))
|
||||
.body(containsString("<DatabaseName>appdb</DatabaseName>"));
|
||||
|
||||
query("CreateDBCluster", "eu-west-1")
|
||||
.formParam("DBClusterIdentifier", SECONDARY)
|
||||
.formParam("Engine", "aurora-postgresql")
|
||||
.formParam("EngineVersion", "16.3")
|
||||
.formParam("GlobalClusterIdentifier", GLOBAL)
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<DBClusterArn>" + SECONDARY_ARN + "</DBClusterArn>"))
|
||||
.body(containsString("<MasterUsername>admin</MasterUsername>"))
|
||||
.body(containsString("<GlobalClusterIdentifier>" + GLOBAL + "</GlobalClusterIdentifier>"));
|
||||
|
||||
query("DescribeGlobalClusters")
|
||||
.formParam("GlobalClusterIdentifier", GLOBAL)
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<GlobalClusters><GlobalClusterMember>"))
|
||||
.body(containsString("<DBClusterArn>" + PRIMARY_ARN + "</DBClusterArn><Readers><member>"
|
||||
+ SECONDARY_ARN + "</member></Readers><IsWriter>true</IsWriter>"))
|
||||
.body(containsString("<DBClusterArn>" + SECONDARY_ARN + "</DBClusterArn><Readers></Readers><IsWriter>false</IsWriter>"))
|
||||
.body(containsString("<SynchronizationStatus>connected</SynchronizationStatus>"));
|
||||
|
||||
// A secondary may not sit in the primary's Region nor bring its own credentials.
|
||||
query("CreateDBCluster")
|
||||
.formParam("DBClusterIdentifier", "gdb-test-third")
|
||||
.formParam("Engine", "aurora-postgresql")
|
||||
.formParam("GlobalClusterIdentifier", GLOBAL)
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body(containsString("<Code>InvalidParameterCombination</Code>"));
|
||||
query("CreateDBCluster", "ap-south-1")
|
||||
.formParam("DBClusterIdentifier", "gdb-test-third")
|
||||
.formParam("Engine", "aurora-postgresql")
|
||||
.formParam("MasterUsername", "admin")
|
||||
.formParam("MasterUserPassword", "password123")
|
||||
.formParam("GlobalClusterIdentifier", GLOBAL)
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body(containsString("<Code>InvalidParameterCombination</Code>"))
|
||||
.body(containsString("Cannot specify user name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void switchoverAndFailoverMoveThePrimaryRole() {
|
||||
query("SwitchoverGlobalCluster")
|
||||
.formParam("GlobalClusterIdentifier", GLOBAL)
|
||||
.formParam("TargetDbClusterIdentifier", SECONDARY_ARN)
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<SwitchoverGlobalClusterResult>"))
|
||||
.body(containsString("<DBClusterArn>" + SECONDARY_ARN + "</DBClusterArn><Readers><member>"
|
||||
+ PRIMARY_ARN + "</member></Readers><IsWriter>true</IsWriter>"));
|
||||
|
||||
query("SwitchoverGlobalCluster")
|
||||
.formParam("GlobalClusterIdentifier", GLOBAL)
|
||||
.formParam("TargetDbClusterIdentifier", SECONDARY_ARN)
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body(containsString("<Code>InvalidDBClusterStateFault</Code>"));
|
||||
|
||||
query("FailoverGlobalCluster", "us-east-1")
|
||||
.formParam("GlobalClusterIdentifier", GLOBAL)
|
||||
.formParam("TargetDbClusterIdentifier", PRIMARY_ARN)
|
||||
.formParam("AllowDataLoss", "true")
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<FailoverGlobalClusterResult>"))
|
||||
.body(containsString("<DBClusterArn>" + PRIMARY_ARN + "</DBClusterArn><Readers><member>"
|
||||
+ SECONDARY_ARN + "</member></Readers><IsWriter>true</IsWriter>"));
|
||||
|
||||
query("FailoverGlobalCluster")
|
||||
.formParam("GlobalClusterIdentifier", GLOBAL)
|
||||
.formParam("TargetDbClusterIdentifier", SECONDARY_ARN)
|
||||
.formParam("AllowDataLoss", "true")
|
||||
.formParam("Switchover", "true")
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body(containsString("<Code>InvalidParameterCombination</Code>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void modifyRenamesAndProtects() {
|
||||
query("ModifyGlobalCluster")
|
||||
.formParam("GlobalClusterIdentifier", GLOBAL)
|
||||
.formParam("DeletionProtection", "true")
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<ModifyGlobalClusterResult>"))
|
||||
.body(containsString("<DeletionProtection>true</DeletionProtection>"));
|
||||
|
||||
query("DescribeDBClusters")
|
||||
.formParam("DBClusterIdentifier", PRIMARY)
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<GlobalClusterIdentifier>" + GLOBAL + "</GlobalClusterIdentifier>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
void removalFollowsTheDocumentedOrderAndDeletionNeedsAnEmptyUnprotectedGlobalCluster() {
|
||||
query("RemoveFromGlobalCluster")
|
||||
.formParam("GlobalClusterIdentifier", GLOBAL)
|
||||
.formParam("DbClusterIdentifier", PRIMARY_ARN)
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body(containsString("<Code>InvalidGlobalClusterStateFault</Code>"));
|
||||
|
||||
query("DeleteGlobalCluster")
|
||||
.formParam("GlobalClusterIdentifier", GLOBAL)
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body(containsString("<Code>InvalidGlobalClusterStateFault</Code>"));
|
||||
|
||||
query("RemoveFromGlobalCluster", "eu-west-1")
|
||||
.formParam("GlobalClusterIdentifier", GLOBAL)
|
||||
.formParam("DbClusterIdentifier", SECONDARY_ARN)
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<RemoveFromGlobalClusterResult>"))
|
||||
.body(not(containsString(SECONDARY_ARN)));
|
||||
query("DescribeDBClusters", "eu-west-1")
|
||||
.formParam("DBClusterIdentifier", SECONDARY)
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(not(containsString("<GlobalClusterIdentifier>")));
|
||||
|
||||
query("RemoveFromGlobalCluster")
|
||||
.formParam("GlobalClusterIdentifier", GLOBAL)
|
||||
.formParam("DbClusterIdentifier", PRIMARY_ARN)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body(containsString("<GlobalClusterMembers></GlobalClusterMembers>"));
|
||||
|
||||
query("DeleteGlobalCluster")
|
||||
.formParam("GlobalClusterIdentifier", GLOBAL)
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body(containsString("<Code>InvalidGlobalClusterStateFault</Code>"))
|
||||
.body(containsString("deletion protection"));
|
||||
query("ModifyGlobalCluster")
|
||||
.formParam("GlobalClusterIdentifier", GLOBAL)
|
||||
.formParam("DeletionProtection", "false")
|
||||
.when().post("/").then().statusCode(200);
|
||||
query("DeleteGlobalCluster")
|
||||
.formParam("GlobalClusterIdentifier", GLOBAL)
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<DeleteGlobalClusterResult>"))
|
||||
.body(containsString("<Status>deleting</Status>"));
|
||||
query("DescribeGlobalClusters")
|
||||
.formParam("GlobalClusterIdentifier", GLOBAL)
|
||||
.when().post("/").then().statusCode(404)
|
||||
.body(containsString("<Code>GlobalClusterNotFoundFault</Code>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
void failoverDbClusterPromotesAReader() {
|
||||
query("CreateDBInstance")
|
||||
.formParam("DBInstanceIdentifier", "gdb-test-writer")
|
||||
.formParam("DBClusterIdentifier", PRIMARY)
|
||||
.formParam("Engine", "aurora-postgresql")
|
||||
.formParam("DBInstanceClass", "db.r6g.large")
|
||||
.when().post("/").then().statusCode(200);
|
||||
|
||||
query("FailoverDBCluster")
|
||||
.formParam("DBClusterIdentifier", PRIMARY)
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body(containsString("<Code>InvalidDBClusterStateFault</Code>"));
|
||||
|
||||
query("CreateDBInstance")
|
||||
.formParam("DBInstanceIdentifier", "gdb-test-reader")
|
||||
.formParam("DBClusterIdentifier", PRIMARY)
|
||||
.formParam("Engine", "aurora-postgresql")
|
||||
.formParam("DBInstanceClass", "db.r6g.large")
|
||||
.when().post("/").then().statusCode(200);
|
||||
query("DescribeDBClusters")
|
||||
.formParam("DBClusterIdentifier", PRIMARY)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body(containsString("<DBInstanceIdentifier>gdb-test-writer</DBInstanceIdentifier><IsClusterWriter>true</IsClusterWriter>"))
|
||||
.body(containsString("<DBInstanceIdentifier>gdb-test-reader</DBInstanceIdentifier><IsClusterWriter>false</IsClusterWriter>"));
|
||||
|
||||
query("FailoverDBCluster")
|
||||
.formParam("DBClusterIdentifier", PRIMARY)
|
||||
.formParam("TargetDBInstanceIdentifier", "gdb-test-reader")
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<FailoverDBClusterResult>"))
|
||||
.body(containsString("<DBInstanceIdentifier>gdb-test-reader</DBInstanceIdentifier><IsClusterWriter>true</IsClusterWriter>"))
|
||||
.body(containsString("<DBInstanceIdentifier>gdb-test-writer</DBInstanceIdentifier><IsClusterWriter>false</IsClusterWriter>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void cleanUp() {
|
||||
for (String instance : new String[]{"gdb-test-writer", "gdb-test-reader"}) {
|
||||
query("DeleteDBInstance").formParam("DBInstanceIdentifier", instance)
|
||||
.when().post("/").then().statusCode(200);
|
||||
}
|
||||
query("DeleteDBCluster").formParam("DBClusterIdentifier", PRIMARY)
|
||||
.when().post("/").then().statusCode(200);
|
||||
query("DeleteDBCluster", "eu-west-1").formParam("DBClusterIdentifier", SECONDARY)
|
||||
.when().post("/").then().statusCode(200);
|
||||
}
|
||||
}
|
||||
@@ -1166,6 +1166,8 @@ class RdsQueryHandlerTest {
|
||||
// answers it with GlobalClusterNotFoundFault rather than an empty list.
|
||||
MultivaluedMap<String, String> params = params();
|
||||
params.putSingle("GlobalClusterIdentifier", "no-such-gc");
|
||||
when(service.describeGlobalCluster("no-such-gc")).thenThrow(
|
||||
new AwsException("GlobalClusterNotFoundFault", "Global cluster 'no-such-gc' not found", 404));
|
||||
|
||||
Response response = handler.handle("DescribeGlobalClusters", params);
|
||||
|
||||
@@ -1191,6 +1193,8 @@ class RdsQueryHandlerTest {
|
||||
void describeGlobalClusters_rejectsMaxRecordsOutsideTheAllowedRange() {
|
||||
// A live account rejects this before it looks the identifier up, so an empty model is no
|
||||
// reason to accept a value AWS refuses.
|
||||
when(service.describeGlobalCluster("no-such-gc")).thenThrow(
|
||||
new AwsException("GlobalClusterNotFoundFault", "Global cluster 'no-such-gc' not found", 404));
|
||||
for (String value : new String[]{"5", "101", "abc"}) {
|
||||
MultivaluedMap<String, String> params = params();
|
||||
params.putSingle("MaxRecords", value);
|
||||
@@ -1227,6 +1231,8 @@ class RdsQueryHandlerTest {
|
||||
assertTrue(((String) response.getEntity()).contains("The request token is invalid."));
|
||||
|
||||
params.putSingle("GlobalClusterIdentifier", "no-such-gc");
|
||||
when(service.describeGlobalCluster("no-such-gc")).thenThrow(
|
||||
new AwsException("GlobalClusterNotFoundFault", "Global cluster 'no-such-gc' not found", 404));
|
||||
Response withBoth = handler.handle("DescribeGlobalClusters", params);
|
||||
assertEquals(404, withBoth.getStatus());
|
||||
assertTrue(((String) withBoth.getEntity()).contains("GlobalClusterNotFoundFault"));
|
||||
|
||||
@@ -29,6 +29,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;
|
||||
@@ -6743,4 +6745,277 @@ class RdsServiceTest {
|
||||
rdsService.promoteReadReplicaDbCluster("aurora", null));
|
||||
assertEquals("InvalidDBClusterStateFault", notReplicaCluster.getErrorCode());
|
||||
}
|
||||
|
||||
// ── Global clusters ───────────────────────────────────────────────────────
|
||||
|
||||
private static final String PRIMARY_ARN = "arn:aws:rds:us-east-1:123456789012:cluster:gdb-primary";
|
||||
private static final String SECONDARY_ARN = "arn:aws:rds:eu-west-1:123456789012:cluster:gdb-secondary";
|
||||
|
||||
private DbCluster createGlobalMember(String globalId, String clusterId, String region,
|
||||
String masterUsername, String masterPassword) {
|
||||
return rdsService.createDbClusterInGlobalCluster(globalId, clusterId, "aurora-postgresql", null,
|
||||
masterUsername, masterPassword, null, false, null, null, null, false, region,
|
||||
null, null, null, false, null, null, false);
|
||||
}
|
||||
|
||||
private static List<String> writers(GlobalCluster global) {
|
||||
return global.getMembers().stream().filter(GlobalClusterMember::isWriter)
|
||||
.map(GlobalClusterMember::getDbClusterArn).toList();
|
||||
}
|
||||
|
||||
@Test
|
||||
void globalClusterGrowsFromEmptyToPrimaryAndSecondary() {
|
||||
when(containerManager.tryStart(any(), any(), any(), any(), any(), any(), any(), any(), any()))
|
||||
.thenAnswer(invocation -> new RdsContainerHandle(
|
||||
"cont-" + invocation.getArgument(1), invocation.getArgument(1), "localhost", 5432));
|
||||
GlobalCluster global = rdsService.createGlobalCluster("gdb", null, "aurora-postgresql", null,
|
||||
"appdb", true, null, Map.of("team", "data"), null);
|
||||
assertEquals("gdb", global.getGlobalClusterIdentifier());
|
||||
assertEquals("arn:aws:rds::123456789012:global-cluster:gdb", global.getGlobalClusterArn());
|
||||
assertEquals("available", global.getStatus());
|
||||
assertEquals("aurora-postgresql", global.getEngine());
|
||||
assertEquals("16.3", global.getEngineVersion(), "the engine's default version when none is given");
|
||||
assertTrue(global.isStorageEncrypted());
|
||||
assertTrue(global.getMembers().isEmpty());
|
||||
|
||||
DbCluster primary = createGlobalMember("gdb", "gdb-primary", null, "admin", "password");
|
||||
assertEquals("gdb", primary.getGlobalClusterIdentifier());
|
||||
assertEquals("appdb", primary.getDatabaseName(), "the global cluster's database name");
|
||||
assertTrue(primary.isStorageEncrypted());
|
||||
assertEquals(List.of(PRIMARY_ARN), writers(rdsService.describeGlobalCluster("gdb")));
|
||||
|
||||
when(containerManager.createPostgresSnapshot("cont-gdb-primary", "admin")).thenReturn("DUMP");
|
||||
DbCluster secondary = createGlobalMember("gdb", "gdb-secondary", "eu-west-1", null, null);
|
||||
assertEquals("gdb", secondary.getGlobalClusterIdentifier());
|
||||
assertEquals("admin", secondary.getMasterUsername(), "a secondary takes the primary's credentials");
|
||||
assertEquals("password", secondary.getMasterPassword());
|
||||
assertEquals("appdb", secondary.getDatabaseName());
|
||||
assertEquals(SECONDARY_ARN, secondary.getDbClusterArn());
|
||||
verify(containerManager).restorePostgresSnapshot("cont-gdb-secondary", "admin", "DUMP");
|
||||
global = rdsService.describeGlobalCluster("gdb");
|
||||
assertEquals(2, global.getMembers().size());
|
||||
assertEquals(List.of(PRIMARY_ARN), writers(global));
|
||||
assertEquals(List.of(global), rdsService.listGlobalClusters());
|
||||
|
||||
// What AWS refuses on a secondary: the primary's Region, a Region already used, its own
|
||||
// credentials, a different engine.
|
||||
AwsException sameRegion = assertThrows(AwsException.class, () ->
|
||||
createGlobalMember("gdb", "gdb-third", null, null, null));
|
||||
assertEquals("InvalidParameterCombination", sameRegion.getErrorCode());
|
||||
AwsException usedRegion = assertThrows(AwsException.class, () ->
|
||||
createGlobalMember("gdb", "gdb-third", "eu-west-1", null, null));
|
||||
assertEquals("InvalidParameterCombination", usedRegion.getErrorCode());
|
||||
AwsException credentials = assertThrows(AwsException.class, () ->
|
||||
createGlobalMember("gdb", "gdb-third", "ap-south-1", "admin", "password"));
|
||||
assertEquals("InvalidParameterCombination", credentials.getErrorCode());
|
||||
assertTrue(credentials.getMessage().contains("Cannot specify user name"));
|
||||
AwsException engine = assertThrows(AwsException.class, () ->
|
||||
rdsService.createDbClusterInGlobalCluster("gdb", "gdb-third", "aurora-mysql", null,
|
||||
null, null, null, false, null, null, null, false, "ap-south-1",
|
||||
null, null, null, false, null, null, false));
|
||||
assertEquals("InvalidParameterCombination", engine.getErrorCode());
|
||||
AwsException unknownGlobal = assertThrows(AwsException.class, () ->
|
||||
createGlobalMember("nope", "gdb-third", "ap-south-1", null, null));
|
||||
assertEquals("GlobalClusterNotFoundFault", unknownGlobal.getErrorCode());
|
||||
assertThrows(AwsException.class, () -> rdsService.getDbCluster("gdb-third", "ap-south-1"),
|
||||
"a refused secondary leaves no cluster behind");
|
||||
}
|
||||
|
||||
/**
|
||||
* The membership checks run before the container starts, outside the lock, so two joins can
|
||||
* both pass them. The container start is the interleaving point: while one join's container
|
||||
* is starting, a competing join completes. The late one must be refused when it attaches, and
|
||||
* the cluster it created must be gone.
|
||||
*/
|
||||
@Test
|
||||
void concurrentJoinsCannotProduceTwoPrimariesOrTwoSecondariesInOneRegion() {
|
||||
rdsService.createGlobalCluster("gdb", null, "aurora-postgresql", null, null, null, null, Map.of(), null);
|
||||
when(containerManager.createPostgresSnapshot(any(), eq("admin"))).thenReturn("DUMP");
|
||||
when(containerManager.tryStart(any(), any(), any(), any(), any(), any(), any(), any(), any()))
|
||||
.thenAnswer(invocation -> {
|
||||
String id = invocation.getArgument(1);
|
||||
if ("primary-a".equals(id)) {
|
||||
createGlobalMember("gdb", "primary-b", null, "admin", "password");
|
||||
} else if ("secondary-a".equals(id)) {
|
||||
createGlobalMember("gdb", "secondary-b", "eu-west-1", null, null);
|
||||
}
|
||||
return new RdsContainerHandle("cont-" + id, id, "localhost", 5432);
|
||||
});
|
||||
|
||||
AwsException twoPrimaries = assertThrows(AwsException.class, () ->
|
||||
rdsService.createDbClusterInGlobalCluster("gdb", "primary-a", "aurora-postgresql", null,
|
||||
"admin", "password", null, false, null, null, null, false, "us-west-2",
|
||||
null, null, null, false, null, null, false));
|
||||
assertEquals("InvalidParameterCombination", twoPrimaries.getErrorCode());
|
||||
assertEquals(List.of("arn:aws:rds:us-east-1:123456789012:cluster:primary-b"),
|
||||
writers(rdsService.describeGlobalCluster("gdb")));
|
||||
assertThrows(AwsException.class, () -> rdsService.getDbCluster("primary-a", "us-west-2"),
|
||||
"the losing join's cluster is deleted");
|
||||
|
||||
AwsException twoSecondaries = assertThrows(AwsException.class, () ->
|
||||
createGlobalMember("gdb", "secondary-a", "eu-west-1", null, null));
|
||||
assertEquals("InvalidParameterCombination", twoSecondaries.getErrorCode());
|
||||
assertEquals(2, rdsService.describeGlobalCluster("gdb").getMembers().size());
|
||||
assertEquals("gdb", rdsService.getDbCluster("secondary-b", "eu-west-1").getGlobalClusterIdentifier());
|
||||
assertThrows(AwsException.class, () -> rdsService.getDbCluster("secondary-a", "eu-west-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void globalClusterFromAnExistingClusterTakesItsSettings() {
|
||||
rdsService.createDbCluster("existing", "aurora-mysql", "8.0.mysql_aurora.3.05.2",
|
||||
"admin", "password", "shop", false, null);
|
||||
rdsService.createDbCluster("plain", "postgres", "16.3", "admin", "password", "shop", false, null);
|
||||
|
||||
AwsException withEngine = assertThrows(AwsException.class, () -> rdsService.createGlobalCluster(
|
||||
"gdb", "arn:aws:rds:us-east-1:123456789012:cluster:existing", "aurora-mysql", null,
|
||||
null, null, null, Map.of(), null));
|
||||
assertEquals("InvalidParameterCombination", withEngine.getErrorCode());
|
||||
AwsException notAurora = assertThrows(AwsException.class, () -> rdsService.createGlobalCluster(
|
||||
"gdb", "plain", null, null, null, null, null, Map.of(), null));
|
||||
assertEquals("InvalidParameterValue", notAurora.getErrorCode());
|
||||
AwsException noEngine = assertThrows(AwsException.class, () -> rdsService.createGlobalCluster(
|
||||
"gdb", null, null, null, null, null, null, Map.of(), null));
|
||||
assertEquals("InvalidParameterCombination", noEngine.getErrorCode());
|
||||
|
||||
GlobalCluster global = rdsService.createGlobalCluster("gdb",
|
||||
"arn:aws:rds:us-east-1:123456789012:cluster:existing", null, null, null, null, null,
|
||||
Map.of(), null);
|
||||
assertEquals("aurora-mysql", global.getEngine());
|
||||
assertEquals("8.0.mysql_aurora.3.05.2", global.getEngineVersion());
|
||||
assertEquals("shop", global.getDatabaseName());
|
||||
assertEquals(List.of("arn:aws:rds:us-east-1:123456789012:cluster:existing"), writers(global));
|
||||
assertEquals("gdb", rdsService.getDbCluster("existing").getGlobalClusterIdentifier());
|
||||
|
||||
AwsException duplicate = assertThrows(AwsException.class, () -> rdsService.createGlobalCluster(
|
||||
"gdb", null, "aurora-postgresql", null, null, null, null, Map.of(), null));
|
||||
assertEquals("GlobalClusterAlreadyExistsFault", duplicate.getErrorCode());
|
||||
AwsException alreadyMember = assertThrows(AwsException.class, () -> rdsService.createGlobalCluster(
|
||||
"gdb2", "existing", null, null, null, null, null, Map.of(), null));
|
||||
assertEquals("InvalidDBClusterStateFault", alreadyMember.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void switchoverAndFailoverPromoteTheNamedSecondary() {
|
||||
rdsService.createGlobalCluster("gdb", null, "aurora-postgresql", null, null, null, null, Map.of(), null);
|
||||
createGlobalMember("gdb", "gdb-primary", null, "admin", "password");
|
||||
when(containerManager.createPostgresSnapshot(any(), eq("admin"))).thenReturn("DUMP");
|
||||
|
||||
AwsException noSecondary = assertThrows(AwsException.class, () ->
|
||||
rdsService.switchoverGlobalCluster("gdb", PRIMARY_ARN, null));
|
||||
assertEquals("InvalidGlobalClusterStateFault", noSecondary.getErrorCode());
|
||||
|
||||
createGlobalMember("gdb", "gdb-secondary", "eu-west-1", null, null);
|
||||
GlobalCluster switched = rdsService.switchoverGlobalCluster("gdb", SECONDARY_ARN, null);
|
||||
assertEquals(List.of(SECONDARY_ARN), writers(switched));
|
||||
assertEquals(2, switched.getMembers().size(), "the old primary stays as a secondary");
|
||||
|
||||
AwsException alreadyPrimary = assertThrows(AwsException.class, () ->
|
||||
rdsService.switchoverGlobalCluster("gdb", SECONDARY_ARN, null));
|
||||
assertEquals("InvalidDBClusterStateFault", alreadyPrimary.getErrorCode());
|
||||
AwsException notMember = assertThrows(AwsException.class, () ->
|
||||
rdsService.failoverGlobalCluster("gdb", "arn:aws:rds:us-east-1:123456789012:cluster:nope",
|
||||
true, null, null));
|
||||
assertEquals("DBClusterNotFoundFault", notMember.getErrorCode());
|
||||
AwsException bothFlags = assertThrows(AwsException.class, () ->
|
||||
rdsService.failoverGlobalCluster("gdb", PRIMARY_ARN, true, true, null));
|
||||
assertEquals("InvalidParameterCombination", bothFlags.getErrorCode());
|
||||
|
||||
GlobalCluster failedOver = rdsService.failoverGlobalCluster("gdb", "gdb-primary", true, null, null);
|
||||
assertEquals(List.of(PRIMARY_ARN), writers(failedOver), "a plain identifier resolves in the request Region");
|
||||
}
|
||||
|
||||
@Test
|
||||
void removalAndDeletionFollowTheDocumentedOrder() {
|
||||
rdsService.createGlobalCluster("gdb", null, "aurora-postgresql", null, null, null, true, Map.of(), null);
|
||||
createGlobalMember("gdb", "gdb-primary", null, "admin", "password");
|
||||
when(containerManager.createPostgresSnapshot(any(), eq("admin"))).thenReturn("DUMP");
|
||||
createGlobalMember("gdb", "gdb-secondary", "eu-west-1", null, null);
|
||||
|
||||
AwsException primaryFirst = assertThrows(AwsException.class, () ->
|
||||
rdsService.removeFromGlobalCluster("gdb", PRIMARY_ARN, null));
|
||||
assertEquals("InvalidGlobalClusterStateFault", primaryFirst.getErrorCode());
|
||||
AwsException deletePrimary = assertThrows(AwsException.class, () ->
|
||||
rdsService.deleteDbCluster("gdb-primary"));
|
||||
assertEquals("InvalidDBClusterStateFault", deletePrimary.getErrorCode());
|
||||
AwsException withMembers = assertThrows(AwsException.class, () -> rdsService.deleteGlobalCluster("gdb"));
|
||||
assertEquals("InvalidGlobalClusterStateFault", withMembers.getErrorCode());
|
||||
|
||||
GlobalCluster afterSecondary = rdsService.removeFromGlobalCluster("gdb", SECONDARY_ARN, null);
|
||||
assertEquals(1, afterSecondary.getMembers().size());
|
||||
assertNull(rdsService.getDbCluster("gdb-secondary", "eu-west-1").getGlobalClusterIdentifier(),
|
||||
"a removed cluster is standalone");
|
||||
AwsException notMember = assertThrows(AwsException.class, () ->
|
||||
rdsService.removeFromGlobalCluster("gdb", SECONDARY_ARN, null));
|
||||
assertEquals("DBClusterNotFoundFault", notMember.getErrorCode());
|
||||
|
||||
// Deleting a member cluster detaches it; the primary is last and deletion protection holds.
|
||||
rdsService.deleteDbCluster("gdb-primary");
|
||||
assertTrue(rdsService.describeGlobalCluster("gdb").getMembers().isEmpty());
|
||||
AwsException protectedGlobal = assertThrows(AwsException.class, () -> rdsService.deleteGlobalCluster("gdb"));
|
||||
assertEquals("InvalidGlobalClusterStateFault", protectedGlobal.getErrorCode());
|
||||
assertTrue(protectedGlobal.getMessage().contains("deletion protection"));
|
||||
rdsService.modifyGlobalCluster("gdb", null, false, null, null);
|
||||
assertEquals("deleting", rdsService.deleteGlobalCluster("gdb").getStatus());
|
||||
AwsException gone = assertThrows(AwsException.class, () -> rdsService.describeGlobalCluster("gdb"));
|
||||
assertEquals("GlobalClusterNotFoundFault", gone.getErrorCode());
|
||||
assertTrue(rdsService.listGlobalClusters().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void modifyGlobalClusterRenamesAndUpgradesItsMembers() {
|
||||
rdsService.createGlobalCluster("gdb", null, "aurora-postgresql", "15.4", null, null, null, Map.of(), null);
|
||||
createGlobalMember("gdb", "gdb-primary", null, "admin", "password");
|
||||
|
||||
GlobalCluster renamed = rdsService.modifyGlobalCluster("gdb", "GDB-Renamed", true, null, null);
|
||||
assertEquals("gdb-renamed", renamed.getGlobalClusterIdentifier());
|
||||
assertEquals("arn:aws:rds::123456789012:global-cluster:gdb-renamed", renamed.getGlobalClusterArn());
|
||||
assertTrue(renamed.isDeletionProtection());
|
||||
assertEquals("gdb-renamed", rdsService.getDbCluster("gdb-primary").getGlobalClusterIdentifier());
|
||||
assertThrows(AwsException.class, () -> rdsService.describeGlobalCluster("gdb"));
|
||||
|
||||
AwsException major = assertThrows(AwsException.class, () ->
|
||||
rdsService.modifyGlobalCluster("gdb-renamed", null, null, "16.3", null));
|
||||
assertEquals("InvalidParameterCombination", major.getErrorCode());
|
||||
GlobalCluster upgraded = rdsService.modifyGlobalCluster("gdb-renamed", null, null, "16.3", true);
|
||||
assertEquals("16.3", upgraded.getEngineVersion());
|
||||
assertEquals("16.3", rdsService.getDbCluster("gdb-primary").getEngineVersion());
|
||||
|
||||
rdsService.createGlobalCluster("other", null, "aurora-mysql", null, null, null, null, Map.of(), null);
|
||||
AwsException taken = assertThrows(AwsException.class, () ->
|
||||
rdsService.modifyGlobalCluster("gdb-renamed", "other", null, null, null));
|
||||
assertEquals("GlobalClusterAlreadyExistsFault", taken.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void failoverDbClusterMovesTheWriterRole() {
|
||||
rdsService.createDbCluster("aurora", "aurora-postgresql", "16.3", "admin", "password", "appdb", false, null);
|
||||
AwsException noMembers = assertThrows(AwsException.class, () ->
|
||||
rdsService.failoverDbCluster("aurora", null, null));
|
||||
assertEquals("InvalidDBClusterStateFault", noMembers.getErrorCode());
|
||||
|
||||
rdsService.createDbInstance("writer", "aurora-postgresql", "16.3", null, null, null,
|
||||
"db.r6g.large", 0, false, null, null, "aurora", null, false);
|
||||
assertEquals("writer", rdsService.getDbCluster("aurora").resolveWriterIdentifier());
|
||||
AwsException noReader = assertThrows(AwsException.class, () ->
|
||||
rdsService.failoverDbCluster("aurora", null, null));
|
||||
assertEquals("InvalidDBClusterStateFault", noReader.getErrorCode());
|
||||
|
||||
rdsService.createDbInstance("reader", "aurora-postgresql", "16.3", null, null, null,
|
||||
"db.r6g.large", 0, false, null, null, "aurora", null, false);
|
||||
assertEquals("writer", rdsService.getDbCluster("aurora").resolveWriterIdentifier(),
|
||||
"a second member joins as a reader");
|
||||
assertEquals("reader", rdsService.failoverDbCluster("aurora", null, null).resolveWriterIdentifier());
|
||||
assertEquals("writer", rdsService.failoverDbCluster("aurora", "writer", null).resolveWriterIdentifier());
|
||||
|
||||
AwsException alreadyWriter = assertThrows(AwsException.class, () ->
|
||||
rdsService.failoverDbCluster("aurora", "writer", null));
|
||||
assertEquals("InvalidDBInstanceState", alreadyWriter.getErrorCode());
|
||||
AwsException stranger = assertThrows(AwsException.class, () ->
|
||||
rdsService.failoverDbCluster("aurora", "nope", null));
|
||||
assertEquals("InvalidDBInstanceState", stranger.getErrorCode());
|
||||
|
||||
// Losing the writer promotes the remaining member.
|
||||
rdsService.deleteDbInstance("writer");
|
||||
assertEquals("reader", rdsService.getDbCluster("aurora").resolveWriterIdentifier());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user