mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
feat(cloudformation): apply DynamoDB GlobalTable Replicas (#4134)
AWS::DynamoDB::GlobalTable was provisioned as a plain table and its Replicas property was ignored. Now the declared replica regions are reconciled onto the table via the same applyReplicaUpdates the UpdateTable ReplicaUpdates path uses. - Create adds every declared replica region; update diffs declared vs tracked, adding new regions and removing dropped ones so an edited Replicas list converges. - The deployment region is served by the table itself and the service rejects a replica there, so it is filtered out rather than tracked.
This commit is contained in:
1 parent
32856bfee0
commit
f9e96f3d22
3 files changed
+111
-2
No files matched your search
+50
-1
@@ -25,7 +25,10 @@ import java.util.Set;
|
||||
* global-table custom resource {@code Custom::DynamoDBReplica}.
|
||||
*
|
||||
* <p>Extracted from {@code CloudFormationResourceProvisioner}. A global table is provisioned as a
|
||||
* plain table: its {@code Replicas} property is not applied. The replica custom resource is the
|
||||
* plain table and then its {@code Replicas} property is reconciled against the tracked replica
|
||||
* regions: declared regions are added and dropped ones removed, so an UpdateStack that changes the
|
||||
* Replicas list converges. The deployment region is served by the table itself and is filtered out
|
||||
* rather than tracked as a replica. The replica custom resource is the
|
||||
* one the CDK legacy global table ({@code dynamodb.Table.replicationRegions}) emits per replica
|
||||
* region. Its provider Lambda only calls UpdateTable with a ReplicaUpdates Create, so that call is
|
||||
* applied directly against the DynamoDB service rather than through the async CDK Provider
|
||||
@@ -222,6 +225,7 @@ public class DynamoDbCfnProvisioner implements CfnResourceProvisioner {
|
||||
// rejects. DescribeTable reports the same value, so Fn::GetAtt and the API agree.
|
||||
if (GLOBAL_TABLE.equals(r.getResourceType())) {
|
||||
r.getAttributes().put("TableId", table.getTableId());
|
||||
reconcileGlobalTableReplicas(tableName, props, ctx);
|
||||
}
|
||||
// Only a live stream has an ARN worth handing to Fn::GetAtt. Publishing one unconditionally
|
||||
// resolved to nothing on a streamless table; publishing the retained ARN of a stream that
|
||||
@@ -235,6 +239,51 @@ public class DynamoDbCfnProvisioner implements CfnResourceProvisioner {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reconciles a global table's tracked replica regions to its declared {@code Replicas} property.
|
||||
* Serves create and update: declared regions absent from the table are added and tracked regions
|
||||
* no longer declared are removed, so an UpdateStack that edits the Replicas list converges rather
|
||||
* than only ever growing. The deployment region is served by the table itself and the service
|
||||
* rejects a replica there (as the UpdateTable ReplicaUpdates API does), so it is filtered out of
|
||||
* the reconcile. On AWS a global table also lists its stack region as an ACTIVE replica; surfacing
|
||||
* that belongs in DescribeTable (it is a pre-existing gap on the {@code Custom::DynamoDBReplica}
|
||||
* path too), not here.
|
||||
*/
|
||||
private void reconcileGlobalTableReplicas(String tableName, JsonNode props, ProvisionContext ctx) {
|
||||
CloudFormationTemplateEngine engine = ctx.engine();
|
||||
String localRegion = ctx.region();
|
||||
List<String> declared = new ArrayList<>();
|
||||
JsonNode replicas = props != null ? engine.resolveNode(props.get("Replicas")) : null;
|
||||
if (replicas != null && replicas.isArray()) {
|
||||
for (JsonNode replica : replicas) {
|
||||
String replicaRegion = engine.resolve(replica.path("Region"));
|
||||
if (replicaRegion != null && !replicaRegion.isBlank()
|
||||
&& !replicaRegion.equals(localRegion) && !declared.contains(replicaRegion)) {
|
||||
declared.add(replicaRegion);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TableDefinition table = dynamoDbService.describeTable(tableName, localRegion);
|
||||
List<String> existing = table.getReplicaRegions();
|
||||
List<String> toAdd = new ArrayList<>();
|
||||
for (String replicaRegion : declared) {
|
||||
if (!existing.contains(replicaRegion)) {
|
||||
toAdd.add(replicaRegion);
|
||||
}
|
||||
}
|
||||
List<String> toRemove = new ArrayList<>();
|
||||
for (String replicaRegion : existing) {
|
||||
if (!declared.contains(replicaRegion)) {
|
||||
toRemove.add(replicaRegion);
|
||||
}
|
||||
}
|
||||
if (toAdd.isEmpty() && toRemove.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
dynamoDbService.applyReplicaUpdates(tableName, toAdd, toRemove, localRegion);
|
||||
}
|
||||
|
||||
private void provisionReplica(StackResource r, JsonNode props, ProvisionContext ctx) {
|
||||
String tableName = ctx.resolveOptional(props, "TableName");
|
||||
String replicaRegion = ctx.resolveOptional(props, "Region");
|
||||
|
||||
+11
-1
@@ -8,6 +8,7 @@ import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.awaitility.Awaitility.await;
|
||||
@@ -58,7 +59,7 @@ class CloudFormationDynamoDbGlobalTableIntegrationTest {
|
||||
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
|
||||
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
|
||||
"BillingMode": "PAY_PER_REQUEST",
|
||||
"Replicas": [{"Region": "us-east-1"}]
|
||||
"Replicas": [{"Region": "us-east-1"}, {"Region": "us-west-2"}]
|
||||
}
|
||||
},
|
||||
"RefParam": {
|
||||
@@ -88,6 +89,15 @@ class CloudFormationDynamoDbGlobalTableIntegrationTest {
|
||||
assertTrue(tableArn != null && tableArn.startsWith("arn:aws:dynamodb:"), "TableArn: " + tableArn);
|
||||
assertFalse(tableId == null || tableId.isBlank(), "DescribeTable reported no TableId");
|
||||
|
||||
// The Replicas property is applied: the non-local region is tracked and surfaced by
|
||||
// DescribeTable. Whether the deployment region itself appears in Replicas is deliberately not
|
||||
// asserted here: on AWS a global table lists its stack region as a replica too, and reporting
|
||||
// that is a DescribeTable follow-up (the reconcile must keep filtering the local region, since
|
||||
// the UpdateTable ReplicaUpdates API rejects adding it).
|
||||
List<String> replicaRegions = described.getList("Table.Replicas.RegionName");
|
||||
assertTrue(replicaRegions != null && replicaRegions.contains("us-west-2"),
|
||||
"DescribeTable Replicas did not report us-west-2: " + replicaRegions);
|
||||
|
||||
assertEquals(tableName, parameterValue("/gt-v2/" + suffix + "/ref"));
|
||||
assertEquals(tableArn, parameterValue("/gt-v2/" + suffix + "/arn"));
|
||||
assertEquals(tableId, parameterValue("/gt-v2/" + suffix + "/table-id"));
|
||||
|
||||
+50
@@ -9,6 +9,7 @@ import io.github.hectorvent.floci.services.dynamodb.DynamoDbService;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.TableDefinition;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -110,6 +111,7 @@ class DynamoDbCfnProvisionerTest {
|
||||
void globalTableIsProvisionedAsATableAndPublishesTableId() throws Exception {
|
||||
when(dynamoDb.createTable(anyString(), anyList(), anyList(), any(), any(), anyList(), anyList(), anyString()))
|
||||
.thenReturn(table(false));
|
||||
when(dynamoDb.describeTable(TABLE_NAME, "us-east-1")).thenReturn(table(false));
|
||||
StackResource r = resource("AWS::DynamoDB::GlobalTable", "Orders");
|
||||
|
||||
provisioner.provision(r, props("{\"TableName\":\"orders\"}"), ctx());
|
||||
@@ -119,6 +121,54 @@ class DynamoDbCfnProvisionerTest {
|
||||
assertEquals(TABLE_NAME, r.getPhysicalId());
|
||||
assertEquals(Set.of("Arn", "TableId"), r.getAttributes().keySet());
|
||||
assertEquals(TABLE_ID, r.getAttributes().get("TableId"));
|
||||
// No Replicas declared and none tracked, so nothing to reconcile.
|
||||
verify(dynamoDb, never()).applyReplicaUpdates(anyString(), anyList(), anyList(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void globalTableCreateAddsDeclaredReplicaRegionsFilteringTheDeploymentRegion() throws Exception {
|
||||
when(dynamoDb.createTable(anyString(), anyList(), anyList(), any(), any(), anyList(), anyList(), anyString()))
|
||||
.thenReturn(table(false));
|
||||
when(dynamoDb.describeTable(TABLE_NAME, "us-east-1")).thenReturn(table(false));
|
||||
StackResource r = resource("AWS::DynamoDB::GlobalTable", "Orders");
|
||||
|
||||
provisioner.provision(r, props("{\"TableName\":\"orders\",\"Replicas\":["
|
||||
+ "{\"Region\":\"us-east-1\"},{\"Region\":\"us-west-2\"},{\"Region\":\"eu-west-1\"}]}"), ctx());
|
||||
|
||||
// us-east-1 is the deployment region served by the table itself, so it is filtered out.
|
||||
verify(dynamoDb).applyReplicaUpdates(TABLE_NAME, List.of("us-west-2", "eu-west-1"), List.of(), "us-east-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void globalTableUpdateAddsNewReplicasAndRemovesDroppedOnes() throws Exception {
|
||||
when(dynamoDb.createTable(anyString(), anyList(), anyList(), any(), any(), anyList(), anyList(), anyString()))
|
||||
.thenReturn(table(false));
|
||||
TableDefinition existing = table(false);
|
||||
existing.setReplicaRegions(new ArrayList<>(List.of("us-west-2", "eu-west-1")));
|
||||
when(dynamoDb.describeTable(TABLE_NAME, "us-east-1")).thenReturn(existing);
|
||||
StackResource r = resource("AWS::DynamoDB::GlobalTable", "Orders");
|
||||
r.setPhysicalId(TABLE_NAME);
|
||||
|
||||
provisioner.provision(r, props("{\"TableName\":\"orders\",\"Replicas\":["
|
||||
+ "{\"Region\":\"us-west-2\"},{\"Region\":\"ap-south-1\"}]}"), updateCtx(TABLE_NAME));
|
||||
|
||||
verify(dynamoDb).applyReplicaUpdates(TABLE_NAME, List.of("ap-south-1"), List.of("eu-west-1"), "us-east-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void globalTableWithUnchangedReplicasSkipsTheReplicaUpdate() throws Exception {
|
||||
when(dynamoDb.createTable(anyString(), anyList(), anyList(), any(), any(), anyList(), anyList(), anyString()))
|
||||
.thenReturn(table(false));
|
||||
TableDefinition existing = table(false);
|
||||
existing.setReplicaRegions(new ArrayList<>(List.of("us-west-2")));
|
||||
when(dynamoDb.describeTable(TABLE_NAME, "us-east-1")).thenReturn(existing);
|
||||
StackResource r = resource("AWS::DynamoDB::GlobalTable", "Orders");
|
||||
r.setPhysicalId(TABLE_NAME);
|
||||
|
||||
provisioner.provision(r, props("{\"TableName\":\"orders\",\"Replicas\":[{\"Region\":\"us-west-2\"}]}"),
|
||||
updateCtx(TABLE_NAME));
|
||||
|
||||
verify(dynamoDb, never()).applyReplicaUpdates(anyString(), anyList(), anyList(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user