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

@@ -0,0 +1,79 @@
package io.github.hectorvent.floci.core.storage;
import com.fasterxml.jackson.core.type.TypeReference;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;
import jakarta.inject.Inject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Reproduces issue #1921: two components request the same storage file, the factory hands out
* two independent backends, and on graceful shutdown the stale duplicate flushes last and
* clobbers the persisted state written by the active instance.
*/
@QuarkusTest
@TestProfile(StorageFactoryDuplicateBackendTest.PersistentProfile.class)
class StorageFactoryDuplicateBackendTest {
private static final String STORAGE_PATH = "/tmp/floci-dedupe-backend-test";
private static final String FILE_NAME = "dynamodb-tables.json";
@Inject
StorageFactory storageFactory;
@BeforeEach
void reset() throws IOException {
// StorageFactory is an application-scoped singleton, so a backend for this file may already
// exist and hold in-memory state from app startup. Clear all backends (wipes memory + disk),
// then remove the file so both create() calls below start from an empty store.
storageFactory.clearAll();
Files.deleteIfExists(Path.of(STORAGE_PATH, FILE_NAME));
}
@Test
void duplicateBackendsForSameFileDoNotClobberOnFlush() {
TypeReference<Map<String, String>> typeRef = new TypeReference<>() {};
// Active component (e.g. DynamoDbService) opens the tables file.
StorageBackend<String, String> active = storageFactory.create("dynamodb", FILE_NAME, typeRef);
// A second component (e.g. DynamoDbStreamService) opens the same file later.
storageFactory.create("dynamodb", FILE_NAME, typeRef);
// Active component writes tables; persistent mode write-throughs to disk.
for (int i = 0; i < 60; i++) {
active.put("table-" + i, "definition-" + i);
}
// Graceful shutdown flushes every registered backend. The stale duplicate must not
// overwrite the active instance's data.
storageFactory.flushAll();
// Read the persisted file back with an independent reader.
PersistentStorage<String, String> reader =
new PersistentStorage<>(Path.of(STORAGE_PATH, FILE_NAME), typeRef);
reader.load();
assertEquals(60, reader.keys().size(),
"persisted table definitions should survive flushAll despite the duplicate backend");
}
public static final class PersistentProfile implements QuarkusTestProfile {
@Override
public Map<String, String> getConfigOverrides() {
return Map.of(
"floci.storage.mode", "memory",
"floci.storage.services.dynamodb.mode", "persistent",
"floci.storage.persistent-path", STORAGE_PATH
);
}
}
}