mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
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
This commit is contained in:
1 parent
9640dabe0f
commit
2ae6d16936
2 files changed
+50
-2
No files matched your search
@@ -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) {
|
||||
|
||||
@@ -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());
|
||||
|
||||
Reference in new issue
Block a user