mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(dynamodb): report a global table's home region as a replica in DescribeTable (#4197)
On AWS a global table (version 2019.11.21) has no primary: the region it was created in is one of its replicas, and DescribeTable lists it under Replicas as ACTIVE next to the others. The CDK's TableV2 calls that table the default replica and refuses a declared replica in the primary table's region (aws-dynamodb/lib/table-v2.ts:280,394), which is why the CloudFormation provisioner filters the deployment region out of its ReplicaUpdates reconcile. Floci only knew the other regions, so a single-region AWS::DynamoDB::GlobalTable described with no replicas and a two-region one listed only the second. TableDefinition.globalTableHomeRegion records the creating region once a replica is added (UpdateTable ReplicaUpdates, Custom::DynamoDBReplica) or when the GlobalTable provisioner calls the new idempotent ensureGlobalTable, and DescribeTable appends it as an ACTIVE replica after the tracked ones with GlobalTableVersion. A plain table has no home region and reports neither.
This commit is contained in:
1 parent
a459dedd43
commit
d96e41cf2f
6 files changed
+76
-11
No files matched your search
+6
-3
@@ -245,9 +245,8 @@ public class DynamoDbCfnProvisioner implements CfnResourceProvisioner {
|
||||
* 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.
|
||||
* the reconcile. The table is still marked a global table homed in that region, so DescribeTable
|
||||
* lists the deployment region as an ACTIVE replica alongside the others, as AWS does.
|
||||
*/
|
||||
private void reconcileGlobalTableReplicas(String tableName, JsonNode props, ProvisionContext ctx) {
|
||||
CloudFormationTemplateEngine engine = ctx.engine();
|
||||
@@ -265,6 +264,10 @@ public class DynamoDbCfnProvisioner implements CfnResourceProvisioner {
|
||||
}
|
||||
|
||||
TableDefinition table = dynamoDbService.describeTable(tableName, localRegion);
|
||||
// This resource is a global table, so mark it homed in the deployment region even when it
|
||||
// declares no other replica: DescribeTable then lists the home region as an ACTIVE replica,
|
||||
// the single-region global table AWS reports (and CDK TableV2 emits by default).
|
||||
dynamoDbService.ensureGlobalTable(tableName, localRegion);
|
||||
List<String> existing = table.getReplicaRegions();
|
||||
List<String> toAdd = new ArrayList<>();
|
||||
for (String replicaRegion : declared) {
|
||||
|
||||
+10
-3
@@ -2448,11 +2448,18 @@ public class DynamoDbJsonHandler {
|
||||
|
||||
// Global-table replicas. In a single-process emulator every region is served by this same
|
||||
// table, so a replica is metadata; AWS reports each as an ACTIVE Replica and marks the table
|
||||
// a 2019.11.21 global table.
|
||||
// a 2019.11.21 global table. A global table also lists its own home region as a replica, so it
|
||||
// is appended (once) after the tracked ones; a plain table (no replicas, no home) has neither.
|
||||
List<String> replicaRegions = table.getReplicaRegions();
|
||||
if (replicaRegions != null && !replicaRegions.isEmpty()) {
|
||||
String homeRegion = table.getGlobalTableHomeRegion();
|
||||
LinkedHashSet<String> regions = new LinkedHashSet<>(
|
||||
replicaRegions != null ? replicaRegions : List.of());
|
||||
if (homeRegion != null && !homeRegion.isBlank()) {
|
||||
regions.add(homeRegion);
|
||||
}
|
||||
if (!regions.isEmpty()) {
|
||||
ArrayNode replicas = objectMapper.createArrayNode();
|
||||
for (String replicaRegion : replicaRegions) {
|
||||
for (String replicaRegion : regions) {
|
||||
ObjectNode replica = objectMapper.createObjectNode();
|
||||
replica.put("RegionName", replicaRegion);
|
||||
replica.put("ReplicaStatus", "ACTIVE");
|
||||
|
||||
@@ -2076,12 +2076,35 @@ public class DynamoDbService implements ResourceProvider {
|
||||
}
|
||||
}
|
||||
table.setReplicaRegions(replicas);
|
||||
// Adding a replica turns the table into a global table, whose home region is the one the
|
||||
// update runs in. Once set it stays, matching AWS keeping the table a global table. This is
|
||||
// what lets DescribeTable list the home region as an ACTIVE replica alongside the others.
|
||||
if (table.getGlobalTableHomeRegion() == null && !replicas.isEmpty()) {
|
||||
table.setGlobalTableHomeRegion(region);
|
||||
}
|
||||
String canonicalTableName = canonicalTableName(region, tableName);
|
||||
tableStore.put(regionKey(region, canonicalTableName), table);
|
||||
LOG.infov("Updated replicas for table {0} in region {1}: {2}", canonicalTableName, region, replicas);
|
||||
return table;
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks a table as a global table homed in {@code region} even when it has no other replica yet,
|
||||
* so DescribeTable lists its home region as an ACTIVE replica. Used for a single-region
|
||||
* {@code AWS::DynamoDB::GlobalTable}, which AWS still reports as a global table. Idempotent.
|
||||
*/
|
||||
public TableDefinition ensureGlobalTable(String tableName, String region) {
|
||||
String canonicalTableName = canonicalTableName(region, tableName);
|
||||
String storageKey = regionKey(region, canonicalTableName);
|
||||
TableDefinition table = tableStore.get(storageKey)
|
||||
.orElseThrow(() -> resourceNotFoundException(canonicalTableName));
|
||||
if (table.getGlobalTableHomeRegion() == null) {
|
||||
table.setGlobalTableHomeRegion(region);
|
||||
tableStore.put(storageKey, table);
|
||||
}
|
||||
return table;
|
||||
}
|
||||
|
||||
public void validateReplicaUpdates(String tableName, List<String> addRegions,
|
||||
List<String> removeRegions, List<String> updateRegions, String region) {
|
||||
validateReplicaUpdatesAndGetTable(tableName, addRegions, removeRegions, updateRegions, region);
|
||||
|
||||
@@ -51,6 +51,7 @@ public class TableDefinition {
|
||||
// Replica regions for a global table (single-process emulator backs them all with this table's
|
||||
// data; the list drives the DescribeTable Replicas/GlobalTableVersion projection).
|
||||
private List<String> replicaRegions;
|
||||
private String globalTableHomeRegion;
|
||||
// Resource-based policy attached via PutResourcePolicy (JSON policy document text), and the
|
||||
// opaque revision id AWS hands back so callers can pass ExpectedRevisionId for optimistic
|
||||
// concurrency on subsequent Put/DeleteResourcePolicy calls. Null when no policy is attached.
|
||||
@@ -206,6 +207,13 @@ public class TableDefinition {
|
||||
this.replicaRegions = replicaRegions != null ? replicaRegions : new ArrayList<>();
|
||||
}
|
||||
|
||||
// The region that owns the table once it has become a global table. On AWS a global table lists
|
||||
// its own home region as an ACTIVE replica alongside the others; null means a plain table.
|
||||
public String getGlobalTableHomeRegion() { return globalTableHomeRegion; }
|
||||
public void setGlobalTableHomeRegion(String globalTableHomeRegion) {
|
||||
this.globalTableHomeRegion = globalTableHomeRegion;
|
||||
}
|
||||
|
||||
public Integer getOnDemandMaxReadRequestUnits() { return onDemandMaxReadRequestUnits; }
|
||||
public void setOnDemandMaxReadRequestUnits(Integer v) { this.onDemandMaxReadRequestUnits = v; }
|
||||
|
||||
|
||||
+5
-5
@@ -89,14 +89,14 @@ 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).
|
||||
// The Replicas property is applied: the declared non-local region is tracked, and the
|
||||
// deployment region is reported as a replica too, so DescribeTable lists both, as AWS does for
|
||||
// a global table.
|
||||
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);
|
||||
assertTrue(replicaRegions.contains("us-east-1"),
|
||||
"DescribeTable Replicas did not report the deployment region us-east-1: " + replicaRegions);
|
||||
|
||||
assertEquals(tableName, parameterValue("/gt-v2/" + suffix + "/ref"));
|
||||
assertEquals(tableArn, parameterValue("/gt-v2/" + suffix + "/arn"));
|
||||
|
||||
@@ -435,6 +435,30 @@ class DynamoDbServiceTest {
|
||||
assertTrue(service.describeTable("Users", "us-east-1").getReplicaRegions().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void addingAReplicaMarksTheTableGlobalHomedInTheUpdateRegion() {
|
||||
createUsersTable("us-east-1");
|
||||
assertNull(service.describeTable("Users", "us-east-1").getGlobalTableHomeRegion());
|
||||
|
||||
service.applyReplicaUpdates("Users", List.of("eu-west-1"), List.of(), "us-east-1");
|
||||
|
||||
assertEquals("us-east-1", service.describeTable("Users", "us-east-1").getGlobalTableHomeRegion());
|
||||
// The home region stays a global table even after the last replica is removed.
|
||||
service.applyReplicaUpdates("Users", List.of(), List.of("eu-west-1"), "us-east-1");
|
||||
assertEquals("us-east-1", service.describeTable("Users", "us-east-1").getGlobalTableHomeRegion());
|
||||
}
|
||||
|
||||
@Test
|
||||
void ensureGlobalTableMarksTheHomeRegionAndIsIdempotent() {
|
||||
createUsersTable("us-east-1");
|
||||
|
||||
service.ensureGlobalTable("Users", "us-east-1");
|
||||
service.ensureGlobalTable("Users", "us-east-1");
|
||||
|
||||
assertEquals("us-east-1", service.describeTable("Users", "us-east-1").getGlobalTableHomeRegion());
|
||||
assertTrue(service.describeTable("Users", "us-east-1").getReplicaRegions().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidReplicaUpdateLeavesExistingReplicasUnchanged() {
|
||||
createUsersTable("us-east-1");
|
||||
|
||||
Reference in new issue
Block a user