Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(dynamodb): bill each BatchGetItem key like a GetItem (#6708)

TATSUNO “Taz” Yasuhiro0cc4984356c6
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 1d08525e60..69b36106c0 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
@@ -1628,6 +1628,8 @@ public class NativeDynamoDbJsonHandler {
ObjectNode response = objectMapper.createObjectNode();
ObjectNode responses = objectMapper.createObjectNode();
+ boolean capacityRequested = !"NONE".equals(request.path("ReturnConsumedCapacity").asText("NONE"));
+ Map<String, Double> unitsByTable = new LinkedHashMap<>();
for (Map.Entry<String, List<JsonNode>> entry : result.responses().entrySet()) {
String tblName = entry.getKey();
JsonNode tableRequest = requestItems.get(tblName);
@@ -1652,13 +1654,31 @@ public class NativeDynamoDbJsonHandler {
tableItems.add(projected);
}
responses.set(tblName, tableItems);
+ if (capacityRequested) {
+ unitsByTable.put(tblName, batchGetReadUnits(tblName, tableRequest, entry.getValue(), region));
+ }
}
response.set("Responses", responses);
response.set("UnprocessedKeys", objectMapper.createObjectNode());
- addBatchConsumedCapacity(response, request, items, false);
+ addBatchConsumedCapacity(response, request, unitsByTable);
return Response.ok(response).build();
}
+ /**
+ * Each key costs what a GetItem of the whole item costs, even a missing or projected one.
+ */
+ private double batchGetReadUnits(String tableName, JsonNode tableRequest, List<JsonNode> found,
+ String region) {
+ TableDefinition table = dynamoDbService.requireTableForItemCall(tableName, region);
+ boolean consistentRead = tableRequest != null && tableRequest.path("ConsistentRead").asBoolean(false);
+ int missingKeys = (tableRequest != null ? tableRequest.path("Keys").size() : 0) - found.size();
+ double units = missingKeys * readCapacityUnits(0, consistentRead);
+ for (JsonNode item : found) {
+ units += readCapacityUnits(DynamoDbItemSize.billedItemSize(table, item), consistentRead);
+ }
+ return units;
+ }
+
private Response handleUpdateTable(JsonNode request, String region) {
String tableName = request.path("TableName").asText();
DynamoDbService.validateBillingMode(request, "");
@@ -3100,25 +3120,23 @@ public class NativeDynamoDbJsonHandler {
}
/**
- * Builds a list-style ConsumedCapacity for batch operations.
+ * Builds the list-style ConsumedCapacity of BatchGetItem, one entry per table.
*/
private void addBatchConsumedCapacity(ObjectNode response, JsonNode request,
- Map<String, ?> tableItems, boolean isWrite) {
+ Map<String, Double> unitsByTable) {
String returnCC = request.path("ReturnConsumedCapacity").asText("NONE");
if ("NONE".equals(returnCC)) return;
ArrayNode ccArray = objectMapper.createArrayNode();
- for (String tableName : tableItems.keySet()) {
+ unitsByTable.forEach((tableName, units) -> {
ObjectNode cc = objectMapper.createObjectNode();
cc.put("TableName", DynamoDbTableNames.resolve(tableName));
- cc.put("CapacityUnits", isWrite ? 1.0 : 0.5);
+ cc.put("CapacityUnits", units);
if ("INDEXES".equals(returnCC)) {
- ObjectNode tableCap = objectMapper.createObjectNode();
- tableCap.put("CapacityUnits", isWrite ? 1.0 : 0.5);
- cc.set("Table", tableCap);
+ cc.set("Table", capacityUnitsNode(units, null));
}
ccArray.add(cc);
- }
+ });
response.set("ConsumedCapacity", ccArray);
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbConsumedCapacityIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbConsumedCapacityIntegrationTest.java
index 7ddec3b154..7201de5a50 100644
--- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbConsumedCapacityIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbConsumedCapacityIntegrationTest.java
@@ -643,6 +643,53 @@ class DynamoDbConsumedCapacityIntegrationTest {
.statusCode(200);
}
+ @Test
+ @Order(20)
+ void batchGetItemChargesEachKeyLikeAGetItemOfTheWholeItem() {
+ putItem("""
+ {"pk": {"S": "cc-bg-small"}}
+ """);
+ // 11 for the key and 3 for the pad's name bring it to 8193 bytes, three 4KB reads.
+ putItem("""
+ {"pk": {"S": "cc-bg-big"}, "pad": {"S": "%s"}}
+ """.formatted("x".repeat(8179)));
+ String keys = """
+ [{"pk": {"S": "cc-bg-small"}}, {"pk": {"S": "cc-bg-big"}}, {"pk": {"S": "cc-bg-missing"}}]
+ """;
+
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.BatchGetItem")
+ .contentType(CT)
+ .body("""
+ {
+ "RequestItems": {"%s": {"Keys": %s, "ConsistentRead": true, "ProjectionExpression": "pk"}},
+ "ReturnConsumedCapacity": "TOTAL"
+ }
+ """.formatted(TABLE, keys))
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("ConsumedCapacity", hasSize(1))
+ .body("ConsumedCapacity[0].TableName", equalTo(TABLE))
+ .body("ConsumedCapacity[0].CapacityUnits", equalTo(5.0f))
+ .body("ConsumedCapacity[0].Table", nullValue());
+
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.BatchGetItem")
+ .contentType(CT)
+ .body("""
+ {"RequestItems": {"%s": {"Keys": %s}}, "ReturnConsumedCapacity": "INDEXES"}
+ """.formatted(TABLE, keys))
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("ConsumedCapacity[0].CapacityUnits", equalTo(2.5f))
+ .body("ConsumedCapacity[0].Table.CapacityUnits", equalTo(2.5f))
+ .body("ConsumedCapacity[0].GlobalSecondaryIndexes", nullValue());
+ }
+
private static void putItem(String itemJson) {
given()
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbVectorBilledSizeIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbVectorBilledSizeIntegrationTest.java
index 06f9aa1a10..f3a501642b 100644
--- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbVectorBilledSizeIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbVectorBilledSizeIntegrationTest.java
@@ -116,6 +116,11 @@ class DynamoDbVectorBilledSizeIntegrationTest {
""".formatted(TABLE))
.statusCode(200)
.body("ConsumedCapacity[0].CapacityUnits", equalTo(2.0f));
+ ddb("BatchGetItem", """
+ {"RequestItems":{"%s":{"Keys":[{"pk":{"S":"r6"}}],"ConsistentRead":true}},"ReturnConsumedCapacity":"TOTAL"}
+ """.formatted(TABLE))
+ .statusCode(200)
+ .body("ConsumedCapacity[0].CapacityUnits", equalTo(1.0f));
}
@Test