Files
lmxopcua/src/Core/ZB.MOM.WW.OtOpcUa.Cluster/Redundancy/ServiceLevelCalculator.cs
T

44 lines
1.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Akka.Cluster;
namespace ZB.MOM.WW.OtOpcUa.Cluster.Redundancy;
public readonly record struct NodeHealthInputs(
MemberStatus MemberState,
bool DbReachable,
bool OpcUaProbeOk,
bool Stale,
bool IsDriverRoleLeader);
/// <summary>
/// Pure ServiceLevel computation per design §6. Output range 0–255, where higher = "more
/// authoritative." The OPC UA SDK exposes this as the node's <c>ServiceLevel</c> Variable and
/// redundant clients use it to pick which server to subscribe to.
///
/// Tiering:
/// - Member not Up/Joining: 0 (cluster cannot trust this node).
/// - DB reachable + OPC UA probe ok + not stale: 240 (full service).
/// - Stale config (DB reachable or not, OPC UA probe state ignored): 100 or 200 depending on DB.
/// - +10 bonus when this node holds the role-leader lease for the "driver" role.
/// </summary>
public static class ServiceLevelCalculator
{
/// <summary>Computes the service level (0-255) based on node health inputs.</summary>
/// <param name="h">The node health inputs including cluster state, database reachability, and probe status.</param>
/// <returns>A service level byte value (0-255) indicating node authority, where higher values indicate more authoritative nodes.</returns>
public static byte Compute(NodeHealthInputs h)
{
if (h.MemberState is not (MemberStatus.Up or MemberStatus.Joining))
return 0;
var basis = (h.DbReachable, h.OpcUaProbeOk, h.Stale) switch
{
(true, true, false) => 240,
(true, _, true) => 200,
(false, _, true) => 100,
_ => 0,
};
return (byte)Math.Clamp(basis + (h.IsDriverRoleLeader ? 10 : 0), 0, 255);
}
}