-
Notifications
You must be signed in to change notification settings - Fork 1k
/
MultiNodeClusterSpec.cs
470 lines (409 loc) · 17.9 KB
/
MultiNodeClusterSpec.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//-----------------------------------------------------------------------
// <copyright file="MultiNodeClusterSpec.cs" company="Akka.NET Project">
// Copyright (C) 2009-2023 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2023 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text.RegularExpressions;
using Akka.Actor;
using Akka.Actor.Setup;
using Akka.Cluster.Tests.MultiNode;
using Akka.Configuration;
using Akka.Dispatch.SysMsg;
using Akka.Remote.TestKit;
using Akka.Remote.Transport;
using Akka.TestKit;
using Akka.TestKit.Xunit2;
using Akka.Util.Internal;
using Xunit;
namespace Akka.Cluster.TestKit
{
//TODO: WatchedByCoroner?
//@Aaronontheweb: Coroner is a JVM-specific instrument used to report deadlocks and other fun stuff.
//can probably skip for now.
public abstract class MultiNodeClusterSpec : MultiNodeSpec
{
public static Config ClusterConfigWithFailureDetectorPuppet()
{
return ConfigurationFactory.ParseString(
string.Format(@"akka.cluster.failure-detector.implementation-class = ""{0}""", typeof(FailureDetectorPuppet).AssemblyQualifiedName))
.WithFallback(ClusterConfig());
}
public static Config ClusterConfig(bool failureDetectorPuppet)
{
return failureDetectorPuppet ? ClusterConfigWithFailureDetectorPuppet() : ClusterConfig();
}
public static Config ClusterConfig()
{
return ConfigurationFactory.ParseString(@"
akka.actor.provider = ""Akka.Cluster.ClusterActorRefProvider, Akka.Cluster""
akka.cluster {
gossip-interval = 200 ms
leader-actions-interval = 200 ms
unreachable-nodes-reaper-interval = 500 ms
periodic-tasks-initial-delay = 300 ms
publish-stats-interval = 0 s # always, when it happens
failure-detector.heartbeat-interval = 500 ms
run-coordinated-shutdown-when-down = off
sharding {
retry-interval = 200ms
waiting-for-state-timeout = 200ms
}
#downing-provider-class = """" # disable default SBR
}
akka.loglevel = INFO
akka.log-dead-letters = off
akka.log-dead-letters-during-shutdown = off
#akka.remote.log-remote-lifecycle-events = off
akka.coordinated-shutdown.run-by-clr-shutdown-hook = off
akka.coordinated-shutdown.terminate-actor-system = off
#akka.loggers = [""Akka.TestKit.TestEventListener, Akka.TestKit""]
akka.test {
single-expect-default = 15 s
}
");
}
public class EndActor : UntypedActor
{
// sometimes we need to coordinate test shutdown with messages instead of barriers
public sealed class SendEnd
{
private SendEnd() { }
public static SendEnd Instance { get; } = new();
}
public sealed class End
{
private End() { }
public static End Instance { get; } = new();
}
public sealed class EndAck
{
private EndAck() { }
public static EndAck Instance { get; } = new();
}
readonly IActorRef _testActor;
readonly Address _target;
/// <summary>
///
/// </summary>
/// <param name="testActor">A reference to a <see cref="TestActor"/> or <see cref="TestProbe"/>.</param>
/// <param name="target">CAN BE NULL</param>
public EndActor(IActorRef testActor, Address target)
{
_testActor = testActor;
_target = target;
}
protected override void OnReceive(object message)
{
if (message is SendEnd)
{
if (_target != null)
Context.ActorSelection(new RootActorPath(_target) / Self.Path.Elements).Tell(End.Instance);
return;
}
if (message is End)
{
_testActor.Forward(End.Instance);
Sender.Tell(EndAck.Instance);
return;
}
if (message is EndAck)
{
_testActor.Forward(EndAck.Instance);
}
}
}
readonly ITestKitAssertions _assertions;
protected MultiNodeClusterSpec(MultiNodeConfig config, Type type)
: base(config, type)
{
_assertions = new XunitAssertions();
_roleNameComparer = new RoleNameComparer(this);
}
protected MultiNodeClusterSpec(
RoleName myself,
ActorSystem system,
ImmutableList<RoleName> roles,
Func<RoleName, ImmutableList<string>> deployments)
: base(myself, system, roles, deployments)
{
_assertions = new XunitAssertions();
_roleNameComparer = new RoleNameComparer(this);
}
protected MultiNodeClusterSpec(
RoleName myself,
ActorSystemSetup setup,
ImmutableList<RoleName> roles,
Func<RoleName, ImmutableList<string>> deployments)
: base(myself, setup, roles, deployments)
{
_assertions = new XunitAssertions();
_roleNameComparer = new RoleNameComparer(this);
}
protected override int InitialParticipantsValueFactory
{
get { return Roles.Count; }
}
readonly ConcurrentDictionary<RoleName, Address> _cachedAddresses = new();
protected override void AtStartup()
{
MuteLog(Sys);
}
protected override void AfterTermination()
{
}
//TODO: ExpectedTestDuration?
public virtual void MuteLog(ActorSystem sys = null)
{
if (sys == null) sys = Sys;
if (!sys.Log.IsDebugEnabled)
{
var patterns = new[]
{
".*Metrics collection has started successfully.*",
".*Cluster Node.* - is starting up.*",
".*Shutting down cluster Node.*",
".*Cluster node successfully shut down.*",
".*Using a dedicated scheduler for cluster.*"
};
foreach (var pattern in patterns)
EventFilter.Info(new Regex(pattern)).Mute();
MuteDeadLetters(sys,
typeof(ClusterHeartbeatSender.Heartbeat),
typeof(ClusterHeartbeatSender.HeartbeatRsp),
typeof(GossipEnvelope),
typeof(GossipStatus),
typeof(GossipStatus),
typeof(InternalClusterAction.ITick),
typeof(PoisonPill),
typeof(DeathWatchNotification),
typeof(Disassociated),
typeof(DisassociateUnderlying),
typeof(InboundPayload));
}
}
protected void MuteMarkingAsUnreachable(ActorSystem system = null)
{
var sys = system ?? Sys;
if (!sys.Log.IsDebugEnabled)
EventFilter.Error(new Regex(".*Marking.* as UNREACHABLE.*")).Mute();
}
protected void MuteMarkingAsReachable(ActorSystem system = null)
{
var sys = system ?? Sys;
if (!sys.Log.IsDebugEnabled)
EventFilter.Info(new Regex(".*Marking.* as REACHABLE.*")).Mute();
}
public Address GetAddress(RoleName role)
{
if (!_cachedAddresses.TryGetValue(role, out var address))
{
address = Node(role).Address;
_cachedAddresses.TryAdd(role, address);
}
return address;
}
internal ClusterReadView ClusterView { get { return Cluster.ReadView; } }
/// <summary>
/// Get the cluster node to use.
/// </summary>
public Akka.Cluster.Cluster Cluster { get { return Akka.Cluster.Cluster.Get(Sys); } }
/// <summary>
/// Use this method for the initial startup of the cluster node
/// </summary>
public void StartClusterNode()
{
if (ClusterView.Members.IsEmpty)
{
Cluster.Join(GetAddress(Myself));
AwaitAssert(() => Assert.Contains(GetAddress(Myself), ClusterView.Members.Select(m => m.Address)));
}
}
/// <summary>
/// Initialize the cluster of the specified member nodes (<paramref name="roles"/>)
/// and wait until all joined and <see cref="MemberStatus.Up"/>.
///
/// First node will be started first and others will join the first.
/// </summary>
public void AwaitClusterUp(params RoleName[] roles)
{
// make sure that the node-to-join is started before other join
RunOn(StartClusterNode, roles.First());
EnterBarrier(roles.First().Name + "-started");
if (roles.Skip(1).Contains(Myself)) Cluster.Join(GetAddress(roles.First()));
if (roles.Contains(Myself))
{
AwaitMembersUp(roles.Length);
}
EnterBarrier(roles.Select(r => r.Name).Aggregate((a, b) => a + "-" + b) + "-joined");
}
public void JoinWithin(RoleName joinNode, TimeSpan? max = null, TimeSpan? interval = null)
{
if (max == null) max = RemainingOrDefault;
if (interval == null) interval = TimeSpan.FromSeconds(1);
Cluster.Join(GetAddress(joinNode));
AwaitCondition(() =>
{
ClusterView.RefreshCurrentState();
if (MemberInState(GetAddress(joinNode), new[] { MemberStatus.Up }) &&
MemberInState(GetAddress(Myself), new[] { MemberStatus.Joining, MemberStatus.Up }))
return true;
Cluster.Join(GetAddress(joinNode));
return false;
}, max, interval);
}
private bool MemberInState(Address member, IEnumerable<MemberStatus> status)
{
return ClusterView.Members.Any(m => m.Address == member && status.Contains(m.Status));
}
/// <summary>
/// Assert that the
/// member addresses match the expected addresses in the
/// sort order used by the cluster.
/// </summary>
public void AssertMembers(IEnumerable<Member> gotMembers, params Address[] expectedAddresses)
{
var members = gotMembers.ToImmutableList();
_assertions.AssertEqual(expectedAddresses.Length, members.Count);
expectedAddresses.ToImmutableSortedSet(Member.AddressOrdering).ZipWithIndex().ForEach(kvp =>
{
_assertions.AssertEqual(kvp.Key, members[kvp.Value].Address);
});
}
/// <summary>
/// Note that this can only be used for a cluster with all members
/// in Up status, i.e. use `awaitMembersUp` before using this method.
/// The reason for that is that the cluster leader is preferably a
/// member with status Up or Leaving and that information can't
/// be determined from the `RoleName`.
/// </summary>
public void AssertLeader(params RoleName[] nodesInCluster)
{
if (nodesInCluster.Contains(Myself)) AssertLeaderIn(nodesInCluster.ToImmutableList());
}
/// <summary>
/// Assert that the cluster has elected the correct leader
/// out of all nodes in the cluster. First
/// member in the cluster ring is expected leader.
///
/// Note that this can only be used for a cluster with all members
/// in Up status, i.e. use `awaitMembersUp` before using this method.
/// The reason for that is that the cluster leader is preferably a
/// member with status Up or Leaving and that information can't
/// be determined from the `RoleName`.
/// </summary>
public void AssertLeaderIn(ImmutableList<RoleName> nodesInCluster)
{
if (!nodesInCluster.Contains(Myself)) return;
_assertions.AssertTrue(nodesInCluster.Count != 0, "nodesInCluster must not be empty");
var expectedLeader = RoleOfLeader(nodesInCluster);
var leader = ClusterView.Leader;
var isLeader = leader == ClusterView.SelfAddress;
_assertions.AssertTrue(isLeader == IsNode(expectedLeader), "expected leader {0}, got leader {1}, members{2}", expectedLeader, leader, ClusterView.Members);
_assertions.AssertTrue(ClusterView.Status is MemberStatus.Up or MemberStatus.Leaving,
"Expected cluster view status Up or Leaving but got {0}", ClusterView.Status);
}
public void AwaitMembersUp(
int numbersOfMembers,
ImmutableHashSet<Address> canNotBePartOfMemberRing = null,
TimeSpan? timeout = null)
{
if (canNotBePartOfMemberRing == null)
canNotBePartOfMemberRing = ImmutableHashSet.Create<Address>();
if (timeout == null) timeout = TimeSpan.FromSeconds(25);
Within(timeout.Value, () =>
{
if (canNotBePartOfMemberRing.Any()) // don't run this on an empty set
AwaitAssert(() =>
{
foreach (var a in canNotBePartOfMemberRing)
_assertions.AssertFalse(ClusterView.Members.Select(m => m.Address).Contains(a));
});
AwaitAssert(() => _assertions.AssertEqual(numbersOfMembers, ClusterView.Members.Count));
AwaitAssert(() => _assertions.AssertTrue(ClusterView.Members.All(m => m.Status == MemberStatus.Up), "All members should be up"));
// clusterView.leader is updated by LeaderChanged, await that to be updated also
var firstMember = ClusterView.Members.FirstOrDefault();
var expectedLeader = firstMember == null ? null : firstMember.Address;
AwaitAssert(() => _assertions.AssertEqual(expectedLeader, ClusterView.Leader));
});
}
public void AwaitAllReachable()
{
AwaitAssert(() => _assertions.AssertFalse(ClusterView.UnreachableMembers.Any()));
}
public void AwaitSeenSameState(params Address[] addresses)
{
AwaitAssert(() => _assertions.AssertFalse(addresses.ToImmutableHashSet().Except(ClusterView.SeenBy).Any()));
}
/// <summary>
/// Leader according to the address ordering of the roles.
/// Note that this can only be used for a cluster with all members
/// in Up status, i.e. use `awaitMembersUp` before using this method.
/// The reason for that is that the cluster leader is preferably a
/// member with status Up or Leaving and that information can't
/// be determined from the `RoleName`.
/// </summary>
public RoleName RoleOfLeader(ImmutableList<RoleName> nodesInCluster)
{
if (nodesInCluster == null) nodesInCluster = Roles;
_assertions.AssertFalse(nodesInCluster.Count == 0);
return nodesInCluster.Sort(_roleNameComparer).First();
}
readonly RoleNameComparer _roleNameComparer;
public class RoleNameComparer : IComparer<RoleName>
{
readonly MultiNodeClusterSpec _spec;
public RoleNameComparer(MultiNodeClusterSpec spec)
{
_spec = spec;
}
public int Compare(RoleName x, RoleName y)
{
return Member.AddressOrdering.Compare(_spec.GetAddress(x), _spec.GetAddress(y));
}
}
public RoleName RoleName(Address addr)
{
return Roles.FirstOrDefault(r => GetAddress(r) == addr);
}
/// <summary>
/// Marks a node as available in the failure detector if
/// <see cref="FailureDetectorPuppet"/> is used as
/// failure detector
/// </summary>
public void MarkNodeAsAvailable(Address address)
{
var puppet = FailureDetectorPuppet(address);
if (puppet != null) puppet.MarkNodeAsAvailable();
}
/// <summary>
/// Marks a node as unavailable in the failure detector if
/// <see cref="FailureDetectorPuppet"/> is used as
/// failure detector
/// </summary>
public void MarkNodeAsUnavailable(Address address)
{
if (IsFailureDetectorPuppet())
{
// before marking it as unavailable there should be at least one heartbeat
// to create the FailureDetectorPuppet in the FailureDetectorRegistry
Cluster.FailureDetector.Heartbeat(address);
var puppet = FailureDetectorPuppet(address);
if (puppet != null) puppet.MarkNodeAsUnavailable();
}
}
public bool IsFailureDetectorPuppet()
{
return Type.GetType(Cluster.Settings.FailureDetectorImplementationClass) == typeof (FailureDetectorPuppet);
}
public FailureDetectorPuppet FailureDetectorPuppet(Address address)
{
return (FailureDetectorPuppet)Cluster.FailureDetector.GetFailureDetector(address);
}
}
}