diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/DynamoDbKinesisStreamingDestinationTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/DynamoDbKinesisStreamingDestinationTest.java new file mode 100644 index 0000000000..e97608a6ac --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/DynamoDbKinesisStreamingDestinationTest.java @@ -0,0 +1,158 @@ +package com.floci.test; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import software.amazon.awssdk.services.dynamodb.DynamoDbClient; +import software.amazon.awssdk.services.dynamodb.model.ApproximateCreationDateTimePrecision; +import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition; +import software.amazon.awssdk.services.dynamodb.model.BillingMode; +import software.amazon.awssdk.services.dynamodb.model.CreateTableRequest; +import software.amazon.awssdk.services.dynamodb.model.DeleteTableRequest; +import software.amazon.awssdk.services.dynamodb.model.DescribeKinesisStreamingDestinationRequest; +import software.amazon.awssdk.services.dynamodb.model.DescribeKinesisStreamingDestinationResponse; +import software.amazon.awssdk.services.dynamodb.model.DestinationStatus; +import software.amazon.awssdk.services.dynamodb.model.DisableKinesisStreamingDestinationRequest; +import software.amazon.awssdk.services.dynamodb.model.DisableKinesisStreamingDestinationResponse; +import software.amazon.awssdk.services.dynamodb.model.EnableKinesisStreamingConfiguration; +import software.amazon.awssdk.services.dynamodb.model.EnableKinesisStreamingDestinationRequest; +import software.amazon.awssdk.services.dynamodb.model.EnableKinesisStreamingDestinationResponse; +import software.amazon.awssdk.services.dynamodb.model.KeySchemaElement; +import software.amazon.awssdk.services.dynamodb.model.KeyType; +import software.amazon.awssdk.services.dynamodb.model.KinesisDataStreamDestination; +import software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType; +import software.amazon.awssdk.services.kinesis.KinesisClient; +import software.amazon.awssdk.services.kinesis.model.CreateStreamRequest; +import software.amazon.awssdk.services.kinesis.model.DeleteStreamRequest; +import software.amazon.awssdk.services.kinesis.model.DescribeStreamSummaryRequest; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Reads the Kinesis streaming destination responses back through the AWS SDK, which parses them + * into typed models: a status outside the enum lands as UNKNOWN_TO_SDK_VERSION, and a missing + * EnableKinesisStreamingConfiguration lands as null. Handcrafted HTTP cannot show either. + * + *

