mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(dynamodb): honor MICROSECOND ApproximateCreationDateTimePrecision for Kinesis destinations and streams false enablement (#3874)
* fix(dynamodb): honor MICROSECOND ApproximateCreationDateTimePrecision for Kinesis destinations Store the precision requested in EnableKinesisStreamingConfiguration, echo it in the enable response, and return it from DescribeKinesisStreamingDestination, so Terraform no longer destroys and recreates the destination on every apply. Stamp ApproximateCreationDateTime in epoch microseconds for MICROSECOND destinations, with a shared eventID per change across destinations. Reject precisions other than MILLISECOND and MICROSECOND with ValidationException. Add forwarder unit and end-to-end integration coverage, and document the behavior. * fix(dynamodb): stop enabling DynamoDB Streams with a Kinesis destination EnableKinesisStreamingDestination turned on DynamoDB Streams for the table. Kinesis forwarding does not need it, and Terraform saw stream_enabled drift on aws_dynamodb_table on every run. Leave the table's stream setting as the caller configured it. * test(dynamodb): add Terraform compat test for Kinesis destination precision Apply a Kinesis streaming destination with MICROSECOND precision and check that it reads back, that a second plan is clean, and that a precision change replaces the destination once and then settles.
This commit is contained in:
1 parent
b10b81d253
commit
5d23a70cc0
9 files changed
+467
-34
No files matched your search
+18
-9
@@ -2000,6 +2000,18 @@ public class DynamoDbJsonHandler {
|
||||
private Response handleEnableKinesisStreamingDestination(JsonNode request, String region) {
|
||||
String tableName = request.path("TableName").asText();
|
||||
String streamArn = request.path("StreamArn").asText();
|
||||
JsonNode precisionNode = request.path("EnableKinesisStreamingConfiguration")
|
||||
.path("ApproximateCreationDateTimePrecision");
|
||||
String precision = precisionNode.isMissingNode() || precisionNode.isNull()
|
||||
? KinesisStreamingDestination.PRECISION_MILLISECOND : precisionNode.asText();
|
||||
if (!KinesisStreamingDestination.PRECISION_MILLISECOND.equals(precision)
|
||||
&& !KinesisStreamingDestination.PRECISION_MICROSECOND.equals(precision)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"1 validation error detected: Value '" + precision
|
||||
+ "' at 'enableKinesisStreamingConfiguration.approximateCreationDateTimePrecision' "
|
||||
+ "failed to satisfy constraint: Member must satisfy enum value set: [MILLISECOND, MICROSECOND]",
|
||||
400);
|
||||
}
|
||||
|
||||
TableDefinition table = dynamoDbService.describeTable(tableName, region);
|
||||
String resolvedTableName = table.getTableName();
|
||||
@@ -2021,18 +2033,13 @@ public class DynamoDbJsonHandler {
|
||||
if (existing.isPresent()) {
|
||||
existing.get().setDestinationStatus("ACTIVE");
|
||||
existing.get().setDestinationStatusDescription("Kinesis streaming is enabled for this table");
|
||||
existing.get().setApproximateCreationDateTimePrecision(precision);
|
||||
} else {
|
||||
table.getKinesisStreamingDestinations().add(new KinesisStreamingDestination(streamArn));
|
||||
}
|
||||
|
||||
if (!table.isStreamEnabled()) {
|
||||
StreamDescription sd = dynamoDbStreamService.enableStream(
|
||||
resolvedTableName, table.getTableArn(), "NEW_AND_OLD_IMAGES", region);
|
||||
table.setStreamEnabled(true);
|
||||
table.setStreamArn(sd.getStreamArn());
|
||||
table.setStreamViewType("NEW_AND_OLD_IMAGES");
|
||||
table.getKinesisStreamingDestinations().add(new KinesisStreamingDestination(streamArn, precision));
|
||||
}
|
||||
|
||||
// DynamoDB Streams is left as the caller configured it: Kinesis forwarding does not depend on it, and
|
||||
// turning it on here showed up as stream_enabled drift on aws_dynamodb_table that never converged.
|
||||
dynamoDbService.persistTable(resolvedTableName, table, region);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
@@ -2040,6 +2047,8 @@ public class DynamoDbJsonHandler {
|
||||
response.put("StreamArn", streamArn);
|
||||
response.put("DestinationStatus", "ACTIVE");
|
||||
response.put("DestinationStatusDescription", "Kinesis streaming is enabled for this table");
|
||||
response.putObject("EnableKinesisStreamingConfiguration")
|
||||
.put("ApproximateCreationDateTimePrecision", precision);
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
|
||||
+47
-21
@@ -17,7 +17,10 @@ import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Executors;
|
||||
@@ -141,23 +144,40 @@ public class KinesisStreamingForwarder {
|
||||
if (destinations == null || destinations.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
// Snapshot each active destination's precision once: Enable/Disable mutate these objects in place on
|
||||
// request threads, and the payload chosen for a destination must match the one serialized for it.
|
||||
Map<String, String> precisionByStreamArn = new LinkedHashMap<>();
|
||||
for (KinesisStreamingDestination dest : destinations) {
|
||||
if ("ACTIVE".equals(dest.getDestinationStatus())) {
|
||||
precisionByStreamArn.putIfAbsent(dest.getStreamArn(), dest.getApproximateCreationDateTimePrecision());
|
||||
}
|
||||
}
|
||||
if (precisionByStreamArn.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Instant now = Instant.now();
|
||||
JsonNode sourceItem = newItem != null ? newItem : oldItem;
|
||||
ObjectNode keys = buildKeys(sourceItem, table);
|
||||
|
||||
byte[] data;
|
||||
// One serialized payload per precision in use: every destination sees the same event (same eventID),
|
||||
// stamped at the resolution its ApproximateCreationDateTimePrecision asks for.
|
||||
String eventId = UUID.randomUUID().toString();
|
||||
Map<String, byte[]> dataByPrecision = new HashMap<>(2);
|
||||
String partitionKey;
|
||||
try {
|
||||
ObjectNode payload = buildPayload(eventName, keys, newItem, oldItem, table.getTableName(), region, now);
|
||||
data = objectMapper.writeValueAsBytes(payload);
|
||||
for (String precision : precisionByStreamArn.values()) {
|
||||
if (!dataByPrecision.containsKey(precision)) {
|
||||
ObjectNode payload = buildPayload(eventId, eventName, keys, newItem, oldItem,
|
||||
table.getTableName(), region, now, precision);
|
||||
dataByPrecision.put(precision, objectMapper.writeValueAsBytes(payload));
|
||||
}
|
||||
}
|
||||
partitionKey = extractPartitionKey(keys, table);
|
||||
} catch (Exception e) {
|
||||
// Serialization is a deterministic terminal failure: nothing can be forwarded for this event.
|
||||
for (KinesisStreamingDestination dest : destinations) {
|
||||
if (!"ACTIVE".equals(dest.getDestinationStatus())) {
|
||||
continue;
|
||||
}
|
||||
DestinationState st = stateFor(ownerAccountId, region, table.getTableName(), dest.getStreamArn());
|
||||
for (String streamArn : precisionByStreamArn.keySet()) {
|
||||
DestinationState st = stateFor(ownerAccountId, region, table.getTableName(), streamArn);
|
||||
synchronized (st) {
|
||||
st.dropped++;
|
||||
recordError(st, e);
|
||||
@@ -167,14 +187,12 @@ public class KinesisStreamingForwarder {
|
||||
return;
|
||||
}
|
||||
|
||||
for (KinesisStreamingDestination dest : destinations) {
|
||||
if (!"ACTIVE".equals(dest.getDestinationStatus())) {
|
||||
continue;
|
||||
}
|
||||
String streamName = extractStreamName(dest.getStreamArn());
|
||||
DestinationState st = stateFor(ownerAccountId, region, table.getTableName(), dest.getStreamArn());
|
||||
for (Map.Entry<String, String> target : precisionByStreamArn.entrySet()) {
|
||||
String streamArn = target.getKey();
|
||||
String streamName = extractStreamName(streamArn);
|
||||
DestinationState st = stateFor(ownerAccountId, region, table.getTableName(), streamArn);
|
||||
PendingRecord rec = new PendingRecord(ownerAccountId, region, streamName, table.getTableName(),
|
||||
data, partitionKey, now);
|
||||
dataByPrecision.get(target.getValue()), partitionKey, now);
|
||||
synchronized (st) {
|
||||
if (st.queue.size() >= MAX_BUFFERED) {
|
||||
st.queue.pollFirst(); // drop OLDEST
|
||||
@@ -413,14 +431,14 @@ public class KinesisStreamingForwarder {
|
||||
return out;
|
||||
}
|
||||
|
||||
// ──────────────────────────── Payload construction (unchanged) ────────────────────────────
|
||||
// ──────────────────────────── Payload construction ────────────────────────────
|
||||
|
||||
private ObjectNode buildPayload(String eventName, JsonNode keys,
|
||||
private ObjectNode buildPayload(String eventId, String eventName, JsonNode keys,
|
||||
JsonNode newImage, JsonNode oldImage,
|
||||
String tableName, String region, Instant timestamp) {
|
||||
String tableName, String region, Instant timestamp, String precision) {
|
||||
ObjectNode payload = objectMapper.createObjectNode();
|
||||
payload.put("awsRegion", region);
|
||||
payload.put("eventID", UUID.randomUUID().toString());
|
||||
payload.put("eventID", eventId);
|
||||
payload.put("eventName", eventName);
|
||||
payload.putNull("userIdentity");
|
||||
payload.put("recordFormat", "application/json");
|
||||
@@ -428,7 +446,7 @@ public class KinesisStreamingForwarder {
|
||||
payload.put("eventSource", "aws:dynamodb");
|
||||
|
||||
ObjectNode dynamodb = objectMapper.createObjectNode();
|
||||
dynamodb.put("ApproximateCreationDateTime", timestamp.toEpochMilli());
|
||||
dynamodb.put("ApproximateCreationDateTime", approximateCreationDateTime(timestamp, precision));
|
||||
if (keys != null) {
|
||||
dynamodb.set("Keys", keys);
|
||||
}
|
||||
@@ -439,12 +457,20 @@ public class KinesisStreamingForwarder {
|
||||
dynamodb.set("OldImage", oldImage);
|
||||
}
|
||||
dynamodb.put("SizeBytes", 0);
|
||||
dynamodb.put("ApproximateCreationDateTimePrecision", "MILLISECOND");
|
||||
dynamodb.put("ApproximateCreationDateTimePrecision", precision);
|
||||
payload.set("dynamodb", dynamodb);
|
||||
|
||||
return payload;
|
||||
}
|
||||
|
||||
static long approximateCreationDateTime(Instant timestamp, String precision) {
|
||||
if (KinesisStreamingDestination.PRECISION_MICROSECOND.equals(precision)) {
|
||||
return Math.addExact(Math.multiplyExact(timestamp.getEpochSecond(), 1_000_000L),
|
||||
timestamp.getNano() / 1_000L);
|
||||
}
|
||||
return timestamp.toEpochMilli();
|
||||
}
|
||||
|
||||
private ObjectNode buildKeys(JsonNode item, TableDefinition table) {
|
||||
ObjectNode keys = objectMapper.createObjectNode();
|
||||
if (item == null) {
|
||||
|
||||
+12
-2
@@ -7,6 +7,9 @@ import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class KinesisStreamingDestination {
|
||||
|
||||
public static final String PRECISION_MILLISECOND = "MILLISECOND";
|
||||
public static final String PRECISION_MICROSECOND = "MICROSECOND";
|
||||
|
||||
private String streamArn;
|
||||
private String destinationStatus;
|
||||
private String destinationStatusDescription;
|
||||
@@ -15,10 +18,14 @@ public class KinesisStreamingDestination {
|
||||
public KinesisStreamingDestination() {}
|
||||
|
||||
public KinesisStreamingDestination(String streamArn) {
|
||||
this(streamArn, PRECISION_MILLISECOND);
|
||||
}
|
||||
|
||||
public KinesisStreamingDestination(String streamArn, String approximateCreationDateTimePrecision) {
|
||||
this.streamArn = streamArn;
|
||||
this.destinationStatus = "ACTIVE";
|
||||
this.destinationStatusDescription = "Kinesis streaming is enabled for this table";
|
||||
this.approximateCreationDateTimePrecision = "MILLISECOND";
|
||||
this.approximateCreationDateTimePrecision = approximateCreationDateTimePrecision;
|
||||
}
|
||||
|
||||
public String getStreamArn() { return streamArn; }
|
||||
@@ -30,7 +37,10 @@ public class KinesisStreamingDestination {
|
||||
public String getDestinationStatusDescription() { return destinationStatusDescription; }
|
||||
public void setDestinationStatusDescription(String desc) { this.destinationStatusDescription = desc; }
|
||||
|
||||
public String getApproximateCreationDateTimePrecision() { return approximateCreationDateTimePrecision; }
|
||||
/** Destinations persisted before the precision was stored read back as the AWS default, MILLISECOND. */
|
||||
public String getApproximateCreationDateTimePrecision() {
|
||||
return approximateCreationDateTimePrecision != null ? approximateCreationDateTimePrecision : PRECISION_MILLISECOND;
|
||||
}
|
||||
public void setApproximateCreationDateTimePrecision(String precision) {
|
||||
this.approximateCreationDateTimePrecision = precision;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user