Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(storage): reopen the WAL when compaction cannot delete the old log (#6302)

Ben Wart2ae6d1693664
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<K, V> implements StorageBackend<K, V> {
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<PosixFilePermission> original = Files.getPosixFilePermissions(walDirectory);
+
+ WalStorage<String, String> store1 = new WalStorage<>(snapshotPath, walPath,
+ new TypeReference<Map<String, String>>() {}, 600000);
+ store1.load();
+ WalStorage<String, String> 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<Map<String, String>>() {}, 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());