Skip to content

Commit 8b1d54b

Browse files
authored
Bring ShardedDaemonProcess up to date (#4571)
* Check spec does not create too many demons * Convenience for ShardedDaemonProcess role
1 parent 8c5d6cc commit 8b1d54b

File tree

5 files changed

+30
-14
lines changed

5 files changed

+30
-14
lines changed

src/contrib/cluster/Akka.Cluster.Sharding.Tests.MultiNode/ShardedDaemonProcessSpec.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void ShardedDaemonProcess_Should_Init_Actor_Set()
7575

7676
var probe = CreateTestProbe();
7777
ShardedDaemonProcess.Get(Sys).Init("the-fearless", 4, id => ProcessActor.Props(id, probe.Ref));
78-
EnterBarrier("actor-set-initialized");
78+
EnterBarrier("sharded-daemon-process-initialized");
7979

8080
RunOn(() =>
8181
{
@@ -87,7 +87,7 @@ public void ShardedDaemonProcess_Should_Init_Actor_Set()
8787
}).ToList();
8888
startedIds.Count.Should().Be(4);
8989
}, _config.First);
90-
EnterBarrier("actor-set-started");
90+
EnterBarrier("sharded-daemon-process-started");
9191
}
9292

9393
private void FormCluster(RoleName first, params RoleName[] rest)

src/contrib/cluster/Akka.Cluster.Sharding.Tests/ShardedDaemonProcessSpec.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public void ShardedDaemonProcess_must_start_N_actors_with_unique_ids()
100100

101101
var started = probe.ReceiveN(5);
102102
started.Count.ShouldBe(5);
103+
probe.ExpectNoMsg();
103104
}
104105

105106
[Fact]
@@ -123,7 +124,7 @@ public void ShardedDaemonProcess_must_not_run_if_the_role_does_not_match_node_ro
123124
Cluster.Get(Sys).Join(Cluster.Get(Sys).SelfAddress);
124125

125126
var probe = CreateTestProbe();
126-
var settings = ShardedDaemonProcessSettings.Create(Sys).WithShardingSettings(ClusterShardingSettings.Create(Sys).WithRole("workers"));
127+
var settings = ShardedDaemonProcessSettings.Create(Sys).WithRole("workers");
127128
ShardedDaemonProcess.Get(Sys).Init("roles", 3, id => MyActor.Props(id, probe.Ref), settings);
128129

129130
probe.ExpectNoMsg();

