-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ClusterShardingCustomShardAllocationSpec.cs
292 lines (248 loc) · 10.1 KB
/
ClusterShardingCustomShardAllocationSpec.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
//-----------------------------------------------------------------------
// <copyright file="ClusterShardingCustomShardAllocationSpec.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.Collections.Immutable;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.MultiNode.TestAdapter;
using Akka.Remote.TestKit;
using Akka.TestKit;
using Akka.TestKit.TestActors;
using FluentAssertions;
namespace Akka.Cluster.Sharding.Tests
{
public class ClusterShardingCustomShardAllocationSpecConfig : MultiNodeClusterShardingConfig
{
public RoleName First { get; }
public RoleName Second { get; }
public ClusterShardingCustomShardAllocationSpecConfig(StateStoreMode mode)
: base(mode: mode, loglevel: "DEBUG", additionalConfig: @"
akka.cluster.sharding.rebalance-interval = 1 s
")
{
First = Role("first");
Second = Role("second");
}
}
public class PersistentClusterShardingCustomShardAllocationSpecConfig : ClusterShardingCustomShardAllocationSpecConfig
{
public PersistentClusterShardingCustomShardAllocationSpecConfig()
: base(StateStoreMode.Persistence)
{
}
}
public class DDataClusterShardingCustomShardAllocationSpecConfig : ClusterShardingCustomShardAllocationSpecConfig
{
public DDataClusterShardingCustomShardAllocationSpecConfig()
: base(StateStoreMode.DData)
{
}
}
public class PersistentClusterShardingCustomShardAllocationSpec : ClusterShardingCustomShardAllocationSpec
{
public PersistentClusterShardingCustomShardAllocationSpec()
: base(new PersistentClusterShardingCustomShardAllocationSpecConfig(), typeof(PersistentClusterShardingCustomShardAllocationSpec))
{
}
}
public class DDataClusterShardingCustomShardAllocationSpec : ClusterShardingCustomShardAllocationSpec
{
public DDataClusterShardingCustomShardAllocationSpec()
: base(new DDataClusterShardingCustomShardAllocationSpecConfig(), typeof(DDataClusterShardingCustomShardAllocationSpec))
{
}
}
public abstract class ClusterShardingCustomShardAllocationSpec : MultiNodeClusterShardingSpec<ClusterShardingCustomShardAllocationSpecConfig>
{
#region setup
internal class AllocateReq
{
public static readonly AllocateReq Instance = new AllocateReq();
private AllocateReq()
{
}
}
internal class UseRegion
{
public readonly IActorRef Region;
public UseRegion(IActorRef region)
{
Region = region;
}
}
internal class UseRegionAck
{
public static readonly UseRegionAck Instance = new UseRegionAck();
private UseRegionAck()
{
}
}
internal class RebalanceReq
{
public static readonly RebalanceReq Instance = new RebalanceReq();
private RebalanceReq()
{
}
}
internal class RebalanceShards
{
public readonly IImmutableSet<string> Shards;
public RebalanceShards(IImmutableSet<string> shards)
{
Shards = shards;
}
}
internal class RebalanceShardsAck
{
public static readonly RebalanceShardsAck Instance = new RebalanceShardsAck();
private RebalanceShardsAck()
{
}
}
internal class Allocator : ActorBase
{
IActorRef UseRegion;
IImmutableSet<string> Rebalance = ImmutableHashSet<string>.Empty;
protected override bool Receive(object message)
{
switch (message)
{
case UseRegion r:
UseRegion = r.Region;
Sender.Tell(UseRegionAck.Instance);
return true;
case AllocateReq _:
if (UseRegion != null)
Sender.Tell(UseRegion);
return true;
case RebalanceShards rs:
Rebalance = rs.Shards;
Sender.Tell(RebalanceShardsAck.Instance);
return true;
case RebalanceReq _:
Sender.Tell(Rebalance);
Rebalance = ImmutableHashSet<string>.Empty;
return true;
}
return false;
}
}
internal class TestAllocationStrategy : IShardAllocationStrategy
{
public readonly IActorRef Ref;
public TestAllocationStrategy(IActorRef @ref)
{
Ref = @ref;
}
public Task<IActorRef> AllocateShard(IActorRef requester, string shardId, IImmutableDictionary<IActorRef, IImmutableList<string>> currentShardAllocations)
{
return Ref.Ask<IActorRef>(AllocateReq.Instance);
}
public Task<IImmutableSet<string>> Rebalance(IImmutableDictionary<IActorRef, IImmutableList<string>> currentShardAllocations, IImmutableSet<string> rebalanceInProgress)
{
return Ref.Ask<IImmutableSet<string>>(RebalanceReq.Instance);
}
}
private readonly Lazy<IActorRef> _region;
private readonly Lazy<IActorRef> _allocator;
protected ClusterShardingCustomShardAllocationSpec(ClusterShardingCustomShardAllocationSpecConfig config, Type type)
: base(config, type)
{
_region = new Lazy<IActorRef>(() => ClusterSharding.Get(Sys).ShardRegion("Entity"));
_allocator = new Lazy<IActorRef>(() => Sys.ActorOf(Props.Create<Allocator>(), "allocator"));
}
private void Join(RoleName from, RoleName to)
{
Join(from, to, () =>
StartSharding(
Sys,
typeName: "Entity",
entityProps: SimpleEchoActor.Props(),
extractEntityId: IntExtractEntityId,
extractShardId: IntExtractShardId,
allocationStrategy: new TestAllocationStrategy(_allocator.Value))
);
}
#endregion
[MultiNodeFact]
public void Cluster_sharding_with_custom_allocation_strategy_specs()
{
Cluster_sharding_with_custom_allocation_strategy_must_use_specified_region();
Cluster_sharding_with_custom_allocation_strategy_must_rebalance_specified_shards();
}
private void Cluster_sharding_with_custom_allocation_strategy_must_use_specified_region()
{
Within(TimeSpan.FromSeconds(30), () =>
{
StartPersistenceIfNeeded(startOn: config.First, config.First, config.Second);
Join(config.First, config.First);
RunOn(() =>
{
_allocator.Value.Tell(new UseRegion(_region.Value));
ExpectMsg<UseRegionAck>();
_region.Value.Tell(1);
ExpectMsg(1);
LastSender.Path.Should().Be(_region.Value.Path / "1" / "1");
}, config.First);
EnterBarrier("first-started");
Join(config.Second, config.First);
_region.Value.Tell(2);
ExpectMsg(2);
RunOn(() =>
{
LastSender.Path.Should().Be(_region.Value.Path / "2" / "2");
}, config.First);
RunOn(() =>
{
LastSender.Path.Should().Be(Node(config.First) / "system" / "sharding" / "Entity" / "2" / "2");
}, config.Second);
EnterBarrier("second-started");
RunOn(() =>
{
Sys.ActorSelection(Node(config.Second) / "system" / "sharding" / "Entity").Tell(new Identify(null));
var secondRegion = ExpectMsg<ActorIdentity>().Subject;
_allocator.Value.Tell(new UseRegion(secondRegion));
ExpectMsg<UseRegionAck>();
}, config.First);
EnterBarrier("second-active");
_region.Value.Tell(3);
ExpectMsg(3);
RunOn(() =>
{
LastSender.Path.Should().Be(_region.Value.Path / "3" / "3");
}, config.Second);
RunOn(() =>
{
LastSender.Path.Should().Be(Node(config.Second) / "system" / "sharding" / "Entity" / "3" / "3");
}, config.First);
EnterBarrier("after-2");
});
}
private void Cluster_sharding_with_custom_allocation_strategy_must_rebalance_specified_shards()
{
Within(TimeSpan.FromSeconds(15), () =>
{
RunOn(() =>
{
_allocator.Value.Tell(new RebalanceShards(ImmutableHashSet.Create("2")));
ExpectMsg<RebalanceShardsAck>();
AwaitAssert(() =>
{
var p = CreateTestProbe();
_region.Value.Tell(2, p.Ref);
p.ExpectMsg(2, TimeSpan.FromSeconds(2));
p.LastSender.Path.Should().Be(Node(config.Second) / "system" / "sharding" / "Entity" / "2" / "2");
});
_region.Value.Tell(1);
ExpectMsg(1);
LastSender.Path.Should().Be(_region.Value.Path / "1" / "1");
}, config.First);
EnterBarrier("after-2");
});
}
}
}