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

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

DynamoDB bills each key of a BatchGetItem as a GetItem of the whole item. That is half a
unit per 4KB, doubled for a consistent read, with the minimum charged for a key that finds
nothing. A projection does not lower it, and a vector attribute counts at its billed size.

* fix(dynamodb): size BatchGetItem items only when capacity is requested
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-10-26 22:05:20 -04:00
1 parent 825564a3d0
commit 0cc4984356
3 files changed
+79 -9

No files matched your search

@@ -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);
}
@@ -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")
@@ -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