fix(health): AkkaActiveNodeGate.ForOptions shares the probe's RolePreference scope; bump 0.3.1 (scadaproj#48)

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
Joseph Doherty
2026-11-09 14:55:45 -05:00
parent 3ef43ca926
commit ab3fa9ca83
7 changed files with 90 additions and 13 deletions
@@ -14,7 +14,8 @@ namespace ZB.MOM.WW.Health.Akka;
/// Shares its rule with <see cref="ActiveNodeHealthCheck"/> via
/// <see cref="ClusterActiveNode"/>, so an endpoint gated by this type and the
/// <c>/health/active</c> probe an orchestrator routes by can never disagree about which node
/// is in charge.
/// is in charge — provided both are given the same scope: the same single role, both
/// unscoped, or (via <see cref="ForOptions"/>) the same <c>RolePreference</c> list.
/// </para>
/// <para>
/// <b>Behaviour change in 0.3.0:</b> through 0.2.1 this gate selected by
@@ -33,6 +34,7 @@ public sealed class AkkaActiveNodeGate : IActiveNodeGate
{
private readonly IServiceProvider _serviceProvider;
private readonly string? _role;
private readonly IReadOnlyList<string>? _rolePreference;
/// <summary>Initializes a new <see cref="AkkaActiveNodeGate"/>.</summary>
/// <param name="serviceProvider">
@@ -49,6 +51,27 @@ public sealed class AkkaActiveNodeGate : IActiveNodeGate
_role = role;
}
private AkkaActiveNodeGate(IServiceProvider serviceProvider, IReadOnlyList<string> rolePreference)
{
_serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
_rolePreference = rolePreference;
}
/// <summary>
/// Creates a gate scoped by the same ordered <see cref="ActiveNodeHealthCheckOptions.RolePreference"/>
/// as <see cref="ActiveNodeHealthCheck"/>, so the gate and <c>/health/active</c> agree on every
/// node of a mixed-role mesh (a fused node answers for the first preferred role it carries).
/// A node carrying none of the preferred roles is never active.
/// </summary>
/// <param name="serviceProvider">The application service provider (see the primary constructor).</param>
/// <param name="options">The options the active-node probe is registered with.</param>
/// <returns>The gate.</returns>
public static AkkaActiveNodeGate ForOptions(IServiceProvider serviceProvider, ActiveNodeHealthCheckOptions options)
{
ArgumentNullException.ThrowIfNull(options);
return new AkkaActiveNodeGate(serviceProvider, options.RolePreference);
}
/// <inheritdoc />
public bool IsActiveNode
{
@@ -60,7 +83,10 @@ public sealed class AkkaActiveNodeGate : IActiveNodeGate
try
{
return ClusterActiveNode.SelfIsActive(Cluster.Get(system), _role);
var cluster = Cluster.Get(system);
return _rolePreference is not null
? ClusterActiveNode.SelfIsActiveForPreference(cluster, _rolePreference)
: ClusterActiveNode.SelfIsActive(cluster, _role);
}
catch (Exception)
{
@@ -98,6 +98,30 @@ public static class ClusterActiveNode
return oldest is not null && oldest.UniqueAddress.Equals(self.UniqueAddress);
}
/// <summary>
/// Whether the local node is the active node under an ordered role preference — the exact scope
/// <see cref="ActiveNodeHealthCheck"/> answers for: the first preferred role the node carries
/// (see <see cref="ResolveActiveRole"/>), then <see cref="SelfIsActive(Cluster, string?)"/> in
/// that role.
/// </summary>
/// <param name="cluster">The Akka cluster to evaluate.</param>
/// <param name="rolePreference">
/// Ordered role preference. Empty means unscoped (every Up member competes).
/// </param>
/// <returns>
/// <c>false</c> when the preference is non-empty and the node carries none of its roles (it owns
/// no active work); otherwise the result of the role-scoped <c>SelfIsActive</c>.
/// </returns>
public static bool SelfIsActiveForPreference(Cluster cluster, IReadOnlyList<string> rolePreference)
{
ArgumentNullException.ThrowIfNull(cluster);
ArgumentNullException.ThrowIfNull(rolePreference);
var role = ResolveActiveRole(cluster.SelfMember.Roles, rolePreference);
if (rolePreference.Count > 0 && role is null) return false;
return SelfIsActive(cluster, role);
}
/// <summary>
/// The first role in <paramref name="rolePreference"/> that <paramref name="selfRoles"/> carries
/// — the role whose oldest Up member owns this node's active work.