mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(dynamodb): global-table v2 replicas (UpdateTable ReplicaUpdates + Custom::DynamoDBReplica) (#1811)
* feat(dynamodb): support global table replicas * fix(dynamodb): validate replica updates before mutation * fix(dynamodb): align replica compatibility behavior * chore: rerun CI after download outage * fix(dynamodb): document replica update semantics --------- Co-authored-by: Alexander Banna <abanna@users.noreply.github.com>
This commit is contained in:
1 parent
d9e4447aeb
commit
a29464abc7
8 files changed
+780
-6
No files matched your search
+83
@@ -142,6 +142,9 @@ public class CloudFormationResourceProvisioner {
|
||||
private static final String LOG_GROUP_NAME_MODE_ATTR = "FlociLogGroupNameMode";
|
||||
private static final String SECRET_TARGET_MANAGED_KEYS_ATTR = "__FlociSecretTargetManagedKeys";
|
||||
private static final String SECRET_TARGET_OWNER_ATTR = "__FlociSecretTargetOwner";
|
||||
private static final String DDB_REPLICA_TABLE_NAME_ATTR = "TableName";
|
||||
private static final String DDB_REPLICA_REGION_ATTR = "__FlociDynamoDbReplicaRegion";
|
||||
private static final String DDB_REPLICA_SKIP_DELETION_ATTR = "__FlociDynamoDbReplicaSkipDeletion";
|
||||
private static final List<String> SECRET_TARGET_CONNECTION_KEYS = List.of(
|
||||
"engine", "host", "port", "dbname", "dbInstanceIdentifier", "dbClusterIdentifier");
|
||||
private static final int LAMBDA_DEFAULT_TIMEOUT_SECONDS = 3;
|
||||
@@ -351,6 +354,7 @@ public class CloudFormationResourceProvisioner {
|
||||
provisionCognitoUserPoolClient(resource, properties, engine, region, accountId, stackName);
|
||||
case "AWS::CloudFormation::CustomResource" ->
|
||||
provisionCustomResource(resource, properties, engine, region, accountId, stackName);
|
||||
case "Custom::DynamoDBReplica" -> provisionDynamoDbReplica(resource, properties, engine, region);
|
||||
case "AWS::ECS::Cluster" -> provisionEcsCluster(resource, properties, engine, region, stackName);
|
||||
case "AWS::ECS::TaskDefinition" -> provisionEcsTaskDefinition(resource, properties, engine, region, stackName);
|
||||
case "AWS::ECS::Service" -> provisionEcsService(resource, properties, engine, region, stackName);
|
||||
@@ -429,6 +433,12 @@ public class CloudFormationResourceProvisioner {
|
||||
*/
|
||||
public void delete(StackResource resource, String region) {
|
||||
String resourceType = resource.getResourceType();
|
||||
// Custom::DynamoDBReplica is applied natively against the DynamoDB service (not via its
|
||||
// provider Lambda), so remove the replica the same way rather than invoking the handler.
|
||||
if ("Custom::DynamoDBReplica".equals(resourceType)) {
|
||||
deleteDynamoDbReplicaSafe(resource, region);
|
||||
return;
|
||||
}
|
||||
boolean custom = "AWS::CloudFormation::CustomResource".equals(resourceType)
|
||||
|| (resourceType != null && resourceType.startsWith("Custom::"));
|
||||
if (custom) {
|
||||
@@ -4307,6 +4317,79 @@ public class CloudFormationResourceProvisioner {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Provisions a {@code Custom::DynamoDBReplica} — the custom resource the CDK legacy global-table
|
||||
* (dynamodb.Table.replicationRegions) emits per replica region. Its provider Lambda simply calls
|
||||
* DynamoDB UpdateTable with a ReplicaUpdates Create, so apply that directly rather than running
|
||||
* the async CDK Provider framework. {@code Ref} (PhysicalResourceId) follows CDK's
|
||||
* {@code <tableName>-<region>} format.
|
||||
*/
|
||||
private void provisionDynamoDbReplica(StackResource r, JsonNode props,
|
||||
CloudFormationTemplateEngine engine, String region) {
|
||||
String tableName = resolveOptional(props, "TableName", engine);
|
||||
String replicaRegion = resolveOptional(props, "Region", engine);
|
||||
if (tableName == null || tableName.isBlank()) {
|
||||
throw new AwsException("ValidationError",
|
||||
"Custom::DynamoDBReplica " + r.getLogicalId() + " is missing TableName", 400);
|
||||
}
|
||||
if (replicaRegion == null || replicaRegion.isBlank()) {
|
||||
throw new AwsException("ValidationError",
|
||||
"Custom::DynamoDBReplica " + r.getLogicalId() + " is missing Region", 400);
|
||||
}
|
||||
String priorTableName = r.getAttributes().get(DDB_REPLICA_TABLE_NAME_ATTR);
|
||||
String priorRegion = r.getAttributes().get(DDB_REPLICA_REGION_ATTR);
|
||||
if (priorRegion == null || priorRegion.isBlank()) {
|
||||
priorRegion = replicaRegionFromPhysicalId(
|
||||
r.getPhysicalId(), priorTableName != null ? priorTableName : tableName);
|
||||
}
|
||||
List<String> removeRegions = priorRegion != null
|
||||
&& !priorRegion.isBlank()
|
||||
&& !priorRegion.equals(replicaRegion)
|
||||
? List.of(priorRegion)
|
||||
: List.of();
|
||||
// Validate and persist replacement as one operation so an old-replica removal failure
|
||||
// cannot leave the new replica applied while the resource still points at the old region.
|
||||
dynamoDbService.applyReplicaUpdates(
|
||||
tableName, List.of(replicaRegion), removeRegions, region);
|
||||
r.setPhysicalId(tableName + "-" + replicaRegion);
|
||||
r.getAttributes().put(DDB_REPLICA_TABLE_NAME_ATTR, tableName);
|
||||
r.getAttributes().put(DDB_REPLICA_REGION_ATTR, replicaRegion);
|
||||
r.getAttributes().put(DDB_REPLICA_SKIP_DELETION_ATTR,
|
||||
Boolean.toString(Boolean.TRUE.equals(
|
||||
parseBooleanOrNull(resolveOptional(props, "SkipReplicaDeletion", engine)))));
|
||||
}
|
||||
|
||||
private void deleteDynamoDbReplicaSafe(StackResource r, String region) {
|
||||
if (Boolean.parseBoolean(r.getAttributes().get(DDB_REPLICA_SKIP_DELETION_ATTR))) {
|
||||
LOG.debugv("Keeping replica for retained Custom::DynamoDBReplica {0}", r.getLogicalId());
|
||||
return;
|
||||
}
|
||||
String tableName = r.getAttributes().get(DDB_REPLICA_TABLE_NAME_ATTR);
|
||||
String replicaRegion = r.getAttributes().get(DDB_REPLICA_REGION_ATTR);
|
||||
if (replicaRegion == null || replicaRegion.isBlank()) {
|
||||
replicaRegion = replicaRegionFromPhysicalId(r.getPhysicalId(), tableName);
|
||||
}
|
||||
if (tableName == null || tableName.isBlank() || replicaRegion == null || replicaRegion.isBlank()) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
dynamoDbService.applyReplicaUpdates(tableName, List.of(), List.of(replicaRegion), region);
|
||||
} catch (Exception e) {
|
||||
LOG.debugv("Could not remove replica {0} from table {1}: {2}",
|
||||
replicaRegion, tableName, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static String replicaRegionFromPhysicalId(String physicalId, String tableName) {
|
||||
if (physicalId == null || physicalId.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
String prefix = tableName + "-";
|
||||
return tableName != null && !tableName.isBlank() && physicalId.startsWith(prefix)
|
||||
? physicalId.substring(prefix.length())
|
||||
: physicalId;
|
||||
}
|
||||
|
||||
// Reads the ResourceProperties stashed at the last create/update (CR_PROPERTIES_ATTR).
|
||||
// Returns null when nothing is stashed or it cannot be parsed.
|
||||
private ObjectNode readStashedProperties(StackResource r) {
|
||||
|
||||
@@ -1138,6 +1138,32 @@ public class DynamoDbJsonHandler {
|
||||
|
||||
private Response handleUpdateTable(JsonNode request, String region) {
|
||||
String tableName = request.path("TableName").asText();
|
||||
JsonNode replicaUpdates = request.path("ReplicaUpdates");
|
||||
List<String> addRegions = new ArrayList<>();
|
||||
List<String> removeRegions = new ArrayList<>();
|
||||
List<String> updateRegions = new ArrayList<>();
|
||||
if (replicaUpdates.isArray()) {
|
||||
for (JsonNode update : replicaUpdates) {
|
||||
JsonNode create = update.path("Create");
|
||||
if (create.isObject()) {
|
||||
String replicaRegion = create.path("RegionName").asText(null);
|
||||
validateReplicaRegion(replicaRegion);
|
||||
addRegions.add(replicaRegion);
|
||||
}
|
||||
JsonNode delete = update.path("Delete");
|
||||
if (delete.isObject()) {
|
||||
String replicaRegion = delete.path("RegionName").asText(null);
|
||||
validateReplicaRegion(replicaRegion);
|
||||
removeRegions.add(replicaRegion);
|
||||
}
|
||||
JsonNode updateNode = update.path("Update");
|
||||
if (updateNode.isObject()) {
|
||||
String replicaRegion = updateNode.path("RegionName").asText(null);
|
||||
validateReplicaRegion(replicaRegion);
|
||||
updateRegions.add(replicaRegion);
|
||||
}
|
||||
}
|
||||
}
|
||||
Long readCapacity = null;
|
||||
Long writeCapacity = null;
|
||||
JsonNode pt = request.path("ProvisionedThroughput");
|
||||
@@ -1198,6 +1224,11 @@ public class DynamoDbJsonHandler {
|
||||
}
|
||||
}
|
||||
|
||||
if (!addRegions.isEmpty() || !removeRegions.isEmpty() || !updateRegions.isEmpty()) {
|
||||
dynamoDbService.validateReplicaUpdates(
|
||||
tableName, addRegions, removeRegions, updateRegions, region);
|
||||
}
|
||||
|
||||
TableDefinition table = dynamoDbService.updateTable(tableName, readCapacity, writeCapacity,
|
||||
gsiCreates, gsiDeletes, newAttrDefs, region);
|
||||
|
||||
@@ -1246,11 +1277,21 @@ public class DynamoDbJsonHandler {
|
||||
}
|
||||
}
|
||||
|
||||
if (!addRegions.isEmpty() || !removeRegions.isEmpty() || !updateRegions.isEmpty()) {
|
||||
table = dynamoDbService.applyReplicaUpdates(tableName, addRegions, removeRegions, updateRegions, region);
|
||||
}
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.set("TableDescription", tableToNode(table));
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private static void validateReplicaRegion(String replicaRegion) {
|
||||
if (replicaRegion == null || replicaRegion.isBlank()) {
|
||||
throw new AwsException("ValidationException", "Replica RegionName must not be empty", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private Response handleDescribeTimeToLive(JsonNode request, String region) {
|
||||
String tableName = request.path("TableName").asText();
|
||||
TableDefinition table = dynamoDbService.describeTable(tableName, region);
|
||||
@@ -1913,6 +1954,22 @@ public class DynamoDbJsonHandler {
|
||||
warmThroughput.put("WriteUnitsPerSecond", 0);
|
||||
node.set("WarmThroughput", warmThroughput);
|
||||
|
||||
// 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.
|
||||
List<String> replicaRegions = table.getReplicaRegions();
|
||||
if (replicaRegions != null && !replicaRegions.isEmpty()) {
|
||||
ArrayNode replicas = objectMapper.createArrayNode();
|
||||
for (String replicaRegion : replicaRegions) {
|
||||
ObjectNode replica = objectMapper.createObjectNode();
|
||||
replica.put("RegionName", replicaRegion);
|
||||
replica.put("ReplicaStatus", "ACTIVE");
|
||||
replicas.add(replica);
|
||||
}
|
||||
node.set("Replicas", replicas);
|
||||
node.put("GlobalTableVersion", "2019.11.21");
|
||||
}
|
||||
|
||||
ArrayNode keySchemaArray = objectMapper.createArrayNode();
|
||||
for (var ks : table.getKeySchema()) {
|
||||
ObjectNode ksNode = objectMapper.createObjectNode();
|
||||
|
||||
@@ -52,6 +52,10 @@ import java.util.zip.GZIPOutputStream;
|
||||
@ApplicationScoped
|
||||
public class DynamoDbService {
|
||||
|
||||
private static final String LOCAL_REPLICA_UPDATE_ERROR =
|
||||
"Cannot add, delete, or update the local region through ReplicaUpdates. "
|
||||
+ "Use CreateTable, DeleteTable, or UpdateTable as required.";
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(DynamoDbService.class);
|
||||
|
||||
private final StorageBackend<String, TableDefinition> tableStore;
|
||||
@@ -1307,6 +1311,84 @@ public class DynamoDbService {
|
||||
return table;
|
||||
}
|
||||
|
||||
// --- Global-table replicas ---
|
||||
|
||||
/**
|
||||
* Applies global-table replica changes (the {@code ReplicaUpdates} of UpdateTable, and the
|
||||
* legacy {@code dynamodb.Table.replicationRegions} replica custom resource). This single-process
|
||||
* emulator serves every region from the same table, so a replica is tracked as metadata and
|
||||
* surfaced by DescribeTable as an ACTIVE Replica; no cross-region copy is performed. Regions are
|
||||
* de-duplicated and adding an existing / removing an absent one is a no-op, keeping
|
||||
* CloudFormation re-applies and cleanup idempotent. Update remains strict because it represents
|
||||
* a settings change and has no target when the replica is absent.
|
||||
*/
|
||||
public TableDefinition applyReplicaUpdates(String tableName, List<String> addRegions,
|
||||
List<String> removeRegions, String region) {
|
||||
return applyReplicaUpdates(tableName, addRegions, removeRegions, Collections.emptyList(), region);
|
||||
}
|
||||
|
||||
public TableDefinition applyReplicaUpdates(String tableName, List<String> addRegions,
|
||||
List<String> removeRegions, List<String> updateRegions, String region) {
|
||||
TableDefinition table = validateReplicaUpdatesAndGetTable(
|
||||
tableName, addRegions, removeRegions, updateRegions, region);
|
||||
List<String> replicas = new ArrayList<>(table.getReplicaRegions());
|
||||
if (removeRegions != null) {
|
||||
replicas.removeAll(removeRegions);
|
||||
}
|
||||
if (addRegions != null) {
|
||||
for (String r : addRegions) {
|
||||
if (r != null && !r.isBlank() && !replicas.contains(r)) {
|
||||
replicas.add(r);
|
||||
}
|
||||
}
|
||||
}
|
||||
table.setReplicaRegions(replicas);
|
||||
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;
|
||||
}
|
||||
|
||||
public void validateReplicaUpdates(String tableName, List<String> addRegions,
|
||||
List<String> removeRegions, List<String> updateRegions, String region) {
|
||||
validateReplicaUpdatesAndGetTable(tableName, addRegions, removeRegions, updateRegions, region);
|
||||
}
|
||||
|
||||
private TableDefinition validateReplicaUpdatesAndGetTable(
|
||||
String tableName, List<String> addRegions, List<String> removeRegions,
|
||||
List<String> updateRegions, String region) {
|
||||
validateReplicaRegions(addRegions, region);
|
||||
validateReplicaRegions(removeRegions, region);
|
||||
validateReplicaRegions(updateRegions, region);
|
||||
String canonicalTableName = canonicalTableName(region, tableName);
|
||||
String storageKey = regionKey(region, canonicalTableName);
|
||||
TableDefinition table = tableStore.get(storageKey)
|
||||
.orElseThrow(() -> resourceNotFoundException(canonicalTableName));
|
||||
if (updateRegions != null) {
|
||||
for (String replicaRegion : updateRegions) {
|
||||
if (!table.getReplicaRegions().contains(replicaRegion)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Replica " + replicaRegion + " does not exist", 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
return table;
|
||||
}
|
||||
|
||||
private static void validateReplicaRegions(List<String> replicaRegions, String localRegion) {
|
||||
if (replicaRegions == null) {
|
||||
return;
|
||||
}
|
||||
for (String replicaRegion : replicaRegions) {
|
||||
if (replicaRegion == null || replicaRegion.isBlank()) {
|
||||
throw new AwsException("ValidationException", "Replica RegionName must not be empty", 400);
|
||||
}
|
||||
if (replicaRegion.equals(localRegion)) {
|
||||
throw new AwsException("ValidationException", LOCAL_REPLICA_UPDATE_ERROR, 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- TTL ---
|
||||
|
||||
public void updateTimeToLive(String tableName, String ttlAttributeName, boolean enabled, String region) {
|
||||
|
||||
@@ -47,6 +47,9 @@ public class TableDefinition {
|
||||
private String tableClass; // "STANDARD" or "STANDARD_INFREQUENT_ACCESS"
|
||||
private Integer onDemandMaxReadRequestUnits;
|
||||
private Integer onDemandMaxWriteRequestUnits;
|
||||
// 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;
|
||||
|
||||
public TableDefinition() {
|
||||
this.keySchema = new ArrayList<>();
|
||||
@@ -56,6 +59,7 @@ public class TableDefinition {
|
||||
this.localSecondaryIndexes = new ArrayList<>();
|
||||
this.pointInTimeRecoveryRecoveryPeriodInDays = 35;
|
||||
this.kinesisStreamingDestinations = new ArrayList<>();
|
||||
this.replicaRegions = new ArrayList<>();
|
||||
}
|
||||
|
||||
public TableDefinition(String tableName,
|
||||
@@ -83,6 +87,7 @@ public class TableDefinition {
|
||||
this.localSecondaryIndexes = new ArrayList<>();
|
||||
this.pointInTimeRecoveryRecoveryPeriodInDays = 35;
|
||||
this.kinesisStreamingDestinations = new ArrayList<>();
|
||||
this.replicaRegions = new ArrayList<>();
|
||||
}
|
||||
|
||||
public String getTableName() { return tableName; }
|
||||
@@ -188,6 +193,13 @@ public class TableDefinition {
|
||||
public String getTableClass() { return tableClass; }
|
||||
public void setTableClass(String tableClass) { this.tableClass = tableClass; }
|
||||
|
||||
public List<String> getReplicaRegions() {
|
||||
return replicaRegions != null ? replicaRegions : new ArrayList<>();
|
||||
}
|
||||
public void setReplicaRegions(List<String> replicaRegions) {
|
||||
this.replicaRegions = replicaRegions != null ? replicaRegions : new ArrayList<>();
|
||||
}
|
||||
|
||||
public Integer getOnDemandMaxReadRequestUnits() { return onDemandMaxReadRequestUnits; }
|
||||
public void setOnDemandMaxReadRequestUnits(Integer v) { this.onDemandMaxReadRequestUnits = v; }
|
||||
|
||||
|
||||
Reference in new issue
Block a user