using Akka.Actor;
using Akka.Cluster;
using Akka.Cluster.Tools.PublishSubscribe;
using Akka.Event;
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Redundancy;
using ZB.MOM.WW.OtOpcUa.Commons.Types;
using CommonsRedundancyRole = ZB.MOM.WW.OtOpcUa.Commons.Messages.Redundancy.RedundancyRole;
namespace ZB.MOM.WW.OtOpcUa.ControlPlane.Redundancy;
///
/// Admin-role cluster singleton that aggregates per-node cluster events into a
/// snapshot and publishes it on the redundancy-state
/// DistributedPubSub topic. Subscribers (notably the OPC UA host's ServiceLevel calc) react to
/// topology changes without polling.
///
/// Recomputation is debounced by — a burst of cluster events from
/// a rolling restart should produce one published snapshot, not one per event.
///
public sealed class RedundancyStateActor : ReceiveActor, IWithTimers
{
public const string Topic = "redundancy-state";
public static readonly TimeSpan DebounceWindow = TimeSpan.FromMilliseconds(250);
///
/// Default interval for the periodic heartbeat re-publish. DistributedPubSub does not replay
/// to late subscribers, so a node that subscribes just after a change-driven publish would
/// otherwise never learn its role until the next membership change. The heartbeat re-publishes
/// the current snapshot so any missed subscriber converges within this interval.
///
public static readonly TimeSpan DefaultHeartbeatInterval = TimeSpan.FromSeconds(10);
private readonly ILoggingAdapter _log = Context.GetLogger();
private readonly Akka.Cluster.Cluster _cluster;
private readonly Action