fix(cluster): record RoleLeaderChanged leader from the event, not the lagging state snapshot (#590)

ClusterRoleInfo resolved the leader address through Cluster.State.Members, which is
published asynchronously; a miss stored null permanently, so RoleLeader(role) stayed null
(flaky test timed out under load: 6/10 failing with CPU contention, 0/20 after). Only the
address is ever read, so store it directly. Adds a deterministic unit test (fails without
the fix) and InternalsVisibleTo for the Cluster.Tests project.

Claude-Session: https://claude.ai/code/session_01Ef2ozxS7QPanGT8uoATbTe
This commit is contained in:
Joseph Doherty
2026-10-08 00:25:57 -04:00
parent 390c8269a0
commit 129b84fd69
3 changed files with 56 additions and 15 deletions
@@ -126,4 +126,43 @@ public sealed class ClusterRoleInfoRoleLeaderCaseTests
hostA.Dispose();
}
}
/// <summary>
/// Deterministic pin for Gitea #590: a RoleLeaderChanged event whose leader address is not (yet)
/// in <c>Cluster.State.Members</c> must still record that leader. The handler used to resolve the
/// address through the state snapshot, which is published asynchronously and can lag the event;
/// a miss stored null permanently. Here the cluster never joins anything, so the leader address
/// is guaranteed absent from the snapshot — the lagging-snapshot case, made exact.
/// </summary>
[Fact]
public async Task RoleLeaderChanged_is_recorded_even_when_the_leader_is_not_in_the_state_snapshot()
{
var port = FreePort();
var options = new AkkaClusterOptions
{
SystemName = SystemName, Hostname = "127.0.0.1", PublicHostname = "127.0.0.1",
Port = port, Roles = new[] { "driver" }, SeedNodes = Array.Empty<string>(),
};
var system = Akka.Actor.ActorSystem.Create(SystemName, Akka.Configuration.ConfigurationFactory.ParseString(
"akka.actor.provider = cluster\nakka.remote.dot-netty.tcp.hostname = 127.0.0.1\n"
+ $"akka.remote.dot-netty.tcp.port = {port}"));
try
{
using var info = new ClusterRoleInfo(
system, Options.Create(options),
Microsoft.Extensions.Logging.Abstractions.NullLogger<ClusterRoleInfo>.Instance);
var driver = new Akka.Actor.Address("akka.tcp", SystemName, "127.0.0.1", 11111);
var capital = new Akka.Actor.Address("akka.tcp", SystemName, "127.0.0.1", 22222);
info.HandleRoleLeaderChanged(new Akka.Cluster.ClusterEvent.RoleLeaderChanged("driver", driver));
info.HandleRoleLeaderChanged(new Akka.Cluster.ClusterEvent.RoleLeaderChanged("Driver", capital));
info.RoleLeader("driver")!.Value.ToString().ShouldContain("11111");
info.RoleLeader("Driver")!.Value.ToString().ShouldContain("22222");
}
finally
{
await system.Terminate();
}
}
}