CultNet subscription server: a forward that throws still drains the deferred ones in order; contract scopes the fence to the C# subscription server; tests pin the fence boundary, the max, queue order and a nested Reconcile
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
30eb3a4edd
commit
413c64a442
3 changed files with 171 additions and 14 deletions
|
|
@ -145,8 +145,8 @@ A malformed put or delete is `message_invalid`, not a per-operation code: the pe
|
|||
peer that does not know them ignores them and applies each change alone. A replica that knows them applies the
|
||||
commit's changes as one commit, so the replica judges the result (a swap of two unique-index values) and not the state
|
||||
between records. A replica whose cache refuses a commit is faulted, never silent. `commitId` is opaque and valid only
|
||||
within the subscription. A server that re-evaluates a subscription (C# `Reconcile`) sends its diff as one such commit, and a server
|
||||
never forwards a commit that its last snapshot to the subscription already reflects. A replica may bound how many
|
||||
within the subscription. A server that re-evaluates a subscription (C# `Reconcile`) sends its diff as one such commit, and the C# subscription server
|
||||
never forwards a commit that its last snapshot to the subscription already reflects (the database server adopts next). A replica may bound how many
|
||||
changes it buffers across incomplete commits (the C# client: `MaxBufferedChanges`, 1,000,000). One bound covers every
|
||||
commit, whatever made it, a `Reconcile` diff included: a commit announcing more faults the replica loudly, and a faulted
|
||||
consumer resubscribes for a snapshot, which is one message and has no such bound. A server does not split a commit to
|
||||
|
|
|
|||
|
|
@ -521,33 +521,49 @@ namespace GameCult.Networking
|
|||
string.Equals(left.SourceRole, right.SourceRole, StringComparison.Ordinal);
|
||||
|
||||
// The commit being forwarded, under _lifecycleGate: the change messages it produces are held here, not sent one by one.
|
||||
// Set only by InOutbox; at most one is open. A forward that arrives while one is open (a DemandChanged handler, an
|
||||
// authorizer or a cache write re-entering on this thread) waits in _deferred and runs after the open one is sent, so
|
||||
// its older held changes are never overtaken.
|
||||
// Set only by InOutbox; at most one is open.
|
||||
private CommitOutbox? _outbox;
|
||||
private readonly Queue<Action> _deferred = new();
|
||||
private bool _draining;
|
||||
|
||||
// Under _lifecycleGate: runs work now, or after the open outbox is sent.
|
||||
// One forward runs at a time. A forward that arrives while another is running (a DemandChanged handler, an authorizer, a
|
||||
// cache write or a replica handler re-entering on this thread, while the outbox is open or being sent) waits in _deferred
|
||||
// and runs, in arrival order, after the running one, so its older held changes are never overtaken.
|
||||
private readonly Queue<Action> _deferred = new();
|
||||
private bool _forwarding;
|
||||
|
||||
// Under _lifecycleGate: runs work now, or after the running forward. A forward that throws does not strand the deferred
|
||||
// ones: all of them run, and the first exception is rethrown afterwards.
|
||||
private void Forward(Action work)
|
||||
{
|
||||
if (_outbox != null)
|
||||
if (_forwarding)
|
||||
{
|
||||
_deferred.Enqueue(work);
|
||||
return;
|
||||
}
|
||||
work();
|
||||
if (_draining) return;
|
||||
_draining = true;
|
||||
_forwarding = true;
|
||||
Exception? first = null;
|
||||
try
|
||||
{
|
||||
while (_deferred.Count > 0)
|
||||
_deferred.Dequeue()();
|
||||
var next = work;
|
||||
while (true)
|
||||
{
|
||||
try
|
||||
{
|
||||
next();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
first ??= exception;
|
||||
}
|
||||
if (_deferred.Count == 0) break;
|
||||
next = _deferred.Dequeue();
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
_draining = false;
|
||||
_forwarding = false;
|
||||
}
|
||||
if (first != null)
|
||||
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(first).Throw();
|
||||
}
|
||||
|
||||
private void InOutbox(string commitId, ICultNetSchemaServerPeer peer, Action produce)
|
||||
|
|
|
|||
|
|
@ -838,6 +838,147 @@ namespace GameCult.Networking.Tests
|
|||
Assert.That(run.Client.Fault, Is.Null);
|
||||
}
|
||||
|
||||
// A commit exactly at the snapshot's sequence is reflected by it and sends nothing; one above it is news.
|
||||
[Test]
|
||||
public async Task ACommitAtTheSnapshotSequenceIsDroppedAndOneAboveIsApplied()
|
||||
{
|
||||
using var run = new Replication(source => Stage(source, ("a", "s1"), ("b", "s9")));
|
||||
Task write;
|
||||
int sentAtRelease;
|
||||
|
||||
lock (run.Gate)
|
||||
{
|
||||
write = Task.Run(() => Stage(run.Source, ("a", "s2")));
|
||||
Assert.That(SpinWait.SpinUntil(() => run.Source.Get<ReplicaItem>(new CultRecordKey("a"))!.Sku == "s2", 10_000));
|
||||
run.Reconcile();
|
||||
sentAtRelease = run.Peer.Sent.Count;
|
||||
}
|
||||
await write;
|
||||
await Task.Delay(200);
|
||||
Assert.That(run.Peer.Sent.Count, Is.EqualTo(sentAtRelease), "the commit the snapshot reflects was sent again");
|
||||
|
||||
Stage(run.Source, ("a", "s4"));
|
||||
await Until(() => run.ReplicaItem("a")!.Sku == "s4");
|
||||
Assert.That(run.Client.Fault, Is.Null);
|
||||
}
|
||||
|
||||
// An unauthorized Reconcile takes the empty snapshot; it must not lower the fence the last real snapshot set.
|
||||
[Test]
|
||||
public async Task AnUnauthorizedReconcileDoesNotLowerTheFence()
|
||||
{
|
||||
var allowed = true;
|
||||
using var run = new Replication(source => Stage(source, ("a", "s1"), ("b", "s9")),
|
||||
authorizeRequest: (_, _) => allowed);
|
||||
Task write;
|
||||
int sentAtRelease;
|
||||
|
||||
lock (run.Gate)
|
||||
{
|
||||
write = Task.Run(() => Stage(run.Source, ("a", "s2")));
|
||||
Assert.That(SpinWait.SpinUntil(() => run.Source.Get<ReplicaItem>(new CultRecordKey("a"))!.Sku == "s2", 10_000));
|
||||
run.Reconcile();
|
||||
allowed = false;
|
||||
run.Reconcile();
|
||||
allowed = true;
|
||||
sentAtRelease = run.Peer.Sent.Count;
|
||||
}
|
||||
await write;
|
||||
await Task.Delay(200);
|
||||
|
||||
Assert.That(run.Peer.Sent.Count, Is.EqualTo(sentAtRelease), "a commit the fence covered was replayed");
|
||||
}
|
||||
|
||||
// Two writes from one handler are two commits deferred behind the open diff; they reach the replica in the order written.
|
||||
[Test]
|
||||
public async Task DeferredCommitsReachTheReplicaInArrivalOrder()
|
||||
{
|
||||
var armed = false;
|
||||
var allowed = true;
|
||||
using var run = new Replication(source => Stage(source, ("a", "one"), ("b", "two")),
|
||||
authorizeRequest: (_, _) => allowed);
|
||||
run.Server.DemandChanged += demand =>
|
||||
{
|
||||
if (!armed || !demand.Active) return;
|
||||
Stage(run.Source, ("a", "three"));
|
||||
Stage(run.Source, ("a", "four"));
|
||||
};
|
||||
allowed = false;
|
||||
run.Reconcile();
|
||||
await Until(() => run.ReplicaItem("a") == null);
|
||||
|
||||
armed = true;
|
||||
allowed = true;
|
||||
run.Reconcile();
|
||||
await Until(() => run.ReplicaItem("a")?.Sku == "four");
|
||||
await Task.Delay(100);
|
||||
|
||||
Assert.That(run.ReplicaItem("a")!.Sku, Is.EqualTo("four"));
|
||||
Assert.That(run.Client.Fault, Is.Null);
|
||||
}
|
||||
|
||||
// A Reconcile called from inside a forward is a forward of its own: it runs after the open diff, on state that includes
|
||||
// the handler's write, and does not send the older held diff over it.
|
||||
[Test]
|
||||
public async Task AReconcileNestedInsideAForwardRunsAfterIt()
|
||||
{
|
||||
var armed = false;
|
||||
var allowed = true;
|
||||
using var run = new Replication(source => Stage(source, ("a", "one"), ("b", "two")),
|
||||
authorizeRequest: (_, _) => allowed);
|
||||
run.Server.DemandChanged += demand =>
|
||||
{
|
||||
if (!armed || !demand.Active) return;
|
||||
armed = false;
|
||||
Stage(run.Source, ("a", "three"));
|
||||
run.Reconcile();
|
||||
};
|
||||
allowed = false;
|
||||
run.Reconcile();
|
||||
await Until(() => run.ReplicaItem("a") == null);
|
||||
|
||||
armed = true;
|
||||
allowed = true;
|
||||
run.Reconcile();
|
||||
await Until(() => run.ReplicaItem("a")?.Sku == "three");
|
||||
await Task.Delay(100);
|
||||
|
||||
Assert.That(run.ReplicaItem("a")!.Sku, Is.EqualTo("three"));
|
||||
Assert.That(run.ReplicaItem("b")!.Sku, Is.EqualTo("two"));
|
||||
Assert.That(run.Client.Fault, Is.Null);
|
||||
}
|
||||
|
||||
// A second DemandChanged handler throws after the first wrote. The write's commit was deferred; the throw must not strand
|
||||
// it, and the replica ends equal to the source.
|
||||
[Test]
|
||||
public async Task AThrowFromTheOpenForwardDoesNotStrandADeferredCommit()
|
||||
{
|
||||
var armed = false;
|
||||
var allowed = true;
|
||||
using var run = new Replication(source => Stage(source, ("a", "one"), ("b", "two")),
|
||||
authorizeRequest: (_, _) => allowed);
|
||||
run.Server.DemandChanged += demand =>
|
||||
{
|
||||
if (armed && demand.Active) Stage(run.Source, ("a", "three"));
|
||||
};
|
||||
run.Server.DemandChanged += demand =>
|
||||
{
|
||||
if (armed && demand.Active) throw new InvalidOperationException("second handler");
|
||||
};
|
||||
allowed = false;
|
||||
run.Reconcile();
|
||||
await Until(() => run.ReplicaItem("a") == null);
|
||||
|
||||
armed = true;
|
||||
allowed = true;
|
||||
Assert.Throws<InvalidOperationException>(() => run.Reconcile());
|
||||
armed = false;
|
||||
Stage(run.Source, ("a", "four"));
|
||||
await Until(() => run.ReplicaItem("a")?.Sku == "four");
|
||||
|
||||
Assert.That(run.ReplicaItem("b")!.Sku, Is.EqualTo("two"));
|
||||
Assert.That(run.Client.Fault, Is.Null);
|
||||
}
|
||||
|
||||
// Source, wire and client all real: a CultNetDatabaseServer behind a Server on a loopback socket, a LiteNetLib client
|
||||
// standing in for the transport of a CultNetDatabaseSubscriptionClient. No handler is called directly.
|
||||
private sealed class SocketClient : ICultNetSchemaClient
|
||||
|
|
|
|||
Loading…
Reference in a new issue