Light, fluffy, and always free - The AWS Local Emulator alternative
fix(dynamodb): save each batch item inside its own item lock (#6314)
Ben Wart4f45541d44e8
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.javaindex d7f01a754f..9b90614993 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@@ -1841,6 +1841,11 @@ public class DynamoDbService {}}+ // A store that writes single items saves each one inside its item lock, as PutItem does: a+ // pass after the locks are released could read an item, lose the race to a concurrent+ // write of the same key, and then save the stale copy over it. A whole-table store is+ // saved once at the end, as a copy of the live table.+ boolean persistEachItem = entryWritingItemStore() != null;Set<String> affectedStorageKeys = new LinkedHashSet<>();try {for (Map.Entry<String, List<JsonNode>> entry : requestItems.entrySet()) {@@ -1849,17 +1854,19 @@ public class DynamoDbService {for (JsonNode writeRequest : entry.getValue()) {if (writeRequest.has("PutRequest")) {JsonNode item = writeRequest.get("PutRequest").get("Item");- putItemInternal(tableName, item, null, null, null, region, "NONE", false);+ putItemInternal(tableName, item, null, null, null, region, "NONE", persistEachItem);affectedStorageKeys.add(storageKey);} else if (writeRequest.has("DeleteRequest")) {JsonNode key = writeRequest.get("DeleteRequest").get("Key");- deleteItemInternal(tableName, key, null, null, null, region, "NONE", false);+ deleteItemInternal(tableName, key, null, null, null, region, "NONE", persistEachItem);affectedStorageKeys.add(storageKey);}}}} finally {- persistItems(affectedStorageKeys, requestedItemKeys);+ if (!persistEachItem) {+ persistItems(affectedStorageKeys, requestedItemKeys);+ }}return new BatchWriteResult(Map.of());}diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartitionedItemStoreTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartitionedItemStoreTest.javaindex 14fd3b45eb..d259d80738 100644--- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartitionedItemStoreTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartitionedItemStoreTest.java@@ -167,6 +167,30 @@ class DynamoDbPartitionedItemStoreTest {assertEquals("beat-2", value(restart().getItem("Nodes", key("node-1"), REGION)));}+ @Test+ void eachBatchItemIsSavedInsideItsOwnWrite() {+ DynamoDbStreamService streams = mock(DynamoDbStreamService.class);+ DynamoDbService batching = new DynamoDbService(tableStore, openItemStore(), resolver, streams, null);+ List<JsonNode> onDiskWhenTheSecondItemWasWritten = new ArrayList<>();+ // A save deferred until after the batch releases its item locks could read an item, lose+ // the race to a concurrent write of the same key, then save the stale copy over it. So by+ // the time the batch writes its second item, the first must already be on disk.+ doAnswer(invocation -> {+ JsonNode written = invocation.getArgument(2);+ if ("node-b".equals(written.get("id").get("S").asText())) {+ onDiskWhenTheSecondItemWasWritten.add(restart().getItem("Nodes", key("node-a"), REGION));+ }+ return null;+ }).when(streams).captureEvent(eq("INSERT"), any(), any(), any(), any());++ batching.batchWriteItem(Map.of("Nodes", List.of(+ putRequest(item("node-a", "a")),+ putRequest(item("node-b", "b")))), REGION);++ assertEquals(1, onDiskWhenTheSecondItemWasWritten.size());+ assertEquals("a", value(onDiskWhenTheSecondItemWasWritten.getFirst()));+ }+@Testvoid aWriteToAShardedTableRewritesOneOfItsFiles() throws Exception {Path directory = tempDir.resolve("sharded");