Skip to content

Commit d178b63

Browse files
clean up seed node process (#4975)
1 parent 321d0e4 commit d178b63

File tree

1 file changed

+73
-63
lines changed

1 file changed

+73
-63
lines changed

src/core/Akka.Cluster/ClusterDaemon.cs

Lines changed: 73 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2691,6 +2691,7 @@ internal sealed class JoinSeedNodeProcess : UntypedActor
26912691
private readonly ILoggingAdapter _log = Context.GetLogger();
26922692

26932693
private readonly ImmutableList<Address> _seeds;
2694+
private readonly ImmutableList<Address> _otherSeeds;
26942695
private readonly Address _selfAddress;
26952696
private int _attempts = 0;
26962697

@@ -2706,6 +2707,7 @@ public JoinSeedNodeProcess(ImmutableList<Address> seeds)
27062707
{
27072708
_selfAddress = Cluster.Get(Context.System).SelfAddress;
27082709
_seeds = seeds;
2710+
_otherSeeds = _seeds.Remove(_selfAddress);
27092711
if (seeds.IsEmpty || seeds.Head() == _selfAddress)
27102712
throw new ArgumentException("Join seed node should not be done");
27112713
Context.SetReceiveTimeout(Cluster.Get(Context.System).Settings.SeedNodeTimeout);
@@ -2725,46 +2727,53 @@ protected override void PreStart()
27252727
/// <param name="message">TBD</param>
27262728
protected override void OnReceive(object message)
27272729
{
2728-
if (message is InternalClusterAction.JoinSeenNode)
2730+
switch (message)
27292731
{
2730-
//send InitJoin to all seed nodes (except myself)
2731-
foreach (var path in _seeds.Where(x => x != _selfAddress)
2732-
.Select(y => Context.ActorSelection(Context.Parent.Path.ToStringWithAddress(y))))
2732+
case InternalClusterAction.JoinSeenNode _:
27332733
{
2734-
path.Tell(new InternalClusterAction.InitJoin());
2734+
//send InitJoin to all seed nodes (except myself)
2735+
foreach (var path in _otherSeeds
2736+
.Select(y => Context.ActorSelection(Context.Parent.Path.ToStringWithAddress(y))))
2737+
{
2738+
path.Tell(new InternalClusterAction.InitJoin());
2739+
}
2740+
_attempts++;
2741+
break;
27352742
}
2736-
_attempts++;
2737-
}
2738-
else if (message is InternalClusterAction.InitJoinAck)
2739-
{
2740-
//first InitJoinAck reply
2741-
var initJoinAck = (InternalClusterAction.InitJoinAck)message;
2742-
Context.Parent.Tell(new ClusterUserAction.JoinTo(initJoinAck.Address));
2743-
Context.Become(Done);
2744-
}
2745-
else if (message is InternalClusterAction.InitJoinNack) { } //that seed was uninitialized
2746-
else if (message is ReceiveTimeout)
2747-
{
2748-
if (_attempts >= 2)
2749-
_log.Warning(
2750-
"Couldn't join seed nodes after [{0}] attempts, will try again. seed-nodes=[{1}]",
2751-
_attempts, string.Join(",", _seeds.Where(x => !x.Equals(_selfAddress))));
2752-
//no InitJoinAck received - try again
2753-
Self.Tell(new InternalClusterAction.JoinSeenNode());
2754-
}
2755-
else
2756-
{
2757-
Unhandled(message);
2743+
case InternalClusterAction.InitJoinAck initJoinAck:
2744+
//first InitJoinAck reply
2745+
Context.Parent.Tell(new ClusterUserAction.JoinTo(initJoinAck.Address));
2746+
Context.Become(Done);
2747+
break;
2748+
case InternalClusterAction.InitJoinNack _:
2749+
break; //that seed was uninitialized
2750+
case ReceiveTimeout _:
2751+
{
2752+
if (_attempts >= 2)
2753+
_log.Warning(
2754+
"Couldn't join seed nodes after [{0}] attempts, will try again. seed-nodes=[{1}]",
2755+
_attempts, string.Join(",", _seeds.Where(x => !x.Equals(_selfAddress))));
2756+
//no InitJoinAck received - try again
2757+
Self.Tell(new InternalClusterAction.JoinSeenNode());
2758+
break;
2759+
}
2760+
default:
2761+
Unhandled(message);
2762+
break;
27582763
}
27592764
}
27602765

27612766
private void Done(object message)
27622767
{
2763-
if (message is InternalClusterAction.InitJoinAck)
2768+
switch (message)
27642769
{
2765-
//already received one, skip the rest
2770+
case InternalClusterAction.InitJoinAck _:
2771+
//already received one, skip the rest
2772+
break;
2773+
case ReceiveTimeout _:
2774+
Context.Stop(Self);
2775+
break;
27662776
}
2767-
else if (message is ReceiveTimeout) Context.Stop(Self);
27682777
}
27692778
}
27702779

@@ -2784,16 +2793,17 @@ internal sealed class FirstSeedNodeProcess : UntypedActor
27842793
{
27852794
private readonly ILoggingAdapter _log = Context.GetLogger();
27862795

2796+
private readonly ImmutableList<Address> _seeds;
27872797
private ImmutableList<Address> _remainingSeeds;
27882798
private readonly Address _selfAddress;
27892799
private readonly Cluster _cluster;
27902800
private readonly Deadline _timeout;
27912801
private readonly ICancelable _retryTaskToken;
27922802

27932803
/// <summary>
2794-
/// TBD
2804+
/// Launches a new instance of the "first seed node" joining process.
27952805
/// </summary>
2796-
/// <param name="seeds">TBD</param>
2806+
/// <param name="seeds">The set of seed nodes to join.</param>
27972807
/// <exception cref="ArgumentException">
27982808
/// This exception is thrown when either the number of specified <paramref name="seeds"/> is less than or equal to 1
27992809
/// or the first listed seed is a reference to the <see cref="IActorContext.System">IUntypedActorContext.System</see>'s address.
@@ -2806,63 +2816,63 @@ public FirstSeedNodeProcess(ImmutableList<Address> seeds)
28062816
if (seeds.Count <= 1 || seeds.Head() != _selfAddress)
28072817
throw new ArgumentException("Join seed node should not be done");
28082818

2819+
_seeds = seeds;
28092820
_remainingSeeds = seeds.Remove(_selfAddress);
28102821
_timeout = Deadline.Now + _cluster.Settings.SeedNodeTimeout;
28112822
_retryTaskToken = Context.System.Scheduler.ScheduleTellRepeatedlyCancelable(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1), Self, new InternalClusterAction.JoinSeenNode(), Self);
28122823
Self.Tell(new InternalClusterAction.JoinSeenNode());
28132824
}
28142825

