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

Co-authored-by: Priamo <pgermosen@gmail.com>
This commit is contained in:
Langning ZhangandPriamo authored and GitHub committed 2026-09-08 00:31:51 -04:00
1 parent 725577416d
commit cc8b8e9f14
4 files changed
+57 -17

No files matched your search

@@ -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);
}
}
@@ -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);
}
}
@@ -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());
}
}
}
@@ -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");