Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c4b4046832 | |||
| e1feb9fbb6 | |||
| 1bf8c614a9 | |||
| bc80b53e0e | |||
| f478b844a1 | |||
| 1e71a25e3d | |||
| 8828e999c4 | |||
| 4f7a07fbd3 | |||
| da2e441bb9 | |||
| c8e3e279b4 |
@@ -1,21 +1,38 @@
|
||||
namespace ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
|
||||
|
||||
/// <summary>
|
||||
/// Late-binding adapter that holds an inner <see cref="IServiceLevelPublisher"/> reference
|
||||
/// swappable at runtime. Mirrors <see cref="DeferredAddressSpaceSink"/>: Akka actors resolve
|
||||
/// the publisher at DI time, but the production <c>SdkServiceLevelPublisher</c> only exists
|
||||
/// after <c>StandardServer.Start</c>. The Host's hosted service swaps the inner once the SDK
|
||||
/// is up; until then writes route through <see cref="NullServiceLevelPublisher"/>.
|
||||
/// Late-binding adapter that accepts and retains the latest requested ServiceLevel until
|
||||
/// the SDK publisher is available. Binding synchronously replays that request, including zero.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Publishing and binding are serialized through the synchronous inner call. Inner publishers
|
||||
/// must not call back into this adapter. Acceptance while unbound is not an SDK write confirmation.
|
||||
/// A failed call retains both the requested level and the installed publisher for later recovery.
|
||||
/// </remarks>
|
||||
public sealed class DeferredServiceLevelPublisher : IServiceLevelPublisher
|
||||
{
|
||||
private volatile IServiceLevelPublisher _inner = NullServiceLevelPublisher.Instance;
|
||||
private readonly object _gate = new();
|
||||
private IServiceLevelPublisher? _inner;
|
||||
private byte? _latest;
|
||||
|
||||
/// <summary>Swap the underlying publisher. Pass null to revert to the Null no-op.</summary>
|
||||
/// <param name="inner">The publisher implementation to use, or null to use the null publisher.</param>
|
||||
public void SetInner(IServiceLevelPublisher? inner) =>
|
||||
_inner = inner ?? NullServiceLevelPublisher.Instance;
|
||||
/// <summary>Bind and replay the latest request, or unbind while retaining that request.</summary>
|
||||
/// <param name="inner">The publisher implementation to use, or null to unbind.</param>
|
||||
public void SetInner(IServiceLevelPublisher? inner)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
_inner = inner;
|
||||
if (_latest is { } latest) _inner?.Publish(latest);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Publish(byte serviceLevel) => _inner.Publish(serviceLevel);
|
||||
public void Publish(byte serviceLevel)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
_latest = serviceLevel;
|
||||
_inner?.Publish(serviceLevel);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Host.OpcUa;
|
||||
|
||||
/// <summary>Coordinates endpoint cleanup after ServiceLevel binding fails during startup.</summary>
|
||||
internal static class FailedServiceLevelBindingCleanup
|
||||
{
|
||||
/// <summary>Stops and disposes the endpoint, returning whether ownership can be released.</summary>
|
||||
/// <param name="stop">The SDK stop operation.</param>
|
||||
/// <param name="dispose">The application-host disposal operation.</param>
|
||||
/// <param name="logger">The cleanup logger.</param>
|
||||
/// <returns>Whether cleanup completed successfully.</returns>
|
||||
internal static async Task<bool> StopAndDisposeAsync(
|
||||
Func<Task> stop, Func<ValueTask> dispose, ILogger logger)
|
||||
{
|
||||
var completed = true;
|
||||
try
|
||||
{
|
||||
await stop().ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
completed = false;
|
||||
logger.LogError(ex, "OtOpcUaServerHostedService: SDK stop failed after binding failure; retaining endpoint ownership for shutdown retry");
|
||||
}
|
||||
// Attempt application cleanup even when stopping faults; keep ownership on either failure.
|
||||
try
|
||||
{
|
||||
await dispose().ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
completed = false;
|
||||
logger.LogError(ex, "OtOpcUaServerHostedService: endpoint disposal failed after binding failure; retaining endpoint ownership for shutdown retry");
|
||||
}
|
||||
return completed;
|
||||
}
|
||||
}
|
||||
@@ -339,11 +339,36 @@ public sealed class OtOpcUaServerHostedService : IHostedService, IAsyncDisposabl
|
||||
_server.NodeManager.Clock = historyUpdate.Clock;
|
||||
|
||||
// ServiceLevel publisher needs IServerInternal — only available after Start.
|
||||
if (_server.CurrentInstance is { } serverInternal)
|
||||
try
|
||||
{
|
||||
_deferredServiceLevel.SetInner(new SdkServiceLevelPublisher(
|
||||
serverInternal,
|
||||
_loggerFactory.CreateLogger<SdkServiceLevelPublisher>()));
|
||||
if (_server.CurrentInstance is { } serverInternal)
|
||||
{
|
||||
_deferredServiceLevel.SetInner(new SdkServiceLevelPublisher(
|
||||
serverInternal,
|
||||
_loggerFactory.CreateLogger<SdkServiceLevelPublisher>()));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex,
|
||||
"OtOpcUaServerHostedService: ServiceLevel binding failed; stopping the OPC UA endpoint while the admin host continues");
|
||||
try
|
||||
{
|
||||
await StopAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception cleanupError)
|
||||
{
|
||||
_logger.LogError(cleanupError, "OtOpcUaServerHostedService: failed to detach OPC UA bindings after startup failure");
|
||||
}
|
||||
var released = await FailedServiceLevelBindingCleanup.StopAndDisposeAsync(
|
||||
async () => await _server.StopAsync(CancellationToken.None).ConfigureAwait(false),
|
||||
() => _appHost.DisposeAsync(), _logger).ConfigureAwait(false);
|
||||
if (released)
|
||||
{
|
||||
_appHost = null;
|
||||
_server = null;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// arch-review 03/S19 — LAST, after the node manager is known non-null and after the ACL gate is
|
||||
|
||||
@@ -69,8 +69,10 @@ public sealed class OpcUaApplicationHostOptions
|
||||
/// Note this controls <i>admission</i>, not authority. An admitted anonymous session is a
|
||||
/// normal principal (<c>anonymous</c>) whose grants come from <c>NodeAcl</c> rows naming it,
|
||||
/// exactly like a key's principal — there is no anonymous bypass branch in the evaluator.
|
||||
/// Until per-node enforcement lands, an admitted session of ANY kind can still read the whole
|
||||
/// address space, because reads carry no authorization check yet.
|
||||
/// When a wired data-plane authorizer is enabled, per-node ACL checks apply to anonymous
|
||||
/// sessions, including reads. Reads denied by the node manager return <c>BadUserAccessDenied</c>.
|
||||
/// With the authorizer disabled or not wired, reads retain the legacy behaviour without
|
||||
/// per-node ACL enforcement.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public bool AllowAnonymous { get; set; } = true;
|
||||
@@ -558,10 +560,15 @@ public sealed class OpcUaApplicationHost : IAsyncDisposable
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (_impersonateHandler is not null && _server?.CurrentInstance?.SessionManager is { } sessionManager)
|
||||
try
|
||||
{
|
||||
try { sessionManager.ImpersonateUser -= _impersonateHandler; }
|
||||
catch (Exception ex) { _logger.LogWarning(ex, "OpcUaApplicationHost: detaching ImpersonateUser threw"); }
|
||||
// An explicit SDK stop may already have halted CurrentInstance; still continue disposal.
|
||||
if (_impersonateHandler is not null && _server?.CurrentInstance?.SessionManager is { } sessionManager)
|
||||
sessionManager.ImpersonateUser -= _impersonateHandler;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "OpcUaApplicationHost: detaching ImpersonateUser threw");
|
||||
}
|
||||
_impersonateHandler = null;
|
||||
|
||||
|
||||
@@ -38,8 +38,8 @@ public sealed class SdkServiceLevelPublisher : IServiceLevelPublisher
|
||||
var node = _serverInternal.ServerObject?.ServiceLevel;
|
||||
if (node is null)
|
||||
{
|
||||
_logger.LogWarning("SdkServiceLevelPublisher: ServerObject.ServiceLevel unavailable; skipping write");
|
||||
return;
|
||||
_logger.LogWarning("SdkServiceLevelPublisher: ServerObject.ServiceLevel unavailable");
|
||||
throw new InvalidOperationException("Server.ServiceLevel is unavailable.");
|
||||
}
|
||||
|
||||
try
|
||||
@@ -55,6 +55,7 @@ public sealed class SdkServiceLevelPublisher : IServiceLevelPublisher
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "SdkServiceLevelPublisher: write to Server.ServiceLevel threw");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,6 +147,7 @@ public sealed class OpcUaPublishActor : ReceiveActor, IWithTimers
|
||||
private readonly TimeSpan _staleWindow;
|
||||
private readonly TimeSpan _probeFreshnessWindow;
|
||||
private readonly TimeSpan _healthTickInterval;
|
||||
private readonly Func<DateTime> _utcNow;
|
||||
private readonly Akka.Cluster.Cluster _cluster = Akka.Cluster.Cluster.Get(Context.System);
|
||||
private readonly ILoggingAdapter _log = Context.GetLogger();
|
||||
|
||||
@@ -200,9 +201,10 @@ public sealed class OpcUaPublishActor : ReceiveActor, IWithTimers
|
||||
/// <param name="probeFreshnessWindow">The window beyond which a peer's OPC UA probe verdict about
|
||||
/// this node is considered stale (and thus given the benefit of the doubt rather than demoting);
|
||||
/// defaults to 30 seconds.</param>
|
||||
/// <param name="healthTickInterval">The period between self-driven DB-health refresh ticks (each
|
||||
/// Asks <paramref name="dbHealthProbe"/> for its cached status); defaults to 5 seconds. No timer is
|
||||
/// started when <paramref name="dbHealthProbe"/> is null.</param>
|
||||
/// <param name="healthTickInterval">The period between freshness ticks; defaults to 5 seconds.
|
||||
/// DB-backed nodes Ask the probe for cached status; DB-less nodes recompute snapshot freshness.
|
||||
/// Legacy nodes without a probe do not start a timer.</param>
|
||||
/// <param name="utcNow">The UTC clock used for freshness; defaults to <see cref="DateTime.UtcNow"/>.</param>
|
||||
/// <param name="dbLess">Per-cluster mesh Phase 4: this is a driver-only node with no ConfigDb, so the
|
||||
/// ServiceLevel DB-reachability input is retired (<c>DbReachable</c> treated constant-true, only
|
||||
/// staleness demotes). No <paramref name="dbHealthProbe"/> is wired on such a node.</param>
|
||||
@@ -219,7 +221,8 @@ public sealed class OpcUaPublishActor : ReceiveActor, IWithTimers
|
||||
TimeSpan? probeFreshnessWindow = null,
|
||||
TimeSpan? healthTickInterval = null,
|
||||
bool dbLess = false,
|
||||
IDataPlaneAuthoritySink? dataPlaneAuthority = null) =>
|
||||
IDataPlaneAuthoritySink? dataPlaneAuthority = null,
|
||||
Func<DateTime>? utcNow = null) =>
|
||||
Akka.Actor.Props.Create(() => new OpcUaPublishActor(
|
||||
sink ?? NullOpcUaAddressSpaceSink.Instance,
|
||||
serviceLevel ?? NullServiceLevelPublisher.Instance,
|
||||
@@ -232,7 +235,8 @@ public sealed class OpcUaPublishActor : ReceiveActor, IWithTimers
|
||||
probeFreshnessWindow,
|
||||
healthTickInterval,
|
||||
dbLess,
|
||||
dataPlaneAuthority)).WithDispatcher(DispatcherId);
|
||||
dataPlaneAuthority,
|
||||
utcNow)).WithDispatcher(DispatcherId);
|
||||
|
||||
/// <summary>Test-only Props that omits the pinned-dispatcher requirement and skips the
|
||||
/// DPS subscribe so unit tests can spin up the actor on a vanilla TestKit cluster.</summary>
|
||||
@@ -249,9 +253,10 @@ public sealed class OpcUaPublishActor : ReceiveActor, IWithTimers
|
||||
/// <param name="probeFreshnessWindow">The window beyond which a peer's OPC UA probe verdict about
|
||||
/// this node is considered stale (and thus given the benefit of the doubt rather than demoting);
|
||||
/// defaults to 30 seconds.</param>
|
||||
/// <param name="healthTickInterval">The period between self-driven DB-health refresh ticks (each
|
||||
/// Asks <paramref name="dbHealthProbe"/> for its cached status); defaults to 5 seconds. No timer is
|
||||
/// started when <paramref name="dbHealthProbe"/> is null.</param>
|
||||
/// <param name="healthTickInterval">The period between freshness ticks; defaults to 5 seconds.
|
||||
/// DB-backed nodes Ask the probe for cached status; DB-less nodes recompute snapshot freshness.
|
||||
/// Legacy nodes without a probe do not start a timer.</param>
|
||||
/// <param name="utcNow">The UTC clock used for freshness; defaults to <see cref="DateTime.UtcNow"/>.</param>
|
||||
/// <param name="dbLess">Per-cluster mesh Phase 4: this is a driver-only node with no ConfigDb, so the
|
||||
/// ServiceLevel DB-reachability input is retired (<c>DbReachable</c> treated constant-true, only
|
||||
/// staleness demotes). No <paramref name="dbHealthProbe"/> is wired on such a node.</param>
|
||||
@@ -269,7 +274,8 @@ public sealed class OpcUaPublishActor : ReceiveActor, IWithTimers
|
||||
TimeSpan? probeFreshnessWindow = null,
|
||||
TimeSpan? healthTickInterval = null,
|
||||
bool dbLess = false,
|
||||
IDataPlaneAuthoritySink? dataPlaneAuthority = null) =>
|
||||
IDataPlaneAuthoritySink? dataPlaneAuthority = null,
|
||||
Func<DateTime>? utcNow = null) =>
|
||||
Akka.Actor.Props.Create(() => new OpcUaPublishActor(
|
||||
sink ?? NullOpcUaAddressSpaceSink.Instance,
|
||||
serviceLevel ?? NullServiceLevelPublisher.Instance,
|
||||
@@ -282,7 +288,8 @@ public sealed class OpcUaPublishActor : ReceiveActor, IWithTimers
|
||||
probeFreshnessWindow,
|
||||
healthTickInterval,
|
||||
dbLess,
|
||||
dataPlaneAuthority));
|
||||
dataPlaneAuthority,
|
||||
utcNow));
|
||||
|
||||
/// <summary>Initializes a new instance of the <see cref="OpcUaPublishActor"/> class.</summary>
|
||||
/// <param name="sink">The OPC UA address space sink.</param>
|
||||
@@ -298,9 +305,10 @@ public sealed class OpcUaPublishActor : ReceiveActor, IWithTimers
|
||||
/// <param name="probeFreshnessWindow">The window beyond which a peer's OPC UA probe verdict about
|
||||
/// this node is considered stale (and thus given the benefit of the doubt rather than demoting);
|
||||
/// defaults to 30 seconds.</param>
|
||||
/// <param name="healthTickInterval">The period between self-driven DB-health refresh ticks (each
|
||||
/// Asks <paramref name="dbHealthProbe"/> for its cached status); defaults to 5 seconds. No timer is
|
||||
/// started when <paramref name="dbHealthProbe"/> is null.</param>
|
||||
/// <param name="healthTickInterval">The period between freshness ticks; defaults to 5 seconds.
|
||||
/// DB-backed nodes Ask the probe for cached status; DB-less nodes recompute snapshot freshness.
|
||||
/// Legacy nodes without a probe do not start a timer.</param>
|
||||
/// <param name="utcNow">The UTC clock used for freshness; defaults to <see cref="DateTime.UtcNow"/>.</param>
|
||||
/// <param name="dbLess">Per-cluster mesh Phase 4: this is a driver-only node with no ConfigDb, so the
|
||||
/// ServiceLevel DB-reachability input is retired (<c>DbReachable</c> treated constant-true, only
|
||||
/// staleness demotes). No <paramref name="dbHealthProbe"/> is wired on such a node.</param>
|
||||
@@ -316,7 +324,8 @@ public sealed class OpcUaPublishActor : ReceiveActor, IWithTimers
|
||||
TimeSpan? probeFreshnessWindow = null,
|
||||
TimeSpan? healthTickInterval = null,
|
||||
bool dbLess = false,
|
||||
IDataPlaneAuthoritySink? dataPlaneAuthority = null)
|
||||
IDataPlaneAuthoritySink? dataPlaneAuthority = null,
|
||||
Func<DateTime>? utcNow = null)
|
||||
{
|
||||
_sink = sink;
|
||||
_dataPlaneAuthority = dataPlaneAuthority;
|
||||
@@ -330,6 +339,7 @@ public sealed class OpcUaPublishActor : ReceiveActor, IWithTimers
|
||||
_staleWindow = staleWindow ?? TimeSpan.FromSeconds(30);
|
||||
_probeFreshnessWindow = probeFreshnessWindow ?? TimeSpan.FromSeconds(30);
|
||||
_healthTickInterval = healthTickInterval ?? TimeSpan.FromSeconds(5);
|
||||
_utcNow = utcNow ?? (() => DateTime.UtcNow);
|
||||
|
||||
Receive<AttributeValueUpdate>(HandleAttributeUpdate);
|
||||
Receive<AttributeValueBatchUpdate>(HandleAttributeBatchUpdate);
|
||||
@@ -353,11 +363,10 @@ public sealed class OpcUaPublishActor : ReceiveActor, IWithTimers
|
||||
DistributedPubSub.Get(Context.System).Mediator.Tell(new Subscribe(RedundancyStateTopic, Self));
|
||||
}
|
||||
|
||||
// Production delivery of DB-health: when a probe is wired, kick an immediate refresh + start the
|
||||
// periodic tick so _lastDbHealth gets populated and kept fresh without any external pump. Gated
|
||||
// ONLY on the probe being present (independent of the DPS subscribe) so the calculator path works
|
||||
// in tests too. No probe → stay on the legacy role-only seam (no timer).
|
||||
if (_dbHealthProbe is not null)
|
||||
// DB-backed nodes refresh their cached probe status; DB-less nodes still need periodic
|
||||
// snapshot/probe-verdict age checks even when no new messages arrive. Legacy nodes keep
|
||||
// their role-only behavior without a timer.
|
||||
if (_dbLess || _dbHealthProbe is not null)
|
||||
{
|
||||
Self.Tell(HealthTick.Instance); // immediate first refresh
|
||||
Timers.StartPeriodicTimer("db-health", HealthTick.Instance, _healthTickInterval);
|
||||
@@ -970,10 +979,10 @@ public sealed class OpcUaPublishActor : ReceiveActor, IWithTimers
|
||||
// node starting Detached/role-less (first level = 0) would be dedup'd away, leaving the SDK's
|
||||
// built-in default (255 = full service) standing — a degraded node wrongly advertising 255.
|
||||
if (_publishedAtLeastOnce && msg.ServiceLevel == _lastServiceLevel) return;
|
||||
_lastServiceLevel = msg.ServiceLevel;
|
||||
try
|
||||
{
|
||||
_serviceLevel.Publish(msg.ServiceLevel);
|
||||
_lastServiceLevel = msg.ServiceLevel;
|
||||
_publishedAtLeastOnce = true;
|
||||
OtOpcUaTelemetry.ServiceLevelChange.Add(1,
|
||||
new KeyValuePair<string, object?>("level", msg.ServiceLevel));
|
||||
@@ -981,6 +990,8 @@ public sealed class OpcUaPublishActor : ReceiveActor, IWithTimers
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// A failed write may be partially applied; even a previous successful value must retry.
|
||||
_publishedAtLeastOnce = false;
|
||||
_log.Warning(ex, "OpcUaPublish: ServiceLevel publisher threw at level {Level}", msg.ServiceLevel);
|
||||
}
|
||||
}
|
||||
@@ -1004,7 +1015,8 @@ public sealed class OpcUaPublishActor : ReceiveActor, IWithTimers
|
||||
RecomputeServiceLevel();
|
||||
}
|
||||
|
||||
/// <summary>Periodic self-tick: Asks the local <see cref="DbHealthProbeActor"/> for its cached
|
||||
/// <summary>Periodic self-tick: recomputes freshness directly when no DB probe exists; otherwise
|
||||
/// Asks the local <see cref="DbHealthProbeActor"/> for its cached
|
||||
/// status and pipes the reply back to <see cref="Self"/>, where <see cref="HandleDbHealthStatus"/>
|
||||
/// caches it + recomputes. A hung/late probe (Ask timeout) pipes a <c>Reachable=false</c> status so
|
||||
/// the node FAIL-SAFE-DEMOTES rather than freezing the last-known-good. The continuation only
|
||||
@@ -1013,11 +1025,17 @@ public sealed class OpcUaPublishActor : ReceiveActor, IWithTimers
|
||||
/// touched here.</summary>
|
||||
private void OnHealthTick()
|
||||
{
|
||||
_dbHealthProbe!.Ask<DbHealthProbeActor.DbHealthStatus>(
|
||||
if (_dbHealthProbe is null)
|
||||
{
|
||||
RecomputeServiceLevel();
|
||||
return;
|
||||
}
|
||||
|
||||
_dbHealthProbe.Ask<DbHealthProbeActor.DbHealthStatus>(
|
||||
DbHealthProbeActor.GetStatus.Instance, TimeSpan.FromSeconds(1))
|
||||
.ContinueWith(t => t.IsCompletedSuccessfully
|
||||
? t.Result
|
||||
: new DbHealthProbeActor.DbHealthStatus(false, DateTime.UtcNow, "db-health ask timeout"))
|
||||
: new DbHealthProbeActor.DbHealthStatus(false, _utcNow(), "db-health ask timeout"))
|
||||
.PipeTo(Self);
|
||||
}
|
||||
|
||||
@@ -1030,7 +1048,7 @@ public sealed class OpcUaPublishActor : ReceiveActor, IWithTimers
|
||||
{
|
||||
// The result targets the probed node. If it isn't me, it's about a peer — ignore it.
|
||||
if (_localNode is null || r.NodeId != _localNode.Value) return;
|
||||
_probeAboutMe = (r.Ok, DateTime.UtcNow);
|
||||
_probeAboutMe = (r.Ok, _utcNow());
|
||||
RecomputeServiceLevel();
|
||||
}
|
||||
|
||||
@@ -1042,7 +1060,7 @@ public sealed class OpcUaPublishActor : ReceiveActor, IWithTimers
|
||||
private bool OpcUaProbeOk()
|
||||
{
|
||||
if (_probeAboutMe is not { } verdict) return true;
|
||||
if (DateTime.UtcNow - verdict.At > _probeFreshnessWindow) return true;
|
||||
if (_utcNow() - verdict.At > _probeFreshnessWindow) return true;
|
||||
return verdict.Ok;
|
||||
}
|
||||
|
||||
@@ -1079,7 +1097,7 @@ public sealed class OpcUaPublishActor : ReceiveActor, IWithTimers
|
||||
// wins, so a DB-less Detached node stays 0.
|
||||
if (_dbLess)
|
||||
{
|
||||
var utcNow = DateTime.UtcNow;
|
||||
var utcNow = _utcNow();
|
||||
var dbLessInputs = new NodeHealthInputs(
|
||||
MemberState: SafeSelfStatus(),
|
||||
DbReachable: true,
|
||||
@@ -1099,7 +1117,7 @@ public sealed class OpcUaPublishActor : ReceiveActor, IWithTimers
|
||||
return;
|
||||
}
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
var now = _utcNow();
|
||||
var inputs = new NodeHealthInputs(
|
||||
MemberState: SafeSelfStatus(),
|
||||
DbReachable: _lastDbHealth.Reachable,
|
||||
|
||||
+136
-5
@@ -10,10 +10,10 @@ namespace ZB.MOM.WW.OtOpcUa.Commons.Tests.OpcUa;
|
||||
public class DeferredServiceLevelPublisherTests
|
||||
{
|
||||
[Fact]
|
||||
public void Before_SetInner_Publish_is_a_noop()
|
||||
public void Before_SetInner_Publish_accepts_a_cached_request()
|
||||
{
|
||||
var publisher = new DeferredServiceLevelPublisher();
|
||||
// Must not throw; the NullServiceLevelPublisher absorbs it.
|
||||
// Accept the request before an SDK publisher exists.
|
||||
publisher.Publish(200);
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ public class DeferredServiceLevelPublisherTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetInner_null_reverts_to_null_publisher()
|
||||
public void SetInner_null_detaches_the_previous_publisher()
|
||||
{
|
||||
var spy = new SpyPublisher();
|
||||
var publisher = new DeferredServiceLevelPublisher();
|
||||
@@ -39,8 +39,139 @@ public class DeferredServiceLevelPublisherTests
|
||||
|
||||
publisher.Publish(100);
|
||||
|
||||
// The spy should NOT have received the publish after revert.
|
||||
spy.LastPublished.ShouldBe((byte)0, "spy should not have been updated after revert to null");
|
||||
// The previous publisher must not receive an unbound request.
|
||||
spy.LastPublished.ShouldBe((byte)0, "the previous publisher should not be updated after unbinding");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Binding_without_a_request_does_not_invent_a_level()
|
||||
{
|
||||
var received = new List<byte>();
|
||||
new DeferredServiceLevelPublisher().SetInner(new CallbackPublisher(received.Add));
|
||||
received.ShouldBeEmpty();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData((byte)100)]
|
||||
[InlineData((byte)0)]
|
||||
public void Binding_replays_the_latest_request_including_zero(byte latest)
|
||||
{
|
||||
var publisher = new DeferredServiceLevelPublisher();
|
||||
publisher.Publish(240);
|
||||
publisher.Publish(latest);
|
||||
var received = new List<byte>();
|
||||
publisher.SetInner(new CallbackPublisher(received.Add));
|
||||
received.ShouldBe(new[] { latest });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Unbinding_retains_latest_for_rebinding()
|
||||
{
|
||||
var publisher = new DeferredServiceLevelPublisher();
|
||||
var old = new List<byte>();
|
||||
publisher.SetInner(new CallbackPublisher(old.Add));
|
||||
publisher.Publish(240);
|
||||
publisher.SetInner(null);
|
||||
publisher.Publish(0);
|
||||
var replacement = new List<byte>();
|
||||
publisher.SetInner(new CallbackPublisher(replacement.Add));
|
||||
old.ShouldBe(new byte[] { 240 });
|
||||
replacement.ShouldBe(new byte[] { 0 });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Failed_publish_retains_the_requested_value_for_replay()
|
||||
{
|
||||
var publisher = new DeferredServiceLevelPublisher();
|
||||
publisher.SetInner(new CallbackPublisher(_ => throw new InvalidOperationException()));
|
||||
Should.Throw<InvalidOperationException>(() => publisher.Publish(0));
|
||||
var received = new List<byte>();
|
||||
publisher.SetInner(new CallbackPublisher(received.Add));
|
||||
received.ShouldBe(new byte[] { 0 });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Failed_replay_keeps_candidate_bound_for_later_publish()
|
||||
{
|
||||
var publisher = new DeferredServiceLevelPublisher();
|
||||
publisher.Publish(100);
|
||||
var fail = true;
|
||||
var received = new List<byte>();
|
||||
var candidate = new CallbackPublisher(value =>
|
||||
{
|
||||
if (fail) throw new InvalidOperationException();
|
||||
received.Add(value);
|
||||
});
|
||||
Should.Throw<InvalidOperationException>(() => publisher.SetInner(candidate));
|
||||
fail = false;
|
||||
publisher.Publish(0);
|
||||
publisher.SetInner(null);
|
||||
publisher.SetInner(candidate);
|
||||
received.ShouldBe(new byte[] { 0, 0 });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Failed_replay_retains_latest_for_a_replacement_publisher()
|
||||
{
|
||||
var publisher = new DeferredServiceLevelPublisher();
|
||||
publisher.Publish(100);
|
||||
Should.Throw<InvalidOperationException>(() => publisher.SetInner(
|
||||
new CallbackPublisher(_ => throw new InvalidOperationException())));
|
||||
var received = new List<byte>();
|
||||
publisher.SetInner(new CallbackPublisher(received.Add));
|
||||
received.ShouldBe(new byte[] { 100 });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Binding_waits_for_inflight_publish_then_replays_latest()
|
||||
{
|
||||
var publisher = new DeferredServiceLevelPublisher();
|
||||
using var entered = new ManualResetEventSlim();
|
||||
using var release = new ManualResetEventSlim();
|
||||
publisher.SetInner(new CallbackPublisher(_ =>
|
||||
{
|
||||
entered.Set();
|
||||
release.Wait(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken).ShouldBeTrue();
|
||||
}));
|
||||
var publish = Task.Run(() => publisher.Publish(100), TestContext.Current.CancellationToken);
|
||||
var received = new List<byte>();
|
||||
using var bindingStarted = new ManualResetEventSlim();
|
||||
Exception? bindingError = null;
|
||||
var binding = new Thread(() =>
|
||||
{
|
||||
bindingStarted.Set();
|
||||
try { publisher.SetInner(new CallbackPublisher(received.Add)); }
|
||||
catch (Exception ex) { bindingError = ex; }
|
||||
});
|
||||
var bindingWasStarted = false;
|
||||
try
|
||||
{
|
||||
entered.Wait(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken).ShouldBeTrue();
|
||||
binding.Start();
|
||||
bindingWasStarted = true;
|
||||
bindingStarted.Wait(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken).ShouldBeTrue();
|
||||
// Past the marker, SetInner is the thread's only potentially blocking operation.
|
||||
// Observe actual contention rather than assuming Task.Run has entered the adapter.
|
||||
SpinWait.SpinUntil(() => !binding.IsAlive ||
|
||||
(binding.ThreadState & System.Threading.ThreadState.WaitSleepJoin) != 0,
|
||||
TimeSpan.FromSeconds(3)).ShouldBeTrue();
|
||||
(binding.ThreadState & System.Threading.ThreadState.WaitSleepJoin).ShouldNotBe(
|
||||
(System.Threading.ThreadState)0, "binding must wait for the in-flight delivery");
|
||||
}
|
||||
finally
|
||||
{
|
||||
release.Set();
|
||||
if (bindingWasStarted) binding.Join(TimeSpan.FromSeconds(5)).ShouldBeTrue();
|
||||
await publish.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
|
||||
}
|
||||
bindingError.ShouldBeNull();
|
||||
publisher.Publish(0);
|
||||
received.ShouldBe(new byte[] { 100, 0 });
|
||||
}
|
||||
|
||||
private sealed class CallbackPublisher(Action<byte> publish) : IServiceLevelPublisher
|
||||
{
|
||||
public void Publish(byte serviceLevel) => publish(serviceLevel);
|
||||
}
|
||||
|
||||
// ---- test double ----
|
||||
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.OtOpcUa.Host.OpcUa;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Host.Tests.OpcUa;
|
||||
|
||||
public sealed class FailedServiceLevelBindingCleanupTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task Successful_cleanup_observes_stop_before_disposal_and_releases_ownership()
|
||||
{
|
||||
var events = new List<string>();
|
||||
var released = await FailedServiceLevelBindingCleanup.StopAndDisposeAsync(
|
||||
() => { events.Add("stop"); return Task.CompletedTask; },
|
||||
() => { events.Add("dispose"); return ValueTask.CompletedTask; },
|
||||
NullLogger.Instance);
|
||||
events.ShouldBe(new[] { "stop", "dispose" });
|
||||
released.ShouldBeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Failed_stop_still_disposes_and_retains_ownership_for_retry()
|
||||
{
|
||||
var events = new List<string>();
|
||||
var released = await FailedServiceLevelBindingCleanup.StopAndDisposeAsync(
|
||||
() => { events.Add("stop"); throw new InvalidOperationException("controlled stop failure"); },
|
||||
() => { events.Add("dispose"); return ValueTask.CompletedTask; },
|
||||
NullLogger.Instance);
|
||||
events.ShouldBe(new[] { "stop", "dispose" });
|
||||
released.ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Failed_disposal_retains_ownership_without_escaping_the_binding_failure_handler()
|
||||
{
|
||||
var events = new List<string>();
|
||||
var released = await FailedServiceLevelBindingCleanup.StopAndDisposeAsync(
|
||||
() => { events.Add("stop"); return Task.CompletedTask; },
|
||||
() => { events.Add("dispose"); throw new InvalidOperationException("controlled disposal failure"); },
|
||||
NullLogger.Instance);
|
||||
events.ShouldBe(new[] { "stop", "dispose" });
|
||||
released.ShouldBeFalse();
|
||||
}
|
||||
}
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using Akka.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
|
||||
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
||||
using ZB.MOM.WW.OtOpcUa.Core.Authorization;
|
||||
using ZB.MOM.WW.OtOpcUa.Host.OpcUa;
|
||||
using ZB.MOM.WW.OtOpcUa.OpcUaServer;
|
||||
using ZB.MOM.WW.OtOpcUa.OpcUaServer.Security;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Host.Tests.OpcUa;
|
||||
|
||||
public sealed class OtOpcUaServerHostedServiceBindingFailureTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task Binding_failure_closes_started_endpoint_and_allows_other_host_services_to_start()
|
||||
{
|
||||
using var deadline = new CancellationTokenSource(TimeSpan.FromSeconds(15));
|
||||
using var probe = new TcpListener(IPAddress.Loopback, 0);
|
||||
probe.Start();
|
||||
var port = ((IPEndPoint)probe.LocalEndpoint).Port;
|
||||
probe.Stop();
|
||||
var pki = Path.Combine(Path.GetTempPath(), $"otopcua-binding-{Guid.NewGuid():N}");
|
||||
var deferred = new DeferredServiceLevelPublisher();
|
||||
deferred.Publish(0);
|
||||
var bindingReached = false;
|
||||
var listenerObserved = false;
|
||||
OtOpcUaServerHostedService? service = null;
|
||||
var factory = new FaultInjectingLoggerFactory(() =>
|
||||
{
|
||||
using var connected = new TcpClient();
|
||||
connected.Connect(IPAddress.Loopback, port);
|
||||
listenerObserved = connected.Connected;
|
||||
bindingReached = true;
|
||||
throw new InvalidOperationException("controlled binding factory failure");
|
||||
});
|
||||
service = new OtOpcUaServerHostedService(
|
||||
Options.Create(new OpcUaApplicationHostOptions
|
||||
{
|
||||
ApplicationName = "OtOpcUa.BindingFailure",
|
||||
ApplicationUri = $"urn:OtOpcUa.BindingFailure:{Guid.NewGuid():N}",
|
||||
OpcUaPort = port, PublicHostname = "localhost", PkiStoreRoot = pki,
|
||||
}), new DeferredAddressSpaceSink(), deferred,
|
||||
NullOpcUaUserAuthenticator.Instance, NullHistorianDataSource.Instance,
|
||||
EmptyDataPlaneAuthoritySource.Instance, new ConfigurationBuilder().Build(),
|
||||
() => throw new NotSupportedException("no actor system required"), new ActorRegistry(),
|
||||
null, Options.Create(new AclOptions()), TimeProvider.System, factory);
|
||||
var admin = new AdminBootSentinel();
|
||||
using var host = new HostBuilder().ConfigureServices(services =>
|
||||
{
|
||||
services.AddSingleton<IHostedService>(service);
|
||||
services.AddSingleton<IHostedService>(admin);
|
||||
}).Build();
|
||||
try
|
||||
{
|
||||
await host.StartAsync(deadline.Token);
|
||||
listenerObserved.ShouldBeTrue("the SDK endpoint was actually listening before replay");
|
||||
bindingReached.ShouldBeTrue();
|
||||
admin.Started.ShouldBeTrue();
|
||||
using var client = new TcpClient();
|
||||
await Should.ThrowAsync<SocketException>(async () =>
|
||||
await client.ConnectAsync(IPAddress.Loopback, port, deadline.Token));
|
||||
// After construction failure, subsequent requests are accepted into the unbound cache.
|
||||
deferred.Publish(100);
|
||||
await host.StopAsync(deadline.Token);
|
||||
}
|
||||
finally
|
||||
{
|
||||
await service.DisposeAsync();
|
||||
if (Directory.Exists(pki)) Directory.Delete(pki, recursive: true);
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class FaultInjectingLoggerFactory(Action installFault) : ILoggerFactory
|
||||
{
|
||||
public ILogger CreateLogger(string categoryName)
|
||||
{
|
||||
if (categoryName == typeof(SdkServiceLevelPublisher).FullName) installFault();
|
||||
return NullLogger.Instance;
|
||||
}
|
||||
public void AddProvider(ILoggerProvider provider) { }
|
||||
public void Dispose() { }
|
||||
}
|
||||
|
||||
private sealed class AdminBootSentinel : IHostedService
|
||||
{
|
||||
public bool Started { get; private set; }
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
Started = true;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Opc.Ua;
|
||||
using Opc.Ua.Client;
|
||||
using Opc.Ua.Server;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.OpcUaServer.IntegrationTests;
|
||||
|
||||
/// <summary>Local loopback evidence that late binding replaces the SDK default on the wire.</summary>
|
||||
public sealed class ServiceLevelBindingReplayTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData((byte)100)]
|
||||
[InlineData((byte)0)]
|
||||
public async Task Late_binding_replays_latest_request_to_standard_ServiceLevel_read(byte latest)
|
||||
{
|
||||
using var deadline = CancellationTokenSource.CreateLinkedTokenSource(TestContext.Current.CancellationToken);
|
||||
deadline.CancelAfter(TimeSpan.FromSeconds(15));
|
||||
var ct = deadline.Token;
|
||||
var pki = Path.Combine(Path.GetTempPath(), $"otopcua-replay-{Guid.NewGuid():N}");
|
||||
var clientPki = TestClientSecurity.AllocatePkiRoot();
|
||||
var port = 0;
|
||||
try
|
||||
{
|
||||
var deferred = new DeferredServiceLevelPublisher();
|
||||
deferred.Publish(240);
|
||||
deferred.Publish(latest);
|
||||
var started = await OpcUaHostStart.StartAsync(p =>
|
||||
{
|
||||
port = p;
|
||||
return new OpcUaApplicationHost(new OpcUaApplicationHostOptions
|
||||
{
|
||||
ApplicationName = "OtOpcUa.Replay",
|
||||
ApplicationUri = $"urn:OtOpcUa.Replay:{Path.GetFileName(pki)}",
|
||||
OpcUaPort = p, PublicHostname = "localhost", PkiStoreRoot = pki,
|
||||
}, NullLogger<OpcUaApplicationHost>.Instance);
|
||||
}, () => new StandardServer(), ct);
|
||||
await using var host = started.Host;
|
||||
deferred.SetInner(new SdkServiceLevelPublisher(started.Server.CurrentInstance,
|
||||
NullLogger<SdkServiceLevelPublisher>.Instance));
|
||||
var config = new ApplicationConfiguration
|
||||
{
|
||||
ApplicationName = "OtOpcUa.ReplayClient",
|
||||
ApplicationUri = $"urn:OtOpcUa.ReplayClient:{Guid.NewGuid():N}",
|
||||
ApplicationType = ApplicationType.Client,
|
||||
SecurityConfiguration = TestClientSecurity.Build(clientPki),
|
||||
ClientConfiguration = new ClientConfiguration { DefaultSessionTimeout = 10_000 },
|
||||
TransportQuotas = new TransportQuotas { OperationTimeout = 5_000 },
|
||||
};
|
||||
await config.ValidateAsync(ApplicationType.Client, ct);
|
||||
config.CertificateValidator.CertificateValidation += (_, e) => e.Accept = true;
|
||||
var endpoint = await CoreClientUtils.SelectEndpointAsync(config,
|
||||
$"opc.tcp://localhost:{port}/OtOpcUa", false, DefaultTelemetry.Create(_ => { }), ct);
|
||||
using var session = await new DefaultSessionFactory(DefaultTelemetry.Create(_ => { })).CreateAsync(
|
||||
config, new ConfiguredEndpoint(null, endpoint, EndpointConfiguration.Create(config)),
|
||||
updateBeforeConnect: false, checkDomain: false, sessionName: "ServiceLevelReplay",
|
||||
sessionTimeout: 10_000, identity: new UserIdentity(new AnonymousIdentityToken()),
|
||||
preferredLocales: null, ct: ct);
|
||||
var read = await session.ReadValueAsync(new Opc.Ua.NodeId(2267u), ct);
|
||||
read.StatusCode.ShouldBe(StatusCodes.Good);
|
||||
read.Value.ShouldBe(latest);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (Directory.Exists(pki)) Directory.Delete(pki, recursive: true);
|
||||
if (Directory.Exists(clientPki)) Directory.Delete(clientPki, recursive: true);
|
||||
}
|
||||
}
|
||||
}
|
||||
+4
-4
@@ -6,9 +6,9 @@ namespace ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests;
|
||||
|
||||
public sealed class DeferredServiceLevelPublisherTests
|
||||
{
|
||||
/// <summary>Verifies that publish before SetInner is a safe no-op.</summary>
|
||||
/// <summary>Verifies that publish before SetInner safely accepts a cached request.</summary>
|
||||
[Fact]
|
||||
public void Publish_before_SetInner_is_a_safe_noop()
|
||||
public void Publish_before_SetInner_accepts_a_cached_request()
|
||||
{
|
||||
var deferred = new DeferredServiceLevelPublisher();
|
||||
|
||||
@@ -28,9 +28,9 @@ public sealed class DeferredServiceLevelPublisherTests
|
||||
recording.LastValue.ShouldBe((byte)200);
|
||||
}
|
||||
|
||||
/// <summary>Verifies that SetInner with null reverts to the null publisher.</summary>
|
||||
/// <summary>Verifies that SetInner with null detaches the previous publisher.</summary>
|
||||
[Fact]
|
||||
public void SetInner_null_reverts_to_Null_publisher()
|
||||
public void SetInner_null_detaches_the_previous_publisher()
|
||||
{
|
||||
var recording = new RecordingPublisher();
|
||||
var deferred = new DeferredServiceLevelPublisher();
|
||||
|
||||
@@ -19,6 +19,40 @@ public sealed class SdkServiceLevelPublisherTests : IDisposable
|
||||
Path.GetTempPath(),
|
||||
$"otopcua-pki-{Guid.NewGuid():N}");
|
||||
|
||||
[Fact]
|
||||
public void Missing_ServiceLevel_is_reported_as_failure()
|
||||
{
|
||||
var server = System.Reflection.DispatchProxy.Create<IServerInternal, FaultingServer>();
|
||||
var publisher = new SdkServiceLevelPublisher(server, NullLogger<SdkServiceLevelPublisher>.Instance);
|
||||
Should.Throw<InvalidOperationException>(() => publisher.Publish(0));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Sdk_write_failure_propagates_the_original_exception()
|
||||
{
|
||||
var server = System.Reflection.DispatchProxy.Create<IServerInternal, FaultingServer>();
|
||||
var proxy = (FaultingServer)(object)server;
|
||||
proxy.Node = new Opc.Ua.ServerObjectState(null)
|
||||
{
|
||||
ServiceLevel = new Opc.Ua.PropertyState<byte>(null),
|
||||
};
|
||||
proxy.Fault = new InvalidOperationException("controlled diagnostics failure");
|
||||
var publisher = new SdkServiceLevelPublisher(server, NullLogger<SdkServiceLevelPublisher>.Instance);
|
||||
Should.Throw<InvalidOperationException>(() => publisher.Publish(0)).ShouldBeSameAs(proxy.Fault);
|
||||
}
|
||||
|
||||
public class FaultingServer : System.Reflection.DispatchProxy
|
||||
{
|
||||
public Opc.Ua.ServerObjectState? Node { get; set; }
|
||||
public Exception? Fault { get; set; }
|
||||
protected override object? Invoke(System.Reflection.MethodInfo? method, object?[]? args)
|
||||
{
|
||||
if (method?.Name == "get_ServerObject") return Node;
|
||||
if (method?.Name == "get_DiagnosticsLock") throw Fault!;
|
||||
throw new NotSupportedException(method?.Name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Verifies that the publisher writes values to the standard Server.ServiceLevel variable.</summary>
|
||||
[Fact]
|
||||
public async Task Publish_writes_value_to_Server_ServiceLevel_variable()
|
||||
|
||||
+240
@@ -0,0 +1,240 @@
|
||||
using System.Collections.Concurrent;
|
||||
using Akka.Actor;
|
||||
using Akka.TestKit;
|
||||
using Akka.TestKit.Xunit2;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Redundancy;
|
||||
using ZB.MOM.WW.OtOpcUa.Commons.Types;
|
||||
using ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
|
||||
using ZB.MOM.WW.OtOpcUa.OpcUaServer;
|
||||
using ZB.MOM.WW.OtOpcUa.Runtime.OpcUa;
|
||||
using ZB.MOM.WW.OtOpcUa.Runtime.Health;
|
||||
using ZB.MOM.WW.OtOpcUa.Runtime.Tests.Harness;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.OpcUa;
|
||||
|
||||
public sealed class OpcUaPublishActorFreshnessTests : RuntimeActorTestBase
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(false, 240, 200)]
|
||||
[InlineData(true, 250, 210)]
|
||||
public void DbLess_timer_demotes_without_incoming_messages(bool primary, byte fresh, byte stale)
|
||||
{
|
||||
var publisher = new RecordingPublisher();
|
||||
var clock = new ControlledClock();
|
||||
var local = NodeId.Parse("freshness-node");
|
||||
var actor = Sys.ActorOf(OpcUaPublishActor.PropsForTests(
|
||||
serviceLevel: publisher, localNode: local, dbLess: true,
|
||||
staleWindow: TimeSpan.FromSeconds(1), healthTickInterval: TimeSpan.FromMilliseconds(25),
|
||||
utcNow: clock.Read));
|
||||
actor.Tell(Snapshot(local, primary, clock.Now));
|
||||
AwaitAssert(() => publisher.Levels.ShouldBe(new[] { fresh }), PresenceBudget);
|
||||
// Equality remains fresh. Observe several real ticks with a fixed clock; no actor Tell.
|
||||
clock.Advance(TimeSpan.FromSeconds(1));
|
||||
var reads = clock.Reads;
|
||||
AwaitCondition(() => clock.Reads >= reads + 4, PresenceBudget);
|
||||
// Drain earlier self-enqueued publications without triggering a health recomputation.
|
||||
actor.Tell(new Identify("freshness-barrier"), TestActor);
|
||||
ExpectMsg<ActorIdentity>().Subject.ShouldBe(actor);
|
||||
publisher.Levels.ShouldBe(new[] { fresh });
|
||||
clock.Advance(TimeSpan.FromTicks(1));
|
||||
// No further Tell: only the production timer can notice age crossing the window.
|
||||
AwaitAssert(() => publisher.Levels.ShouldBe(new[] { fresh, stale }), PresenceBudget);
|
||||
// A new fresh snapshot restores service, after which ticks continue to age that snapshot.
|
||||
actor.Tell(Snapshot(local, primary, clock.Now));
|
||||
AwaitAssert(() => publisher.Levels.ShouldBe(new[] { fresh, stale, fresh }), PresenceBudget);
|
||||
clock.Advance(TimeSpan.FromSeconds(1) + TimeSpan.FromTicks(1));
|
||||
AwaitAssert(() => publisher.Levels.ShouldBe(new[] { fresh, stale, fresh, stale }), PresenceBudget);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DbLess_timer_expires_negative_peer_verdict_only_after_exact_boundary()
|
||||
{
|
||||
var publisher = new RecordingPublisher();
|
||||
var clock = new ControlledClock();
|
||||
var local = NodeId.Parse("peer-verdict-node");
|
||||
var actor = Sys.ActorOf(OpcUaPublishActor.PropsForTests(
|
||||
serviceLevel: publisher, localNode: local, dbLess: true,
|
||||
staleWindow: TimeSpan.FromSeconds(10), probeFreshnessWindow: TimeSpan.FromSeconds(2),
|
||||
healthTickInterval: TimeSpan.FromMilliseconds(25), utcNow: clock.Read));
|
||||
actor.Tell(Snapshot(local, false, clock.Now));
|
||||
AwaitAssert(() => publisher.Levels.ShouldBe(new byte[] { 240 }), PresenceBudget);
|
||||
actor.Tell(new PeerOpcUaProbeActor.OpcUaProbeResult(local, Ok: false));
|
||||
AwaitAssert(() => publisher.Levels.ShouldBe(new byte[] { 240, 0 }), PresenceBudget);
|
||||
clock.Advance(TimeSpan.FromSeconds(2));
|
||||
var reads = clock.Reads;
|
||||
AwaitCondition(() => clock.Reads >= reads + 4, PresenceBudget);
|
||||
// Drain earlier self-enqueued publications without triggering a health recomputation.
|
||||
actor.Tell(new Identify("freshness-barrier"), TestActor);
|
||||
ExpectMsg<ActorIdentity>().Subject.ShouldBe(actor);
|
||||
publisher.Levels.ShouldBe(new byte[] { 240, 0 });
|
||||
clock.Advance(TimeSpan.FromTicks(1));
|
||||
AwaitAssert(() => publisher.Levels.ShouldBe(new byte[] { 240, 0, 240 }), PresenceBudget);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Stopping_DbLess_actor_cancels_freshness_timer_and_publications()
|
||||
{
|
||||
var publisher = new RecordingPublisher();
|
||||
var clock = new ControlledClock();
|
||||
var local = NodeId.Parse("stopped-node");
|
||||
var actor = Sys.ActorOf(OpcUaPublishActor.PropsForTests(
|
||||
serviceLevel: publisher, localNode: local, dbLess: true,
|
||||
staleWindow: TimeSpan.FromSeconds(1), healthTickInterval: TimeSpan.FromMilliseconds(25),
|
||||
utcNow: clock.Read));
|
||||
actor.Tell(Snapshot(local, false, clock.Now));
|
||||
AwaitAssert(() => publisher.Levels.ShouldBe(new byte[] { 240 }), PresenceBudget);
|
||||
var reads = clock.Reads;
|
||||
AwaitCondition(() => clock.Reads >= reads + 4, PresenceBudget);
|
||||
Watch(actor);
|
||||
Sys.Stop(actor);
|
||||
ExpectTerminated(actor);
|
||||
reads = clock.Reads;
|
||||
clock.Advance(TimeSpan.FromSeconds(2));
|
||||
ExpectNoMsg(TimeSpan.FromMilliseconds(150)); // Six timer periods after confirmed termination.
|
||||
clock.Reads.ShouldBe(reads);
|
||||
publisher.Levels.ShouldBe(new byte[] { 240 });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DbBacked_timer_refreshes_probe_status_periodically()
|
||||
{
|
||||
var publisher = new RecordingPublisher();
|
||||
var clock = new ControlledClock();
|
||||
var local = NodeId.Parse("db-backed-periodic-node");
|
||||
var db = CreateTestProbe();
|
||||
var actor = Sys.ActorOf(OpcUaPublishActor.PropsForTests(
|
||||
serviceLevel: publisher, localNode: local, dbHealthProbe: db.Ref,
|
||||
healthTickInterval: TimeSpan.FromMilliseconds(100), utcNow: clock.Read));
|
||||
actor.Tell(Snapshot(local, false, clock.Now));
|
||||
db.ExpectMsg<DbHealthProbeActor.GetStatus>();
|
||||
db.Reply(new DbHealthProbeActor.DbHealthStatus(true, clock.Now, null));
|
||||
AwaitAssert(() => publisher.Levels.ShouldContain((byte)240), PresenceBudget);
|
||||
// A second request comes from the periodic timer, not the immediate startup tick.
|
||||
db.ExpectMsg<DbHealthProbeActor.GetStatus>();
|
||||
db.Reply(new DbHealthProbeActor.DbHealthStatus(false, clock.Now, "unreachable"));
|
||||
AwaitAssert(() => publisher.Levels.Last().ShouldBe((byte)100), PresenceBudget);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DbBacked_timer_ask_timeout_demotes_last_known_healthy_node()
|
||||
{
|
||||
var publisher = new RecordingPublisher();
|
||||
var clock = new ControlledClock();
|
||||
var local = NodeId.Parse("db-backed-timeout-node");
|
||||
var db = CreateTestProbe();
|
||||
var actor = Sys.ActorOf(OpcUaPublishActor.PropsForTests(
|
||||
serviceLevel: publisher, localNode: local, dbHealthProbe: db.Ref,
|
||||
healthTickInterval: TimeSpan.FromMilliseconds(100), utcNow: clock.Read));
|
||||
actor.Tell(Snapshot(local, true, clock.Now));
|
||||
db.ExpectMsg<DbHealthProbeActor.GetStatus>();
|
||||
db.Reply(new DbHealthProbeActor.DbHealthStatus(true, clock.Now, null));
|
||||
AwaitAssert(() => publisher.Levels.ShouldContain((byte)250), PresenceBudget);
|
||||
// Leave subsequent real Ask requests unanswered: the timeout status must reach the actor.
|
||||
db.ExpectMsg<DbHealthProbeActor.GetStatus>();
|
||||
AwaitAssert(() => publisher.Levels.Last().ShouldBe((byte)110), PresenceBudget);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Missing_local_snapshot_entry_retries_zero_on_timer_and_recovers()
|
||||
{
|
||||
var publisher = new RetryZeroPublisher();
|
||||
var clock = new ControlledClock();
|
||||
var local = NodeId.Parse("missing-entry-node");
|
||||
var actor = Sys.ActorOf(OpcUaPublishActor.PropsForTests(
|
||||
serviceLevel: publisher, localNode: local, dbLess: true,
|
||||
healthTickInterval: TimeSpan.FromMilliseconds(25), utcNow: clock.Read));
|
||||
actor.Tell(Snapshot(local, false, clock.Now));
|
||||
AwaitAssert(() => publisher.Levels.ShouldBe(new byte[] { 240 }), PresenceBudget);
|
||||
|
||||
// Only this snapshot Tell requests the first zero. Fail it and the next attempt so
|
||||
// subsequent zero attempts must come from the periodic timer, exercising the null-entry
|
||||
// branch repeatedly rather than mistaking the Detached branch for missing-node coverage.
|
||||
actor.Tell(Snapshot(NodeId.Parse("other-node"), false, clock.Now));
|
||||
AwaitAssert(() => publisher.Levels.ShouldBe(new byte[] { 240, 0 }), PresenceBudget);
|
||||
publisher.ZeroAttempts.ShouldBeGreaterThanOrEqualTo(3);
|
||||
|
||||
actor.Tell(Snapshot(local, false, clock.Now));
|
||||
AwaitAssert(() => publisher.Levels.ShouldBe(new byte[] { 240, 0, 240 }), PresenceBudget);
|
||||
clock.Advance(TimeSpan.FromSeconds(30) + TimeSpan.FromTicks(1));
|
||||
AwaitAssert(() => publisher.Levels.ShouldBe(new byte[] { 240, 0, 240, 200 }), PresenceBudget);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void No_snapshot_startup_does_not_publish_across_virtual_timer_periods()
|
||||
{
|
||||
// This actor requires the Cluster extension, but the absent/missing-entry guards do
|
||||
// not inspect membership. Do not self-join: the ordinary harness waits for Up in its
|
||||
// constructor, which cannot progress with a frozen scheduler.
|
||||
using var local = new TestKit(AkkaTestHocon + @"
|
||||
akka.scheduler.implementation = ""Akka.TestKit.TestScheduler, Akka.TestKit""");
|
||||
var scheduler = (TestScheduler)local.Sys.Scheduler;
|
||||
var publisher = new RetryZeroPublisher();
|
||||
var clock = new ControlledClock();
|
||||
var actor = local.ActorOfAsTestActorRef<OpcUaPublishActor>(OpcUaPublishActor.PropsForTests(
|
||||
serviceLevel: publisher, localNode: NodeId.Parse("no-snapshot-node"), dbLess: true,
|
||||
healthTickInterval: TimeSpan.FromSeconds(1), utcNow: clock.Read));
|
||||
|
||||
// TestActorRef uses the calling-thread dispatcher: Advance completes delivery before
|
||||
// these assertions, including the immediate startup tick and three periodic ticks.
|
||||
for (var period = 0; period < 3; period++)
|
||||
scheduler.Advance(TimeSpan.FromSeconds(1));
|
||||
publisher.Levels.ShouldBeEmpty();
|
||||
publisher.ZeroAttempts.ShouldBe(0);
|
||||
clock.Reads.ShouldBe(0);
|
||||
|
||||
// Positive control: the same registered timer must subsequently retry a failed zero.
|
||||
// No whole snapshot at startup means no publication; a received snapshot missing the
|
||||
// local entry means zero. Keep these two policies explicitly distinct.
|
||||
actor.Tell(Snapshot(NodeId.Parse("other-node"), false, clock.Now));
|
||||
publisher.ZeroAttempts.ShouldBe(1);
|
||||
scheduler.Advance(TimeSpan.FromSeconds(1));
|
||||
publisher.ZeroAttempts.ShouldBe(2);
|
||||
publisher.Levels.ShouldBeEmpty();
|
||||
scheduler.Advance(TimeSpan.FromSeconds(1));
|
||||
publisher.ZeroAttempts.ShouldBe(3);
|
||||
publisher.Levels.ShouldBe(new byte[] { 0 });
|
||||
clock.Reads.ShouldBe(0);
|
||||
}
|
||||
|
||||
private sealed class RetryZeroPublisher : IServiceLevelPublisher
|
||||
{
|
||||
private readonly ConcurrentQueue<byte> _levels = new();
|
||||
private int _zeroAttempts;
|
||||
public byte[] Levels => _levels.ToArray();
|
||||
public int ZeroAttempts => Volatile.Read(ref _zeroAttempts);
|
||||
public void Publish(byte serviceLevel)
|
||||
{
|
||||
if (serviceLevel == 0 && Interlocked.Increment(ref _zeroAttempts) <= 2)
|
||||
throw new InvalidOperationException("test zero-publication failure");
|
||||
_levels.Enqueue(serviceLevel);
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class ControlledClock
|
||||
{
|
||||
private long _ticks = new DateTime(2026, 1, 1, 0, 0, 0, DateTimeKind.Utc).Ticks;
|
||||
private int _reads;
|
||||
public DateTime Now => new(Interlocked.Read(ref _ticks), DateTimeKind.Utc);
|
||||
public int Reads => Volatile.Read(ref _reads);
|
||||
public DateTime Read()
|
||||
{
|
||||
Interlocked.Increment(ref _reads);
|
||||
return Now;
|
||||
}
|
||||
public void Advance(TimeSpan age) => Interlocked.Add(ref _ticks, age.Ticks);
|
||||
}
|
||||
|
||||
private static RedundancyStateChanged Snapshot(NodeId local, bool primary, DateTime asOf) =>
|
||||
new(new[] { new NodeRedundancyState(local,
|
||||
primary ? RedundancyRole.Primary : RedundancyRole.Secondary,
|
||||
IsClusterLeader: primary, IsDriverPrimary: primary, asOf) }, CorrelationId.NewId());
|
||||
|
||||
private sealed class RecordingPublisher : IServiceLevelPublisher
|
||||
{
|
||||
private readonly ConcurrentQueue<byte> _levels = new();
|
||||
public byte[] Levels => _levels.ToArray();
|
||||
public void Publish(byte serviceLevel) => _levels.Enqueue(serviceLevel);
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,49 @@ namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.OpcUa;
|
||||
|
||||
public sealed class OpcUaPublishActorTests : RuntimeActorTestBase
|
||||
{
|
||||
[Fact]
|
||||
public void Failed_value_is_retried_and_last_level_only_tracks_success()
|
||||
{
|
||||
var sink = new RecoveringPublisher { Fail = false };
|
||||
var actor = ActorOfAsTestActorRef<OpcUaPublishActor>(OpcUaPublishActor.PropsForTests(serviceLevel: sink));
|
||||
actor.Tell(new OpcUaPublishActor.ServiceLevelChanged(100));
|
||||
sink.Fail = true;
|
||||
actor.Tell(new OpcUaPublishActor.ServiceLevelChanged(0));
|
||||
actor.UnderlyingActor.LastServiceLevel.ShouldBe((byte)100);
|
||||
sink.Fail = false;
|
||||
actor.Tell(new OpcUaPublishActor.ServiceLevelChanged(0));
|
||||
sink.Delivered.ShouldBe(new byte[] { 100, 0 });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Requesting_prior_success_after_failure_updates_delivery_and_replay()
|
||||
{
|
||||
var sink = new RecoveringPublisher();
|
||||
var deferred = new DeferredServiceLevelPublisher();
|
||||
deferred.SetInner(sink);
|
||||
var actor = ActorOfAsTestActorRef<OpcUaPublishActor>(OpcUaPublishActor.PropsForTests(serviceLevel: deferred));
|
||||
actor.Tell(new OpcUaPublishActor.ServiceLevelChanged(100));
|
||||
sink.Fail = true;
|
||||
actor.Tell(new OpcUaPublishActor.ServiceLevelChanged(0));
|
||||
sink.Fail = false;
|
||||
actor.Tell(new OpcUaPublishActor.ServiceLevelChanged(100));
|
||||
sink.Delivered.ShouldBe(new byte[] { 100, 100 });
|
||||
var rebound = new RecoveringPublisher();
|
||||
deferred.SetInner(rebound);
|
||||
rebound.Delivered.ShouldBe(new byte[] { 100 });
|
||||
}
|
||||
|
||||
private sealed class RecoveringPublisher : IServiceLevelPublisher
|
||||
{
|
||||
public bool Fail { get; set; }
|
||||
public List<byte> Delivered { get; } = new();
|
||||
public void Publish(byte level)
|
||||
{
|
||||
if (Fail) throw new InvalidOperationException("controlled failure");
|
||||
Delivered.Add(level);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Verifies that message contracts are accepted without a pinned dispatcher in tests.</summary>
|
||||
[Fact]
|
||||
public void Accepts_message_contracts_without_pinned_dispatcher_in_tests()
|
||||
|
||||
Reference in New Issue
Block a user