From 9640dabe0ffacf08f6b0460c6f0da02f807a0064 Mon Sep 17 00:00:00 2001 From: Ben Wart Date: Thu, 22 Oct 2026 17:47:28 -0700 Subject: [PATCH] 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. --- .../floci/core/storage/HybridStorage.java | 25 +------- .../floci/core/storage/PersistentStorage.java | 6 +- .../floci/core/storage/StorageFiles.java | 62 +++++++++++++++++++ .../floci/core/storage/WalStorage.java | 25 +------- 4 files changed, 69 insertions(+), 49 deletions(-) create mode 100644 src/main/java/io/github/hectorvent/floci/core/storage/StorageFiles.java diff --git a/src/main/java/io/github/hectorvent/floci/core/storage/HybridStorage.java b/src/main/java/io/github/hectorvent/floci/core/storage/HybridStorage.java index bcf1fb9bc9..54b1c114b9 100644 --- a/src/main/java/io/github/hectorvent/floci/core/storage/HybridStorage.java +++ b/src/main/java/io/github/hectorvent/floci/core/storage/HybridStorage.java @@ -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 implements StorageBackend { 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 implements StorageBackend { } 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 implements StorageBackend { 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); diff --git a/src/main/java/io/github/hectorvent/floci/core/storage/PersistentStorage.java b/src/main/java/io/github/hectorvent/floci/core/storage/PersistentStorage.java index dc4a7da20d..6b0e26ccbb 100644 --- a/src/main/java/io/github/hectorvent/floci/core/storage/PersistentStorage.java +++ b/src/main/java/io/github/hectorvent/floci/core/storage/PersistentStorage.java @@ -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 implements StorageBackend { 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); } diff --git a/src/main/java/io/github/hectorvent/floci/core/storage/StorageFiles.java b/src/main/java/io/github/hectorvent/floci/core/storage/StorageFiles.java new file mode 100644 index 0000000000..41c9a880be --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/core/storage/StorageFiles.java @@ -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(); + } + } +} diff --git a/src/main/java/io/github/hectorvent/floci/core/storage/WalStorage.java b/src/main/java/io/github/hectorvent/floci/core/storage/WalStorage.java index 316374a826..f15c4ac49b 100644 --- a/src/main/java/io/github/hectorvent/floci/core/storage/WalStorage.java +++ b/src/main/java/io/github/hectorvent/floci/core/storage/WalStorage.java @@ -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 implements StorageBackend { 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 implements StorageBackend { } 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 implements StorageBackend { 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();