Recover from corrupt CultCache snapshots
This commit is contained in:
parent
37a3f46499
commit
498c2e9411
5 changed files with 62 additions and 1 deletions
|
|
@ -2,6 +2,7 @@ using System.Numerics;
|
|||
using GameCult.Caching;
|
||||
using GameCult.Caching.MessagePack;
|
||||
using GameCult.Networking;
|
||||
using MessagePack;
|
||||
|
||||
namespace Aquarium.Engine.State;
|
||||
|
||||
|
|
@ -21,7 +22,15 @@ public sealed class AquariumCultStateStore : IDisposable
|
|||
cache = new CultCache();
|
||||
backingStore = new SingleFileMessagePackBackingStore(CachePath);
|
||||
cache.AddBackingStore(backingStore);
|
||||
cache.PullAllBackingStoresAsync().GetAwaiter().GetResult();
|
||||
try
|
||||
{
|
||||
cache.PullAllBackingStoresAsync().GetAwaiter().GetResult();
|
||||
}
|
||||
catch (Exception error) when (IsRecoverableBackingStoreFailure(error))
|
||||
{
|
||||
QuarantineUnreadableCache(CachePath, error);
|
||||
}
|
||||
|
||||
Hello = CreateHello(cache.Registry);
|
||||
}
|
||||
|
||||
|
|
@ -87,4 +96,39 @@ public sealed class AquariumCultStateStore : IDisposable
|
|||
]
|
||||
};
|
||||
}
|
||||
|
||||
private static bool IsRecoverableBackingStoreFailure(Exception error)
|
||||
{
|
||||
return error is EndOfStreamException
|
||||
or InvalidDataException
|
||||
or MessagePackSerializationException;
|
||||
}
|
||||
|
||||
private static void QuarantineUnreadableCache(string cachePath, Exception error)
|
||||
{
|
||||
if (!File.Exists(cachePath))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var directory = Path.GetDirectoryName(cachePath)!;
|
||||
var fileName = Path.GetFileName(cachePath);
|
||||
var stamp = DateTimeOffset.UtcNow.ToString("yyyyMMdd-HHmmss-fff");
|
||||
var quarantinePath = Path.Combine(directory, $"{fileName}.corrupt-{stamp}");
|
||||
|
||||
try
|
||||
{
|
||||
File.Move(cachePath, quarantinePath);
|
||||
Console.Error.WriteLine(
|
||||
$"CultCache snapshot was unreadable and has been quarantined: {quarantinePath}. {error.GetType().Name}: {error.Message}");
|
||||
}
|
||||
catch (Exception quarantineError)
|
||||
{
|
||||
var fallbackPath = Path.Combine(directory, $"{fileName}.corrupt-{stamp}-{Guid.NewGuid():N}");
|
||||
File.Move(cachePath, fallbackPath);
|
||||
Console.Error.WriteLine(
|
||||
$"CultCache snapshot was unreadable and quarantine path was busy; moved it to {fallbackPath}. "
|
||||
+ $"{error.GetType().Name}: {error.Message}; quarantine retry after {quarantineError.GetType().Name}: {quarantineError.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue