-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ReplicatorSpec.cs
808 lines (672 loc) · 33.8 KB
/
ReplicatorSpec.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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
//-----------------------------------------------------------------------
// <copyright file="ReplicatorSpec.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 Akka.Actor;
using Akka.Configuration;
using Akka.Remote.TestKit;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Akka.Cluster;
using Akka.Cluster.TestKit;
using Akka.Event;
using Akka.MultiNode.TestAdapter;
using Akka.Remote.Transport;
using Akka.TestKit;
using FluentAssertions;
namespace Akka.DistributedData.Tests.MultiNode
{
public class ReplicatorSpecConfig : MultiNodeConfig
{
public RoleName First { get; }
public RoleName Second { get; }
public RoleName Third { get; }
public ReplicatorSpecConfig()
{
First = Role("first");
Second = Role("second");
Third = Role("third");
CommonConfig = ConfigurationFactory.ParseString(@"
akka.actor.provider = cluster
akka.loglevel = INFO
akka.log-dead-letters-during-shutdown = off
").WithFallback(DistributedData.DefaultConfig()).WithFallback(DebugConfig(false));
TestTransport = true;
}
}
public class ReplicatorSpec : MultiNodeClusterSpec
{
private readonly ReplicatorSpecConfig _config;
private readonly Cluster.Cluster _cluster;
private readonly IActorRef _replicator;
private readonly GCounterKey KeyA = new GCounterKey("A");
private readonly GCounterKey KeyB = new GCounterKey("B");
private readonly GCounterKey KeyC = new GCounterKey("C");
private readonly GCounterKey KeyD = new GCounterKey("D");
private readonly GCounterKey KeyE = new GCounterKey("E");
private readonly GCounterKey KeyE2 = new GCounterKey("E2");
private readonly GCounterKey KeyF = new GCounterKey("F");
private readonly ORSetKey<string> KeyG = new ORSetKey<string>("G");
private readonly ORDictionaryKey<string, Flag> KeyH = new ORDictionaryKey<string, Flag>("H");
private readonly GSetKey<string> KeyI = new GSetKey<string>("I");
private readonly GSetKey<string> KeyJ = new GSetKey<string>("J");
private readonly LWWRegisterKey<string> KeyK = new LWWRegisterKey<string>("K");
private readonly GCounterKey KeyX = new GCounterKey("X");
private readonly GCounterKey KeyY = new GCounterKey("Y");
private readonly GCounterKey KeyZ = new GCounterKey("Z");
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 int _afterCounter = 0;
private readonly RoleName _first;
private readonly RoleName _second;
private readonly RoleName _third;
public ReplicatorSpec()
: this(new ReplicatorSpecConfig())
{ }
protected ReplicatorSpec(ReplicatorSpecConfig config)
: base(config, typeof(ReplicatorSpec))
{
_config = config;
_first = config.First;
_second = config.Second;
_third = config.Third;
_cluster = Akka.Cluster.Cluster.Get(Sys);
var settings = ReplicatorSettings.Create(Sys)
.WithGossipInterval(TimeSpan.FromSeconds(1.0))
.WithMaxDeltaElements(10);
var props = Replicator.Props(settings);
_replicator = Sys.ActorOf(props, "replicator");
_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);
}
[MultiNodeFact()]
public void ReplicatorSpecTests()
{
Cluster_CRDT_should_work_in_single_node_cluster();
Cluster_CRDT_should_merge_the_update_with_existing_value();
Cluster_CRDT_should_reply_with_ModifyFailure_if_exception_is_thrown_by_modify_function();
Cluster_CRDT_should_replicate_values_to_new_node();
Cluster_CRDT_should_work_in_2_node_cluster();
Cluster_CRDT_should_be_replicated_after_successful_update();
Cluster_CRDT_should_converge_after_partition();
Cluster_CRDT_should_support_majority_quorum_write_and_read_with_3_nodes_with_1_unreachable();
Cluster_CRDT_should_converge_after_many_concurrent_updates();
Cluster_CRDT_should_read_repair_happens_before_GetSuccess();
Cluster_CRDT_should_check_that_remote_update_and_local_update_both_cause_a_change_event_to_emit_with_the_merged_data();
Cluster_CRDT_should_avoid_duplicate_change_events_for_same_data();
}
public void Cluster_CRDT_should_work_in_single_node_cluster()
{
Join(_first, _first);
RunOn(() =>
{
Within(TimeSpan.FromSeconds(5.0), () =>
{
_replicator.Tell(Dsl.GetReplicaCount);
ExpectMsg(new ReplicaCount(1));
});
var changedProbe = CreateTestProbe();
_replicator.Tell(Dsl.Subscribe(KeyA, changedProbe.Ref));
_replicator.Tell(Dsl.Subscribe(KeyX, changedProbe.Ref));
_replicator.Tell(Dsl.Get(KeyA, ReadLocal.Instance));
ExpectMsg(new NotFound(KeyA, null));
var c3 = GCounter.Empty.Increment(_cluster, 3);
var update = Dsl.Update(KeyA, GCounter.Empty, WriteLocal.Instance, x => x.Increment(_cluster, 3));
_replicator.Tell(update);
ExpectMsg(new UpdateSuccess(KeyA, null));
_replicator.Tell(Dsl.Get(KeyA, ReadLocal.Instance));
ExpectMsg(new GetSuccess(KeyA, null, c3));
changedProbe.ExpectMsg(new Changed(KeyA, c3));
var changedProbe2 = CreateTestProbe();
_replicator.Tell(new Subscribe(KeyA, changedProbe2.Ref));
changedProbe2.ExpectMsg(new Changed(KeyA, c3));
var c4 = c3.Increment(_cluster);
// too strong consistency level
_replicator.Tell(Dsl.Update(KeyA, _writeTwo, x => x.Increment(_cluster)));
ExpectMsg(new UpdateTimeout(KeyA, null), _timeOut.Add(TimeSpan.FromSeconds(1)));
_replicator.Tell(Dsl.Get(KeyA, ReadLocal.Instance));
ExpectMsg(new GetSuccess(KeyA, null, c4));
changedProbe.ExpectMsg(new Changed(KeyA, c4));
var c5 = c4.Increment(_cluster);
// too strong consistency level
_replicator.Tell(Dsl.Update(KeyA, _writeMajority, x => x.Increment(_cluster)));
ExpectMsg(new UpdateSuccess(KeyA, null));
_replicator.Tell(Dsl.Get(KeyA, _readMajority));
ExpectMsg(new GetSuccess(KeyA, null, c5));
changedProbe.ExpectMsg(new Changed(KeyA, c5));
var c6 = c5.Increment(_cluster);
_replicator.Tell(Dsl.Update(KeyA, _writeAll, x => x.Increment(_cluster)));
ExpectMsg(new UpdateSuccess(KeyA, null));
_replicator.Tell(Dsl.Get(KeyA, _readAll));
ExpectMsg(new GetSuccess(KeyA, null, c6));
changedProbe.ExpectMsg(new Changed(KeyA, c6));
var c9 = GCounter.Empty.Increment(_cluster, 9);
_replicator.Tell(Dsl.Update(KeyX, GCounter.Empty, WriteLocal.Instance, x => x.Increment(_cluster, 9)));
ExpectMsg(new UpdateSuccess(KeyX, null));
changedProbe.ExpectMsg(new Changed(KeyX, c9));
_replicator.Tell(Dsl.Delete(KeyX, WriteLocal.Instance));
ExpectMsg(new DeleteSuccess(KeyX));
changedProbe.ExpectMsg(new DataDeleted(KeyX));
_replicator.Tell(Dsl.Get(KeyX, ReadLocal.Instance));
ExpectMsg(new DataDeleted(KeyX));
_replicator.Tell(Dsl.Get(KeyX, _readAll));
ExpectMsg(new DataDeleted(KeyX));
_replicator.Tell(Dsl.Update(KeyX, WriteLocal.Instance, x => x.Increment(_cluster)));
ExpectMsg(new DataDeleted(KeyX));
_replicator.Tell(Dsl.Delete(KeyX, WriteLocal.Instance));
ExpectMsg(new DataDeleted(KeyX));
_replicator.Tell(Dsl.GetKeyIds);
ExpectMsg(new GetKeysIdsResult(ImmutableHashSet<string>.Empty.Add("A")));
}, _first);
EnterBarrierAfterTestStep();
}
public void Cluster_CRDT_should_merge_the_update_with_existing_value()
{
RunOn(() =>
{
var update = new Update(KeyJ, new GSet<string>(), WriteLocal.Instance, x => ((GSet<string>)x).Add("a").Add("b"));
_replicator.Tell(update);
ExpectMsg(new UpdateSuccess(KeyJ, null));
var update2 = new Update(KeyJ, new GSet<string>(), WriteLocal.Instance, x => ((GSet<string>)x).Add("c"));
_replicator.Tell(update2);
ExpectMsg(new UpdateSuccess(KeyJ, null));
_replicator.Tell(new Get(KeyJ, ReadLocal.Instance));
ExpectMsg<GetSuccess>(x => x.Data.Equals(new GSet<string>(new[] { "a", "b", "c" }.ToImmutableHashSet())));
}, _first);
EnterBarrierAfterTestStep();
}
public void Cluster_CRDT_should_reply_with_ModifyFailure_if_exception_is_thrown_by_modify_function()
{
RunOn(() =>
{
var exception = new Exception("Test exception");
Func<IReplicatedData, IReplicatedData> update = x =>
{
throw exception;
};
_replicator.Tell(new Update(KeyA, GCounter.Empty, WriteLocal.Instance, update));
ExpectMsg<ModifyFailure>(x => x.Cause.Equals(exception));
}, _first);
EnterBarrierAfterTestStep();
}
public void Cluster_CRDT_should_replicate_values_to_new_node()
{
Join(_second, _first);
RunOn(() =>
Within(TimeSpan.FromSeconds(10), () =>
AwaitAssert(() =>
{
_replicator.Tell(Dsl.GetReplicaCount);
ExpectMsg(new ReplicaCount(2));
})),
_first, _second);
EnterBarrier("2-nodes");
RunOn(() =>
{
var changedProbe = CreateTestProbe();
_replicator.Tell(Dsl.Subscribe(KeyA, changedProbe.Ref));
// "A" should be replicated via gossip to the new node
Within(TimeSpan.FromSeconds(5), () =>
AwaitAssert(() =>
{
// for some reason result is returned before CRDT gets replicated
_replicator.Tell(Dsl.Get(KeyA, ReadLocal.Instance));
var c = ExpectMsg<GetSuccess>(g => Equals(g.Key, KeyA)).Get(KeyA);
c.Value.ShouldBe(6UL);
}));
var c2 = changedProbe.ExpectMsg<Changed>(g => Equals(g.Key, KeyA)).Get(KeyA);
c2.Value.ShouldBe(6UL);
}, _second);
EnterBarrierAfterTestStep();
}
public void Cluster_CRDT_should_work_in_2_node_cluster()
{
RunOn(() =>
{
// start with 20 on both nodes
_replicator.Tell(Dsl.Update(KeyB, GCounter.Empty, WriteLocal.Instance, x => x.Increment(_cluster, 20)));
ExpectMsg(new UpdateSuccess(KeyB, null));
// add 1 on both nodes using WriteTwo
_replicator.Tell(Dsl.Update(KeyB, GCounter.Empty, _writeTwo, x => x.Increment(_cluster, 1)));
ExpectMsg(new UpdateSuccess(KeyB, null));
// the total, after replication should be 42
AwaitAssert(() =>
{
_replicator.Tell(Dsl.Get(KeyB, _readTwo));
var c = ExpectMsg<GetSuccess>(g => Equals(g.Key, KeyB)).Get(KeyB);
c.Value.ShouldBe(42UL);
});
}, _first, _second);
EnterBarrier("update-42");
RunOn(() =>
{
// add 1 on both nodes using WriteAll
_replicator.Tell(Dsl.Update(KeyB, GCounter.Empty, _writeAll, x => x.Increment(_cluster, 1)));
ExpectMsg(new UpdateSuccess(KeyB, null));
// the total, after replication should be 44
AwaitAssert(() =>
{
_replicator.Tell(Dsl.Get(KeyB, _readAll));
var c = ExpectMsg<GetSuccess>(g => Equals(g.Key, KeyB)).Get(KeyB);
c.Value.ShouldBe(44UL);
});
}, _first, _second);
EnterBarrier("update-44");
RunOn(() =>
{
// add 1 on both nodes using WriteMajority
_replicator.Tell(Dsl.Update(KeyB, GCounter.Empty, _writeMajority, x => x.Increment(_cluster, 1)));
ExpectMsg(new UpdateSuccess(KeyB, null));
// the total, after replication should be 46
AwaitAssert(() =>
{
_replicator.Tell(Dsl.Get(KeyB, _readMajority));
var c = ExpectMsg<GetSuccess>(g => Equals(g.Key, KeyB)).Get(KeyB);
c.Value.ShouldBe(46UL);
});
}, _first, _second);
EnterBarrierAfterTestStep();
}
public void Cluster_CRDT_should_be_replicated_after_successful_update()
{
var changedProbe = CreateTestProbe();
RunOn(() =>
{
_replicator.Tell(Dsl.Subscribe(KeyC, changedProbe.Ref));
}, _first, _second);
RunOn(() =>
{
_replicator.Tell(Dsl.Update(KeyC, GCounter.Empty, _writeTwo, x => x.Increment(_cluster, 30)));
ExpectMsg(new UpdateSuccess(KeyC, null));
changedProbe.ExpectMsg<Changed>(c => Equals(c.Key, KeyC)).Get(KeyC).Value.ShouldBe(30UL);
_replicator.Tell(Dsl.Update(KeyY, GCounter.Empty, _writeTwo, x => x.Increment(_cluster, 30)));
ExpectMsg(new UpdateSuccess(KeyY, null));
_replicator.Tell(Dsl.Update(KeyZ, GCounter.Empty, _writeMajority, x => x.Increment(_cluster, 30)));
ExpectMsg(new UpdateSuccess(KeyZ, null));
}, _first);
EnterBarrier("update-c30");
RunOn(() =>
{
_replicator.Tell(Dsl.Get(KeyC, ReadLocal.Instance));
var c30 = ExpectMsg<GetSuccess>(c => Equals(c.Key, KeyC)).Get(KeyC);
c30.Value.ShouldBe(30UL);
changedProbe.ExpectMsg<Changed>(c => Equals(c.Key, KeyC)).Get(KeyC).Value.ShouldBe(30UL);
// replicate with gossip after WriteLocal
_replicator.Tell(Dsl.Update(KeyC, GCounter.Empty, WriteLocal.Instance, x => x.Increment(_cluster, 1)));
ExpectMsg(new UpdateSuccess(KeyC, null));
changedProbe.ExpectMsg<Changed>(c => Equals(c.Key, KeyC)).Get(KeyC).Value.ShouldBe(31UL);
_replicator.Tell(Dsl.Delete(KeyY, WriteLocal.Instance, 777));
ExpectMsg(new DeleteSuccess(KeyY, 777));
_replicator.Tell(Dsl.Get(KeyZ, _readMajority));
ExpectMsg<GetSuccess>(c => Equals(c.Key, KeyZ)).Get(KeyZ).Value.ShouldBe(30UL);
}, _second);
EnterBarrier("update-c31");
RunOn(() =>
{
// KeyC and deleted KeyY should be replicated via gossip to the other node
Within(TimeSpan.FromSeconds(5), () =>
{
AwaitAssert(() =>
{
_replicator.Tell(Dsl.Get(KeyC, ReadLocal.Instance));
var c = ExpectMsg<GetSuccess>(g => Equals(g.Key, KeyC)).Get(KeyC);
c.Value.ShouldBe(31UL);
_replicator.Tell(Dsl.Get(KeyY, ReadLocal.Instance));
ExpectMsg(new DataDeleted(KeyY));
});
});
changedProbe.ExpectMsg<Changed>(c => Equals(c.Key, KeyC)).Get(KeyC).Value.ShouldBe(31UL);
}, _first);
EnterBarrier("verified-c31");
// and also for concurrent updates
RunOn(() =>
{
_replicator.Tell(Dsl.Get(KeyC, ReadLocal.Instance));
var c31 = ExpectMsg<GetSuccess>(g => Equals(g.Key, KeyC)).Get(KeyC);
c31.Value.ShouldBe(31UL);
_replicator.Tell(Dsl.Update(KeyC, GCounter.Empty, WriteLocal.Instance, x => x.Increment(_cluster, 1)));
ExpectMsg(new UpdateSuccess(KeyC, null));
Within(TimeSpan.FromSeconds(5), () =>
{
AwaitAssert(() =>
{
_replicator.Tell(Dsl.Get(KeyC, ReadLocal.Instance));
var c = ExpectMsg<GetSuccess>(g => Equals(g.Key, KeyC), TimeSpan.FromMilliseconds(300)).Get(KeyC);
c.Value.ShouldBe(33UL);
}, interval: TimeSpan.FromMilliseconds(300));
});
}, _first, _second);
EnterBarrierAfterTestStep();
}
public void Cluster_CRDT_should_converge_after_partition()
{
RunOn(() =>
{
_replicator.Tell(Dsl.Update(KeyD, GCounter.Empty, _writeTwo, x => x.Increment(_cluster, 40)));
ExpectMsg(new UpdateSuccess(KeyD, null));
TestConductor.Blackhole(_first, _second, ThrottleTransportAdapter.Direction.Both)
.Wait(TimeSpan.FromSeconds(10));
}, _first);
EnterBarrier("blackhole-first-second");
RunOn(() =>
{
_replicator.Tell(Dsl.Get(KeyD, ReadLocal.Instance));
var c40 = ExpectMsg<GetSuccess>(g => Equals(g.Key, KeyD)).Get(KeyD);
c40.Value.ShouldBe(40UL);
_replicator.Tell(Dsl.Update(KeyD, GCounter.Empty.Increment(_cluster, 1), _writeTwo, x => x.Increment(_cluster, 1)));
ExpectMsg(new UpdateTimeout(KeyD, null), _timeOut.Add(TimeSpan.FromSeconds(1)));
_replicator.Tell(Dsl.Update(KeyD, GCounter.Empty, _writeTwo, x => x.Increment(_cluster, 1)));
ExpectMsg(new UpdateTimeout(KeyD, null), _timeOut.Add(TimeSpan.FromSeconds(1)));
}, _first, _second);
RunOn(() =>
{
for (ulong i = 1; i <= 30UL; i++)
{
var n = i;
var keydn = new GCounterKey("D" + n);
_replicator.Tell(Dsl.Update(keydn, GCounter.Empty, WriteLocal.Instance, x => x.Increment(_cluster, n)));
ExpectMsg(new UpdateSuccess(keydn, null));
}
}, _first);
EnterBarrier("updates-during-partion");
RunOn(() =>
{
TestConductor.PassThrough(_first, _second, ThrottleTransportAdapter.Direction.Both)
.Wait(TimeSpan.FromSeconds(5));
}, _first);
EnterBarrier("passThrough-first-second");
RunOn(() =>
{
_replicator.Tell(Dsl.Get(KeyD, _readTwo));
var c44 = ExpectMsg<GetSuccess>(g => Equals(g.Key, KeyD)).Get(KeyD);
c44.Value.ShouldBe(44UL);
Within(TimeSpan.FromSeconds(10), () =>
AwaitAssert(() =>
{
for (ulong i = 1; i <= 30UL; i++)
{
var keydn = new GCounterKey("D" + i);
_replicator.Tell(Dsl.Get(keydn, ReadLocal.Instance));
ExpectMsg<GetSuccess>(g => Equals(g.Key, keydn), TimeSpan.FromMilliseconds(50)).Get(keydn).Value.ShouldBe(i);
}
}));
}, _first, _second);
EnterBarrierAfterTestStep();
}
public void Cluster_CRDT_should_support_majority_quorum_write_and_read_with_3_nodes_with_1_unreachable()
{
Join(_third, _first);
RunOn(() =>
{
Within(TimeSpan.FromSeconds(10), () => AwaitAssert(() =>
{
_replicator.Tell(Dsl.GetReplicaCount);
ExpectMsg(new ReplicaCount(3));
}));
}, _first, _second, _third);
EnterBarrier("3-nodes");
RunOn(() =>
{
_replicator.Tell(Dsl.Update(KeyE, GCounter.Empty, _writeMajority, x => x.Increment(_cluster, 50)));
ExpectMsg(new UpdateSuccess(KeyE, null));
}, _first, _second, _third);
EnterBarrier("write-initial-majority");
RunOn(() =>
{
_replicator.Tell(Dsl.Get(KeyE, _readMajority));
var c150 = ExpectMsg<GetSuccess>(g => Equals(g.Key, KeyE)).Get(KeyE);
c150.Value.ShouldBe(150UL);
}, _first, _second, _third);
EnterBarrier("read-initial-majority");
RunOn(() =>
{
TestConductor.Blackhole(_first, _third, ThrottleTransportAdapter.Direction.Both).Wait(TimeSpan.FromSeconds(5));
TestConductor.Blackhole(_second, _third, ThrottleTransportAdapter.Direction.Both).Wait(TimeSpan.FromSeconds(5));
}, _first);
EnterBarrier("blackhole-third");
RunOn(() =>
{
_replicator.Tell(Dsl.Update(KeyE, GCounter.Empty, WriteLocal.Instance, x => x.Increment(_cluster, 1)));
ExpectMsg(new UpdateSuccess(KeyE, null));
}, _second);
EnterBarrier("local-update-from-second");
RunOn(() =>
{
// ReadMajority should retrieve the previous update from second, before applying the modification
var probe1 = CreateTestProbe();
var probe2 = CreateTestProbe();
_replicator.Tell(Dsl.Get(KeyE, _readMajority), probe2.Ref);
probe2.ExpectMsg<GetSuccess>();
_replicator.Tell(Dsl.Update(KeyE, GCounter.Empty, _writeMajority, data =>
{
probe1.Ref.Tell(data.Value);
return data.Increment(_cluster, 1);
}), probe2.Ref);
// verify read your own writes, without waiting for the UpdateSuccess reply
// note that the order of the replies are not defined, and therefore we use separate probes
var probe3 = CreateTestProbe();
_replicator.Tell(Dsl.Get(KeyE, _readMajority), probe3.Ref);
probe1.ExpectMsg(151UL);
probe2.ExpectMsg(new UpdateSuccess(KeyE, null));
var c152 = probe3.ExpectMsg<GetSuccess>(g => Equals(g.Key, KeyE)).Get(KeyE);
c152.Value.ShouldBe(152UL);
}, _first);
EnterBarrier("majority-update-from-first");
RunOn(() =>
{
var probe1 = CreateTestProbe();
_replicator.Tell(Dsl.Get(KeyE, _readMajority), probe1.Ref);
probe1.ExpectMsg<GetSuccess>();
_replicator.Tell(Dsl.Update(KeyE, GCounter.Empty, _writeMajority, 153, x => x.Increment(_cluster, 1)), probe1.Ref);
// verify read your own writes, without waiting for the UpdateSuccess reply
// note that the order of the replies are not defined, and therefore we use separate probes
var probe2 = CreateTestProbe();
_replicator.Tell(Dsl.Update(KeyE, GCounter.Empty, _writeMajority, 154, x => x.Increment(_cluster, 1)), probe2.Ref);
var probe3 = CreateTestProbe();
_replicator.Tell(Dsl.Update(KeyE, GCounter.Empty, _writeMajority, 155, x => x.Increment(_cluster, 1)), probe3.Ref);
var probe5 = CreateTestProbe();
_replicator.Tell(Dsl.Get(KeyE, _readMajority), probe5.Ref);
probe1.ExpectMsg(new UpdateSuccess(KeyE, 153));
probe2.ExpectMsg(new UpdateSuccess(KeyE, 154));
probe3.ExpectMsg(new UpdateSuccess(KeyE, 155));
var c155 = probe5.ExpectMsg<GetSuccess>(g => Equals(g.Key, KeyE)).Get(KeyE);
c155.Value.ShouldBe(155UL);
}, _second);
EnterBarrier("majority-update-from-second");
RunOn(() =>
{
_replicator.Tell(Dsl.Get(KeyE2, _readAll, 998));
ExpectMsg(new GetFailure(KeyE2, 998), _timeOut.Add(TimeSpan.FromSeconds(1)));
_replicator.Tell(Dsl.Get(KeyE2, Dsl.ReadLocal));
ExpectMsg(new NotFound(KeyE2, null));
}, _first, _second);
EnterBarrier("read-all-fail-update");
RunOn(() =>
{
Sys.Log.Info("Opening up traffic to third node again...");
TestConductor.PassThrough(_first, _third, ThrottleTransportAdapter.Direction.Both).Wait(TimeSpan.FromSeconds(5));
TestConductor.PassThrough(_second, _third, ThrottleTransportAdapter.Direction.Both).Wait(TimeSpan.FromSeconds(5));
Sys.Log.Info("Traffic open to node 3.");
}, _first);
EnterBarrier("passThrough-third");
RunOn(() =>
{
_replicator.Tell(Dsl.Get(KeyE, _readMajority));
var c155 = ExpectMsg<GetSuccess>(g => Equals(g.Key, KeyE)).Get(KeyE);
c155.Value.ShouldBe(155UL);
}, _third);
EnterBarrierAfterTestStep();
}
public void Cluster_CRDT_should_converge_after_many_concurrent_updates()
{
Within(TimeSpan.FromSeconds(10), () =>
{
RunOn(() =>
{
var c = GCounter.Empty;
for (ulong i = 0; i < 100UL; i++)
{
c = c.Increment(_cluster, i);
_replicator.Tell(Dsl.Update(KeyF, GCounter.Empty, _writeTwo, x => x.Increment(_cluster, 1)));
}
var results = ReceiveN(100);
results.All(x => x is UpdateSuccess).ShouldBeTrue();
}, _first, _second, _third);
EnterBarrier("100-updates-done");
RunOn(() =>
{
_replicator.Tell(Dsl.Get(KeyF, _readTwo));
var c = ExpectMsg<GetSuccess>(g => Equals(g.Key, KeyF)).Get(KeyF);
c.Value.ShouldBe(3 * 100UL);
}, _first, _second, _third);
EnterBarrierAfterTestStep();
});
}
public void Cluster_CRDT_should_read_repair_happens_before_GetSuccess()
{
RunOn(() =>
{
_replicator.Tell(Dsl.Update(KeyG, ORSet<string>.Empty, _writeTwo, x => x
.Add(_cluster, "a")
.Add(_cluster, "b")));
ExpectMsg<UpdateSuccess>();
}, _first);
EnterBarrier("a-b-added-to-G");
RunOn(() =>
{
_replicator.Tell(Dsl.Get(KeyG, _readAll));
ExpectMsg<GetSuccess>(g => Equals(g.Key, KeyG)).Get(KeyG).Elements.SetEquals(new[] { "a", "b" });
_replicator.Tell(Dsl.Get(KeyG, ReadLocal.Instance));
ExpectMsg<GetSuccess>(g => Equals(g.Key, KeyG)).Get(KeyG).Elements.SetEquals(new[] { "a", "b" });
}, _second);
EnterBarrierAfterTestStep();
}
public void Cluster_CRDT_should_check_that_remote_update_and_local_update_both_cause_a_change_event_to_emit_with_the_merged_data()
{
var changedProbe = CreateTestProbe();
RunOn(() =>
{
_replicator.Tell(Dsl.Subscribe(KeyH, changedProbe.Ref));
_replicator.Tell(Dsl.Update(KeyH, ORDictionary<string, Flag>.Empty, _writeTwo, x => x.SetItem(_cluster, "a", Flag.False)));
changedProbe.ExpectMsg<Changed>(g => Equals(g.Key, KeyH)).Get(KeyH).Entries.SequenceEqual(ImmutableDictionary.CreateRange(new[]
{
new KeyValuePair<string, Flag>("a", Flag.False),
})).ShouldBeTrue();
}, _second);
EnterBarrier("update-h1");
RunOn(() =>
{
_replicator.Tell(Dsl.Update(KeyH, ORDictionary<string, Flag>.Empty, _writeTwo, x => x.SetItem(_cluster, "a", Flag.True)));
}, _first);
RunOn(() =>
{
changedProbe.ExpectMsg<Changed>(g => Equals(g.Key, KeyH)).Get(KeyH).Entries.SequenceEqual(ImmutableDictionary.CreateRange(new[]
{
new KeyValuePair<string, Flag>("a", Flag.True)
})).ShouldBeTrue();
_replicator.Tell(Dsl.Update(KeyH, ORDictionary<string, Flag>.Empty, _writeTwo, x => x.SetItem(_cluster, "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();
}, _second);
EnterBarrierAfterTestStep();
}
public void Cluster_CRDT_should_avoid_duplicate_change_events_for_same_data()
{
var changedProbe = CreateTestProbe();
_replicator.Tell(Dsl.Subscribe(KeyI, changedProbe.Ref));
EnterBarrier("subscribed-I");
RunOn(() => _replicator.Tell(Dsl.Update(KeyI, GSet<string>.Empty, _writeTwo, a => a.Add("a"))),
_second);
Within(TimeSpan.FromSeconds(5), () =>
{
var changed = changedProbe.ExpectMsg<Changed>(c =>
c.Get(KeyI).Elements.ShouldBe(ImmutableHashSet.Create("a")));
var keyIData = changed.Get(KeyI);
Sys.Log.Debug("DEBUG: Received Changed {0}", changed);
});
EnterBarrier("update-I");
RunOn(() => _replicator.Tell(Dsl.Update(KeyI, GSet<string>.Empty, _writeTwo, a => a.Add("a"))),
_first);
changedProbe.ExpectNoMsg(TimeSpan.FromSeconds(1));
EnterBarrierAfterTestStep();
}
public void Cluster_CRDT_should_support_prefer_oldest_members()
{
// disable gossip and delta replication to only verify the write and read operations
var oldestReplicator = Sys.ActorOf(
Replicator.Props(
ReplicatorSettings.Create(Sys).WithPreferOldest(true).WithGossipInterval(TimeSpan.FromMinutes(1))),//.withDeltaCrdtEnabled(false)),
"oldestReplicator");
Within(TimeSpan.FromSeconds(5), () =>
{
var countProbe = CreateTestProbe();
AwaitAssert(() =>
{
oldestReplicator.Tell(GetReplicaCount.Instance, countProbe.Ref);
countProbe.ExpectMsg(new ReplicaCount(3));
});
});
EnterBarrier("oldest-replicator-started");
var probe = CreateTestProbe();
RunOn(() =>
{
oldestReplicator.Tell(
Dsl.Update(KeyK, new LWWRegister<string>(Cluster.SelfUniqueAddress, "0"), _writeTwo, a => a.WithValue(Cluster.SelfUniqueAddress, "1")),
probe.Ref);
probe.ExpectMsg(new UpdateSuccess(KeyK, null));
}, _second);
EnterBarrier("updated-1");
RunOn(() =>
{
// replicated to oldest
oldestReplicator.Tell(new Get(KeyK, ReadLocal.Instance), probe.Ref);
var msg = probe.ExpectMsg<GetSuccess>(m => m.Data is LWWRegister<string>);
((LWWRegister<string>)msg.Data).Value.Should().Be("1");
//probe.ExpectMsg<GetSuccess[LWWRegister[String]]>.dataValue.value should === ("1");
}, _first);
RunOn(() =>
{
// not replicated to third (not among the two oldest)
oldestReplicator.Tell(Dsl.Get(KeyK, ReadLocal.Instance), probe.Ref);
probe.ExpectMsg(new NotFound(KeyK, null));
// read from oldest
oldestReplicator.Tell(Dsl.Get(KeyK, _readTwo), probe.Ref);
var msg = probe.ExpectMsg<GetSuccess>(m => m.Data is LWWRegister<string>);
((LWWRegister<string>)msg.Data).Value.Should().Be("1");
//probe.ExpectMsg<GetSuccess[LWWRegister[String]]>.dataValue.value should === ("1");
}, _third);
EnterBarrierAfterTestStep();
}
protected override int InitialParticipantsValueFactory => Roles.Count;
private void EnterBarrierAfterTestStep()
{
_afterCounter++;
EnterBarrier("after-" + _afterCounter);
}
private void Join(RoleName from, RoleName to)
{
RunOn(() =>
{
_cluster.Join(Node(to).Address);
}, from);
EnterBarrier(from.Name + "-joined");
}
}
}