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 d0f77cdd7d..b19f3fa5c0 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 @@ -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 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 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 deferredStreamEvents, - Map> stagedItems) { + Map> stagedItems, + Consumer 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 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 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 deferredStreamEvents, - Map> stagedItems) { + Map> stagedItems, + Consumer 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 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 deferredStreamEvents, Map> stagedItems, - UpdateSizeRule sizeRule) { + UpdateSizeRule sizeRule, + Consumer 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 touched) {} // One path the update expression acted on, with the value there before and after. diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java index ac18b2a578..0d70b1eb12 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java @@ -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 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 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 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 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(); diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbConcurrencyIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbConcurrencyIntegrationTest.java index 732fc61cbe..69ab52c256 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbConcurrencyIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbConcurrencyIntegrationTest.java @@ -18,6 +18,7 @@ import io.github.hectorvent.floci.services.dynamodb.model.TableDefinition; import io.github.hectorvent.floci.services.kinesis.KinesisService; import io.github.hectorvent.floci.services.kinesis.model.KinesisRecord; import io.github.hectorvent.floci.services.kinesis.model.KinesisStream; +import io.github.hectorvent.floci.testing.StartGate; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; @@ -30,10 +31,6 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import static org.junit.jupiter.api.Assertions.*; @@ -45,7 +42,7 @@ import static org.mockito.Mockito.when; /** * Concurrency compatibility suite for {@link DynamoDbService}. * - *

All scenarios use a {@link CountDownLatch} starting gate so threads release + *

