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); /// /// Pure ServiceLevel computation per design §6. Output range 0–255, where higher = "more /// authoritative." The OPC UA SDK exposes this as the node's ServiceLevel 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. /// public static class ServiceLevelCalculator { /// Computes the service level (0-255) based on node health inputs. /// The node health inputs including cluster state, database reachability, and probe status. /// A service level byte value (0-255) indicating node authority, where higher values indicate more authoritative nodes. 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); } }