feat(dynamodb): opt-in per-table item files, split into shards (#6303)

* feat(dynamodb): opt-in per-table item files, split into shards

Every table's items live in one dynamodb-items.json, and any item write
rewrites all of them. A small table that changes often, such as a node
heartbeat table, then keeps rewriting every large table beside it.

Add an opt-in item layout. With item-layout: per-table, items are stored
under dynamodb-items/ as item-shards files per table, and a write saves
only the items it touched:

- persistent rewrites the one shard file that holds the item before the
  write returns, and fails the write when that file cannot be written
- hybrid marks the shard dirty and writes dirty shards on the flush
  interval; an explicit flush or shutdown reports shards it could not
  write
- wal keeps the single-file layout for now and logs a warning

The default, single-file, is unchanged.

On first start, an existing dynamodb-items.json, or a wal snapshot and
log, is migrated through a staging directory moved in place in one step
and then renamed .migrated; a failed migration stops startup and leaves
the old files untouched. DynamoDbService writes single items through the
new MapEntryWriter, and the TTL sweeper removes each item under its item
lock.

Part of #6293

* test(storage): skip read-only directory tests where POSIX permissions are missing
This commit is contained in:
Ben Wart authored and GitHub committed 2026-10-22 21:27:19 -04:00
1 parent 9eaa4404a4
commit 7635345474
17 files changed
+2327 -20

No files matched your search

@@ -435,6 +435,21 @@ public interface EmulatorConfig {
@WithDefault("5000")
long flushIntervalMs();
/**
* How table items are laid out on disk: {@code single-file} keeps every table in one
* {@code dynamodb-items.json}, rewritten on any change; {@code per-table} writes one file
* per table under {@code dynamodb-items/}, so a change rewrites only its own table.
*/
@WithDefault("single-file")
String itemLayout();
/**
* With the {@code per-table} layout, how many files each table is split into. An item's
* write rewrites only its own file. Changing it rewrites the files on the next start.
*/
@WithDefault("1")
int itemShards();
}
interface SnsStorageConfig {
@@ -426,6 +426,38 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
delegate.delete(accountId + "/" + key);
}
// --- 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. */
public boolean writesEntries() {
return delegate instanceof MapEntryWriter<?>;
}
public <E> void putEntry(String key, String entryKey, E entryValue) {
putEntryForAccount(prefix(), key, entryKey, entryValue);
}
public void removeEntry(String key, String entryKey) {
removeEntryForAccount(prefix(), key, entryKey);
}
public <E> void putEntryForAccount(String accountId, String key, String entryKey, E entryValue) {
this.<E>entryWriter().putEntry(accountId + "/" + key, entryKey, entryValue);
trackOwner(accountId);
}
public void removeEntryForAccount(String accountId, String key, String entryKey) {
entryWriter().removeEntry(accountId + "/" + key, entryKey);
}
@SuppressWarnings("unchecked")
private <E> MapEntryWriter<E> entryWriter() {
if (delegate instanceof MapEntryWriter<?> writer) {
return (MapEntryWriter<E>) writer;
}
throw new UnsupportedOperationException("This store does not persist single map entries");
}
/** Deletes the entry stored under {@code rawKey} exactly, as {@link #scanAllAccountsWithRawKeys} reports it. */
public void deleteRaw(String rawKey) {
delegate.delete(rawKey);
@@ -0,0 +1,17 @@
package io.github.hectorvent.floci.core.storage;
/**
* A store whose values are maps and that can persist one entry of a value without rewriting the
* rest of it. A caller that changes a single entry of a large value (one DynamoDB item of a table)
* uses these instead of {@link StorageBackend#put}, which hands the backend the whole value.
*
* @param <E> the type of one map entry's value
*/
public interface MapEntryWriter<E> {
/** Sets {@code entryKey} in the map stored under {@code key}, creating the map when absent. */
void putEntry(String key, String entryKey, E entryValue);
/** Removes {@code entryKey} from the map stored under {@code key}; a missing map stays missing. */
void removeEntry(String key, String entryKey);
}
@@ -0,0 +1,689 @@
package io.github.hectorvent.floci.core.storage;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.jboss.logging.Logger;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HexFormat;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* File storage for a store whose values are maps, written one file per shard of each key. A
* change to one map entry rewrites the one shard file that holds it, so a store holding one large
* value and one small, busy value (a large DynamoDB table next to a heartbeat table) no longer
* rewrites the large one for every change to the small one, as {@link PersistentStorage} and
* {@link HybridStorage} do with their single file. With more than one shard, a change to a large
* value rewrites only a fraction of it.
*
* <p>An entry's shard is {@code floorMod(entryKey.hashCode(), shards)}; {@link String#hashCode}
* is fixed by the language, so it is stable across runs. Each file records its own key, so a file
* name only has to be unique and safe: it is a readable prefix of the key, a hash of it and, with
* more than one shard, the shard's number and the shard count. Files written under another shard
* count are merged on load, rewritten under the current count and then removed.
*
* <p>A {@link Mode#SYNC} store writes a shard's file before the mutating call returns, like
* {@link PersistentStorage}. A {@link Mode#PERIODIC} store marks the shard dirty and writes every
* dirty shard on the flush interval, like {@link HybridStorage}.
*
* <p>While the directory holds no data, a legacy single-file store (the {@code Map<String,
* Map<String, E>>} file the other backends write, or a {@code wal} snapshot and log) is migrated:
* its keys are written to a staging directory that is moved in place in one step, and the legacy
* files are then renamed with a {@code .migrated} suffix so they are never read again.
*
* @param <E> the type of one map entry's value
*/
public class PartitionedFileStorage<E> implements StorageBackend<String, Map<String, E>>, MapEntryWriter<E> {
private static final Logger LOG = Logger.getLogger(PartitionedFileStorage.class);
static final String FILE_SUFFIX = ".json";
static final String MIGRATING_SUFFIX = ".migrating";
private static final String KEY_FIELD = "key";
private static final String ENTRIES_FIELD = "entries";
private static final int READABLE_PREFIX_LENGTH = 80;
/** When a change reaches disk. */
public enum Mode {
/** Its shard file is rewritten before the mutating call returns. */
SYNC,
/** Dirty shard files are rewritten on the flush interval. */
PERIODIC
}
/** One shard of one key: the unit that is written to a file. */
private record ShardId(String key, int shard) {}
/** The value stored under one key, split into its shards. */
private static final class Shards<E> {
private final List<ConcurrentHashMap<String, E>> parts;
Shards(int count) {
List<ConcurrentHashMap<String, E>> created = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
created.add(new ConcurrentHashMap<>());
}
this.parts = List.copyOf(created);
}
ConcurrentHashMap<String, E> part(int shard) {
return parts.get(shard);
}
Map<String, E> merged() {
Map<String, E> merged = new HashMap<>();
parts.forEach(merged::putAll);
return Collections.unmodifiableMap(merged);
}
}
private final ConcurrentHashMap<String, Shards<E>> store = new ConcurrentHashMap<>();
private final Set<ShardId> dirty = ConcurrentHashMap.newKeySet();
private final ConcurrentHashMap<ShardId, Object> fileLocks = new ConcurrentHashMap<>();
private final Path directory;
private final LegacyStore<E> legacy;
private final int shardCount;
private final Mode mode;
private final ObjectMapper mapper;
private final ObjectReader entriesReader;
private final ObjectWriter entriesWriter;
private final ScheduledExecutorService scheduler;
// Shard writes hold the read lock, and load and clear hold the write lock, so a write that
// read a shard before a clear cannot put its file back afterwards, and the flush schedule,
// which starts with the constructor, cannot write while load is filling the store.
private final ReentrantReadWriteLock fileLock = new ReentrantReadWriteLock();
// Runs inside a shard write after the shard was read and before its file is replaced: the
// moment a clear() that slipped past the lock would be undone. Tests use it to stage that.
private volatile Runnable beforeShardWrite = () -> { };
// Runs on load after superseded files were rewritten under their current names and before
// the old files are removed. Tests use it to make only the removal fail.
private volatile Runnable beforeSupersededDelete = () -> { };
/**
* @param directory where the shard files live
* @param legacyFile a plain single-file store to migrate on first load, or {@code null}
* @param entryType the type of one map entry's value
* @param shardCount how many files each key is split into, at least one
* @param mode when a change reaches disk
* @param flushIntervalMs the flush interval of a {@link Mode#PERIODIC} store
*/
public PartitionedFileStorage(Path directory, Path legacyFile, Class<E> entryType, int shardCount,
Mode mode, long flushIntervalMs) {
this(directory, legacyFile == null ? null : LegacyStore.jsonFile(legacyFile, entryType), entryType,
shardCount, mode, flushIntervalMs);
}
/**
* @param legacy a single-file store to migrate into the directory while it holds no data,
* or {@code null}
*/
public PartitionedFileStorage(Path directory, LegacyStore<E> legacy, Class<E> entryType, int shardCount,
Mode mode, long flushIntervalMs) {
if (shardCount < 1) {
throw new IllegalArgumentException("shardCount must be at least 1, was " + shardCount);
}
this.directory = directory;
this.legacy = legacy;
this.shardCount = shardCount;
this.mode = mode;
this.mapper = new ObjectMapper();
this.mapper.registerModule(new JavaTimeModule());
JavaType entriesType = mapper.getTypeFactory().constructMapType(Map.class, String.class, entryType);
this.entriesReader = mapper.readerFor(entriesType);
this.entriesWriter = mapper.writerFor(entriesType);
this.scheduler = mode == Mode.SYNC
? null
: StorageFiles.scheduleEvery("partitioned-storage-flush", flushIntervalMs, this::flushQuietly);
}
// --- StorageBackend ---
@Override
public void put(String key, Map<String, E> value) {
applyPut(key, value);
persistChange(allShardsOf(key));
}
@Override
public void putAll(Map<String, Map<String, E>> entries) {
entries.forEach(this::put);
}
/** A read-only copy of the stored map. */
@Override
public Optional<Map<String, E>> get(String key) {
Shards<E> value = store.get(key);
return value == null ? Optional.empty() : Optional.of(value.merged());
}
@Override
public void delete(String key) {
applyDelete(key);
persistChange(allShardsOf(key));
}
@Override
public List<Map<String, E>> scan(Predicate<String> keyFilter) {
return store.entrySet().stream()
.filter(e -> keyFilter.test(e.getKey()))
.map(e -> e.getValue().merged())
.collect(Collectors.toCollection(ArrayList::new));
}
@Override
public Set<String> keys() {
return Collections.unmodifiableSet(store.keySet());
}
/**
* Writes every dirty shard.
*
* @throws UncheckedIOException when a shard could not be written; it stays dirty and is
* written again by the next flush
*/
@Override
public void flush() {
int failed = writeDirtyShards();
if (failed > 0) {
throw new UncheckedIOException(new IOException(
"Could not write " + failed + " shards of " + directory + "; see the errors logged above"));
}
}
@Override
public synchronized void load() {
// The flush schedule starts with the constructor: hold it off while the store is filled.
fileLock.writeLock().lock();
try {
store.clear();
dirty.clear();
migrateLegacyFileIfNeeded();
if (Files.isDirectory(directory)) {
loadDirectory();
}
} finally {
fileLock.writeLock().unlock();
}
}
@Override
public synchronized void clear() {
fileLock.writeLock().lock();
try {
store.clear();
dirty.clear();
if (Files.isDirectory(directory)) {
try (DirectoryStream<Path> files = Files.newDirectoryStream(directory, "*" + FILE_SUFFIX)) {
for (Path file : files) {
Files.deleteIfExists(file);
}
} catch (IOException e) {
LOG.errorv(e, "Failed to clear {0}", directory);
}
}
} finally {
fileLock.writeLock().unlock();
}
}
// --- MapEntryWriter ---
@Override
public void putEntry(String key, String entryKey, E entryValue) {
persistChange(List.of(applyPutEntry(key, entryKey, entryValue)));
}
@Override
public void removeEntry(String key, String entryKey) {
ShardId id = applyRemoveEntry(key, entryKey);
if (id != null) {
persistChange(List.of(id));
}
}
// ---
/** Stops the flush schedule and writes what is still dirty. */
public void shutdown() {
if (scheduler != null) {
StorageFiles.stop(scheduler);
}
flush();
}
/**
* Writes the changed shards of a sync store before the call returns; a periodic store leaves
* them to the next flush.
*
* @throws UncheckedIOException when a shard could not be written; the change stays in memory
* and dirty, so a later flush writes it
*/
private void persistChange(List<ShardId> changed) {
if (mode != Mode.SYNC) {
return;
}
int failed = 0;
for (ShardId id : changed) {
if (!writeShard(id)) {
failed++;
}
}
if (failed > 0) {
throw new UncheckedIOException(new IOException(
"A change to " + changed.getFirst().key() + " in " + directory
+ " is in memory but could not be written; see the errors logged above"));
}
}
// The apply methods change memory and mark what they change dirty.
private ShardId applyPutEntry(String key, String entryKey, E entryValue) {
ShardId id = new ShardId(key, shardOf(entryKey));
store.computeIfAbsent(key, ignored -> new Shards<>(shardCount)).part(id.shard()).put(entryKey, entryValue);
dirty.add(id);
return id;
}
/** The shard the entry was removed from, or {@code null} when there was no such entry. */
private ShardId applyRemoveEntry(String key, String entryKey) {
ShardId id = new ShardId(key, shardOf(entryKey));
Shards<E> value = store.get(key);
if (value == null || value.part(id.shard()).remove(entryKey) == null) {
return null;
}
dirty.add(id);
return id;
}
private void applyPut(String key, Map<String, E> value) {
store.put(key, split(value));
markAll(key);
}
private void applyDelete(String key) {
store.remove(key);
markAll(key);
}
/** Writes every dirty shard and returns how many could not be written. */
private int writeDirtyShards() {
int failed = 0;
for (ShardId id : List.copyOf(dirty)) {
if (!writeShard(id)) {
failed++;
}
}
return failed;
}
void setBeforeShardWrite(Runnable hook) {
this.beforeShardWrite = hook;
}
void setBeforeSupersededDelete(Runnable hook) {
this.beforeSupersededDelete = hook;
}
/** The file that holds {@code entryKey} of {@code key}. */
Path fileFor(String key, String entryKey) {
return fileFor(new ShardId(key, shardOf(entryKey)));
}
/** The file of a single-shard store that holds {@code key}. */
Path fileFor(String key) {
return fileFor(new ShardId(key, 0));
}
int shardOf(String entryKey) {
return Math.floorMod(entryKey.hashCode(), shardCount);
}
private Path fileFor(ShardId id) {
return directory.resolve(fileNameFor(id.key(), id.shard(), shardCount));
}
static String fileNameFor(String key, int shard, int shardCount) {
StringBuilder name = new StringBuilder();
for (int i = 0; i < key.length() && name.length() < READABLE_PREFIX_LENGTH; i++) {
char c = key.charAt(i);
boolean safe = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')
|| c == '.' || c == '-' || c == '_';
name.append(safe ? c : '_');
}
name.append('-').append(shortHash(key));
if (shardCount > 1) {
name.append('.').append(shard).append("-of-").append(shardCount);
}
return name.append(FILE_SUFFIX).toString();
}
private static String shortHash(String key) {
try {
byte[] digest = MessageDigest.getInstance("SHA-256").digest(key.getBytes(StandardCharsets.UTF_8));
return HexFormat.of().formatHex(digest, 0, 8);
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException("SHA-256 is unavailable", e);
}
}
private Shards<E> split(Map<String, E> value) {
Shards<E> shards = new Shards<>(shardCount);
value.forEach((entryKey, entryValue) -> shards.part(shardOf(entryKey)).put(entryKey, entryValue));
return shards;
}
private void markAll(String key) {
for (int shard = 0; shard < shardCount; shard++) {
dirty.add(new ShardId(key, shard));
}
}
private List<ShardId> allShardsOf(String key) {
List<ShardId> ids = new ArrayList<>(shardCount);
for (int shard = 0; shard < shardCount; shard++) {
ids.add(new ShardId(key, shard));
}
return ids;
}
/**
* The flush timer's task. It must not throw: an exception escaping a scheduled task cancels
* every later run, so one bad flush would stop the store from ever writing again. A failure
* is logged here and its shards stay dirty, so the next run retries them; an explicit
* {@link #flush()} (shutdown included) reports the same failure to its caller.
*/
private void flushQuietly() {
try {
flush();
} catch (RuntimeException e) {
LOG.errorv(e, "Scheduled flush of {0} failed; its unwritten shards are retried on the next one",
directory);
}
}
/**
* Writes one shard's file, or deletes it when the shard is empty or its key is gone. The dirty
* mark is cleared before the shard is read, so a change racing the write marks it again and
* is written next.
*
* @return {@code false} when the file could not be written; the shard is then dirty again
*/
private boolean writeShard(ShardId id) {
// clear() holds the write side, so a write that read a shard before the clear cannot put
// its file back after the clear removed it. Load already holds the write side, which lets
// it take the read side here.
fileLock.readLock().lock();
try {
return writeShardLocked(id);
} finally {
fileLock.readLock().unlock();
}
}
private boolean writeShardLocked(ShardId id) {
Object lock = fileLocks.computeIfAbsent(id, ignored -> new Object());
synchronized (lock) {
if (!dirty.remove(id)) {
return true;
}
Path file = fileFor(id);
try {
Shards<E> value = store.get(id.key());
ConcurrentHashMap<String, E> part = value == null ? null : value.part(id.shard());
if (part == null || part.isEmpty()) {
Files.deleteIfExists(file);
return true;
}
beforeShardWrite.run();
StorageFiles.writeAtomically(file, tempFile -> writeShardContent(tempFile, id.key(), part));
return true;
} catch (IOException e) {
dirty.add(id);
LOG.errorv(e, "Failed to persist shard {0} of {1} to {2}", id.shard(), id.key(), file);
return false;
}
}
}
private void writeShardFile(Path file, String key, Map<String, E> part) throws IOException {
StorageFiles.writeAtomically(file, tempFile -> writeShardContent(tempFile, key, part));
}
private void writeShardContent(Path file, String key, Map<String, E> part) throws IOException {
try (OutputStream out = Files.newOutputStream(file);
JsonGenerator generator = mapper.getFactory().createGenerator(out)) {
generator.writeStartObject();
generator.writeStringField(KEY_FIELD, key);
generator.writeFieldName(ENTRIES_FIELD);
entriesWriter.writeValue(generator, part);
generator.writeEndObject();
}
}
private void loadDirectory() {
List<Path> superseded = new ArrayList<>();
try (DirectoryStream<Path> files = Files.newDirectoryStream(directory, "*" + FILE_SUFFIX)) {
for (Path file : files) {
String key = readFile(file);
if (key != null && !isCurrentFileOf(file, key)) {
superseded.add(file);
markAll(key);
}
}
} catch (IOException e) {
LOG.errorv(e, "Failed to list {0}", directory);
}
LOG.infov("Loaded {0} keys from {1}", store.size(), directory);
if (superseded.isEmpty()) {
return;
}
// Written under another name or shard count: write each key under its current files
// first, and only then drop the old ones, so a crash in between loses nothing. An old
// file must not outlive this load: later writes change only the current files, so the
// next load would merge it back in and revive what they deleted or overwrote.
writeDirtyShards();
if (!dirty.isEmpty()) {
throw new IllegalStateException("Could not rewrite " + dirty.size() + " shards of " + directory
+ " from superseded files; they are kept, fix the cause and restart");
}
beforeSupersededDelete.run();
for (Path file : superseded) {
try {
Files.deleteIfExists(file);
} catch (IOException e) {
throw new UncheckedIOException("Failed to remove superseded file " + file
+ "; its entries are in the current files, remove it and restart", e);
}
}
LOG.infov("Rewrote {0} superseded files of {1} as {2} shards per key", superseded.size(), directory,
shardCount);
}
private boolean isCurrentFileOf(Path file, String key) {
for (int shard = 0; shard < shardCount; shard++) {
if (file.equals(fileFor(new ShardId(key, shard)))) {
return true;
}
}
return false;
}
/** Merges one file into the store and returns its key, or {@code null} when it is unreadable. */
private String readFile(Path file) {
String key = null;
Map<String, E> entries = null;
try (JsonParser parser = mapper.getFactory().createParser(file.toFile())) {
if (parser.nextToken() != JsonToken.START_OBJECT) {
throw new IOException("not a JSON object");
}
while (parser.nextToken() == JsonToken.FIELD_NAME) {
String field = parser.currentName();
parser.nextToken();
switch (field) {
case KEY_FIELD -> key = parser.getValueAsString();
case ENTRIES_FIELD -> entries = entriesReader.readValue(parser);
default -> parser.skipChildren();
}
}
if (key == null || entries == null) {
throw new IOException("missing \"" + KEY_FIELD + "\" or \"" + ENTRIES_FIELD + "\"");
}
} catch (IOException e) {
StorageQuarantine.quarantine(file, e, LOG);
return null;
}
Shards<E> shards = store.computeIfAbsent(key, ignored -> new Shards<>(shardCount));
entries.forEach((entryKey, entryValue) -> shards.part(shardOf(entryKey)).put(entryKey, entryValue));
return key;
}
private void migrateLegacyFileIfNeeded() {
Path staging = directory.resolveSibling(directory.getFileName() + MIGRATING_SUFFIX);
// A staging directory left over is a migration that crashed before it was moved in place.
deleteRecursively(staging);
if (legacy == null) {
return;
}
List<Path> sources = legacy.existingFiles();
if (sources.isEmpty()) {
return;
}
if (holdsData(directory)) {
// Moved in place by a migration that crashed before renaming its sources, or written
// since: either way the directory is newer than the legacy files.
LOG.warnv("Ignoring {0}: {1} already holds data", sources, directory);
return;
}
Map<String, Map<String, E>> entries;
try {
entries = legacy.read();
} catch (IOException e) {
for (Path source : sources) {
StorageQuarantine.quarantine(source, e, LOG);
}
return;
}
// Every file is written to a staging directory that is moved in place in one step, so a
// crash or a failed write never leaves a directory holding only part of the legacy store.
try {
Files.createDirectories(staging);
for (Map.Entry<String, Map<String, E>> entry : entries.entrySet()) {
Shards<E> shards = split(entry.getValue());
for (int shard = 0; shard < shardCount; shard++) {
ConcurrentHashMap<String, E> part = shards.part(shard);
if (!part.isEmpty()) {
writeShardFile(staging.resolve(fileNameFor(entry.getKey(), shard, shardCount)),
entry.getKey(), part);
}
}
}
if (Files.isDirectory(directory)) {
// Empty (holdsData said so): make way for the move.
Files.delete(directory);
}
Files.move(staging, directory, StandardCopyOption.ATOMIC_MOVE);
} catch (IOException e) {
deleteRecursively(staging);
// Starting empty would let new writes fill the directory, after which the legacy
// files are ignored for good: refuse to start instead.
throw new UncheckedIOException("Failed to migrate " + sources + " into " + directory
+ "; the legacy files are untouched", e);
}
for (Path source : sources) {
Path migrated = source.resolveSibling(source.getFileName() + ".migrated");
try {
Files.move(source, migrated, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
LOG.errorv(e, "Migrated {0} into {1} but could not rename it", source, directory);
}
}
LOG.infov("Migrated {0} keys from {1} into {2}; the old files now end in .migrated",
entries.size(), sources, directory);
}
/** Whether {@code dir} holds anything at all. */
private static boolean holdsData(Path dir) {
if (!Files.isDirectory(dir)) {
return false;
}
try (DirectoryStream<Path> files = Files.newDirectoryStream(dir)) {
return files.iterator().hasNext();
} catch (IOException e) {
throw new UncheckedIOException("Failed to list " + dir, e);
}
}
private static void deleteRecursively(Path dir) {
if (!Files.exists(dir)) {
return;
}
try (Stream<Path> paths = Files.walk(dir)) {
for (Path path : paths.sorted(Comparator.reverseOrder()).toList()) {
Files.delete(path);
}
} catch (IOException e) {
throw new UncheckedIOException("Failed to remove " + dir, e);
}
}
/**
* A single-file store to migrate into an empty directory on load: the file a {@code
* persistent} or {@code hybrid} store writes, or the snapshot and log of a {@code wal} one.
*/
public interface LegacyStore<E> {
/** The files the store occupies that exist now; empty when there is nothing to migrate. */
List<Path> existingFiles();
/** Every key and its map, read without changing what the files hold. */
Map<String, Map<String, E>> read() throws IOException;
/** The plain JSON file of a {@code persistent} or {@code hybrid} store. */
static <E> LegacyStore<E> jsonFile(Path file, Class<E> entryType) {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
JavaType type = mapper.getTypeFactory().constructMapType(Map.class,
mapper.getTypeFactory().constructType(String.class),
mapper.getTypeFactory().constructMapType(Map.class, String.class, entryType));
return new LegacyStore<>() {
@Override
public List<Path> existingFiles() {
return Files.exists(file) ? List.of(file) : List.of();
}
@Override
public Map<String, Map<String, E>> read() throws IOException {
return mapper.readValue(file.toFile(), type);
}
};
}
}
}
@@ -9,6 +9,9 @@ import jakarta.enterprise.inject.Instance;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
@@ -17,6 +20,7 @@ import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;
/**
* Factory that creates {@link AccountAwareStorageBackend} instances based on configuration.
@@ -37,6 +41,7 @@ public class StorageFactory {
private final Map<Path, StorageBackend<?, ?>> backendsByPath = new HashMap<>();
private final List<HybridStorage<?, ?>> hybridBackends = new ArrayList<>();
private final List<WalStorage<?, ?>> walBackends = new ArrayList<>();
private final List<PartitionedFileStorage<?>> partitionedBackends = new ArrayList<>();
// Backends whose initial load has completed. create() loads each backend exactly once and
// loadAll() only picks up backends that were never loaded, so neither path replays a store
// that is already live or opens a second WAL writer on top of the first.
@@ -143,6 +148,122 @@ public class StorageFactory {
return backend;
}
/**
* Create an account-aware store whose values are maps, persisted under a directory named
* after {@code fileName} (without {@code .json}) as {@code shardCount} files per key, so a
* change to one entry rewrites only the file of its shard. Writers that change one map entry at a time use
* {@link AccountAwareStorageBackend#putEntry} and {@link AccountAwareStorageBackend#removeEntry}
* when {@link AccountAwareStorageBackend#writesEntries()} is true.
*
* <p>{@code persistent} writes a shard's file on every change and {@code hybrid} writes dirty
* shards on the flush interval. {@code memory} keeps the store in memory, and {@code wal}
* keeps its single-file {@link WalStorage} for now. An existing single-file store (a
* {@code wal}-mode snapshot and log included) is migrated into the directory on first load.
*/
public synchronized <E> AccountAwareStorageBackend<Map<String, E>> createPartitioned(
String serviceName, String fileName, TypeReference<Map<String, Map<String, E>>> typeReference,
Class<E> entryType, int shardCount) {
String mode = resolveMode(serviceName);
if ("memory".equals(mode)) {
return create(serviceName, fileName, typeReference);
}
if ("wal".equals(mode)) {
LOG.warnv("The per-table layout does not support wal mode yet; storing {0} for service {1} "
+ "in the single-file layout", fileName, serviceName);
return create(serviceName, fileName, typeReference);
}
Path basePath = Path.of(config.storage().persistentPath());
Path directory = basePath.resolve(fileName.replace(".json", ""));
StorageBackend<?, ?> existing = backendsByPath.get(directory);
if (existing != null) {
@SuppressWarnings("unchecked")
AccountAwareStorageBackend<Map<String, E>> typed = (AccountAwareStorageBackend<Map<String, E>>) existing;
return typed;
}
PartitionedFileStorage.Mode writeMode;
long interval;
switch (mode) {
case "persistent" -> {
writeMode = PartitionedFileStorage.Mode.SYNC;
interval = 0;
}
case "hybrid" -> {
writeMode = PartitionedFileStorage.Mode.PERIODIC;
interval = resolveFlushInterval(serviceName);
}
default -> throw new IllegalArgumentException("Unknown storage mode: " + mode);
}
PartitionedFileStorage<E> inner = new PartitionedFileStorage<>(directory,
legacyStore(basePath, fileName, typeReference, entryType), entryType, shardCount, writeMode,
interval);
partitionedBackends.add(inner);
LOG.infov("Storing {0} for service {1} in {2}, {3} file(s) per key", fileName, serviceName, directory,
shardCount);
AccountAwareStorageBackend<Map<String, E>> backend = new AccountAwareStorageBackend<>(
inner, requestContextInstance, config.defaultAccountId());
loadOnce(backend);
allBackends.add(backend);
backendsByPath.put(directory, backend);
return backend;
}
/**
* The single-file store to migrate into a partitioned store, whatever mode it last ran in:
* the plain file a {@code persistent} or {@code hybrid} store writes, or the snapshot and log
* of a {@link WalStorage}. When both exist, the one written last is the live one. Nothing is
* folded or deleted here; the partitioned store renames the files once its migration lands.
*/
private static <E> PartitionedFileStorage.LegacyStore<E> legacyStore(
Path basePath, String fileName, TypeReference<Map<String, Map<String, E>>> typeReference,
Class<E> entryType) {
Path plainFile = basePath.resolve(fileName);
Path snapshot = basePath.resolve(fileName.replace(".json", "-snapshot.json"));
Path log = basePath.resolve(fileName.replace(".json", ".wal"));
PartitionedFileStorage.LegacyStore<E> plain = PartitionedFileStorage.LegacyStore.jsonFile(plainFile, entryType);
PartitionedFileStorage.LegacyStore<E> wal = new PartitionedFileStorage.LegacyStore<>() {
@Override
public List<Path> existingFiles() {
return Stream.of(snapshot, log).filter(Files::exists).toList();
}
@Override
public Map<String, Map<String, E>> read() {
return WalStorage.readContents(snapshot, log, typeReference);
}
};
return new PartitionedFileStorage.LegacyStore<>() {
private PartitionedFileStorage.LegacyStore<E> live() {
List<Path> walFiles = wal.existingFiles();
if (walFiles.isEmpty()) {
return plain;
}
if (!Files.exists(plainFile)) {
return wal;
}
return lastModified(plainFile) > walFiles.stream().mapToLong(StorageFactory::lastModified).max()
.orElse(Long.MIN_VALUE) ? plain : wal;
}
@Override
public List<Path> existingFiles() {
return live().existingFiles();
}
@Override
public Map<String, Map<String, E>> read() throws IOException {
return live().read();
}
};
}
private static long lastModified(Path file) {
try {
return Files.getLastModifiedTime(file).toMillis();
} catch (IOException e) {
throw new UncheckedIOException("Failed to read the modification time of " + file, e);
}
}
/** Load every managed backend that has not been loaded yet. Safe to call repeatedly. */
public synchronized void loadAll() {
for (StorageBackend<?, ?> backend : allBackends) {
@@ -158,11 +279,16 @@ public class StorageFactory {
loadedBackends.add(backend);
}
/** Flush all storage backends to disk. */
/**
* Flush all storage backends to disk. Every backend is flushed even when one fails; the first
* failure is then thrown, with the others attached as suppressed.
*/
public synchronized void flushAll() {
List<RuntimeException> failures = new ArrayList<>();
for (StorageBackend<?, ?> backend : allBackends) {
backend.flush();
runCollecting(backend::flush, failures);
}
throwFirst(failures);
}
/** Clear all storage backends. */
@@ -179,13 +305,37 @@ public class StorageFactory {
* every WAL backend with an open writer that nothing closes.
*/
public synchronized void shutdownAll() {
flushAll();
List<RuntimeException> failures = new ArrayList<>();
runCollecting(this::flushAll, failures);
for (HybridStorage<?, ?> hybrid : hybridBackends) {
hybrid.shutdown();
runCollecting(hybrid::shutdown, failures);
}
for (WalStorage<?, ?> wal : walBackends) {
wal.shutdown();
runCollecting(wal::shutdown, failures);
}
for (PartitionedFileStorage<?> partitioned : partitionedBackends) {
runCollecting(partitioned::shutdown, failures);
}
throwFirst(failures);
}
private static void runCollecting(Runnable step, List<RuntimeException> failures) {
try {
step.run();
} catch (RuntimeException e) {
failures.add(e);
}
}
private static void throwFirst(List<RuntimeException> failures) {
if (failures.isEmpty()) {
return;
}
RuntimeException first = failures.getFirst();
for (RuntimeException other : failures.subList(1, failures.size())) {
first.addSuppressed(other);
}
throw first;
}
private String resolveMode(String serviceName) {
@@ -15,6 +15,7 @@ import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -193,6 +194,30 @@ public class WalStorage<K, V> implements StorageBackend<K, V> {
closeWalWriter();
}
/**
* Reads the store a snapshot and its log hold, replaying the log over the snapshot, and
* leaves both files as they are: nothing is compacted or deleted.
*/
static <K, V> Map<K, V> readContents(Path snapshotPath, Path walPath, TypeReference<Map<K, V>> typeReference) {
WalStorage<K, V> reader = new WalStorage<>(snapshotPath, walPath, typeReference, Long.MAX_VALUE);
boolean hadLog = Files.exists(walPath);
try {
reader.load();
return new HashMap<>(reader.store);
} finally {
StorageFiles.stop(reader.scheduler);
reader.closeWalWriter();
if (!hadLog) {
// load() opens the log for appends, creating it; this read must leave no file behind.
try {
Files.deleteIfExists(walPath);
} catch (IOException e) {
LOG.warnv(e, "Failed to remove the empty log {0} a read created", walPath);
}
}
}
}
private void compact() {
compactionLock.writeLock().lock();
try {
@@ -53,6 +53,7 @@ import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
@@ -169,8 +170,7 @@ public class DynamoDbService {
EmulatorConfig config) {
this(storageFactory.create("dynamodb", "dynamodb-tables.json",
new TypeReference<Map<String, TableDefinition>>() {}),
storageFactory.create("dynamodb", "dynamodb-items.json",
new TypeReference<Map<String, Map<String, JsonNode>>>() {}),
itemStore(storageFactory, config),
storageFactory.create("dynamodb", "dynamodb-exports.json",
new TypeReference<Map<String, ExportDescription>>() {}),
storageFactory.create("dynamodb", "dynamodb-imports.json",
@@ -185,6 +185,20 @@ public class DynamoDbService {
config.services().dynamodb().latestRestorableLagSeconds(), Clock.systemUTC());
}
/** The item store the configured {@code item-layout} selects; package-private for tests. */
static StorageBackend<String, Map<String, JsonNode>> itemStore(StorageFactory storageFactory,
EmulatorConfig config) {
TypeReference<Map<String, Map<String, JsonNode>>> type = new TypeReference<>() {};
String layout = Objects.requireNonNullElse(config.storage().services().dynamodb().itemLayout(), "single-file");
return switch (layout) {
case "single-file" -> storageFactory.create("dynamodb", "dynamodb-items.json", type);
case "per-table" -> storageFactory.createPartitioned("dynamodb", "dynamodb-items.json", type,
JsonNode.class, config.storage().services().dynamodb().itemShards());
default -> throw new IllegalArgumentException(
"Unknown floci.storage.services.dynamodb.item-layout: " + layout);
};
}
/** Package-private constructor for testing. */
DynamoDbService(StorageBackend<String, TableDefinition> tableStore) {
this(tableStore, null, null, null, new RegionResolver("us-east-1", "000000000000"), null, null, null, null); // partition-literal: test-shaped constructor default
@@ -316,8 +330,17 @@ public class DynamoDbService {
// scanAllAccountsRaw() returns every account's items already in the "accountId/
// region::tableName" key format itemsByTable expects.
if (itemStore instanceof AccountAwareStorageBackend<Map<String, JsonNode>> aware) {
aware.scanAllAccountsRaw().forEach((rawKey, items) ->
itemsByTable.put(rawKey, rekeyPersistedItems(persistedTables.get(rawKey), items)));
aware.scanAllAccountsRaw().forEach((rawKey, items) -> {
ConcurrentSkipListMap<String, JsonNode> rekeyed = rekeyPersistedItems(persistedTables.get(rawKey), items);
itemsByTable.put(rawKey, rekeyed);
int slash = rawKey.indexOf('/');
if (aware.writesEntries() && slash > 0 && !rekeyed.keySet().equals(items.keySet())) {
// Entry writes address items by their current key, so a store still holding
// older keys would keep an item that is later deleted under its new one.
aware.putForAccount(rawKey.substring(0, slash), rawKey.substring(slash + 1),
new HashMap<>(rekeyed));
}
});
return;
}
for (String key : itemStore.keys()) {
@@ -361,6 +384,50 @@ public class DynamoDbService {
}
}
/**
* Persists the current state of the given items of one table. A store that writes single map
* entries gets only those items; any other store gets the whole table, as before.
*/
private void persistItems(String storageKey, Collection<String> itemKeys) {
AccountAwareStorageBackend<Map<String, JsonNode>> entryStore = entryWritingItemStore();
if (entryStore == null) {
persistItems(storageKey);
return;
}
ConcurrentSkipListMap<String, JsonNode> items = currentItems(storageKey, false);
if (items == null) {
itemStore.delete(storageKey);
return;
}
for (String itemKey : itemKeys) {
JsonNode item = items.get(itemKey);
if (item == null) {
entryStore.removeEntry(storageKey, itemKey);
} else {
entryStore.putEntry(storageKey, itemKey, item);
}
}
}
private void persistItem(String storageKey, String itemKey) {
persistItems(storageKey, List.of(itemKey));
}
/** Persists the given items of every table a multi-item write touched. */
private void persistItems(Collection<String> storageKeys, Map<String, Set<String>> itemKeysByTable) {
for (String storageKey : storageKeys) {
persistItems(storageKey, itemKeysByTable.getOrDefault(storageKey, Set.of()));
}
}
/** The item store when it persists one item at a time, otherwise {@code null}. */
private AccountAwareStorageBackend<Map<String, JsonNode>> entryWritingItemStore() {
if (itemStore instanceof AccountAwareStorageBackend<Map<String, JsonNode>> aware && aware.writesEntries()) {
return aware;
}
return null;
}
public TableDefinition createTable(String tableName,
List<KeySchemaElement> keySchema,
List<AttributeDefinition> attributeDefinitions,
@@ -1043,7 +1110,7 @@ public class DynamoDbService {
}
tableItems.put(itemKey, normalizedItem);
if (shouldPersist) {
persistItems(storageKey);
persistItem(storageKey, itemKey);
}
LOG.debugv("Put item in {0}: key={1}", canonicalTableName, itemKey);
LOG.tracev("Put item in {0}: key={1} item={2}", canonicalTableName, itemKey, item);
@@ -1162,7 +1229,7 @@ public class DynamoDbService {
}
JsonNode removed = items.remove(itemKey);
if (shouldPersist) {
persistItems(storageKey);
persistItem(storageKey, itemKey);
}
LOG.debugv("Deleted item from {0}: key={1}", canonicalTableName, itemKey);
LOG.tracev("Deleted item from {0}: key={1} removed={2}", canonicalTableName, itemKey, removed);
@@ -1398,7 +1465,7 @@ public class DynamoDbService {
}
items.put(itemKey, item);
if (shouldPersist) {
persistItems(storageKey);
persistItem(storageKey, itemKey);
}
LOG.tracev("Updated item in {0}: key={1} updateExpression={2} item={3}",
canonicalTableName, itemKey, updateExpression, item);
@@ -1740,6 +1807,7 @@ public class DynamoDbService {
public BatchWriteResult batchWriteItem(Map<String, List<JsonNode>> requestItems, String region) {
// Pre-validate all write requests before applying any mutation to memory or streams
Map<String, Set<String>> requestedItemKeys = new HashMap<>();
for (Map.Entry<String, List<JsonNode>> entry : requestItems.entrySet()) {
String tableName = canonicalTableName(region, entry.getKey());
String storageKey = regionKey(region, tableName);
@@ -1769,6 +1837,7 @@ public class DynamoDbService {
throw new AwsException("ValidationException",
"Provided list of item keys contains duplicates", 400);
}
requestedItemKeys.computeIfAbsent(storageKey, ignored -> new HashSet<>()).add(itemKey);
}
}
@@ -1790,9 +1859,7 @@ public class DynamoDbService {
}
}
} finally {
for (String storageKey : affectedStorageKeys) {
persistItems(storageKey);
}
persistItems(affectedStorageKeys, requestedItemKeys);
}
return new BatchWriteResult(Map.of());
}
@@ -1998,9 +2065,12 @@ public class DynamoDbService {
live.put(participant.itemKey(), value);
}
}
for (String storageKey : affectedStorageKeys) {
persistItems(storageKey);
Map<String, Set<String>> participantKeys = new HashMap<>();
for (TransactParticipant participant : toAcquire.keySet()) {
participantKeys.computeIfAbsent(participant.storageKey(), ignored -> new HashSet<>())
.add(participant.itemKey());
}
persistItems(affectedStorageKeys, participantKeys);
for (Runnable streamEvent : pendingStreamEvents) {
streamEvent.run();
}
@@ -3002,6 +3072,10 @@ public class DynamoDbService {
ConcurrentSkipListMap<String, JsonNode> items = scan.items();
int deletedForTable = 0;
// A store that writes single entries drops each item under its item lock, so a write
// that replaces the item right after cannot be overtaken by this removal.
AccountAwareStorageBackend<Map<String, JsonNode>> entryStore =
scan.accountId() == null ? null : entryWritingItemStore();
for (String itemKey : scan.itemKeys()) {
// A table deleted, or deleted and recreated, since the scan or during this sweep: its items are not this scan's.
if (itemsByTable.get(scan.rawKey()) != items) {
@@ -3013,7 +3087,18 @@ public class DynamoDbService {
return null;
}
pointInTimeRecovery.recordItem(scan.accountId(), scan.storageKey(), scan.table(), itemKey, current);
return items.remove(itemKey);
JsonNode gone = items.remove(itemKey);
if (entryStore != null) {
// Under the table entry's lock, so a DeleteTable and a recreate cannot slip
// in and have this removal drop the new table's item of the same key.
itemsByTable.computeIfPresent(scan.rawKey(), (key, live) -> {
if (live == items) {
entryStore.removeEntryForAccount(scan.accountId(), scan.storageKey(), itemKey);
}
return live;
});
}
return gone;
});
if (removed == null) {
continue;
@@ -3029,7 +3114,7 @@ public class DynamoDbService {
true);
}
}
if (deletedForTable > 0) {
if (deletedForTable > 0 && entryStore == null) {
// A DeleteTable during the loop detached this map; writing it back would revive its items.
// Persisting under the entry's lock holds off DeleteTable, which removes the entry before
// it deletes the stored items.
@@ -3039,8 +3124,8 @@ public class DynamoDbService {
}
return live;
});
totalDeleted += deletedForTable;
}
totalDeleted += deletedForTable;
}
if (totalDeleted > 0) {
LOG.infov("TTL sweeper removed {0} expired items", totalDeleted);