diff --git a/docs/DataConnections.md b/docs/DataConnections.md index 1dc5e0961..ab5796b27 100644 --- a/docs/DataConnections.md +++ b/docs/DataConnections.md @@ -303,6 +303,40 @@ connection, node or principal id in a label: Before H4 the origin's `otopcua.dataconn.history.reads` sat on a private meter the host did not export; it is now on the OtOpcUa meter and carries `side`. +## Condition streams (A2a, origin side) + +An origin streams its alarm condition state to a peer that negotiated `conditions` (`x-otopcua-data-features`). Plan: +`docs/plans/2026-11-23-grpc-data-connections-history-alarms-plan.md` §5.1/§5.5; evidence `docs/plans/evidence/grpc-data-ext/A2.md`. + +- **Served only when** `DataConnections:ConditionKeySecret` is provisioned (≥ 32 UTF-8 bytes, identical on both pair + members) and the node manager is up. Otherwise `CapabilitiesResponse.conditions` (field 17) and the `AlarmSubscribe` bit + (64) are absent and condition items are `Unsupported`. +- **Grant.** `AlarmSubscribe` on the export (and the connection/mapping, authored like the other bits), plus the origin + ACL `Subscribe` (`CreateMonitoredItems`) on each placement's scope for the connection's service principal. A stream with + only `AlarmSubscribe` (no `Subscribe`) is admitted when the call negotiated `conditions`. +- **The item.** `SubscriptionItem.condition_scope` on the mapping's remote root (an export root) = every condition placed + under that root in the root's realm (Raw: native, at the RawPath; UNS: native at the reserved reference id and scripted + at the ScriptedAlarmId; a UNS listing never carries a RawPath). One handle; it counts as one monitored item. Primary only + (`Unavailable` on a Secondary or Warm standby). +- **Frames** (`condition_states`, arm 9, never on a call without the header): + - snapshot begin: `is_snapshot`, `total`, no records. `total` above the export's `MaxConditions` ⇒ that is the only + frame for the item (nothing is copied); a later deploy that changes the listing re-admits it; + - record chunks (≤ 128 records, `info` on every snapshot record and on a condition's first delta); + - the last snapshot chunk carries `snapshot_complete` only when every node-manager hold saw a completed apply of one + generation and the node is Primary; otherwise the snapshot is retaken (at most once a second) when the origin is ready; + - then delta records in order; a condition added or removed by a deploy arrives as a delta record (`removed` for one + that left the listing). +- **Priority and overflow.** A control or ack at the head of the stream goes first, then condition frames, then queued + values. Condition production is pull-based: a slow consumer leaves records in the bounded condition-state hub; if the hub + evicts records the item has not read, the item takes a fresh snapshot, and if that happens while its previous snapshot + is still undelivered the stream is `Reset` ("Condition stream overflow; retry later."). +- **Revocation** (grant, catalog or authority change) ends the stream like a revoked v1 value item; no further condition + frame is delivered. +- **Limits** (`AddDataConnectionConditions`): `DataExport.MaxConditions` (1000, ≤ 5000), `RemoteNamespaceMapping.MaxConditions` + and the three condition timeouts (consumer side, A3). Metrics: `otopcua.dataconn.conditions.items`, `…resyncs`, + `…capacity_rejections`. +- Condition methods (`CallConditionMethod`) are stage A2b; the OPC UA projection of imported conditions is A3. + ## Feature boundary Alarm/event forwarding and relay operations are future work; remote history is above. Authorized imported diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/ConditionItemStream.cs b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/ConditionItemStream.cs index 03bc12fb8..b81039c55 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/ConditionItemStream.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/ConditionItemStream.cs @@ -39,6 +39,9 @@ internal sealed class ConditionItemStream /// The most records one chunk carries. internal const int MaxRecordsPerChunk = 128; + /// The most estimated bytes one chunk carries (the transport reserves ≤ the session's pending bytes per frame). + internal const long MaxChunkBytes = 256 * 1024; + /// The least time between two snapshots of one item (re-snapshot storms after an overflow). internal static readonly TimeSpan MinResnapshotInterval = TimeSpan.FromSeconds(1); @@ -196,8 +199,7 @@ internal sealed class ConditionItemStream { _state = State.Live; return new ConditionChunk(true, _total, [], _snapshotComplete, Snapshots, EndsSnapshot: true); } return new ConditionChunk(true, _total, [], false); } - var batch = new List(Math.Min(MaxRecordsPerChunk, _snapshotRecords.Count)); - while (batch.Count < MaxRecordsPerChunk && _snapshotRecords.TryDequeue(out var record)) batch.Add(record); + var batch = Take(_snapshotRecords); var last = _snapshotRecords.Count == 0; if (last) _state = State.Live; return new ConditionChunk(true, null, batch, last && _snapshotComplete, Snapshots, EndsSnapshot: last); @@ -240,11 +242,31 @@ internal sealed class ConditionItemStream private ConditionChunk? NextDeltaChunk() { if (_pendingDeltas.Count == 0) return null; - var batch = new List(Math.Min(MaxRecordsPerChunk, _pendingDeltas.Count)); - while (batch.Count < MaxRecordsPerChunk && _pendingDeltas.TryDequeue(out var record)) batch.Add(record); - return new ConditionChunk(false, null, batch, false); + return new ConditionChunk(false, null, Take(_pendingDeltas), false); } + /// At most records and estimated bytes (at least one). + private static List Take(Queue queue) + { + var batch = new List(Math.Min(MaxRecordsPerChunk, queue.Count)); + long bytes = 0; + while (batch.Count < MaxRecordsPerChunk && queue.TryPeek(out var next)) + { + var size = EstimatedBytes(next); + if (batch.Count > 0 && bytes + size > MaxChunkBytes) break; + batch.Add(queue.Dequeue()); + bytes += size; + } + return batch; + } + + /// The transport's per-record reservation (the host's GrpcDeliverySink charges the same shape). + internal static long EstimatedBytes(DataConditionState r) => 2048L + 4L * (r.ConditionKey.Length + r.Message.Length + + (r.Comment?.Length ?? 0) + (r.ClientUserId?.Length ?? 0) + (r.Activation?.Incarnation.Length ?? 0) + + (r.Info is { } info ? info.ConditionType.Length + info.ConditionName.Length + + info.Placements.Sum(p => 256 + p.Subject.Length + p.Address.Identifier.Length + p.Address.NamespaceUri.Length + + p.Address.OriginClusterId.Length) : 0)); + /// A new catalog: conditions leaving the listing get a removal record; joining ones a small snapshot as deltas. private void ApplyScopeChange(ConditionItemScope scope) {