mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
perf(dynamodb): retain the live item map in memory mode instead of copying the table (#6915)
Keep memory item storage current by retaining the concurrent live map. Preserve snapshots for durable backends and cover writes, reloads, restore, and account-scoped storage. Refs #1292
This commit is contained in:
1 parent
479595e6ec
commit
5f89a70095
4 files changed
+87
-4
No files matched your search
@@ -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:
|
||||
|
||||
|
||||
@@ -449,6 +449,11 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
|
||||
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. */
|
||||
|
||||
@@ -408,11 +408,20 @@ public class DynamoDbService {
|
||||
return rekeyedItems;
|
||||
}
|
||||
|
||||
private Map<String, JsonNode> itemsForStorage(Map<String, JsonNode> 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<String, JsonNode> 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<String, JsonNode> items) {
|
||||
if (itemStore == null) return;
|
||||
if (accountId != null && itemStore instanceof AccountAwareStorageBackend<Map<String, JsonNode>> 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));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+67
@@ -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<String, Map<String, JsonNode>> rawItems = new InMemoryStorage<>();
|
||||
InMemoryStorage<String, TableDefinition> rawTables = new InMemoryStorage<>();
|
||||
for (String account : List.of("000000000001", "000000000002")) {
|
||||
StorageBackend<String, Map<String, JsonNode>> items = accountAware
|
||||
? new AccountAwareStorageBackend<>(rawItems, null, account) : rawItems;
|
||||
StorageBackend<String, TableDefinition> 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<String, JsonNode> 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user