Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix cluster startup race #5185

Merged
merged 7 commits into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public ClusterDomainEventPublisherSpec() : base(Config)
Sys.EventStream.Subscribe(_memberSubscriber.Ref, typeof(ClusterEvent.LeaderChanged));
Sys.EventStream.Subscribe(_memberSubscriber.Ref, typeof(ClusterEvent.ClusterShuttingDown));

_publisher = Sys.ActorOf(Props.Create<ClusterDomainEventPublisher>());
var selfUniqueAddress = Cluster.Get(Sys).SelfUniqueAddress;
_publisher = Sys.ActorOf(Props.Create(() => new ClusterDomainEventPublisher(selfUniqueAddress)));
_publisher.Tell(new InternalClusterAction.PublishChanges(state0));
_memberSubscriber.ExpectMsg(new ClusterEvent.MemberUp(aUp));
_memberSubscriber.ExpectMsg(new ClusterEvent.LeaderChanged(aUp.Address));
Expand Down
2 changes: 1 addition & 1 deletion src/core/Akka.Cluster/Cluster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private async Task<IActorRef> GetClusterCoreRef()
var timeout = System.Settings.CreationTimeout;
try
{
return await _clusterDaemons.Ask<IActorRef>(InternalClusterAction.GetClusterCoreRef.Instance, timeout).ConfigureAwait(false);
return await _clusterDaemons.Ask<IActorRef>(new InternalClusterAction.GetClusterCoreRef(this), timeout).ConfigureAwait(false);
}
catch (Exception ex)
{
Expand Down
40 changes: 18 additions & 22 deletions src/core/Akka.Cluster/ClusterDaemon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -583,21 +583,14 @@ public override int GetHashCode()
/// <summary>
/// Gets a reference to the cluster core daemon.
/// </summary>
internal class GetClusterCoreRef
internal class GetClusterCoreRef: INoSerializationVerificationNeeded
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call

{
private GetClusterCoreRef() { }
private static readonly GetClusterCoreRef _instance = new GetClusterCoreRef();

/// <summary>
/// The singleton instance
/// </summary>
public static GetClusterCoreRef Instance
public GetClusterCoreRef(Cluster cluster)
{
get
{
return _instance;
}
Cluster = cluster;
}

public Cluster Cluster { get; }
}

/// <summary>
Expand Down Expand Up @@ -919,6 +912,8 @@ protected override void PostStop()
/// </summary>
internal class ClusterCoreSupervisor : ReceiveActor, IRequiresMessageQueue<IUnboundedMessageQueueSemantics>
{
private Cluster _cluster;

private IActorRef _publisher;
private IActorRef _coreDaemon;

Expand All @@ -937,7 +932,7 @@ public ClusterCoreSupervisor()
Receive<InternalClusterAction.GetClusterCoreRef>(cr =>
{
if (_coreDaemon == null)
CreateChildren();
CreateChildren(cr.Cluster);
Sender.Tell(_coreDaemon);
});
}
Expand All @@ -952,7 +947,7 @@ protected override SupervisorStrategy SupervisorStrategy()
{
//TODO: JVM version matches NonFatal. Can / should we do something similar?
_log.Error(e, "Cluster node [{0}] crashed, [{1}] - shutting down...",
Cluster.Get(Context.System).SelfAddress, e);
_cluster?.SelfAddress, e);
Self.Tell(PoisonPill.Instance);
return Directive.Stop;
});
Expand All @@ -963,14 +958,15 @@ protected override SupervisorStrategy SupervisorStrategy()
/// </summary>
protected override void PostStop()
{
Cluster.Get(Context.System).Shutdown();
_cluster?.Shutdown();
}

private void CreateChildren()
private void CreateChildren(Cluster cluster)
{
_cluster = cluster;
_publisher =
Context.ActorOf(Props.Create<ClusterDomainEventPublisher>().WithDispatcher(Context.Props.Dispatcher), "publisher");
_coreDaemon = Context.ActorOf(Props.Create(() => new ClusterCoreDaemon(_publisher)).WithDispatcher(Context.Props.Dispatcher), "daemon");
Context.ActorOf(Props.Create(() => new ClusterDomainEventPublisher(_cluster.SelfUniqueAddress)).WithDispatcher(Context.Props.Dispatcher), "publisher");
_coreDaemon = Context.ActorOf(Props.Create(() => new ClusterCoreDaemon(_cluster, _publisher)).WithDispatcher(Context.Props.Dispatcher), "daemon");
Context.Watch(_coreDaemon);
}
}
Expand Down Expand Up @@ -1025,10 +1021,11 @@ internal static string VclockName(UniqueAddress node)
/// <summary>
/// Creates a new cluster core daemon instance.
/// </summary>
/// <param name="cluster">A reference to the <see cref="Cluster"/>.</param>
/// <param name="publisher">A reference to the <see cref="ClusterDomainEventPublisher"/>.</param>
public ClusterCoreDaemon(IActorRef publisher)
public ClusterCoreDaemon(Cluster cluster, IActorRef publisher)
{
_cluster = Cluster.Get(Context.System);
_cluster = cluster;
_membershipState = new MembershipState(Gossip.Empty, _cluster.SelfUniqueAddress);
_publisher = publisher;
SelfUniqueAddress = _cluster.SelfUniqueAddress;
Expand Down Expand Up @@ -1086,7 +1083,6 @@ public ClusterCoreDaemon(IActorRef publisher)

private void AddCoordinatedLeave()
{
var sys = Context.System;
var self = Self;
_coordShutdown.AddTask(CoordinatedShutdown.PhaseClusterExiting, "wait-exiting", () =>
{
Expand All @@ -1097,7 +1093,7 @@ private void AddCoordinatedLeave()
});
_coordShutdown.AddTask(CoordinatedShutdown.PhaseClusterExitingDone, "exiting-completed", () =>
{
if (Cluster.Get(sys).IsTerminated || Cluster.Get(sys).SelfMember.Status == MemberStatus.Down)
if (_cluster.IsTerminated || _cluster.SelfMember.Status == MemberStatus.Down)
return TaskEx.Completed;
else
{
Expand Down
8 changes: 5 additions & 3 deletions src/core/Akka.Cluster/ClusterEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -993,15 +993,17 @@ internal static ImmutableList<ReachabilityChanged> DiffReachability(MembershipSt
/// </summary>
internal sealed class ClusterDomainEventPublisher : ReceiveActor, IRequiresMessageQueue<IUnboundedMessageQueueSemantics>
{
private readonly UniqueAddress _selfUniqueAddress = Cluster.Get(Context.System).SelfUniqueAddress;
private readonly MembershipState _emptyMembershipState = new MembershipState(Gossip.Empty, Cluster.Get(Context.System).SelfUniqueAddress);
private readonly UniqueAddress _selfUniqueAddress;
private readonly MembershipState _emptyMembershipState;
private MembershipState _membershipState;

/// <summary>
/// Default constructor for ClusterDomainEventPublisher.
/// </summary>
public ClusterDomainEventPublisher()
public ClusterDomainEventPublisher(UniqueAddress selfUniqueAddress)
{
_selfUniqueAddress = selfUniqueAddress;
_emptyMembershipState = new MembershipState(Gossip.Empty, selfUniqueAddress);
_eventStream = Context.System.EventStream;
_membershipState = _emptyMembershipState;

Expand Down