fix(storage): load each backend once and close WAL writers on shutdown (#3469)

* fix(storage): load each backend once and close WAL writers on shutdown

StorageFactory.shutdownAll() ran the final flush after the WAL backends had
been shut down. A WAL flush compacts and reopens the writer, so every WAL
backend was left with an open writer after shutdown. loadAll() also reloaded
backends that create() had already loaded, replaying live stores and opening
a second WAL writer on top of the first.

create() now owns the initial load of a backend, loadAll() only loads
backends that have not been loaded yet, and shutdownAll() flushes before it
shuts the backends down. Memory, persistent and hybrid behavior is unchanged.

* chore: retrigger CI
This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-09-12 13:21:31 -04:00
1 parent 3bdb019e96
commit 1b83ef323e
2 files changed
+155 -6

No files matched your search

@@ -11,9 +11,12 @@ import org.jboss.logging.Logger;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Factory that creates {@link AccountAwareStorageBackend} instances based on configuration.
@@ -34,6 +37,10 @@ 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<>();
// 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.
private final Set<StorageBackend<?, ?>> loadedBackends = Collections.newSetFromMap(new IdentityHashMap<>());
@Inject
Instance<RequestContext> requestContextInstance;
@@ -93,22 +100,32 @@ public class StorageFactory {
default -> throw new IllegalArgumentException("Unknown storage mode: " + mode);
};
inner.load();
AccountAwareStorageBackend<V> backend = new AccountAwareStorageBackend<>(
inner, requestContextInstance, config.defaultAccountId());
// create() owns the initial load. Most services are lazily instantiated and only reach
// this point after the lifecycle's loadAll() has already run, so a backend that is not
// loaded here would serve an empty store and (for WAL) never open its writer (#71).
loadOnce(backend);
allBackends.add(backend);
backendsByPath.put(filePath, backend);
return backend;
}
/** Load all storage backends from disk. */
/** Load every managed backend that has not been loaded yet. Safe to call repeatedly. */
public synchronized void loadAll() {
for (StorageBackend<?, ?> backend : allBackends) {
backend.load();
loadOnce(backend);
}
}
private void loadOnce(StorageBackend<?, ?> backend) {
if (loadedBackends.contains(backend)) {
return;
}
backend.load();
loadedBackends.add(backend);
}
/** Flush all storage backends to disk. */
public synchronized void flushAll() {
for (StorageBackend<?, ?> backend : allBackends) {
@@ -124,15 +141,19 @@ public class StorageFactory {
flushAll();
}
/** Shutdown all managed backends (stop schedulers, close connections). */
/**
* Shutdown all managed backends (stop schedulers, close connections). The final flush runs
* first: a WAL flush compacts and reopens the writer, so flushing after shutdown would leave
* every WAL backend with an open writer that nothing closes.
*/
public synchronized void shutdownAll() {
flushAll();
for (HybridStorage<?, ?> hybrid : hybridBackends) {
hybrid.shutdown();
}
for (WalStorage<?, ?> wal : walBackends) {
wal.shutdown();
}
flushAll();
}
private String resolveMode(String serviceName) {