Materialize exact reactive state on demand
This commit is contained in:
parent
a1f25af2e0
commit
0ef59b50c2
18 changed files with 164 additions and 7 deletions
|
|
@ -14,7 +14,7 @@
|
|||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||
<Copyright>© Meta Vens and contributors</Copyright>
|
||||
<PublishRepositoryUrl>true</PublishRepositoryUrl>
|
||||
<Version>1.0.29</Version>
|
||||
<Version>1.0.30</Version>
|
||||
|
||||
<!-- Symbols/debugging -->
|
||||
<EmbedUntrackedSources>true</EmbedUntrackedSources>
|
||||
|
|
|
|||
|
|
@ -317,6 +317,7 @@ namespace GameCult.Mesh
|
|||
public void Observe(CultNetDatabaseSubscriptionDemand demand)
|
||||
{
|
||||
if (demand == null) throw new ArgumentNullException(nameof(demand));
|
||||
if (demand.BodyIds.Count == 0) return;
|
||||
var key = demand.ConsumerRuntimeId + "\u001f" + demand.SubscriptionId;
|
||||
lock (_gate)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -528,6 +528,11 @@ shared-file mapping, or network from observed locality and available adapters.
|
|||
Body bytes never become subscription snapshot payload. Either subscription may
|
||||
legitimately start with `HasValue == false`; it remains active until the provider
|
||||
publishes the exact record, avoiding a startup retry loop or broad snapshot.
|
||||
`CultNetDatabaseSubscriptionServer.DemandChanged` exposes the exact requested
|
||||
record and schema set as well as optional body negotiation. A provider can
|
||||
therefore materialize a computed record only while it has consumers. The watch
|
||||
is installed before demand is announced, so the first publication travels as a
|
||||
normal live change and does not require a bootstrap snapshot.
|
||||
|
||||
### Client Prediction
|
||||
|
||||
|
|
|
|||
|
|
@ -920,6 +920,18 @@ stream demand merely by existing.
|
|||
and server disposal all update the same typed demand projection. Stable peer
|
||||
identity, not per-message wrapper identity, keys subscription lifetime.
|
||||
|
||||
Exact reactive document demand carries the requested record and schema set even
|
||||
when no hot body is involved. Providers may use that demand to materialize
|
||||
computed documents lazily; the subscription watch exists before activation is
|
||||
reported, so frame zero is delivered on the live path. Dynamic state is not
|
||||
forced back through a broad snapshot merely because the record did not exist at
|
||||
subscribe time.
|
||||
|
||||
The client installs the declared delivery mode before sending the subscription.
|
||||
A provider may publish synchronously from demand activation, so a live change
|
||||
can correctly precede the empty bootstrap acknowledgement without briefly
|
||||
becoming durable replica state.
|
||||
|
||||
`CultMesh.SubscribeLiveBodyAsync(...)` is the narrow public construction path.
|
||||
Its `CultMeshLiveBodySubscription` names only the logical body, consumer, and
|
||||
subscription lifetime. `CultMeshLiveBody` owns the ephemeral latest-publication
|
||||
|
|
|
|||
|
|
@ -116,6 +116,7 @@ namespace GameCult.Networking
|
|||
var completion = new TaskCompletionSource<IReadOnlyList<object>>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
if (!_initialSnapshots.TryAdd(messageId, new PendingSubscription(subscriptionId, deliveryMode, completion)))
|
||||
throw new InvalidOperationException($"Duplicate database subscription message id '{messageId}'.");
|
||||
_deliveryModes[subscriptionId] = deliveryMode;
|
||||
|
||||
CancellationTokenRegistration cancellation = default;
|
||||
if (cancellationToken.CanBeCanceled)
|
||||
|
|
@ -151,6 +152,7 @@ namespace GameCult.Networking
|
|||
}
|
||||
catch
|
||||
{
|
||||
_deliveryModes.TryRemove(subscriptionId, out _);
|
||||
if (_initialSnapshots.TryRemove(messageId, out var pending))
|
||||
pending.Completion.TrySetException(new InvalidOperationException("CultNet subscription request could not be sent."));
|
||||
throw;
|
||||
|
|
@ -198,7 +200,6 @@ namespace GameCult.Networking
|
|||
{
|
||||
applied = await _documents.ApplyRawSnapshotResponseAsync(_cache, message).ConfigureAwait(false);
|
||||
}
|
||||
_deliveryModes[pending.SubscriptionId] = pending.DeliveryMode;
|
||||
pending.Completion.TrySetResult(applied);
|
||||
}
|
||||
catch (Exception error)
|
||||
|
|
|
|||
|
|
@ -41,7 +41,11 @@ namespace GameCult.Networking
|
|||
_peerLifecycle.PeerDisconnected += HandlePeerDisconnected;
|
||||
}
|
||||
|
||||
/// <summary>Raised when exact document subscriptions add or remove typed hot-body demand.</summary>
|
||||
/// <summary>
|
||||
/// Raised when an exact subscription is activated or withdrawn. Providers can use
|
||||
/// the requested records and schemas to materialize reactive state only while a
|
||||
/// consumer needs it; body publishers additionally use the negotiated body route.
|
||||
/// </summary>
|
||||
public event Action<CultNetDatabaseSubscriptionDemand>? DemandChanged;
|
||||
|
||||
/// <summary>Detaches handlers and releases all active watches.</summary>
|
||||
|
|
@ -142,11 +146,21 @@ namespace GameCult.Networking
|
|||
SubscriptionKey key,
|
||||
bool active)
|
||||
{
|
||||
if (request.BodyIds is not { Length: > 0 }) return;
|
||||
DemandChanged?.Invoke(new CultNetDatabaseSubscriptionDemand(
|
||||
string.IsNullOrWhiteSpace(request.ConsumerRuntimeId) ? key.Id : request.ConsumerRuntimeId!,
|
||||
key.Id,
|
||||
request.BodyIds.Where(value => !string.IsNullOrWhiteSpace(value)).Distinct(StringComparer.Ordinal).ToArray(),
|
||||
(request.RecordKeys ?? Array.Empty<string>())
|
||||
.Where(value => !string.IsNullOrWhiteSpace(value))
|
||||
.Distinct(StringComparer.Ordinal)
|
||||
.ToArray(),
|
||||
(request.SchemaIds ?? Array.Empty<string>())
|
||||
.Where(value => !string.IsNullOrWhiteSpace(value))
|
||||
.Distinct(StringComparer.Ordinal)
|
||||
.ToArray(),
|
||||
(request.BodyIds ?? Array.Empty<string>())
|
||||
.Where(value => !string.IsNullOrWhiteSpace(value))
|
||||
.Distinct(StringComparer.Ordinal)
|
||||
.ToArray(),
|
||||
(request.SupportedBodyTransports ?? Array.Empty<string>())
|
||||
.Where(value => !string.IsNullOrWhiteSpace(value))
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
|
|
@ -284,7 +298,7 @@ namespace GameCult.Networking
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>One active or withdrawn consumer demand for logical hot bodies.</summary>
|
||||
/// <summary>One active or withdrawn exact state subscription and its optional hot-body route.</summary>
|
||||
public sealed class CultNetDatabaseSubscriptionDemand
|
||||
{
|
||||
public CultNetDatabaseSubscriptionDemand(
|
||||
|
|
@ -294,9 +308,32 @@ namespace GameCult.Networking
|
|||
IReadOnlyList<string> supportedBodyTransports,
|
||||
bool sameMachine,
|
||||
bool active)
|
||||
: this(
|
||||
consumerRuntimeId,
|
||||
subscriptionId,
|
||||
Array.Empty<string>(),
|
||||
Array.Empty<string>(),
|
||||
bodyIds,
|
||||
supportedBodyTransports,
|
||||
sameMachine,
|
||||
active)
|
||||
{
|
||||
}
|
||||
|
||||
public CultNetDatabaseSubscriptionDemand(
|
||||
string consumerRuntimeId,
|
||||
string subscriptionId,
|
||||
IReadOnlyList<string> recordKeys,
|
||||
IReadOnlyList<string> schemaIds,
|
||||
IReadOnlyList<string> bodyIds,
|
||||
IReadOnlyList<string> supportedBodyTransports,
|
||||
bool sameMachine,
|
||||
bool active)
|
||||
{
|
||||
ConsumerRuntimeId = consumerRuntimeId;
|
||||
SubscriptionId = subscriptionId;
|
||||
RecordKeys = recordKeys;
|
||||
SchemaIds = schemaIds;
|
||||
BodyIds = bodyIds;
|
||||
SupportedBodyTransports = supportedBodyTransports;
|
||||
SameMachine = sameMachine;
|
||||
|
|
@ -305,6 +342,8 @@ namespace GameCult.Networking
|
|||
|
||||
public string ConsumerRuntimeId { get; }
|
||||
public string SubscriptionId { get; }
|
||||
public IReadOnlyList<string> RecordKeys { get; }
|
||||
public IReadOnlyList<string> SchemaIds { get; }
|
||||
public IReadOnlyList<string> BodyIds { get; }
|
||||
public IReadOnlyList<string> SupportedBodyTransports { get; }
|
||||
public bool SameMachine { get; }
|
||||
|
|
|
|||
|
|
@ -1509,6 +1509,8 @@ namespace GameCult.Networking.Tests
|
|||
var observed = await AwaitWithTimeout(active.Task, TimeSpan.FromSeconds(2));
|
||||
Assert.That(observed.ConsumerRuntimeId, Is.EqualTo("eve-unity"));
|
||||
Assert.That(observed.SubscriptionId, Is.EqualTo("world-body"));
|
||||
Assert.That(observed.RecordKeys, Is.EqualTo(new[] { "world:entities", "mesh:body:world:latest" }));
|
||||
Assert.That(observed.SchemaIds, Is.Empty);
|
||||
Assert.That(observed.BodyIds, Is.EqualTo(new[] { "world" }));
|
||||
Assert.That(observed.SupportedBodyTransports, Is.EqualTo(new[] { "SharedMemory", "Network" }));
|
||||
Assert.That(observed.SameMachine, Is.True);
|
||||
|
|
@ -1524,6 +1526,53 @@ namespace GameCult.Networking.Tests
|
|||
Assert.That(removed.Active, Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DatabaseSubscriptionServer_ProjectsExactReactiveStateDemandWithoutBodyDemand()
|
||||
{
|
||||
var database = new CultNetDatabase(new CultCache());
|
||||
using var server = new RudpCultNetSchemaServer(new RudpCultNetSchemaServerOptions
|
||||
{
|
||||
RuntimeId = "database-reactive-demand-server",
|
||||
Socket = BindUdpSocket()
|
||||
});
|
||||
using var subscriptions = new CultNetDatabaseSubscriptionServer(server, database);
|
||||
var active = new TaskCompletionSource<CultNetDatabaseSubscriptionDemand>(
|
||||
TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
subscriptions.DemandChanged += demand =>
|
||||
{
|
||||
if (demand.Active) active.TrySetResult(demand);
|
||||
};
|
||||
using var cancellation = new CancellationTokenSource();
|
||||
var serverThread = new Thread(() =>
|
||||
{
|
||||
while (!cancellation.IsCancellationRequested)
|
||||
{
|
||||
_ = server.PollOnceAsync().GetAwaiter().GetResult();
|
||||
Thread.Sleep(1);
|
||||
}
|
||||
}) { IsBackground = true };
|
||||
serverThread.Start();
|
||||
|
||||
using var client = CultNetSchemaClients.CreateRudp("database-reactive-demand-client");
|
||||
client.Connect("127.0.0.1", server.LocalEndPoint.Port);
|
||||
await WaitUntilAsync(() => client.Connected, TimeSpan.FromSeconds(2));
|
||||
client.SendCultNet(new CultNetDatabaseSubscribeMessage
|
||||
{
|
||||
MessageId = "subscribe-reactive",
|
||||
SubscriptionId = "fog-field",
|
||||
RecordKeys = ["world:field:fog"],
|
||||
SchemaIds = ["gamecult.fields.splats.v1"],
|
||||
ConsumerRuntimeId = "eve-unity",
|
||||
IncludeSnapshot = false
|
||||
});
|
||||
|
||||
var observed = await AwaitWithTimeout(active.Task, TimeSpan.FromSeconds(2));
|
||||
cancellation.Cancel();
|
||||
Assert.That(observed.RecordKeys, Is.EqualTo(new[] { "world:field:fog" }));
|
||||
Assert.That(observed.SchemaIds, Is.EqualTo(new[] { "gamecult.fields.splats.v1" }));
|
||||
Assert.That(observed.BodyIds, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DatabaseSubscriptionServer_WithdrawsDemandWhenPeerDisconnects()
|
||||
{
|
||||
|
|
@ -1757,6 +1806,56 @@ namespace GameCult.Networking.Tests
|
|||
Assert.That(value.Current.Text, Is.EqualTo("updated"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DatabaseSubscriptionClient_ReceivesProviderMaterializedValueFromExactDemand()
|
||||
{
|
||||
var sourceDatabase = new CultNetDatabase(new CultCache());
|
||||
const string recordKey = "tests:subscription-client:demanded-note";
|
||||
using var server = new RudpCultNetSchemaServer(new RudpCultNetSchemaServerOptions
|
||||
{
|
||||
RuntimeId = "database-demanded-value-server",
|
||||
Socket = BindUdpSocket()
|
||||
});
|
||||
using var subscriptions = new CultNetDatabaseSubscriptionServer(server, sourceDatabase);
|
||||
subscriptions.DemandChanged += demand =>
|
||||
{
|
||||
if (!demand.Active || !demand.RecordKeys.Contains(recordKey, StringComparer.Ordinal))
|
||||
return;
|
||||
sourceDatabase.PutAsync(new CultRecordKey(recordKey), new NetworkSchemaNote
|
||||
{
|
||||
Schema = "tests.networking_note.v1",
|
||||
Text = "materialized-on-demand"
|
||||
}).GetAwaiter().GetResult();
|
||||
};
|
||||
using var cancellation = new CancellationTokenSource();
|
||||
var serverThread = new Thread(() =>
|
||||
{
|
||||
while (!cancellation.IsCancellationRequested)
|
||||
{
|
||||
_ = server.PollOnceAsync().GetAwaiter().GetResult();
|
||||
Thread.Sleep(1);
|
||||
}
|
||||
}) { IsBackground = true };
|
||||
serverThread.Start();
|
||||
|
||||
var targetCache = new CultCache();
|
||||
var targetDocuments = new CultNetDocumentRegistry(targetCache.Registry)
|
||||
.Register(CultNetDocumentBinding.ForDocument<NetworkSchemaNote>(targetCache.Registry));
|
||||
var transport = CultNetSchemaClients.CreateRudp("database-demanded-value-client");
|
||||
using var client = new CultNetDatabaseSubscriptionClient(transport, targetCache, targetDocuments);
|
||||
transport.Connect("127.0.0.1", server.LocalEndPoint.Port);
|
||||
await WaitUntilAsync(() => transport.Connected, TimeSpan.FromSeconds(2));
|
||||
|
||||
using var value = await AwaitWithTimeout(
|
||||
client.SubscribeLiveValueAsync<NetworkSchemaNote>("demanded-note", recordKey),
|
||||
TimeSpan.FromSeconds(2));
|
||||
await WaitUntilAsync(() => value.HasValue, TimeSpan.FromSeconds(2));
|
||||
|
||||
cancellation.Cancel();
|
||||
Assert.That(value.Current.Text, Is.EqualTo("materialized-on-demand"));
|
||||
Assert.That(targetCache.Get(new CultRecordKey(recordKey)), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DatabaseSubscription_FiltersLiveChangesByWireSchemaBinding()
|
||||
{
|
||||
|
|
|
|||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"name": "org.gamecult.cultlib",
|
||||
"displayName": "CultLib",
|
||||
"version": "1.0.29",
|
||||
"version": "1.0.30",
|
||||
"unity": "2021.3",
|
||||
"description": "CultCache, CultNet, and CultMesh managed runtime assemblies for Unity.",
|
||||
"license": "MIT",
|
||||
|
|
|
|||
Loading…
Reference in a new issue