fix(storage): dedupe backends by resolved file path (#1931)

* fix(storage): dedupe backends by resolved file path

StorageFactory.create() built a new backend on every call, so two
components opening the same file (DynamoDbService and
DynamoDbStreamService both open dynamodb-tables.json) got independent
in-memory stores. On graceful shutdown flushAll() flushed them in
registration order and the stale duplicate clobbered the file.

Cache backends by resolved file path so repeat create() calls reuse
the same backend. Add a regression test reproducing the clobber.

* fix(storage): guard lifecycle methods and harden dedup test

Synchronize loadAll/flushAll/clearAll/shutdownAll on the same monitor
as create(), so the /state/reset and /state/nuke endpoints can't
iterate allBackends while a lazily-created service mutates it.

Reset the singleton factory in the dedup test setup so it no longer
depends on backend startup state or test ordering.
This commit is contained in:
Bill DeMoss authored and GitHub committed 2026-07-26 15:01:13 -05:00
1 parent 1a51938a35
commit 551df6371b
2 files changed
+100 -5

No files matched your search

@@ -11,6 +11,7 @@ import org.jboss.logging.Logger;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -28,6 +29,9 @@ public class StorageFactory {
private final EmulatorConfig config;
private final ServiceConfigAccess serviceConfigAccess;
private final List<StorageBackend<?, ?>> allBackends = new ArrayList<>();
// A file path identifies one logical store: callers sharing a path are expected to agree on
// its value type and storage mode. The first create() wins; repeat calls reuse that backend.
private final Map<Path, StorageBackend<?, ?>> backendsByPath = new HashMap<>();
private final List<HybridStorage<?, ?>> hybridBackends = new ArrayList<>();
private final List<WalStorage<?, ?>> walBackends = new ArrayList<>();
@@ -50,13 +54,24 @@ public class StorageFactory {
* @param fileName the JSON file name for persistent storage
* @param typeReference Jackson type reference for deserialization
*/
public <V> StorageBackend<String, V> create(String serviceName, String fileName,
public synchronized <V> StorageBackend<String, V> create(String serviceName, String fileName,
TypeReference<Map<String, V>> typeReference) {
String mode = resolveMode(serviceName);
long flushInterval = resolveFlushInterval(serviceName);
Path basePath = Path.of(config.storage().persistentPath());
Path filePath = basePath.resolve(fileName);
// Reuse an existing backend for the same file. Handing out a second backend bound to the
// same path creates a duplicate in-memory store; on shutdown the stale duplicate flushes
// after the active instance and clobbers persisted state (issue #1921).
StorageBackend<?, ?> existing = backendsByPath.get(filePath);
if (existing != null) {
LOG.debugv("Reusing existing {0} storage for service {1} (file: {2})", mode, serviceName, filePath);
@SuppressWarnings("unchecked")
StorageBackend<String, V> typed = (StorageBackend<String, V>) existing;
return typed;
}
LOG.debugv("Creating {0} storage for service {1} (file: {2})", mode, serviceName, filePath);
StorageBackend<String, V> inner = switch (mode) {
@@ -83,25 +98,26 @@ public class StorageFactory {
AccountAwareStorageBackend<V> backend = new AccountAwareStorageBackend<>(
inner, requestContextInstance, config.defaultAccountId());
allBackends.add(backend);
backendsByPath.put(filePath, backend);
return backend;
}
/** Load all storage backends from disk. */
public void loadAll() {
public synchronized void loadAll() {
for (StorageBackend<?, ?> backend : allBackends) {
backend.load();
}
}
/** Flush all storage backends to disk. */
public void flushAll() {
public synchronized void flushAll() {
for (StorageBackend<?, ?> backend : allBackends) {
backend.flush();
}
}
/** Clear all storage backends. */
public void clearAll() {
public synchronized void clearAll() {
for (StorageBackend<?, ?> backend : allBackends) {
backend.clear();
}
@@ -109,7 +125,7 @@ public class StorageFactory {
}
/** Shutdown all managed backends (stop schedulers, close connections). */
public void shutdownAll() {
public synchronized void shutdownAll() {
for (HybridStorage<?, ?> hybrid : hybridBackends) {
hybrid.shutdown();
}