Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(core): quarantine unreadable hybrid storage (#2250)

Langning Zhangcc8b8e9f14a8
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 38ddca6b89..bcf1fb9bc9 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
@@ -91,7 +91,7 @@ public class HybridStorage<K, V> implements StorageBackend<K, V> {
}
@Override
- public void load() {
+ public synchronized void load() {
if (!Files.exists(filePath)) {
LOG.debugv("No persistent file found at {0}, starting with empty store", filePath);
return;
@@ -102,7 +102,7 @@ public class HybridStorage<K, V> implements StorageBackend<K, V> {
store.putAll(data);
LOG.infov("Loaded {0} entries from {1}", store.size(), filePath);
} catch (IOException e) {
- LOG.errorv(e, "Failed to load data from {0}", filePath);
+ StorageQuarantine.quarantine(filePath, e, LOG);
}
}
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 015751639a..e908265e00 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
@@ -95,21 +95,7 @@ public class PersistentStorage<K, V> implements StorageBackend<K, V> {
// other services (e.g. CloudFormation) referencing resources that now appear missing
// (see issue #1634). Quarantine the unreadable file and log loudly so the data loss is
// detectable rather than masquerading as an empty store.
- quarantineUnreadableFile(e);
- }
- }
-
- private void quarantineUnreadableFile(IOException cause) {
- Path quarantine = filePath.resolveSibling(filePath.getFileName() + ".corrupt");
- try {
- Files.move(filePath, quarantine, StandardCopyOption.REPLACE_EXISTING);
- LOG.errorv(cause, "Failed to load persisted data from {0}; moved the unreadable file to "
- + "{1} and started with an empty store. This store's state was lost.",
- filePath, quarantine);
- } catch (IOException moveError) {
- LOG.errorv(cause, "Failed to load persisted data from {0}; could not quarantine it ({1}). "
- + "Starting with an empty store. This store's state was lost.",
- filePath, moveError.getMessage());
+ StorageQuarantine.quarantine(filePath, e, LOG);
}
}
diff --git a/src/main/java/io/github/hectorvent/floci/core/storage/StorageQuarantine.java b/src/main/java/io/github/hectorvent/floci/core/storage/StorageQuarantine.java
new file mode 100644
index 0000000000..d2191ea14b
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/core/storage/StorageQuarantine.java
@@ -0,0 +1,33 @@
+package io.github.hectorvent.floci.core.storage;
+
+import org.jboss.logging.Logger;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardCopyOption;
+
+/**
+ * Shared quarantine handling for storage backends that persist to a JSON file: moves a file that
+ * failed to deserialize aside to a sibling {@code .corrupt} path, replacing any prior quarantine
+ * there, so the data loss is visible instead of silently starting empty.
+ */
+final class StorageQuarantine {
+
+ private StorageQuarantine() {
+ }
+
+ static void quarantine(Path filePath, IOException cause, Logger log) {
+ Path quarantine = filePath.resolveSibling(filePath.getFileName() + ".corrupt");
+ try {
+ Files.move(filePath, quarantine, StandardCopyOption.REPLACE_EXISTING);
+ log.errorv(cause, "Failed to load persisted data from {0}; moved the unreadable file to "
+ + "{1} and started with an empty store. This store's state was lost.",
+ filePath, quarantine);
+ } catch (IOException moveError) {
+ log.errorv(cause, "Failed to load persisted data from {0}; could not quarantine it ({1}). "
+ + "Starting with an empty store. This store's state was lost.",
+ filePath, moveError.getMessage());
+ }
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/core/storage/HybridStorageTest.java b/src/test/java/io/github/hectorvent/floci/core/storage/HybridStorageTest.java
index 0108e67eb3..7ab47e38f6 100644
--- a/src/test/java/io/github/hectorvent/floci/core/storage/HybridStorageTest.java
+++ b/src/test/java/io/github/hectorvent/floci/core/storage/HybridStorageTest.java
@@ -10,6 +10,7 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
+import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
@@ -55,6 +56,26 @@ class HybridStorageTest {
store2.shutdown();
}
+ @Test
+ void loadQuarantinesUnreadableFile() throws Exception {
+ Path filePath = tempDir.resolve("corrupt-hybrid-store.json");
+ String unreadableContents = "{ not valid json";
+ Path quarantinePath = filePath.resolveSibling("corrupt-hybrid-store.json.corrupt");
+ Files.writeString(filePath, unreadableContents);
+ Files.writeString(quarantinePath, "previous unreadable contents");
+ var loadedStorage = new HybridStorage<>(filePath, new TypeReference<Map<String, String>>() {}, 60000);
+
+ try {
+ loadedStorage.load();
+
+ assertFalse(Files.exists(filePath));
+ assertTrue(Files.exists(quarantinePath));
+ assertEquals(unreadableContents, Files.readString(quarantinePath));
+ } finally {
+ loadedStorage.shutdown();
+ }
+ }
+
@Test
void dynamoTableWithLocalSecondaryIndexSurvivesRestart() {
Path filePath = tempDir.resolve("dynamodb-tables.json");