refactor(storage): share the atomic write and flush scheduler helpers (#6301)

HybridStorage, PersistentStorage and WalStorage each carried their own
copy of two steps: writing a file through a sibling .tmp file that is
then moved over it, and running a periodic flush or compaction on a
daemon thread that shutdown waits up to 5 s for.

Move both into a package-private StorageFiles helper
(writeAtomically, scheduleEvery, stop) and use it from all three.
Behaviour is unchanged: same temp-file name, same move options, same
thread names and the same 5 s shutdown wait.
This commit is contained in:
Ben Wart authored and GitHub committed 2026-10-22 20:47:28 -04:00
1 parent 99ef36998c
commit 9640dabe0f
4 files changed
+69 -49

No files matched your search

@@ -9,7 +9,6 @@ import org.jboss.logging.Logger;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -17,9 +16,7 @@ import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Predicate;
import java.util.stream.Collectors;
@@ -47,12 +44,7 @@ public class HybridStorage<K, V> implements StorageBackend<K, V> {
this.objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
this.objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
this.scheduler = Executors.newSingleThreadScheduledExecutor(r -> {
Thread t = new Thread(r, "hybrid-storage-flush");
t.setDaemon(true);
return t;
});
this.scheduler.scheduleAtFixedRate(this::flushIfDirty, flushIntervalMs, flushIntervalMs, TimeUnit.MILLISECONDS);
this.scheduler = StorageFiles.scheduleEvery("hybrid-storage-flush", flushIntervalMs, this::flushIfDirty);
}
@Override
@@ -113,15 +105,7 @@ public class HybridStorage<K, V> implements StorageBackend<K, V> {
}
public void shutdown() {
scheduler.shutdown();
try {
if (!scheduler.awaitTermination(5, TimeUnit.SECONDS)) {
scheduler.shutdownNow();
}
} catch (InterruptedException e) {
scheduler.shutdownNow();
Thread.currentThread().interrupt();
}
StorageFiles.stop(scheduler);
flush();
}
@@ -133,10 +117,7 @@ public class HybridStorage<K, V> implements StorageBackend<K, V> {
private synchronized void persistToDisk() {
try {
Files.createDirectories(filePath.getParent());
Path tempFile = filePath.resolveSibling(filePath.getFileName() + ".tmp");
objectMapper.writeValue(tempFile.toFile(), store);
Files.move(tempFile, filePath, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
StorageFiles.writeAtomically(filePath, tempFile -> objectMapper.writeValue(tempFile.toFile(), store));
LOG.debugv("Flushed {0} entries to {1}", store.size(), filePath);
} catch (IOException e) {
LOG.errorv(e, "Failed to persist data to {0}", filePath);
@@ -9,7 +9,6 @@ import org.jboss.logging.Logger;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -116,10 +115,7 @@ public class PersistentStorage<K, V> implements StorageBackend<K, V> {
private synchronized void persistToDisk() {
try {
Files.createDirectories(filePath.getParent());
Path tempFile = filePath.resolveSibling(filePath.getFileName() + ".tmp");
objectMapper.writeValue(tempFile.toFile(), store);
Files.move(tempFile, filePath, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
StorageFiles.writeAtomically(filePath, tempFile -> objectMapper.writeValue(tempFile.toFile(), store));
} catch (IOException e) {
LOG.errorv(e, "Failed to persist data to {0}", filePath);
}
@@ -0,0 +1,62 @@
package io.github.hectorvent.floci.core.storage;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* File and scheduling steps the file-backed storage backends share: replacing a file atomically
* and running a periodic flush or compaction on a daemon thread.
*/
final class StorageFiles {
private static final long SHUTDOWN_WAIT_SECONDS = 5;
/** Writes the content of a file at the given path. */
@FunctionalInterface
interface ContentWriter {
void write(Path file) throws IOException;
}
private StorageFiles() {
}
/**
* Writes {@code file} through a sibling {@code .tmp} file that is then moved over it, so a
* reader or a crash sees either the old content or the new, never a partial file.
*/
static void writeAtomically(Path file, ContentWriter writer) throws IOException {
Files.createDirectories(file.getParent());
Path tempFile = file.resolveSibling(file.getFileName() + ".tmp");
writer.write(tempFile);
Files.move(tempFile, file, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
}
/** Runs {@code task} every {@code intervalMs} on one daemon thread named {@code threadName}. */
static ScheduledExecutorService scheduleEvery(String threadName, long intervalMs, Runnable task) {
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(r -> {
Thread t = new Thread(r, threadName);
t.setDaemon(true);
return t;
});
scheduler.scheduleAtFixedRate(task, intervalMs, intervalMs, TimeUnit.MILLISECONDS);
return scheduler;
}
/** Stops {@code scheduler}, letting a run in progress finish for a few seconds first. */
static void stop(ScheduledExecutorService scheduler) {
scheduler.shutdown();
try {
if (!scheduler.awaitTermination(SHUTDOWN_WAIT_SECONDS, TimeUnit.SECONDS)) {
scheduler.shutdownNow();
}
} catch (InterruptedException e) {
scheduler.shutdownNow();
Thread.currentThread().interrupt();
}
}
}
@@ -12,7 +12,6 @@ import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Collections;
@@ -73,13 +72,7 @@ public class WalStorage<K, V> implements StorageBackend<K, V> {
this.walMapper.registerModule(new JavaTimeModule());
this.walMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
this.scheduler = Executors.newSingleThreadScheduledExecutor(r -> {
Thread t = new Thread(r, "wal-storage-compaction");
t.setDaemon(true);
return t;
});
this.scheduler.scheduleAtFixedRate(this::compact, compactionIntervalMs, compactionIntervalMs,
TimeUnit.MILLISECONDS);
this.scheduler = StorageFiles.scheduleEvery("wal-storage-compaction", compactionIntervalMs, this::compact);
}
@Override
@@ -195,15 +188,7 @@ public class WalStorage<K, V> implements StorageBackend<K, V> {
}
public void shutdown() {
scheduler.shutdown();
try {
if (!scheduler.awaitTermination(5, TimeUnit.SECONDS)) {
scheduler.shutdownNow();
}
} catch (InterruptedException e) {
scheduler.shutdownNow();
Thread.currentThread().interrupt();
}
StorageFiles.stop(scheduler);
compact();
closeWalWriter();
}
@@ -214,11 +199,7 @@ public class WalStorage<K, V> implements StorageBackend<K, V> {
if (!dirty) {
return;
}
Files.createDirectories(snapshotPath.getParent());
Path tempFile = snapshotPath.resolveSibling(snapshotPath.getFileName() + ".tmp");
snapshotMapper.writeValue(tempFile.toFile(), store);
Files.move(tempFile, snapshotPath, StandardCopyOption.REPLACE_EXISTING,
StandardCopyOption.ATOMIC_MOVE);
StorageFiles.writeAtomically(snapshotPath, tempFile -> snapshotMapper.writeValue(tempFile.toFile(), store));
dirty = false;
closeWalWriter();