Caching: Update reads its version and copy from one durable read of the home store

Add CacheBackingStore.ReadDurable (single-file and directory stores), factor the
directory pull's lease/retry load into LoadManifestPages, and make a store-backed
Update take apply's argument and the Expected version from that read, with the load's
deterministic element ids. The per-loss pull moves to the LostRace branch; a
store-less cache keeps the held copy and its codec refusal.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Libby 2026-11-13 16:00:55 +00:00
commit a020247fcc
7 changed files with 324 additions and 47 deletions

View file

@ -72,8 +72,8 @@ public static class FirstCall
## C# update with retry
Change a record you may not be the only one changing. `Update` reads the record, hands your function a private
copy, and commits the result only if the record is still the one it read. If another writer got there first, it
Change a record you may not be the only one changing. `Update` reads the record as its store holds it, hands your function a fresh
copy of it, and commits the result only if the record is still the one it read. If another writer got there first, it
reads again and runs your function again, up to five times. The source is
[`tests/GameCult.Caching.Tests/UpdateCallTests.cs`](../../tests/GameCult.Caching.Tests/UpdateCallTests.cs); the
snippet reuses `Item` from the first call.

View file

@ -64,48 +64,7 @@ public sealed class DirectoryMessagePackBackingStore : CacheBackingStore
var reports = new List<CultSchemaMigrationReport>();
var loaded = new Dictionary<string, CultStoredDocument>(StringComparer.Ordinal);
var foreign = new List<CultForeignRecord>();
for (var attempt = 1; ; attempt++)
{
loaded.Clear();
reports.Clear();
foreign.Clear();
// Only pages named by the manifest read under the lease are loaded; orphaned pages are never loaded. With no
// lock to lease (none created yet, or a store copied without it) a writer may commit mid-load, so an unleased
// load stands only if its manifest is still current; if the manifest moved it reloads, under the lease the
// writer created. A page that fails under an unmoved manifest is corruption, not a race.
using var lease = AcquireCommitLease(wait: true, create: false);
var manifestBytes = ReadManifestBytes();
var (manifest, _) = ParseManifest(manifestBytes);
Trace($"manifest records={manifest.Records.Length}");
try
{
if (lease == null)
UnleasedManifestRead?.Invoke();
LoadRecordPages(
manifest.Records.OrderBy(record => record.Key, StringComparer.Ordinal).ToArray(),
manifest.SchemaCatalog,
loaded,
reports,
foreign);
if (lease != null || !ManifestMoved(manifestBytes))
{
Trace($"indexed-pages loaded={loaded.Count}");
break;
}
}
catch (Exception exception) when (lease == null && IsTornGeneration(exception))
{
// The re-read stays out of the filter: an exception thrown inside a filter is swallowed as false.
if (!ManifestMoved(manifestBytes))
throw;
if (attempt == UnleasedLoadAttempts)
throw Unsettled(exception);
continue;
}
if (attempt == UnleasedLoadAttempts)
throw Unsettled(null);
}
LoadManifestPages(null, loaded, reports, foreign, Trace);
bool Staged(string key) => _dirtyKeys.ContainsKey(key) || _deletedKeys.ContainsKey(key);
var arrived = loaded
@ -136,6 +95,70 @@ public sealed class DirectoryMessagePackBackingStore : CacheBackingStore
Trace("publish");
}
// The one load of manifest pages, for the whole store (key null) or one record: under the lease, with the moved-manifest retry.
// It reads only; adopting what it loaded is the caller's.
private void LoadManifestPages(
string? key,
Dictionary<string, CultStoredDocument> loaded,
List<CultSchemaMigrationReport> reports,
List<CultForeignRecord> foreign,
Action<string> trace)
{
for (var attempt = 1; ; attempt++)
{
loaded.Clear();
reports.Clear();
foreign.Clear();
// Only pages named by the manifest read under the lease are loaded; orphaned pages are never loaded. With no
// lock to lease (none created yet, or a store copied without it) a writer may commit mid-load, so an unleased
// load stands only if its manifest is still current; if the manifest moved it reloads, under the lease the
// writer created. A page that fails under an unmoved manifest is corruption, not a race.
using var lease = AcquireCommitLease(wait: true, create: false);
var manifestBytes = ReadManifestBytes();
var (manifest, _) = ParseManifest(manifestBytes);
trace($"manifest records={manifest.Records.Length}");
try
{
if (lease == null)
UnleasedManifestRead?.Invoke();
LoadRecordPages(
manifest.Records.Where(record => key == null || string.Equals(record.Key, key, StringComparison.Ordinal))
.OrderBy(record => record.Key, StringComparer.Ordinal).ToArray(),
manifest.SchemaCatalog,
loaded,
reports,
foreign);
if (lease != null || !ManifestMoved(manifestBytes))
{
trace($"indexed-pages loaded={loaded.Count}");
break;
}
}
catch (Exception exception) when (lease == null && IsTornGeneration(exception))
{
// The re-read stays out of the filter: an exception thrown inside a filter is swallowed as false.
if (!ManifestMoved(manifestBytes))
throw;
if (attempt == UnleasedLoadAttempts)
throw Unsettled(exception);
continue;
}
if (attempt == UnleasedLoadAttempts)
throw Unsettled(null);
}
}
// The record at a key as its page holds it now, decoded fresh and stamped with VersionOf its manifest payload, the version
// CommitBatch compares. Null when none is there or it is foreign. Touches no entry, hydration, report or foreign list of the
// store, and raises nothing.
internal override CultStoredDocument? ReadDurable(string key) => Held(() =>
{
var loaded = new Dictionary<string, CultStoredDocument>(StringComparer.Ordinal);
LoadManifestPages(key, loaded, new List<CultSchemaMigrationReport>(), new List<CultForeignRecord>(), _ => { });
return loaded.TryGetValue(key, out var stored) ? stored : null;
});
// Variants wait for the directory store's own cut; until then it refuses one rather than persist a resolved view as a plain page.
private static void RefuseVariant(CultStoredDocument entry)
{

View file

@ -2682,30 +2682,46 @@ namespace GameCult.Caching
}
/// <summary>
/// Reads the record at <paramref name="key"/>, applies <paramref name="apply"/> to a private copy of it, and commits the result only
/// if the record is still the one that was read; when another writer got there first it re-reads and runs <paramref name="apply"/>
/// again, up to <paramref name="maxAttempts"/> times. The write is durable when Update returns.
/// Reads the record at <paramref name="key"/> as its home store holds it, applies <paramref name="apply"/> to a fresh copy of it, and
/// commits the result only if the record is still the one that was read; when another writer got there first it re-reads and runs
/// <paramref name="apply"/> again, up to <paramref name="maxAttempts"/> times. The write is durable when Update returns.
/// </summary>
/// <remarks>
/// <paramref name="apply"/> may run up to <paramref name="maxAttempts"/> times. It gets a private copy (null when the key holds
/// nothing), which it may change and return. It must not write to the cache or have other side effects. Return null to write
/// nothing. A store with staged blind writes refuses Update until it is flushed, as it refuses any conditional commit. The cache
/// needs a codec to copy with. Running out of attempts returns <see cref="CultUpdateOutcome.LostRace"/>; every other failure throws.
/// <paramref name="apply"/> may run up to <paramref name="maxAttempts"/> times. It gets the record as its home store holds it now,
/// decoded afresh for this attempt (null when the key holds nothing), which it may change and return. A change made in place to an
/// instance the cache holds is neither seen nor written. It must not write to the cache or have other side effects. Return null to write
/// nothing. A store with staged blind writes refuses Update until it is flushed, as it refuses any conditional commit. A cache with no
/// store attached copies its held instance and needs a codec; a store kind outside the library cannot serve Update (<see cref="NotSupportedException"/>). Running out of attempts returns <see cref="CultUpdateOutcome.LostRace"/>; every other failure throws.
/// </remarks>
public CultUpdateResult<T> Update<T>(CultRecordKey key, Func<T?, T?> apply, int maxAttempts = 5) where T : class
{
if (apply == null) throw new ArgumentNullException(nameof(apply));
if (maxAttempts < 1) throw new ArgumentOutOfRangeException(nameof(maxAttempts), maxAttempts, "At least one attempt is required.");
var codec = _codec ?? throw new InvalidOperationException("This cache has no codec; create it with CultCacheMessagePack.Create.");
for (var attempt = 1; ; attempt++)
{
var (version, copy) = Held(() =>
{
if (!_entries.TryGetValue(key.Value, out var held))
_entries.TryGetValue(key.Value, out var held);
if (_stores.Count == 0)
{
// No store: the held entry is the record.
var codec = _codec ?? throw new InvalidOperationException("This cache has no codec; create it with CultCacheMessagePack.Create.");
if (held == null)
return ((string?)null, (T?)null);
if (held.Variant != null || held.Document is not T)
throw new InvalidOperationException($"Record {key.Value} is a variant or not a {typeof(T).Name}; Update cannot copy it.");
return (held.StoredVersion, (T?)codec.Clone(held.Document, held.Descriptor.DocumentType));
}
var home = Home(held?.Descriptor.DocumentType ?? typeof(T))
?? throw new InvalidOperationException("A conditional batch names its home store through a record it upserts or removes.");
var durable = home.ReadDurable(key.Value);
if (durable == null)
return ((string?)null, (T?)null);
if (held.Variant != null || held.Document is not T)
if (durable.Variant != null || durable.Document is not T typed)
throw new InvalidOperationException($"Record {key.Value} is a variant or not a {typeof(T).Name}; Update cannot copy it.");
return (held.StoredVersion, (T?)codec.Clone(held.Document, held.Descriptor.DocumentType));
CultElementIds.Plan(durable.Descriptor.DocumentType, durable.Document, key.Value, deterministic: true).Apply();
return (durable.StoredVersion, typed);
});
var next = apply(copy);
if (next == null)
@ -2728,9 +2744,11 @@ namespace GameCult.Caching
return new CultUpdateResult<T>(CultUpdateOutcome.Updated, next);
if (outcome != CultCommitOutcome.Mismatch)
throw new InvalidOperationException($"Update of {key.Value} was refused: {outcome}.");
Held(() => { Home(next.GetType())?.PullAll(); return true; });
if (attempt >= maxAttempts)
{
Held(() => { Home(next.GetType())?.PullAll(); return true; });
return new CultUpdateResult<T>(CultUpdateOutcome.LostRace, Get<T>(key));
}
}
}
@ -4027,6 +4045,15 @@ namespace GameCult.Caching
internal void UseRegistry(CultDocumentRegistry? registry) => _registry = registry;
public abstract void PullAll();
/// <summary>
/// The record at <paramref name="key"/> as this store holds it now, decoded as a load decodes it by this call (a new instance nobody
/// else holds) and stamped with the version this store's commit compares. Null when the store holds nothing there or holds it foreign.
/// It changes none of the store's entries, staged writes or reports and raises nothing. Internal because only this library can stamp a version.
/// </summary>
internal virtual CultStoredDocument? ReadDurable(string key) =>
throw new NotSupportedException($"{this} cannot read one record, which Update needs.");
public abstract void Push(CultStoredDocument entry);
public abstract void Delete(CultStoredDocument entry);
public void PushAll() => PushAll(soft: false);
@ -5165,6 +5192,8 @@ namespace GameCult.Caching
// The durable record at a key decoded as a load decodes it, at its durable version: null when the store holds none there or
// holds it foreign. The one decode both refusals reload through.
internal override CultStoredDocument? ReadDurable(string key) => Held(() => DurableAt(ReadSnapshot(), key));
private CultStoredDocument? DurableAt(Durable? durable, string key)
{
var record = durable?.Snapshot.Records.FirstOrDefault(candidate => string.Equals(candidate.Key, key, StringComparison.Ordinal));

View file

@ -0,0 +1,131 @@
using System;
using System.Collections.Generic;
using System.IO;
using GameCult.Caching.MessagePack;
using MessagePack;
using NUnit.Framework;
namespace GameCult.Caching.OutsideStoreTests
{
// variants:follow_up:update-copy-from-stored-bytes: Update takes its version and its copy from one durable read of the home
// store, never from the held instance. Consumer side: public API only, so a change made in place to an instance the cache
// hands out can never be committed as if it had been read.
public class HeldInstanceUpdateTests
{
[CultDocument("tests.held_counter", "tests.held_counter.v1")]
[MessagePackObject]
public sealed class HeldCounter
{
[Key(0)] [CultName] public string Name { get; set; } = string.Empty;
[Key(1)] public int Value { get; set; }
}
private static readonly CultDocumentRegistry Registry = CultDocumentRegistry.ForTypes(new[] { typeof(HeldCounter) });
private static readonly CultRecordKey Key = new("counter");
private string _directory = string.Empty;
[SetUp]
public void CreateDirectory()
{
_directory = Path.Combine(Path.GetTempPath(), $"cultlib-held-update-{Guid.NewGuid():N}");
Directory.CreateDirectory(_directory);
}
[TearDown]
public void DeleteDirectory()
{
if (Directory.Exists(_directory))
Directory.Delete(_directory, recursive: true);
}
private static CultCache Open(string path, bool directory)
{
var cache = new CultCache(Registry, CultCacheMessagePack.CreateCodec(Registry));
cache.AddBackingStore(directory ? new DirectoryMessagePackBackingStore(path) : new SingleFileMessagePackBackingStore(path));
cache.PullAllBackingStoresAsync().GetAwaiter().GetResult();
return cache;
}
private string Seed(bool directory, int value)
{
var path = Path.Combine(_directory, "held.cc");
using var cache = Open(path, directory);
Assert.That(cache.Commit(batch => batch.Upsert(new HeldCounter { Name = "counter", Value = value }, new CultRecordHandle<HeldCounter>(Key))), Is.True);
return path;
}
private static int StoredValue(string path, bool directory)
{
using var reader = Open(path, directory);
return reader.Get<HeldCounter>(Key)!.Value;
}
private static HeldCounter Plus(HeldCounter? counter, int by)
{
counter!.Value += by;
return counter;
}
[TestCase(false)]
[TestCase(true)]
public void InPlaceChangeToHeldInstanceIsNeitherAppliedNorPersisted(bool directory)
{
var path = Seed(directory, 1);
using var cache = Open(path, directory);
cache.Get<HeldCounter>(Key)!.Value = 999;
var seen = new List<int>();
var result = cache.Update<HeldCounter>(Key, counter => { seen.Add(counter!.Value); return Plus(counter, 1); });
Assert.That(result.Outcome, Is.EqualTo(CultUpdateOutcome.Updated));
Assert.That(seen, Is.EqualTo(new[] { 1 }), "apply saw the in-place change");
Assert.That(result.Document!.Value, Is.EqualTo(2));
Assert.That(StoredValue(path, directory), Is.EqualTo(2), "the in-place 999 was persisted");
}
[TestCase(false)]
[TestCase(true)]
public void InPlaceChangeToAPreviousResultIsNotCommitted(bool directory)
{
var path = Seed(directory, 1);
using var cache = Open(path, directory);
var first = cache.Update<HeldCounter>(Key, counter => Plus(counter, 1));
Assert.That(first.Outcome, Is.EqualTo(CultUpdateOutcome.Updated));
first.Document!.Value = 999;
var seen = new List<int>();
var second = cache.Update<HeldCounter>(Key, counter => { seen.Add(counter!.Value); return Plus(counter, 1); });
Assert.That(second.Outcome, Is.EqualTo(CultUpdateOutcome.Updated));
Assert.That(seen, Is.EqualTo(new[] { 2 }), "apply ran more than once or saw the in-place change");
Assert.That(StoredValue(path, directory), Is.EqualTo(3));
}
[TestCase(false)]
[TestCase(true)]
public void DeclineAfterInPlaceChangeWritesNothing(bool directory)
{
var path = Seed(directory, 1);
using var cache = Open(path, directory);
cache.Get<HeldCounter>(Key)!.Value = 999;
var before = Snapshot(path);
Assert.That(cache.Update<HeldCounter>(Key, _ => null).Outcome, Is.EqualTo(CultUpdateOutcome.Declined));
Assert.That(Snapshot(path), Is.EqualTo(before));
Assert.That(StoredValue(path, directory), Is.EqualTo(1));
}
// Every file of the store: the single file, or the manifest and its pages.
private static Dictionary<string, byte[]> Snapshot(string path)
{
var files = new Dictionary<string, byte[]> { [path] = File.ReadAllBytes(path) };
var pages = path + ".records";
if (Directory.Exists(pages))
foreach (var file in Directory.GetFiles(pages, "*", SearchOption.AllDirectories))
files[file] = File.ReadAllBytes(file);
return files;
}
}
}

View file

@ -364,5 +364,18 @@ namespace GameCult.Caching.OutsideStoreTests
Assert.That(() => cache.Commit(batch => batch.Remove(K)), Throws.InstanceOf<CultSchemaConflictException>(), "removal");
Assert.That(disk.Records["k"], Is.EqualTo(before));
}
// Only the library can stamp a version on a decoded record, so a store kind outside it cannot read one honestly: Update refuses before apply.
[Test]
public void UpdateOverAnOutsideStoreIsNotSupported()
{
var disk = new Disk();
PutOnDisk(disk, AB, "k", new OutA { Name = "k", Value = 1 });
using var cache = Open(disk, AB);
var calls = 0;
Assert.That(() => cache.Update<OutA>(K, a => { calls++; return a; }), Throws.TypeOf<NotSupportedException>());
Assert.That(calls, Is.EqualTo(0), "apply ran for a store that cannot serve Update");
}
}
}

