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" });