All scenarios use a {@link StartGate} so threads release * simultaneously, maximising contention. Wrapped in {@code @RepeatedTest(5)} so a * regression that surfaces intermittently has a real chance of being observed in a * single CI run. @@ -106,32 +103,10 @@ class DynamoDbConcurrencyIntegrationTest { /** Run {@code work} in {@code threadCount} threads, released together. */ private List runConcurrently(int threadCount, Runnable work) throws InterruptedException { - ExecutorService pool = Executors.newFixedThreadPool(threadCount); - CountDownLatch startGate = new CountDownLatch(1); - CountDownLatch doneGate = new CountDownLatch(threadCount); - List errors = Collections.synchronizedList(new ArrayList<>()); - try { - for (int i = 0; i < threadCount; i++) { - pool.submit(() -> { - try { - startGate.await(); - work.run(); - } catch (Throwable t) { - errors.add(t); - } finally { - doneGate.countDown(); - } - }); - } - startGate.countDown(); - assertTrue(doneGate.await(30, TimeUnit.SECONDS), - "concurrent tasks did not complete within 30s"); - return errors; - } finally { - pool.shutdownNow(); - assertTrue(pool.awaitTermination(5, TimeUnit.SECONDS), - "thread pool did not terminate"); - } + return StartGate.runConcurrently(threadCount, i -> { + work.run(); + return null; + }).errors(); } @RepeatedTest(REPEATS) diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbLegacyExpectedIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbLegacyExpectedIntegrationTest.java new file mode 100644 index 0000000000..b8db71083d --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbLegacyExpectedIntegrationTest.java @@ -0,0 +1,197 @@ +package io.github.hectorvent.floci.services.dynamodb; + +import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; +import io.github.hectorvent.floci.testing.StartGate; +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.response.ValidatableResponse; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.RepeatedTest; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.UUID; +import java.util.function.BiFunction; +import java.util.function.IntFunction; + +import static io.github.hectorvent.floci.testing.DynamoDbRequests.ddb; +import static org.hamcrest.Matchers.equalTo; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Writes carrying a legacy {@code Expected} condition, sent over HTTP so the condition goes through + * the request handler. Concurrent writers of one version: exactly one may win, as on DynamoDB. + */ +@QuarkusTest +class DynamoDbLegacyExpectedIntegrationTest { + + private static final int REPEATS = 3; + private static final int WRITERS = 16; + private static final int ROUNDS = 5; + + private String table; + + @BeforeAll + static void configureJson() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @BeforeEach + void createTable() { + table = "legacy-expected-" + UUID.randomUUID(); + ddb("CreateTable", """ + {"TableName":"%s","KeySchema":[{"AttributeName":"pk","KeyType":"HASH"}], + "AttributeDefinitions":[{"AttributeName":"pk","AttributeType":"S"}], + "BillingMode":"PAY_PER_REQUEST"} + """.formatted(table)).statusCode(200); + } + + @AfterEach + void deleteTable() { + ddb("DeleteTable", "{\"TableName\":\"" + table + "\"}").statusCode(200); + } + + @RepeatedTest(REPEATS) + void updateItem_concurrentWritersOfOneVersion_acceptsExactlyOne() throws InterruptedException { + assertOneWriterWinsEachRound("update", (pk, writer) -> ddb("UpdateItem", """ + {"TableName":"%s","Key":{"pk":{"S":"%s"}}, + "AttributeUpdates":{"version":{"Action":"PUT","Value":{"N":"1"}}, + "writer":{"Action":"PUT","Value":{"S":"%d"}}}, + "Expected":{"version":{"Value":{"N":"0"}}}} + """.formatted(table, pk, writer))); + } + + @RepeatedTest(REPEATS) + void putItem_concurrentWritersOfOneVersion_acceptsExactlyOne() throws InterruptedException { + assertOneWriterWinsEachRound("put", (pk, writer) -> ddb("PutItem", """ + {"TableName":"%s", + "Item":{"pk":{"S":"%s"},"version":{"N":"1"},"writer":{"S":"%d"}}, + "Expected":{"version":{"Value":{"N":"0"}}}} + """.formatted(table, pk, writer))); + } + + @RepeatedTest(REPEATS) + void deleteItem_concurrentDeletesOfOneVersion_acceptsExactlyOne() throws InterruptedException { + assertOneWriterWinsEachRound("delete", (pk, writer) -> ddb("DeleteItem", """ + {"TableName":"%s","Key":{"pk":{"S":"%s"}}, + "Expected":{"version":{"Value":{"N":"0"}}}} + """.formatted(table, pk))); + } + + @RepeatedTest(REPEATS) + void putItem_concurrentAllOld_returnsTheItemEachPutReplaced() throws InterruptedException { + String pk = "all-old"; + ddb("PutItem", """ + {"TableName":"%s","Item":{"pk":{"S":"%s"},"writer":{"S":"seed"}}} + """.formatted(table, pk)).statusCode(200); + + List responses = runConcurrently(writer -> ddb("PutItem", """ + {"TableName":"%s","Item":{"pk":{"S":"%s"},"writer":{"S":"%d"}}, + "ReturnValues":"ALL_OLD"} + """.formatted(table, pk, writer))); + + List replaced = new ArrayList<>(); + for (ValidatableResponse response : responses) { + replaced.add(response.statusCode(200).extract().jsonPath().getString("Attributes.writer.S")); + } + replaced.add(ddb("GetItem", """ + {"TableName":"%s","Key":{"pk":{"S":"%s"}},"ConsistentRead":true} + """.formatted(table, pk)).extract().jsonPath().getString("Item.writer.S")); + + List everyValue = new ArrayList<>(); + everyValue.add("seed"); + for (int writer = 0; writer < WRITERS; writer++) { + everyValue.add(String.valueOf(writer)); + } + Collections.sort(replaced); + Collections.sort(everyValue); + assertEquals(everyValue, replaced, + "every value but the last must be replaced by exactly one put, and the last must be stored"); + } + + @Test + void updateItem_staleExpectedVersion_isRejected() { + seed("stale"); + String update = """ + {"TableName":"%s","Key":{"pk":{"S":"stale"}}, + "AttributeUpdates":{"version":{"Action":"PUT","Value":{"N":"1"}}}, + "Expected":{"version":{"Value":{"N":"0"}}}} + """.formatted(table); + ddb("UpdateItem", update).statusCode(200); + ddb("UpdateItem", update).statusCode(400) + .body("__type", equalTo("ConditionalCheckFailedException")); + } + + @Test + void updateAndDelete_malformedKeyAndInvalidExpected_reportTheKeyError() { + ddb("UpdateItem", """ + {"TableName":"%s","Key":{"other":{"S":"a"}}, + "AttributeUpdates":{"version":{"Action":"PUT","Value":{"N":"1"}}}, + "Expected":{"version":{}}} + """.formatted(table)).statusCode(400) + .body("__type", equalTo("ValidationException")) + .body("message", equalTo("The provided key element does not match the schema")); + ddb("DeleteItem", """ + {"TableName":"%s","Key":{"other":{"S":"a"}},"Expected":{"version":{}}} + """.formatted(table)).statusCode(400) + .body("__type", equalTo("ValidationException")) + .body("message", equalTo("The provided key element does not match the schema")); + } + + @Test + void putItem_itemWithoutItsKey_reportsTheItemKeyErrorWithAnyReturnValuesOrExpected() { + for (String extra : List.of("", ",\"ReturnValues\":\"ALL_OLD\"", ",\"Expected\":{\"version\":{}}")) { + ddb("PutItem", """ + {"TableName":"%s","Item":{"other":{"S":"a"}}%s} + """.formatted(table, extra)).statusCode(400) + .body("__type", equalTo("ValidationException")) + .body("message", equalTo("One of the required keys was not given a value")); + } + } + + @Test + void updateItem_invalidExpectedOnAMissingTable_reportsTheMissingTable() { + ddb("UpdateItem", """ + {"TableName":"absent-%s","Key":{"pk":{"S":"a"}},"Expected":{"version":{}}} + """.formatted(UUID.randomUUID())).statusCode(400) + .body("__type", equalTo("ResourceNotFoundException")); + } + + private void seed(String pk) { + ddb("PutItem", """ + {"TableName":"%s","Item":{"pk":{"S":"%s"},"version":{"N":"0"}}} + """.formatted(table, pk)).statusCode(200); + } + + /** Seeds a fresh item at version 0 each round and races every writer at it. */ + private void assertOneWriterWinsEachRound(String keyPrefix, BiFunction request) + throws InterruptedException { + for (int round = 0; round < ROUNDS; round++) { + String pk = keyPrefix + "-" + round; + seed(pk); + List responses = runConcurrently(writer -> request.apply(pk, writer)); + int accepted = 0; + for (ValidatableResponse response : responses) { + if (response.extract().statusCode() == 200) { + accepted++; + } else { + response.statusCode(400) + .body("__type", equalTo("ConditionalCheckFailedException")); + } + } + assertEquals(1, accepted, "exactly one writer of a version may be accepted"); + } + } + + /** Sends one request per writer, all released together. */ + private static List runConcurrently(IntFunction request) + throws InterruptedException { + StartGate.Results results = StartGate.runConcurrently(WRITERS, request); + assertTrue(results.errors().isEmpty(), () -> "unexpected errors: " + results.errors()); + return results.values(); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/testing/StartGate.java b/src/test/java/io/github/hectorvent/floci/testing/StartGate.java new file mode 100644 index 0000000000..8f224eaf41 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/testing/StartGate.java @@ -0,0 +1,62 @@ +package io.github.hectorvent.floci.testing; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.function.IntFunction; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** Runs tasks on their own threads, all released together, so a race in the code under test has its best chance. */ +public final class StartGate { + + private StartGate() {} + + /** + * What the tasks returned, in task order, with {@code null} for a task that threw, and what the + * throwing tasks threw. + */ + public record Results(List values, List errors) {} + + /** + * Runs {@code task} once per index from 0 to {@code threadCount - 1}, each on its own thread, and + * waits up to 30 seconds for all of them. + */ + public static Results runConcurrently(int threadCount, IntFunction task) throws InterruptedException { + ExecutorService pool = Executors.newFixedThreadPool(threadCount); + CountDownLatch startGate = new CountDownLatch(1); + CountDownLatch doneGate = new CountDownLatch(threadCount); + Object[] values = new Object[threadCount]; + List errors = Collections.synchronizedList(new ArrayList<>()); + try { + for (int i = 0; i < threadCount; i++) { + int index = i; + pool.submit(() -> { + try { + startGate.await(); + values[index] = task.apply(index); + } catch (Throwable t) { + errors.add(t); + } finally { + doneGate.countDown(); + } + }); + } + startGate.countDown(); + assertTrue(doneGate.await(30, TimeUnit.SECONDS), + "concurrent tasks did not complete within 30s"); + } finally { + pool.shutdownNow(); + assertTrue(pool.awaitTermination(5, TimeUnit.SECONDS), + "thread pool did not terminate"); + } + @SuppressWarnings("unchecked") + List results = (List) Arrays.asList(values); + return new Results<>(results, List.copyOf(errors)); + } +}