fix(grpc-data): #994 projected cohorts batch, pace and scale past one item; busy origins no longer reset quiet streams

Wave 3 of the 2026-12-02 review (docs/reviews/2026-12-02/01-runtime.md R-2/3/7/8/9/10/12/13/21/22/23/24,
02-opcuaserver.md U-10/11/12/13).

Consumer (ProjectedDataAccess cohort):
- R-2: a cohort's OpenSubscription declared a snapshot budget of 1 item, which the origin uses to size the stream's
  handle table, so the 2nd item was refused LimitExceeded and the cohort re-opened in a loop (two projected items of
  one interval broke each other). Opens/adds/removes are now batched (IDataSubscriptionCommandLimits, <=256 items,
  bounded by estimated snapshot bytes) and paced (32 burst, 32/s, <=4 commands or snapshots outstanding).
- R-7: control events no longer share the bounded frame queue; a failed Remove can no longer Stop() the cohort.
- R-10: a local capacity refusal (TryPublishOrBackpressure -> CapacityExhausted) is retried for up to 2 s instead of
  invalidating healthy evidence and retiring the route.
- R-13: RemoteSubscription mutates its handle/version tables in place; the cohort caches the source per binding and
  drops the per-sample LINQ in EvidenceFor.
- R-8: only an unselectable endpoint arms the discovery backoff; a caller cancel is OperationCanceledException.
- R-24: the condition stream cancels and observes a pending frame read before disposing the enumerator.

Origin (DataSubscriptionSession / SamplePartition / SourceInterestLease):
- R-2 (found by the perf harness): the delivery index was sized MaxPendingFrames x 16 = 1024, so the 1025th item of a
  stream stopped the session after its ack; it now covers MaxMonitoredItems.
- R-3: interest reads count only own-ticket matches (scan budget separate), re-queue a lagging tick, and catch up from
  per-source last-change versions when the bounded history rolled off - no more "Source change history expired".
- R-9: a partition quiet bump resets a lease only when one of ITS sources was invalidated (per-source quiet version)
  or its binding retired; partition-wide invalidation still resets all.
- R-12: the per-item publish verdict is memoised per (catalog instance, authority generation) for <=1 s; Commit
  under the session lock re-checks identity only.
- R-21/R-22/R-23: registry prune is interval/capacity driven; a cleared history session refuses chunks; the snapshot
  barrier is decided and armed under the same gate its release is decided under.

Node manager: U-10 projected live items indexed by node; U-11 anchor removal / root-leaf swap tombstone the anchor's
own monitored node; U-12 per-parent child count; U-13 per-condition verdict index.

Also: Issue934_r1_N1 test clears its metadata failure before ending the stream (test-only sequencing race,
reproduced with a 200 ms delay; production behaviour correct).

Perf (Category=Perf, OTOPCUA_GRPCDATA_PERF=1): 10k-item cohort opens in 3.3 s / 80 commands / 1 stream (before: never
opens); 100 source versions at 1k/s -> 11,036 item values in 569 ms.
This commit is contained in:
Joseph Doherty
2026-12-03 11:59:26 -05:00
parent b9955f6946
commit 5cccfd75ce
30 changed files with 1697 additions and 189 deletions
+47
View File
@@ -209,6 +209,53 @@ being committed, the replacement could send that newer value as a live delta bef
snapshot that followed then carried the older captured value. Now the change waits until the item's snapshot is
complete, and the item then delivers it as a live delta.
### Scaling the projected subscription path (#994)
These behaviours changed in #994 (review 2026-12-02 Wave 3). They matter when a consumer holds many projected monitored
items or an origin is busy.
- **A cohort holds more than one item.** A cohort is the consumer's one stream per connection, serving identity and
publishing interval. Its `OpenSubscription` used to declare a snapshot budget of 1 item. The origin sizes the stream's
handle table from that budget, so it refused the second item `LimitExceeded`. The consumer then dropped and re-opened
the stream in a loop. In practice two projected items with the same interval broke each other. The open now declares
the consumer's `MaxMonitoredItems`.
- **Opens, adds and removes are batched.** A cohort used to send one command per item. The origin admits 128 commands per
second per principal and stops the session past that, so any cohort of more than about 128 items never came back after
a reconnect. Commands now carry up to the peer's batch limit (`IDataSubscriptionCommandLimits`, at most 256 items,
bounded by the estimated snapshot bytes). Each cohort also paces itself (32 commands burst, 32 per second, at most 4
awaiting acknowledgement). A 10k-item cohort opens in about 80 commands, including the freshness challenges.
- **One removal never tears down its siblings.** Control events (add, remove, refresh, renewal) no longer share the bounded
frame queue. When that queue was full, a failed `Remove` stopped the cohort and disposed every demand on it. Frames keep
their backpressure through a separate slot count.
- **Local publish capacity is backpressure.** A sample the local SDK sink refuses because its pending bytes are full is
retried for up to 2 s. Before, this invalidated healthy evidence, retired the route and forced a re-snapshot. After the
2 s bound the old fail-closed behaviour applies (`IProjectedValuePublisher.TryPublishOrBackpressure`).
- **A per-call failure no longer backs off the connection.** Only an endpoint that could not be *selected* arms the
discovery backoff. A cancelled or failed unary call (Read, GetMetadata, Browse) after a successful selection fails that
call alone. A caller's cancel now surfaces as `OperationCanceledException`. Before, any such failure refused every other
subscribe, write and read on the connection for at least 250 ms ("Data endpoint discovery is backing off").
- **Origin: a busy node no longer resets quiet streams.** A session used to scan at most 32 partition changes per tick,
counting other sources' changes too. It also reset when the 1024-entry change history rolled past its cursor. On a node
publishing thousands of changes per second, a stream with a few slow items reset again and again with "Source change
history expired.". Now unrelated changes do not consume the budget, and a lagging stream re-queues its tick. A cursor that
fell off the history catches up from each own source's last-change version, so it does not reset.
- **Origin: one source's invalidation resets only its own streams.** A sibling source's invalidation, including plain lock
contention while publishing, used to reset every stream on the partition. A partition-wide invalidation still resets
them all.
- **Origin: the publish authorization is memoised.** The publish check runs on every delivered frame. It is now memoised
for the exact catalog instance and authority generation, for at most 1 s. Any deployment or authority publish misses the
memo, so revoking a grant or key is still immediate. The commit under the session lock re-checks only identity.
- **Smaller fixes.** The write-operation registry no longer copies its table on every claim. A history chunk saved into a
session cleared mid-request is refused, so its bytes are not charged until restart. A `Modify` deferred behind a snapshot
can no longer stall until its acknowledgement timeout. The condition stream's teardown no longer hides the real failure
behind `NotSupportedException`.
- **Consumer node manager.** A projected publish looks up only that node's monitored items. It used to snapshot and
type-test every live item on the server, per value, under `Lock`. Eviction at the projected-node cap is linear (a
per-parent child count). Removing a projected anchor, or swapping it for a root leaf, now also tombstones the anchor's
own monitored node. Removing an imported condition purges only its own cached verdicts.
Perf harness: `GrpcDataWave3PerfTests` (`Category=Perf`, opt-in `OTOPCUA_GRPCDATA_PERF=1`).
### Mixed versions (X0 rollout)
The remote history and alarm work (`docs/plans/2026-11-23-grpc-data-connections-history-alarms-plan.md`)