-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ClusterShardCoordinatorDowning2Spec.cs
293 lines (253 loc) · 10.3 KB
/
ClusterShardCoordinatorDowning2Spec.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
290
291
292
293
//-----------------------------------------------------------------------
// <copyright file="ClusterShardCoordinatorDowning2Spec.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 System.Threading;
using Akka.Actor;
using Akka.Event;
using Akka.MultiNode.TestAdapter;
using Akka.Remote.TestKit;
using Akka.Util;
using FluentAssertions;
using static Akka.Remote.Transport.ThrottleTransportAdapter;
namespace Akka.Cluster.Sharding.Tests
{
public class ClusterShardCoordinatorDowning2SpecConfig : MultiNodeClusterShardingConfig
{
public RoleName First { get; }
public RoleName Second { get; }
public ClusterShardCoordinatorDowning2SpecConfig(StateStoreMode mode)
: base(mode: mode, loglevel: "DEBUG", additionalConfig: @"
akka.cluster.sharding.rebalance-interval = 120 s
# setting down-removal-margin, for testing of issue #29131
akka.cluster.down-removal-margin = 3 s
akka.remote.watch-failure-detector.acceptable-heartbeat-pause = 3s
")
{
First = Role("first");
Second = Role("second");
TestTransport = true;
}
}
public class PersistentClusterShardCoordinatorDowning2SpecConfig : ClusterShardCoordinatorDowning2SpecConfig
{
public PersistentClusterShardCoordinatorDowning2SpecConfig()
: base(StateStoreMode.Persistence)
{
}
}
public class DDataClusterShardCoordinatorDowning2SpecConfig : ClusterShardCoordinatorDowning2SpecConfig
{
public DDataClusterShardCoordinatorDowning2SpecConfig()
: base(StateStoreMode.DData)
{
}
}
public class PersistentClusterShardCoordinatorDowning2Spec : ClusterShardCoordinatorDowning2Spec
{
public PersistentClusterShardCoordinatorDowning2Spec()
: base(new PersistentClusterShardCoordinatorDowning2SpecConfig(), typeof(PersistentClusterShardCoordinatorDowning2Spec))
{
}
}
public class DDataClusterShardCoordinatorDowning2Spec : ClusterShardCoordinatorDowning2Spec
{
public DDataClusterShardCoordinatorDowning2Spec()
: base(new DDataClusterShardCoordinatorDowning2SpecConfig(), typeof(DDataClusterShardCoordinatorDowning2Spec))
{
}
}
public abstract class ClusterShardCoordinatorDowning2Spec : MultiNodeClusterShardingSpec<ClusterShardCoordinatorDowning2SpecConfig>
{
#region setup
internal sealed class Ping
{
public readonly string Id;
public Ping(string id)
{
Id = id;
}
}
internal class Entity : ActorBase
{
protected override bool Receive(object message)
{
if (message is Ping)
{
Sender.Tell(Self);
return true;
}
return false;
}
}
internal class GetLocations
{
public static readonly GetLocations Instance = new GetLocations();
private GetLocations()
{
}
}
internal class Locations
{
public Locations(ImmutableDictionary<string, IActorRef> locations)
{
Locs = locations;
}
public ImmutableDictionary<string, IActorRef> Locs { get; }
}
internal class ShardLocations : ActorBase
{
private Locations locations;
public ShardLocations()
{
}
protected override bool Receive(object message)
{
switch (message)
{
case GetLocations _:
Sender.Tell(locations);
return true;
case Locations l:
locations = l;
return true;
}
return false;
}
}
private ExtractEntityId extractEntityId = message =>
{
switch (message)
{
case Ping p:
return (p.Id, message);
}
return Option<(string, object)>.None;
};
internal ExtractShardId extractShardId = message =>
{
switch (message)
{
case Ping p:
return p.Id[0].ToString();
}
return null;
};
private readonly Lazy<IActorRef> _region;
protected ClusterShardCoordinatorDowning2Spec(ClusterShardCoordinatorDowning2SpecConfig 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 Cluster_sharding_with_down_member_scenario_2_specs()
{
Cluster_sharding_with_down_member_scenario_2_must_join_cluster();
Cluster_sharding_with_down_member_scenario_2_must_initialize_shards();
Cluster_sharding_with_down_member_scenario_2_must_recover_after_downing_other_node_not_coordinator();
}
private void Cluster_sharding_with_down_member_scenario_2_must_join_cluster()
{
Within(TimeSpan.FromSeconds(20), () =>
{
StartPersistenceIfNeeded(startOn: config.First, config.First, config.Second);
Join(config.First, config.First, onJoinedRunOnFrom: StartSharding);
Join(config.Second, config.First, onJoinedRunOnFrom: StartSharding, assertNodeUp: false);
// all Up, everywhere before continuing
RunOn(() =>
{
AwaitAssert(() =>
{
Cluster.State.Members.Count.Should().Be(2);
Cluster.State.Members.Should().OnlyContain(m => m.Status == MemberStatus.Up);
});
}, config.First, config.Second);
EnterBarrier("after-2");
});
}
private void Cluster_sharding_with_down_member_scenario_2_must_initialize_shards()
{
RunOn(() =>
{
var shardLocations = Sys.ActorOf(Props.Create(() => new ShardLocations()), "shardLocations");
var locations = Enumerable.Range(1, 4).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(i => $"{i.Key}: {i.Value}")));
}, config.First);
EnterBarrier("after-3");
}
private void Cluster_sharding_with_down_member_scenario_2_must_recover_after_downing_other_node_not_coordinator()
{
Within(TimeSpan.FromSeconds(20), () =>
{
var secondAddress = GetAddress(config.Second);
RunOn(() =>
{
TestConductor.Blackhole(config.First, config.Second, Direction.Both).Wait();
}, config.First);
Thread.Sleep(3000);
RunOn(() =>
{
Cluster.Down(GetAddress(config.Second));
AwaitAssert(() =>
{
Cluster.State.Members.Count.Should().Be(1);
});
// start a few more new shards, could be allocated to second but should notice that it's terminated
ImmutableDictionary<string, IActorRef> additionalLocations = null;
AwaitAssert(() =>
{
var probe = CreateTestProbe();
additionalLocations = Enumerable.Range(5, 4).Select(n =>
{
var id = n.ToString();
_region.Value.Tell(new Ping(id), probe.Ref);
return new KeyValuePair<string, IActorRef>(id, probe.ExpectMsg<IActorRef>(TimeSpan.FromSeconds(1)));
}).ToImmutableDictionary();
});
Sys.Log.Debug("Additional locations: [{0}]", string.Join(", ", additionalLocations.Select(i => $"{i.Key}: {i.Value}")));
Sys.ActorSelection(Node(config.First) / "user" / "shardLocations").Tell(GetLocations.Instance);
var originalLocations = ExpectMsg<Locations>().Locs;
AwaitAssert(() =>
{
var probe = CreateTestProbe();
foreach (var loc in originalLocations.SetItems(additionalLocations))
{
_region.Value.Tell(new Ping(loc.Key), probe.Ref);
if (loc.Value.Path.Address.Equals(secondAddress))
{
var newRef = probe.ExpectMsg<IActorRef>(TimeSpan.FromSeconds(1));
newRef.Should().NotBe(loc.Value);
Sys.Log.Debug("Moved [{0}] from [{1}] to [{2}]", loc.Key, loc.Value, newRef);
}
else
probe.ExpectMsg(loc.Value, TimeSpan.FromSeconds(1)); // should not move
}
});
}, config.First);
});
EnterBarrier("after-4");
}
}
}