-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ClusterShardingQueriesSpec.cs
194 lines (169 loc) · 7.41 KB
/
ClusterShardingQueriesSpec.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//-----------------------------------------------------------------------
// <copyright file="ClusterShardingQueriesSpec.cs" company="Akka.NET Project">
// Copyright (C) 2009-2021 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2021 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Linq;
using Akka.Actor;
using Akka.Configuration;
using Akka.MultiNode.TestAdapter;
using Akka.Remote.TestKit;
using Akka.Util;
using FluentAssertions;
namespace Akka.Cluster.Sharding.Tests
{
public class ClusterShardingQueriesSpecConfig : MultiNodeClusterShardingConfig
{
public RoleName Controller { get; }
public RoleName Busy { get; }
public RoleName Second { get; }
public RoleName Third { get; }
public ClusterShardingQueriesSpecConfig()
: base(loglevel: "DEBUG", additionalConfig: @"
akka.cluster.sharding.rebalance-interval = 120s #disable rebalance
akka.cluster.min-nr-of-members = 3
")
{
Controller = Role("controller");
Busy = Role("busy");
Second = Role("second");
Third = Role("third");
var shardRoles = ConfigurationFactory.ParseString(@"akka.cluster.roles=[""shard""]");
NodeConfig(new RoleName[] { Busy }, new Config[] {
ConfigurationFactory.ParseString(@"akka.cluster.sharding.shard-region-query-timeout = 0ms")
.WithFallback(shardRoles)
});
NodeConfig(new RoleName[] { Second, Third }, new Config[] {
shardRoles
});
}
}
public class ClusterShardingQueriesSpec : MultiNodeClusterShardingSpec<ClusterShardingQueriesSpecConfig>
{
#region setup
private ExtractEntityId extractEntityId = message =>
{
switch (message)
{
case PingPongActor.Ping msg:
return (msg.Id.ToString(), message);
}
return Option<(string, object)>.None;
};
private ExtractShardId extractShardId = message =>
{
switch (message)
{
case PingPongActor.Ping msg:
return (msg.Id % NumberOfShards).ToString();
}
return null;
};
private const int NumberOfShards = 6;
private const string ShardTypeName = "DatatypeA";
private readonly Lazy<IActorRef> _region;
public ClusterShardingQueriesSpec()
: this(new ClusterShardingQueriesSpecConfig(), typeof(ClusterShardingQueriesSpec))
{
}
protected ClusterShardingQueriesSpec(ClusterShardingQueriesSpecConfig config, Type type)
: base(config, type)
{
_region = new Lazy<IActorRef>(() => ClusterSharding.Get(Sys).ShardRegion(ShardTypeName));
}
#endregion
[MultiNodeFact]
public void Querying_cluster_sharding_specs()
{
Querying_cluster_sharding_must_join_cluster_initialize_sharding();
Querying_cluster_sharding_must_trigger_sharded_actors();
Querying_cluster_sharding_must_return_shard_stats_of_cluster_sharding_regions_if_one_or_more_shards_timeout_versus_all_as_empty();
Querying_cluster_sharding_must_return_shard_state_of_sharding_regions_if_one_or_more_shards_timeout_versus_all_as_empty();
}
private void Querying_cluster_sharding_must_join_cluster_initialize_sharding()
{
AwaitClusterUp(config.Controller, config.Busy, config.Second, config.Third);
RunOn(() =>
{
StartProxy(
Sys,
typeName: ShardTypeName,
role: "shard",
extractEntityId: extractEntityId,
extractShardId: extractShardId);
}, config.Controller);
RunOn(() =>
{
StartSharding(
Sys,
typeName: ShardTypeName,
entityProps: Props.Create(() => new PingPongActor()),
settings: settings.Value.WithRole("shard"),
extractEntityId: extractEntityId,
extractShardId: extractShardId);
}, config.Busy, config.Second, config.Third);
EnterBarrier("sharding started");
}
private void Querying_cluster_sharding_must_trigger_sharded_actors()
{
RunOn(() =>
{
Within(TimeSpan.FromSeconds(10), () =>
{
AwaitAssert(() =>
{
var pingProbe = CreateTestProbe();
foreach (var n in Enumerable.Range(0, 20))
{
_region.Value.Tell(new PingPongActor.Ping(n), pingProbe.Ref);
}
pingProbe.ReceiveWhile(null, m => (PingPongActor.Pong)m, 20);
});
});
}, config.Controller);
EnterBarrier("sharded actors started");
}
private void Querying_cluster_sharding_must_return_shard_stats_of_cluster_sharding_regions_if_one_or_more_shards_timeout_versus_all_as_empty()
{
RunOn(() =>
{
var probe = CreateTestProbe();
var region = ClusterSharding.Get(Sys).ShardRegion(ShardTypeName);
region.Tell(new GetClusterShardingStats(TimeSpan.FromSeconds(10)), probe.Ref);
var regions = probe.ExpectMsg<ClusterShardingStats>().Regions;
regions.Count.Should().Be(3);
var timeouts = NumberOfShards / regions.Count;
// 3 regions, 2 shards per region, all 2 shards/region were unresponsive
// within shard-region-query-timeout, which only on first is 0ms
regions.Values.Select(i => i.Stats.Count).Sum().Should().Be(4);
regions.Values.Select(i => i.Failed.Count).Sum().Should().Be(timeouts);
}, config.Busy, config.Second, config.Third);
EnterBarrier("received failed stats from timed out shards vs empty");
}
private void Querying_cluster_sharding_must_return_shard_state_of_sharding_regions_if_one_or_more_shards_timeout_versus_all_as_empty()
{
RunOn(() =>
{
var probe = CreateTestProbe();
var region = ClusterSharding.Get(Sys).ShardRegion(ShardTypeName);
region.Tell(GetShardRegionState.Instance, probe.Ref);
var state = probe.ExpectMsg<CurrentShardRegionState>();
state.Shards.Should().BeEmpty();
state.Failed.Should().HaveCount(2);
}, config.Busy);
EnterBarrier("query-timeout-on-busy-node");
RunOn(() =>
{
var probe = CreateTestProbe();
var region = ClusterSharding.Get(Sys).ShardRegion(ShardTypeName);
region.Tell(GetShardRegionState.Instance, probe.Ref);
var state = probe.ExpectMsg<CurrentShardRegionState>();
state.Shards.Should().HaveCount(2);
state.Failed.Should().BeEmpty();
}, config.Second, config.Third);
EnterBarrier("done");
}
}
}