2815-
/// <summary>
2816-
/// TBD
2817-
/// </summary>
28182826
protected override void PostStop()
28192827
{
28202828
_retryTaskToken.Cancel();
28212829
}
28222830

2823-
/// <summary>
2824-
/// TBD
2825-
/// </summary>
2826-
/// <param name="message">TBD</param>
28272831
protected override void OnReceive(object message)
28282832
{
2829-
if (message is InternalClusterAction.JoinSeenNode)
2833+
switch (message)
28302834
{
2831-
if (_timeout.HasTimeLeft)
2835+
case InternalClusterAction.JoinSeenNode _ when _timeout.HasTimeLeft:
28322836
{
28332837
// send InitJoin to remaining seed nodes (except myself)
28342838
foreach (var seed in _remainingSeeds.Select(
2835-
x => Context.ActorSelection(Context.Parent.Path.ToStringWithAddress(x))))
2839+
x => Context.ActorSelection(Context.Parent.Path.ToStringWithAddress(x))))
28362840
seed.Tell(new InternalClusterAction.InitJoin());
2841+
break;
28372842
}
2838-
else
2843+
case InternalClusterAction.JoinSeenNode _:
28392844
{
2845+
if (_log.IsDebugEnabled)
2846+
{
2847+
_log.Debug("Couldn't join other seed nodes, will join myself. seed-nodes=[{0}]", string.Join(",", _seeds));
2848+
}
28402849
// no InitJoinAck received, initialize new cluster by joining myself
28412850
Context.Parent.Tell(new ClusterUserAction.JoinTo(_selfAddress));
28422851
Context.Stop(Self);
2852+
break;
28432853
}
2844-
}
2845-
else if (message is InternalClusterAction.InitJoinAck)
2846-
{
2847-
// first InitJoinAck reply, join existing cluster
2848-
var initJoinAck = (InternalClusterAction.InitJoinAck)message;
2849-
Context.Parent.Tell(new ClusterUserAction.JoinTo(initJoinAck.Address));
2850-
Context.Stop(Self);
2851-
}
2852-
else if (message is InternalClusterAction.InitJoinNack)
2853-
{
2854-
var initJoinNack = (InternalClusterAction.InitJoinNack)message;
2855-
_remainingSeeds = _remainingSeeds.Remove(initJoinNack.Address);
2856-
if (_remainingSeeds.IsEmpty)
2857-
{
2858-
// initialize new cluster by joining myself when nacks from all other seed nodes
2859-
Context.Parent.Tell(new ClusterUserAction.JoinTo(_selfAddress));
2854+
case InternalClusterAction.InitJoinAck initJoinAck:
2855+
_log.Info("Received InitJoinAck message from [{0}] to [{1}]", initJoinAck.Address, _selfAddress);
2856+
// first InitJoinAck reply, join existing cluster
2857+
Context.Parent.Tell(new ClusterUserAction.JoinTo(initJoinAck.Address));
28602858
Context.Stop(Self);
2859+
break;
2860+
case InternalClusterAction.InitJoinNack initJoinNack:
2861+
{
2862+
_log.Info("Received InitJoinNack message from [{0}] to [{1}]", initJoinNack.Address, _selfAddress);
2863+
_remainingSeeds = _remainingSeeds.Remove(initJoinNack.Address);
2864+
if (_remainingSeeds.IsEmpty)
2865+
{
2866+
// initialize new cluster by joining myself when nacks from all other seed nodes
2867+
Context.Parent.Tell(new ClusterUserAction.JoinTo(_selfAddress));
2868+
Context.Stop(Self);
2869+
}
2870+
2871+
break;
28612872
}
2862-
}
2863-
else
2864-
{
2865-
Unhandled(message);
2873+
default:
2874+
Unhandled(message);
2875+
break;
28662876
}
28672877
}
28682878
}

0 commit comments

Comments
 (0)