From 3798977e394cb248d538d5ad3c69e72ea05246aa Mon Sep 17 00:00:00 2001 From: Andreas Dirnberger Date: Fri, 26 Nov 2021 05:55:08 +0100 Subject: [PATCH] remove of Cluster.Get(sys) (#5399) --- src/core/Akka.Cluster/AutoDown.cs | 15 ++++---- src/core/Akka.Cluster/ClusterDaemon.cs | 38 +++++++++++-------- .../SBR/SplitBrainResolverProvider.cs | 4 +- src/core/Akka.Cluster/SplitBrainResolver.cs | 6 +-- 4 files changed, 35 insertions(+), 28 deletions(-) diff --git a/src/core/Akka.Cluster/AutoDown.cs b/src/core/Akka.Cluster/AutoDown.cs index 42167898622..a78ebb93250 100644 --- a/src/core/Akka.Cluster/AutoDown.cs +++ b/src/core/Akka.Cluster/AutoDown.cs @@ -24,7 +24,7 @@ namespace Akka.Cluster /// The implementation is split into two classes AutoDown and AutoDownBase to be /// able to unit test the logic without running cluster. /// - internal class AutoDown : AutoDownBase + internal sealed class AutoDown : AutoDownBase { /// /// TBD @@ -275,7 +275,7 @@ private void Remove(UniqueAddress node) /// public sealed class AutoDowning : IDowningProvider { - private readonly ClusterSettings _clusterSettings; + private readonly ActorSystem _system; /// /// TBD @@ -283,13 +283,13 @@ public sealed class AutoDowning : IDowningProvider /// TBD public AutoDowning(ActorSystem system) { - _clusterSettings = Cluster.Get(system).Settings; + _system = system; } /// /// TBD /// - public TimeSpan DownRemovalMargin => _clusterSettings.DownRemovalMargin; + public TimeSpan DownRemovalMargin => Cluster.Get(_system).Settings.DownRemovalMargin; /// /// TBD @@ -301,10 +301,11 @@ public Props DowningActorProps { get { - if (_clusterSettings.AutoDownUnreachableAfter.HasValue) - return AutoDown.Props(_clusterSettings.AutoDownUnreachableAfter.Value); - else + var autoDownUnreachableAfter = Cluster.Get(_system).Settings.AutoDownUnreachableAfter; + if (!autoDownUnreachableAfter.HasValue) throw new ConfigurationException("AutoDowning downing provider selected but 'akka.cluster.auto-down-unreachable-after' not set"); + + return AutoDown.Props(autoDownUnreachableAfter.Value); } } } diff --git a/src/core/Akka.Cluster/ClusterDaemon.cs b/src/core/Akka.Cluster/ClusterDaemon.cs index 3e1e60bfefb..d7e9211001c 100644 --- a/src/core/Akka.Cluster/ClusterDaemon.cs +++ b/src/core/Akka.Cluster/ClusterDaemon.cs @@ -816,6 +816,8 @@ public ClusterEvent.IClusterDomainEvent Event /// internal sealed class ClusterDaemon : ReceiveActor, IRequiresMessageQueue { + private Cluster _cluster; + private IActorRef _coreSupervisor; private readonly ClusterSettings _settings; private readonly ILoggingAdapter _log = Context.GetLogger(); @@ -841,21 +843,21 @@ public ClusterDaemon(ClusterSettings settings) Receive(msg => { if (_coreSupervisor == null) - CreateChildren(); + CreateChildren(msg.Cluster); _coreSupervisor.Forward(msg); }); Receive(msg => { Context.ActorOf( - Props.Create(() => new OnMemberStatusChangedListener(msg.Callback, MemberStatus.Up)) + Props.Create(() => new OnMemberStatusChangedListener(_cluster, msg.Callback, MemberStatus.Up)) .WithDeploy(Deploy.Local)); }); Receive(msg => { Context.ActorOf( - Props.Create(() => new OnMemberStatusChangedListener(msg.Callback, MemberStatus.Removed)) + Props.Create(() => new OnMemberStatusChangedListener(_cluster, msg.Callback, MemberStatus.Removed)) .WithDeploy(Deploy.Local)); }); @@ -874,7 +876,7 @@ private void AddCoordinatedLeave() var self = Self; _coordShutdown.AddTask(CoordinatedShutdown.PhaseClusterLeave, "leave", () => { - if (Cluster.Get(sys).IsTerminated || Cluster.Get(sys).SelfMember.Status == MemberStatus.Down) + if (_cluster is null || _cluster.IsTerminated || _cluster.SelfMember.Status == MemberStatus.Down) { return Task.FromResult(Done.Instance); } @@ -888,11 +890,12 @@ private void AddCoordinatedLeave() _coordShutdown.AddTask(CoordinatedShutdown.PhaseClusterShutdown, "wait-shutdown", () => _clusterPromise.Task); } - private void CreateChildren() + private void CreateChildren(Cluster cluster) { + _cluster = cluster; _coreSupervisor = Context.ActorOf(Props.Create(), "core"); - Context.ActorOf(ClusterHeartbeatReceiver.Props(() => Cluster.Get(Context.System)), "heartbeatReceiver"); + Context.ActorOf(ClusterHeartbeatReceiver.Props(() => _cluster), "heartbeatReceiver"); } protected override void PostStop() @@ -1483,11 +1486,13 @@ public void JoinSeedNodes(ImmutableList
newSeedNodes) _seedNodeProcessCounter += 1; if (newSeedNodes.Head().Equals(_cluster.SelfAddress)) { - _seedNodeProcess = Context.ActorOf(Props.Create(() => new FirstSeedNodeProcess(newSeedNodes)).WithDispatcher(_cluster.Settings.UseDispatcher), "firstSeedNodeProcess-" + _seedNodeProcessCounter); + _seedNodeProcess = Context.ActorOf(Props.Create(() => new FirstSeedNodeProcess(_cluster, newSeedNodes)) + .WithDispatcher(_cluster.Settings.UseDispatcher), "firstSeedNodeProcess-" + _seedNodeProcessCounter); } else { - _seedNodeProcess = Context.ActorOf(Props.Create(() => new JoinSeedNodeProcess(newSeedNodes)).WithDispatcher(_cluster.Settings.UseDispatcher), "joinSeedNodeProcess-" + _seedNodeProcessCounter); + _seedNodeProcess = Context.ActorOf(Props.Create(() => new JoinSeedNodeProcess(_cluster, newSeedNodes)) + .WithDispatcher(_cluster.Settings.UseDispatcher), "joinSeedNodeProcess-" + _seedNodeProcessCounter); } } } @@ -2695,19 +2700,20 @@ internal sealed class JoinSeedNodeProcess : UntypedActor /// /// TBD /// + /// TBD /// TBD /// /// This exception is thrown when either the list of specified is empty /// or the first listed seed is a reference to the IUntypedActorContext.System's address. /// - public JoinSeedNodeProcess(ImmutableList
seeds) + public JoinSeedNodeProcess(Cluster cluster, ImmutableList
seeds) { - _selfAddress = Cluster.Get(Context.System).SelfAddress; + _selfAddress = cluster.SelfAddress; _seeds = seeds; _otherSeeds = _seeds.Remove(_selfAddress); if (seeds.IsEmpty || seeds.Head() == _selfAddress) throw new ArgumentException("Join seed node should not be done"); - Context.SetReceiveTimeout(Cluster.Get(Context.System).Settings.SeedNodeTimeout); + Context.SetReceiveTimeout(cluster.Settings.SeedNodeTimeout); } /// @@ -2800,14 +2806,15 @@ internal sealed class FirstSeedNodeProcess : UntypedActor /// /// Launches a new instance of the "first seed node" joining process. /// + /// TBD /// The set of seed nodes to join. /// /// This exception is thrown when either the number of specified is less than or equal to 1 /// or the first listed seed is a reference to the IUntypedActorContext.System's address. /// - public FirstSeedNodeProcess(ImmutableList
seeds) + public FirstSeedNodeProcess(Cluster cluster, ImmutableList
seeds) { - _cluster = Cluster.Get(Context.System); + _cluster = cluster; _selfAddress = _cluster.SelfAddress; if (seeds.Count <= 1 || seeds.Head() != _selfAddress) @@ -3043,17 +3050,18 @@ private Type To /// /// TBD /// + /// TBD /// TBD /// TBD /// /// This exception is thrown when the specified is invalid. /// Acceptable values are: | . /// - public OnMemberStatusChangedListener(Action callback, MemberStatus targetStatus) + public OnMemberStatusChangedListener(Cluster cluster, Action callback, MemberStatus targetStatus) { + _cluster = cluster; _callback = callback; _status = targetStatus; - _cluster = Cluster.Get(Context.System); Receive(state => { diff --git a/src/core/Akka.Cluster/SBR/SplitBrainResolverProvider.cs b/src/core/Akka.Cluster/SBR/SplitBrainResolverProvider.cs index 4ad2253249d..b150976820a 100644 --- a/src/core/Akka.Cluster/SBR/SplitBrainResolverProvider.cs +++ b/src/core/Akka.Cluster/SBR/SplitBrainResolverProvider.cs @@ -47,8 +47,6 @@ public Props DowningActorProps { get { - var cluster = Cluster.Get(system); - DowningStrategy strategy; switch (settings.DowningStrategy) { @@ -68,7 +66,7 @@ public Props DowningActorProps break; case SplitBrainResolverSettings.LeaseMajorityName: var lms = settings.LeaseMajoritySettings; - var leaseOwnerName = cluster.SelfUniqueAddress.Address.HostPort(); + var leaseOwnerName = Cluster.Get(system).SelfUniqueAddress.Address.HostPort(); var leaseName = lms.SafeLeaseName(system.Name); var lease = LeaseProvider.Get(system).GetLease(leaseName, lms.LeaseImplementation, leaseOwnerName); diff --git a/src/core/Akka.Cluster/SplitBrainResolver.cs b/src/core/Akka.Cluster/SplitBrainResolver.cs index 2124eb1e7d6..0eb10106828 100644 --- a/src/core/Akka.Cluster/SplitBrainResolver.cs +++ b/src/core/Akka.Cluster/SplitBrainResolver.cs @@ -17,11 +17,11 @@ namespace Akka.Cluster { public sealed class SplitBrainResolver : IDowningProvider { - private readonly ClusterSettings _clusterSettings; + private readonly ActorSystem _system; public SplitBrainResolver(ActorSystem system) { - _clusterSettings = Cluster.Get(system).Settings; + _system = system; var config = system.Settings.Config.GetConfig("akka.cluster.split-brain-resolver"); if (config.IsNullOrEmpty()) throw ConfigurationException.NullOrEmptyConfig("akka.cluster.split-brain-resolver"); @@ -30,7 +30,7 @@ public SplitBrainResolver(ActorSystem system) Strategy = ResolveSplitBrainStrategy(config); } - public TimeSpan DownRemovalMargin => _clusterSettings.DownRemovalMargin; + public TimeSpan DownRemovalMargin => Cluster.Get(_system).Settings.DownRemovalMargin; public TimeSpan StableAfter { get; } public Props DowningActorProps => SplitBrainDecider.Props(StableAfter, Strategy);