fix(grpc-data): size projected unary reservations from the node's shape (#960); per-item status for projected items refused at create (#961)

#960: a projected read reserves 16 KiB for a fixed-size scalar (16 KiB + 256 B/element for a
fixed-length fixed-size array) instead of the whole MaxPendingBytes, so the default 1 MiB admits
64 concurrent scalar reads; String/Bytes/variable arrays keep the whole cap, and a waiting
whole-cap read holds back smaller ones for 100 ms (anti-starvation). Writes reserve
16 KiB + identifiers + 3 x the value bound (what the origin's DataWriteExecutor admits the batch
against). Monitored-item renewals reserve 256 KiB. Measured: 4 x 250 ms maxAge=0 reads 1016 ms /
1 in flight -> 285 ms / 4 in flight.

#961: TryAcquireProjectedItem reports BadResourceUnavailable for a stream-capacity refusal; the
node manager's CreateMonitoredItem override turns the item manager's ServiceResultException into
that item's status (was a whole-request fault), and an authority race is re-classified outside
Lock like the per-item probe.
This commit is contained in:
Joseph Doherty
2026-12-01 20:34:35 -05:00
parent 92da56c071
commit 75e6ff9af8
14 changed files with 644 additions and 48 deletions
@@ -0,0 +1,88 @@
# #960 / #961 — projected unary reservation sizing, per-item create refusals
2026-12-01. Follow-ups of #938 and #940 (`938-939-940-948.md`). Consumer side of gRPC data connections.
## #960 — one projected origin read/write in flight per node
**Root cause.** `ProjectedDataAccess` admitted every projected read (`ScheduleRead` / `ReadNowAsync`) and write by reserving
`min(caller cap = 4 MiB, MaxUnaryBytes)` against `MaxUnaryBytes`, which production wires to `DataConnections:MaxPendingBytes`
(default **1 MiB**). Every operation reserved the whole budget, so one read or write excluded all others and
`MaxConcurrentCalls` (64) never applied. A `maxAge=0` batch of N nodes was N serial origin round trips; a concurrent write was
rejected outright (`WriteAsync` does not wait). An existing monitored item's metadata renewal (`RenewOwnedAsync`) also reserved
the whole budget, so it needed every other projected operation idle.
**Fix.** Size each reservation from what the operation can carry, keeping the sum bounded by `MaxPendingBytes`:
- **Read** — `ReadReservationBytes(originMetadata, cap)`: 16 KiB (`ProjectedEnvelopeBytes`) for a fixed-size scalar (Boolean,
integers, Float/Double, Timestamp, Guid, Null); 16 KiB + 256 B/element for a fixed-length array of fixed-size elements; the
whole cap for String / Bytes / variable-length or string arrays (no declared bound — unchanged). The reservation is also the
read's budget, i.e. the consumer's decode limit and the origin's response limit, so an origin answering more than the node's
shape allows is refused rather than exceeding the bound. `GrpcDataProjectedReadReservationTests` pins the constants against
the real wire codec (widest value of each bounded shape, 128-char serving identifiers, decoded at exactly the reservation).
- **Anti-starvation.** A whole-cap read refused for bytes by a caller that waits (`ReadNowAsync`, the #938 pump) holds back
smaller reads for 100 ms (`ReadDrainWindow`, re-armed by each refused retry, cleared when it is admitted), so scalar reads
that now share the budget cannot starve it. Fire-and-forget `ScheduleRead` does not arm it; writes neither arm nor honour it.
- **Write** — `16 KiB + 2 × identifier chars + 3 × WriteValueBytes(value)` (capped). The `3 ×` term is required: the reservation
is the batch budget the ORIGIN admits the write against (`DataWriteExecutor`: `3 × EstimatedRetainedBytes + 512 + 2 ×
identifiers`), and `WriteValueBytes` (the former `FitsWriteBudget` bound) is ≥ that estimate for every kind. A first draft
without it would have refused strings over ~4 K chars at the origin (`A_large_string_write_reserves_what_the_origin_admits_it_against`).
- **Monitored-item renewal** — 256 KiB (`LapseRenewalBytes`, the same metadata call the on-demand lapse renewal makes).
The default `MaxPendingBytes` is unchanged: 1 MiB now holds 64 scalar reads (= `MaxConcurrentCalls`). Variable-size reads still
serialize at the default; raising `MaxPendingBytes` buys concurrency for those (docs/Configuration.md).
**Measurement** (`A_max_age_zero_batch_of_scalar_nodes_reads_the_origin_concurrently_within_the_default_budget`: real consumer
SDK session, 4 projected Int32 nodes, `maxAge=0`, 1 MiB budget, origin reads delayed 250 ms):
| | elapsed | max origin reads in flight |
|---|---|---|
| pre-fix | 1016 ms | 1 |
| fix | 285 ms | 4 (the harness's operation limit) |
Not measured on the rig with ScadaBridge-sized batches (no rig run in this change).
## #961 — a projected item refused inside the item-manager create faulted the whole request
**Root cause.** `OrdinaryTagItemManager.CreateMonitoredItem` threw `ServiceResultException(BadUserAccessDenied)` when
`TryAcquireProjectedItem` failed. SDK 2.0.1 `CustomNodeManager2.CreateMonitoredItems` calls the protected `CreateMonitoredItem`
per item under `Lock` with no catch, so the throw became a service fault for every item in the request. Since #940 this was only
the remainder after the per-item probe: the authority changing between probe and create, and `StartSubscription` refusing
(`MaxStreams` cohorts — one per connection × serving identity × publishing interval — all in use, or a closing cohort).
**Fix.**
- `TryAcquireProjectedItem` reports why: `BadResourceUnavailable` when the authority admitted the caller but `StartSubscription`
refused (the demand still valid), else `BadUserAccessDenied` (provisional).
- `OtOpcUaNodeManager` overrides the protected `CreateMonitoredItem`: a `ServiceResultException` becomes THIS item's status.
Nothing leaks — the item manager releases what it reserved before throwing, and the base deletes an item it already created
before rethrowing.
- After `base.CreateMonitoredItems` (outside `Lock`) every projected item the probe admitted but the create answered
`BadUserAccessDenied` is re-classified exactly like the probe (`ProjectedSubscribeRefusal`): granted lapse ⇒
`BadResourceUnavailable` (its renewal starts), real refusal ⇒ `BadUserAccessDenied`, admitted again by now ⇒
`BadResourceUnavailable` (a transient race; the retry succeeds).
## Red / green (Runtime.Tests)
| Test (`Trait Issue`) | pre-fix | fix |
|---|---|---|
| 960 `Scalar_reads_reserve_their_shape_bound_so_concurrent_reads_share_the_default_budget` | **FAIL** | pass |
| 960 `A_waiting_whole_cap_read_holds_back_smaller_reads_until_it_is_admitted` | **FAIL** | pass |
| 960 `Scalar_writes_are_dispatched_concurrently_within_the_default_budget` | **FAIL** | pass |
| 960 `A_max_age_zero_batch_of_scalar_nodes_reads_the_origin_concurrently_within_the_default_budget` (wire) | **FAIL** (1 in flight) | pass |
| 960 `Cold_sdk_read_budget…` / `Sdk_write_budget…` (assert the sized budget now) | **FAIL** | pass |
| 960 `Variable_size_read_reserves_the_full_configured_budget_until_physical_completion` (control) | pass | pass |
| 960 `A_large_string_write_reserves_what_the_origin_admits_it_against` (guards the 3 × term) | pass | pass |
| 960 `GrpcDataProjectedReadReservationTests` (13, codec pins) | n/a (new API) | pass |
| 961 `A_projected_item_refused_for_stream_capacity_at_create_fails_only_that_item` (wire) | **FAIL** (request fault `BadUserAccessDenied`) | pass |
| 961 `A_projected_item_whose_lease_lapses_between_the_probe_and_the_create_is_retryable_for_that_item_only` (wire) | **FAIL** (request fault) | pass |
Adjusted for #960 (they encoded "one operation holds the whole budget"): `Cancelled_clamped_write_keeps_capacity…` (30 KiB budget
= one scalar write), `Full_write_quota_rejects_large_array…` (the held write is now ~2 MiB so the second does not fit beside it),
`A_max_age_zero_batch_larger_than_the_unary_budget_is_pumped…` (budget = one scalar reservation; asserts 1 in flight),
`Sdk_mapped_write_uses_default_one_mib_budget_and_is_dispatched_once` (asserts the sized write budget, < 64 KiB and above the
origin's scalar accounting, instead of the whole 1 MiB).
Suites: Runtime.Tests `FullyQualifiedName~DataConnections` 1005/1005 (after the last adjustment), OpcUaServer.Tests 1460 passed,
2 skipped, 0 failed.
Not live-gated (no rig run in this change).