fix(runtime): derive every write bound and Ask from one write timeout (lmxopcua#777)
v2-ci / unit-tests (push) Has been cancelled

Three write bounds were configured independently: the inbound-write
gateway's 10 s Ask, DriverHostActor's fixed 8 s Ask of the child, and the
child's own write timeout (5 s + up to 2 s grace) with a fixed 8 s queue
budget. An outer Ask that fires first reports the write failed and the node
reverts while the child is still driving the device, so any write timeout
above ~6 s (or a write dequeued late in its 8 s budget) let the device take
a value the client was told failed.

DriverWriteBounds (Runtime) derives them all from DriverWrites:Timeout:
  token cancel   = Timeout
  abandon        = Timeout + min(Timeout/2, 2 s)            (RunBound)
  queue budget   = RunBound
  child replies  <= queue budget + RunBound                  (ReplyBound)
  host Ask       = ReplyBound + 1 s
  gateway Ask    = host Ask + 1 s
Default Timeout is 10 s (the Tier C Write timeout, so no tier default is
capped below its own policy); host Ask 25 s, gateway Ask 26 s. Tier A/B
writes are still cancelled first by their 2 s / 4 s tier timeouts.

- DriverHostActor takes writeBounds + writeClock, spawns children with them,
  and runs its Ask deadline as a token on the write clock (Infinite Akka
  timeout), so host and child bounds share one clock.
- WriteAttribute gains IssuedAt (write-clock timestamp, stamped by the
  host): the queue budget counts mailbox time too, and a write past it is
  failed BadTimeout and never started - including on arrival. Stale queued
  writes are expired across the whole queue, not only up to a free slot.
- ActorNodeWriteGateway's default Ask = Default.GatewayAsk; the hosted
  service passes the configured one.
- Startup validation (Host DriverWriteBoundsBinding, same warn-and-clamp
  style as ScriptingOptionsBinding): unparsable / non-positive -> default,
  above 5 min -> clamped, and each tier whose default Write timeout exceeds
  the write timeout is named in a Warning (its policy would never take
  effect). The derived bounds are logged at Information. Per-instance
  ResilienceConfig Write overrides arrive with a deployment, so
  DriverCapabilityInvokerFactory warns at spawn (writeTimeoutCeiling).
- appsettings.json documents DriverWrites:Timeout.

Tests (manual clock):
- DriverHostActorWriteAskTests: a held write under a 9 s timeout survives
  an 8.5 s step and succeeds; a token-ignoring write is answered by the
  child's BadTimeout at 9 s + grace, not by the Ask; the Infinite-timeout
  Ask ends exactly at its clock deadline.
- DriverInstanceActorConcurrentWriteTests: a write issued longer ago than
  the queue budget is failed and never reaches the driver; positive control
  at exactly the budget is written.
- DriverWriteBoundsTests: every outer bound covers the inner ones for 0.5 s
  to 300 s timeouts.
- DriverWriteBoundsBindingTests: default fits every tier; 3 s timeout is
  caught at startup with Tier B and C named (not A); bad values fall back
  with a warning; over-max clamps.
- DriverCapabilityInvokerFactoryTests: spawn warns when a Write override
  exceeds the ceiling; no warning within it or without a ceiling.
Mutation checks: a fixed 8 s host Ask on the same clock fails both 9 s host
tests; stamping arrival time instead of IssuedAt fails the issued-at test;
dropping the tier check / factory check fails their tests.

Runtime.Tests 961 passed / 3 skipped at AKKA_TEST_TIMEFACTOR 3 and 1;
Host.Tests 232/232 at 3 and 1; Core.Tests 361 passed / 1 skipped.
Solution Release build: 0 warnings.

ContinuousHistorizationRecorderTypedValueTests (historian) deliberately
left for the historian lane.

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
Joseph Doherty
2026-11-09 15:46:04 -05:00
parent b051bdc87c
commit 97dc715dac
16 changed files with 824 additions and 57 deletions
@@ -0,0 +1,101 @@
using System.Globalization;
using Microsoft.Extensions.Configuration;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
using ZB.MOM.WW.OtOpcUa.Core.Resilience;
using ZB.MOM.WW.OtOpcUa.Runtime.Drivers;
namespace ZB.MOM.WW.OtOpcUa.Host.Configuration;
/// <summary>
/// Binds <c>DriverWrites:Timeout</c> into <see cref="DriverWriteBounds"/> and checks it against the resilience
/// tiers' Write timeouts (lmxopcua#777). One place, so <c>Program.cs</c> and its unit test bind the same way.
/// </summary>
/// <remarks>
/// Matches <see cref="ScriptingOptionsBinding"/>: a bad value is clamped to a safe one and reported as a startup
/// Warning, never a boot failure. No setting here can make a write outlive an Ask — every Ask is derived from the
/// timeout — so an inconsistency only means a configured bound does not take effect, which is worth saying loudly
/// but not worth an outage.
/// </remarks>
public static class DriverWriteBoundsBinding
{
/// <summary>The configuration key of the write timeout.</summary>
public const string TimeoutKey = DriverWriteBounds.SectionName + ":Timeout";
/// <summary>Binds, clamps and validates the write timeout.</summary>
/// <param name="configuration">The host configuration.</param>
/// <param name="warnings">One line per clamped value or tier inconsistency, for the caller to log as a Warning.</param>
/// <returns>The write bounds; <see cref="DriverWriteBounds.Default"/> when the key is absent.</returns>
public static DriverWriteBounds Bind(IConfiguration configuration, out IReadOnlyList<string> warnings)
{
ArgumentNullException.ThrowIfNull(configuration);
var found = new List<string>();
var timeout = DriverWriteBounds.DefaultTimeout;
var raw = configuration[TimeoutKey];
if (!string.IsNullOrWhiteSpace(raw))
{
if (!TimeSpan.TryParse(raw, CultureInfo.InvariantCulture, out var parsed))
{
found.Add($"{TimeoutKey} '{raw}' is not a TimeSpan (e.g. 00:00:10); using the default {Format(timeout)}.");
}
else if (parsed <= TimeSpan.Zero)
{
found.Add($"{TimeoutKey} {parsed} is not positive; using the default {Format(timeout)}.");
}
else if (parsed > DriverWriteBounds.MaxTimeout)
{
timeout = DriverWriteBounds.MaxTimeout;
found.Add($"{TimeoutKey} {parsed} exceeds the max {Format(timeout)}; clamped.");
}
else
{
timeout = parsed;
}
}
var bounds = new DriverWriteBounds(timeout);
found.AddRange(CheckTierDefaults(bounds));
warnings = found;
return bounds;
}
/// <summary>
/// Reports every resilience tier whose DEFAULT Write timeout exceeds <paramref name="bounds"/>' timeout. The
/// actor cancels a write at that timeout, so such a tier's Write policy never takes effect: its drivers'
/// writes are cut off sooner than their tier says. (Per-instance <c>ResilienceConfig</c> overrides arrive
/// with a deployment, not at startup; <see cref="DriverCapabilityInvokerFactory"/> checks those at spawn.)
/// </summary>
/// <param name="bounds">The write bounds in force.</param>
/// <returns>One warning per inconsistent tier; empty when every tier fits.</returns>
public static IReadOnlyList<string> CheckTierDefaults(DriverWriteBounds bounds)
{
ArgumentNullException.ThrowIfNull(bounds);
var found = new List<string>();
foreach (var tier in Enum.GetValues<DriverTier>())
{
var write = DriverResilienceOptions.GetTierDefaults(tier)[DriverCapability.Write];
if (TimeSpan.FromSeconds(write.TimeoutSeconds) > bounds.Timeout)
{
found.Add(
$"Tier {tier} drivers' Write timeout ({write.TimeoutSeconds}s) exceeds {TimeoutKey} " +
$"({Format(bounds.Timeout)}): their writes are cancelled at {Format(bounds.Timeout)}, so the tier " +
$"policy never takes effect. Raise {TimeoutKey} to at least {write.TimeoutSeconds}s.");
}
}
return found;
}
/// <summary>One line naming every derived bound, for the startup log.</summary>
/// <param name="bounds">The write bounds in force.</param>
/// <returns>The description.</returns>
public static string Describe(DriverWriteBounds bounds)
{
ArgumentNullException.ThrowIfNull(bounds);
return $"write timeout {Format(bounds.Timeout)}, token-ignoring writes abandoned at {Format(bounds.RunBound)}, " +
$"queue budget {Format(bounds.QueueBudget)}, driver-host Ask {Format(bounds.HostAsk)}, " +
$"inbound-write gateway Ask {Format(bounds.GatewayAsk)}";
}
private static string Format(TimeSpan value) =>
value.TotalSeconds.ToString("0.###", CultureInfo.InvariantCulture) + "s";
}