-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ReplicatorSpecs.cs
659 lines (567 loc) · 27.9 KB
/
ReplicatorSpecs.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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
//-----------------------------------------------------------------------
// <copyright file="ReplicatorSpecs.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.Diagnostics;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.Cluster;
using Akka.Configuration;
using Akka.Event;
using Akka.TestKit;
using FluentAssertions;
using FluentAssertions.Extensions;
using Xunit;
using Xunit.Abstractions;
using ConfigurationFactory = Akka.Configuration.ConfigurationFactory;
namespace Akka.DistributedData.Tests
{
public class ReplicatorSpecs : AkkaSpec
{
public static readonly Config SpecConfig;
static ReplicatorSpecs()
{
SpecConfig = ConfigurationFactory.ParseString(@"
akka.loglevel = DEBUG
akka.actor.provider = cluster
akka.remote.dot-netty.tcp.port = 0
akka.remote.dot-netty.tcp.send-buffer-size = 2000000
akka.remote.dot-netty.tcp.receive-buffer-size = 2000000
akka.remote.dot-netty.tcp.maximum-frame-size = 1000000
akka.cluster.sharding.updating-state-timeout = 15s")
.WithFallback(DistributedData.DefaultConfig());
}
private readonly ActorSystem _sys1;
private readonly ActorSystem _sys2;
private readonly ActorSystem _sys3;
private readonly IActorRef _replicator1;
private readonly IActorRef _replicator2;
private readonly IActorRef _replicator3;
private readonly TimeSpan _timeOut;
private readonly WriteTo _writeTwo;
private readonly WriteMajority _writeMajority;
private readonly WriteAll _writeAll;
private readonly ReadFrom _readTwo;
private readonly ReadMajority _readMajority;
private readonly ReadAll _readAll;
private readonly PNCounterDictionaryKey<string> _keyC = new PNCounterDictionaryKey<string>("C");
private readonly ORDictionaryKey<string, Flag> _keyH = new ORDictionaryKey<string, Flag>("H");
private readonly GSetKey<string> _keyI = new GSetKey<string>("I");
private readonly ORMultiValueDictionaryKey<string, string> _keyJ = new ORMultiValueDictionaryKey<string, string>("J");
private readonly LWWDictionaryKey<string, string> _keyK = new LWWDictionaryKey<string, string>("K");
public ReplicatorSpecs(ITestOutputHelper helper) : base(SpecConfig, helper)
{
_sys1 = Sys;
_sys3 = ActorSystem.Create(Sys.Name, Sys.Settings.Config);
_sys2 = ActorSystem.Create(Sys.Name, Sys.Settings.Config);
_timeOut = Dilated(TimeSpan.FromSeconds(3.0));
_writeTwo = new WriteTo(2, _timeOut);
_writeMajority = new WriteMajority(_timeOut);
_writeAll = new WriteAll(_timeOut);
_readTwo = new ReadFrom(2, _timeOut);
_readMajority = new ReadMajority(_timeOut);
_readAll = new ReadAll(_timeOut);
var settings = ReplicatorSettings.Create(Sys)
.WithGossipInterval(TimeSpan.FromSeconds(1.0))
.WithMaxDeltaElements(10);
var props = Replicator.Props(settings);
_replicator1 = _sys1.ActorOf(props, "replicator");
_replicator2 = _sys2.ActorOf(props, "replicator");
_replicator3 = _sys3.ActorOf(props, "replicator");
}
/// <summary>
/// Reproduction spec for https://github.com/akkadotnet/akka.net/issues/4184
/// </summary>
[Fact]
public async Task Bugfix_4184_Merge_ORDictionary()
{
await InitCluster();
UpdateORDictionaryNode2And1();
}
private async Task InitCluster()
{
Cluster.Cluster.Get(_sys1).Join(Cluster.Cluster.Get(_sys1).SelfAddress); // coordinator will initially run on sys1
await AwaitAssertAsync(() => Cluster.Cluster.Get(_sys1).SelfMember.Status.Should().Be(MemberStatus.Up));
Cluster.Cluster.Get(_sys2).Join(Cluster.Cluster.Get(_sys1).SelfAddress);
await WithinAsync(10.Seconds(), async () =>
{
await AwaitAssertAsync(() =>
{
Cluster.Cluster.Get(_sys1).State.Members.Count.Should().Be(2);
Cluster.Cluster.Get(_sys1).State.Members.All(x => x.Status == MemberStatus.Up).Should().BeTrue();
Cluster.Cluster.Get(_sys2).State.Members.Count.Should().Be(2);
Cluster.Cluster.Get(_sys2).State.Members.All(x => x.Status == MemberStatus.Up).Should().BeTrue();
});
});
Cluster.Cluster.Get(_sys3).Join(Cluster.Cluster.Get(_sys1).SelfAddress);
await WithinAsync(10.Seconds(), async () =>
{
await AwaitAssertAsync(() =>
{
Cluster.Cluster.Get(_sys1).State.Members.Count.Should().Be(3);
Cluster.Cluster.Get(_sys1).State.Members.All(x => x.Status == MemberStatus.Up).Should().BeTrue();
Cluster.Cluster.Get(_sys2).State.Members.Count.Should().Be(3);
Cluster.Cluster.Get(_sys2).State.Members.All(x => x.Status == MemberStatus.Up).Should().BeTrue();
Cluster.Cluster.Get(_sys3).State.Members.Count.Should().Be(3);
Cluster.Cluster.Get(_sys3).State.Members.All(x => x.Status == MemberStatus.Up).Should().BeTrue();
});
});
}
private void UpdateORDictionaryNode2And1()
{
var changedProbe = CreateTestProbe(_sys2);
// subscribe to updates for KeyH, then update it with a replication factor of two
_replicator2.Tell(Dsl.Subscribe(_keyH, changedProbe.Ref));
_replicator2.Tell(Dsl.Update(_keyH, ORDictionary<string, Flag>.Empty, _writeTwo, x => x.SetItem(Cluster.Cluster.Get(_sys2), "a", Flag.False)));
// receive local update
changedProbe.ExpectMsg<Changed>(g => Equals(g.Key, _keyH)).Get(_keyH).Entries.SequenceEqual(ImmutableDictionary.CreateRange(new[]
{
new KeyValuePair<string, Flag>("a", Flag.False),
})).ShouldBeTrue();
// push update from node 1
_replicator1.Tell(Dsl.Update(_keyH, ORDictionary<string, Flag>.Empty, _writeTwo, x => x.SetItem(Cluster.Cluster.Get(_sys1), "a", Flag.True)));
// expect replication of update on node 2
changedProbe.ExpectMsg<Changed>(g => Equals(g.Key, _keyH)).Get(_keyH).Entries.SequenceEqual(ImmutableDictionary.CreateRange(new[]
{
new KeyValuePair<string, Flag>("a", Flag.True)
})).ShouldBeTrue();
// add new value to dictionary from node 2
_replicator2.Tell(Dsl.Update(_keyH, ORDictionary<string, Flag>.Empty, _writeTwo, x => x.SetItem(Cluster.Cluster.Get(_sys2), "b", Flag.True)));
changedProbe.ExpectMsg<Changed>(g => Equals(g.Key, _keyH)).Get(_keyH).Entries.SequenceEqual(ImmutableDictionary.CreateRange(new[]
{
new KeyValuePair<string, Flag>("a", Flag.True),
new KeyValuePair<string, Flag>("b", Flag.True)
})).ShouldBeTrue();
}
// <summary>
/// Reproduction spec for replicator duplicate publication.
/// </summary>
[Fact]
public async Task Bugfix_Duplicate_Publish()
{
await InitCluster();
await ReplicatorDuplicatePublish();
}
private async Task ReplicatorDuplicatePublish()
{
var p1 = CreateTestProbe(_sys1);
var p2 = CreateTestProbe(_sys2);
var p3 = CreateTestProbe(_sys3);
var probes = new[] { p1, p2, p3 };
// subscribe to updates on all 3 nodes
_replicator1.Tell(Dsl.Subscribe(_keyI, p1.Ref));
_replicator2.Tell(Dsl.Subscribe(_keyI, p2.Ref));
_replicator3.Tell(Dsl.Subscribe(_keyI, p3.Ref));
// update item on 2
_replicator2.Tell(Dsl.Update(_keyI, GSet<string>.Empty, _writeTwo, a => a.Add("a")));
Sys.Log.Info("Pushed change from sys2 for I");
// wait for write to replicate to all 3 nodes
Within(TimeSpan.FromSeconds(5), () =>
{
foreach (var p in probes)
p.ExpectMsg<Changed>(c => c.Get(_keyI).Elements.ShouldBe(ImmutableHashSet.Create("a")));
});
// create duplicate write on node 1
Sys.Log.Info("Pushing change from sys1 for I");
_replicator1.Tell(Dsl.Update(_keyI, GSet<string>.Empty, _writeTwo, a => a.Add("a")));
// no probe should receive an update
p2.ExpectNoMsg(TimeSpan.FromSeconds(1));
}
/// <summary>
/// Reproduction spec for https://github.com/akkadotnet/akka.net/issues/4198
/// </summary>
[Fact]
public async Task Bugfix_4198_PNCounterDictionary_Merge()
{
await InitCluster();
await PNCounterDictionary_Should_Merge();
}
private async Task PNCounterDictionary_Should_Merge()
{
var p1 = CreateTestProbe(_sys1);
var p2 = CreateTestProbe(_sys2);
var p3 = CreateTestProbe(_sys3);
var probes = new[] {
(p1, _replicator1, Cluster.Cluster.Get(_sys1)),
(p2, _replicator2, Cluster.Cluster.Get(_sys2)),
(p3, _replicator3, Cluster.Cluster.Get(_sys3))
};
AwaitAssert(() =>
{
_replicator1.Tell(Dsl.GetReplicaCount, p1);
p1.ExpectMsg(new ReplicaCount(3));
});
// kick off writes
foreach (var i in Enumerable.Repeat(0, 10))
{
foreach (var (probe, replicator, cluster) in probes)
{
replicator.Tell(Dsl.Update(_keyC, PNCounterDictionary<string>.Empty,
new WriteAll(_timeOut), x => x.Increment(cluster, "x")
.Increment(cluster, "y")), probe);
probe.ExpectMsg(new UpdateSuccess(_keyC, null));
}
}
// perform read to ensure values are consistent
Within(TimeSpan.FromSeconds(5), () =>
{
AwaitAssert(() =>
{
foreach (var (probe, replicator, _) in probes)
{
replicator.Tell(Dsl.Get(_keyC, new ReadAll(_timeOut)), probe);
probe.ExpectMsg<GetSuccess>().Get(_keyC).Entries["x"].ShouldBe(new BigInteger(30.0d));
}
});
});
Sys.Log.Info("Done");
}
/// <summary>
/// Reproduction spec for https://github.com/akkadotnet/akka.net/issues/4400
/// </summary>
[Fact]
public async Task Bugfix_4400_LWWDictionary_Merge()
{
await InitCluster();
var changedProbe = CreateTestProbe(_sys2);
// subscribe to updates for KeyJ, then
_replicator2.Tell(Dsl.Subscribe(_keyK, changedProbe.Ref));
Within(TimeSpan.FromSeconds(2), () =>
{
AwaitAssert(() =>
{
// update it with a replication factor of two
_replicator2.Tell(Dsl.Update(
_keyK,
LWWDictionary<string, string>.Empty,
WriteLocal.Instance,
x => x.SetItem(Cluster.Cluster.Get(_sys2), "a", "A")));
// receive local update
var entries = changedProbe.ExpectMsg<Changed>(g => Equals(g.Key, _keyK)).Get(_keyK).Entries;
entries.Should().BeEquivalentTo(new Dictionary<string, string> {
{"a", "A" }
});
});
});
Within(TimeSpan.FromSeconds(2), () =>
{
AwaitAssert(() =>
{
// push update from node 1
// add item
_replicator1.Tell(Dsl.Update(
_keyK,
LWWDictionary<string, string>.Empty,
WriteLocal.Instance,
x => x.SetItem(Cluster.Cluster.Get(_sys1), "a", "A1")));
var entries = changedProbe.ExpectMsg<Changed>(g => Equals(g.Key, _keyK)).Get(_keyK).Entries;
// expect replication of update on node 2
entries.Should().BeEquivalentTo(new Dictionary<string, string> {
{"a", "A1" }
});
});
});
Within(TimeSpan.FromSeconds(2), () =>
{
AwaitAssert(() =>
{
// remove item
_replicator1.Tell(Dsl.Update(
_keyK,
LWWDictionary<string, string>.Empty,
WriteLocal.Instance,
x => x.Remove(Cluster.Cluster.Get(_sys1), "a")));
var entries = changedProbe.ExpectMsg<Changed>(g => Equals(g.Key, _keyK)).Get(_keyK).Entries;
// expect replication of remove on node 2
entries.Should().BeEquivalentTo(new Dictionary<string, string> ());
});
});
Within(TimeSpan.FromSeconds(2), () =>
{
AwaitAssert(() =>
{
// send multiple updates
_replicator1.Tell(Dsl.Update(
_keyK,
LWWDictionary<string, string>.Empty,
WriteLocal.Instance,
x => x
.SetItem(Cluster.Cluster.Get(_sys1), "a", "A")
.SetItem(Cluster.Cluster.Get(_sys1), "b", "B")));
var entries = changedProbe.ExpectMsg<Changed>(g => Equals(g.Key, _keyK)).Get(_keyK).Entries;
// expect replication of remove on node 2
entries.Should().BeEquivalentTo(new Dictionary<string, string> {
{ "a", "A" },
{ "b", "B" },
});
});
});
}
/// <summary>
/// Reproduction spec for https://github.com/akkadotnet/akka.net/issues/4198
/// </summary>
[Fact]
public async Task Bugfix_4302_ORMultiValueDictionary_Merge()
{
await InitCluster();
await ORMultiValueDictionary_Should_Merge();
}
private async Task ORMultiValueDictionary_Should_Merge()
{
var changedProbe = CreateTestProbe(_sys2);
// subscribe to updates for KeyJ, then
_replicator2.Tell(Dsl.Subscribe(_keyJ, changedProbe.Ref));
Within(TimeSpan.FromSeconds(10), () =>
{
AwaitAssert(() =>
{
// update it with a replication factor of two
_replicator2.Tell(Dsl.Update(
_keyJ,
ORMultiValueDictionary<string, string>.Empty,
WriteLocal.Instance,
x => x.AddItem(Cluster.Cluster.Get(_sys2), "a", "A")));
// receive local update
var entries = changedProbe.ExpectMsg<Changed>(g => Equals(g.Key, _keyJ)).Get(_keyJ).Entries;
VerifyMultiValueDictionaryEntries(
ImmutableDictionary.CreateRange(
new[] {
new KeyValuePair<string, IImmutableSet<string>>("a", ImmutableHashSet.Create("A")),
}),
entries
);
});
});
Within(TimeSpan.FromSeconds(10), () =>
{
AwaitAssert(() =>
{
// push update from node 1
// add item
_replicator1.Tell(Dsl.Update(
_keyJ,
ORMultiValueDictionary<string, string>.Empty,
WriteLocal.Instance,
x => x.AddItem(Cluster.Cluster.Get(_sys1), "a", "A1")));
// expect replication of update on node 2
VerifyMultiValueDictionaryEntries(
ImmutableDictionary.CreateRange(
new[] {
new KeyValuePair<string, IImmutableSet<string>>("a", ImmutableHashSet.Create(new []{"A1", "A" })),
}),
changedProbe.ExpectMsg<Changed>(g => Equals(g.Key, _keyJ)).Get(_keyJ).Entries);
});
});
Within(TimeSpan.FromSeconds(10), () =>
{
AwaitAssert(() =>
{
// remove item
_replicator1.Tell(Dsl.Update(
_keyJ,
ORMultiValueDictionary<string, string>.Empty,
WriteLocal.Instance,
x => x.RemoveItem(Cluster.Cluster.Get(_sys1), "a", "A")));
VerifyMultiValueDictionaryEntries(ImmutableDictionary.CreateRange(
new[] {
new KeyValuePair<string, IImmutableSet<string>>("a", ImmutableHashSet.Create("A1")),
}),
changedProbe.ExpectMsg<Changed>(g => Equals(g.Key, _keyJ)).Get(_keyJ).Entries);
});
});
Within(TimeSpan.FromSeconds(10), () =>
{
AwaitAssert(() =>
{
// replace item
_replicator1.Tell(Dsl.Update(
_keyJ,
ORMultiValueDictionary<string, string>.Empty,
WriteLocal.Instance,
x => x.ReplaceItem(Cluster.Cluster.Get(_sys1), "a", "A1", "A")));
VerifyMultiValueDictionaryEntries(ImmutableDictionary.CreateRange(
new[] {
new KeyValuePair<string, IImmutableSet<string>>("a", ImmutableHashSet.Create("A")),
}),
changedProbe.ExpectMsg<Changed>(g => Equals(g.Key, _keyJ)).Get(_keyJ).Entries);
});
});
Within(TimeSpan.FromSeconds(10), () =>
{
AwaitAssert(() =>
{
// add new value to dictionary from node 2
_replicator2.Tell(Dsl.Update(
_keyJ,
ORMultiValueDictionary<string, string>.Empty,
WriteLocal.Instance,
x => x.SetItems(Cluster.Cluster.Get(_sys2), "b", ImmutableHashSet.Create("B"))));
VerifyMultiValueDictionaryEntries(ImmutableDictionary.CreateRange(
new[] {
new KeyValuePair<string, IImmutableSet<string>>("a", ImmutableHashSet.Create("A")),
new KeyValuePair<string, IImmutableSet<string>>("b", ImmutableHashSet.Create("B")),
}),
changedProbe.ExpectMsg<Changed>(g => Equals(g.Key, _keyJ)).Get(_keyJ).Entries);
});
});
}
private void VerifyMultiValueDictionaryEntries(
IImmutableDictionary<string, IImmutableSet<string>> expected,
IImmutableDictionary<string, IImmutableSet<string>> entries)
{
expected.Count.Should().Equals(entries.Count);
foreach (var kvp in entries)
{
expected.ContainsKey(kvp.Key).Should().BeTrue();
expected.Values.Count().Should().Equals(expected[kvp.Key].Count());
foreach (var value in kvp.Value)
{
expected[kvp.Key].Contains(value).Should().BeTrue();
}
}
}
/// <summary>
/// Reproduction spec for https://github.com/akkadotnet/akka.net/issues/4367
/// </summary>
[Fact]
public async Task Bugfix_4367_ORMultiValueDictionary_WithValueDeltas_DeltaGroup_Should_Cast_To_ORSet()
{
await InitCluster();
var changedProbe2 = CreateTestProbe(_sys2);
_replicator2.Tell(Dsl.Subscribe(_keyJ, changedProbe2.Ref));
var changedProbe3 = CreateTestProbe(_sys3);
_replicator3.Tell(Dsl.Subscribe(_keyJ, changedProbe3.Ref));
// Scenario 1 - add 1 entry with multiple values to all nodes
var keyA = "A";
var entryA = ImmutableHashSet<string>.Empty.Add("1").Add("2").Add("3").Add("4");
await AwaitAssertAsync(async () => {
var m1 = await _replicator1.Ask<UpdateSuccess>(Dsl.Update(
_keyJ,
ORMultiValueDictionary<string, string>.EmptyWithValueDeltas,
new WriteMajority(_timeOut),
s => s.SetItems(Cluster.Cluster.Get(_sys1), keyA, entryA)));
var node2EntriesA = changedProbe2.ExpectMsg<Changed>(g => Equals(g.Key, _keyJ)).Get(_keyJ).Entries;
node2EntriesA[keyA].Should().BeEquivalentTo(entryA);
var node3EntriesA = changedProbe3.ExpectMsg<Changed>(g => Equals(g.Key, _keyJ)).Get(_keyJ).Entries;
node3EntriesA[keyA].Should().BeEquivalentTo(entryA);
});
// Scenario 2 - add 2 entries to all nodes
var valuesBC = ImmutableDictionary<string, IImmutableSet<string>>.Empty
.Add("B", ImmutableHashSet<string>.Empty.Add("1").Add("4"))
.Add("C", ImmutableHashSet<string>.Empty.Add("0"));
await AwaitAssertAsync(async () =>
{
var m2 = await _replicator1.Ask<UpdateSuccess>(Dsl.Update(
_keyJ,
ORMultiValueDictionary<string, string>.EmptyWithValueDeltas,
new WriteMajority(_timeOut),
s => s.SetItems(Cluster.Cluster.Get(_sys1), valuesBC.Keys.First(), valuesBC.Values.First())
.SetItems(Cluster.Cluster.Get(_sys1), valuesBC.Keys.Last(), valuesBC.Values.Last())));
var node2EntriesBC = changedProbe2.ExpectMsg<Changed>(g => Equals(g.Key, _keyJ)).Get(_keyJ).Entries;
node2EntriesBC.Count.Should().Be(3);
var node3EntriesBC = changedProbe3.ExpectMsg<Changed>(g => Equals(g.Key, _keyJ)).Get(_keyJ).Entries;
node3EntriesBC.Count.Should().Be(3);
});
// Scenario 3 - modify set with existing items in it
var entryA1 = entryA.Add("6");
ORMultiValueDictionary<string, string> node2EntriesBCA = null;
await AwaitAssertAsync(async () =>
{
// Used to fail on Node2 and Node3 due to "Trying to merge two ORMultiValueDictionaries of different map sub-types" error here
var m3 = await _replicator1.Ask<UpdateSuccess>(Dsl.Update(
_keyJ,
ORMultiValueDictionary<string, string>.EmptyWithValueDeltas,
new WriteMajority(_timeOut),
s => s.SetItems(Cluster.Cluster.Get(_sys1), keyA, entryA1)));
node2EntriesBCA = changedProbe2.ExpectMsg<Changed>(g => Equals(g.Key, _keyJ)).Get(_keyJ);
node2EntriesBCA.Entries["A"].Should().BeEquivalentTo("1", "2", "3", "4", "6");
var node3EntriesBCA = changedProbe3.ExpectMsg<Changed>(g => Equals(g.Key, _keyJ)).Get(_keyJ).Entries;
node3EntriesBCA["A"].Should().BeEquivalentTo("1", "2", "3", "4", "6");
});
// Trigger update from Node2 back to Node 1
var changedProbe1 = CreateTestProbe(_sys1);
_replicator1.Tell(Dsl.Subscribe(_keyJ, changedProbe1.Ref));
var entryA2 = entryA1.Add("5");
await AwaitAssertAsync(async () =>
{
var m4 = await _replicator2.Ask<UpdateSuccess>(Dsl.Update(
_keyJ,
node2EntriesBCA, // use the last value we received
new WriteMajority(_timeOut),
s => s.SetItems(Cluster.Cluster.Get(_sys2), keyA, entryA2)));
var node1EntriesBCA = changedProbe1.ExpectMsg<Changed>(g => Equals(g.Key, _keyJ)).Get(_keyJ).Entries;
node1EntriesBCA["A"].Should().BeEquivalentTo("1", "2", "3", "4", "5", "6");
});
}
// Reproduction spec for issue #5663
[Fact]
public async Task ORMultiValueDictionary_WithValueDeltas_LargeDataSet()
{
await InitCluster();
var changedProbe2 = CreateTestProbe(_sys2);
_replicator2.Tell(Dsl.Subscribe(_keyJ, changedProbe2.Ref));
var changedProbe3 = CreateTestProbe(_sys3);
_replicator3.Tell(Dsl.Subscribe(_keyJ, changedProbe3.Ref));
var messages = Enumerable.Range(0, 20000).Select(i => i.ToString()).ToList();
// Scenario 1 - add 1 entry with multiple values to all nodes
var keyA = "A";
var entryA = messages.ToImmutableHashSet();
var stopwatch = Stopwatch.StartNew();
try
{
await _replicator1.Ask<UpdateSuccess>(Dsl.Update(
_keyJ,
ORMultiValueDictionary<string, string>.EmptyWithValueDeltas,
new WriteMajority(_timeOut),
s => s.SetItems(Cluster.Cluster.Get(_sys1), keyA, entryA)));
}
finally
{
stopwatch.Stop();
}
Log.Info($"Update time: {stopwatch.ElapsedMilliseconds} ms ({stopwatch.ElapsedMilliseconds / 1000.0} s)");
var node2EntriesA = changedProbe2.ExpectMsg<Changed>(g => Equals(g.Key, _keyJ)).Get(_keyJ).Entries;
node2EntriesA[keyA].Should().BeEquivalentTo(entryA);
var node3EntriesA = changedProbe3.ExpectMsg<Changed>(g => Equals(g.Key, _keyJ)).Get(_keyJ).Entries;
node3EntriesA[keyA].Should().BeEquivalentTo(entryA);
// Scenario 2 - modify set with existing items in it
var entryA1 = entryA.Add("999999").Add("1000000");
stopwatch = Stopwatch.StartNew();
try
{
await _replicator1.Ask<UpdateSuccess>(Dsl.Update(
_keyJ,
ORMultiValueDictionary<string, string>.EmptyWithValueDeltas,
new WriteMajority(_timeOut),
s => s.SetItems(Cluster.Cluster.Get(_sys1), keyA, entryA1)));
}
finally
{
stopwatch.Stop();
}
Log.Info($"Single update time: {stopwatch.ElapsedMilliseconds} ms ({stopwatch.ElapsedMilliseconds / 1000.0} s)");
var node2Changed = changedProbe2.ExpectMsg<Changed>(g => Equals(g.Key, _keyJ), TimeSpan.FromSeconds(3));
var node2EntriesBCA = node2Changed.Get(_keyJ);
node2EntriesBCA.Entries["A"].Should().BeEquivalentTo(entryA1);
var node3Changed = changedProbe3.ExpectMsg<Changed>(g => Equals(g.Key, _keyJ), TimeSpan.FromSeconds(3));
var node3EntriesBCA = node3Changed.Get(_keyJ).Entries;
node3EntriesBCA["A"].Should().BeEquivalentTo(entryA1);
}
protected override async Task AfterAllAsync()
{
await base.AfterAllAsync();
await ShutdownAsync(_sys1);
await ShutdownAsync(_sys2);
await ShutdownAsync(_sys3);
GC.Collect();
}
}
}