mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(dynamodb): mark time-to-live deletions on the stream as AWS does (#4930)
AWS writes a TTL deletion to the table's stream as a REMOVE record whose
userIdentity names the DynamoDB service. A DeleteItem removal has none.
The member names depend on where the record is read:
- The DynamoDB Streams API (GetRecords) capitalises them, as the SDK's
Identity shape expects:
{"PrincipalId": "dynamodb.amazonaws.com", "Type": "Service"}.
- The Lambda and EventBridge Pipes event record and the Kinesis Data
Streams destination payload use lowercase names,
{"type": "Service", "principalId": "dynamodb.amazonaws.com"}, the
shape the developer guide's TTL filter pattern matches.
With the DynamoDB Local engine, Floci passed Local's record through as
is: eventName UNKNOWN_TO_SDK_VERSION and no userIdentity.
DynamoDbLocalBackend now rewrites such records in a successful Streams
GetRecords reply to REMOVE with the Streams API identity, the one place
the public API, the Lambda poller, Pipes and the Redshift zero-ETL
consumer all read through.
The native engine wrote REMOVE but no identity, so a TTL deletion could
not be told from a DeleteItem. The sweep now marks its stream records
(a new in-memory DynamoDbStreamRecord flag) and its Kinesis CDC records.
GetRecords writes the Streams API identity, and the Kinesis record the
lowercase one.
The Lambda event source mapping poller and the Pipes poller rename the
Streams API identity to the event record shape before filtering and
delivery. DynamoDbTtlIdentity.Shape holds the member names of every
output. DeleteItem removals are unchanged.
This commit is contained in:
1 parent
04621d8444
commit
9a7ec1c9d0
19 files changed
+572
-15
No files matched your search
@@ -2778,12 +2778,13 @@ public class DynamoDbService {
|
||||
}
|
||||
deletedForTable++;
|
||||
if (streamService != null) {
|
||||
streamService.captureEvent("REMOVE", removed, null, scan.table(), scan.region());
|
||||
streamService.captureEvent("REMOVE", removed, null, scan.table(), scan.region(), true);
|
||||
}
|
||||
if (kinesisForwarder != null) {
|
||||
// Out of request scope here: pass the table owner's account explicitly so the CDC
|
||||
// record lands in the owner's stream, not the default account's same-named stream.
|
||||
kinesisForwarder.forward("REMOVE", removed, null, scan.table(), scan.region(), scan.accountId());
|
||||
kinesisForwarder.forward("REMOVE", removed, null, scan.table(), scan.region(), scan.accountId(),
|
||||
true);
|
||||
}
|
||||
}
|
||||
if (deletedForTable > 0) {
|
||||
|
||||
@@ -157,6 +157,12 @@ public class DynamoDbStreamService {
|
||||
|
||||
public void captureEvent(String eventName, JsonNode oldItem, JsonNode newItem,
|
||||
TableDefinition table, String region) {
|
||||
captureEvent(eventName, oldItem, newItem, table, region, false);
|
||||
}
|
||||
|
||||
/** {@code ttlDeletion} marks a removal made by the time to live sweep rather than by a caller. */
|
||||
public void captureEvent(String eventName, JsonNode oldItem, JsonNode newItem,
|
||||
TableDefinition table, String region, boolean ttlDeletion) {
|
||||
StreamDescription sd = streams.get(table.getTableArn());
|
||||
if (sd == null || !"ENABLED".equals(sd.getStreamStatus())) {
|
||||
return;
|
||||
@@ -184,6 +190,7 @@ public class DynamoDbStreamService {
|
||||
record.setNewImage(newImage);
|
||||
record.setOldImage(oldImage);
|
||||
record.setStreamViewType(viewType);
|
||||
record.setTtlDeletion(ttlDeletion);
|
||||
|
||||
ConcurrentLinkedDeque<DynamoDbStreamRecord> deque = records.get(sd.getStreamArn());
|
||||
if (deque != null) {
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package io.github.hectorvent.floci.services.dynamodb;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.ServicePrincipals;
|
||||
|
||||
/**
|
||||
* The {@code userIdentity} AWS writes on the records of an item that time to live deleted. A consumer
|
||||
* tells a TTL deletion from a {@code DeleteItem} by it. Each output names the identity's members its
|
||||
* own way, and {@link Shape} holds the names of every output.
|
||||
*/
|
||||
public final class DynamoDbTtlIdentity {
|
||||
|
||||
private static final String USER_IDENTITY = "userIdentity";
|
||||
|
||||
/** An output a record is read from, with the member names its identity has there. */
|
||||
public enum Shape {
|
||||
/** The DynamoDB Streams API, whose {@code Identity} members AWS returns capitalised. */
|
||||
STREAMS_API("Type", "PrincipalId"),
|
||||
/**
|
||||
* The Lambda and EventBridge Pipes event record, and the Kinesis Data Streams destination
|
||||
* payload: lowercase names, as measured against AWS.
|
||||
*/
|
||||
EVENT_RECORD("type", "principalId");
|
||||
|
||||
private final String type;
|
||||
private final String principalId;
|
||||
|
||||
Shape(String type, String principalId) {
|
||||
this.type = type;
|
||||
this.principalId = principalId;
|
||||
}
|
||||
}
|
||||
|
||||
private DynamoDbTtlIdentity() {
|
||||
}
|
||||
|
||||
public static void putOn(ObjectNode record, Shape shape) {
|
||||
record.putObject(USER_IDENTITY)
|
||||
.put(shape.type, "Service")
|
||||
.put(shape.principalId, ServicePrincipals.of("dynamodb"));
|
||||
}
|
||||
|
||||
/** A copy of a Streams API record, its identity renamed to the event record shape Lambda and Pipes deliver. */
|
||||
public static ObjectNode toEventRecord(JsonNode streamsRecord) {
|
||||
ObjectNode record = streamsRecord.deepCopy();
|
||||
if (record.get(USER_IDENTITY) instanceof ObjectNode identity) {
|
||||
ObjectNode event = record.putObject(USER_IDENTITY);
|
||||
event.set(Shape.EVENT_RECORD.type, identity.get(Shape.STREAMS_API.type));
|
||||
event.set(Shape.EVENT_RECORD.principalId, identity.get(Shape.STREAMS_API.principalId));
|
||||
}
|
||||
return record;
|
||||
}
|
||||
}
|
||||
+14
-3
@@ -140,6 +140,12 @@ public class KinesisStreamingForwarder {
|
||||
|
||||
public void forward(String eventName, JsonNode oldItem, JsonNode newItem,
|
||||
TableDefinition table, String region, String ownerAccountId) {
|
||||
forward(eventName, oldItem, newItem, table, region, ownerAccountId, false);
|
||||
}
|
||||
|
||||
/** {@code ttlDeletion} marks a removal made by the time to live sweep rather than by a caller. */
|
||||
public void forward(String eventName, JsonNode oldItem, JsonNode newItem,
|
||||
TableDefinition table, String region, String ownerAccountId, boolean ttlDeletion) {
|
||||
List<KinesisStreamingDestination> destinations = table.getKinesisStreamingDestinations();
|
||||
if (destinations == null || destinations.isEmpty()) {
|
||||
return;
|
||||
@@ -169,7 +175,7 @@ public class KinesisStreamingForwarder {
|
||||
for (String precision : precisionByStreamArn.values()) {
|
||||
if (!dataByPrecision.containsKey(precision)) {
|
||||
ObjectNode payload = buildPayload(eventId, eventName, keys, newItem, oldItem,
|
||||
table.getTableName(), region, now, precision);
|
||||
table.getTableName(), region, now, precision, ttlDeletion);
|
||||
dataByPrecision.put(precision, objectMapper.writeValueAsBytes(payload));
|
||||
}
|
||||
}
|
||||
@@ -441,12 +447,17 @@ public class KinesisStreamingForwarder {
|
||||
|
||||
private ObjectNode buildPayload(String eventId, String eventName, JsonNode keys,
|
||||
JsonNode newImage, JsonNode oldImage,
|
||||
String tableName, String region, Instant timestamp, String precision) {
|
||||
String tableName, String region, Instant timestamp, String precision,
|
||||
boolean ttlDeletion) {
|
||||
ObjectNode payload = objectMapper.createObjectNode();
|
||||
payload.put("awsRegion", region);
|
||||
payload.put("eventID", eventId);
|
||||
payload.put("eventName", eventName);
|
||||
payload.putNull("userIdentity");
|
||||
if (ttlDeletion) {
|
||||
DynamoDbTtlIdentity.putOn(payload, DynamoDbTtlIdentity.Shape.EVENT_RECORD);
|
||||
} else {
|
||||
payload.putNull("userIdentity");
|
||||
}
|
||||
payload.put("recordFormat", "application/json");
|
||||
payload.put("tableName", tableName);
|
||||
payload.put("eventSource", "aws:dynamodb");
|
||||
|
||||
+3
@@ -177,6 +177,9 @@ public class NativeDynamoDbStreamsJsonHandler {
|
||||
node.put("eventVersion", record.getEventVersion());
|
||||
node.put("eventSource", record.getEventSource());
|
||||
node.put("awsRegion", record.getAwsRegion());
|
||||
if (record.isTtlDeletion()) {
|
||||
DynamoDbTtlIdentity.putOn(node, DynamoDbTtlIdentity.Shape.STREAMS_API);
|
||||
}
|
||||
|
||||
ObjectNode dynamodb = objectMapper.createObjectNode();
|
||||
dynamodb.put("ApproximateCreationDateTime", record.getApproximateCreationDateTime());
|
||||
|
||||
+19
@@ -17,6 +17,7 @@ import io.github.hectorvent.floci.core.resource.ExplorerResource;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.dynamodb.DynamoDbTableNames;
|
||||
import io.github.hectorvent.floci.services.dynamodb.DynamoDbTtlIdentity;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.AttributeDefinition;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.GlobalSecondaryIndex;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.KeySchemaElement;
|
||||
@@ -53,6 +54,7 @@ public class DynamoDbLocalBackend implements DynamoDbBackend {
|
||||
|
||||
private static final String LOCAL_ARN_PREFIX = "arn:aws:dynamodb:ddblocal:000000000000:"; // partition-literal: DynamoDB Local's fixed ARN prefix
|
||||
private static final String LOCAL_REGION = "ddblocal";
|
||||
private static final String LOCAL_TTL_EVENT_NAME = "UNKNOWN_TO_SDK_VERSION";
|
||||
private static final Duration READINESS_BUDGET = Duration.ofSeconds(30);
|
||||
private static final Scope PROBE = new Scope("000000000000", "us-east-1"); // partition-literal: readiness probe namespace, Local accepts any region
|
||||
private static final String REPLICAS_UNSUPPORTED = "Replicas are not supported by the DynamoDB Local backend";
|
||||
@@ -171,9 +173,26 @@ public class DynamoDbLocalBackend implements DynamoDbBackend {
|
||||
}
|
||||
}
|
||||
toPublic(scope, reply.body());
|
||||
if (reply.status() >= 200 && reply.status() < 300 && call.api() == Api.DYNAMODB_STREAMS
|
||||
&& "GetRecords".equals(action)) {
|
||||
markTtlDeletions(reply.body());
|
||||
}
|
||||
return reply;
|
||||
}
|
||||
|
||||
// DynamoDB Local writes a TTL deletion under an event name the SDK does not know and with no identity.
|
||||
private static void markTtlDeletions(JsonNode reply) {
|
||||
if (reply == null) {
|
||||
return;
|
||||
}
|
||||
for (JsonNode record : reply.path("Records")) {
|
||||
if (record instanceof ObjectNode object && LOCAL_TTL_EVENT_NAME.equals(text(object, "eventName"))) {
|
||||
object.put("eventName", "REMOVE");
|
||||
DynamoDbTtlIdentity.putOn(object, DynamoDbTtlIdentity.Shape.STREAMS_API);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Reply send(Scope scope, Api api, String action, JsonNode body) {
|
||||
try {
|
||||
return client.send(scope, api, action, body);
|
||||
|
||||
+4
@@ -19,6 +19,7 @@ public class DynamoDbStreamRecord {
|
||||
private JsonNode newImage;
|
||||
private JsonNode oldImage;
|
||||
private String streamViewType;
|
||||
private boolean ttlDeletion;
|
||||
|
||||
public DynamoDbStreamRecord() {}
|
||||
|
||||
@@ -56,4 +57,7 @@ public class DynamoDbStreamRecord {
|
||||
|
||||
public String getStreamViewType() { return streamViewType; }
|
||||
public void setStreamViewType(String streamViewType) { this.streamViewType = streamViewType; }
|
||||
|
||||
public boolean isTtlDeletion() { return ttlDeletion; }
|
||||
public void setTtlDeletion(boolean ttlDeletion) { this.ttlDeletion = ttlDeletion; }
|
||||
}
|
||||
+8
-5
@@ -9,6 +9,7 @@ import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RequestScopes;
|
||||
import io.github.hectorvent.floci.core.common.Resettable;
|
||||
import io.github.hectorvent.floci.services.dynamodb.DynamoDbTtlIdentity;
|
||||
import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbStreamReader;
|
||||
import io.github.hectorvent.floci.services.lambda.model.EventSourceMapping;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
|
||||
@@ -737,13 +738,15 @@ public class DynamoDbStreamsEventSourcePoller implements Resettable {
|
||||
/**
|
||||
* Builds the single-record node: a copy of the AWS stream record, whose top-level {@code eventName}
|
||||
* and metadata and {@code dynamodb} map with AttributeValue-wrapped images Lambda delivers as they
|
||||
* are, plus {@code eventSourceARN}. {@code ApproximateCreationDateTime} is written as a double, as
|
||||
* Floci has always delivered it. This is both the delivery record shape and the exact structure a
|
||||
* DynamoDB filter pattern matches against, so it serves the matcher unchanged. (Numeric operators
|
||||
* naturally never match here because AttributeValue numbers are JSON strings, AWS parity for free.)
|
||||
* are, plus {@code eventSourceARN}, with a {@code userIdentity} renamed from the Streams API's
|
||||
* capitalised members to the event record's. {@code ApproximateCreationDateTime} is written as a
|
||||
* double, as Floci has always delivered it. This is both the delivery record shape and the exact
|
||||
* structure a DynamoDB filter pattern matches against, so it serves the matcher unchanged. (Numeric
|
||||
* operators naturally never match here because AttributeValue numbers are JSON strings, AWS parity
|
||||
* for free.)
|
||||
*/
|
||||
private ObjectNode buildDynamoDbRecordNode(DynamoDbStreamReader.Record rec, EventSourceMapping esm) {
|
||||
ObjectNode item = rec.awsRecord().deepCopy();
|
||||
ObjectNode item = DynamoDbTtlIdentity.toEventRecord(rec.awsRecord());
|
||||
item.put("eventSourceARN", esm.getEventSourceArn());
|
||||
JsonNode created = item.path("dynamodb").path("ApproximateCreationDateTime");
|
||||
if (created.isNumber()) {
|
||||
|
||||
@@ -9,6 +9,7 @@ import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.Resettable;
|
||||
import io.github.hectorvent.floci.services.dynamodb.DynamoDbTtlIdentity;
|
||||
import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbStreamReader;
|
||||
import io.github.hectorvent.floci.services.kinesis.KinesisService;
|
||||
import io.github.hectorvent.floci.services.pipes.model.DesiredState;
|
||||
@@ -365,7 +366,7 @@ public class PipesPoller implements Resettable {
|
||||
LOG.infov("Pipe {0}: received {1} DynamoDB Stream record(s)", pipe.getName(), records.size());
|
||||
List<JsonNode> recordNodes = new ArrayList<>(records.size());
|
||||
for (DynamoDbStreamReader.Record record : records) {
|
||||
ObjectNode node = record.awsRecord().deepCopy();
|
||||
ObjectNode node = DynamoDbTtlIdentity.toEventRecord(record.awsRecord());
|
||||
node.put("eventSourceARN", pipe.getSource());
|
||||
recordNodes.add(node);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user