mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(dynamodb): return attributes and map keys in a random order where AWS does (#9191)
* feat(dynamodb): return attributes and map keys in a random order where AWS does * perf(dynamodb): presize the maps of a shuffled item * refactor(dynamodb): read the attribute order setting when the handler is built * docs(dynamodb): say the attribute order setting works with the native backend only * test(dynamodb): share the attribute order test helpers and delete each test table * test(dynamodb): check the attribute order setting in a unit test instead of a test profile * docs(dynamodb): say reads return the same order on every call
This commit is contained in:
1 parent
e295f9ed12
commit
a966242daf
11 files changed
+513
-15
No files matched your search
@@ -1742,6 +1742,16 @@ public interface EmulatorConfig {
|
||||
@WithDefault("false")
|
||||
boolean transactionConflictsEnabled();
|
||||
|
||||
/**
|
||||
* Whether the operations that AWS answers in a new order on each call do the same: the
|
||||
* ReturnValues of PutItem, UpdateItem and DeleteItem, the Item of a failed condition check,
|
||||
* TransactGetItems and the Item of a TransactWriteItems cancellation reason. Map keys and
|
||||
* top-level attributes come back shuffled. False keeps the stored order. The native backend only.
|
||||
* Env: FLOCI_SERVICES_DYNAMODB_RANDOMIZE_ATTRIBUTE_ORDER
|
||||
*/
|
||||
@WithDefault("true")
|
||||
boolean randomizeAttributeOrder();
|
||||
|
||||
/** The engine behind the DynamoDB API: {@code native} or {@code local}. Env: FLOCI_SERVICES_DYNAMODB_BACKEND. */
|
||||
@WithDefault("native")
|
||||
String backend();
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package io.floci.aws.services.dynamodb;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
/**
|
||||
* Some DynamoDB operations return the top-level attributes and every map in a new random order
|
||||
* on each call. Each map is drawn on its own. Sets and lists keep their order.
|
||||
*/
|
||||
final class DynamoDbAttributeOrder {
|
||||
|
||||
private static final JsonNodeFactory FACTORY = JsonNodeFactory.instance;
|
||||
|
||||
private DynamoDbAttributeOrder() {
|
||||
}
|
||||
|
||||
static JsonNode shuffled(JsonNode item, boolean enabled) {
|
||||
if (!enabled || item == null || !item.isObject()) {
|
||||
return item;
|
||||
}
|
||||
return shuffleFields((ObjectNode) item);
|
||||
}
|
||||
|
||||
private static ObjectNode shuffleFields(ObjectNode node) {
|
||||
int n = node.size();
|
||||
@SuppressWarnings("unchecked")
|
||||
Map.Entry<String, JsonNode>[] entries = new Map.Entry[n];
|
||||
int i = 0;
|
||||
for (Map.Entry<String, JsonNode> entry : node.properties()) {
|
||||
entries[i++] = entry;
|
||||
}
|
||||
ThreadLocalRandom random = ThreadLocalRandom.current();
|
||||
for (int j = n - 1; j > 0; j--) {
|
||||
int k = random.nextInt(j + 1);
|
||||
Map.Entry<String, JsonNode> swap = entries[j];
|
||||
entries[j] = entries[k];
|
||||
entries[k] = swap;
|
||||
}
|
||||
Map<String, JsonNode> map = new LinkedHashMap<>((int) (n / 0.75f) + 1);
|
||||
for (Map.Entry<String, JsonNode> entry : entries) {
|
||||
map.put(entry.getKey(), shuffleValue(entry.getValue()));
|
||||
}
|
||||
return new ObjectNode(FACTORY, map);
|
||||
}
|
||||
|
||||
private static ObjectNode wrap(String key, JsonNode value) {
|
||||
Map<String, JsonNode> map = new LinkedHashMap<>(2);
|
||||
map.put(key, value);
|
||||
return new ObjectNode(FACTORY, map);
|
||||
}
|
||||
|
||||
private static JsonNode shuffleValue(JsonNode value) {
|
||||
if (value.isObject() && value.size() == 1) {
|
||||
JsonNode map = value.get("M");
|
||||
if (map != null && map.isObject()) {
|
||||
return wrap("M", shuffleFields((ObjectNode) map));
|
||||
}
|
||||
JsonNode list = value.get("L");
|
||||
if (list != null && list.isArray()) {
|
||||
ArrayNode copy = FACTORY.arrayNode(list.size());
|
||||
for (JsonNode element : list) {
|
||||
copy.add(shuffleValue(element));
|
||||
}
|
||||
return wrap("L", copy);
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -89,16 +89,24 @@ public class NativeDynamoDbJsonHandler {
|
||||
private final NativeDynamoDbTableService tableService;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final EmulatorConfig config;
|
||||
private final boolean randomizeOrder;
|
||||
private final DynamoDbPartiQLHandler partiQLHandler;
|
||||
private final Object importLock = new Object();
|
||||
|
||||
@Inject
|
||||
public NativeDynamoDbJsonHandler(DynamoDbService dynamoDbService, NativeDynamoDbTableService tableService,
|
||||
ObjectMapper objectMapper, EmulatorConfig config) {
|
||||
this(dynamoDbService, tableService, objectMapper, config,
|
||||
config.services().dynamodb().randomizeAttributeOrder());
|
||||
}
|
||||
|
||||
private NativeDynamoDbJsonHandler(DynamoDbService dynamoDbService, NativeDynamoDbTableService tableService,
|
||||
ObjectMapper objectMapper, EmulatorConfig config, boolean randomizeOrder) {
|
||||
this.dynamoDbService = dynamoDbService;
|
||||
this.tableService = tableService;
|
||||
this.objectMapper = objectMapper;
|
||||
this.config = config;
|
||||
this.randomizeOrder = randomizeOrder;
|
||||
this.partiQLHandler = new DynamoDbPartiQLHandler(dynamoDbService, objectMapper);
|
||||
}
|
||||
|
||||
@@ -106,7 +114,18 @@ public class NativeDynamoDbJsonHandler {
|
||||
NativeDynamoDbJsonHandler(DynamoDbService dynamoDbService, DynamoDbStreamService dynamoDbStreamService,
|
||||
KinesisService kinesisService, ObjectMapper objectMapper) {
|
||||
this(dynamoDbService, new NativeDynamoDbTableService(dynamoDbService, dynamoDbStreamService, kinesisService),
|
||||
objectMapper, null);
|
||||
objectMapper, null, false);
|
||||
}
|
||||
|
||||
private <T> T withShuffledFailureItem(Supplier<T> call) {
|
||||
try {
|
||||
return call.get();
|
||||
} catch (ConditionalCheckFailedException e) {
|
||||
if (e.getItem() == null || !randomizeOrder) {
|
||||
throw e;
|
||||
}
|
||||
throw new ConditionalCheckFailedException(DynamoDbAttributeOrder.shuffled(e.getItem(), true));
|
||||
}
|
||||
}
|
||||
|
||||
public Response handle(String action, JsonNode request, String region) throws Exception {
|
||||
@@ -839,12 +858,13 @@ public class NativeDynamoDbJsonHandler {
|
||||
|
||||
Consumer<JsonNode> expectedCheck = legacyExpectedCheck(tableName, region, item, false, expected,
|
||||
conditionalOperator, returnValuesOnConditionCheckFailure);
|
||||
JsonNode previousItem = dynamoDbService.putItem(tableName, item, conditionExpression,
|
||||
exprAttrNames, exprAttrValues, region, returnValuesOnConditionCheckFailure, expectedCheck);
|
||||
JsonNode previousItem = withShuffledFailureItem(() -> dynamoDbService.putItem(tableName, item,
|
||||
conditionExpression, exprAttrNames, exprAttrValues, region, returnValuesOnConditionCheckFailure,
|
||||
expectedCheck));
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
if ("ALL_OLD".equals(returnValues) && previousItem != null) {
|
||||
response.set("Attributes", previousItem);
|
||||
response.set("Attributes", DynamoDbAttributeOrder.shuffled(previousItem, randomizeOrder));
|
||||
}
|
||||
addItemCollectionMetrics(response, request, tableName, item, region);
|
||||
addWriteConsumedCapacity(response, request, tableName, region, previousItem, item);
|
||||
@@ -973,12 +993,13 @@ public class NativeDynamoDbJsonHandler {
|
||||
? request.get("ConditionalOperator").asText() : "AND";
|
||||
Consumer<JsonNode> expectedCheck = legacyExpectedCheck(tableName, region, key, true, expectedDel,
|
||||
condOpDel, returnValuesOnConditionCheckFailure);
|
||||
JsonNode oldItem = dynamoDbService.deleteItem(tableName, key, conditionExpression,
|
||||
exprAttrNames, exprAttrValues, region, returnValuesOnConditionCheckFailure, expectedCheck);
|
||||
JsonNode oldItem = withShuffledFailureItem(() -> dynamoDbService.deleteItem(tableName, key,
|
||||
conditionExpression, exprAttrNames, exprAttrValues, region, returnValuesOnConditionCheckFailure,
|
||||
expectedCheck));
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
if ("ALL_OLD" .equals(returnValues) && oldItem != null) {
|
||||
response.set("Attributes", oldItem);
|
||||
response.set("Attributes", DynamoDbAttributeOrder.shuffled(oldItem, randomizeOrder));
|
||||
}
|
||||
addItemCollectionMetrics(response, request, tableName, key, region);
|
||||
addWriteConsumedCapacity(response, request, tableName, region, oldItem, null);
|
||||
@@ -1079,20 +1100,20 @@ public class NativeDynamoDbJsonHandler {
|
||||
|
||||
Consumer<JsonNode> expectedCheck = legacyExpectedCheck(tableName, region, key, true, expectedUpd,
|
||||
conditionalOperatorUpd, returnValuesOnConditionCheckFailure);
|
||||
DynamoDbService.UpdateResult result = dynamoDbService.updateItem(
|
||||
DynamoDbService.UpdateResult result = withShuffledFailureItem(() -> dynamoDbService.updateItem(
|
||||
tableName, key, updateData, updateExpression, exprAttrNames, exprAttrValues,
|
||||
returnValues, conditionExpression, region, returnValuesOnConditionCheckFailure, expectedCheck);
|
||||
returnValues, conditionExpression, region, returnValuesOnConditionCheckFailure, expectedCheck));
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
if ("ALL_NEW" .equals(returnValues) && result.newItem() != null) {
|
||||
response.set("Attributes", result.newItem());
|
||||
response.set("Attributes", DynamoDbAttributeOrder.shuffled(result.newItem(), randomizeOrder));
|
||||
} else if ("ALL_OLD" .equals(returnValues) && result.oldItem() != null) {
|
||||
response.set("Attributes", result.oldItem());
|
||||
response.set("Attributes", DynamoDbAttributeOrder.shuffled(result.oldItem(), randomizeOrder));
|
||||
} else if ("UPDATED_NEW".equals(returnValues) || "UPDATED_OLD".equals(returnValues)) {
|
||||
ObjectNode updated = DynamoDbUpdatedAttributes.collect(result.touched(), "UPDATED_NEW".equals(returnValues));
|
||||
// AWS omits Attributes when no touched path has a value on that side.
|
||||
if (!updated.isEmpty()) {
|
||||
response.set("Attributes", updated);
|
||||
response.set("Attributes", DynamoDbAttributeOrder.shuffled(updated, randomizeOrder));
|
||||
}
|
||||
}
|
||||
addItemCollectionMetrics(response, request, tableName, key, region);
|
||||
@@ -3298,7 +3319,7 @@ public class NativeDynamoDbJsonHandler {
|
||||
r.put("Message", reason.message() != null ? reason.message() : "The conditional request failed");
|
||||
}
|
||||
if (reason.item() != null) {
|
||||
r.set("Item", reason.item());
|
||||
r.set("Item", DynamoDbAttributeOrder.shuffled(reason.item(), randomizeOrder));
|
||||
}
|
||||
reasons.add(r);
|
||||
}
|
||||
@@ -3380,7 +3401,7 @@ public class NativeDynamoDbJsonHandler {
|
||||
for (JsonNode item : result.items()) {
|
||||
ObjectNode entry = objectMapper.createObjectNode();
|
||||
if (item != null) {
|
||||
entry.set("Item", item);
|
||||
entry.set("Item", DynamoDbAttributeOrder.shuffled(item, randomizeOrder));
|
||||
}
|
||||
responsesArray.add(entry);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user