View file

@ -224,6 +224,28 @@ namespace GameCult.Caching.Tests
Assert.That(AllIds(reread), Is.EqualTo(first), "the ids minted at load are the ids written, and stay through repeated writes");
}
// Update reads the store's bytes, which hold no ids; it mints the ids the load minted, so the write keeps the ids the cache showed.
[Test]
public void UpdateOfAPreIdRecordPersistsTheIdsTheLoadMinted()
{
var path = PathOf("old.cc");
WritePreIdStore(path, null, ("a", "a", 2));
var key = new CultRecordKey("a");
string[] shown;
using (var cache = Open(path))
{
shown = AllIds(cache.Get<IdDeck>(key)!);
Assert.That(shown, Has.All.Not.Empty);
var result = cache.Update<IdDeck>(key, deck => { deck!.Name = "a2"; return deck; });
Assert.That(result.Outcome, Is.EqualTo(CultUpdateOutcome.Updated));
}
var written = MessagePackSerializer.Deserialize<IdDeck>(DiskRecord(path, "a").Payload);
Assert.That(written.Name, Is.EqualTo("a2"));
Assert.That(AllIds(written), Is.EqualTo(shown), "the write replaced the ids the cache showed with random ones");
}
[Test]
public void MintElementIdsRewritesEveryPreIdRecordAndNothingElse()
{

View file

@ -219,8 +219,19 @@ namespace GameCult.Caching.Tests
var calls = 0;
Func<UpdateCounter?, UpdateCounter?> apply = counter => { calls++; return Plus(counter, 1); };
using (var codecLess = Open(path, false, codec: false))
Assert.That(() => codecLess.Update(Key, apply), Throws.InvalidOperationException);
// A cache with no store copies its held instance, so it needs a codec.
using (var storeLess = new CultCache(Registry))
{
storeLess.Commit(batch => batch.Upsert(new UpdateCounter { Name = "counter", Value = 1 }, new CultRecordHandle<UpdateCounter>(Key)));
Assert.That(() => storeLess.Update(Key, apply), Throws.InvalidOperationException.With.Message.Contains("no codec"));
}
// Stores are attached, but none is home to the type: refused before apply.
using (var noHome = new CultCache(Registry, CultCacheMessagePack.CreateCodec(Registry)))
{
noHome.AddBackingStore(new SingleFileMessagePackBackingStore(PathOf("nohome.cc")), typeof(UpdateTally));
Assert.That(() => noHome.Update(Key, apply), Throws.InvalidOperationException.With.Message.Contains("names its home store"));
}
using (var cache = Open(path, false))
{
Assert.That(() => cache.Update(Key, apply, maxAttempts: 0), Throws.TypeOf<ArgumentOutOfRangeException>());
@ -479,6 +490,54 @@ namespace GameCult.Caching.Tests
Assert.That(cache.Get<UpdateCounter>(Key)!.Value, Is.EqualTo(200));
}
[TestCase(false)]
[TestCase(true)]
public void StaleCacheFirstAttemptSeesTheStore(bool directory)
{
var path = PathOf("stale.cc");
Seed(path, directory, 1);
using var a = Open(path, directory);
using var b = Open(path, directory);
Assert.That(b.Get<UpdateCounter>(Key)!.Value, Is.EqualTo(1));
Rival(a, 7);
var seen = new List<int>();
var result = b.Update<UpdateCounter>(Key, counter => { seen.Add(counter!.Value); return Plus(counter, 1); });
Assert.That(result.Outcome, Is.EqualTo(CultUpdateOutcome.Updated));
Assert.That(seen, Is.EqualTo(new[] { 7 }), "the first attempt read the cache's stale copy");
Assert.That(StoredValue(path, directory), Is.EqualTo(8));
}
[Test]
public void CodecLessStoreBackedCacheUpdates()
{
var path = PathOf("codecless.cc");
Seed(path, false, 1);
var calls = 0;
using (var cache = Open(path, false, codec: false))
Assert.That(cache.Update<UpdateCounter>(Key, counter => { calls++; return Plus(counter, 1); }).Outcome, Is.EqualTo(CultUpdateOutcome.Updated));
Assert.That(calls, Is.EqualTo(1));
Assert.That(StoredValue(path, false), Is.EqualTo(2));
}
[TestCase(false)]
[TestCase(true)]
public void AStoreHoldingNothingAtTheKeyIsAbsentEvenIfTheCacheHoldsIt(bool directory)
{
var path = PathOf("gone.cc");
Seed(path, directory, 5);
using var a = Open(path, directory);
using var b = Open(path, directory);
Assert.That(b.Get<UpdateCounter>(Key), Is.Not.Null);
Assert.That(a.Commit(batch => batch.Remove(Key)), Is.True);
var seen = new List<UpdateCounter?>();
Assert.That(b.Update<UpdateCounter>(Key, counter => { seen.Add(counter); return null; }).Outcome, Is.EqualTo(CultUpdateOutcome.Declined));
Assert.That(seen, Is.EqualTo(new UpdateCounter?[] { null }));
}
[CultDocument("tests.update_counter", "tests.update_counter.v1")]
[MessagePackObject(AllowPrivate = true)]
internal sealed class UpdateCounter