mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(dynamodb): evaluate legacy Expected under the item lock (#6278)
* fix(dynamodb): evaluate legacy Expected under the item lock PutItem, UpdateItem and DeleteItem evaluated a legacy Expected condition in the request handler, against an item read before the per-item lock was taken, and only then called the locked write. Concurrent writers holding the same version could all pass the check, so an optimistic-locking client saw several of them accepted where DynamoDB accepts exactly one. The handler still validates the Expected map, but now hands its evaluation to the service, which runs it against the stored item under the same lock as the write, beside ConditionExpression. PutItem with ReturnValues ALL_OLD now returns the item the put replaced instead of that earlier read. Closes #6276 * fix(dynamodb): report key errors before legacy Expected errors Validate the table and the key a write names before the legacy Expected map, as the handler's earlier unlocked read did, but without reading the stored item. UpdateItem and DeleteItem keep their key-argument wording; PutItem now reports its item-body wording whether or not the request carries Expected or ReturnValues ALL_OLD, matching a plain PutItem. * test(dynamodb): share the round loop of the legacy Expected race tests Rename the class to DynamoDbLegacyExpectedIntegrationTest now that it also covers error precedence. * test(dynamodb): share the start-gate helper and the DynamoDB request helper Move the start-gate thread pool from DynamoDbConcurrencyIntegrationTest into testing/StartGate, which returns each task's result along with any errors, and use it from DynamoDbLegacyExpectedIntegrationTest too. The legacy Expected test now sends its requests through DynamoDbRequests.ddb instead of its own copy.
This commit is contained in:
1 parent
94c4572fed
commit
f94e0583b7
5 files changed
+371
-71
No files matched your search
@@ -967,10 +967,24 @@ public class DynamoDbService {
|
||||
String conditionExpression,
|
||||
JsonNode exprAttrNames, JsonNode exprAttrValues,
|
||||
String region, String returnValuesOnConditionCheckFailure) {
|
||||
var canonicalTableName = canonicalTableName(region, tableName);
|
||||
return putItem(tableName, item, conditionExpression, exprAttrNames, exprAttrValues,
|
||||
region, returnValuesOnConditionCheckFailure, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* As above, and {@code existingItemCheck}, when present, receives the stored item (null when
|
||||
* absent) under the item's lock and rejects the put by throwing. A check made against an item
|
||||
* read before the lock is taken can be invalidated by a concurrent write.
|
||||
*/
|
||||
public JsonNode putItem(String tableName, JsonNode item,
|
||||
String conditionExpression,
|
||||
JsonNode exprAttrNames, JsonNode exprAttrValues,
|
||||
String region, String returnValuesOnConditionCheckFailure,
|
||||
Consumer<JsonNode> existingItemCheck) {
|
||||
String canonicalTableName = canonicalTableName(region, tableName);
|
||||
requireActiveTable(regionKey(region, canonicalTableName));
|
||||
return putItemInternal(tableName, item, conditionExpression, exprAttrNames, exprAttrValues,
|
||||
region, returnValuesOnConditionCheckFailure, true);
|
||||
region, returnValuesOnConditionCheckFailure, true, null, null, existingItemCheck);
|
||||
}
|
||||
|
||||
private JsonNode putItemInternal(String tableName, JsonNode item,
|
||||
@@ -989,7 +1003,7 @@ public class DynamoDbService {
|
||||
boolean shouldPersist,
|
||||
Consumer<Runnable> deferredStreamEvents) {
|
||||
return putItemInternal(tableName, item, conditionExpression, exprAttrNames, exprAttrValues,
|
||||
region, returnValuesOnConditionCheckFailure, shouldPersist, deferredStreamEvents, null);
|
||||
region, returnValuesOnConditionCheckFailure, shouldPersist, deferredStreamEvents, null, null);
|
||||
}
|
||||
|
||||
private JsonNode putItemInternal(String tableName, JsonNode item,
|
||||
@@ -998,7 +1012,8 @@ public class DynamoDbService {
|
||||
String region, String returnValuesOnConditionCheckFailure,
|
||||
boolean shouldPersist,
|
||||
Consumer<Runnable> deferredStreamEvents,
|
||||
Map<String, ConcurrentSkipListMap<String, JsonNode>> stagedItems) {
|
||||
Map<String, ConcurrentSkipListMap<String, JsonNode>> stagedItems,
|
||||
Consumer<JsonNode> existingItemCheck) {
|
||||
String canonicalTableName = canonicalTableName(region, tableName);
|
||||
String storageKey = regionKey(region, canonicalTableName);
|
||||
TableDefinition table = tableStore.get(storageKey)
|
||||
@@ -1018,6 +1033,9 @@ public class DynamoDbService {
|
||||
if (conditionExpression != null) {
|
||||
evaluateCondition(existing, conditionExpression, exprAttrNames, exprAttrValues, returnValuesOnConditionCheckFailure);
|
||||
}
|
||||
if (existingItemCheck != null) {
|
||||
existingItemCheck.accept(existing);
|
||||
}
|
||||
requireItemNestingWithinLimit(normalizedItem);
|
||||
|
||||
if (stagedItems == null) {
|
||||
@@ -1076,8 +1094,21 @@ public class DynamoDbService {
|
||||
String conditionExpression,
|
||||
JsonNode exprAttrNames, JsonNode exprAttrValues,
|
||||
String region, String returnValuesOnConditionCheckFailure) {
|
||||
return deleteItem(tableName, key, conditionExpression, exprAttrNames, exprAttrValues,
|
||||
region, returnValuesOnConditionCheckFailure, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* As above, and {@code existingItemCheck}, when present, receives the stored item (null when
|
||||
* absent) under the item's lock and rejects the delete by throwing.
|
||||
*/
|
||||
public JsonNode deleteItem(String tableName, JsonNode key,
|
||||
String conditionExpression,
|
||||
JsonNode exprAttrNames, JsonNode exprAttrValues,
|
||||
String region, String returnValuesOnConditionCheckFailure,
|
||||
Consumer<JsonNode> existingItemCheck) {
|
||||
return deleteItemInternal(tableName, key, conditionExpression, exprAttrNames, exprAttrValues,
|
||||
region, returnValuesOnConditionCheckFailure, true);
|
||||
region, returnValuesOnConditionCheckFailure, true, null, null, existingItemCheck);
|
||||
}
|
||||
|
||||
private JsonNode deleteItemInternal(String tableName, JsonNode key,
|
||||
@@ -1096,7 +1127,7 @@ public class DynamoDbService {
|
||||
boolean shouldPersist,
|
||||
Consumer<Runnable> deferredStreamEvents) {
|
||||
return deleteItemInternal(tableName, key, conditionExpression, exprAttrNames, exprAttrValues,
|
||||
region, returnValuesOnConditionCheckFailure, shouldPersist, deferredStreamEvents, null);
|
||||
region, returnValuesOnConditionCheckFailure, shouldPersist, deferredStreamEvents, null, null);
|
||||
}
|
||||
|
||||
private JsonNode deleteItemInternal(String tableName, JsonNode key,
|
||||
@@ -1105,7 +1136,8 @@ public class DynamoDbService {
|
||||
String region, String returnValuesOnConditionCheckFailure,
|
||||
boolean shouldPersist,
|
||||
Consumer<Runnable> deferredStreamEvents,
|
||||
Map<String, ConcurrentSkipListMap<String, JsonNode>> stagedItems) {
|
||||
Map<String, ConcurrentSkipListMap<String, JsonNode>> stagedItems,
|
||||
Consumer<JsonNode> existingItemCheck) {
|
||||
String canonicalTableName = canonicalTableName(region, tableName);
|
||||
String storageKey = regionKey(region, canonicalTableName);
|
||||
TableDefinition table = requireActiveTable(storageKey);
|
||||
@@ -1114,6 +1146,9 @@ public class DynamoDbService {
|
||||
|
||||
return withItemLock(storageKey, itemKey, () -> {
|
||||
var items = itemsFor(storageKey, stagedItems, false);
|
||||
if (existingItemCheck != null) {
|
||||
existingItemCheck.accept(items == null ? null : items.get(itemKey));
|
||||
}
|
||||
if (items == null) return null;
|
||||
|
||||
if (conditionExpression != null) {
|
||||
@@ -1174,6 +1209,22 @@ public class DynamoDbService {
|
||||
returnValuesOnConditionCheckFailure, UpdateSizeRule.UPDATE_ITEM);
|
||||
}
|
||||
|
||||
/**
|
||||
* As above, and {@code existingItemCheck}, when present, receives the stored item (null when
|
||||
* absent) under the item's lock and rejects the update by throwing.
|
||||
*/
|
||||
public UpdateResult updateItem(String tableName, JsonNode key, JsonNode attributeUpdates,
|
||||
String updateExpression,
|
||||
JsonNode expressionAttrNames, JsonNode expressionAttrValues,
|
||||
String returnValues, String conditionExpression, String region,
|
||||
String returnValuesOnConditionCheckFailure,
|
||||
Consumer<JsonNode> existingItemCheck) {
|
||||
return updateItemInternal(tableName, key, attributeUpdates, updateExpression,
|
||||
expressionAttrNames, expressionAttrValues, returnValues,
|
||||
conditionExpression, region, returnValuesOnConditionCheckFailure, true, null, null,
|
||||
UpdateSizeRule.UPDATE_ITEM, existingItemCheck);
|
||||
}
|
||||
|
||||
UpdateResult updateItem(String tableName, JsonNode key, JsonNode attributeUpdates,
|
||||
String updateExpression,
|
||||
JsonNode expressionAttrNames, JsonNode expressionAttrValues,
|
||||
@@ -1209,7 +1260,7 @@ public class DynamoDbService {
|
||||
return updateItemInternal(tableName, key, attributeUpdates, updateExpression,
|
||||
expressionAttrNames, expressionAttrValues, returnValues,
|
||||
conditionExpression, region, returnValuesOnConditionCheckFailure, shouldPersist,
|
||||
deferredStreamEvents, null, sizeRule);
|
||||
deferredStreamEvents, null, sizeRule, null);
|
||||
}
|
||||
|
||||
private UpdateResult updateItemInternal(String tableName, JsonNode key, JsonNode attributeUpdates,
|
||||
@@ -1220,7 +1271,8 @@ public class DynamoDbService {
|
||||
boolean shouldPersist,
|
||||
Consumer<Runnable> deferredStreamEvents,
|
||||
Map<String, ConcurrentSkipListMap<String, JsonNode>> stagedItems,
|
||||
UpdateSizeRule sizeRule) {
|
||||
UpdateSizeRule sizeRule,
|
||||
Consumer<JsonNode> existingItemCheck) {
|
||||
String canonicalTableName = canonicalTableName(region, tableName);
|
||||
String storageKey = regionKey(region, canonicalTableName);
|
||||
TableDefinition table = requireActiveTable(storageKey);
|
||||
@@ -1236,6 +1288,9 @@ public class DynamoDbService {
|
||||
if (conditionExpression != null) {
|
||||
evaluateCondition(existing, conditionExpression, expressionAttrNames, expressionAttrValues, returnValuesOnConditionCheckFailure);
|
||||
}
|
||||
if (existingItemCheck != null) {
|
||||
existingItemCheck.accept(existing);
|
||||
}
|
||||
|
||||
ObjectNode item;
|
||||
if (existing != null) {
|
||||
@@ -1896,14 +1951,14 @@ public class DynamoDbService {
|
||||
String tableName = put.path("TableName").asText();
|
||||
String storageKey = regionKey(region, canonicalTableName(region, tableName));
|
||||
putItemInternal(tableName, put.get("Item"), null, null, null, region, "NONE", false,
|
||||
pendingStreamEvents::add, staged);
|
||||
pendingStreamEvents::add, staged, null);
|
||||
affectedStorageKeys.add(storageKey);
|
||||
} else if (transactItem.has("Delete")) {
|
||||
JsonNode del = transactItem.get("Delete");
|
||||
String tableName = del.path("TableName").asText();
|
||||
String storageKey = regionKey(region, canonicalTableName(region, tableName));
|
||||
deleteItemInternal(tableName, del.get("Key"), null, null, null, region, "NONE", false,
|
||||
pendingStreamEvents::add, staged);
|
||||
pendingStreamEvents::add, staged, null);
|
||||
affectedStorageKeys.add(storageKey);
|
||||
} else if (transactItem.has("Update")) {
|
||||
JsonNode upd = transactItem.get("Update");
|
||||
@@ -1914,7 +1969,7 @@ public class DynamoDbService {
|
||||
upd.has("ExpressionAttributeNames") ? upd.get("ExpressionAttributeNames") : null,
|
||||
upd.has("ExpressionAttributeValues") ? upd.get("ExpressionAttributeValues") : null,
|
||||
"NONE", null, region, "NONE", false, pendingStreamEvents::add, staged,
|
||||
UpdateSizeRule.FINISHED_ITEM);
|
||||
UpdateSizeRule.FINISHED_ITEM, null);
|
||||
affectedStorageKeys.add(storageKey);
|
||||
}
|
||||
}
|
||||
@@ -4703,6 +4758,14 @@ public class DynamoDbService {
|
||||
return requireActiveTable(regionKey(region, canonicalTableName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the table for an item call and validates the key it names, without reading the
|
||||
* stored item, so a caller can report table and key errors before its own validation.
|
||||
*/
|
||||
public void requireItemCallKey(String tableName, JsonNode keyOrItem, boolean isKeyArg, String region) {
|
||||
buildItemKey(requireTableForItemCall(tableName, region), keyOrItem, isKeyArg);
|
||||
}
|
||||
|
||||
public record UpdateResult(JsonNode newItem, JsonNode oldItem, List<TouchedPath> touched) {}
|
||||
|
||||
// One path the update expression acted on, with the value there before and after.
|
||||
|
||||
+31
-28
@@ -44,6 +44,7 @@ import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -626,22 +627,14 @@ public class NativeDynamoDbJsonHandler {
|
||||
|
||||
validateItemSets(item);
|
||||
|
||||
JsonNode oldItem = null;
|
||||
if ("ALL_OLD".equals(returnValues) || expected != null) {
|
||||
dynamoDbService.requireTableForItemCall(tableName, region);
|
||||
oldItem = dynamoDbService.getItem(tableName, item, region);
|
||||
}
|
||||
|
||||
if (expected != null) {
|
||||
evaluateLegacyExpected(oldItem, expected, conditionalOperator, returnValuesOnConditionCheckFailure);
|
||||
}
|
||||
|
||||
var previousItem = dynamoDbService.putItem(tableName, item, conditionExpression,
|
||||
exprAttrNames, exprAttrValues, region, returnValuesOnConditionCheckFailure);
|
||||
Consumer<JsonNode> expectedCheck = legacyExpectedCheck(tableName, region, item, false, expected,
|
||||
conditionalOperator, returnValuesOnConditionCheckFailure);
|
||||
JsonNode previousItem = dynamoDbService.putItem(tableName, item, conditionExpression,
|
||||
exprAttrNames, exprAttrValues, region, returnValuesOnConditionCheckFailure, expectedCheck);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
if ("ALL_OLD" .equals(returnValues) && oldItem != null) {
|
||||
response.set("Attributes", oldItem);
|
||||
if ("ALL_OLD".equals(returnValues) && previousItem != null) {
|
||||
response.set("Attributes", previousItem);
|
||||
}
|
||||
addItemCollectionMetrics(response, request, tableName, item, region);
|
||||
addWriteConsumedCapacity(response, request, tableName, region, previousItem, item);
|
||||
@@ -745,13 +738,10 @@ public class NativeDynamoDbJsonHandler {
|
||||
|
||||
String condOpDel = request.has("ConditionalOperator")
|
||||
? request.get("ConditionalOperator").asText() : "AND";
|
||||
if (expectedDel != null) {
|
||||
JsonNode existingForDel = dynamoDbService.getItem(tableName, key, region);
|
||||
evaluateLegacyExpected(existingForDel, expectedDel, condOpDel, returnValuesOnConditionCheckFailure);
|
||||
}
|
||||
|
||||
Consumer<JsonNode> expectedCheck = legacyExpectedCheck(tableName, region, key, true, expectedDel,
|
||||
condOpDel, returnValuesOnConditionCheckFailure);
|
||||
JsonNode oldItem = dynamoDbService.deleteItem(tableName, key, conditionExpression,
|
||||
exprAttrNames, exprAttrValues, region, returnValuesOnConditionCheckFailure);
|
||||
exprAttrNames, exprAttrValues, region, returnValuesOnConditionCheckFailure, expectedCheck);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
if ("ALL_OLD" .equals(returnValues) && oldItem != null) {
|
||||
@@ -855,14 +845,11 @@ public class NativeDynamoDbJsonHandler {
|
||||
ExpressionEvaluator.validateSemantics(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues);
|
||||
}
|
||||
|
||||
if (expectedUpd != null) {
|
||||
JsonNode existingForUpd = dynamoDbService.getItem(tableName, key, region);
|
||||
evaluateLegacyExpected(existingForUpd, expectedUpd, conditionalOperatorUpd, returnValuesOnConditionCheckFailure);
|
||||
}
|
||||
|
||||
Consumer<JsonNode> expectedCheck = legacyExpectedCheck(tableName, region, key, true, expectedUpd,
|
||||
conditionalOperatorUpd, returnValuesOnConditionCheckFailure);
|
||||
DynamoDbService.UpdateResult result = dynamoDbService.updateItem(
|
||||
tableName, key, updateData, updateExpression, exprAttrNames, exprAttrValues,
|
||||
returnValues, conditionExpression, region, returnValuesOnConditionCheckFailure);
|
||||
returnValues, conditionExpression, region, returnValuesOnConditionCheckFailure, expectedCheck);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
if ("ALL_NEW" .equals(returnValues) && result.newItem() != null) {
|
||||
@@ -881,10 +868,26 @@ public class NativeDynamoDbJsonHandler {
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates a legacy Expected map and returns its evaluation, for the service to run against
|
||||
* the stored item under the item's lock, or null when the request has no Expected. The table
|
||||
* and the key are checked first, so their errors take precedence over the map's.
|
||||
*/
|
||||
private Consumer<JsonNode> legacyExpectedCheck(String tableName, String region, JsonNode keyOrItem,
|
||||
boolean isKeyArg, JsonNode expected,
|
||||
String conditionalOperator,
|
||||
String returnValuesOnConditionCheckFailure) {
|
||||
if (expected == null) {
|
||||
return null;
|
||||
}
|
||||
dynamoDbService.requireItemCallKey(tableName, keyOrItem, isKeyArg, region);
|
||||
validateLegacyExpected(expected);
|
||||
return existing -> evaluateLegacyExpected(existing, expected, conditionalOperator,
|
||||
returnValuesOnConditionCheckFailure);
|
||||
}
|
||||
|
||||
private void evaluateLegacyExpected(JsonNode existing, JsonNode expected, String conditionalOperator,
|
||||
String returnValuesOnConditionCheckFailure) {
|
||||
if (expected == null) return;
|
||||
validateLegacyExpected(expected);
|
||||
boolean useOr = "OR".equals(conditionalOperator);
|
||||
boolean overall = !useOr;
|
||||
var fields = expected.fields();
|
||||
|
||||
Reference in new issue
Block a user