mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 14:30:28 -05:00
feat(dynamodb): add EnableKinesisStreamingDestination support (#427)
* feat(dynamodb): add EnableKinesisStreamingDestination support Implement EnableKinesisStreamingDestination, DisableKinesisStreamingDestination, and DescribeKinesisStreamingDestination API actions for DynamoDB. When a Kinesis streaming destination is enabled on a table, all item mutations (INSERT/MODIFY/REMOVE) are automatically forwarded to the configured Kinesis stream. The Kinesis record payload matches the AWS format with awsRegion, eventName, tableName, and dynamodb (Keys, NewImage, OldImage, etc.). Closes #426 * fix(dynamodb): address review feedback for KinesisStreamingDestination - Decouple Kinesis forwarding from DDB Streams: forwardToKinesisDestinations is now called directly from DynamoDbService mutation hooks, independent of whether DDB Streams is enabled - Fix ApproximateCreationDateTime to use milliseconds (was seconds) - Remove SequenceNumber and StreamViewType from Kinesis payload (not present in real AWS) - Add ApproximateCreationDateTimePrecision inside dynamodb record node - Return ValidationException when disabling an already-disabled destination - Simplify persistTable to accept TableDefinition directly - Add test for disable-already-disabled and payload format assertions
This commit is contained in:
1 parent
994eddc0ba
commit
467f5e0f79
7 files changed
+848
-5
No files matched your search
+103
-1
@@ -1,12 +1,14 @@
|
||||
package io.github.hectorvent.floci.services.dynamodb;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsErrorResponse;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsJsonController;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.*;
|
||||
import io.github.hectorvent.floci.services.kinesis.KinesisService;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
@@ -22,13 +24,15 @@ public class DynamoDbJsonHandler {
|
||||
|
||||
private final DynamoDbService dynamoDbService;
|
||||
private final DynamoDbStreamService dynamoDbStreamService;
|
||||
private final KinesisService kinesisService;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
@Inject
|
||||
public DynamoDbJsonHandler(DynamoDbService dynamoDbService, DynamoDbStreamService dynamoDbStreamService,
|
||||
ObjectMapper objectMapper) {
|
||||
KinesisService kinesisService, ObjectMapper objectMapper) {
|
||||
this.dynamoDbService = dynamoDbService;
|
||||
this.dynamoDbStreamService = dynamoDbStreamService;
|
||||
this.kinesisService = kinesisService;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
@@ -54,6 +58,9 @@ public class DynamoDbJsonHandler {
|
||||
case "TagResource" -> handleTagResource(request, region);
|
||||
case "UntagResource" -> handleUntagResource(request, region);
|
||||
case "ListTagsOfResource" -> handleListTagsOfResource(request, region);
|
||||
case "EnableKinesisStreamingDestination" -> handleEnableKinesisStreamingDestination(request, region);
|
||||
case "DisableKinesisStreamingDestination" -> handleDisableKinesisStreamingDestination(request, region);
|
||||
case "DescribeKinesisStreamingDestination" -> handleDescribeKinesisStreamingDestination(request, region);
|
||||
default -> Response.status(400)
|
||||
.entity(new AwsErrorResponse("UnknownOperationException", "Operation " + action + " is not supported."))
|
||||
.build();
|
||||
@@ -610,6 +617,101 @@ public class DynamoDbJsonHandler {
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handleEnableKinesisStreamingDestination(JsonNode request, String region) {
|
||||
String tableName = request.path("TableName").asText();
|
||||
String streamArn = request.path("StreamArn").asText();
|
||||
|
||||
TableDefinition table = dynamoDbService.describeTable(tableName, region);
|
||||
|
||||
String streamName = streamArn.substring(streamArn.lastIndexOf('/') + 1);
|
||||
try {
|
||||
kinesisService.describeStream(streamName, region);
|
||||
} catch (AwsException e) {
|
||||
throw new AwsException("ResourceNotFoundException",
|
||||
"Kinesis stream not found: " + streamArn, 400);
|
||||
}
|
||||
|
||||
Optional<KinesisStreamingDestination> existing = table.findKinesisStreamingDestination(streamArn);
|
||||
if (existing.isPresent() && "ACTIVE".equals(existing.get().getDestinationStatus())) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Table already has an active Kinesis streaming destination with this stream ARN", 400);
|
||||
}
|
||||
|
||||
if (existing.isPresent()) {
|
||||
existing.get().setDestinationStatus("ACTIVE");
|
||||
existing.get().setDestinationStatusDescription("Kinesis streaming is enabled for this table");
|
||||
} else {
|
||||
table.getKinesisStreamingDestinations().add(new KinesisStreamingDestination(streamArn));
|
||||
}
|
||||
|
||||
if (!table.isStreamEnabled()) {
|
||||
StreamDescription sd = dynamoDbStreamService.enableStream(
|
||||
tableName, table.getTableArn(), "NEW_AND_OLD_IMAGES", region);
|
||||
table.setStreamEnabled(true);
|
||||
table.setStreamArn(sd.getStreamArn());
|
||||
table.setStreamViewType("NEW_AND_OLD_IMAGES");
|
||||
}
|
||||
|
||||
dynamoDbService.persistTable(tableName, table, region);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.put("TableName", tableName);
|
||||
response.put("StreamArn", streamArn);
|
||||
response.put("DestinationStatus", "ACTIVE");
|
||||
response.put("DestinationStatusDescription", "Kinesis streaming is enabled for this table");
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handleDisableKinesisStreamingDestination(JsonNode request, String region) {
|
||||
String tableName = request.path("TableName").asText();
|
||||
String streamArn = request.path("StreamArn").asText();
|
||||
|
||||
TableDefinition table = dynamoDbService.describeTable(tableName, region);
|
||||
|
||||
Optional<KinesisStreamingDestination> existing = table.findKinesisStreamingDestination(streamArn);
|
||||
if (existing.isEmpty()) {
|
||||
throw new AwsException("ResourceNotFoundException",
|
||||
"Kinesis streaming destination not found for stream: " + streamArn, 400);
|
||||
}
|
||||
|
||||
if ("DISABLED".equals(existing.get().getDestinationStatus())) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Kinesis streaming destination is already disabled for stream: " + streamArn, 400);
|
||||
}
|
||||
|
||||
existing.get().setDestinationStatus("DISABLED");
|
||||
existing.get().setDestinationStatusDescription("Kinesis streaming is disabled for this table");
|
||||
dynamoDbService.persistTable(tableName, table, region);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.put("TableName", tableName);
|
||||
response.put("StreamArn", streamArn);
|
||||
response.put("DestinationStatus", "DISABLED");
|
||||
response.put("DestinationStatusDescription", "Kinesis streaming is disabled for this table");
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handleDescribeKinesisStreamingDestination(JsonNode request, String region) {
|
||||
String tableName = request.path("TableName").asText();
|
||||
TableDefinition table = dynamoDbService.describeTable(tableName, region);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.put("TableName", tableName);
|
||||
|
||||
ArrayNode destinations = objectMapper.createArrayNode();
|
||||
for (KinesisStreamingDestination dest : table.getKinesisStreamingDestinations()) {
|
||||
ObjectNode destNode = objectMapper.createObjectNode();
|
||||
destNode.put("StreamArn", dest.getStreamArn());
|
||||
destNode.put("DestinationStatus", dest.getDestinationStatus());
|
||||
destNode.put("DestinationStatusDescription", dest.getDestinationStatusDescription());
|
||||
destNode.put("ApproximateCreationDateTimePrecision",
|
||||
dest.getApproximateCreationDateTimePrecision());
|
||||
destinations.add(destNode);
|
||||
}
|
||||
response.set("KinesisDataStreamDestinations", destinations);
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private ObjectNode tableToNode(TableDefinition table) {
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
node.put("TableName", table.getTableName());
|
||||
|
||||
@@ -181,6 +181,10 @@ public class DynamoDbService {
|
||||
return table;
|
||||
}
|
||||
|
||||
public void persistTable(String tableName, TableDefinition table, String region) {
|
||||
tableStore.put(regionKey(region, tableName), table);
|
||||
}
|
||||
|
||||
public void deleteTable(String tableName) {
|
||||
deleteTable(tableName, regionResolver.getDefaultRegion());
|
||||
}
|
||||
@@ -241,9 +245,10 @@ public class DynamoDbService {
|
||||
persistItems(storageKey);
|
||||
LOG.debugv("Put item in {0}: key={1}", tableName, itemKey);
|
||||
|
||||
String eventName = existing == null ? "INSERT" : "MODIFY";
|
||||
if (streamService != null) {
|
||||
streamService.captureEvent(tableName,
|
||||
existing == null ? "INSERT" : "MODIFY", existing, item, table, region);
|
||||
streamService.captureEvent(tableName, eventName, existing, item, table, region);
|
||||
streamService.forwardToKinesisDestinations(eventName, existing, item, table, region);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -295,6 +300,7 @@ public class DynamoDbService {
|
||||
|
||||
if (streamService != null && removed != null) {
|
||||
streamService.captureEvent(tableName, "REMOVE", removed, null, table, region);
|
||||
streamService.forwardToKinesisDestinations("REMOVE", removed, null, table, region);
|
||||
}
|
||||
|
||||
return removed;
|
||||
@@ -371,6 +377,7 @@ public class DynamoDbService {
|
||||
|
||||
if (streamService != null) {
|
||||
streamService.captureEvent(tableName, "MODIFY", existing, item, table, region);
|
||||
streamService.forwardToKinesisDestinations("MODIFY", existing, item, table, region);
|
||||
}
|
||||
|
||||
return new UpdateResult(item, existing);
|
||||
@@ -813,6 +820,7 @@ public class DynamoDbService {
|
||||
JsonNode removed = items.remove(itemKey);
|
||||
if (removed != null && streamService != null) {
|
||||
streamService.captureEvent(table.getTableName(), "REMOVE", removed, null, table, region);
|
||||
streamService.forwardToKinesisDestinations("REMOVE", removed, null, table, region);
|
||||
}
|
||||
}
|
||||
persistItems(storageKey);
|
||||
|
||||
+91
-2
@@ -5,8 +5,10 @@ import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.DynamoDbStreamRecord;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.KeySchemaElement;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.KinesisStreamingDestination;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.StreamDescription;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.TableDefinition;
|
||||
import io.github.hectorvent.floci.services.kinesis.KinesisService;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
@@ -44,16 +46,24 @@ public class DynamoDbStreamService {
|
||||
private final AtomicLong sequenceCounter = new AtomicLong(0);
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
private KinesisService kinesisService;
|
||||
|
||||
@Inject
|
||||
public DynamoDbStreamService(ObjectMapper objectMapper, StorageFactory storageFactory) {
|
||||
public DynamoDbStreamService(ObjectMapper objectMapper, StorageFactory storageFactory,
|
||||
KinesisService kinesisService) {
|
||||
this(objectMapper, storageFactory.create("dynamodb", "dynamodb-tables.json",
|
||||
new TypeReference<Map<String, TableDefinition>>() {}));
|
||||
new TypeReference<Map<String, TableDefinition>>() {}), kinesisService);
|
||||
}
|
||||
|
||||
/** Package-private constructor for testing. */
|
||||
DynamoDbStreamService(ObjectMapper objectMapper, StorageBackend<String, TableDefinition> tableStore) {
|
||||
this(objectMapper, tableStore, null);
|
||||
}
|
||||
|
||||
DynamoDbStreamService(ObjectMapper objectMapper, StorageBackend<String, TableDefinition> tableStore,
|
||||
KinesisService kinesisService) {
|
||||
this.objectMapper = objectMapper;
|
||||
this.kinesisService = kinesisService;
|
||||
loadPersistedStreams(tableStore);
|
||||
}
|
||||
|
||||
@@ -165,6 +175,85 @@ public class DynamoDbStreamService {
|
||||
}
|
||||
}
|
||||
|
||||
public void forwardToKinesisDestinations(String eventName, JsonNode oldItem, JsonNode newItem,
|
||||
TableDefinition table, String region) {
|
||||
if (kinesisService == null) return;
|
||||
|
||||
List<KinesisStreamingDestination> destinations = table.getKinesisStreamingDestinations();
|
||||
if (destinations == null || destinations.isEmpty()) return;
|
||||
|
||||
Instant now = Instant.now();
|
||||
JsonNode sourceItem = newItem != null ? newItem : oldItem;
|
||||
ObjectNode keys = buildKeys(sourceItem, table);
|
||||
|
||||
for (KinesisStreamingDestination dest : destinations) {
|
||||
if (!"ACTIVE".equals(dest.getDestinationStatus())) continue;
|
||||
|
||||
try {
|
||||
ObjectNode payload = buildKinesisPayload(eventName, keys, newItem, oldItem,
|
||||
table.getTableName(), region, now);
|
||||
byte[] data = objectMapper.writeValueAsBytes(payload);
|
||||
|
||||
String partitionKey = extractPartitionKey(keys, table);
|
||||
String streamName = extractStreamName(dest.getStreamArn());
|
||||
|
||||
kinesisService.putRecord(streamName, data, partitionKey, region);
|
||||
LOG.debugv("Forwarded DynamoDB event to Kinesis stream {0}: {1} on {2}",
|
||||
streamName, eventName, table.getTableName());
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("Failed to forward DynamoDB event to Kinesis destination {0}: {1}",
|
||||
dest.getStreamArn(), e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private ObjectNode buildKinesisPayload(String eventName, JsonNode keys,
|
||||
JsonNode newImage, JsonNode oldImage,
|
||||
String tableName, String region, Instant timestamp) {
|
||||
ObjectNode payload = objectMapper.createObjectNode();
|
||||
payload.put("awsRegion", region);
|
||||
payload.put("eventID", UUID.randomUUID().toString());
|
||||
payload.put("eventName", eventName);
|
||||
payload.putNull("userIdentity");
|
||||
payload.put("recordFormat", "application/json");
|
||||
payload.put("tableName", tableName);
|
||||
payload.put("eventSource", "aws:dynamodb");
|
||||
|
||||
ObjectNode dynamodb = objectMapper.createObjectNode();
|
||||
dynamodb.put("ApproximateCreationDateTime", timestamp.toEpochMilli());
|
||||
if (keys != null) {
|
||||
dynamodb.set("Keys", keys);
|
||||
}
|
||||
if (newImage != null) {
|
||||
dynamodb.set("NewImage", newImage);
|
||||
}
|
||||
if (oldImage != null) {
|
||||
dynamodb.set("OldImage", oldImage);
|
||||
}
|
||||
dynamodb.put("SizeBytes", 0);
|
||||
dynamodb.put("ApproximateCreationDateTimePrecision", "MILLISECOND");
|
||||
payload.set("dynamodb", dynamodb);
|
||||
|
||||
return payload;
|
||||
}
|
||||
|
||||
private String extractPartitionKey(JsonNode keys, TableDefinition table) {
|
||||
if (keys == null || keys.isEmpty()) return "default";
|
||||
String pkName = table.getPartitionKeyName();
|
||||
JsonNode pkValue = keys.get(pkName);
|
||||
if (pkValue == null) return "default";
|
||||
if (pkValue.has("S")) return pkValue.get("S").asText();
|
||||
if (pkValue.has("N")) return pkValue.get("N").asText();
|
||||
if (pkValue.has("B")) return pkValue.get("B").asText();
|
||||
return pkValue.toString();
|
||||
}
|
||||
|
||||
private String extractStreamName(String streamArn) {
|
||||
int idx = streamArn.lastIndexOf('/');
|
||||
if (idx >= 0) return streamArn.substring(idx + 1);
|
||||
return streamArn;
|
||||
}
|
||||
|
||||
private ObjectNode buildKeys(JsonNode item, TableDefinition table) {
|
||||
ObjectNode keys = objectMapper.createObjectNode();
|
||||
if (item == null) {
|
||||
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package io.github.hectorvent.floci.services.dynamodb.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class KinesisStreamingDestination {
|
||||
|
||||
private String streamArn;
|
||||
private String destinationStatus;
|
||||
private String destinationStatusDescription;
|
||||
private String approximateCreationDateTimePrecision;
|
||||
|
||||
public KinesisStreamingDestination() {}
|
||||
|
||||
public KinesisStreamingDestination(String streamArn) {
|
||||
this.streamArn = streamArn;
|
||||
this.destinationStatus = "ACTIVE";
|
||||
this.destinationStatusDescription = "Kinesis streaming is enabled for this table";
|
||||
this.approximateCreationDateTimePrecision = "MILLISECOND";
|
||||
}
|
||||
|
||||
public String getStreamArn() { return streamArn; }
|
||||
public void setStreamArn(String streamArn) { this.streamArn = streamArn; }
|
||||
|
||||
public String getDestinationStatus() { return destinationStatus; }
|
||||
public void setDestinationStatus(String destinationStatus) { this.destinationStatus = destinationStatus; }
|
||||
|
||||
public String getDestinationStatusDescription() { return destinationStatusDescription; }
|
||||
public void setDestinationStatusDescription(String desc) { this.destinationStatusDescription = desc; }
|
||||
|
||||
public String getApproximateCreationDateTimePrecision() { return approximateCreationDateTimePrecision; }
|
||||
public void setApproximateCreationDateTimePrecision(String precision) {
|
||||
this.approximateCreationDateTimePrecision = precision;
|
||||
}
|
||||
}
|
||||
@@ -35,6 +35,7 @@ public class TableDefinition {
|
||||
private boolean streamEnabled;
|
||||
private String streamArn;
|
||||
private String streamViewType;
|
||||
private List<KinesisStreamingDestination> kinesisStreamingDestinations;
|
||||
|
||||
public TableDefinition() {
|
||||
this.keySchema = new ArrayList<>();
|
||||
@@ -42,6 +43,7 @@ public class TableDefinition {
|
||||
this.tags = new HashMap<>();
|
||||
this.globalSecondaryIndexes = new ArrayList<>();
|
||||
this.localSecondaryIndexes = new ArrayList<>();
|
||||
this.kinesisStreamingDestinations = new ArrayList<>();
|
||||
}
|
||||
|
||||
public TableDefinition(String tableName,
|
||||
@@ -66,6 +68,7 @@ public class TableDefinition {
|
||||
this.tags = new HashMap<>();
|
||||
this.globalSecondaryIndexes = new ArrayList<>();
|
||||
this.localSecondaryIndexes = new ArrayList<>();
|
||||
this.kinesisStreamingDestinations = new ArrayList<>();
|
||||
}
|
||||
|
||||
public String getTableName() { return tableName; }
|
||||
@@ -126,6 +129,19 @@ public class TableDefinition {
|
||||
public String getStreamViewType() { return streamViewType; }
|
||||
public void setStreamViewType(String streamViewType) { this.streamViewType = streamViewType; }
|
||||
|
||||
public List<KinesisStreamingDestination> getKinesisStreamingDestinations() {
|
||||
return kinesisStreamingDestinations != null ? kinesisStreamingDestinations : new ArrayList<>();
|
||||
}
|
||||
public void setKinesisStreamingDestinations(List<KinesisStreamingDestination> destinations) {
|
||||
this.kinesisStreamingDestinations = destinations != null ? destinations : new ArrayList<>();
|
||||
}
|
||||
|
||||
public Optional<KinesisStreamingDestination> findKinesisStreamingDestination(String streamArn) {
|
||||
return getKinesisStreamingDestinations().stream()
|
||||
.filter(d -> streamArn.equals(d.getStreamArn()))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
/** Returns the partition key attribute name. */
|
||||
public String getPartitionKeyName() {
|
||||
return keySchema.stream()
|
||||
|
||||
Reference in new issue
Block a user