feat(dynamodb): update Kinesis destination precision (#9072)

* feat(dynamodb): update Kinesis destination precision
* fix(dynamodb): reuse validation for Kinesis precision updates

Co-authored-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
Jackson PriceandAndre Kurait authored and GitHub committed 2026-11-20 06:54:51 -04:00
1 parent 546065dc53
commit ab7fc6f81f
11 files changed
+663 -15

No files matched your search

@@ -11,6 +11,7 @@ import com.fasterxml.jackson.dataformat.cbor.CBORGenerator;
import com.google.gson.JsonParseException;
import io.floci.aws.services.cloudwatch.metrics.CloudWatchMetricsJsonHandler;
import io.floci.aws.services.dynamodb.DynamoDbJsonHandler;
import io.floci.aws.services.dynamodb.DynamoDbRequestShapes;
import io.floci.aws.services.dynamodb.DynamoDbStreamsJsonHandler;
import io.floci.aws.services.eventbridgev2.EventBridgeV2Handler;
import io.floci.aws.services.kinesis.KinesisJsonHandler;
@@ -446,6 +447,7 @@ public class AwsJsonCborController {
if (targetMatch.prefix().startsWith("DynamoDBStreams_")) {
yield dynamoDbStreamsJsonHandler.handle(action, request, region);
}
DynamoDbRequestShapes.check(action, request);
yield dynamoDbJsonHandler.handle(action, request, region);
}
case "sqs" -> sqsJsonHandler.handle(action, request, region);
@@ -518,6 +520,7 @@ public class AwsJsonCborController {
if ("DynamoDB Streams".equals(serviceId) || serviceId.startsWith("DynamoDBStreams")) {
yield dynamoDbStreamsJsonHandler.handle(operation, request, region);
}
DynamoDbRequestShapes.check(operation, request);
yield dynamoDbJsonHandler.handle(operation, request, region);
}
case "sqs" -> sqsJsonHandler.handle(operation, request, region);
@@ -18,6 +18,7 @@ import io.floci.aws.core.common.Digests;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.common.RequestScopes;
import io.floci.aws.core.common.SmithyValidationErrors;
import io.floci.aws.core.common.StripedLocks;
import io.floci.aws.core.common.Totp;
import io.floci.aws.core.resource.ExplorerResource;
import io.floci.aws.core.storage.AccountAwareStorageBackend;
@@ -121,6 +122,7 @@ public class DynamoDbService {
static final String DEFAULT_STREAM_VIEW_TYPE = "NEW_AND_OLD_IMAGES";
private final StorageBackend<String, TableDefinition> tableStore;
private final StripedLocks tableControlLocks = new StripedLocks(256);
private final StorageBackend<String, Map<String, JsonNode>> itemStore;
private final StorageBackend<String, ExportDescription> exportStore;
private final StorageBackend<String, ImportTableDescription> importStore;
@@ -1019,7 +1021,19 @@ public class DynamoDbService {
return table;
}
/** Serializes Kinesis destination mutations with deletion for the same account, Region and table. */
Object tableControlLock(String tableName, String region) {
String storageKey = regionKey(region, canonicalTableName(region, tableName));
return tableControlLocks.lockFor(scopedItemsKey(storageKey));
}
public void deleteTable(String tableName, String region) {
synchronized (tableControlLock(tableName, region)) {
deleteTableLocked(tableName, region);
}
}
private void deleteTableLocked(String tableName, String region) {
String canonicalTableName = canonicalTableName(region, tableName);
String storageKey = regionKey(region, canonicalTableName);
TableDefinition table = tableStore.get(storageKey)
@@ -5230,7 +5244,7 @@ public class DynamoDbService {
return results;
}
private AwsException resourceNotFoundException(String tableName) {
static AwsException resourceNotFoundException(String tableName) {
return new AwsException("ResourceNotFoundException",
"Requested resource not found: Table: " + tableName + " not found", 400);
}
@@ -149,6 +149,7 @@ public class NativeDynamoDbJsonHandler {
case "GetResourcePolicy" -> handleGetResourcePolicy(request, region);
case "DeleteResourcePolicy" -> handleDeleteResourcePolicy(request, region);
case "EnableKinesisStreamingDestination" -> handleEnableKinesisStreamingDestination(request, region);
case "UpdateKinesisStreamingDestination" -> handleUpdateKinesisStreamingDestination(request, region);
case "DisableKinesisStreamingDestination" -> handleDisableKinesisStreamingDestination(request, region);
case "DescribeKinesisStreamingDestination" -> handleDescribeKinesisStreamingDestination(request, region);
case "ExportTableToPointInTime" -> handleExportTable(request, region);
@@ -3515,13 +3516,8 @@ public class NativeDynamoDbJsonHandler {
.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 SmithyValidationErrors.validationException(
"Value '" + precision
+ "' at 'enableKinesisStreamingConfiguration.approximateCreationDateTimePrecision' "
+ "failed to satisfy constraint: Member must satisfy enum value set: [MILLISECOND, MICROSECOND]");
}
validateKinesisPrecision(precision,
"enableKinesisStreamingConfiguration.approximateCreationDateTimePrecision");
return Response.ok(kinesisDestinationResponse(
tableService.enableKinesisStreamingDestination(tableName, streamArn, precision, region))).build();
@@ -3535,6 +3531,51 @@ public class NativeDynamoDbJsonHandler {
tableService.disableKinesisStreamingDestination(tableName, streamArn, region))).build();
}
private Response handleUpdateKinesisStreamingDestination(JsonNode request, String region) {
String tableName = kinesisUpdateString(request, "TableName", "tableName", 1);
String streamArn = kinesisUpdateString(request, "StreamArn", "streamArn", 37);
String precision = kinesisUpdatePrecision(request);
KinesisDestination result = tableService.updateKinesisStreamingDestination(
tableName, streamArn, precision, region);
ObjectNode response = objectMapper.createObjectNode();
response.put("TableName", result.tableName());
response.put("StreamArn", result.streamArn());
response.put("DestinationStatus", result.destinationStatus());
response.putObject("UpdateKinesisStreamingConfiguration")
.put("ApproximateCreationDateTimePrecision", result.approximateCreationDateTimePrecision());
return Response.ok(response).build();
}
private static String kinesisUpdatePrecision(JsonNode request) {
JsonNode precisionNode = request.path("UpdateKinesisStreamingConfiguration")
.path("ApproximateCreationDateTimePrecision");
String precision = precisionNode.isMissingNode() || precisionNode.isNull() ? null : precisionNode.asText();
validateKinesisPrecision(precision,
"updateKinesisStreamingConfiguration.approximateCreationDateTimePrecision");
return precision;
}
private static void validateKinesisPrecision(String precision, String member) {
if (precision != null && !KinesisStreamingDestination.PRECISION_MILLISECOND.equals(precision)
&& !KinesisStreamingDestination.PRECISION_MICROSECOND.equals(precision)) {
throw SmithyValidationErrors.validationException(SmithyValidationErrors.notInEnum(precision, member,
List.of(KinesisStreamingDestination.PRECISION_MILLISECOND,
KinesisStreamingDestination.PRECISION_MICROSECOND)));
}
}
private static String kinesisUpdateString(JsonNode request, String member, String path, int minimumLength) {
JsonNode value = request.path(member);
if (value.isMissingNode() || value.isNull()) {
throw SmithyValidationErrors.validationException(SmithyValidationErrors.notNull(path));
}
String violation = SmithyValidationErrors.lengthOutOfRange(path, value.asText(), minimumLength, 1024);
if (violation != null) {
throw SmithyValidationErrors.validationException(violation);
}
return value.asText();
}
/**
* The shape Enable and Disable both answer with: TableName, StreamArn, DestinationStatus and
* EnableKinesisStreamingConfiguration. DestinationStatusDescription belongs to Describe only.
@@ -539,6 +539,13 @@ public class NativeDynamoDbTableService {
*/
public KinesisDestination enableKinesisStreamingDestination(String tableName, String streamArn, String precision,
String region) {
synchronized (dynamoDbService.tableControlLock(tableName, region)) {
return enableKinesisStreamingDestinationLocked(tableName, streamArn, precision, region);
}
}
private KinesisDestination enableKinesisStreamingDestinationLocked(String tableName, String streamArn,
String precision, String region) {
TableDefinition table = dynamoDbService.describeTable(tableName, region);
String resolvedTableName = table.getTableName();
@@ -558,9 +565,6 @@ public class NativeDynamoDbTableService {
KinesisStreamingDestination destination;
if (existing.isPresent()) {
destination = existing.get();
// The precision goes in before the status. A concurrent Disable gets past its
// already-disabled check only once the status reads ACTIVE, so writing the status last
// means that call can no longer read the precision this one is replacing.
destination.setApproximateCreationDateTimePrecision(precision);
destination.setDestinationStatusDescription("Kinesis streaming is enabled for this table");
destination.setDestinationStatus("ACTIVE");
@@ -582,6 +586,12 @@ public class NativeDynamoDbTableService {
* @return a snapshot of what this call left on the destination, with the canonical table name
*/
public KinesisDestination disableKinesisStreamingDestination(String tableName, String streamArn, String region) {
synchronized (dynamoDbService.tableControlLock(tableName, region)) {
return disableKinesisStreamingDestinationLocked(tableName, streamArn, region);
}
}
private KinesisDestination disableKinesisStreamingDestinationLocked(String tableName, String streamArn, String region) {
TableDefinition table = dynamoDbService.describeTable(tableName, region);
String resolvedTableName = table.getTableName();
@@ -613,12 +623,22 @@ public class NativeDynamoDbTableService {
* does. Forwarding never stops: records already captured are still delivered, and the forwarder
* stamps each later write with the new precision.
*
* @param precision an already validated ApproximateCreationDateTimePrecision
* @param precision an already validated ApproximateCreationDateTimePrecision, or null to retain it
* @return a snapshot of what this call left on the destination, with the canonical table name
*/
public KinesisDestination updateKinesisStreamingDestination(String tableName, String streamArn, String precision,
String region) {
synchronized (dynamoDbService.tableControlLock(tableName, region)) {
return updateKinesisStreamingDestinationLocked(tableName, streamArn, precision, region);
}
}
private KinesisDestination updateKinesisStreamingDestinationLocked(String tableName, String streamArn,
String precision, String region) {
TableDefinition table = dynamoDbService.describeTable(tableName, region);
if (AwsArnUtils.looksLikeArn(tableName) && !tableName.equals(table.getTableArn())) {
throw DynamoDbService.resourceNotFoundException(table.getTableName());
}
String resolvedTableName = table.getTableName();
Optional<KinesisStreamingDestination> existing = table.findKinesisStreamingDestination(streamArn);
@@ -631,8 +651,10 @@ public class NativeDynamoDbTableService {
"Kinesis streaming destination is not active for stream: " + streamArn, 400);
}
existing.get().setApproximateCreationDateTimePrecision(precision);
KinesisDestination updated = new KinesisDestination(resolvedTableName, streamArn, "ACTIVE", precision);
KinesisStreamingDestination destination = existing.get();
String effectivePrecision = precision != null ? precision : destination.getApproximateCreationDateTimePrecision();
destination.setApproximateCreationDateTimePrecision(effectivePrecision);
KinesisDestination updated = new KinesisDestination(resolvedTableName, streamArn, "ACTIVE", effectivePrecision);
dynamoDbService.persistTable(resolvedTableName, table, region);
return updated;
}