C#: a flush refuses to overwrite a store file whose header it cannot read
SingleFileBackingStore.PushAll replaced the file without reading it, so a truncated store or one in a format this runtime does not read was clobbered, where Rust, TypeScript and Python refuse. PushAll now reads the header of a non-empty file under the lock before it writes; a file it cannot read throws and is left as it was. An empty file and a legacy envelope array carry no header and are still replaced. The header is read by the store's codec: SingleFileBackingStore gains a protected virtual ReadStoreHeader (default: read the whole snapshot), and the MessagePack store overrides it with CultDocumentMessagePackSerialization.ReadStoreHeader. An unconditional commit reads the file it rewrites through the same check. Doc text: last-writer-wins now says it holds only over a file the writer can read (CultCache.cs PushAll comment, cultcache-persistence-format.md "Conditional commit is the only safe multi-process write" and writer rule 4, cultcache-store-composition.md "A plain flush and an unconditional commit are last-writer-wins"). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
619e089fa6
commit
4f742d05c3
5 changed files with 69 additions and 7 deletions
|
|
@ -3417,6 +3417,15 @@ namespace GameCult.Caching
|
|||
protected FileInfo FileInfo { get; }
|
||||
protected abstract byte[] SerializeSnapshot(CultPersistedStoreSnapshot snapshot);
|
||||
protected abstract CultPersistedStoreSnapshot DeserializeSnapshot(byte[] data);
|
||||
|
||||
/// <summary>
|
||||
/// The format header of a non-empty store file this store is about to replace: null for a legacy file that carries none,
|
||||
/// the header otherwise. It throws for bytes that are not exactly one store this runtime reads, so a rewrite never
|
||||
/// overwrites a file it cannot see. The default reads the whole snapshot; a codec that can read the header alone
|
||||
/// overrides it.
|
||||
/// </summary>
|
||||
protected virtual string? ReadStoreHeader(byte[] data) => DeserializeSnapshot(data).FormatVersion;
|
||||
|
||||
protected abstract byte[] SerializePayload(object document);
|
||||
protected abstract object DeserializePayload(Type documentType, byte[] payload);
|
||||
|
||||
|
|
@ -3484,7 +3493,8 @@ namespace GameCult.Caching
|
|||
}
|
||||
|
||||
// A plain flush writes this store's whole view under the lock: two writers never interleave bytes, but the last
|
||||
// one wins. Processes sharing a store must use conditional commit.
|
||||
// one wins over any file this runtime can read. Processes sharing a store must use conditional commit. A file it
|
||||
// cannot read (truncated, trailing bytes, not a store, a format it does not read) is refused and left as it is.
|
||||
public override void PushAll()
|
||||
{
|
||||
ThrowIfReadOnly();
|
||||
|
|
@ -3492,6 +3502,8 @@ namespace GameCult.Caching
|
|||
{
|
||||
using (AcquireLock(wait: true))
|
||||
{
|
||||
if (ReadDisk() is { Length: > 0 } onDisk)
|
||||
ReadStoreHeader(onDisk);
|
||||
WriteSnapshot(
|
||||
Entries.Values.Select(entry => ToPersistedRecord(entry, SerializePayload)),
|
||||
Entries.Values.Select(entry => entry.Descriptor.ToCatalogEntry()),
|
||||
|
|
@ -3517,7 +3529,7 @@ namespace GameCult.Caching
|
|||
if (fileLock == null)
|
||||
return CultCommitOutcome.Contended;
|
||||
|
||||
var disk = ReadSnapshot() ?? new CultPersistedStoreSnapshot();
|
||||
var disk = ReadSnapshot(rewriting: true) ?? new CultPersistedStoreSnapshot();
|
||||
if (!request.ConditionsHold(disk.Records, Entries.Values))
|
||||
return CultCommitOutcome.Mismatch;
|
||||
|
||||
|
|
@ -3586,14 +3598,25 @@ namespace GameCult.Caching
|
|||
Judging!(arriving, departing);
|
||||
}
|
||||
|
||||
private CultPersistedStoreSnapshot? ReadSnapshot()
|
||||
// A commit rewrites the file it read, so it reads it as a rewrite does: the header proves the bytes are one store first.
|
||||
private CultPersistedStoreSnapshot? ReadSnapshot(bool rewriting = false)
|
||||
{
|
||||
var bytes = ReadDisk();
|
||||
if (bytes == null)
|
||||
return null;
|
||||
if (rewriting && bytes.Length > 0)
|
||||
ReadStoreHeader(bytes);
|
||||
return DeserializeSnapshot(bytes);
|
||||
}
|
||||
|
||||
private byte[]? ReadDisk()
|
||||
{
|
||||
FileInfo.Refresh();
|
||||
if (!FileInfo.Exists)
|
||||
return null;
|
||||
try
|
||||
{
|
||||
return DeserializeSnapshot(ReadAllBytesShared(FileInfo.FullName));
|
||||
return ReadAllBytesShared(FileInfo.FullName);
|
||||
}
|
||||
catch (FileNotFoundException)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue