when cached snapshot is corrupted fall back to loading from backend

This commit is contained in:
Torsten Grote 2024-10-10 10:33:32 -03:00
parent 14775b5c3e
commit 9339f9f0fb
No known key found for this signature in database
GPG key ID: 3E5F77D92CF891FF
2 changed files with 27 additions and 1 deletions

View file

@ -133,7 +133,12 @@ internal class SnapshotManager(
val file = File(snapshotFolder, snapshotHandle.name)
snapshotFolder.mkdirs()
val inputStream = if (file.isFile) {
loader.loadFile(file, snapshotHandle.hash)
try {
loader.loadFile(file, snapshotHandle.hash)
} catch (e: Exception) {
log.error(e) { "Error loading $snapshotHandle from local cache. Trying backend..." }
loader.loadFile(snapshotHandle, file)
}
} else {
loader.loadFile(snapshotHandle, file)
}

View file

@ -34,6 +34,7 @@ import java.io.IOException
import java.io.InputStream
import java.io.OutputStream
import java.nio.file.Path
import java.security.GeneralSecurityException
import java.security.MessageDigest
import kotlin.random.Random
@ -132,6 +133,26 @@ internal class SnapshotManagerTest : TransportTest() {
assertEquals(listOf(snapshot), snapshotManager.loadCachedSnapshots())
}
@Test
fun `snapshot corrupted on cache falls back to backend`(@TempDir tmpDir: Path) = runBlocking {
val snapshotManager = getSnapshotManager(File(tmpDir.toString()))
val snapshot = snapshot { token = 1337 }
val snapshotData = snapshot.toByteArray()
val inputStream = ByteArrayInputStream(snapshotData)
val snapshotHandle = AppBackupFileType.Snapshot(repoId, chunkId1)
val file = getSnapshotFolder(tmpDir, snapshotHandle.name)
every { crypto.repoId } returns repoId
coEvery { loader.loadFile(file, snapshotHandle.hash) } throws GeneralSecurityException()
coEvery { loader.loadFile(snapshotHandle, file) } returns inputStream
assertEquals(listOf(snapshot), snapshotManager.onSnapshotsLoaded(listOf(snapshotHandle)))
coVerify { // did load from backend
loader.loadFile(snapshotHandle, file)
}
}
@Test
fun `failing to load a snapshot isn't fatal`(@TempDir tmpDir: Path) = runBlocking {
val snapshotManager = getSnapshotManager(File(tmpDir.toString()))