mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
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
This commit is contained in:
1 parent
748f71c943
commit
3958be1d0e
6 files changed
+460
-23
No files matched your search
@@ -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<String> writtenAttributes, int actionCost) {
|
||||
int total = 0;
|
||||
int written = 0;
|
||||
Iterator<Map.Entry<String, JsonNode>> fields = item.fields();
|
||||
while (fields.hasNext()) {
|
||||
Map.Entry<String, JsonNode> 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;
|
||||
|
||||
+2
-1
@@ -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));
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Runnable> 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<Runnable> deferredStreamEvents,
|
||||
Map<String, ConcurrentSkipListMap<String, JsonNode>> 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<Runnable> deferredStreamEvents,
|
||||
Map<String, ConcurrentSkipListMap<String, JsonNode>> 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<TouchedPath>();
|
||||
for (var path : touchedPaths) {
|
||||
var tokens = updateExpression != null ? parsePath(path, expressionAttrNames) : List.<Object>of(path);
|
||||
List<TouchedPath> touched = new ArrayList<>();
|
||||
for (String path : touchedPaths) {
|
||||
List<Object> 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<Object> pathTokens(String path, String updateExpression, JsonNode expressionAttrNames) {
|
||||
return updateExpression != null ? parsePath(path, expressionAttrNames) : List.<Object>of(path);
|
||||
}
|
||||
|
||||
private Set<String> writtenAttributes(List<String> touchedPaths, String updateExpression,
|
||||
JsonNode expressionAttrNames) {
|
||||
Set<String> names = new LinkedHashSet<>();
|
||||
for (String path : touchedPaths) {
|
||||
List<Object> 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<String> 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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
+11
@@ -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);
|
||||
}
|
||||
}
|
||||
+284
@@ -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());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user