-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ClusterShardingLeavingSpec.cs
289 lines (250 loc) · 10 KB
/
ClusterShardingLeavingSpec.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//-----------------------------------------------------------------------
// <copyright file="ClusterShardingLeavingSpec.cs" company="Akka.NET Project">
// Copyright (C) 2009-2022 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2022 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Akka.Actor;
using Akka.Event;
using Akka.MultiNode.TestAdapter;
using Akka.Remote.TestKit;
using Akka.Util;
using FluentAssertions;
namespace Akka.Cluster.Sharding.Tests
{
public class ClusterShardingLeavingSpecConfig : MultiNodeClusterShardingConfig
{
public RoleName First { get; }
public RoleName Second { get; }
public RoleName Third { get; }
public RoleName Fourth { get; }
public RoleName Fifth { get; }
public ClusterShardingLeavingSpecConfig(StateStoreMode mode)
: base(mode: mode, loglevel: "DEBUG", additionalConfig: @"
akka.cluster.sharding.verbose-debug-logging = on
akka.cluster.sharding.rebalance-interval = 1s # make rebalancing more likely to happen to test for https://github.com/akka/akka/issues/29093
akka.cluster.sharding.distributed-data.majority-min-cap = 1
akka.cluster.sharding.coordinator-state.write-majority-plus = 1
akka.cluster.sharding.coordinator-state.read-majority-plus = 1
")
{
First = Role("first");
Second = Role("second");
Third = Role("third");
Fourth = Role("fourth");
Fifth = Role("fifth");
}
}
public class PersistentClusterShardingLeavingSpecConfig : ClusterShardingLeavingSpecConfig
{
public PersistentClusterShardingLeavingSpecConfig()
: base(StateStoreMode.Persistence)
{
}
}
public class DDataClusterShardingLeavingSpecConfig : ClusterShardingLeavingSpecConfig
{
public DDataClusterShardingLeavingSpecConfig()
: base(StateStoreMode.DData)
{
}
}
public class PersistentClusterShardingLeavingSpec : ClusterShardingLeavingSpec
{
public PersistentClusterShardingLeavingSpec()
: base(new PersistentClusterShardingLeavingSpecConfig(), typeof(PersistentClusterShardingLeavingSpec))
{
}
}
public class DDataClusterShardingLeavingSpec : ClusterShardingLeavingSpec
{
public DDataClusterShardingLeavingSpec()
: base(new DDataClusterShardingLeavingSpecConfig(), typeof(DDataClusterShardingLeavingSpec))
{
}
}
public abstract class ClusterShardingLeavingSpec : MultiNodeClusterShardingSpec<ClusterShardingLeavingSpecConfig>
{
#region setup
[Serializable]
internal sealed class Ping
{
public readonly string Id;
public Ping(string id)
{
Id = id;
}
}
[Serializable]
internal sealed class GetLocations
{
public static readonly GetLocations Instance = new GetLocations();
private GetLocations()
{
}
}
[Serializable]
internal sealed class Locations
{
public readonly IImmutableDictionary<string, IActorRef> LocationMap;
public Locations(IImmutableDictionary<string, IActorRef> locationMap)
{
LocationMap = locationMap;
}
}
internal class Entity : ReceiveActor
{
public Entity()
{
Receive<Ping>(_ => Sender.Tell(Self));
}
}
internal class ShardLocations : ReceiveActor
{
private Locations _locations = null;
public ShardLocations()
{
Receive<GetLocations>(_ => Sender.Tell(_locations));
Receive<Locations>(l => _locations = l);
}
}
private ExtractEntityId extractEntityId = message =>
{
switch (message)
{
case Ping msg:
return (msg.Id, message);
}
return Option<(string, object)>.None;
};
private ExtractShardId extractShardId = message =>
{
switch (message)
{
case Ping msg:
return msg.Id[0].ToString();
}
return null;
};
private readonly Lazy<IActorRef> _region;
protected ClusterShardingLeavingSpec(ClusterShardingLeavingSpecConfig config, Type type)
: base(config, type)
{
_region = new Lazy<IActorRef>(() => ClusterSharding.Get(Sys).ShardRegion("Entity"));
}
private void StartSharding()
{
StartSharding(
Sys,
typeName: "Entity",
entityProps: Props.Create(() => new Entity()),
extractEntityId: extractEntityId,
extractShardId: extractShardId);
}
#endregion
[MultiNodeFact]
public void ClusterSharding_with_leaving_member_specs()
{
Cluster_sharding_with_leaving_member_must_join_cluster();
Cluster_sharding_with_leaving_member_must_initialize_shards();
Cluster_sharding_with_leaving_member_must_recover_after_leaving_coordinator_node();
}
private void Cluster_sharding_with_leaving_member_must_join_cluster()
{
Within(TimeSpan.FromSeconds(20), () =>
{
StartPersistenceIfNeeded(startOn: config.First, Roles.ToArray());
Join(config.First, config.First, onJoinedRunOnFrom: StartSharding);
Join(config.Second, config.First, onJoinedRunOnFrom: StartSharding, assertNodeUp: false);
Join(config.Third, config.First, onJoinedRunOnFrom: StartSharding, assertNodeUp: false);
Join(config.Fourth, config.First, onJoinedRunOnFrom: StartSharding, assertNodeUp: false);
Join(config.Fifth, config.First, onJoinedRunOnFrom: StartSharding, assertNodeUp: false);
// all Up, everywhere before continuing
AwaitAssert(() =>
{
Cluster.State.Members.Count.Should().Be(Roles.Count);
Cluster.State.Members.Should().OnlyContain(m => m.Status == MemberStatus.Up);
});
RunOn(() =>
{
// wait for all regions registered
AwaitAssert(() =>
{
_region.Value.Tell(GetCurrentRegions.Instance);
ExpectMsg<CurrentRegions>().Regions.Count.Should().Be(5);
});
}, config.First);
EnterBarrier("after-2");
});
}
private void Cluster_sharding_with_leaving_member_must_initialize_shards()
{
RunOn(() =>
{
var shardLocations = Sys.ActorOf(Props.Create<ShardLocations>(), "shardLocations");
var locations = Enumerable.Range(1, 10).Select(n =>
{
var id = n.ToString();
_region.Value.Tell(new Ping(id));
return new KeyValuePair<string, IActorRef>(id, ExpectMsg<IActorRef>());
}).ToImmutableDictionary();
shardLocations.Tell(new Locations(locations));
Sys.Log.Debug("Original locations: {0}", string.Join(",", locations.Select(x => $"{x.Key}->{x.Value}")));
}, config.First);
EnterBarrier("after-3");
}
private void Cluster_sharding_with_leaving_member_must_recover_after_leaving_coordinator_node()
{
Sys.ActorSelection(Node(config.First) / "user" / "shardLocations").Tell(GetLocations.Instance);
var originalLocations = ExpectMsg<Locations>().LocationMap;
var numberOfNodesLeaving = 2;
var leavingRoles = Roles.Take(numberOfNodesLeaving).ToArray();
var leavingNodes = leavingRoles.Select(r => GetAddress(r)).ToImmutableHashSet();
var remainingRoles = Roles.Skip(numberOfNodesLeaving).ToArray();
RunOn(() =>
{
foreach (var a in leavingNodes)
Cluster.Leave(a);
}, Roles.Last());
RunOn(() =>
{
var region = _region.Value;
Watch(region);
ExpectTerminated(region, TimeSpan.FromSeconds(15));
}, config.First);
EnterBarrier("stopped");
// more stress by not having the barrier here
RunOn(() =>
{
Within(TimeSpan.FromSeconds(15), () =>
{
AwaitAssert(() =>
{
//var region = _region.Value;
var probe = CreateTestProbe();
foreach (var kv in originalLocations)
{
var id = kv.Key;
var r = kv.Value;
_region.Value.Tell(new Ping(id), probe.Ref);
if (leavingNodes.Contains(r.Path.Address))
{
var newRef = probe.ExpectMsg<IActorRef>(TimeSpan.FromSeconds(1));
// have to log before we assert
Sys.Log.Debug("Moved [{0}] from [{1}] to [{2}]", id, r, newRef);
newRef.Should().NotBe(r);
}
else
probe.ExpectMsg(r, TimeSpan.FromSeconds(1)); // should not move
}
});
});
}, remainingRoles);
EnterBarrier("after-4");
}
}
}