-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraft_test.cc
855 lines (715 loc) · 28.7 KB
/
raft_test.cc
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
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
/*
* The test cases in the first three parts are ported from 6.824 raft lab.
*/
#include "raft_test_utils.h"
typedef raft_group<list_state_machine, list_command> list_raft_group;
TEST_CASE(part1, leader_election, "Initial election")
{
int num_nodes = 3;
list_raft_group *group = new list_raft_group(num_nodes);
// 1. check whether there is exact one leader in the group
mssleep(300); // sleep 300 ms
group->check_exact_one_leader();
// 2. check whether every one has the same term
mssleep(2000); // sleep 2s
int term1 = group->check_same_term();
// 3. wait for a few seconds and check whether the term is not changed.
mssleep(2000); // sleep 2s
int term2 = group->check_same_term();
ASSERT(term1 == term2, "inconsistent term: " << term1 << ", " << term2);
group->check_exact_one_leader();
delete group;
}
TEST_CASE(part1, re_election, "Election after network failure")
{
int num_nodes = 5;
list_raft_group *group = new list_raft_group(num_nodes);
// 1. check whether there is exact one leader in the group
mssleep(300); // sleep 300ms
int leader1 = group->check_exact_one_leader();
// 2. kill the leader
group->disable_node(leader1);
mssleep(1000); // sleep 1s
int leader2 = group->check_exact_one_leader();
ASSERT(leader1 != leader2, "node " << leader2 << "is killed, which should not be the new leader!");
// 3. kill the second leader
group->disable_node(leader2);
mssleep(1000); // sleep 1s
int leader3 = group->check_exact_one_leader();
ASSERT(leader3 != leader1 && leader3 != leader2, "node " << leader3 << "is killed, which should not be the new leader!");
// 4. kill the third leader
group->disable_node(leader3);
mssleep(1000); // sleep 1s
// 5. now, there are only 2 nodes left with no leader.
group->check_no_leader();
// 6. resume a node
group->enable_node(leader1);
mssleep(1000);
group->check_exact_one_leader();
delete group;
}
TEST_CASE(part2, basic_agree, "Basic Agreement")
{
int num_nodes = 3;
list_raft_group *group = new list_raft_group(3);
mssleep(300);
int iters = 3;
for (int i = 1; i < 1 + iters; i++) {
int num_commited = group->num_committed(i);
ASSERT(num_commited == 0, "The log " << i << " should not be committed!");
int log_idx = group->append_new_command(i * 100, num_nodes);
ASSERT(log_idx == i, "got index " << log_idx << ", but expect " << i);
}
delete group;
}
TEST_CASE(part2, fail_agree, "Fail Agreement")
{
int num_nodes = 3;
list_raft_group* group = new list_raft_group(3);
group->append_new_command(101, num_nodes);
int leader = group->check_exact_one_leader();
group->disable_node(leader);
group->append_new_command(102, num_nodes - 1);
group->append_new_command(103, num_nodes - 1);
mssleep(1000);
group->append_new_command(104, num_nodes - 1);
group->append_new_command(105, num_nodes - 1);
group->enable_node(leader);
group->append_new_command(106, num_nodes);
mssleep(1000);
group->append_new_command(107, num_nodes);
delete group;
}
TEST_CASE(part2, fail_no_agree, "Fail No Agreement")
{
int num_nodes = 5;
list_raft_group *group = new list_raft_group(num_nodes);
mssleep(300);
group->append_new_command(10, num_nodes);
// 3 of 5 followers disconnect
int leader = group->check_exact_one_leader();
group->disable_node((leader+1)%num_nodes);
group->disable_node((leader+2)%num_nodes);
group->disable_node((leader+3)%num_nodes);
int temp_term, temp_index;
int is_leader = group->nodes[leader]->new_command(list_command(20), temp_term, temp_index);
ASSERT(is_leader, "node " << leader << " is leader, but it rejects the command.");
ASSERT(temp_index == 2, "expected index 2, got " << temp_index);
mssleep(2000);
int num_committed = group->num_committed(temp_index);
ASSERT(num_committed == 0, "There is no majority, but index " << temp_index << " is committed");
// repair
group->enable_node((leader+1)%num_nodes);
group->enable_node((leader+2)%num_nodes);
group->enable_node((leader+3)%num_nodes);
int leader2 = group->check_exact_one_leader();
is_leader = group->nodes[leader2]->new_command(list_command(30), temp_term, temp_index);
ASSERT(is_leader, "leader2 reject the new command");
ASSERT(temp_index == 2 || temp_index == 3, "unexpected index " << temp_index);
group->append_new_command(1000, num_nodes);
delete group;
}
std::ostream& operator<< (std::ostream& out, const std::vector<int>& vec) {
out << "[";
for (size_t i = 0; i < vec.size(); i++)
out << vec[i] << (i == vec.size() - 1? ']' : ',');
return out;
}
TEST_CASE(part2, concurrent_start, "Concurrent starts")
{
int num_nodes = 3;
list_raft_group* group = new list_raft_group(num_nodes);
bool success;
for (int tries = 0; tries < 5; tries++) {
if (tries > 0) {
mssleep(3000); // give solution some time to settle
}
int leader = group->check_exact_one_leader();
int term, index;
if (!group->nodes[leader]->new_command(list_command(1), term, index)) {
continue; // leader moved on really quickly
}
int iters = 5;
std::mutex mtx; // to protect the vector
std::vector<int> indices;
std::vector<std::thread*> threads;
bool failed = false;
std::vector<int> values;
for (int i = 0; i < iters; i++) {
std::thread *worker = new std::thread([=](int it, std::mutex *mtx, std::vector<int> *indices) {
int temp_term, temp_index;
bool is_leader = group->nodes[leader]->new_command(list_command(100 + it), temp_term, temp_index);
if (!is_leader) return;
if (term != temp_term) return;
mtx->lock();
indices->push_back(temp_index);
mtx->unlock();
}, i, &mtx, &indices);
threads.push_back(worker);
}
for (int i = 0; i < iters; i++) {
threads[i]->join();
delete threads[i];
}
for (int i = 0; i < num_nodes; i++) {
int temp_term;
group->nodes[i]->is_leader(temp_term);
if (temp_term != term) goto loop_end; // term changed -- can't expect low RPC counts
}
for (auto index : indices) {
int res = group->wait_commit(index, num_nodes, term);
if (res == -1) {
// nodes have moved on to later terms
failed = true;
break;
}
values.push_back(res);
}
if (failed) continue;
for (int i = 0; i < iters; i++) {
int ans = i + 100;
bool find = false;
for (auto value : values) {
if (value == ans) find = true;
}
ASSERT(find, "cmd " << ans << " missing in " << values);
}
success = true;
break;
loop_end: continue;
}
ASSERT(success, "term change too often");
delete group;
}
TEST_CASE(part2, rejoin, "Rejoin of partitioned leader")
{
int num_nodes = 3;
list_raft_group *group = new list_raft_group(num_nodes);
group->append_new_command(101, num_nodes);
int leader1 = group->check_exact_one_leader();
// leader network failure
group->disable_node(leader1);
// make old leader try to agree on some entries
int temp_term, temp_index;
group->nodes[leader1]->new_command(list_command(102), temp_term, temp_index);
group->nodes[leader1]->new_command(list_command(103), temp_term, temp_index);
group->nodes[leader1]->new_command(list_command(104), temp_term, temp_index);
// new leader commits, also for index=2
int leader2 = group->check_exact_one_leader();
group->append_new_command(103, 2);
// new leader network failure
group->disable_node(leader2);
// old leader connected again
group->enable_node(leader1);
group->append_new_command(104, 2);
// all together now
group->enable_node(leader2);
group->append_new_command(105, num_nodes);
delete group;
}
TEST_CASE(part2, backup, "Leader backs up quickly over incorrect follower logs")
{
int num_nodes = 5;
list_raft_group *group = new list_raft_group(num_nodes);
int value = 0;
group->append_new_command(value++, num_nodes);
// put leader and one follower in a partition
int leader1 = group->check_exact_one_leader();
group->disable_node((leader1 + 2) % num_nodes);
group->disable_node((leader1 + 3) % num_nodes);
group->disable_node((leader1 + 4) % num_nodes);
// submit lots of commands that won't commit
int temp_term, temp_index;
for (int i = 0; i < 50; i++)
group->nodes[leader1]->new_command(list_command(value++), temp_term, temp_index);
mssleep(500);
group->disable_node(leader1);
group->disable_node((leader1 + 1) % num_nodes);
// allow other partition to recover
group->enable_node((leader1 + 2) % num_nodes);
group->enable_node((leader1 + 3) % num_nodes);
group->enable_node((leader1 + 4) % num_nodes);
// lots of successful commands to new group.
for (int i = 0; i < 50; i++)
group->append_new_command(value++, 3);
// now another partitioned leader and one follower
int leader2 = group->check_exact_one_leader();
int other = (leader1 + 2) % num_nodes;
if (other == leader2) other = (leader2 + 1) % num_nodes;
group->disable_node(other);
// lots more commands that won't commit
for (int i = 0; i < 50; i++)
group->nodes[leader2]->new_command(list_command(value++), temp_term, temp_index);
mssleep(500);
// bring original leader back to life,
for (int i = 0; i < num_nodes; i++)
group->disable_node(i);
group->enable_node((leader1 + 0) % num_nodes);
group->enable_node((leader1 + 1) % num_nodes);
group->enable_node(other);
// lots of successful commands to new group.
for (int i = 0; i < 50; i++)
group->append_new_command(value++, 3);
// now everyone
for (int i = 0; i < num_nodes; i++)
group->enable_node(i);
group->append_new_command(value++, num_nodes);
delete group;
}
TEST_CASE(part2, rpc_count, "RPC counts aren't too high")
{
int num_nodes = 3;
int value = 1;
list_raft_group *group = new list_raft_group(num_nodes);
int leader = group->check_exact_one_leader();
int total1 = group->rpc_count(-1);
ASSERT(total1 <= 30 && total1 >= 1, "too many or few RPCs (" << total1 << ") to elect initial leader");
bool success = false;
int total2; bool failed;
for (int tries = 0; tries < 5; tries++) {
if (tries > 0)
mssleep(3000); // give solution some time to settle
leader = group->check_exact_one_leader();
total1 = group->rpc_count(-1);
int iters = 10;
int term, index;
int is_leader = group->nodes[leader]->new_command(list_command(1024), term, index);
if (!is_leader) continue;
for (int i = 1; i < iters + 2; i++) {
int term1, index1;
is_leader = group->nodes[leader]->new_command(list_command(value), term1, index1);
if (!is_leader) goto loop_end;
if (term1 != term) goto loop_end;
value++;
ASSERT(index+i == index1, "wrong commit index " << index1 << ", expected " << index+i);
}
for (int i = 1; i < iters + 1; i++) {
int res = group->wait_commit(index+i, num_nodes, term);
if (res == -1) goto loop_end;
ASSERT(res == i, "wrong value " << res << " committed for index " << index+i << "; expected " << i);
}
failed = false;
for (int j = 0; j < num_nodes; j++) {
int temp_term;
group->nodes[j]->is_leader(temp_term);
if (temp_term != term) {
failed = true;
}
}
if (failed) goto loop_end;
total2 = group->rpc_count(-1);
ASSERT(total2 - total1 <= (iters + 1 + 3)*3, "too many RPCs (" << total2-total1 << ") for " << iters << " entries");
success = true;
break;
loop_end: continue;
}
ASSERT(success, "term changed too often");
mssleep(1000);
int total3 = group->rpc_count(-1);
ASSERT(total3 - total2 <= 3 * 20, "too many RPCs (" << total3-total2 << ") for 1 second of idleness");
delete group;
}
TEST_CASE(part3, persist1, "Basic persistence")
{
int num_nodes = 3;
list_raft_group *group = new list_raft_group(num_nodes);
mssleep(100);
group->append_new_command(11, num_nodes);
for (int i = 0; i < num_nodes; i++) {
group->restart(i);
}
for (int i = 0; i < num_nodes; i++) {
group->disable_node(i);
group->enable_node(i);
}
group->append_new_command(12, num_nodes);
int leader1 = group->check_exact_one_leader();
group->disable_node(leader1);
group->restart(leader1);
group->enable_node(leader1);
group->append_new_command(13, num_nodes);
int leader2 = group->check_exact_one_leader();
group->disable_node(leader2);
group->append_new_command(14, num_nodes - 1);
group->restart(leader2);
group->enable_node(leader2);
group->wait_commit(4, num_nodes, -1); // wait for leader2 to join before killing i3
int i3 = (group->check_exact_one_leader() + 1) % num_nodes;
group->disable_node(i3);
group->append_new_command(15, num_nodes - 1);
group->restart(i3);
group->enable_node(i3);
group->append_new_command(16, num_nodes);
delete group;
}
TEST_CASE(part3, persist2, "More persistence")
{
int num_nodes = 5;
list_raft_group *group = new list_raft_group(num_nodes);
int index = 1;
for (int iters = 0; iters < 5; iters++) {
group->append_new_command(10 + index, num_nodes);
index++;
int leader1 = group->check_exact_one_leader();
group->disable_node((leader1 + 1) % num_nodes);
group->disable_node((leader1 + 2) % num_nodes);
group->append_new_command(10 + index, num_nodes - 2);
index++;
group->disable_node((leader1 + 0) % num_nodes);
group->disable_node((leader1 + 3) % num_nodes);
group->disable_node((leader1 + 4) % num_nodes);
group->restart((leader1 + 1) % num_nodes);
group->restart((leader1 + 2) % num_nodes);
group->enable_node((leader1 + 1) % num_nodes);
group->enable_node((leader1 + 2) % num_nodes);
mssleep(1000);
group->restart((leader1 + 3) % num_nodes);
group->enable_node((leader1 + 3) % num_nodes);
group->append_new_command(10 + index, num_nodes - 2);
index++;
group->enable_node((leader1 + 0) % num_nodes);
group->enable_node((leader1 + 4) % num_nodes);
}
group->append_new_command(1000, num_nodes);
group->wait_commit(index, num_nodes, -1);
delete group;
}
TEST_CASE(part3, persist3, "Partitioned leader and one follower crash, leader restarts")
{
int num_nodes = 3;
list_raft_group *group = new list_raft_group(num_nodes);
group->append_new_command(101, num_nodes);
int leader = group->check_exact_one_leader();
group->disable_node((leader + 2) % num_nodes);
group->append_new_command(102, num_nodes - 1);
group->disable_node((leader + 0) % num_nodes);
group->disable_node((leader + 1) % num_nodes);
group->enable_node((leader + 2) % num_nodes);
group->restart((leader + 0) % num_nodes);
group->enable_node((leader + 0) % num_nodes);
group->append_new_command(103, 2);
group->restart((leader + 1) % num_nodes);
group->append_new_command(104, num_nodes);
group->wait_commit(4, num_nodes, -1);
delete group;
}
void figure_8_test(list_raft_group* group, int num_tries = 1000)
{
int num_nodes = 5;
group->append_new_command(2048, 1);
int nup = num_nodes;
for (int iters = 0; iters < num_tries; iters++) {
int leader = -1;
for (int i = 0; i < num_nodes; i++) {
int term, index;
if (group->nodes[i]->new_command(list_command(iters), term, index)) {
leader = i;
}
}
if (rand() % 1000 < 100) {
mssleep(rand() % 500);
} else {
mssleep(rand() % 13);
}
if (leader != -1) {
group->disable_node(leader);
nup --;
}
if (nup < 3) {
int s = rand() % num_nodes;
if (!group->servers[s]->reachable()) {
group->restart(s);
nup ++;
}
}
}
for (int i = 0; i < num_nodes; i++) {
group->restart(i);
}
group->append_new_command(1024, num_nodes);
}
TEST_CASE(part3, figure8, "Raft paper figure 8")
{
int num_nodes = 5;
list_raft_group *group = new list_raft_group(num_nodes);
figure_8_test(group);
delete group;
}
TEST_CASE(part3, unreliable_agree, "Agreement under unreliable network")
{
int num_nodes = 5;
list_raft_group *group = new list_raft_group(num_nodes);
group->set_reliable(false);
std::vector<std::thread*> workers;
for (int iters = 1; iters < 50; iters++) {
for (int j = 0; j < 4; j++) {
std::thread* t = new std::thread([=](){
group->append_new_command((100 * iters) + j, 1);
});
workers.push_back(t);
}
group->append_new_command(iters, 1);
}
for (auto worker : workers) {
worker->join();
delete worker;
}
group->append_new_command(4096, num_nodes);
delete group;
}
TEST_CASE(part3, unreliable_figure_8, "Raft paper Figure 8 under unreliable network")
{
int num_nodes = 5;
list_raft_group *group = new list_raft_group(num_nodes);
group->set_reliable(false);
figure_8_test(group, 100);
delete group;
}
TEST_CASE(part4, basic_snapshot, "Basic snapshot")
{
int num_nodes = 3;
list_raft_group *group = new list_raft_group(num_nodes);
int leader = group->check_exact_one_leader();
int killed_node = (leader + 1) % num_nodes;
group->disable_node(killed_node);
for (int i = 1 ; i < 100; i++)
group->append_new_command(100 + i, num_nodes - 1);
leader = group->check_exact_one_leader();
int other_node = (leader + 1) % num_nodes;
if (other_node == killed_node) other_node = (leader + 2) % num_nodes;
ASSERT(group->nodes[leader]->save_snapshot(), "leader cannot save snapshot");
ASSERT(group->nodes[other_node]->save_snapshot(), "follower cannot save snapshot");
mssleep(2000);
group->enable_node(killed_node);
leader = group->check_exact_one_leader();
group->append_new_command(1024, num_nodes);
ASSERT(group->states[killed_node]->num_append_logs < 90, "the snapshot does not work");
delete group;
}
TEST_CASE(part4, restore_snapshot, "Restore snapshot after failure")
{
int num_nodes = 3;
list_raft_group *group = new list_raft_group(num_nodes);
int leader = group->check_exact_one_leader();
for (int i = 1 ; i < 100; i++)
group->append_new_command(100 + i, num_nodes - 1);
leader = group->check_exact_one_leader();
ASSERT(group->nodes[leader]->save_snapshot(), "leader cannot save snapshot");
mssleep(2000);
group->restart(leader);
group->append_new_command(1024, num_nodes);
ASSERT(group->states[leader]->num_append_logs < 90, "the snapshot does not work");
delete group;
}
TEST_CASE(part4, override_snapshot, "Overrive snapshot")
{
int num_nodes = 3;
list_raft_group *group = new list_raft_group(num_nodes);
int leader = group->check_exact_one_leader();
for (int i = 1 ; i < 30; i++)
group->append_new_command(100 + i, num_nodes - 1);
leader = group->check_exact_one_leader();
ASSERT(group->nodes[leader]->save_snapshot(), "leader cannot save snapshot");
for (int i = 30 ; i < 60; i++)
group->append_new_command(100 + i, num_nodes - 1);
leader = group->check_exact_one_leader();
ASSERT(group->nodes[leader]->save_snapshot(), "leader cannot save snapshot");
mssleep(2000);
group->restart(leader);
group->append_new_command(1024, num_nodes);
ASSERT(group->states[leader]->num_append_logs < 50, "the snapshot does not work");
delete group;
}
typedef raft_group<kv_state_machine, kv_command> kv_raft_group;
void push_kv_commands(kv_raft_group* group, int num, int begin)
{
int leader = group->check_exact_one_leader();
std::vector<std::shared_ptr<kv_command::result>> puts;
for (int i = begin; i < begin + num; i++) {
ASSERT(leader == group->check_exact_one_leader(), "Leader should not change");
kv_command cmd(kv_command::CMD_PUT, "k" + std::to_string(i), "v" + std::to_string(i * 10));
int term, index;
puts.push_back(cmd.res);
ASSERT(group->nodes[leader]->new_command(cmd, term, index), "Leader should not change");
}
for (int i = begin; i < begin + num; i++) {
std::unique_lock<std::mutex> lock(puts[i]->mtx);
if (!puts[i]->done) {
ASSERT(puts[i]->cv.wait_until(lock, std::chrono::system_clock::now() + std::chrono::milliseconds(2500)) == std::cv_status::no_timeout,
"Put command timeout");
}
ASSERT(puts[i]->succ, "Put command does not succ?");
std::string ans_key = "k" + std::to_string(i);
std::string ans_val = "v" + std::to_string(i * 10);
ASSERT(puts[i]->key == ans_key, "Put command key doesn't match, " << ans_key << " != " << puts[i]->key);
ASSERT(puts[i]->value == ans_val, "Put command value doesn't match, " << ans_val << " != " << puts[i]->value);
}
}
void put_kv_pair(kv_raft_group* group, int k, bool succ)
{
int leader = group->check_exact_one_leader();
int term, index;
std::string ans_key = "k" + std::to_string(k);
std::string ans_val = "v" + std::to_string(k*10);
kv_command cmd(kv_command::CMD_PUT, ans_key, ans_val);
ASSERT(group->nodes[leader]->new_command(cmd, term, index), "Leader should not change");
{
std::unique_lock<std::mutex> lock(cmd.res->mtx);
if (!cmd.res->done) {
ASSERT(cmd.res->cv.wait_until(lock, std::chrono::system_clock::now() + std::chrono::milliseconds(2500)) == std::cv_status::no_timeout,
"Put command timeout");
}
ASSERT(cmd.res->succ == succ, "Put command fails");
if (succ) {
ASSERT(cmd.res->key == ans_key, "Wrong key");
ASSERT(cmd.res->value == ans_val, "Wrong value " << cmd.res->value << " vs " << ans_val);
}
}
}
void del_kv_pair(kv_raft_group* group, int k, bool succ)
{
int leader = group->check_exact_one_leader();
int term, index;
std::string ans_key = "k" + std::to_string(k);
std::string ans_val = "v" + std::to_string(k * 10);
kv_command cmd(kv_command::CMD_DEL, ans_key, "");
ASSERT(group->nodes[leader]->new_command(cmd, term, index), "Leader should not change");
{
std::unique_lock<std::mutex> lock(cmd.res->mtx);
if (!cmd.res->done) {
ASSERT(cmd.res->cv.wait_until(lock, std::chrono::system_clock::now() + std::chrono::milliseconds(2500)) == std::cv_status::no_timeout,
"Del command timeout");
}
ASSERT(cmd.res->succ == succ, "Del command fails");
if (succ) {
ASSERT(cmd.res->key == ans_key, "Wrong key");
ASSERT(cmd.res->value == ans_val, "Wrong value");
}
}
}
void check_kv_pair(kv_raft_group* group, int k, bool succ)
{
int total_tries = 5;
int tries = 0;
while (tries < total_tries) {
tries ++;
int leader = group->check_exact_one_leader();
int term, index;
std::string ans_key = "k" + std::to_string(k);
std::string ans_val = "v" + std::to_string(k * 10);
kv_command cmd(kv_command::CMD_GET, ans_key, "");
if (!group->nodes[leader]->new_command(cmd, term, index)) goto next_try;
{
std::unique_lock<std::mutex> lock(cmd.res->mtx);
if (!cmd.res->done) {
if(cmd.res->cv.wait_until(lock, std::chrono::system_clock::now() + std::chrono::milliseconds(2500)) == std::cv_status::timeout)
goto next_try;
}
ASSERT(cmd.res->succ == succ, "Get command fails " << cmd.res->succ << " vs " << succ);
if (succ) {
ASSERT(cmd.res->key == ans_key, "Wrong key");
ASSERT(cmd.res->value == ans_val, "Wrong value");
}
}
return;
next_try:
mssleep(200);
}
ASSERT(false, "Cannot read key-value pair");
}
TEST_CASE(part5, basic_kv, "Basic key-value store")
{
int num_nodes = 3;
int term, index;
kv_raft_group *group = new kv_raft_group(num_nodes);
int leader = group->check_exact_one_leader();
push_kv_commands(group, 10, 0);
std::vector<std::shared_ptr<kv_command::result>> reads;
for (int i = 0; i < 10; i++) {
ASSERT(leader == group->check_exact_one_leader(), "Leader should not change");
kv_command cmd(kv_command::CMD_GET, "k" + std::to_string(i), "");
reads.push_back(cmd.res);
ASSERT(group->nodes[leader]->new_command(cmd, term, index), "Leader should not change");
}
for (int i = 0; i < 10; i++) {
std::unique_lock<std::mutex> lock(reads[i]->mtx);
if (!reads[i]->done) {
ASSERT(reads[i]->cv.wait_until(lock, std::chrono::system_clock::now() + std::chrono::milliseconds(2500)) == std::cv_status::no_timeout,
"Get command timeout");
}
ASSERT(reads[i]->succ, "Get command does not succ?");
std::string ans_key = "k" + std::to_string(i);
std::string ans_val = "v" + std::to_string(i * 10);
ASSERT(reads[i]->key == ans_key, "Get command key doesn't match, " << ans_key << " != " << reads[i]->key);
ASSERT(reads[i]->value == ans_val, "Get command value doesn't match, " << ans_val << " != " << reads[i]->value);
}
std::string read_key = "k" + std::to_string(5);
kv_command del_cmd(kv_command::CMD_DEL, read_key, "");
kv_command read_cmd(kv_command::CMD_GET, read_key, "");
std::shared_ptr<kv_command::result> del = del_cmd.res;
std::shared_ptr<kv_command::result> rd = read_cmd.res;
ASSERT(group->nodes[leader]->new_command(del_cmd, term, index), "Leader should not change");
ASSERT(group->nodes[leader]->new_command(read_cmd, term, index), "Leader should not change");
{
std::unique_lock<std::mutex> lock(del->mtx);
if (!del->done) {
ASSERT(del->cv.wait_until(lock, std::chrono::system_clock::now() + std::chrono::milliseconds(2500)) == std::cv_status::no_timeout,
"Del command timeout");
}
ASSERT(del->key == read_key, "Del command key doesn't match, " << read_key << " != " << del->key);
ASSERT(del->succ, "Del command does not succ?");
}
{
std::unique_lock<std::mutex> lock(rd->mtx);
if (!rd->done) {
ASSERT(rd->cv.wait_until(lock, std::chrono::system_clock::now() + std::chrono::milliseconds(2500)) == std::cv_status::no_timeout,
"Get command timeout");
}
ASSERT(rd->key == read_key, "Get command key doesn't match, " << read_key << " != " << rd->key);
ASSERT(rd->succ == false, "Get succ on deleted key?");
}
delete group;
}
TEST_CASE(part5, persist_kv, "Persist key-value store")
{
int num_nodes = 3;
kv_raft_group *group = new kv_raft_group(num_nodes);
for (int i = 0; i < 10; i++)
put_kv_pair(group, i, true);
del_kv_pair(group, 0, true);
for (int i = 0; i < num_nodes; i++) {
group->disable_node(i);
group->restart(i);
}
mssleep(2000); // wait for election
for (int i = 1; i < 10; i++)
check_kv_pair(group, i, true);
check_kv_pair(group, 0, false);
delete group;
}
TEST_CASE(part5, snapshot_kv, "Persist key-value snapshot")
{
int num_nodes = 3;
kv_raft_group *group = new kv_raft_group(num_nodes);
int leader = group->check_exact_one_leader();
int killed_node = (leader + 1) % num_nodes;
group->disable_node(killed_node);
for (int i = 0; i < 50; i++)
put_kv_pair(group, i, true);
leader = group->check_exact_one_leader();
int other_node = (leader + 1) % num_nodes;
if (other_node == killed_node) other_node = (leader + 2) % num_nodes;
ASSERT(group->nodes[leader]->save_snapshot(), "leader cannot save snapshot");
ASSERT(group->nodes[other_node]->save_snapshot(), "follower cannot save snapshot");
mssleep(2000);
group->enable_node(killed_node);
mssleep(2000); // wait for the election
leader = group->check_exact_one_leader();
for (int i = 0; i < 50; i++)
check_kv_pair(group, i, true);
delete group;
}
int main(int argc, char** argv) {
unit_test_suite::instance()->run(argc, argv);
return 0;
}