Floci used to ignore ApproximateCreationDateTimePrecision and always report MILLISECOND. + */ +@DisplayName("DynamoDB Kinesis Streaming Destination") +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class DynamoDbKinesisStreamingDestinationTest { + + private static final String TABLE_NAME = "sdk-kinesis-destination-table"; + private static final String STREAM_NAME = "sdk-kinesis-destination-stream"; + + private static DynamoDbClient ddb; + private static KinesisClient kinesis; + private static String streamArn; + + @BeforeAll + static void setup() { + ddb = TestFixtures.dynamoDbClient(); + kinesis = TestFixtures.kinesisClient(); + + kinesis.createStream(CreateStreamRequest.builder() + .streamName(STREAM_NAME) + .shardCount(1) + .build()); + streamArn = kinesis.describeStreamSummary(DescribeStreamSummaryRequest.builder() + .streamName(STREAM_NAME) + .build()) + .streamDescriptionSummary() + .streamARN(); + + ddb.createTable(CreateTableRequest.builder() + .tableName(TABLE_NAME) + .keySchema(KeySchemaElement.builder().attributeName("pk").keyType(KeyType.HASH).build()) + .attributeDefinitions(AttributeDefinition.builder().attributeName("pk") + .attributeType(ScalarAttributeType.S).build()) + .billingMode(BillingMode.PAY_PER_REQUEST) + .build()); + } + + @AfterAll + static void cleanup() { + try { + ddb.deleteTable(DeleteTableRequest.builder().tableName(TABLE_NAME).build()); + } catch (Exception ignored) { + // The table may already be gone; cleanup must not mask a test failure. + } + try { + kinesis.deleteStream(DeleteStreamRequest.builder().streamName(STREAM_NAME).build()); + } catch (Exception ignored) { + // Same for the stream. + } + if (ddb != null) { + ddb.close(); + } + if (kinesis != null) { + kinesis.close(); + } + } + + @Test + @Order(1) + void enableEchoesTheRequestedPrecision() { + EnableKinesisStreamingDestinationResponse response = ddb.enableKinesisStreamingDestination( + EnableKinesisStreamingDestinationRequest.builder() + .tableName(TABLE_NAME) + .streamArn(streamArn) + .enableKinesisStreamingConfiguration(EnableKinesisStreamingConfiguration.builder() + .approximateCreationDateTimePrecision( + ApproximateCreationDateTimePrecision.MICROSECOND) + .build()) + .build()); + + assertThat(response.tableName()).isEqualTo(TABLE_NAME); + assertThat(response.streamArn()).isEqualTo(streamArn); + assertThat(response.destinationStatus()) + .as("a status the SDK does not know would be UNKNOWN_TO_SDK_VERSION") + .isEqualTo(DestinationStatus.ACTIVE); + assertThat(response.enableKinesisStreamingConfiguration()) + .as("the response must carry the configuration, not just the status") + .isNotNull(); + assertThat(response.enableKinesisStreamingConfiguration().approximateCreationDateTimePrecision()) + .isEqualTo(ApproximateCreationDateTimePrecision.MICROSECOND); + } + + @Test + @Order(2) + void describeReportsTheStoredPrecision() { + DescribeKinesisStreamingDestinationResponse response = ddb.describeKinesisStreamingDestination( + DescribeKinesisStreamingDestinationRequest.builder().tableName(TABLE_NAME).build()); + + assertThat(response.tableName()).isEqualTo(TABLE_NAME); + assertThat(response.kinesisDataStreamDestinations()).hasSize(1); + + KinesisDataStreamDestination destination = response.kinesisDataStreamDestinations().get(0); + assertThat(destination.streamArn()).isEqualTo(streamArn); + assertThat(destination.destinationStatus()).isEqualTo(DestinationStatus.ACTIVE); + assertThat(destination.destinationStatusDescription()).isNotBlank(); + assertThat(destination.approximateCreationDateTimePrecision()) + .as("the precision the destination was enabled with, not the MILLISECOND default") + .isEqualTo(ApproximateCreationDateTimePrecision.MICROSECOND); + } + + @Test + @Order(3) + void disableReportsTheStoredPrecision() { + DisableKinesisStreamingDestinationResponse response = ddb.disableKinesisStreamingDestination( + DisableKinesisStreamingDestinationRequest.builder() + .tableName(TABLE_NAME) + .streamArn(streamArn) + .build()); + + assertThat(response.tableName()).isEqualTo(TABLE_NAME); + assertThat(response.streamArn()).isEqualTo(streamArn); + assertThat(response.destinationStatus()).isEqualTo(DestinationStatus.DISABLED); + assertThat(response.enableKinesisStreamingConfiguration()) + .as("AWS answers Disable with the destination's configuration as well") + .isNotNull(); + assertThat(response.enableKinesisStreamingConfiguration().approximateCreationDateTimePrecision()) + .isEqualTo(ApproximateCreationDateTimePrecision.MICROSECOND); + } +} diff --git a/docs/services/dynamodb.md b/docs/services/dynamodb.md index 6eb99b1660..5b25fec9b8 100644 --- a/docs/services/dynamodb.md +++ b/docs/services/dynamodb.md @@ -392,13 +392,18 @@ AWS CDC envelope (`eventName`, `dynamodb.Keys`, `NewImage`/`OldImage`, `Approxim `ApproximateCreationDateTime` follows the destination's `EnableKinesisStreamingConfiguration.ApproximateCreationDateTimePrecision`: epoch milliseconds for -`MILLISECOND` (the default) and epoch microseconds for `MICROSECOND`. The precision is returned by -`DescribeKinesisStreamingDestination` and stamped on each record as -`dynamodb.ApproximateCreationDateTimePrecision`. +`MILLISECOND` (the default) and epoch microseconds for `MICROSECOND`. The precision is stamped on each +record as `dynamodb.ApproximateCreationDateTimePrecision`, and reported by +`DescribeKinesisStreamingDestination` as well as in the `EnableKinesisStreamingConfiguration` member of +the `EnableKinesisStreamingDestination` and `DisableKinesisStreamingDestination` responses. Enabling a Kinesis streaming destination does not change the table's DynamoDB Streams setting. Kinesis forwarding works whether or not `StreamSpecification.StreamEnabled` is set. +Both calls take effect at once, so `EnableKinesisStreamingDestination` answers `ACTIVE` and +`DisableKinesisStreamingDestination` answers `DISABLED`. AWS reports the transitional `ENABLING` and +`DISABLING` first, because it enables and disables the destination in the background. + ### Delivery contract Forwarding is **bounded best-effort with in-process retry**. A write is never blocked or failed by diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java index 7bb2c71a92..4af0dead3f 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java @@ -9,6 +9,7 @@ import io.github.hectorvent.floci.core.common.AwsErrorResponse; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.services.dynamodb.NativeDynamoDbTableService.CreateTableRequest; import io.github.hectorvent.floci.services.dynamodb.NativeDynamoDbTableService.GsiThroughputUpdate; +import io.github.hectorvent.floci.services.dynamodb.NativeDynamoDbTableService.KinesisDestination; import io.github.hectorvent.floci.services.dynamodb.NativeDynamoDbTableService.OnDemandThroughput; import io.github.hectorvent.floci.services.dynamodb.NativeDynamoDbTableService.TableSettings; import io.github.hectorvent.floci.services.dynamodb.NativeDynamoDbTableService.Throughput; @@ -2253,31 +2254,31 @@ public class NativeDynamoDbJsonHandler { 400); } - String resolvedTableName = tableService.enableKinesisStreamingDestination( - tableName, streamArn, precision, region); - - ObjectNode response = objectMapper.createObjectNode(); - response.put("TableName", resolvedTableName); - 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(); + return Response.ok(kinesisDestinationResponse( + tableService.enableKinesisStreamingDestination(tableName, streamArn, precision, region))).build(); } private Response handleDisableKinesisStreamingDestination(JsonNode request, String region) { String tableName = request.path("TableName").asText(); String streamArn = request.path("StreamArn").asText(); - String resolvedTableName = tableService.disableKinesisStreamingDestination(tableName, streamArn, region); + return Response.ok(kinesisDestinationResponse( + tableService.disableKinesisStreamingDestination(tableName, streamArn, region))).build(); + } + /** + * The shape Enable and Disable both answer with: TableName, StreamArn, DestinationStatus and + * EnableKinesisStreamingConfiguration. DestinationStatusDescription belongs to Describe only. + */ + private ObjectNode kinesisDestinationResponse(KinesisDestination result) { ObjectNode response = objectMapper.createObjectNode(); - response.put("TableName", resolvedTableName); - response.put("StreamArn", streamArn); - response.put("DestinationStatus", "DISABLED"); - response.put("DestinationStatusDescription", "Kinesis streaming is disabled for this table"); - return Response.ok(response).build(); + response.put("TableName", result.tableName()); + response.put("StreamArn", result.streamArn()); + response.put("DestinationStatus", result.destinationStatus()); + response.putObject("EnableKinesisStreamingConfiguration") + .put("ApproximateCreationDateTimePrecision", + result.approximateCreationDateTimePrecision()); + return response; } private Response handleDescribeKinesisStreamingDestination(JsonNode request, String region) { diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbTableService.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbTableService.java index 93ed32aaba..f743fe3541 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbTableService.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbTableService.java @@ -65,6 +65,14 @@ public class NativeDynamoDbTableService { List replicaAdds, List replicaRemoves, List replicaUpdates, TableSettings settings) {} + /** + * What one Enable or Disable call applied. The values are the ones the call itself wrote, not a + * read-back of the stored {@link KinesisStreamingDestination}: that object is shared across + * requests, so reading it again could report a status a concurrent call wrote in the meantime. + */ + record KinesisDestination(String tableName, String streamArn, String destinationStatus, + String approximateCreationDateTimePrecision) {} + private final DynamoDbService dynamoDbService; private final DynamoDbStreamService streamService; private final KinesisService kinesisService; @@ -279,9 +287,9 @@ public class NativeDynamoDbTableService { * when one exists. * * @param precision an already validated ApproximateCreationDateTimePrecision - * @return the resolved (canonical) table name + * @return a snapshot of what this call left on the destination, with the canonical table name */ - String enableKinesisStreamingDestination(String tableName, String streamArn, String precision, String region) { + KinesisDestination enableKinesisStreamingDestination(String tableName, String streamArn, String precision, String region) { TableDefinition table = dynamoDbService.describeTable(tableName, region); String resolvedTableName = table.getTableName(); @@ -299,26 +307,33 @@ public class NativeDynamoDbTableService { "Table already has an active Kinesis streaming destination with this stream ARN", 400); } + KinesisStreamingDestination destination; if (existing.isPresent()) { - existing.get().setDestinationStatus("ACTIVE"); - existing.get().setDestinationStatusDescription("Kinesis streaming is enabled for this table"); - existing.get().setApproximateCreationDateTimePrecision(precision); + 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"); } else { - table.getKinesisStreamingDestinations().add(new KinesisStreamingDestination(streamArn, precision)); + destination = new KinesisStreamingDestination(streamArn, precision); + table.getKinesisStreamingDestinations().add(destination); } + KinesisDestination enabled = new KinesisDestination(resolvedTableName, streamArn, "ACTIVE", 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); - return resolvedTableName; + return enabled; } /** * Turns off forwarding to a Kinesis stream and stops the forwarder draining it. * - * @return the resolved (canonical) table name + * @return a snapshot of what this call left on the destination, with the canonical table name */ - String disableKinesisStreamingDestination(String tableName, String streamArn, String region) { + KinesisDestination disableKinesisStreamingDestination(String tableName, String streamArn, String region) { TableDefinition table = dynamoDbService.describeTable(tableName, region); String resolvedTableName = table.getTableName(); @@ -333,12 +348,16 @@ public class NativeDynamoDbTableService { "Kinesis streaming destination is already disabled for stream: " + streamArn, 400); } - existing.get().setDestinationStatus("DISABLED"); - existing.get().setDestinationStatusDescription("Kinesis streaming is disabled for this table"); + KinesisStreamingDestination destination = existing.get(); + // Disable does not take a precision, so the destination's own is read once, here. + String precision = destination.getApproximateCreationDateTimePrecision(); + destination.setDestinationStatus("DISABLED"); + destination.setDestinationStatusDescription("Kinesis streaming is disabled for this table"); + KinesisDestination disabled = new KinesisDestination(resolvedTableName, streamArn, "DISABLED", precision); dynamoDbService.persistTable(resolvedTableName, table, region); // Stop forwarding and discard buffered CDC records for this now-disabled destination. dynamoDbService.onKinesisStreamingDestinationDisabled(resolvedTableName, streamArn, region); - return resolvedTableName; + return disabled; } /** The AWS managed key a table reports when SSE is KMS and no customer key was given. */ diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbKinesisStreamingIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbKinesisStreamingIntegrationTest.java index cfb19edcfd..e9ea90a45a 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbKinesisStreamingIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbKinesisStreamingIntegrationTest.java @@ -106,7 +106,11 @@ class DynamoDbKinesisStreamingIntegrationTest { .statusCode(200) .body("TableName", equalTo("StreamingTable")) .body("StreamArn", equalTo(kinesisStreamArn)) - .body("DestinationStatus", equalTo("ACTIVE")); + .body("DestinationStatus", equalTo("ACTIVE")) + .body("EnableKinesisStreamingConfiguration.ApproximateCreationDateTimePrecision", + equalTo("MILLISECOND")) + // AWS carries DestinationStatusDescription on Describe only, not on Enable. + .body("$", not(hasKey("DestinationStatusDescription"))); } @Test @@ -344,7 +348,13 @@ class DynamoDbKinesisStreamingIntegrationTest { .post("/") .then() .statusCode(200) - .body("DestinationStatus", equalTo("DISABLED")); + .body("TableName", equalTo("StreamingTable")) + .body("StreamArn", equalTo(kinesisStreamArn)) + .body("DestinationStatus", equalTo("DISABLED")) + // AWS answers Disable with the destination's configuration too. + .body("EnableKinesisStreamingConfiguration.ApproximateCreationDateTimePrecision", + equalTo("MILLISECOND")) + .body("$", not(hasKey("DestinationStatusDescription"))); } @Test @@ -657,7 +667,10 @@ class DynamoDbKinesisStreamingIntegrationTest { .when() .post("/") .then() - .statusCode(200); + .statusCode(200) + // Disable reports the precision the destination still carries, not the default. + .body("EnableKinesisStreamingConfiguration.ApproximateCreationDateTimePrecision", + equalTo("MICROSECOND")); // No configuration on re-enable means the AWS default, not the previously stored MICROSECOND. given() diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbTableServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbTableServiceTest.java index d3b193fc86..2169290a48 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbTableServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbTableServiceTest.java @@ -337,10 +337,15 @@ class NativeDynamoDbTableServiceTest { void kinesisDestinationTransitionsPersistAndRepeatedTransitionsAreRejected() { TableDefinition created = tableService.createTable(createRequest(null, NO_SETTINGS), "ACTIVE", REGION); - String resolved = tableService.enableKinesisStreamingDestination( + NativeDynamoDbTableService.KinesisDestination resolved = tableService.enableKinesisStreamingDestination( created.getTableArn(), STREAM_ARN, KinesisStreamingDestination.PRECISION_MICROSECOND, REGION); - assertEquals(TABLE, resolved, "an ARN input resolves to the canonical table name"); + assertEquals(TABLE, resolved.tableName(), "an ARN input resolves to the canonical table name"); + assertEquals(STREAM_ARN, resolved.streamArn()); + assertEquals("ACTIVE", resolved.destinationStatus()); + assertEquals(KinesisStreamingDestination.PRECISION_MICROSECOND, + resolved.approximateCreationDateTimePrecision(), + "the snapshot carries the precision the response echoes"); verify(kinesisService).describeStream(STREAM_NAME, REGION); KinesisStreamingDestination enabled = reloadFromDisk().findKinesisStreamingDestination(STREAM_ARN) .orElseThrow(); @@ -352,7 +357,13 @@ class NativeDynamoDbTableServiceTest { TABLE, STREAM_ARN, KinesisStreamingDestination.PRECISION_MILLISECOND, REGION), "Table already has an active Kinesis streaming destination with this stream ARN"); - assertEquals(TABLE, tableService.disableKinesisStreamingDestination(TABLE, STREAM_ARN, REGION)); + NativeDynamoDbTableService.KinesisDestination afterDisable = + tableService.disableKinesisStreamingDestination(TABLE, STREAM_ARN, REGION); + assertEquals(TABLE, afterDisable.tableName()); + assertEquals("DISABLED", afterDisable.destinationStatus()); + assertEquals(KinesisStreamingDestination.PRECISION_MICROSECOND, + afterDisable.approximateCreationDateTimePrecision(), + "disabling keeps the stored precision so the response can report it"); TableDefinition disabled = reloadFromDisk(); assertEquals(1, disabled.getKinesisStreamingDestinations().size()); assertEquals("DISABLED", disabled.findKinesisStreamingDestination(STREAM_ARN).orElseThrow()