mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(dynamodb): save each batch item inside its own item lock (#6314)
With a store that writes single items, BatchWriteItem saved its items in one pass after the item locks were released. That pass could read an item, lose the race to a concurrent write of the same key, and then save the stale copy over it. Save each item inside its write, as PutItem and DeleteItem do; a whole-table store still saves once at the end.
This commit is contained in:
1 parent
2babe62ce4
commit
4f45541d44
2 files changed
+34
-3
No files matched your search
@@ -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());
|
||||
}
|
||||
|
||||
+24
@@ -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()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aWriteToAShardedTableRewritesOneOfItsFiles() throws Exception {
|
||||
Path directory = tempDir.resolve("sharded");
|
||||
|
||||
Reference in new issue
Block a user