Single-file and directory snapshot reads judge the format stamp before parsing the body; a flush heals an empty or truncated file
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
b02cb0ab3d
commit
39e8ab5efd
5 changed files with 157 additions and 31 deletions
|
|
@ -182,7 +182,15 @@ public static class CultDocumentMessagePackSerialization
|
|||
return buffer.WrittenSpan.ToArray();
|
||||
}
|
||||
|
||||
public static CultPersistedStoreSnapshot DeserializeSnapshot(byte[] payload)
|
||||
/// <summary>
|
||||
/// Reads a single-file snapshot. The stamp in slot 0 is checked against the formats this runtime reads (v1, v2) before
|
||||
/// the catalog or any record is parsed, so a store stamped for another reader is refused, never mistaken for damage.
|
||||
/// </summary>
|
||||
public static CultPersistedStoreSnapshot DeserializeSnapshot(byte[] payload) =>
|
||||
DeserializeSnapshot(payload, RequireReadableSingleFileStamp);
|
||||
|
||||
// The stamp decides readability and the body never does: requireReadable runs on slot 0 before anything else is parsed.
|
||||
internal static CultPersistedStoreSnapshot DeserializeSnapshot(byte[] payload, Action<string> requireReadable)
|
||||
{
|
||||
var reader = new MessagePackReader(payload);
|
||||
var fieldCount = reader.ReadArrayHeader();
|
||||
|
|
@ -192,6 +200,7 @@ public static class CultDocumentMessagePackSerialization
|
|||
{
|
||||
snapshot.FormatVersion = reader.ReadString()
|
||||
?? throw new NotSupportedException("Store snapshot declares no format version; this runtime reads " + CultPersistedStoreSnapshot.FormatV1 + " and " + CultPersistedStoreSnapshot.FormatV2 + ".");
|
||||
requireReadable(snapshot.FormatVersion);
|
||||
}
|
||||
|
||||
if (fieldCount > 1)
|
||||
|
|
@ -226,12 +235,7 @@ public static class CultDocumentMessagePackSerialization
|
|||
public static void RequireSingleFileFormat(CultPersistedStoreSnapshot snapshot)
|
||||
{
|
||||
var version = snapshot.FormatVersion;
|
||||
if (!string.Equals(version, CultPersistedStoreSnapshot.FormatV1, StringComparison.Ordinal) &&
|
||||
!string.Equals(version, CultPersistedStoreSnapshot.FormatV2, StringComparison.Ordinal))
|
||||
{
|
||||
throw new NotSupportedException(
|
||||
$"Store format {DescribeHeader(version)} is not readable; this runtime reads {CultPersistedStoreSnapshot.FormatV1} and {CultPersistedStoreSnapshot.FormatV2}.");
|
||||
}
|
||||
RequireReadableSingleFileStamp(version);
|
||||
|
||||
// Only a v2 store may hold a variant: a v1 header over a variant record is a store no reader could trust.
|
||||
if (string.Equals(version, CultPersistedStoreSnapshot.FormatV1, StringComparison.Ordinal))
|
||||
|
|
@ -245,6 +249,16 @@ public static class CultDocumentMessagePackSerialization
|
|||
}
|
||||
}
|
||||
|
||||
private static void RequireReadableSingleFileStamp(string version)
|
||||
{
|
||||
if (!string.Equals(version, CultPersistedStoreSnapshot.FormatV1, StringComparison.Ordinal) &&
|
||||
!string.Equals(version, CultPersistedStoreSnapshot.FormatV2, StringComparison.Ordinal))
|
||||
{
|
||||
throw new NotSupportedException(
|
||||
$"Store format {DescribeHeader(version)} is not readable; this runtime reads {CultPersistedStoreSnapshot.FormatV1} and {CultPersistedStoreSnapshot.FormatV2}.");
|
||||
}
|
||||
}
|
||||
|
||||
// A store header as a refusal may show it: echoed in the shape cultcache.store.v<digits>, and otherwise described
|
||||
// only by its length, since the bytes are the store's.
|
||||
private static string DescribeHeader(string header)
|
||||
|
|
|
|||
|
|
@ -448,22 +448,25 @@ public sealed class DirectoryMessagePackBackingStore : CacheBackingStore
|
|||
};
|
||||
}
|
||||
|
||||
var snapshot = CultDocumentMessagePackSerialization.DeserializeSnapshot(manifestBytes);
|
||||
if (CultPersistedStoreSnapshot.FormatNumber(snapshot.FormatVersion) is int number && number > 4)
|
||||
// The stamp is judged before the manifest body is parsed, so a store stamped newer is refused whatever its body.
|
||||
return CultDocumentMessagePackSerialization.DeserializeSnapshot(manifestBytes, RequireReadableStamp);
|
||||
}
|
||||
|
||||
private void RequireReadableStamp(string stamp)
|
||||
{
|
||||
if (CultPersistedStoreSnapshot.FormatNumber(stamp) is int number && number > 4)
|
||||
{
|
||||
// Every write path parses the manifest under its commit lease before it writes a page or the manifest, so a
|
||||
// store another process has since stamped newer is refused here, untouched.
|
||||
throw new NotSupportedException(
|
||||
$"Directory store {_manifestFile.FullName} is {snapshot.FormatVersion}, a newer format than this runtime reads ({IndexedFormatVersion}).");
|
||||
$"Directory store {_manifestFile.FullName} is {stamp}, a newer format than this runtime reads ({IndexedFormatVersion}).");
|
||||
}
|
||||
|
||||
if (!string.Equals(snapshot.FormatVersion, IndexedFormatVersion, StringComparison.Ordinal))
|
||||
if (!string.Equals(stamp, IndexedFormatVersion, StringComparison.Ordinal))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Directory store {_manifestFile.FullName} is {snapshot.FormatVersion}; only {IndexedFormatVersion} is readable.");
|
||||
$"Directory store {_manifestFile.FullName} is {stamp}; only {IndexedFormatVersion} is readable.");
|
||||
}
|
||||
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
private string ContentAddressedRecordPath(CultPersistedRecord metadata)
|
||||
|
|
|
|||
|
|
@ -3542,39 +3542,34 @@ namespace GameCult.Caching
|
|||
|
||||
// A plain flush replaces the file without merging it, so under the lock it first reads the disk the way attach and
|
||||
// commit do: a store this runtime does not read (a newer format, or a header the reader refuses) is refused and left
|
||||
// as it is, and so is a read that fails on I/O. A file that merely will not parse (empty, corrupt) is not a newer
|
||||
// store, and the flush proceeds as it always has, so it can still heal a damaged store.
|
||||
// as it is, and so is a file that cannot be read (an I/O or access error). A file that is read but will not parse
|
||||
// (empty, truncated, corrupt) is not a newer store, and the flush proceeds as it always has, so it can still heal.
|
||||
private void RefuseToRewriteAStoreItCannotRead()
|
||||
{
|
||||
var bytes = ReadFileBytes();
|
||||
if (bytes == null)
|
||||
return;
|
||||
try
|
||||
{
|
||||
ReadSnapshot();
|
||||
DeserializeSnapshot(bytes);
|
||||
}
|
||||
catch (NotSupportedException)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
catch (UnauthorizedAccessException)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
private CultPersistedStoreSnapshot? ReadSnapshot()
|
||||
private byte[]? ReadFileBytes()
|
||||
{
|
||||
FileInfo.Refresh();
|
||||
if (!FileInfo.Exists)
|
||||
return null;
|
||||
try
|
||||
{
|
||||
return DeserializeSnapshot(ReadAllBytesShared(FileInfo.FullName));
|
||||
return ReadAllBytesShared(FileInfo.FullName);
|
||||
}
|
||||
catch (FileNotFoundException)
|
||||
{
|
||||
|
|
@ -3582,6 +3577,12 @@ namespace GameCult.Caching
|
|||
}
|
||||
}
|
||||
|
||||
private CultPersistedStoreSnapshot? ReadSnapshot()
|
||||
{
|
||||
var bytes = ReadFileBytes();
|
||||
return bytes == null ? null : DeserializeSnapshot(bytes);
|
||||
}
|
||||
|
||||
private void WriteSnapshot(IEnumerable<CultPersistedRecord> records, IEnumerable<CultSchemaCatalogEntry> catalog)
|
||||
{
|
||||
var ordered = records.OrderBy(record => record.Key, StringComparer.Ordinal).ToArray();
|
||||
|
|
|
|||
Loading…
Reference in a new issue