diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/DynamoDbCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/DynamoDbCfnProvisioner.java index f9f1c3d30a..8ae05a236f 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/DynamoDbCfnProvisioner.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/DynamoDbCfnProvisioner.java @@ -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 existing = table.getReplicaRegions(); List toAdd = new ArrayList<>(); for (String replicaRegion : declared) { diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java index d75a17d47e..5a1d6fdfa6 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java @@ -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 replicaRegions = table.getReplicaRegions(); - if (replicaRegions != null && !replicaRegions.isEmpty()) { + String homeRegion = table.getGlobalTableHomeRegion(); + LinkedHashSet 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"); diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java index a9dc06ac27..a8693b8c9f 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java @@ -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 addRegions, List removeRegions, List updateRegions, String region) { validateReplicaUpdatesAndGetTable(tableName, addRegions, removeRegions, updateRegions, region); diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/model/TableDefinition.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/model/TableDefinition.java index 981ab2f509..ba21da8284 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/model/TableDefinition.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/model/TableDefinition.java @@ -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 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; } diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationDynamoDbGlobalTableIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationDynamoDbGlobalTableIntegrationTest.java index 455ee618d8..599e22a90b 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationDynamoDbGlobalTableIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationDynamoDbGlobalTableIntegrationTest.java @@ -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 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")); diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java index 5aa9255710..d2613c30dd 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java @@ -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");