mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(cloudformation): reconcile DynamoDB streams declared by StreamSpecification (#2411)
Provisioning AWS::DynamoDB::Table ignored StreamSpecification, so a stack that declared a stream produced a streamless table: DescribeTable returned neither StreamSpecification nor LatestStreamArn, and event source mappings registered against the expected stream ARN polled "Stream not found" forever. The plain UpdateTable API enabled streams correctly on the same runtime, which is what made this look CloudFormation-specific. The CloudFormation property carries no StreamEnabled flag — unlike the DynamoDB API, declaring the block IS the request — so the block's presence now turns the stream on, honouring StreamViewType. A block that omits StreamViewType still yields a working stream: the schema marks it required and AWS documents no default, so this is deliberately the same leniency DynamoDbJsonHandler already applies on CreateTable/UpdateTable, not an AWS behaviour. Removing the block on an update is the inverse request and switches the stream back off, so a table updated out of streaming stops emitting records to whatever still holds its ARN. Streams are reconciled through new DynamoDbService enableStream/disableStream, mirroring what the JSON handler already does inline for CreateTable/UpdateTable and persisting the table so the change survives. Callers that reach DynamoDB through the service rather than the handler had no entry point for this. Re-enabling a live stream now retargets its view type. The records a stream emits are built from the stream description, which was left untouched once the stream was ENABLED, so a changed StreamViewType was reported by DescribeTable while records kept the old shape. This also fixes the same inconsistency on the UpdateTable path. Make that stream state durable. Both handler paths mutate the table after the service has stored it, and a persistent backend serializes on write rather than holding the live object, so the on-disk copy kept the previous settings. Since startup rebuilds every stream from the persisted table, a restart resurrected the old view type — and a stream declared on CreateTable was lost outright, because the persisted copy still had streamEnabled false. Both paths now flush once after their mutations, which also makes the billing mode, table class, tags and SSE settings they set survive a restart. Also reconcile the StreamArn attribute: only a live stream has an ARN worth handing to Fn::GetAtt. The previous synthetic value resolved to nothing on a streamless table, and because an update starts from the resource's previous attributes, a stale ARN is now removed rather than merely left unwritten. Adds DynamoDbStreamSpecificationCfnProvisionerTest covering the declared stream, an explicit StreamViewType, a block with no view type, removal and retention on update, a view-type change on update, and the unchanged no-stream path; DynamoDbStreamServiceTest coverage that a retarget reaches the emitted records; and DynamoDbStreamViewTypeDurabilityTest, which drives the handler against a file-backed store and reloads it to prove both paths survive a restart.
This commit is contained in:
1 parent
2314c85b62
commit
c4597f2e53
7 files changed
+526
-1
No files matched your search
+29
-1
@@ -2199,9 +2199,37 @@ public class CloudFormationResourceProvisioner {
|
||||
}
|
||||
table = dynamoDbService.describeTable(tableName, region);
|
||||
}
|
||||
|
||||
// A template that declares StreamSpecification wants a stream. Unlike the DynamoDB API,
|
||||
// the CloudFormation property carries no StreamEnabled flag — declaring the block IS the
|
||||
// request — so its presence alone turns the stream on. Without this the table is created
|
||||
// streamless and an event source mapping polls its ARN forever.
|
||||
//
|
||||
// Removing the block on an update is the inverse request: the stream is reconciled off,
|
||||
// or a table updated out of streaming would keep emitting records to whatever still holds
|
||||
// its ARN.
|
||||
JsonNode streamSpec = props != null ? props.path("StreamSpecification") : null;
|
||||
if (streamSpec != null && streamSpec.isObject()) {
|
||||
String viewType = streamSpec.has("StreamViewType")
|
||||
? engine.resolve(streamSpec.get("StreamViewType"))
|
||||
: null;
|
||||
table = dynamoDbService.enableStream(tableName, viewType, region);
|
||||
} else if (table.isStreamEnabled()) {
|
||||
table = dynamoDbService.disableStream(tableName, region);
|
||||
}
|
||||
|
||||
r.setPhysicalId(tableName);
|
||||
r.getAttributes().put("Arn", table.getTableArn());
|
||||
r.getAttributes().put("StreamArn", table.getTableArn() + "/stream/2024-01-01T00:00:00.000");
|
||||
// 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
|
||||
// has since been switched off would resolve to something no longer running. An update
|
||||
// starts from the previous attributes, so the stale entry has to be removed rather than
|
||||
// merely left unwritten.
|
||||
if (table.isStreamEnabled() && table.getStreamArn() != null) {
|
||||
r.getAttributes().put("StreamArn", table.getStreamArn());
|
||||
} else {
|
||||
r.getAttributes().remove("StreamArn");
|
||||
}
|
||||
}
|
||||
|
||||
// ── Lambda ────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -213,6 +213,12 @@ public class DynamoDbJsonHandler {
|
||||
table.setKmsMasterKeyArn(defaultKmsMasterKeyArn(region));
|
||||
}
|
||||
|
||||
// Everything above mutates the table AFTER createTable stored it, and a persistent
|
||||
// backend serializes on write rather than holding the live object. Flush once so the
|
||||
// stream, billing, class, tag and SSE settings survive a restart -- stream state
|
||||
// especially, since startup rebuilds streams from the persisted table.
|
||||
dynamoDbService.persistTable(tableName, table, region);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.set("TableDescription", tableToNode(table));
|
||||
return Response.ok(response).build();
|
||||
@@ -1277,6 +1283,11 @@ public class DynamoDbJsonHandler {
|
||||
}
|
||||
}
|
||||
|
||||
// Same flush as CreateTable: the stream mutations above land after updateTable's write,
|
||||
// so without this a retargeted view type is rebuilt from the stale persisted value on
|
||||
// restart and records resume the old image shape.
|
||||
dynamoDbService.persistTable(tableName, table, region);
|
||||
|
||||
if (!addRegions.isEmpty() || !removeRegions.isEmpty() || !updateRegions.isEmpty()) {
|
||||
table = dynamoDbService.applyReplicaUpdates(tableName, addRegions, removeRegions, updateRegions, region);
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import io.github.hectorvent.floci.services.dynamodb.model.ExportSummary;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.GlobalSecondaryIndex;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.LocalSecondaryIndex;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.KeySchemaElement;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.StreamDescription;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.TableDefinition;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.ConditionalCheckFailedException;
|
||||
import io.github.hectorvent.floci.services.s3.S3Service;
|
||||
@@ -58,6 +59,17 @@ public class DynamoDbService {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(DynamoDbService.class);
|
||||
|
||||
/**
|
||||
* View type applied when a stream is requested without an explicit StreamViewType.
|
||||
*
|
||||
* <p>Not an AWS default: the CloudFormation schema marks StreamViewType required inside
|
||||
* StreamSpecification, and the DynamoDB API documents no default either. This mirrors the
|
||||
* lenient handling {@code DynamoDbJsonHandler} already applies on CreateTable/UpdateTable
|
||||
* ({@code path("StreamViewType").asText("NEW_AND_OLD_IMAGES")}), so an under-specified
|
||||
* template gets a working stream instead of a rejection, and every entry point agrees.
|
||||
*/
|
||||
private static final String DEFAULT_STREAM_VIEW_TYPE = "NEW_AND_OLD_IMAGES";
|
||||
|
||||
private final StorageBackend<String, TableDefinition> tableStore;
|
||||
private final StorageBackend<String, Map<String, JsonNode>> itemStore;
|
||||
private final StorageBackend<String, ExportDescription> exportStore;
|
||||
@@ -345,6 +357,56 @@ public class DynamoDbService {
|
||||
tableStore.put(regionKey(region, canonicalTableName), table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Turns on the table's stream and persists the result, so DescribeTable reports
|
||||
* StreamSpecification / LatestStreamArn and event source mappings can find the stream.
|
||||
*
|
||||
* <p>Callers that reach DynamoDB through the service rather than the JSON handler — notably
|
||||
* CloudFormation provisioning — need this: the handler enables the stream inline on
|
||||
* CreateTable/UpdateTable, and without an equivalent entry point a table created by any other
|
||||
* path is left streamless. A null {@code viewType} falls back to
|
||||
* {@link #DEFAULT_STREAM_VIEW_TYPE}, matching the leniency the JSON handler already applies
|
||||
* rather than any documented AWS default.
|
||||
*
|
||||
* @return the updated table, or the unchanged table when no stream service is wired.
|
||||
*/
|
||||
public TableDefinition enableStream(String tableName, String viewType, String region) {
|
||||
TableDefinition table = describeTable(tableName, region);
|
||||
if (streamService == null) {
|
||||
return table;
|
||||
}
|
||||
String effectiveViewType = (viewType == null || viewType.isBlank())
|
||||
? DEFAULT_STREAM_VIEW_TYPE
|
||||
: viewType;
|
||||
StreamDescription sd = streamService.enableStream(
|
||||
table.getTableName(), table.getTableArn(), effectiveViewType, region);
|
||||
table.setStreamEnabled(true);
|
||||
table.setStreamArn(sd.getStreamArn());
|
||||
table.setStreamViewType(effectiveViewType);
|
||||
persistTable(tableName, table, region);
|
||||
return table;
|
||||
}
|
||||
|
||||
/**
|
||||
* Turns the table's stream off and persists the result, so it stops producing records.
|
||||
*
|
||||
* <p>The counterpart to {@link #enableStream}, for the same service-layer callers. The stream
|
||||
* ARN is retained on the table, matching what UpdateTable does through the JSON handler: AWS
|
||||
* keeps reporting {@code LatestStreamArn} for a disabled stream.
|
||||
*
|
||||
* @return the updated table, or the unchanged table when no stream service is wired.
|
||||
*/
|
||||
public TableDefinition disableStream(String tableName, String region) {
|
||||
TableDefinition table = describeTable(tableName, region);
|
||||
if (streamService == null) {
|
||||
return table;
|
||||
}
|
||||
streamService.disableStream(table.getTableName(), region);
|
||||
table.setStreamEnabled(false);
|
||||
persistTable(tableName, table, region);
|
||||
return table;
|
||||
}
|
||||
|
||||
public void deleteTable(String tableName, String region) {
|
||||
String canonicalTableName = canonicalTableName(region, tableName);
|
||||
String storageKey = regionKey(region, canonicalTableName);
|
||||
|
||||
@@ -77,6 +77,14 @@ public class DynamoDbStreamService {
|
||||
String key = streamKey(region, tableName);
|
||||
StreamDescription existing = streams.get(key);
|
||||
if (existing != null && "ENABLED".equals(existing.getStreamStatus())) {
|
||||
// Re-enabling a live stream with a different view type retargets it. The records this
|
||||
// stream emits are built from the description's view type, so leaving it untouched
|
||||
// would keep producing the old shape while the table reports the requested one.
|
||||
if (viewType != null && !viewType.equals(existing.getStreamViewType())) {
|
||||
existing.setStreamViewType(viewType);
|
||||
LOG.infov("Stream view type for table {0} in region {1} is now {2}",
|
||||
tableName, region, viewType);
|
||||
}
|
||||
return existing;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user