From 2ae6d1693664696dc2a95a052e9149af52568f98 Mon Sep 17 00:00:00 2001 From: Ben Wart Date: Thu, 22 Oct 2026 17:50:18 -0700 Subject: [PATCH] fix(storage): reopen the WAL when compaction cannot delete the old log (#6302) * fix(storage): reopen the WAL when compaction cannot delete the old log WalStorage.compact closed the log writer, deleted the log and reopened it. When the delete failed, the IOException skipped the reopen, so the writer stayed null and every later write lived only in memory until the next successful compaction; a crash in between lost them. Reopen the writer in a finally block, so appends continue even when the old log could not be removed. * test(storage): skip the read-only WAL test where POSIX permissions are missing --- .../floci/core/storage/WalStorage.java | 8 +++- .../floci/core/storage/WalStorageTest.java | 44 +++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) 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 f15c4ac49b..4c1f4d0996 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 @@ -203,8 +203,12 @@ public class WalStorage implements StorageBackend { dirty = false; closeWalWriter(); - Files.deleteIfExists(walPath); - openWalWriter(); + try { + Files.deleteIfExists(walPath); + } finally { + // Reopen even when the delete failed, so later records are still appended. + openWalWriter(); + } LOG.debugv("Compacted {0} entries to snapshot, WAL truncated", store.size()); } catch (IOException e) { diff --git a/src/test/java/io/github/hectorvent/floci/core/storage/WalStorageTest.java b/src/test/java/io/github/hectorvent/floci/core/storage/WalStorageTest.java index 73e8e07cb6..26a4c1904e 100644 --- a/src/test/java/io/github/hectorvent/floci/core/storage/WalStorageTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/storage/WalStorageTest.java @@ -14,10 +14,16 @@ import java.io.IOException; import java.io.RandomAccessFile; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.attribute.PosixFileAttributeView; +import java.nio.file.attribute.PosixFilePermission; +import java.nio.file.attribute.PosixFilePermissions; import java.util.List; import java.util.Map; +import java.util.Set; import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assumptions.assumeFalse; +import static org.junit.jupiter.api.Assumptions.assumeTrue; class WalStorageTest { @@ -381,6 +387,44 @@ class WalStorageTest { } } + @Test + void aWalThatCannotBeEmptiedStaysOpenForLaterChanges() throws IOException { + // The WAL lives apart from the snapshot, so only emptying the WAL can be made to fail. + Path snapshotPath = tempDir.resolve("stuck-snapshot.json"); + Path walDirectory = Files.createDirectories(tempDir.resolve("stuck-wal")); + Path walPath = walDirectory.resolve("data.wal"); + // The test makes the directory read-only through POSIX permissions, which Windows lacks. + assumeTrue(Files.getFileStore(walDirectory).supportsFileAttributeView(PosixFileAttributeView.class), + "POSIX file permissions are not supported here"); + Set original = Files.getPosixFilePermissions(walDirectory); + + WalStorage store1 = new WalStorage<>(snapshotPath, walPath, + new TypeReference>() {}, 600000); + store1.load(); + WalStorage store2 = null; + try { + store1.put("key1", "value1"); + Files.setPosixFilePermissions(walDirectory, PosixFilePermissions.fromString("r-xr-xr-x")); + // A root user writes through the read-only mode, so there is nothing to test there. + assumeFalse(Files.isWritable(walDirectory), "the directory stays writable for this user"); + + store1.flush(); + assertTrue(Files.exists(walPath), "the WAL could not be deleted"); + store1.put("later", "write"); + + store2 = new WalStorage<>(snapshotPath, walPath, new TypeReference>() {}, 600000); + store2.load(); + assertEquals("value1", store2.get("key1").orElseThrow()); + assertEquals("write", store2.get("later").orElseThrow()); + } finally { + Files.setPosixFilePermissions(walDirectory, original); + store1.shutdown(); + if (store2 != null) { + store2.shutdown(); + } + } + } + /** Writes one PUT record in the documented WAL layout, independent of the store's writer. */ private static void writePutRecord(DataOutputStream out, String key, String value) throws IOException { ObjectMapper cbor = new ObjectMapper(new CBORFactory());