Light, fluffy, and always free - The AWS Local Emulator alternative
refactor(storage): share the atomic write and flush scheduler helpers (#6301)
Ben Wart9640dabe0ffa
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.javaindex 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<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);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.javaindex 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<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);}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.javanew file mode 100644index 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.javaindex 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<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();