mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -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
+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