fix(grpc-data): A4 review r2 S1–S4 — the deploy hold reads one (placements, undecided) set, settles on any completion, is bounded (stale + MaxHold, reported as held), and its takeover sends RefreshRequired once

- S1: ProjectedMappingDeploymentRuntime publishes ProjectedMappingSet (placements + undecided + sequence) captured under
  its lock; DeployedConditionMappings keeps it as one volatile value (highest sequence wins); ReconcileRunners reads
  IProjectedConditionMappings.Snapshot. Deterministic window test via a Changed handler registered before the adapter.
- S2: a separate _undecided flag clears on ANY completion (failed/degraded/throwing build ⇒ settled empty ⇒ release);
  browse-anchor Suspended semantics unchanged. A held set goes BadNoCommunication on the stale timeout and is released
  past ProjectedConditionOptions.MaxHold (5 min); diagnostics report "held" and keep its counters; Dispose clears _held.
- S3: a runner that took over a non-empty held set sends RequireRefresh once at its first barrier.
- S4/NITs: runtime-level Began/Completed tests, F1 must-arrive control, linger-expiry-while-held.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Joseph Doherty
2026-11-25 04:04:07 -05:00
parent 5752243724
commit d72f2d966d
5 changed files with 480 additions and 30 deletions
@@ -9,6 +9,13 @@ namespace ZB.MOM.WW.OtOpcUa.Runtime.DataConnections;
public sealed record ProjectedMappingPlacement(RemoteNamespaceMapping Mapping, DataView Parent, DataView Anchor,
NodeScope Scope, IReadOnlyList<string> LocalAncestry, IDataMappedCatalogAuthority Authority);
/// <summary>
/// A4 review r2 S1: one published mapping set — the placements AND whether a catalog attempt is still undecided — as a
/// single immutable value, so a reader never pairs a settled flag with a stale (empty) placement list. <paramref name="Sequence"/>
/// orders sets published by concurrent transitions (a reader keeps the highest).
/// </summary>
public sealed record ProjectedMappingSet(IReadOnlyList<ProjectedMappingPlacement> Placements, bool Suspended, long Sequence);
/// <summary>Owns mapping placements tied to one activated catalog attempt.</summary>
public sealed class ProjectedMappingDeploymentRuntime : IDisposable
{
@@ -17,7 +24,18 @@ public sealed class ProjectedMappingDeploymentRuntime : IDisposable
// the catalog gate across SDK calls or introducing SDK -> transition lock ordering.
internal object TransitionGate { get; } = new();
private bool _suspended;
/// <summary>
/// Browse-anchor retention: set when an attempt begins and cleared only by a SUCCESSFUL activation (an unsuccessful one
/// keeps the old anchors' SDK lifetime so an identical placement can be reauthorized later).
/// </summary>
internal bool Suspended => Volatile.Read(ref _suspended);
/// <summary>
/// A4 review r2 S2: a catalog attempt has begun and not completed — its empty set is "not decided yet". Unlike
/// <see cref="Suspended"/> it clears on ANY completion. Under <see cref="_gate"/>; published in <see cref="ProjectedMappingSet"/>.
/// </summary>
private bool _undecided;
/// <summary>A4 review r2 S1: bumped under <see cref="_gate"/> with every published (placements, suspended) pair.</summary>
private long _setSequence;
private readonly LocalCatalogCoordinator _coordinator;
private readonly RemoteNamespaceResolver _resolver;
private CatalogAttempt? _attempt;
@@ -107,6 +125,23 @@ public sealed class ProjectedMappingDeploymentRuntime : IDisposable
}
}
public event Action<IReadOnlyList<ProjectedMappingPlacement>>? Changed;
/// <summary>
/// A4 review r2 S1: raised after EVERY transition (including a suspend/settle that leaves the placements unchanged)
/// with the placements and the suspended flag captured together under the transition lock.
/// </summary>
public event Action<ProjectedMappingSet>? SetChanged;
/// <summary>A4 review r2 S1: the current published set (placements + suspended, one consistent pair).</summary>
public ProjectedMappingSet Set
{
get
{
lock (_gate)
return new(_active.All(x => x.Authority.IsActive) ? CopyPlacements(_active) : Array.Empty<ProjectedMappingPlacement>(),
_undecided, _setSequence);
}
}
/// <summary>Resolve a mapping only while its original activated authority remains current.</summary>
public bool TryGetConnectionId(string mappingId, out string? connectionId)
{
@@ -187,12 +222,14 @@ public sealed class ProjectedMappingDeploymentRuntime : IDisposable
internal void AttemptBegan(CatalogAttempt attempt)
{
bool changed;
long sequence;
lock (TransitionGate)
lock (_gate)
{
if (_disposed || _attempt is not null && attempt.Epoch <= _attempt.Epoch) return;
_attempt = attempt;
Volatile.Write(ref _suspended, true);
_undecided = true;
Volatile.Write(ref _configuredStamp, new object());
_configuredCapabilities.Clear();
_staged = []; _connections = [];
@@ -201,13 +238,16 @@ public sealed class ProjectedMappingDeploymentRuntime : IDisposable
Volatile.Write(ref _active, Array.Empty<ProjectedMappingPlacement>());
_byMapping.Clear();
_resolver.ApplyMappings([]);
sequence = ++_setSequence;
}
if (changed) Notify([]);
Notify([], changed, suspended: true, sequence);
}
internal void AttemptCompleted(CatalogAttempt attempt, bool completed)
{
IReadOnlyList<ProjectedMappingPlacement> published;
long sequence;
bool undecided;
lock (TransitionGate)
lock (_gate)
{
@@ -241,11 +281,19 @@ public sealed class ProjectedMappingDeploymentRuntime : IDisposable
Volatile.Write(ref _active, Array.Empty<ProjectedMappingPlacement>());
}
}
// The browse anchors stay retained after an unsuccessful attempt (an identical placement can be reauthorized).
Volatile.Write(ref _suspended, !success);
// A4 review r2 S2: for imported conditions, a COMPLETED attempt is settled whatever its outcome. A failed, degraded
// (the coordinator did not activate the catalog) or throwing activation publishes the empty set as the DECIDED
// one — the consumer releases the conditions it held for the deploy instead of holding them, invisible and
// charged, until some later good deploy.
_undecided = false;
_byMapping = _active.ToDictionary(x => x.Mapping.RemoteNamespaceMappingId, StringComparer.OrdinalIgnoreCase);
published = CopyPlacements(_active);
sequence = ++_setSequence;
undecided = _undecided;
}
Notify(published);
Notify(published, changed: true, undecided, sequence);
}
private static ProjectedMappingPlacement[] Build(LocalExportCatalog catalog, IReadOnlyList<RemoteNamespaceMapping> mappings,
@@ -287,29 +335,34 @@ public sealed class ProjectedMappingDeploymentRuntime : IDisposable
return placements.ToArray();
}
private void Notify(IReadOnlyList<ProjectedMappingPlacement> placements)
private void Notify(IReadOnlyList<ProjectedMappingPlacement> placements, bool changed, bool suspended, long sequence)
{
var handlers = Changed;
if (handlers is null) return;
foreach (Action<IReadOnlyList<ProjectedMappingPlacement>> handler in handlers.GetInvocationList())
try { handler(CopyPlacements(placements)); } catch (Exception) { }
if (changed && Changed is { } handlers)
foreach (Action<IReadOnlyList<ProjectedMappingPlacement>> handler in handlers.GetInvocationList())
try { handler(CopyPlacements(placements)); } catch (Exception) { }
if (SetChanged is { } sets)
foreach (Action<ProjectedMappingSet> handler in sets.GetInvocationList())
try { handler(new ProjectedMappingSet(CopyPlacements(placements), suspended, sequence)); } catch (Exception) { }
}
public void Dispose()
{
long sequence;
lock (TransitionGate)
lock (_gate)
{
if (_disposed) return;
_disposed = true;
Volatile.Write(ref _suspended, false);
_undecided = false;
Volatile.Write(ref _configuredStamp, new object());
_configuredCapabilities.Clear();
Volatile.Write(ref _active, Array.Empty<ProjectedMappingPlacement>());
_byMapping.Clear();
_resolver.ApplyMappings([]);
_coordinator.DetachProjectedMappings(this);
sequence = ++_setSequence;
}
Notify([]);
Notify([], changed: true, suspended: false, sequence);
}
}