src/contrib/cluster/Akka.Cluster.Sharding/ShardedDaemonProcess.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public void Init(string name, int numberOfInstances, Func<int, Props> propsFacto
138138
var numberOfShards = numberOfInstances;
139139
var entityIds = Enumerable.Range(0, numberOfInstances).Select(i => i.ToString()).ToArray();
140140

141-
// Defaults in `akka.cluster.sharding` but allow overrides specifically for actor-set
141+
// Defaults in `akka.cluster.sharding` but allow overrides specifically for sharded-daemon-process
142142
var shardingBaseSettings = settings.ShardingSettings;
143143
if (shardingBaseSettings == null)
144144
{
@@ -148,7 +148,7 @@ public void Init(string name, int numberOfInstances, Func<int, Props> propsFacto
148148
}
149149

150150
var shardingSettings = new ClusterShardingSettings(
151-
shardingBaseSettings.Role,
151+
settings.Role ?? shardingBaseSettings.Role,
152152
false, // remember entities disabled
153153
"",
154154
"",

src/contrib/cluster/Akka.Cluster.Sharding/ShardedDaemonProcessSettings.cs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public sealed class ShardedDaemonProcessSettings
2626
/// </summary>
2727
public readonly ClusterShardingSettings ShardingSettings;
2828

29+
/// <summary>
30+
/// Specifies that the ShardedDaemonProcess should run on nodes with a specific role.
31+
/// </summary>
32+
public readonly string Role;
33+
2934
/// <summary>
3035
/// Create default settings for system
3136
/// </summary>
@@ -43,30 +48,38 @@ public static ShardedDaemonProcessSettings FromConfig(Config config)
4348
/// <summary>
4449
/// Not for user constructions, use factory methods to instantiate.
4550
/// </summary>
46-
private ShardedDaemonProcessSettings(TimeSpan keepAliveInterval, ClusterShardingSettings shardingSettings = null)
51+
private ShardedDaemonProcessSettings(TimeSpan keepAliveInterval, ClusterShardingSettings shardingSettings = null, string role = null)
4752
{
4853
KeepAliveInterval = keepAliveInterval;
4954
ShardingSettings = shardingSettings;
55+
Role = role;
56+
}
57+
58+
private ShardedDaemonProcessSettings Copy(TimeSpan? keepAliveInterval = null, ClusterShardingSettings shardingSettings = null, string role = null)
59+
{
60+
return new ShardedDaemonProcessSettings(keepAliveInterval ?? KeepAliveInterval, shardingSettings ?? ShardingSettings, role ?? Role);
5061
}
5162

5263
/// <summary>
5364
/// NOTE: How the sharded set is kept alive may change in the future meaning this setting may go away.
5465
/// </summary>
5566
/// <param name="keepAliveInterval">The interval each parent of the sharded set is pinged from each node in the cluster.</param>
56-
public ShardedDaemonProcessSettings WithKeepAliveInterval(TimeSpan keepAliveInterval)
57-
{
58-
return new ShardedDaemonProcessSettings(keepAliveInterval, ShardingSettings);
59-
}
67+
public ShardedDaemonProcessSettings WithKeepAliveInterval(TimeSpan keepAliveInterval) => Copy(keepAliveInterval: keepAliveInterval);
6068

6169
/// <summary>
6270
/// Specify sharding settings that should be used for the sharded daemon process instead of loading from config.
6371
/// Some settings can not be changed (remember-entities and related settings, passivation, number-of-shards),
6472
/// changing those settings will be ignored.
6573
/// </summary>
6674
/// <param name="shardingSettings">TBD</param>
67-
public ShardedDaemonProcessSettings WithShardingSettings(ClusterShardingSettings shardingSettings)
68-
{
69-
return new ShardedDaemonProcessSettings(KeepAliveInterval, shardingSettings);
70-
}
75+
public ShardedDaemonProcessSettings WithShardingSettings(ClusterShardingSettings shardingSettings) => Copy(shardingSettings: shardingSettings);
76+
77+
/// <summary>
78+
/// Specifies that the ShardedDaemonProcess should run on nodes with a specific role.
79+
/// If the role is not specified all nodes in the cluster are used. If the given role does
80+
/// not match the role of the current node the ShardedDaemonProcess will not be started.
81+
/// </summary>
82+
/// <param name="role">TBD</param>
83+
public ShardedDaemonProcessSettings WithRole(string role) => Copy(role: role);
7184
}
7285
}

src/core/Akka.API.Tests/CoreAPISpec.ApproveClusterSharding.approved.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,12 @@ namespace Akka.Cluster.Sharding
230230
public sealed class ShardedDaemonProcessSettings
231231
{
232232
public readonly System.TimeSpan KeepAliveInterval;
233+
public readonly string Role;
233234
public readonly Akka.Cluster.Sharding.ClusterShardingSettings ShardingSettings;
234235
public static Akka.Cluster.Sharding.ShardedDaemonProcessSettings Create(Akka.Actor.ActorSystem system) { }
235236
public static Akka.Cluster.Sharding.ShardedDaemonProcessSettings FromConfig(Akka.Configuration.Config config) { }
236237
public Akka.Cluster.Sharding.ShardedDaemonProcessSettings WithKeepAliveInterval(System.TimeSpan keepAliveInterval) { }
238+
public Akka.Cluster.Sharding.ShardedDaemonProcessSettings WithRole(string role) { }
237239
public Akka.Cluster.Sharding.ShardedDaemonProcessSettings WithShardingSettings(Akka.Cluster.Sharding.ClusterShardingSettings shardingSettings) { }
238240
}
239241
public sealed class ShardingEnvelope

0 commit comments

Comments
 (0)