feat(grpc-data): A2a — origin condition streaming: root-scoped items over the A1 hub, priority queue, host negotiation, consumer decode

This commit is contained in:
Joseph Doherty
2026-11-24 12:08:08 -05:00
parent 73142e52d4
commit 5b03c8a26e
17 changed files with 809 additions and 43 deletions
@@ -5,7 +5,7 @@ using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
namespace ZB.MOM.WW.OtOpcUa.Runtime.DataConnections;
/// <summary>One caller-bound subscription with bounded commands and one ordered delivery mode.</summary>
internal sealed class DataSubscriptionSession : IDataSubscriptionTransport
internal sealed partial class DataSubscriptionSession : IDataSubscriptionTransport
{
private readonly LocalDataServices _services;
private readonly DataAuthenticatedContext _context;
@@ -310,8 +310,10 @@ internal sealed class DataSubscriptionSession : IDataSubscriptionTransport
lock (_gate)
{
if (!_ackByCommand.Remove(command, out var entry) || _services.Clock.GetTimestamp() >= entry.Deadline) return false;
_ackDeadlines.Remove(entry); ArmAckTimer(); return true;
_ackDeadlines.Remove(entry); ArmAckTimer();
}
ActivateConditionItems(command); // A2: a condition item's frames follow its delivered ack
return true;
}
private async Task ProcessCommandsAsync(CancellationToken lifetimeToken)
@@ -379,6 +381,7 @@ internal sealed class DataSubscriptionSession : IDataSubscriptionTransport
_snapshotJobs.Clear();
foreach (var change in _pendingChanges.ToArray()) CleanupStaged(change);
_pendingChanges.Clear();
TearDownConditions();
}
_sessionLease.Dispose();
}
@@ -438,6 +441,8 @@ internal sealed class DataSubscriptionSession : IDataSubscriptionTransport
_stop.Token.ThrowIfCancellationRequested();
if (request is null || string.IsNullOrWhiteSpace(request.ClientItemId) || request.View is null || request.Settings is null)
{ acks.Add(new(request?.ClientItemId ?? "", DataItemStatus.InvalidArgument, null, null, null)); continue; }
// A2: a root-scoped condition item (the host only lets one through on a call that negotiated `conditions`).
if (request.ConditionScope is not null) { acks.Add(AddConditionItem(request, commandId)); continue; }
DataItemStatus status;
SubscriptionAuthorizedView? view;
try { request.Settings.Validate(); status = _services.AuthorizeSubscription(_context, _target, request.View, out view); }
@@ -596,6 +601,7 @@ internal sealed class DataSubscriptionSession : IDataSubscriptionTransport
if (!_handles!.IsCurrent(handle) || !_items.Remove(handle.Handle, out item)) item = null;
else { _handles.Remove(handle); _itemsByClient.Remove(item.ClientItemId); }
}
if (item is null && TryRemoveConditionItem(handle, out var conditionAck)) { acks.Add(conditionAck); continue; }
if (item is null) { acks.Add(new("", DataItemStatus.NotFound, null, null, null)); continue; }
ReleaseItem(item);
acks.Add(new(item.ClientItemId, DataItemStatus.Good, handle, null, null));
@@ -863,6 +869,7 @@ internal sealed class DataSubscriptionSession : IDataSubscriptionTransport
}
}
await PumpSnapshotsAsync().ConfigureAwait(false);
await PumpConditionsAsync().ConfigureAwait(false);
BeforeDirtyTickForTests?.Invoke(_outbound.Reader.Count, Volatile.Read(ref _queuedValues), interests.Count);
foreach (var interest in interests)
{
@@ -1208,14 +1215,7 @@ internal sealed class DataSubscriptionSession : IDataSubscriptionTransport
{
while (true)
{
PendingDelivery pending;
if (Interlocked.Exchange(ref owner._coalesced, 0) != 0) pending = owner.CoalescedControl();
else
{
if (!await owner._outbound.Reader.WaitToReadAsync(_linked!.Token).ConfigureAwait(false)) break;
if (!owner._outbound.Reader.TryRead(out pending!)) continue;
owner.Dequeued(pending);
}
if (await owner.NextPendingAsync(_linked!.Token).ConfigureAwait(false) is not { } pending) break;
if (!pending.Gate()) { pending.Dispose(); if (pending.IsObsolete()) continue; owner.Stop(); return false; }
if (!pending.IsEligible()) { pending.Dispose(); continue; }
var rate = pending.ConsumeSnapshot();
@@ -1251,14 +1251,7 @@ internal sealed class DataSubscriptionSession : IDataSubscriptionTransport
{
while (true)
{
PendingDelivery pending;
if (Interlocked.Exchange(ref _coalesced, 0) != 0) pending = CoalescedControl();
else
{
if (!await _outbound.Reader.WaitToReadAsync(linked.Token).ConfigureAwait(false)) break;
if (!_outbound.Reader.TryRead(out pending!)) continue;
Dequeued(pending);
}
if (await NextPendingAsync(linked.Token).ConfigureAwait(false) is not { } pending) break;
if (!pending.Gate()) { pending.Dispose(); if (pending.IsObsolete()) continue; Stop(); return; }
IDisposable? reservation;
try { reservation = await _capacity.ReserveAsync(_context.PrincipalId, _limits.MaxPendingBytes, linked.Token).ConfigureAwait(false); }