Skip to content

Commit 51c446d

Browse files
committed
enable ChannelTaskScheduler to work inside Akka.Cluster without causing errors inside /system actors (akkadotnet#5861)
* close akkadotnet#5498 enable `ChannelTaskScheduler` to work inside Akka.Cluster without causing errors inside `/system` actors * fix `HeartbeatSender` * cleaned up SBR internals (style) * cleaned up some comments * asynchronously attempt to acquire `Cluster` inside SBR * fixed SBR compilation * Update SplitBrainResolver.cs * subscribe on PreStart
1 parent c2460eb commit 51c446d

File tree

7 files changed

+80
-86
lines changed

7 files changed

+80
-86
lines changed

src/core/Akka.Cluster.Tests/ClusterHeartbeatReceiverSpec.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public ClusterHeartbeatReceiverSpec(ITestOutputHelper output)
2727
[Fact]
2828
public void ClusterHeartbeatReceiver_should_respond_to_heartbeats_with_same_SeqNo_and_SendTime()
2929
{
30-
var heartbeater = Sys.ActorOf(ClusterHeartbeatReceiver.Props(() => Cluster.Get(Sys)));
30+
var heartbeater = Sys.ActorOf(ClusterHeartbeatReceiver.Props(Cluster.Get(Sys)));
3131
heartbeater.Tell(new Heartbeat(Cluster.Get(Sys).SelfAddress, 1, 2));
3232
ExpectMsg<HeartbeatRsp>(new HeartbeatRsp(Cluster.Get(Sys).SelfUniqueAddress, 1, 2));
3333
}

src/core/Akka.Cluster.Tests/ClusterHeartbeatSenderSpec.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class TestClusterHeartbeatSender : ClusterHeartbeatSender
2323
{
2424
private readonly TestProbe _probe;
2525

26-
public TestClusterHeartbeatSender(TestProbe probe)
26+
public TestClusterHeartbeatSender(TestProbe probe) : base(Cluster.Get(Context.System))
2727
{
2828
_probe = probe;
2929
}

src/core/Akka.Cluster/Cluster.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ public ImmutableHashSet<string> SelfRoles
520520
public DefaultFailureDetectorRegistry<Address> FailureDetector { get; }
521521

522522
/// <summary>
523-
/// TBD
523+
/// The downing provider used to execute automatic downing inside Akka.Cluster.
524524
/// </summary>
525525
public IDowningProvider DowningProvider => _downingProvider.Value;
526526

@@ -535,7 +535,6 @@ public ImmutableHashSet<string> SelfRoles
535535

536536
private static IScheduler CreateScheduler(ActorSystem system)
537537
{
538-
//TODO: Whole load of stuff missing here!
539538
return system.Scheduler;
540539
}
541540

src/core/Akka.Cluster/ClusterDaemon.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ private void CreateChildren(Cluster cluster)
895895
_cluster = cluster;
896896
_coreSupervisor = Context.ActorOf(Props.Create<ClusterCoreSupervisor>(), "core");
897897

898-
Context.ActorOf(ClusterHeartbeatReceiver.Props(() => _cluster), "heartbeatReceiver");
898+
Context.ActorOf(ClusterHeartbeatReceiver.Props(cluster), "heartbeatReceiver");
899899
}
900900

901901
protected override void PostStop()
@@ -1333,7 +1333,7 @@ private void BecomeInitialized()
13331333
{
13341334
// start heartbeatSender here, and not in constructor to make sure that
13351335
// heartbeating doesn't start before Welcome is received
1336-
Context.ActorOf(Props.Create<ClusterHeartbeatSender>().WithDispatcher(_cluster.Settings.UseDispatcher),
1336+
Context.ActorOf(Props.Create(() => new ClusterHeartbeatSender(_cluster)).WithDispatcher(_cluster.Settings.UseDispatcher),
13371337
"heartbeatSender");
13381338
// make sure that join process is stopped
13391339
StopSeedNodeProcess();

src/core/Akka.Cluster/ClusterHeartbeat.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ internal sealed class ClusterHeartbeatReceiver : UntypedActor
2626
{
2727
// Important - don't use Cluster.Get(Context.System) in constructor because that would
2828
// cause deadlock. See startup sequence in ClusterDaemon.
29-
private readonly Lazy<Cluster> _cluster;
29+
private readonly Cluster _cluster;
3030

31-
public bool VerboseHeartbeat => _cluster.Value.Settings.VerboseHeartbeatLogging;
31+
public bool VerboseHeartbeat => _cluster.Settings.VerboseHeartbeatLogging;
3232

3333
/// <summary>
3434
/// TBD
3535
/// </summary>
36-
public ClusterHeartbeatReceiver(Func<Cluster> getCluster)
36+
public ClusterHeartbeatReceiver(Cluster cluster)
3737
{
38-
_cluster = new Lazy<Cluster>(getCluster);
38+
_cluster = cluster;
3939
}
4040

4141
protected override void OnReceive(object message)
@@ -44,8 +44,8 @@ protected override void OnReceive(object message)
4444
{
4545
case ClusterHeartbeatSender.Heartbeat hb:
4646
// TODO log the sequence nr once serializer is enabled
47-
if(VerboseHeartbeat) _cluster.Value.CurrentInfoLogger.LogDebug("Heartbeat from [{0}]", hb.From);
48-
Sender.Tell(new ClusterHeartbeatSender.HeartbeatRsp(_cluster.Value.SelfUniqueAddress,
47+
if(VerboseHeartbeat) _cluster.CurrentInfoLogger.LogDebug("Heartbeat from [{0}]", hb.From);
48+
Sender.Tell(new ClusterHeartbeatSender.HeartbeatRsp(_cluster.SelfUniqueAddress,
4949
hb.SequenceNr, hb.CreationTimeNanos));
5050
break;
5151
default:
@@ -54,7 +54,7 @@ protected override void OnReceive(object message)
5454
}
5555
}
5656

57-
public static Props Props(Func<Cluster> getCluster)
57+
public static Props Props(Cluster getCluster)
5858
{
5959
return Akka.Actor.Props.Create(() => new ClusterHeartbeatReceiver(getCluster));
6060
}
@@ -76,11 +76,12 @@ internal class ClusterHeartbeatSender : ReceiveActor
7676
private DateTime _tickTimestamp;
7777

7878
/// <summary>
79-
/// TBD
79+
/// Create a new instance of the <see cref="ClusterHeartbeatSender"/> and pass in a reference to the <see cref="Cluster"/>
80+
/// to which it belongs.
8081
/// </summary>
81-
public ClusterHeartbeatSender()
82+
public ClusterHeartbeatSender(Cluster cluster)
8283
{
83-
_cluster = Cluster.Get(Context.System);
84+
_cluster = cluster;
8485
var tickInitialDelay = _cluster.Settings.PeriodicTasksInitialDelay.Max(_cluster.Settings.HeartbeatInterval);
8586
_tickTimestamp = DateTime.UtcNow + tickInitialDelay;
8687

0 commit comments

Comments
 (0)