diff --git a/docs/DataConnections.md b/docs/DataConnections.md index 5d9a76bbf..a69987c28 100644 --- a/docs/DataConnections.md +++ b/docs/DataConnections.md @@ -353,6 +353,26 @@ Follow-ups (#1098): B, 21-48 B unattributed. The harness's hub samples carry a different serving than the items, so its tick still pays the 96 B re-stamp. Where they match, a value reuses the read's sample object outright. +Follow-ups (#1105): + +- **A queued frame is no longer wrapped.** The cohort's event channel carries a `CohortMessage` struct (a control event or a + frame with its charge and receive timestamp) instead of a `FrameEvent` record per frame. The channel's circular segment holds + it, so queueing a frame allocates nothing (queue section 40 B to 0 B per value; one frame per value under a live publish). + Slot release, charge and dispose obligations are unchanged. Release, 10k cohort: about 1.69 KB to 1.65-1.67 KB per value + process-wide. `GrpcDataWave3PerfTests` now asserts the queue section is 0 B, the build section is under 192 B, and (Release) + the process-wide figure is under 1.8 KB. +- **Not done, and why.** Each remaining fixed record either leaves the session or is public contract: + - `ProjectedValueUpdate` + `DataValueSnapshot` (184 B, the build section): both are public records on the publish API + (`IProjectedValuePublisher`), and `DataValueSnapshot` is the driver-wide value type. Folding one into the other, or making + either a struct, breaks that API, and the publisher keeps the update after the call returns. + - The one-element list handed to `TryPublishOrBackpressure` (24 B): reusing a list per cohort would rely on every + `IProjectedValuePublisher` copying it before returning. `ActorProjectedValuePublisher` does, but the interface does not + say so, so that would be an ownership change. + - The origin frame records (`DataSubscriptionValues`, its one-element item list, `DataSubscriptionValue`): public + `Core.Abstractions` contract, and the frame (with its `ResourceLease`) is handed to the reader, which owns and disposes it. + - The hub read (`CachedSourceRead`): public, escapes into `LocalDataServices`' publication lease and `SnapshotLease`, and its + dispose/transfer guards are once-only by object identity. A struct copy would duplicate those guards. + ### Mixed versions (X0 rollout) The remote history and alarm work (`docs/plans/2026-11-23-grpc-data-connections-history-alarms-plan.md`) diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/ProjectedDataAccess.cs b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/ProjectedDataAccess.cs index 5caf9d4dd..f711da556 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/ProjectedDataAccess.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/ProjectedDataAccess.cs @@ -1274,7 +1274,21 @@ public sealed class ProjectedDataAccess : IProjectedDataAccess, IDisposable private sealed record AddEvent(Demand Demand) : Event; private sealed record RemoveEvent(Demand Demand) : Event; private sealed record RefreshEvent(Demand Demand) : Event; - private sealed record FrameEvent(DataSubscriptionFrame Frame, long Bytes, long ReceivedTimestamp) : Event; + /// + /// #1105: what the cohort's channel carries — a control or a stream frame. A frame used to be wrapped + /// in a FrameEvent record (40 B per frame, one frame per value under a live publish); a value in the channel's own + /// (reused, circular) segment carries it with no allocation. The frame's slot, charge and dispose obligations are unchanged. + /// + private readonly struct CohortMessage + { + internal readonly Event? Control; + internal readonly DataSubscriptionFrame? Frame; + internal readonly long Bytes, ReceivedTimestamp; + private CohortMessage(Event control) => Control = control; + internal CohortMessage(DataSubscriptionFrame frame, long bytes, long receivedTimestamp) + { Frame = frame; Bytes = bytes; ReceivedTimestamp = receivedTimestamp; } + public static implicit operator CohortMessage(Event control) => new(control); + } private sealed record FreshnessEvent : Event; private sealed record RenewalEvent : Event; private sealed record RenewedEvent(Demand Demand, Demand.Binding Expected, bool Success) : Event; @@ -1300,7 +1314,7 @@ public sealed class ProjectedDataAccess : IProjectedDataAccess, IDisposable // Value frames keep their backpressure through _frameSlots (and the _frameBytes budget), so a slow consumer still // stalls the pump rather than growing memory. #1098: a FrameSlotWindow (the semaphore's semantics, without its per-wait // allocations: a fast origin keeps the window full, so the pump waits for nearly every frame). - private readonly Channel _events; + private readonly Channel _events; private readonly FrameSlotWindow _frameSlots; private readonly CancellationTokenSource _stop = new(); private readonly Dictionary _registrations = new(); @@ -1342,7 +1356,7 @@ public sealed class ProjectedDataAccess : IProjectedDataAccess, IDisposable internal Cohort(ProjectedDataAccess owner, CohortKey key, string connectionId) { _owner = owner; _key = key; _connectionId = connectionId; - _events = Channel.CreateUnbounded(new UnboundedChannelOptions { SingleReader = true }); + _events = Channel.CreateUnbounded(new UnboundedChannelOptions { SingleReader = true }); var frames = Math.Clamp(owner._limits.MaxDemands * 2, 4, 128); _frameSlots = new FrameSlotWindow(frames); _commandTokens = CommandBurst; _commandTokenStamp = owner.Clock.GetTimestamp(); @@ -1387,7 +1401,7 @@ public sealed class ProjectedDataAccess : IProjectedDataAccess, IDisposable catch { probe?.Exit(ProjectedPublishSection.Pump); Interlocked.Add(ref _frameBytes, -bytes); (frame as IDisposable)?.Dispose(); throw; } probe = _owner.PublishProbeForTests; probe?.Enter(ProjectedPublishSection.Queue); - var queued = _events.Writer.TryWrite(new FrameEvent(frame, bytes, receivedTimestamp)); + var queued = _events.Writer.TryWrite(new CohortMessage(frame, bytes, receivedTimestamp)); probe?.Exit(ProjectedPublishSection.Queue); if (!queued) { _frameSlots.Release(); Interlocked.Add(ref _frameBytes, -bytes); (frame as IDisposable)?.Dispose(); break; } @@ -1876,7 +1890,7 @@ public sealed class ProjectedDataAccess : IProjectedDataAccess, IDisposable // #1098: the probe sees only the synchronous part of each call (no await inside a section). var probe = _owner.PublishProbeForTests; probe?.Enter(ProjectedPublishSection.Dispatch); - if (!_events.Reader.TryRead(out var message)) + if (!_events.Reader.TryRead(out var entry)) { sinceFlush = 0; var flush = FlushAsync(route.Token); @@ -1885,7 +1899,7 @@ public sealed class ProjectedDataAccess : IProjectedDataAccess, IDisposable probe?.Enter(ProjectedPublishSection.Dispatch); var read = _events.Reader.ReadAsync(route.Token); probe?.Exit(ProjectedPublishSection.Dispatch); - message = await read.ConfigureAwait(false); + entry = await read.ConfigureAwait(false); } else if (++sinceFlush >= 64) { @@ -1895,6 +1909,7 @@ public sealed class ProjectedDataAccess : IProjectedDataAccess, IDisposable await flush.ConfigureAwait(false); } else probe?.Exit(ProjectedPublishSection.Dispatch); + var message = entry.Control; if (message is FlushEvent) { Interlocked.Exchange(ref _flushQueued, 0); continue; } if (message is AddEvent add) { @@ -1996,8 +2011,9 @@ public sealed class ProjectedDataAccess : IProjectedDataAccess, IDisposable if (registration.Handle is not null) removeQueue.Add(registration); } else if (message is EndEvent) break; - else if (message is FrameEvent delivered) + else if (entry.Frame is not null) { + var delivered = entry; var frame = delivered.Frame; ProjectedStreamDiagnostics.Count(ProjectedStreamDiagnostics.Event.Frame); try @@ -2204,8 +2220,8 @@ public sealed class ProjectedDataAccess : IProjectedDataAccess, IDisposable route?.Dispose(); while (_events.Reader.TryRead(out var remaining)) { - if (remaining is RenewalEvent) Interlocked.Increment(ref _owner._renewalTicksHandled); - else if (remaining is FrameEvent f) { _frameSlots.Release(); Interlocked.Add(ref _frameBytes, -f.Bytes); (f.Frame as IDisposable)?.Dispose(); } + if (remaining.Control is RenewalEvent) Interlocked.Increment(ref _owner._renewalTicksHandled); + else if (remaining.Frame is { } f) { _frameSlots.Release(); Interlocked.Add(ref _frameBytes, -remaining.Bytes); (f as IDisposable)?.Dispose(); } } lock (_owner._sync) _refreshRequests.Clear(); } diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataWave3PerfTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataWave3PerfTests.cs index 9cd072c0d..dc5ccff5c 100644 --- a/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataWave3PerfTests.cs +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataWave3PerfTests.cs @@ -72,6 +72,15 @@ public sealed class GrpcDataWave3PerfTests(ITestOutputHelper output) perValue.Session.ShouldBeGreaterThan(0, "the tick seam must see the publish"); perValue.Consumer.ShouldBeGreaterThan(0, "the consumer seam must see frames"); fixture.Opens.ShouldBeLessThanOrEqualTo(3); + // #1105 regression bounds (Release, 10k cohort: ~1.65 KB/value process-wide after #1105, ~1.69 KB before). A frame rides + // the cohort channel as a struct, so queueing it allocates nothing; a value's build is the two publish-API records only. + fixture.Allocations.Section(ProjectedPublishSection.Queue).PerValue.ShouldBe(0, "a queued frame must not be wrapped in an object"); + fixture.Allocations.Section(ProjectedPublishSection.Build).PerValue.ShouldBeLessThan(192, + "a value's build is ProjectedValueUpdate + DataValueSnapshot (184 B) only"); +#if !DEBUG + // Debug builds keep unoptimized async state (~2.5 KB/value), so the process-wide bound is a Release figure. + (publishAllocated / (double)Math.Max(1, delivered)).ShouldBeLessThan(1800, "process-wide publish allocation per value regressed"); +#endif } finally { foreach (var demand in demands) demand.Dispose(); } }