From 9f6b2d40818506cb1182f55e2426f967144f98ad Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 30 Sep 2026 04:08:45 +0200 Subject: [PATCH] CultNetDatabase: a write no shard owns is refused, not cached and never logged PutAsync and DeleteAsync carry no door, so the journal cannot place an unowned schema and now skips it; accepting the write anyway was a silent loss of replication. They resolve their shard strictly and throw CultNetUnownedSchemaException (an InvalidOperationException naming the schema) before the cache is touched. Doors keep the fallback shard they were resolved to. No GameCult consumer configures shards outside vendored CultLib copies: AetheriaEve's node uses the default PrimaryAll shard, which owns everything. Co-Authored-By: Claude Sonnet 5.5 --- src/GameCult.Networking/CultNetDatabase.cs | 38 +++++++++++++++++-- .../CultNetJournalTests.cs | 16 ++++++-- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/src/GameCult.Networking/CultNetDatabase.cs b/src/GameCult.Networking/CultNetDatabase.cs index 97d822a5..4ef5820b 100644 --- a/src/GameCult.Networking/CultNetDatabase.cs +++ b/src/GameCult.Networking/CultNetDatabase.cs @@ -500,6 +500,32 @@ namespace GameCult.Networking public override string ToString() => $"{ShardId}:{Sequence}"; } + /// + /// Raised when a database write names a schema and key that no configured shard owns. Nothing was cached. + /// + public sealed class CultNetUnownedSchemaException : InvalidOperationException + { + /// + /// Creates an unowned schema exception. + /// + public CultNetUnownedSchemaException(string schemaId, CultRecordKey key) + : base($"No shard owns schema '{schemaId}' key '{key.Value}'; the write was refused.") + { + SchemaId = schemaId; + Key = key; + } + + /// + /// Gets the schema that no shard owns. + /// + public string SchemaId { get; } + + /// + /// Gets the key of the refused write. + /// + public CultRecordKey Key { get; } + } + /// /// Raised to the writer when a shard primary committed changes and could not log them. The commits stand and the /// changes were published; each shard's log burned the sequences in and compacted past them, @@ -931,8 +957,7 @@ namespace GameCult.Networking if (document == null) throw new ArgumentNullException(nameof(document)); var descriptor = _cache.Registry.GetRequired(document.GetType()); - var shard = ResolveShardInternal(descriptor, key); - EnsurePrimary(shard, descriptor.SchemaId, key); + EnsurePrimary(OwningShard(descriptor, key), descriptor.SchemaId, key); return await _cache.UpsertAsync(document, new CultRecordHandle(key)).ConfigureAwait(false); } @@ -968,8 +993,7 @@ namespace GameCult.Networking { ThrowIfDisposed(); var descriptor = _cache.Registry.GetRequired(); - var shard = ResolveShardInternal(descriptor, key); - EnsurePrimary(shard, descriptor.SchemaId, key); + EnsurePrimary(OwningShard(descriptor, key), descriptor.SchemaId, key); _cache.Remove(new CultRecordHandle(key)); return Task.CompletedTask; @@ -1908,6 +1932,12 @@ namespace GameCult.Networking return _shards.FirstOrDefault(shard => shard.Matches(descriptor, key)) ?? _shards[0]; } + // The shard that owns the schema and key, for a write that carries no door and so cannot be placed by the wire message: + // a write no shard owns would be cached and never logged, so it is refused. + private CultNetShardDescriptor OwningShard(CultDocumentDescriptor descriptor, CultRecordKey key) => + _shards.FirstOrDefault(shard => shard.Matches(descriptor, key)) + ?? throw new CultNetUnownedSchemaException(descriptor.SchemaId, key); + private static void EnsurePrimary(CultNetShardDescriptor shard, string schemaId, CultRecordKey key, long? expectedEpoch = null) { if (expectedEpoch.HasValue && expectedEpoch.Value != shard.Epoch) diff --git a/tests/GameCult.Networking.Tests/CultNetJournalTests.cs b/tests/GameCult.Networking.Tests/CultNetJournalTests.cs index b50d675d..806ad08b 100644 --- a/tests/GameCult.Networking.Tests/CultNetJournalTests.cs +++ b/tests/GameCult.Networking.Tests/CultNetJournalTests.cs @@ -627,17 +627,25 @@ namespace GameCult.Networking.Tests Assert.That(database.GetMutationLog(ShardId).Select(entry => entry.Key.Value), Is.EqualTo(new[] { One.Value })); } - // A door chose its shard, by the fallback when none matches; the journal honours it. A plain PutAsync carries no - // door, so an unowned schema written through it is cached and not logged. + // The database is the sharded write door: a write it cannot place in a shard is refused before anything is cached. + // (An authoritative wire put carries a door that names its shard, and keeps the fallback shard.) [Test] - public async Task AnAuthoritativePutOfASchemaNoShardOwnsIsLoggedInTheFallbackShardButAPlainPutIsNot() + public async Task ADatabaseWriteOfASchemaNoShardOwnsIsRefusedNamingTheSchemaAndAnAuthoritativePutKeepsTheFallbackShard() { var store = new FlakyLogStore(); var cache = new CultCache(); var database = Database(cache, primary: true, store); var shard = database.Shards[0]; + var unowned = cache.Registry.GetRequired().SchemaId; + var plainKey = new CultRecordKey("unrelated:plain"); - await database.PutAsync(new CultRecordKey("unrelated:plain"), new MeshQuickstartNote { NoteId = "p", Body = "plain" }); + var put = Assert.ThrowsAsync(async () => + await database.PutAsync(plainKey, new MeshQuickstartNote { NoteId = "p", Body = "plain" }))!; + Assert.That(put.SchemaId, Is.EqualTo(unowned)); + Assert.That(put.Message, Does.Contain(unowned)); + Assert.That(put, Is.InstanceOf(), "a refused write reads as one"); + Assert.That(cache.Get(plainKey), Is.Null, "nothing was cached"); + Assert.ThrowsAsync(async () => await database.DeleteAsync(plainKey)); Assert.That(Sequences(store), Is.Empty); var message = database.Documents.CreateRawDocumentPutMessage("door", new CultRecordHandle(new CultRecordKey("unrelated:door")), new MeshQuickstartNote { NoteId = "d", Body = "door" });