feat(mqtt): Sparkplug primary-host STATE publishing, Primary-gated (#511)
`ActAsPrimaryHost` was accepted by config and by the AdminUI form and did nothing but
log a warning. It now delivers the whole three-part contract or none of it: a retained
`ONLINE` on `spBv1.0/STATE/{hostId}`, a matching retained `OFFLINE` armed as the MQTT
Last Will, and an explicit `OFFLINE` on a clean stand-down.
**The claim is a dedicated connection, not a message.** A Will can only be registered in
the CONNECT packet — it cannot be added to a live session, changed, or withdrawn. So a
claim that is acquired and released at runtime cannot ride the long-lived ingest session.
`SparkplugPrimaryHost` opens its own connection (client id `{clientId}-spbhost`, forced
clean-session) when the node gains Primary and closes it when the node loses Primary; the
connection's lifetime *is* the claim, so every leg follows from its lifecycle. Ingest is
untouched by a role change — no dropped subscriptions, no lost birth cache, no data gap at
the Secondary->Primary transition.
**Primary-gated** (owner decision, recorded on the issue). Both pair nodes subscribe
independently, so an ungated claim means both claim the same host id — and the sharp
failure is not the two fighting over the retained STATE, it is that the SECONDARY's death
fires its Will and publishes OFFLINE while the Primary is healthy, stopping every edge node
in the group. Registering the Will only on whichever node is Primary at connect time fails
the other way: a node that later gains Primary would publish ONLINE with no Will behind it.
New seam: `IDriverPrimaryRoleSource` (`Core.Abstractions`) carrying the *data-plane* verdict
(`PrimaryGatePolicy.ShouldServiceAsPrimary`), not the alarm-drain verdict — an unknown role
with a driver peer present must deny. Registered unconditionally by `AddOtOpcUaRuntime`,
deliberately NOT alongside `IRedundancyRoleView`, which `AddAlarmHistorian` registers only
when that section is enabled: piggybacking would leave a pair with no alarm historian
falling back to "claim unconditionally". Polled on a 1s timer rather than evented — a
process singleton raising events at per-instance drivers outlives every driver it holds.
Accepted trade: a brief edge-visible OFFLINE window during failover. That is a genuine
state transition, not a glitch to hide.
Live-gated 5/5 against the Mosquitto fixture, including the Will leg — `AbandonForTests`
drops the socket with no DISCONNECT, because a clean disconnect makes the broker discard
the Will by protocol rule, so a test driven through `StopAsync` would observe the
stand-down OFFLINE and pass against an implementation that never armed a Will. Falsified:
removing the Will turns that leg and only that leg red.
Also corrects two stale claims found en route: the AdminUI form still said MQTT tags are
read-only (#508 shipped), and the driver doc still said nothing consumes `IRediscoverable`.
Claude-Session: https://claude.ai/code/session_015p7wGqy3YpZNCpDzTpGMKo
This commit is contained in:
@@ -190,6 +190,12 @@ public sealed record MqttSparkplugOptions
|
||||
/// When <c>true</c>, this driver instance acts as the Sparkplug Primary Host
|
||||
/// Application. At most one primary host may exist per host-id per broker.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Implemented by <c>SparkplugPrimaryHost</c> (Gitea #511) on a dedicated broker connection
|
||||
/// whose lifetime <i>is</i> the claim, and gated on the redundancy Primary role so the two nodes
|
||||
/// of a pair never both claim. Requires <see cref="HostId"/>; set without one, the driver warns
|
||||
/// and declines rather than publishing to a malformed topic.
|
||||
/// </remarks>
|
||||
public bool ActAsPrimaryHost { get; init; } = false;
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
namespace ZB.MOM.WW.OtOpcUa.Driver.Mqtt;
|
||||
|
||||
/// <summary>
|
||||
/// An MQTT Last Will and Testament, as the client options factory wants it (Gitea #511).
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// <b>A Will can only be registered at CONNECT, and that single fact drives the whole design
|
||||
/// of the Sparkplug primary-host claim.</b> It rides in the CONNECT packet, so it cannot be
|
||||
/// added, changed or withdrawn on a live session. A node that gains the Primary role after
|
||||
/// connecting therefore cannot simply start publishing <c>ONLINE</c> — it would have no Will,
|
||||
/// and a crash would leave a retained <c>ONLINE</c> asserting forever that a dead host is
|
||||
/// alive. Equally, a node that registers a Will and later loses the role cannot withdraw it,
|
||||
/// so its eventual disconnect would publish <c>OFFLINE</c> over the new Primary's live claim
|
||||
/// and stop every edge node in the group.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// The consequence is that <b>the claim must be bound to the lifetime of a connection</b>:
|
||||
/// <c>SparkplugPrimaryHost</c> opens a dedicated connection when the node gains Primary and
|
||||
/// closes it when the node loses Primary, rather than trying to mutate the ingest session it
|
||||
/// shares with the rest of the driver.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// <b>Clean session matters too.</b> A broker that resumes an existing session for the same
|
||||
/// client id may discard the Will registered by the new CONNECT. The primary-host connection
|
||||
/// forces clean-session on for that reason, regardless of the driver's configured value.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <param name="Topic">The topic the broker publishes the Will to.</param>
|
||||
/// <param name="Payload">The Will body.</param>
|
||||
/// <param name="Qos">Requested QoS, 0–2.</param>
|
||||
/// <param name="Retain">
|
||||
/// Whether the broker retains the Will. For a Sparkplug host death certificate this is
|
||||
/// <see langword="true"/>: an edge node that connects <i>after</i> the host died must still learn
|
||||
/// the host is offline, and only a retained message reaches it.
|
||||
/// </param>
|
||||
public sealed record MqttLastWill(string Topic, byte[] Payload, int Qos, bool Retain);
|
||||
+141
@@ -0,0 +1,141 @@
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Driver.Mqtt.Sparkplug;
|
||||
|
||||
/// <summary>
|
||||
/// Builds the Sparkplug v3.0 primary-host <c>STATE</c> bodies — the retained <c>ONLINE</c> birth
|
||||
/// and the matching <c>OFFLINE</c> death that is registered as the MQTT Last Will (Gitea #511).
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// <b>Encode only.</b> The receive side lives in <c>SparkplugIngestor.TryReadStateOnline</c>,
|
||||
/// which additionally tolerates the pre-3.0 bare <c>ONLINE</c>/<c>OFFLINE</c> text form. This
|
||||
/// type only ever <i>produces</i> the v3.0 JSON shape, matching
|
||||
/// <see cref="SparkplugTopic.FormatState"/>'s refusal to produce the legacy topic.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// <b>The two bodies share one timestamp, and that is a spec requirement rather than a
|
||||
/// convenience.</b> Sparkplug 3.0 pairs a host's birth and death certificates by their
|
||||
/// <c>timestamp</c> field: an edge node that receives an <c>OFFLINE</c> whose timestamp is
|
||||
/// older than the <c>ONLINE</c> it already holds is entitled to ignore it as a stale death
|
||||
/// from a previous session. That rule is what makes the mechanism safe across a reconnect —
|
||||
/// and it means the death body has to be built <i>before</i> CONNECT (a Will is registered in
|
||||
/// the CONNECT packet) while the birth is published <i>after</i> CONNACK. Deriving each from
|
||||
/// its own <c>UtcNow</c> would give them different timestamps and quietly break the pairing,
|
||||
/// so <see cref="ForSession"/> mints both at once and there is no single-body overload to
|
||||
/// reach for by accident.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public static class SparkplugStatePayload
|
||||
{
|
||||
/// <summary>
|
||||
/// QoS for both STATE bodies. Sparkplug 3.0 requires QoS 1 for the host birth and death —
|
||||
/// unlike the QoS 0 that carries NBIRTH/DBIRTH/DATA. A host claim delivered at most once is a
|
||||
/// claim that may simply never arrive.
|
||||
/// </summary>
|
||||
public const int StateQos = 1;
|
||||
|
||||
/// <summary>
|
||||
/// Mints the matched <c>ONLINE</c>/<c>OFFLINE</c> pair for one host session, both carrying
|
||||
/// <paramref name="timestampMs"/>.
|
||||
/// </summary>
|
||||
/// <param name="hostId">The Sparkplug host id; used to build the topic.</param>
|
||||
/// <param name="timestampMs">
|
||||
/// Sparkplug epoch milliseconds for this session. Supplied by the caller rather than read from
|
||||
/// the clock here so the value is visible at the call site and testable.
|
||||
/// </param>
|
||||
/// <returns>The topic plus both bodies.</returns>
|
||||
/// <exception cref="ArgumentException"><paramref name="hostId"/> is blank.</exception>
|
||||
public static SparkplugStateSession ForSession(string hostId, long timestampMs)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(hostId);
|
||||
|
||||
return new SparkplugStateSession(
|
||||
Topic: SparkplugTopic.FormatState(hostId),
|
||||
TimestampMs: timestampMs,
|
||||
OnlineBody: Encode(online: true, timestampMs),
|
||||
OfflineBody: Encode(online: false, timestampMs));
|
||||
}
|
||||
|
||||
/// <summary>Encodes one STATE body: <c>{"online":<bool>,"timestamp":<ms>}</c>.</summary>
|
||||
/// <param name="online">The online flag.</param>
|
||||
/// <param name="timestampMs">Sparkplug epoch milliseconds.</param>
|
||||
/// <returns>The UTF-8 body bytes.</returns>
|
||||
/// <remarks>
|
||||
/// Hand-written rather than serialized from a DTO. The body is two fields, and the wire form
|
||||
/// is fixed by the spec — a serializer would put naming policy, casing options and a shared
|
||||
/// <c>JsonSerializerOptions</c> between this repo and a wire contract it does not control.
|
||||
/// <c>timestamp</c> is emitted as a bare JSON number, never a string: a quoted timestamp
|
||||
/// parses as a valid JSON object and is silently wrong to every conformant host.
|
||||
/// </remarks>
|
||||
public static byte[] Encode(bool online, long timestampMs) =>
|
||||
Encoding.UTF8.GetBytes(
|
||||
$"{{\"online\":{(online ? "true" : "false")},\"timestamp\":{timestampMs.ToString(System.Globalization.CultureInfo.InvariantCulture)}}}");
|
||||
|
||||
/// <summary>Reads the <c>online</c> flag back out of a v3.0 STATE body, for tests and diagnostics.</summary>
|
||||
/// <param name="body">The STATE body bytes.</param>
|
||||
/// <param name="online">The decoded flag on success.</param>
|
||||
/// <param name="timestampMs">The decoded timestamp on success.</param>
|
||||
/// <returns><see langword="true"/> when the body is a well-formed v3.0 STATE object.</returns>
|
||||
public static bool TryDecode(ReadOnlySpan<byte> body, out bool online, out long timestampMs)
|
||||
{
|
||||
online = false;
|
||||
timestampMs = 0;
|
||||
|
||||
if (body.IsEmpty)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var reader = new Utf8JsonReader(body);
|
||||
if (!JsonDocument.TryParseValue(ref reader, out var doc))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
using (doc)
|
||||
{
|
||||
if (doc.RootElement.ValueKind != JsonValueKind.Object
|
||||
|| !doc.RootElement.TryGetProperty("online", out var flag)
|
||||
|| flag.ValueKind is not (JsonValueKind.True or JsonValueKind.False))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
online = flag.GetBoolean();
|
||||
timestampMs = doc.RootElement.TryGetProperty("timestamp", out var ts)
|
||||
&& ts.ValueKind == JsonValueKind.Number
|
||||
&& ts.TryGetInt64(out var parsed)
|
||||
? parsed
|
||||
: 0;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The STATE topic plus the matched pair of bodies for one primary-host session (Gitea #511).
|
||||
/// </summary>
|
||||
/// <param name="Topic">The v3.0 STATE topic, <c>spBv1.0/STATE/{hostId}</c>.</param>
|
||||
/// <param name="TimestampMs">The session timestamp carried by BOTH bodies.</param>
|
||||
/// <param name="OnlineBody">
|
||||
/// The birth certificate — published retained, at QoS 1, immediately after CONNACK.
|
||||
/// </param>
|
||||
/// <param name="OfflineBody">
|
||||
/// The death certificate — registered as the MQTT Last Will in the CONNECT packet, and also
|
||||
/// published explicitly on a clean stand-down so the broker holds it without waiting for a
|
||||
/// keep-alive timeout.
|
||||
/// </param>
|
||||
public sealed record SparkplugStateSession(
|
||||
string Topic,
|
||||
long TimestampMs,
|
||||
byte[] OnlineBody,
|
||||
byte[] OfflineBody);
|
||||
@@ -41,20 +41,51 @@ public static class MqttClientOptionsFactory
|
||||
/// empty MQTTnet generates a random client id.
|
||||
/// </param>
|
||||
/// <param name="logger">Optional logger for the deferred CA-pin load; never receives credentials.</param>
|
||||
/// <param name="lastWill">
|
||||
/// Optional MQTT Last Will registered in the CONNECT packet (Gitea #511 — the Sparkplug
|
||||
/// primary-host death certificate). See <see cref="MqttLastWill"/> for why this can only be
|
||||
/// supplied here.
|
||||
/// </param>
|
||||
/// <param name="cleanSessionOverride">
|
||||
/// Forces clean-session on or off, ignoring <see cref="MqttDriverOptions.CleanSession"/>. Used
|
||||
/// by the primary-host connection, which must connect clean so the broker cannot resume a
|
||||
/// previous session and discard the Will — see <see cref="MqttLastWill"/>.
|
||||
/// </param>
|
||||
public static MqttClientOptions BuildClientOptions(
|
||||
MqttDriverOptions options,
|
||||
string? clientIdSuffix,
|
||||
ILogger? logger = null)
|
||||
ILogger? logger = null,
|
||||
MqttLastWill? lastWill = null,
|
||||
bool? cleanSessionOverride = null)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(options);
|
||||
|
||||
var builder = new MqttClientOptionsBuilder()
|
||||
.WithTcpServer(options.Host, options.Port)
|
||||
.WithProtocolVersion(MapProtocolVersion(options.ProtocolVersion))
|
||||
.WithCleanSession(options.CleanSession)
|
||||
.WithCleanSession(cleanSessionOverride ?? options.CleanSession)
|
||||
.WithKeepAlivePeriod(TimeSpan.FromSeconds(options.KeepAliveSeconds))
|
||||
.WithTimeout(TimeSpan.FromSeconds(options.ConnectTimeoutSeconds));
|
||||
|
||||
if (lastWill is { } will)
|
||||
{
|
||||
builder = builder
|
||||
.WithWillTopic(will.Topic)
|
||||
.WithWillPayload(will.Payload)
|
||||
.WithWillQualityOfServiceLevel((MQTTnet.Protocol.MqttQualityOfServiceLevel)will.Qos)
|
||||
.WithWillRetain(will.Retain);
|
||||
|
||||
// MQTT 5.0 only. Left at its default the broker MAY hold the Will back for up to the
|
||||
// session-expiry interval after an ungraceful disconnect, which for a host death
|
||||
// certificate is precisely the wrong behaviour: the whole point is that edge nodes learn
|
||||
// promptly. Zero means "publish it the moment the network connection is lost".
|
||||
// The setter is harmless on 3.1.1 — the property simply has no wire representation there.
|
||||
if (options.ProtocolVersion == MqttProtocolVersion.V500)
|
||||
{
|
||||
builder = builder.WithWillDelayInterval(0);
|
||||
}
|
||||
}
|
||||
|
||||
var clientId = string.Concat(options.ClientId ?? string.Empty, clientIdSuffix ?? string.Empty);
|
||||
if (!string.IsNullOrWhiteSpace(clientId))
|
||||
{
|
||||
|
||||
@@ -286,11 +286,40 @@ public sealed class MqttConnection : IAsyncDisposable, IMqttSubscribeTransport,
|
||||
private Task? _reconnectWorker;
|
||||
private int _state = (int)MqttConnectionState.Disconnected;
|
||||
|
||||
/// <summary>Client-id suffix distinguishing this connection from the driver's other one; see the ctor.</summary>
|
||||
private readonly string? _clientIdSuffix;
|
||||
|
||||
/// <summary>The Will to register on every CONNECT, or null. See <see cref="MqttLastWill"/>.</summary>
|
||||
private readonly MqttLastWill? _lastWill;
|
||||
|
||||
/// <summary>Clean-session override, or null to use the configured value.</summary>
|
||||
private readonly bool? _cleanSessionOverride;
|
||||
|
||||
/// <summary>Stores configuration only — no network, no client instantiation, no supervisor.</summary>
|
||||
/// <param name="options">Broker connection settings.</param>
|
||||
/// <param name="driverId">Driver instance id, used only for log/diagnostic correlation.</param>
|
||||
/// <param name="logger">Optional logger; never receives credentials.</param>
|
||||
public MqttConnection(MqttDriverOptions options, string driverId, ILogger? logger = null)
|
||||
/// <param name="clientIdSuffix">
|
||||
/// Appended to the configured client id so a second connection from the same driver — the
|
||||
/// Sparkplug primary-host claim (Gitea #511) — does not collide with the ingest session. Two
|
||||
/// MQTT clients presenting one client id is not an error the broker reports: it silently
|
||||
/// disconnects the older one, so the two would take turns evicting each other forever.
|
||||
/// </param>
|
||||
/// <param name="lastWill">
|
||||
/// Optional Last Will registered in the CONNECT packet, and therefore rebuilt on every
|
||||
/// reconnect attempt along with the rest of the options. See <see cref="MqttLastWill"/>.
|
||||
/// </param>
|
||||
/// <param name="cleanSessionOverride">
|
||||
/// Forces clean-session on or off regardless of <see cref="MqttDriverOptions.CleanSession"/>.
|
||||
/// Null keeps the configured value.
|
||||
/// </param>
|
||||
public MqttConnection(
|
||||
MqttDriverOptions options,
|
||||
string driverId,
|
||||
ILogger? logger = null,
|
||||
string? clientIdSuffix = null,
|
||||
MqttLastWill? lastWill = null,
|
||||
bool? cleanSessionOverride = null)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(options);
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(driverId);
|
||||
@@ -298,6 +327,9 @@ public sealed class MqttConnection : IAsyncDisposable, IMqttSubscribeTransport,
|
||||
_options = options;
|
||||
_driverId = driverId;
|
||||
_logger = logger;
|
||||
_clientIdSuffix = clientIdSuffix;
|
||||
_lastWill = lastWill;
|
||||
_cleanSessionOverride = cleanSessionOverride;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -370,6 +402,47 @@ public sealed class MqttConnection : IAsyncDisposable, IMqttSubscribeTransport,
|
||||
/// </summary>
|
||||
internal Func<Task>? AfterConnectHookForTests { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Test seam: drops the session the way a crashed process does — no MQTT <c>DISCONNECT</c>, so
|
||||
/// the broker treats the closed socket as an abnormal termination and publishes the Last Will.
|
||||
/// </summary>
|
||||
/// <returns>A task completing once the client is gone and the supervisor has stopped.</returns>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// Exists because the Sparkplug primary-host contract's most important leg is the one a
|
||||
/// normal teardown cannot reach (Gitea #511): <c>DisposeAsync</c> sends a clean
|
||||
/// <c>DISCONNECT</c>, which by MQTT rule means the broker <b>discards</b> the Will. So an
|
||||
/// in-suite test driven through the ordinary lifecycle can only ever observe the explicit
|
||||
/// stand-down <c>OFFLINE</c>, and would pass identically against an implementation whose
|
||||
/// Will was never armed at all.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Not a full substitute for the <c>kill -9</c> the live gate runs by hand — that also
|
||||
/// proves the process dies without any of this code running — but it is the closest an
|
||||
/// in-process test can get, and it is what makes the Will's <i>content</i> observable.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
internal async Task AbandonForTests()
|
||||
{
|
||||
// Mark disposed first, so the supervisor cannot reconnect into the gap and publish a fresh
|
||||
// birth on top of the Will the broker is about to deliver.
|
||||
Interlocked.Exchange(ref _disposed, 1);
|
||||
SetState(MqttConnectionState.Disposed);
|
||||
|
||||
try
|
||||
{
|
||||
await _lifetimeCts.CancelAsync().ConfigureAwait(false);
|
||||
}
|
||||
catch (ObjectDisposedException)
|
||||
{
|
||||
// Nothing to cancel.
|
||||
}
|
||||
|
||||
// Dispose WITHOUT DisconnectAsync: MQTTnet closes the channel, the broker sees a dead socket
|
||||
// rather than a graceful goodbye, and the Will fires. This is the whole point of the seam.
|
||||
Interlocked.Exchange(ref _client, null)?.Dispose();
|
||||
}
|
||||
|
||||
private bool Disposed => Volatile.Read(ref _disposed) != 0;
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -568,7 +641,8 @@ public sealed class MqttConnection : IAsyncDisposable, IMqttSubscribeTransport,
|
||||
MqttClientOptions clientOptions;
|
||||
try
|
||||
{
|
||||
clientOptions = MqttClientOptionsFactory.BuildClientOptions(_options, clientIdSuffix: null, _logger);
|
||||
clientOptions = MqttClientOptionsFactory.BuildClientOptions(
|
||||
_options, _clientIdSuffix, _logger, _lastWill, _cleanSessionOverride);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
@@ -185,6 +185,16 @@ public sealed class MqttDriver
|
||||
private MqttConnection? _connection;
|
||||
private CancellationTokenSource? _hostProbeCts;
|
||||
|
||||
/// <summary>The Primary data-plane verdict the Sparkplug host claim is gated on (Gitea #511).</summary>
|
||||
private readonly IDriverPrimaryRoleSource _primaryRoleSource;
|
||||
|
||||
/// <summary>
|
||||
/// The Sparkplug primary-host claim, or <see langword="null"/> when this configuration does not
|
||||
/// ask for one. Owns its own broker connection — see <see cref="SparkplugPrimaryHost"/> for why
|
||||
/// it cannot share the ingest session.
|
||||
/// </summary>
|
||||
private SparkplugPrimaryHost? _primaryHost;
|
||||
|
||||
private DriverHealth _health = new(DriverState.Unknown, null, null);
|
||||
private HostState _hostState = HostState.Unknown;
|
||||
private DateTime _hostStateChangedUtc = DateTime.UtcNow;
|
||||
@@ -198,7 +208,18 @@ public sealed class MqttDriver
|
||||
/// </param>
|
||||
/// <param name="driverInstanceId">Stable logical id of this driver instance.</param>
|
||||
/// <param name="logger">Optional logger; never receives credentials or payload bodies.</param>
|
||||
public MqttDriver(MqttDriverOptions options, string driverInstanceId, ILogger<MqttDriver>? logger = null)
|
||||
/// <param name="primaryRoleSource">
|
||||
/// Gitea #511: the Primary data-plane verdict the Sparkplug primary-host claim is gated on.
|
||||
/// <see langword="null"/> falls back to <see cref="UnmanagedDriverPrimaryRoleSource"/> — which
|
||||
/// always claims — for standalone hosts and tests. On a redundant pair that fallback is the
|
||||
/// dual-claim this gate exists to prevent, so the Host resolves a real one and
|
||||
/// <c>DriverFactoryBootstrap</c> does so with <c>GetRequiredService</c>.
|
||||
/// </param>
|
||||
public MqttDriver(
|
||||
MqttDriverOptions options,
|
||||
string driverInstanceId,
|
||||
ILogger<MqttDriver>? logger = null,
|
||||
IDriverPrimaryRoleSource? primaryRoleSource = null)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(options);
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(driverInstanceId);
|
||||
@@ -206,6 +227,7 @@ public sealed class MqttDriver
|
||||
_options = options;
|
||||
_driverInstanceId = driverInstanceId;
|
||||
_logger = logger;
|
||||
_primaryRoleSource = primaryRoleSource ?? UnmanagedDriverPrimaryRoleSource.Instance;
|
||||
_subscriptions = CreateSubscriptionManager(options);
|
||||
_sparkplug = CreateSparkplugIngestor(options);
|
||||
RegisterAuthoredTags(options);
|
||||
@@ -1001,6 +1023,7 @@ public sealed class MqttDriver
|
||||
WriteHealth(new DriverHealth(DriverState.Healthy, connection.LastMessageUtc, null));
|
||||
TransitionHost(HostState.Running);
|
||||
StartHostProbe();
|
||||
await StartPrimaryHostAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -1094,21 +1117,15 @@ public sealed class MqttDriver
|
||||
return null;
|
||||
}
|
||||
|
||||
if (options.Sparkplug is { ActAsPrimaryHost: true })
|
||||
// The flag no longer reads as if it does something it does not (Gitea #511) — but a request it
|
||||
// CANNOT honour still warns loudly rather than being dropped, because the operator's plant is
|
||||
// waiting on a STATE message that will never arrive.
|
||||
if (SparkplugPrimaryHost.DescribeUnusable(options) is { } unusable)
|
||||
{
|
||||
// Loud, because the flag reads as if it does something. Being a Sparkplug Primary Host is a
|
||||
// three-part contract — a retained ONLINE after CONNECT, a matching retained OFFLINE
|
||||
// registered as the MQTT Last Will AT CONNECT TIME, and an explicit OFFLINE on clean
|
||||
// shutdown — and edge nodes stop publishing while their host is offline. Shipping the
|
||||
// ONLINE half without the Will is strictly worse than shipping neither: a crashed node
|
||||
// would leave a retained ONLINE asserting forever that a dead host is alive, which is the
|
||||
// exact state the mechanism exists to prevent. The Will belongs in
|
||||
// MqttClientOptionsFactory.BuildClientOptions; until it is there this flag stays inert and said so.
|
||||
_logger?.LogWarning(
|
||||
"MQTT driver '{DriverId}': Sparkplug actAsPrimaryHost is set but primary-host STATE "
|
||||
+ "publishing is not implemented — this driver observes STATE and never publishes one. "
|
||||
+ "Edge nodes gated on this host id will not see it come online.",
|
||||
_driverInstanceId);
|
||||
"MQTT driver '{DriverId}': {Reason} Edge nodes gated on this host id will not see it come online.",
|
||||
_driverInstanceId,
|
||||
unusable);
|
||||
}
|
||||
|
||||
var ingestor = new SparkplugIngestor(
|
||||
@@ -1263,9 +1280,55 @@ public sealed class MqttDriver
|
||||
o.Sparkplug,
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Opens the Sparkplug primary-host claim when this configuration asks for one (Gitea #511).
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// <b>Runs last, and can never fail the initialize.</b> The claim is an advisory assertion
|
||||
/// to other applications on the broker; the ingest path is this driver's data plane. A
|
||||
/// broker that accepts the ingest session but rejects a publish on the STATE topic — an ACL
|
||||
/// that does not grant <c>spBv1.0/STATE/#</c> is the ordinary way this happens — must cost
|
||||
/// the operator a warning and a plant-visible missing host, not every tag's value.
|
||||
/// <see cref="SparkplugPrimaryHost.StartAsync"/> already swallows and retries; the catch
|
||||
/// here covers the rest so no future edit can turn this into a fault path by accident.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
private async Task StartPrimaryHostAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (!SparkplugPrimaryHost.CanClaim(_options))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var host = new SparkplugPrimaryHost(_options, _driverInstanceId, _primaryRoleSource, _logger);
|
||||
_primaryHost = host;
|
||||
await host.StartAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger?.LogWarning(
|
||||
ex,
|
||||
"MQTT driver '{DriverId}': the Sparkplug primary-host claim could not be started; ingest is unaffected.",
|
||||
_driverInstanceId);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Shared teardown for <see cref="ShutdownAsync"/>, <see cref="DisposeAsync"/> and the session rebuild.</summary>
|
||||
private async Task TeardownAsync()
|
||||
{
|
||||
// Stand the host claim down FIRST, while the broker is still reachable and before the ingest
|
||||
// session goes. Its explicit retained OFFLINE is what makes the transition immediate for edge
|
||||
// nodes; leaving it to the Will costs them up to 1.5 keep-alive periods of believing a host
|
||||
// that has already stopped is still online.
|
||||
var primaryHost = Interlocked.Exchange(ref _primaryHost, null);
|
||||
if (primaryHost is not null)
|
||||
{
|
||||
await primaryHost.DisposeAsync().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var probe = Interlocked.Exchange(ref _hostProbeCts, null);
|
||||
if (probe is not null)
|
||||
{
|
||||
|
||||
@@ -49,16 +49,30 @@ public static class MqttDriverFactoryExtensions
|
||||
/// </summary>
|
||||
/// <param name="registry">The driver factory registry to register with.</param>
|
||||
/// <param name="loggerFactory">Optional logger factory used to create per-instance loggers.</param>
|
||||
public static void Register(DriverFactoryRegistry registry, ILoggerFactory? loggerFactory = null)
|
||||
/// <param name="primaryRoleSource">
|
||||
/// Gitea #511: the Primary data-plane verdict the Sparkplug primary-host claim is gated on,
|
||||
/// captured at registration time like <paramref name="loggerFactory"/>. Null falls back to
|
||||
/// <see cref="UnmanagedDriverPrimaryRoleSource"/> (always claims) — correct for a standalone
|
||||
/// host or a test, and the reason <c>DriverFactoryBootstrap</c> resolves it with
|
||||
/// <c>GetRequiredService</c> rather than letting a redundant deployment reach that fallback.
|
||||
/// </param>
|
||||
public static void Register(
|
||||
DriverFactoryRegistry registry,
|
||||
ILoggerFactory? loggerFactory = null,
|
||||
IDriverPrimaryRoleSource? primaryRoleSource = null)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(registry);
|
||||
registry.Register(DriverTypeName, (id, json) => CreateInstance(id, json, loggerFactory));
|
||||
registry.Register(DriverTypeName, (id, json) => CreateInstance(id, json, loggerFactory, primaryRoleSource));
|
||||
}
|
||||
|
||||
/// <summary>Public for the Server-side bootstrapper + test consumers.</summary>
|
||||
/// <param name="driverInstanceId">Stable logical id of the driver instance.</param>
|
||||
/// <param name="driverConfigJson">The <c>DriverConfig</c> JSON blob for the instance.</param>
|
||||
/// <param name="loggerFactory">Optional logger factory for the per-instance logger.</param>
|
||||
/// <param name="primaryRoleSource">
|
||||
/// Gitea #511: the Primary data-plane verdict gating the Sparkplug primary-host claim; null
|
||||
/// falls back to <see cref="UnmanagedDriverPrimaryRoleSource"/>.
|
||||
/// </param>
|
||||
/// <returns>A configured, not-yet-connected <see cref="MqttDriver"/>.</returns>
|
||||
/// <exception cref="ArgumentException">
|
||||
/// <paramref name="driverInstanceId"/> or <paramref name="driverConfigJson"/> is blank.
|
||||
@@ -68,7 +82,8 @@ public static class MqttDriverFactoryExtensions
|
||||
public static MqttDriver CreateInstance(
|
||||
string driverInstanceId,
|
||||
string driverConfigJson,
|
||||
ILoggerFactory? loggerFactory = null)
|
||||
ILoggerFactory? loggerFactory = null,
|
||||
IDriverPrimaryRoleSource? primaryRoleSource = null)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(driverInstanceId);
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(driverConfigJson);
|
||||
@@ -79,6 +94,7 @@ public static class MqttDriverFactoryExtensions
|
||||
?? throw new InvalidOperationException(
|
||||
$"MQTT driver config for '{driverInstanceId}' deserialised to null");
|
||||
|
||||
return new MqttDriver(options, driverInstanceId, loggerFactory?.CreateLogger<MqttDriver>());
|
||||
return new MqttDriver(
|
||||
options, driverInstanceId, loggerFactory?.CreateLogger<MqttDriver>(), primaryRoleSource);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,9 +73,11 @@ namespace ZB.MOM.WW.OtOpcUa.Driver.Mqtt.Sparkplug;
|
||||
/// subscriber is contained, and the rebirth publish is dispatched off-thread.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// <b>Primary-host STATE publishing is deliberately NOT implemented</b> — see
|
||||
/// <see cref="MqttSparkplugOptions.ActAsPrimaryHost"/> and the remarks on
|
||||
/// <see cref="HandleState"/>. This type <i>observes</i> STATE; it never publishes one.
|
||||
/// <b>This type only ever <i>observes</i> STATE.</b> Primary-host STATE publishing ships
|
||||
/// (Gitea #511) but deliberately does not live here: the claim is bound to the lifetime of its
|
||||
/// own dedicated connection, because an MQTT Will can only be registered at CONNECT. See
|
||||
/// <see cref="SparkplugPrimaryHost"/>, and the remarks on <see cref="HandleState"/> for what
|
||||
/// this side does with an observed one.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public sealed class SparkplugIngestor
|
||||
@@ -1029,18 +1031,17 @@ public sealed class SparkplugIngestor
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// <b>Receive only. This driver never publishes STATE, and
|
||||
/// <see cref="MqttSparkplugOptions.ActAsPrimaryHost"/> is therefore inert — deliberately, and
|
||||
/// loudly.</b> Being a Sparkplug Primary Host is not "publish an ONLINE message": it is a
|
||||
/// three-part contract — a retained <c>ONLINE</c> STATE published after CONNECT, a matching
|
||||
/// retained <c>OFFLINE</c> registered as the client's <b>MQTT Last Will at CONNECT time</b>,
|
||||
/// and an explicit OFFLINE on clean shutdown. Edge nodes subscribe to it and stop publishing
|
||||
/// while their host is offline. Shipping the ONLINE half without the Will is strictly worse
|
||||
/// than shipping neither: an OtOpcUa node that crashes would leave a retained ONLINE asserting
|
||||
/// forever that a dead host is alive, which is precisely the state the mechanism exists to
|
||||
/// prevent. The Will has to be set inside <c>MqttClientOptionsFactory.BuildClientOptions</c> — a
|
||||
/// different file, a different task — so this task implements the observation half and
|
||||
/// <see cref="MqttDriver"/> warns when the flag is set. See the design doc §7/§8.
|
||||
/// <b>Receive only, on this connection.</b> The publish half ships as
|
||||
/// <see cref="SparkplugPrimaryHost"/> (Gitea #511), on a connection of its own — an MQTT Will
|
||||
/// can only be registered in the CONNECT packet, so a claim that is acquired and released at
|
||||
/// runtime cannot ride the long-lived ingest session this type is attached to.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// <b>A host claimed by THIS node is observed here too, and that is correct rather than an
|
||||
/// oversight.</b> The ingestor subscribes the configured host id whether or not this node
|
||||
/// publishes it, so a node acting as primary host sees its own retained <c>ONLINE</c> come
|
||||
/// back. Nothing here gates on it — <see cref="HostStateObserved"/> is informational — so the
|
||||
/// echo costs one log line at the transition and cannot feed back into the claim.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Both payload forms are accepted on receive: the v3.0 JSON <c>{"online":bool,...}</c> and
|
||||
|
||||
@@ -0,0 +1,470 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Driver.Mqtt.Sparkplug;
|
||||
|
||||
/// <summary>
|
||||
/// Claims the Sparkplug Primary Host Application identity for this node — the three-part contract
|
||||
/// <see cref="MqttSparkplugOptions.ActAsPrimaryHost"/> promises and, until Gitea #511, did not
|
||||
/// deliver: a retained <c>ONLINE</c> while the claim is held, a matching retained <c>OFFLINE</c>
|
||||
/// registered as the MQTT Last Will, and an explicit <c>OFFLINE</c> on a clean stand-down.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// <b>The claim is a whole dedicated connection, not a message.</b> A Will can only be
|
||||
/// registered in the CONNECT packet (see <see cref="MqttLastWill"/>), so a claim that must be
|
||||
/// acquired and released at runtime cannot live on the driver's shared ingest session — that
|
||||
/// session connects once and stays up, and its Will could be neither added on gaining the role
|
||||
/// nor withdrawn on losing it. This type therefore opens its <i>own</i> connection when the
|
||||
/// node gains Primary and closes it when the node loses Primary. The connection's lifetime
|
||||
/// <i>is</i> the claim, which is what makes every leg of the contract fall out for free:
|
||||
/// <list type="bullet">
|
||||
/// <item>the Will is registered because the connection was opened to hold the claim;</item>
|
||||
/// <item>a <c>kill -9</c> drops the connection, so the broker publishes the Will;</item>
|
||||
/// <item>a clean stand-down publishes <c>OFFLINE</c> and then disconnects, so the retained
|
||||
/// message is correct before the Will could ever fire.</item>
|
||||
/// </list>
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// <b>Why not rebuild the ingest connection instead.</b> That was the shape first sketched on
|
||||
/// the issue, and it works, but it drops every subscription and the whole birth cache on each
|
||||
/// role change — producing a data gap on the Secondary→Primary transition, which is precisely
|
||||
/// the moment a plant most needs values flowing. A second connection costs one extra client id
|
||||
/// and leaves ingest untouched.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// <b>Primary-gated, by owner decision (2026-07-31).</b> Both nodes of a redundant pair
|
||||
/// subscribe independently — MQTT fan-out is per-connection and the Primary gate applies
|
||||
/// downstream of ingest — so without gating, both would claim the same host id. The sharp
|
||||
/// failure is not the two fighting over the retained <c>STATE</c>: it is that the
|
||||
/// <b>Secondary's</b> death would publish <c>OFFLINE</c>, edge nodes would stop publishing,
|
||||
/// and the surviving Primary would be perfectly healthy throughout. Redundancy would cause the
|
||||
/// outage the mechanism exists to prevent. The gate is
|
||||
/// <see cref="IDriverPrimaryRoleSource.ShouldActAsPrimary"/>, sampled on a timer.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// <b>Accepted trade: a brief edge-visible <c>OFFLINE</c> window during failover</b>, while the
|
||||
/// old Primary stands down and the new one connects and republishes. That is a genuine state
|
||||
/// transition — for a window there really is no host — not a glitch to paper over.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// <b>Reconnect re-asserts the claim.</b> The dedicated connection carries the same supervisor
|
||||
/// as the ingest one, and <c>Reconnected</c> republishes the birth. Without that, a broker
|
||||
/// restart would leave the host connected but its retained <c>ONLINE</c> gone — a live host
|
||||
/// invisible to every edge node, with nothing in any log to say so.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public sealed class SparkplugPrimaryHost : IAsyncDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Appended to the driver's configured client id for the STATE connection. Two MQTT clients
|
||||
/// presenting one client id is not reported as an error — the broker silently evicts the older
|
||||
/// one — so ingest and the host claim would take turns killing each other forever.
|
||||
/// </summary>
|
||||
internal const string ClientIdSuffix = "-spbhost";
|
||||
|
||||
/// <summary>How often the role gate is sampled. Bounds the failover republish latency.</summary>
|
||||
private static readonly TimeSpan RoleSampleInterval = TimeSpan.FromSeconds(1);
|
||||
|
||||
private readonly MqttDriverOptions _options;
|
||||
private readonly string _hostId;
|
||||
private readonly string _driverId;
|
||||
private readonly IDriverPrimaryRoleSource _roleSource;
|
||||
private readonly ILogger? _logger;
|
||||
private readonly TimeProvider _timeProvider;
|
||||
|
||||
/// <summary>Serializes claim/release against each other and against <see cref="DisposeAsync"/>.</summary>
|
||||
private readonly SemaphoreSlim _gate = new(1, 1);
|
||||
|
||||
private CancellationTokenSource? _loopCts;
|
||||
|
||||
/// <summary>The live claim, or null when this node is not currently the primary host.</summary>
|
||||
private MqttConnection? _connection;
|
||||
|
||||
/// <summary>The session bodies backing <see cref="_connection"/>; non-null exactly when it is.</summary>
|
||||
private SparkplugStateSession? _session;
|
||||
|
||||
private int _disposed;
|
||||
|
||||
/// <summary>Creates a primary-host claim. Opens nothing — <see cref="StartAsync"/> does that.</summary>
|
||||
/// <param name="options">Broker connection settings; the host id comes from its Sparkplug section.</param>
|
||||
/// <param name="driverId">Driver instance id, for log correlation.</param>
|
||||
/// <param name="roleSource">The Primary data-plane verdict this claim is gated on.</param>
|
||||
/// <param name="logger">Optional logger.</param>
|
||||
/// <param name="timeProvider">Clock seam; defaults to <see cref="TimeProvider.System"/>.</param>
|
||||
/// <exception cref="ArgumentException">
|
||||
/// The options carry no Sparkplug host id — <see cref="CanClaim"/> exists so a caller never has
|
||||
/// to hit this.
|
||||
/// </exception>
|
||||
public SparkplugPrimaryHost(
|
||||
MqttDriverOptions options,
|
||||
string driverId,
|
||||
IDriverPrimaryRoleSource roleSource,
|
||||
ILogger? logger = null,
|
||||
TimeProvider? timeProvider = null)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(options);
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(driverId);
|
||||
ArgumentNullException.ThrowIfNull(roleSource);
|
||||
|
||||
var hostId = options.Sparkplug?.HostId;
|
||||
if (string.IsNullOrWhiteSpace(hostId))
|
||||
{
|
||||
throw new ArgumentException(
|
||||
"A Sparkplug primary-host claim needs a hostId; check CanClaim first.", nameof(options));
|
||||
}
|
||||
|
||||
_options = options;
|
||||
_hostId = hostId.Trim();
|
||||
_driverId = driverId;
|
||||
_roleSource = roleSource;
|
||||
_logger = logger;
|
||||
_timeProvider = timeProvider ?? TimeProvider.System;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether <paramref name="options"/> asks for — and can support — a primary-host claim:
|
||||
/// Sparkplug mode, <c>actAsPrimaryHost</c> set, and a non-blank <c>hostId</c>.
|
||||
/// </summary>
|
||||
/// <param name="options">The driver options to test.</param>
|
||||
/// <returns><see langword="true"/> when a claim should be made.</returns>
|
||||
/// <remarks>
|
||||
/// The blank-host-id case is a configuration error the AdminUI form already refuses, but the
|
||||
/// driver must not fault on it: the config could have been authored before that validation
|
||||
/// existed. It is reported by <see cref="DescribeUnusable"/> and the flag is treated as off.
|
||||
/// </remarks>
|
||||
public static bool CanClaim(MqttDriverOptions options) =>
|
||||
options is { Mode: MqttMode.SparkplugB, Sparkplug: { ActAsPrimaryHost: true } spb }
|
||||
&& !string.IsNullOrWhiteSpace(spb.HostId);
|
||||
|
||||
/// <summary>
|
||||
/// Describes why an <c>actAsPrimaryHost</c> request cannot be honoured, or null when it can be
|
||||
/// (or was never made).
|
||||
/// </summary>
|
||||
/// <param name="options">The driver options to test.</param>
|
||||
/// <returns>An operator-facing reason, or <see langword="null"/>.</returns>
|
||||
public static string? DescribeUnusable(MqttDriverOptions options)
|
||||
{
|
||||
if (options?.Sparkplug is not { ActAsPrimaryHost: true } spb)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (options.Mode != MqttMode.SparkplugB)
|
||||
{
|
||||
return "actAsPrimaryHost is set but the driver is not in SparkplugB mode; there is no host state to publish.";
|
||||
}
|
||||
|
||||
return string.IsNullOrWhiteSpace(spb.HostId)
|
||||
? "actAsPrimaryHost is set but hostId is blank; a host claim has no topic to publish on."
|
||||
: null;
|
||||
}
|
||||
|
||||
/// <summary>The STATE topic this claim publishes on.</summary>
|
||||
public string StateTopic => SparkplugTopic.FormatState(_hostId);
|
||||
|
||||
/// <summary>
|
||||
/// Whether the claim is currently held — i.e. a retained <c>ONLINE</c> has been published and a
|
||||
/// Will is armed behind it.
|
||||
/// </summary>
|
||||
public bool IsClaimed => Volatile.Read(ref _connection) is not null;
|
||||
|
||||
/// <summary>
|
||||
/// Test seam: how many times a claim connect has been <i>attempted</i>, successful or not.
|
||||
/// Always ignored in production.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <b>Without this, "a Secondary does not claim" is a vacuous assertion.</b> The obvious test —
|
||||
/// start a Secondary and assert <see cref="IsClaimed"/> is false — passes identically whether
|
||||
/// the gate works or whether the gate is deleted and the connect simply fails against a broker
|
||||
/// the test cannot reach. Counting attempts separates "did not try" from "tried and failed",
|
||||
/// which is the whole distinction the gate is.
|
||||
/// </remarks>
|
||||
internal int ClaimAttemptsForTests => Volatile.Read(ref _claimAttempts);
|
||||
|
||||
private int _claimAttempts;
|
||||
|
||||
/// <summary>
|
||||
/// Test seam: kills the claim connection the way a crashed process does — no clean
|
||||
/// <c>DISCONNECT</c> and no explicit stand-down publish — so the broker delivers the Last Will.
|
||||
/// See <see cref="MqttConnection.AbandonForTests"/> for why this cannot be reached through the
|
||||
/// ordinary lifecycle.
|
||||
/// </summary>
|
||||
/// <returns>A task completing once the connection is gone.</returns>
|
||||
internal async Task AbandonClaimForTests()
|
||||
{
|
||||
// Stop the reconcile loop first, or the next tick sees no connection, decides the claim is
|
||||
// lost, and re-claims — republishing ONLINE over the Will this is meant to observe.
|
||||
var loop = Interlocked.Exchange(ref _loopCts, null);
|
||||
if (loop is not null)
|
||||
{
|
||||
await loop.CancelAsync().ConfigureAwait(false);
|
||||
loop.Dispose();
|
||||
}
|
||||
|
||||
var connection = Interlocked.Exchange(ref _connection, null);
|
||||
_session = null;
|
||||
if (connection is not null)
|
||||
{
|
||||
await connection.AbandonForTests().ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts the role-reconcile loop. Returns once the first reconcile has run, so a caller that
|
||||
/// starts as Primary has published its <c>ONLINE</c> before this completes.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">Cancels the initial reconcile.</param>
|
||||
/// <returns>A task completing after the first reconcile.</returns>
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(Volatile.Read(ref _disposed) != 0, this);
|
||||
|
||||
var cts = new CancellationTokenSource();
|
||||
var previous = Interlocked.Exchange(ref _loopCts, cts);
|
||||
if (previous is not null)
|
||||
{
|
||||
await previous.CancelAsync().ConfigureAwait(false);
|
||||
previous.Dispose();
|
||||
}
|
||||
|
||||
// Reconcile once inline. A failure here is logged and swallowed, never rethrown: the driver's
|
||||
// ingest path is the data plane, and refusing to start it because a broker would not accept a
|
||||
// host claim would trade every tag's value for an advisory message. The loop retries.
|
||||
await ReconcileAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
_ = Task.Run(() => ReconcileLoopAsync(cts.Token), CancellationToken.None);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stands the claim down cleanly: publishes an explicit retained <c>OFFLINE</c>, then
|
||||
/// disconnects. Idempotent, and safe to call when no claim is held.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">Bounds the stand-down publish.</param>
|
||||
/// <returns>A task completing when the claim is released.</returns>
|
||||
public async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var loop = Interlocked.Exchange(ref _loopCts, null);
|
||||
if (loop is not null)
|
||||
{
|
||||
await loop.CancelAsync().ConfigureAwait(false);
|
||||
loop.Dispose();
|
||||
}
|
||||
|
||||
await ReleaseAsync("shutdown", cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (Interlocked.Exchange(ref _disposed, 1) != 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Bounded: dispose must not hang on an unreachable broker. A stand-down we cannot publish is
|
||||
// covered by the Will the moment the socket closes, which is the whole point of having one.
|
||||
using var deadline = new CancellationTokenSource(TimeSpan.FromSeconds(_options.ConnectTimeoutSeconds));
|
||||
try
|
||||
{
|
||||
await StopAsync(deadline.Token).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger?.LogDebug(
|
||||
ex, "MQTT driver '{DriverId}': Sparkplug primary-host stand-down failed during dispose.", _driverId);
|
||||
}
|
||||
|
||||
_gate.Dispose();
|
||||
}
|
||||
|
||||
private async Task ReconcileLoopAsync(CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
while (!ct.IsCancellationRequested)
|
||||
{
|
||||
await Task.Delay(RoleSampleInterval, _timeProvider, ct).ConfigureAwait(false);
|
||||
await ReconcileAsync(ct).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
// Shutdown.
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// The loop must not die silently — a dead loop means the claim freezes in whatever state
|
||||
// it was last in, including "claimed by a node that is no longer Primary".
|
||||
_logger?.LogError(
|
||||
ex, "MQTT driver '{DriverId}': Sparkplug primary-host reconcile loop stopped.", _driverId);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Brings the held claim into line with the current role verdict. Never throws — a transient
|
||||
/// broker failure leaves the claim as it was and the next tick tries again.
|
||||
/// </summary>
|
||||
private async Task ReconcileAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_roleSource.ShouldActAsPrimary)
|
||||
{
|
||||
await ClaimAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
await ReleaseAsync("this node is not the redundancy Primary", cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger?.LogWarning(
|
||||
ex,
|
||||
"MQTT driver '{DriverId}': could not reconcile the Sparkplug primary-host claim for '{HostId}'; will retry.",
|
||||
_driverId,
|
||||
_hostId);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ClaimAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (Volatile.Read(ref _connection) is not null)
|
||||
{
|
||||
return; // Already held; the connection's own supervisor keeps it that way.
|
||||
}
|
||||
|
||||
await _gate.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
if (_connection is not null || Volatile.Read(ref _disposed) != 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Interlocked.Increment(ref _claimAttempts);
|
||||
|
||||
// ONE timestamp for both bodies — the Will is built here, before CONNECT, and the birth
|
||||
// published after CONNACK. See SparkplugStatePayload's remarks on why they must match.
|
||||
var session = SparkplugStatePayload.ForSession(
|
||||
_hostId, _timeProvider.GetUtcNow().ToUnixTimeMilliseconds());
|
||||
|
||||
var connection = new MqttConnection(
|
||||
_options,
|
||||
_driverId,
|
||||
_logger,
|
||||
clientIdSuffix: ClientIdSuffix,
|
||||
lastWill: new MqttLastWill(
|
||||
session.Topic, session.OfflineBody, SparkplugStatePayload.StateQos, Retain: true),
|
||||
// Forced clean, ignoring the driver's configured value: a broker that resumes a prior
|
||||
// session for this client id may keep the OLD session's Will and discard the one this
|
||||
// CONNECT carries — leaving a claim with no death certificate behind it, which is the
|
||||
// single state this whole mechanism exists to make impossible.
|
||||
cleanSessionOverride: true);
|
||||
|
||||
// Re-assert on every reconnect. A broker restart drops retained messages; without this the
|
||||
// host would be connected and believed offline by every edge node, silently.
|
||||
connection.Reconnected += async ct =>
|
||||
{
|
||||
await connection.PublishAsync(
|
||||
session.Topic, session.OnlineBody, SparkplugStatePayload.StateQos, retain: true, ct)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
_logger?.LogInformation(
|
||||
"MQTT driver '{DriverId}': re-published Sparkplug primary-host ONLINE for '{HostId}' after reconnect.",
|
||||
_driverId,
|
||||
_hostId);
|
||||
};
|
||||
|
||||
await connection.ConnectAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
try
|
||||
{
|
||||
await connection.PublishAsync(
|
||||
session.Topic, session.OnlineBody, SparkplugStatePayload.StateQos, retain: true, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// A connection whose birth never landed is worse than no connection: it holds a Will
|
||||
// that will publish OFFLINE for a claim that was never asserted, and it looks claimed
|
||||
// to IsClaimed. Tear it down and let the next tick retry from scratch.
|
||||
await connection.DisposeAsync().ConfigureAwait(false);
|
||||
throw;
|
||||
}
|
||||
|
||||
_session = session;
|
||||
Volatile.Write(ref _connection, connection);
|
||||
|
||||
_logger?.LogInformation(
|
||||
"MQTT driver '{DriverId}': claimed Sparkplug primary host '{HostId}' — retained ONLINE published on '{Topic}', "
|
||||
+ "OFFLINE armed as the Last Will.",
|
||||
_driverId,
|
||||
_hostId,
|
||||
session.Topic);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_gate.Release();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ReleaseAsync(string reason, CancellationToken cancellationToken)
|
||||
{
|
||||
if (Volatile.Read(ref _connection) is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await _gate.WaitAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
var connection = Interlocked.Exchange(ref _connection, null);
|
||||
var session = _session;
|
||||
_session = null;
|
||||
if (connection is null || session is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// Explicit OFFLINE before disconnecting. The Will would eventually say the same thing,
|
||||
// but only after the broker's keep-alive timeout notices the socket is gone — up to
|
||||
// 1.5 keep-alive periods of edge nodes believing a host that has already stood down is
|
||||
// still online. Publishing it makes the transition immediate.
|
||||
await connection.PublishAsync(
|
||||
session.Topic, session.OfflineBody, SparkplugStatePayload.StateQos, retain: true, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
_logger?.LogInformation(
|
||||
"MQTT driver '{DriverId}': released Sparkplug primary host '{HostId}' ({Reason}) — retained OFFLINE published.",
|
||||
_driverId,
|
||||
_hostId,
|
||||
reason);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Not fatal, and not silent. The Will still covers this: dropping the connection below
|
||||
// makes the broker publish the identical OFFLINE body. The only thing lost is promptness.
|
||||
_logger?.LogWarning(
|
||||
ex,
|
||||
"MQTT driver '{DriverId}': could not publish the Sparkplug primary-host OFFLINE for '{HostId}'; "
|
||||
+ "the Last Will will deliver it when the session drops.",
|
||||
_driverId,
|
||||
_hostId);
|
||||
}
|
||||
|
||||
await connection.DisposeAsync().ConfigureAwait(false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_gate.Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,10 @@
|
||||
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="ZB.MOM.WW.OtOpcUa.Driver.Mqtt.Tests"/>
|
||||
<!-- The Sparkplug primary-host live gate (Gitea #511) drives AbandonForTests: the Will leg is
|
||||
unreachable through the ordinary lifecycle, because a clean DISCONNECT makes the broker
|
||||
discard the Will by protocol rule. -->
|
||||
<InternalsVisibleTo Include="ZB.MOM.WW.OtOpcUa.Driver.Mqtt.IntegrationTests"/>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user