diff --git a/docs/services/dynamodb.md b/docs/services/dynamodb.md index aa85b58fac..b61da77b74 100644 --- a/docs/services/dynamodb.md +++ b/docs/services/dynamodb.md @@ -264,7 +264,9 @@ For write-heavy workloads under persistent setups, configuring `FLOCI_STORAGE_SE ### Item layout on disk -With the default `single-file` layout, every table's items live in one `dynamodb-items.json`, and any item write saves all of them: `persistent` rewrites the file on the write, `hybrid` on the next flush, and `wal` appends the whole table to its log and rewrites the whole snapshot on compaction. A small table that changes often, such as a heartbeat table, then keeps rewriting every large table beside it. +In `memory` mode, item writes update the live concurrent table map without copying the table, with either layout. + +With the default `single-file` layout and a durable storage mode, every table's items live in one `dynamodb-items.json`, and any item write saves all of them: `persistent` rewrites the file on the write, `hybrid` on the next flush, and `wal` appends the whole table to its log and rewrites the whole snapshot on compaction. A small table that changes often, such as a heartbeat table, then keeps rewriting every large table beside it. `FLOCI_STORAGE_SERVICES_DYNAMODB_ITEM_LAYOUT=per-table` stores items under `dynamodb-items/` instead, as `FLOCI_STORAGE_SERVICES_DYNAMODB_ITEM_SHARDS` files per table (one by default). An item belongs to the file chosen by a hash of its key, and a write saves only the items it touched: diff --git a/src/main/java/io/github/hectorvent/floci/core/storage/AccountAwareStorageBackend.java b/src/main/java/io/github/hectorvent/floci/core/storage/AccountAwareStorageBackend.java index 4f877ccddf..919f12b543 100644 --- a/src/main/java/io/github/hectorvent/floci/core/storage/AccountAwareStorageBackend.java +++ b/src/main/java/io/github/hectorvent/floci/core/storage/AccountAwareStorageBackend.java @@ -449,6 +449,11 @@ public class AccountAwareStorageBackend implements StorageBackend delegate.deleteAllAtomically(rawKeys); } + /** Whether the delegate only retains values in memory, without persistence or serialization. */ + public boolean isInMemory() { + return delegate instanceof InMemoryStorage; + } + // --- Entry writes, for map-valued stores whose backend persists one entry at a time --- /** Whether the backend persists one map entry at a time; the entry methods require it. */ 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 191279df71..1ab8ef0908 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 @@ -408,11 +408,20 @@ public class DynamoDbService { return rekeyedItems; } + private Map itemsForStorage(Map items) { + // Memory storage can retain the live concurrent map; durable stores still need a snapshot. + if (itemStore instanceof InMemoryStorage + || itemStore instanceof AccountAwareStorageBackend aware && aware.isInMemory()) { + return items; + } + return new HashMap<>(items); + } + private void persistItems(String storageKey) { if (itemStore == null) return; ConcurrentSkipListMap items = currentItems(storageKey, false); if (items != null) { - itemStore.put(storageKey, new HashMap<>(items)); + itemStore.put(storageKey, itemsForStorage(items)); } else { itemStore.delete(storageKey); } @@ -3212,9 +3221,9 @@ public class DynamoDbService { private void persistItemsForAccount(String accountId, String storageKey, Map items) { if (itemStore == null) return; if (accountId != null && itemStore instanceof AccountAwareStorageBackend> aware) { - aware.putForAccount(accountId, storageKey, new HashMap<>(items)); + aware.putForAccount(accountId, storageKey, itemsForStorage(items)); } else { - itemStore.put(storageKey, new HashMap<>(items)); + itemStore.put(storageKey, itemsForStorage(items)); } } diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbMemoryStorageTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbMemoryStorageTest.java new file mode 100644 index 0000000000..b6f2538e80 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbMemoryStorageTest.java @@ -0,0 +1,67 @@ +package io.github.hectorvent.floci.services.dynamodb; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; +import io.github.hectorvent.floci.core.storage.InMemoryStorage; +import io.github.hectorvent.floci.core.storage.StorageBackend; +import io.github.hectorvent.floci.services.dynamodb.model.AttributeDefinition; +import io.github.hectorvent.floci.services.dynamodb.model.KeySchemaElement; +import io.github.hectorvent.floci.services.dynamodb.model.TableDefinition; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.List; +import java.util.Map; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.sameInstance; + +class DynamoDbMemoryStorageTest { + + private final ObjectMapper mapper = new ObjectMapper(); + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void writesRetainLiveMapAndReloadCurrentItems(boolean accountAware) throws Exception { + InMemoryStorage> rawItems = new InMemoryStorage<>(); + InMemoryStorage rawTables = new InMemoryStorage<>(); + for (String account : List.of("000000000001", "000000000002")) { + StorageBackend> items = accountAware + ? new AccountAwareStorageBackend<>(rawItems, null, account) : rawItems; + StorageBackend tables = accountAware + ? new AccountAwareStorageBackend<>(rawTables, null, account) : rawTables; + RegionResolver resolver = new RegionResolver("us-east-1", account); + DynamoDbService service = new DynamoDbService(tables, items, resolver); + String table = accountAware ? "Users" : "Users" + account; + String storageKey = "us-east-1::" + table; + service.createTable(table, List.of(new KeySchemaElement("pk", "HASH")), + List.of(new AttributeDefinition("pk", "S")), 5L, 5L, "us-east-1"); + JsonNode key = mapper.readTree("{\"pk\":{\"S\":\"one\"}}"); + service.putItem(table, key, "us-east-1"); + Map live = items.get(storageKey).orElseThrow(); + service.updateItem(table, key, null, "SET v = :v", null, + mapper.readTree("{\":v\":{\"S\":\"updated\"}}"), "NONE", "us-east-1"); + assertThat(items.get(storageKey).orElseThrow(), sameInstance(live)); + assertThat(live.values().iterator().next().path("v").path("S").asText(), equalTo("updated")); + service.batchWriteItem(Map.of(table, List.of(mapper.readTree( + "{\"PutRequest\":{\"Item\":{\"pk\":{\"S\":\"two\"}}}}"))), "us-east-1"); + service.transactWriteItems(List.of(mapper.readTree( + "{\"Put\":{\"TableName\":\"" + table + "\",\"Item\":{\"pk\":{\"S\":\"three\"}}}}")), "us-east-1"); + service.deleteItem(table, key, "us-east-1"); + assertThat(items.get(storageKey).orElseThrow(), sameInstance(live)); + assertThat(live.size(), equalTo(2)); + DynamoDbService reloaded = new DynamoDbService(tables, items, resolver); + JsonNode second = mapper.readTree("{\"pk\":{\"S\":\"two\"}}"); + assertThat(reloaded.getItem(table, second, "us-east-1"), equalTo(second)); + service.finishRestore(table, List.of(key), "us-east-1"); + service.putItem(table, second, "us-east-1"); + assertThat(items.get(storageKey).orElseThrow().size(), equalTo(2)); + if (accountAware) { + assertThat(rawItems.get("000000000001/" + storageKey).orElseThrow().size(), equalTo(2)); + } + } + } +}