fix(grpc-data): A1 review S3 + S4 + nits — snapshot start cursor, readiness cut with the snapshot, failed applies

- S3a: SnapshotConditions reads a start cursor under Lock before enumerating keys; every slice carries it, so a
  condition materialised and activated between the enumeration and slice 1's hold is covered by deltas after it.
- S3b: each hold captures the apply generation (bumped when a rebuild begins) and the complete flag; slices carry
  them and ConditionSnapshotSlice.IsCompleteSnapshot / ConditionStateHub.IsSnapshotCompleteFor detect a rebuild
  that straddles a multi-slice snapshot. An empty scope still takes one hold.
- S3c: maxConditionsPerHold is clamped to MaxConditionsPerHoldLimit (5,000).
- S4: CompleteAddressSpaceApply(bool succeeded) (new default sink member, forwarded by the deferred and SDK sinks);
  the publish actor passes !Degraded. A failed/degraded rebuild keeps readiness false and its pending removals,
  emitting no removal records; the next successful apply completes it.
- Nits: ConditionStateRecord.UnshelveTimeUtc (timed-shelve expiry); ConditionStateHub.Read has a bounded default.
This commit is contained in:
Joseph Doherty
2026-11-24 10:43:04 -05:00
parent 6c94a2ec0e
commit 16166c21e5
10 changed files with 274 additions and 14 deletions
@@ -41,6 +41,9 @@ public sealed class ConditionStateHub : IConditionStateSink
/// <summary>Default per-partition buffer capacity (records).</summary>
public const int DefaultPartitionCapacity = 8192;
/// <summary>The default most records one <see cref="Read"/> returns (review nit: bounded, not <c>int.MaxValue</c>).</summary>
public const int DefaultReadMax = 1024;
private sealed class Partition
{
public readonly object Gate = new();
@@ -82,6 +85,16 @@ public sealed class ConditionStateHub : IConditionStateSink
/// <summary><c>snapshot_complete</c> may be sent: apply complete AND this node is Primary.</summary>
public bool IsSnapshotComplete => _applyComplete && _primary?.ShouldActAsPrimary == true;
/// <summary>
/// <c>snapshot_complete</c> for ONE taken snapshot (review S3b): readiness read atomically with the cut — every
/// slice's hold saw a completed apply of one generation — AND this node is Primary. Prefer this over
/// <see cref="IsSnapshotComplete"/>, which a rebuild can flip between the read and the snapshot.
/// </summary>
/// <param name="slices">The snapshot's slices (<c>OtOpcUaNodeManager.SnapshotConditions</c>).</param>
/// <returns>Whether that snapshot may be sent as complete.</returns>
public bool IsSnapshotCompleteFor(IReadOnlyList<ConditionSnapshotSlice> slices) =>
ConditionSnapshotSlice.IsCompleteSnapshot(slices) && _primary?.ShouldActAsPrimary == true;
/// <inheritdoc />
public long Append(ConditionStateRecord record)
{
@@ -134,8 +147,9 @@ public sealed class ConditionStateHub : IConditionStateSink
/// <param name="afterVersion">The reader's cursor (the slice watermark or the last version it applied).</param>
/// <param name="max">The most records to return.</param>
/// <returns>The read.</returns>
public ConditionDeltaRead Read(int partition, long afterVersion, int max = int.MaxValue)
public ConditionDeltaRead Read(int partition, long afterVersion, int max = DefaultReadMax)
{
ArgumentOutOfRangeException.ThrowIfLessThan(max, 1);
var p = _partitions[partition];
lock (p.Gate)
{
@@ -160,7 +174,7 @@ public sealed class ConditionStateHub : IConditionStateSink
var all = new List<ConditionStateRecord>();
for (var i = 0; i < _partitions.Length; i++)
{
var read = Read(i, afterVersion);
var read = Read(i, afterVersion, DefaultPartitionCapacity);
if (read.ResetRequired) return null;
all.AddRange(read.Records);
}
@@ -1345,7 +1345,9 @@ public sealed class OpcUaPublishActor : ReceiveActor, IWithTimers
/// </summary>
private void FinishRebuild(RebuildAddressSpace msg, IActorRef requester, (bool Degraded, string? Reason) outcome)
{
try { _sink.CompleteAddressSpaceApply(); }
// A1 review S4: a degraded/throwing apply must not complete a rebuild's condition set (no removal records for
// conditions it failed to re-materialise, readiness stays withheld).
try { _sink.CompleteAddressSpaceApply(succeeded: !outcome.Degraded); }
catch (Exception ex)
{
_log.Error(ex, "OpcUaPublish: sink.CompleteAddressSpaceApply threw (correlation={Correlation})", msg.Correlation);