From 3958be1d0ead23b1adcc2f2b6711df235dad6191 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?TATSUNO=20=E2=80=9CTaz=E2=80=9D=20Yasuhiro?= Date: Sun, 20 Sep 2026 23:08:50 +0900 Subject: [PATCH] fix(dynamodb): enforce the 400KB item limit on every update surface (#3991) * fix(dynamodb): enforce the 400KB item limit on every update surface * docs(dynamodb): correct the update size exception javadoc --- .../services/dynamodb/DynamoDbItemSize.java | 76 +++++ .../dynamodb/DynamoDbPartiQLHandler.java | 3 +- .../services/dynamodb/DynamoDbService.java | 101 +++++-- .../services/dynamodb/UpdateSizeRule.java | 8 + .../UpdatedItemTooLargeException.java | 11 + .../dynamodb/DynamoDbItemSizeServiceTest.java | 284 ++++++++++++++++++ 6 files changed, 460 insertions(+), 23 deletions(-) create mode 100644 src/main/java/io/github/hectorvent/floci/services/dynamodb/UpdateSizeRule.java create mode 100644 src/main/java/io/github/hectorvent/floci/services/dynamodb/UpdatedItemTooLargeException.java create mode 100644 src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbItemSizeServiceTest.java diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbItemSize.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbItemSize.java index 8e8eca5de5..1be75fa0ae 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbItemSize.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbItemSize.java @@ -5,6 +5,10 @@ import io.github.hectorvent.floci.core.common.AwsException; import java.nio.charset.StandardCharsets; import java.util.Base64; +import java.util.Iterator; +import java.util.Locale; +import java.util.Map; +import java.util.Set; /** * Calculates DynamoDB item size and enforces the 400KB limit. @@ -13,6 +17,15 @@ final class DynamoDbItemSize { static final int MAX_ITEM_SIZE = 400 * 1024; // 400 KB = 409,600 bytes + static final String UPDATE_EXCEEDED = "Item size to update has exceeded the maximum allowed size"; + + // UpdateItem measures what the statement writes, name and value, not the item it stores, + // and charges these on top. Every other surface measures the finished item. + private static final int UPDATE_COST = 3; + private static final int SET_OR_ADD_COST = 19; + private static final int REMOVE_OR_DELETE_COST = 2; + private static final int LIST_INDEX_COST = 1; + private DynamoDbItemSize() {} /** @@ -26,6 +39,69 @@ final class DynamoDbItemSize { } } + static boolean updatedItemWithinLimit(JsonNode item) { + return calculateItemSize(item) <= MAX_ITEM_SIZE; + } + + static boolean updateItemWithinLimit(JsonNode item, Set writtenAttributes, int actionCost) { + int total = 0; + int written = 0; + Iterator> fields = item.fields(); + while (fields.hasNext()) { + Map.Entry entry = fields.next(); + int size = utf8Length(entry.getKey()) + attributeValueSize(entry.getValue()); + total += size; + if (writtenAttributes.contains(entry.getKey())) { + written += size; + } + } + return total <= MAX_ITEM_SIZE && written + actionCost <= MAX_ITEM_SIZE; + } + + static int updateExpressionCost(String expression) { + int cost = UPDATE_COST; + String remaining = expression.trim().replaceAll("\\s+", " "); + while (!remaining.isEmpty()) { + String keyword = remaining.substring(0, Math.max(remaining.indexOf(' '), 0)).toUpperCase(Locale.ROOT); + String body = remaining.substring(keyword.length()).trim(); + int nextClause = DynamoDbService.findNextClauseKeyword(body); + String actions = nextClause < 0 ? body : body.substring(0, nextClause); + remaining = nextClause < 0 ? "" : body.substring(nextClause); + while (!actions.isBlank()) { + int comma = DynamoDbService.findNextComma(actions); + cost += actionCost(keyword, comma < 0 ? actions : actions.substring(0, comma)); + actions = comma < 0 ? "" : actions.substring(comma + 1); + } + } + return cost; + } + + static int attributeUpdatesCost(JsonNode attributeUpdates) { + int cost = UPDATE_COST; + if (attributeUpdates == null || !attributeUpdates.isObject()) { + return cost; + } + for (JsonNode update : attributeUpdates) { + cost += "DELETE".equals(update.path("Action").asText("PUT")) + ? REMOVE_OR_DELETE_COST : SET_OR_ADD_COST; + } + return cost; + } + + private static int actionCost(String clause, String action) { + return switch (clause) { + case "SET" -> SET_OR_ADD_COST + (writesListElement(action) ? LIST_INDEX_COST : 0); + case "ADD" -> SET_OR_ADD_COST; + default -> REMOVE_OR_DELETE_COST; + }; + } + + private static boolean writesListElement(String action) { + int bracket = action.indexOf('['); + int assignment = action.indexOf('='); + return bracket >= 0 && (assignment < 0 || bracket < assignment); + } + static int calculateItemSize(JsonNode item) { if (item == null || !item.isObject()) return 0; int total = 0; diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLHandler.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLHandler.java index 84f03fb608..916ec81f4a 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLHandler.java @@ -504,7 +504,8 @@ class DynamoDbPartiQLHandler { DynamoDbService.UpdateResult result = service.updateItem(stmt.table(), key, null, ue, ean.isEmpty() ? null : ean.toNode(mapper), - eav.isEmpty() ? null : eav.toNode(mapper), returnValues, ce, region, "NONE"); + eav.isEmpty() ? null : eav.toNode(mapper), returnValues, ce, region, "NONE", + UpdateSizeRule.FINISHED_ITEM); return itemsResponse(returnedAttributes(stmt.returning(), result)); } diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java index 566aa28625..41603c4220 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java @@ -781,20 +781,20 @@ public class DynamoDbService implements ResourceProvider { JsonNode expressionAttrNames, JsonNode expressionAttrValues, String returnValues, String conditionExpression, String region, String returnValuesOnConditionCheckFailure) { - return updateItemInternal(tableName, key, attributeUpdates, updateExpression, - expressionAttrNames, expressionAttrValues, returnValues, - conditionExpression, region, returnValuesOnConditionCheckFailure, true); + return updateItem(tableName, key, attributeUpdates, updateExpression, expressionAttrNames, + expressionAttrValues, returnValues, conditionExpression, region, + returnValuesOnConditionCheckFailure, UpdateSizeRule.UPDATE_ITEM); } - private UpdateResult updateItemInternal(String tableName, JsonNode key, JsonNode attributeUpdates, - String updateExpression, - JsonNode expressionAttrNames, JsonNode expressionAttrValues, - String returnValues, String conditionExpression, String region, - String returnValuesOnConditionCheckFailure, - boolean shouldPersist) { + UpdateResult updateItem(String tableName, JsonNode key, JsonNode attributeUpdates, + String updateExpression, + JsonNode expressionAttrNames, JsonNode expressionAttrValues, + String returnValues, String conditionExpression, String region, + String returnValuesOnConditionCheckFailure, + UpdateSizeRule sizeRule) { return updateItemInternal(tableName, key, attributeUpdates, updateExpression, expressionAttrNames, expressionAttrValues, returnValues, - conditionExpression, region, returnValuesOnConditionCheckFailure, shouldPersist, null); + conditionExpression, region, returnValuesOnConditionCheckFailure, true, sizeRule); } private UpdateResult updateItemInternal(String tableName, JsonNode key, JsonNode attributeUpdates, @@ -803,11 +803,11 @@ public class DynamoDbService implements ResourceProvider { String returnValues, String conditionExpression, String region, String returnValuesOnConditionCheckFailure, boolean shouldPersist, - Consumer deferredStreamEvents) { + UpdateSizeRule sizeRule) { return updateItemInternal(tableName, key, attributeUpdates, updateExpression, expressionAttrNames, expressionAttrValues, returnValues, - conditionExpression, region, returnValuesOnConditionCheckFailure, shouldPersist, - deferredStreamEvents, null); + conditionExpression, region, returnValuesOnConditionCheckFailure, shouldPersist, null, + sizeRule); } private UpdateResult updateItemInternal(String tableName, JsonNode key, JsonNode attributeUpdates, @@ -817,7 +817,22 @@ public class DynamoDbService implements ResourceProvider { String returnValuesOnConditionCheckFailure, boolean shouldPersist, Consumer deferredStreamEvents, - Map> stagedItems) { + UpdateSizeRule sizeRule) { + return updateItemInternal(tableName, key, attributeUpdates, updateExpression, + expressionAttrNames, expressionAttrValues, returnValues, + conditionExpression, region, returnValuesOnConditionCheckFailure, shouldPersist, + deferredStreamEvents, null, sizeRule); + } + + private UpdateResult updateItemInternal(String tableName, JsonNode key, JsonNode attributeUpdates, + String updateExpression, + JsonNode expressionAttrNames, JsonNode expressionAttrValues, + String returnValues, String conditionExpression, String region, + String returnValuesOnConditionCheckFailure, + boolean shouldPersist, + Consumer deferredStreamEvents, + Map> stagedItems, + UpdateSizeRule sizeRule) { String canonicalTableName = canonicalTableName(region, tableName); String storageKey = regionKey(region, canonicalTableName); var table = requireActiveTable(storageKey, canonicalTableName); @@ -928,6 +943,16 @@ public class DynamoDbService implements ResourceProvider { // AWS validates index key values against the item the update produces. validateIndexKeyTypes(table, item, true); + if (sizeRule == UpdateSizeRule.UPDATE_ITEM) { + requireUpdateItemWithinSizeLimit(item, + writtenAttributes(touchedPaths, updateExpression, expressionAttrNames), + updateExpression != null + ? DynamoDbItemSize.updateExpressionCost(updateExpression) + : DynamoDbItemSize.attributeUpdatesCost(attributeUpdates)); + } else { + requireUpdatedItemWithinSizeLimit(item); + } + items.put(itemKey, item); if (shouldPersist) { persistItems(storageKey); @@ -953,9 +978,9 @@ public class DynamoDbService implements ResourceProvider { streamEvent.run(); } - var touched = new ArrayList(); - for (var path : touchedPaths) { - var tokens = updateExpression != null ? parsePath(path, expressionAttrNames) : List.of(path); + List touched = new ArrayList<>(); + for (String path : touchedPaths) { + List tokens = pathTokens(path, updateExpression, expressionAttrNames); touched.add(new TouchedPath(tokens, existing == null ? null : valueAtTokens(existing, tokens), valueAtTokens(item, tokens))); } @@ -963,6 +988,22 @@ public class DynamoDbService implements ResourceProvider { }); } + private List pathTokens(String path, String updateExpression, JsonNode expressionAttrNames) { + return updateExpression != null ? parsePath(path, expressionAttrNames) : List.of(path); + } + + private Set writtenAttributes(List touchedPaths, String updateExpression, + JsonNode expressionAttrNames) { + Set names = new LinkedHashSet<>(); + for (String path : touchedPaths) { + List tokens = pathTokens(path, updateExpression, expressionAttrNames); + if (!tokens.isEmpty() && tokens.get(0) instanceof String name) { + names.add(name); + } + } + return names; + } + public QueryResult query(String tableName, JsonNode keyConditions, JsonNode expressionAttrValues, String keyConditionExpression, String filterExpression, Integer limit, String region) { @@ -1468,7 +1509,8 @@ public class DynamoDbService implements ResourceProvider { for (int i = 0; i < transactItems.size(); i++) { try { validateTransactItem(transactItems.get(i), region, staged); - } catch (KeySchemaMismatchException | ItemNestingExceededException e) { + } catch (KeySchemaMismatchException | ItemNestingExceededException + | UpdatedItemTooLargeException e) { throw cancelledByMember(transactItems.size(), i, e.getMessage()); } } @@ -1495,7 +1537,8 @@ public class DynamoDbService implements ResourceProvider { upd.has("UpdateExpression") ? upd.get("UpdateExpression").asText() : null, upd.has("ExpressionAttributeNames") ? upd.get("ExpressionAttributeNames") : null, upd.has("ExpressionAttributeValues") ? upd.get("ExpressionAttributeValues") : null, - "NONE", null, region, "NONE", false, pendingStreamEvents::add, staged); + "NONE", null, region, "NONE", false, pendingStreamEvents::add, staged, + UpdateSizeRule.FINISHED_ITEM); affectedStorageKeys.add(storageKey); } } @@ -1754,6 +1797,20 @@ public class DynamoDbService implements ResourceProvider { validateKeyNotModified(table, key, item); requireItemNestingWithinLimit(item); validateIndexKeyTypes(table, item, true); + requireUpdatedItemWithinSizeLimit(item); + } + } + + private static void requireUpdatedItemWithinSizeLimit(JsonNode item) { + if (!DynamoDbItemSize.updatedItemWithinLimit(item)) { + throw new UpdatedItemTooLargeException(); + } + } + + private static void requireUpdateItemWithinSizeLimit(JsonNode item, Set writtenAttributes, + int actionCost) { + if (!DynamoDbItemSize.updateItemWithinLimit(item, writtenAttributes, actionCost)) { + throw new UpdatedItemTooLargeException(); } } @@ -3085,7 +3142,7 @@ public class DynamoDbService implements ResourceProvider { } } - private int findNextComma(String s) { + static int findNextComma(String s) { // Find next comma that is not inside a function call int depth = 0; for (int i = 0; i < s.length(); i++) { @@ -3097,7 +3154,7 @@ public class DynamoDbService implements ResourceProvider { return -1; } - private int findNextClauseKeyword(String s) { + static int findNextClauseKeyword(String s) { // Find the start of the next clause keyword (SET, REMOVE, ADD, DELETE) String upper = s.toUpperCase(); int[] positions = { @@ -3115,7 +3172,7 @@ public class DynamoDbService implements ResourceProvider { return min; } - private int indexOfKeyword(String upper, String keyword) { + private static int indexOfKeyword(String upper, String keyword) { // Find the next occurrence of keyword at a word boundary (start of string // or preceded by whitespace). Loop past non-boundary hits so attribute // names that contain a keyword as a substring (e.g. "oldSET" before a diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/UpdateSizeRule.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/UpdateSizeRule.java new file mode 100644 index 0000000000..6a76303514 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/UpdateSizeRule.java @@ -0,0 +1,8 @@ +package io.github.hectorvent.floci.services.dynamodb; + +/** How a surface measures an update against the 400KB item limit. */ +enum UpdateSizeRule { + + UPDATE_ITEM, + FINISHED_ITEM +} diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/UpdatedItemTooLargeException.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/UpdatedItemTooLargeException.java new file mode 100644 index 0000000000..b9f72ba108 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/UpdatedItemTooLargeException.java @@ -0,0 +1,11 @@ +package io.github.hectorvent.floci.services.dynamodb; + +import io.github.hectorvent.floci.core.common.AwsException; + +/** An update over 400KB. A transact member carrying one cancels instead of failing up front. */ +public class UpdatedItemTooLargeException extends AwsException { + + public UpdatedItemTooLargeException() { + super("ValidationException", DynamoDbItemSize.UPDATE_EXCEEDED, 400); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbItemSizeServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbItemSizeServiceTest.java new file mode 100644 index 0000000000..c46c7d8b7c --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbItemSizeServiceTest.java @@ -0,0 +1,284 @@ +package io.github.hectorvent.floci.services.dynamodb; + +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.JsonNodeFactory; +import com.fasterxml.jackson.databind.node.ObjectNode; +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.storage.InMemoryStorage; +import io.github.hectorvent.floci.services.dynamodb.model.AttributeDefinition; +import io.github.hectorvent.floci.services.dynamodb.model.KeySchemaElement; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class DynamoDbItemSizeServiceTest { + + private static final String REGION = "us-east-1"; + private static final String TABLE = "ItemSizes"; + private static final int MAX_ITEM_SIZE = 400 * 1024; + private static final String UPDATE_EXCEEDED = + "Item size to update has exceeded the maximum allowed size"; + private static final int UPDATE_COST = 3; + private static final int SET_OR_ADD_COST = 19; + private static final int REMOVE_OR_DELETE_COST = 2; + private static final int LIST_INDEX_COST = 1; + // "pk" plus a one-byte value, short enough that the ceiling falls below 400KB. + private static final int SHORT_KEY_BYTES = 3; + + private DynamoDbService service; + private DynamoDbJsonHandler handler; + + @BeforeEach + void setUp() { + service = new DynamoDbService(new InMemoryStorage<>()); + handler = new DynamoDbJsonHandler(service, null, null, new ObjectMapper()); + service.createTable(TABLE, List.of(new KeySchemaElement("pk", "HASH")), + List.of(new AttributeDefinition("pk", "S")), 5L, 5L, REGION); + } + + @Test + void updateItemSitsBelowTheLimitAtAShortKey() { + int ceiling = MAX_ITEM_SIZE + SHORT_KEY_BYTES - (UPDATE_COST + SET_OR_ADD_COST); + + update("a", "SET b = :pad", ceiling, 0); + assertEquals(ceiling, storedBytes("a")); + assertRefused(() -> update("b", "SET b = :pad", ceiling + 1, 0)); + } + + @Test + void updateItemCapsAtFourHundredKilobytesAtALongKey() { + String longKey = "k".repeat(100); + + update(longKey, "SET b = :pad", MAX_ITEM_SIZE, 0); + assertEquals(MAX_ITEM_SIZE, storedBytes(longKey)); + assertRefused(() -> update("K".repeat(100), "SET b = :pad", MAX_ITEM_SIZE + 1, 0)); + } + + @Test + void updateItemChargesNineteenBytesForASecondSetClause() { + int ceiling = MAX_ITEM_SIZE + SHORT_KEY_BYTES - (UPDATE_COST + SET_OR_ADD_COST * 2); + ObjectNode second = values(":c", stringValue("y")); + + update("a", "SET b = :pad, c = :c", ceiling, 2, second); + assertEquals(ceiling, storedBytes("a")); + assertRefused(() -> update("b", "SET b = :pad, c = :c", ceiling + 1, 2, second)); + } + + @Test + void updateItemChargesNineteenBytesForAnAdd() { + int ceiling = MAX_ITEM_SIZE + SHORT_KEY_BYTES - (UPDATE_COST + SET_OR_ADD_COST * 2); + ObjectNode one = values(":one", numberValue("1")); + + update("a", "SET b = :pad ADD n :one", ceiling, 2, one); + assertEquals(ceiling, storedBytes("a")); + assertRefused(() -> update("b", "SET b = :pad ADD n :one", ceiling + 1, 2, one)); + } + + @Test + void updateItemChargesTwoBytesForARemoveAlongsideASet() { + int ceiling = MAX_ITEM_SIZE + SHORT_KEY_BYTES + - (UPDATE_COST + SET_OR_ADD_COST + REMOVE_OR_DELETE_COST); + seed("a", "r", stringValue("z")); + seed("b", "r", stringValue("z")); + + update("a", "SET b = :pad REMOVE r", ceiling, 0); + assertEquals(ceiling, storedBytes("a")); + assertRefused(() -> update("b", "SET b = :pad REMOVE r", ceiling + 1, 0)); + } + + @Test + void updateItemChargesTwoBytesForADeleteAlongsideASet() { + int ceiling = MAX_ITEM_SIZE + SHORT_KEY_BYTES + - (UPDATE_COST + SET_OR_ADD_COST + REMOVE_OR_DELETE_COST); + ObjectNode members = values(":members", stringSetValue("z")); + seed("a", "s", stringSetValue("z")); + seed("b", "s", stringSetValue("z")); + + update("a", "SET b = :pad DELETE s :members", ceiling, 0, members); + assertEquals(ceiling, storedBytes("a")); + assertRefused(() -> update("b", "SET b = :pad DELETE s :members", ceiling + 1, 0, members)); + } + + @Test + void updateItemChargesADocumentPathLikeAPlainSet() { + int ceiling = MAX_ITEM_SIZE + SHORT_KEY_BYTES - (UPDATE_COST + SET_OR_ADD_COST); + int mapBytes = "leaf".length() + 1 + 3; + seed("a", "d", mapValue("leaf", stringValue("x"))); + seed("b", "d", mapValue("leaf", stringValue("x"))); + + update("a", "SET d.leaf = :pad", ceiling, mapBytes); + assertEquals(ceiling, storedBytes("a")); + assertRefused(() -> update("b", "SET d.leaf = :pad", ceiling + 1, mapBytes)); + } + + @Test + void updateItemChargesOneMoreByteThroughAListIndex() { + int ceiling = MAX_ITEM_SIZE + SHORT_KEY_BYTES + - (UPDATE_COST + SET_OR_ADD_COST + LIST_INDEX_COST); + int elementBytes = 3; + seed("a", "d", listValue(stringValue("x"))); + seed("b", "d", listValue(stringValue("x"))); + + update("a", "SET d[0] = :pad", ceiling, elementBytes); + assertEquals(ceiling, storedBytes("a")); + assertRefused(() -> update("b", "SET d[0] = :pad", ceiling + 1, elementBytes)); + } + + @Test + void updateItemChargesAFunctionCallAsOneAction() { + int ceiling = MAX_ITEM_SIZE + SHORT_KEY_BYTES - (UPDATE_COST + SET_OR_ADD_COST); + + update("a", "SET b = if_not_exists(b, :pad)", ceiling, 0); + assertEquals(ceiling, storedBytes("a")); + assertRefused(() -> update("b", "SET b = if_not_exists(b, :pad)", ceiling + 1, 0)); + } + + @Test + void updateItemChargesALegacyAttributeUpdateLikeASet() { + int ceiling = MAX_ITEM_SIZE + SHORT_KEY_BYTES - (UPDATE_COST + SET_OR_ADD_COST); + + updateAttribute("a", ceiling); + assertEquals(ceiling, storedBytes("a")); + assertRefused(() -> updateAttribute("b", ceiling + 1)); + } + + @Test + void updateItemDoesNotChargeForAnAttributeItLeavesAlone() { + int untouchedBytes = 1 + 10; // "u" plus its value + int ceiling = MAX_ITEM_SIZE + SHORT_KEY_BYTES + untouchedBytes + - (UPDATE_COST + SET_OR_ADD_COST); + seed("a", "u", stringValue("y".repeat(10))); + seed("b", "u", stringValue("y".repeat(10))); + + update("a", "SET b = :pad", ceiling, untouchedBytes); + assertEquals(ceiling, storedBytes("a")); + assertRefused(() -> update("b", "SET b = :pad", ceiling + 1, untouchedBytes)); + } + + @Test + void partiQlUpdateMeasuresTheFinishedItem() throws Exception { + service.putItem(TABLE, key("a"), REGION); + service.putItem(TABLE, key("b"), REGION); + + executeUpdateStatement("a", MAX_ITEM_SIZE); + assertEquals(MAX_ITEM_SIZE, storedBytes("a")); + assertRefused(() -> executeUpdateStatement("b", MAX_ITEM_SIZE + 1)); + } + + @Test + void transactedUpdateMeasuresTheFinishedItemAndCancels() { + service.transactWriteItems(List.of(transactUpdate("a", MAX_ITEM_SIZE)), REGION); + assertEquals(MAX_ITEM_SIZE, storedBytes("a")); + + TransactionCanceledException cancelled = assertThrows(TransactionCanceledException.class, + () -> service.transactWriteItems(List.of(transactUpdate("b", MAX_ITEM_SIZE + 1)), REGION)); + assertEquals("ValidationError", cancelled.getCancellationReasons().get(0).code()); + assertEquals(UPDATE_EXCEEDED, cancelled.getCancellationReasons().get(0).message()); + assertNull(service.getItem(TABLE, key("b"), REGION)); + } + + private void update(String keyValue, String expression, int totalBytes, int otherBytes) { + update(keyValue, expression, totalBytes, otherBytes, JsonNodeFactory.instance.objectNode()); + } + + private void update(String keyValue, String expression, int totalBytes, int otherBytes, + ObjectNode extraValues) { + ObjectNode values = extraValues.deepCopy(); + values.set(":pad", stringValue(padding(keyValue, totalBytes, otherBytes))); + service.updateItem(TABLE, key(keyValue), null, expression, null, values, "NONE", REGION); + } + + private void updateAttribute(String keyValue, int totalBytes) { + ObjectNode updates = JsonNodeFactory.instance.objectNode(); + updates.putObject("b").set("Value", stringValue(padding(keyValue, totalBytes, 0))); + service.updateItem(TABLE, key(keyValue), updates, null, null, null, "NONE", REGION); + } + + private void executeUpdateStatement(String keyValue, int totalBytes) throws Exception { + ObjectNode request = JsonNodeFactory.instance.objectNode(); + request.put("Statement", "UPDATE \"" + TABLE + "\" SET b = ? WHERE pk = ?"); + ArrayNode parameters = request.putArray("Parameters"); + parameters.add(stringValue(padding(keyValue, totalBytes, 0))); + parameters.add(stringValue(keyValue)); + handler.handle("ExecuteStatement", request, REGION); + } + + private JsonNode transactUpdate(String keyValue, int totalBytes) { + ObjectNode member = JsonNodeFactory.instance.objectNode(); + ObjectNode update = member.putObject("Update"); + update.put("TableName", TABLE); + update.set("Key", key(keyValue)); + update.put("UpdateExpression", "SET b = :pad"); + update.set("ExpressionAttributeValues", + values(":pad", stringValue(padding(keyValue, totalBytes, 0)))); + return member; + } + + private void seed(String keyValue, String attribute, JsonNode value) { + ObjectNode item = key(keyValue); + item.set(attribute, value); + service.putItem(TABLE, item, REGION); + } + + private String padding(String keyValue, int totalBytes, int otherBytes) { + return "x".repeat(totalBytes - "pk".length() - keyValue.length() - "b".length() - otherBytes); + } + + private ObjectNode key(String keyValue) { + ObjectNode key = JsonNodeFactory.instance.objectNode(); + key.set("pk", stringValue(keyValue)); + return key; + } + + private ObjectNode values(String placeholder, JsonNode value) { + ObjectNode values = JsonNodeFactory.instance.objectNode(); + values.set(placeholder, value); + return values; + } + + private ObjectNode stringValue(String text) { + return JsonNodeFactory.instance.objectNode().put("S", text); + } + + private ObjectNode numberValue(String number) { + return JsonNodeFactory.instance.objectNode().put("N", number); + } + + private ObjectNode stringSetValue(String member) { + ObjectNode value = JsonNodeFactory.instance.objectNode(); + value.putArray("SS").add(member); + return value; + } + + private ObjectNode mapValue(String attribute, JsonNode value) { + ObjectNode map = JsonNodeFactory.instance.objectNode(); + map.putObject("M").set(attribute, value); + return map; + } + + private ObjectNode listValue(JsonNode element) { + ObjectNode list = JsonNodeFactory.instance.objectNode(); + list.putArray("L").add(element); + return list; + } + + private int storedBytes(String keyValue) { + JsonNode item = service.getItem(TABLE, key(keyValue), REGION); + return DynamoDbItemSize.calculateItemSize(item); + } + + private void assertRefused(Executable body) { + AwsException refused = assertThrows(AwsException.class, body); + assertEquals("ValidationException", refused.getErrorCode()); + assertEquals(400, refused.getHttpStatus()); + assertEquals(UPDATE_EXCEEDED, refused.getMessage()); + } +}