forked from akkadotnet/akka.net
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCoordinatedShutdownShardingSpec.cs
More file actions
247 lines (216 loc) · 9.29 KB
/
Copy pathCoordinatedShutdownShardingSpec.cs
File metadata and controls
247 lines (216 loc) · 9.29 KB
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
//-----------------------------------------------------------------------
// <copyright file="CoordinatedShutdownShardingSpec.cs" company="Akka.NET Project">
// Copyright (C) 2009-2022 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2025 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Linq;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.Cluster.Tools.Singleton;
using Akka.Configuration;
using Akka.TestKit;
using Akka.Util;
using FluentAssertions;
using FluentAssertions.Extensions;
using Xunit;
namespace Akka.Cluster.Sharding.Tests
{
public class CoordinatedShutdownShardingSpec : AkkaSpec
{
private readonly ActorSystem _sys1;
private readonly ActorSystem _sys2;
private readonly ActorSystem _sys3;
private IActorRef _region1;
private IActorRef _region2;
private IActorRef _region3;
private readonly TestProbe _probe1;
private readonly TestProbe _probe2;
private readonly TestProbe _probe3;
private class EchoActor : ReceiveActor
{
public EchoActor()
{
ReceiveAny(_ => Sender.Tell(_));
}
}
private sealed class MessageExtractor: IMessageExtractor
{
public string EntityId(object message)
=> message.ToString();
public object EntityMessage(object message)
=> message;
public string ShardId(object message)
=> MurmurHash.StringHash(message.ToString()).ToString();
public string ShardId(string entityId, object messageHint = null)
=> MurmurHash.StringHash(entityId).ToString();
}
private static Config SpecConfig =>
ConfigurationFactory.ParseString(@"
akka.loglevel = DEBUG
akka.actor.provider = cluster
akka.remote.dot-netty.tcp.port = 0
akka.cluster.sharding.verbose-debug-logging = on")
.WithFallback(ClusterSingleton.DefaultConfig())
.WithFallback(ClusterSharding.DefaultConfig());
public CoordinatedShutdownShardingSpec(ITestOutputHelper helper) : base(SpecConfig, helper)
{
_sys1 = ActorSystem.Create(Sys.Name, Sys.Settings.Config);
_sys2 = ActorSystem.Create(Sys.Name, Sys.Settings.Config);
_sys3 = Sys;
InitializeLogger(_sys1, "[Sys1]");
InitializeLogger(_sys2, "[Sys2]");
_probe1 = CreateTestProbe(_sys1);
_probe2 = CreateTestProbe(_sys2);
_probe3 = CreateTestProbe(_sys3);
CoordinatedShutdown.Get(_sys1).AddTask(CoordinatedShutdown.PhaseBeforeServiceUnbind, "unbind", () =>
{
_probe1.Ref.Tell("CS-unbind-1");
return Task.FromResult(Done.Instance);
});
CoordinatedShutdown.Get(_sys2).AddTask(CoordinatedShutdown.PhaseBeforeServiceUnbind, "unbind", () =>
{
_probe2.Ref.Tell("CS-unbind-2");
return Task.FromResult(Done.Instance);
});
CoordinatedShutdown.Get(_sys3).AddTask(CoordinatedShutdown.PhaseBeforeServiceUnbind, "unbind", () =>
{
_probe3.Ref.Tell("CS-unbind-3");
return Task.FromResult(Done.Instance);
});
}
protected override void AfterAll()
{
base.AfterAll();
Shutdown(_sys1);
Shutdown(_sys2);
}
/// <summary>
/// Using region 2 as it is not shutdown in either test.
/// </summary>
private async Task PingEntities()
{
await AwaitAssertAsync(() =>
{
var p1 = CreateTestProbe(_sys2);
_region2.Tell(1, p1.Ref);
p1.ExpectMsg<int>(1.Seconds()).Should().Be(1);
var p2 = CreateTestProbe(_sys2);
_region2.Tell(2, p2.Ref);
p2.ExpectMsg<int>(1.Seconds()).Should().Be(2);
var p3 = CreateTestProbe(_sys2);
_region2.Tell(3, p3.Ref);
p3.ExpectMsg<int>(1.Seconds()).Should().Be(3);
}, TimeSpan.FromSeconds(60));
}
[Theory]
[InlineData(StateStoreMode.Persistence)]
[InlineData(StateStoreMode.DData)]
public async Task Sharding_and_CoordinatedShutdown_must_run_successfully(StateStoreMode stateStoreMode)
{
StartSharding(stateStoreMode);
await InitCluster();
await CreateEventFilter(_sys1)
.Error(start: "Cluster Sharding DData replicator")
.ExpectAsync(0, 100.Milliseconds(), RunCoordinatedShutdownWhenLeaving);
await CreateEventFilter(_sys3)
.Error(start: "Cluster Sharding DData replicator")
.ExpectAsync(0, 100.Milliseconds(), RunCoordinatedShutdownWhenDowning);
}
private void StartSharding(StateStoreMode stateStoreMode)
{
var props = Props.Create(() => new EchoActor());
var messageExtractor = new MessageExtractor();
_region1 = ClusterSharding.Get(_sys1).Start(
"type1",
props,
ClusterShardingSettings.Create(_sys1).WithStateStoreMode(stateStoreMode),
messageExtractor);
_region2 = ClusterSharding.Get(_sys2).Start(
"type1",
props,
ClusterShardingSettings.Create(_sys2).WithStateStoreMode(stateStoreMode),
messageExtractor);
_region3 = ClusterSharding.Get(_sys3).Start(
"type1",
props,
ClusterShardingSettings.Create(_sys3).WithStateStoreMode(stateStoreMode),
messageExtractor);
}
private async Task InitCluster()
{
Cluster.Get(_sys1).Join(Cluster.Get(_sys1).SelfAddress); // coordinator will initially run on sys1
await AwaitAssertAsync(() => Cluster.Get(_sys1).SelfMember.Status.Should().Be(MemberStatus.Up));
Cluster.Get(_sys2).Join(Cluster.Get(_sys1).SelfAddress);
await WithinAsync(10.Seconds(), async () =>
{
await AwaitAssertAsync(() =>
{
Cluster.Get(_sys1).State.Members.Count.Should().Be(2);
Cluster.Get(_sys1).State.Members.All(x => x.Status == MemberStatus.Up).Should().BeTrue();
Cluster.Get(_sys2).State.Members.Count.Should().Be(2);
Cluster.Get(_sys2).State.Members.All(x => x.Status == MemberStatus.Up).Should().BeTrue();
});
});
Cluster.Get(_sys3).Join(Cluster.Get(_sys1).SelfAddress);
await WithinAsync(10.Seconds(), async () =>
{
await AwaitAssertAsync(() =>
{
Cluster.Get(_sys1).State.Members.Count.Should().Be(3);
Cluster.Get(_sys1).State.Members.All(x => x.Status == MemberStatus.Up).Should().BeTrue();
Cluster.Get(_sys2).State.Members.Count.Should().Be(3);
Cluster.Get(_sys2).State.Members.All(x => x.Status == MemberStatus.Up).Should().BeTrue();
Cluster.Get(_sys3).State.Members.Count.Should().Be(3);
Cluster.Get(_sys3).State.Members.All(x => x.Status == MemberStatus.Up).Should().BeTrue();
});
});
await PingEntities();
}
private async Task RunCoordinatedShutdownWhenLeaving()
{
Cluster.Get(_sys3).Leave(Cluster.Get(_sys1).SelfAddress);
_probe1.ExpectMsg("CS-unbind-1", TimeSpan.FromSeconds(10));
await WithinAsync(20.Seconds(), async () =>
{
await AwaitAssertAsync(() =>
{
Cluster.Get(_sys2).State.Members.Count.Should().Be(2);
Cluster.Get(_sys3).State.Members.Count.Should().Be(2);
});
});
await WithinAsync(10.Seconds(), async () =>
{
await AwaitAssertAsync(() =>
{
Cluster.Get(_sys1).IsTerminated.Should().BeTrue();
_sys1.WhenTerminated.IsCompleted.Should().BeTrue();
});
});
await PingEntities();
}
private async Task RunCoordinatedShutdownWhenDowning()
{
// coordinator is on Sys2
Cluster.Get(_sys2).Down(Cluster.Get(_sys3).SelfAddress);
_probe3.ExpectMsg("CS-unbind-3", TimeSpan.FromSeconds(10));
await WithinAsync(20.Seconds(), async () =>
{
await AwaitAssertAsync(() =>
{
Cluster.Get(_sys2).State.Members.Count.Should().Be(1);
});
});
await WithinAsync(10.Seconds(), async () =>
{
await AwaitAssertAsync(() =>
{
Cluster.Get(_sys3).IsTerminated.Should().BeTrue();
_sys3.WhenTerminated.IsCompleted.Should().BeTrue();
});
});
await PingEntities();
}
}
}