/
ep_testsuite_tap.cc
1511 lines (1343 loc) · 56.9 KB
/
ep_testsuite_tap.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
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- MODE: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* Copyright 2016 Couchbase, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Testsuite for TAP functionality in ep-engine.
*/
#include "config.h"
#include "ep_test_apis.h"
#include "ep_testsuite_common.h"
#include <platform/cb_malloc.h>
// Helper functions ///////////////////////////////////////////////////////////
static enum test_result verify_item(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
item *i,
const char* key, size_t klen,
const char* val, size_t vlen)
{
item_info info;
check(h1->get_item_info(h, NULL, i, &info), "get item info failed");
// We can pass in a NULL key to avoid the key check (for tap streams)
if (key) {
check(klen == info.nkey, "Incorrect key length");
check(memcmp(info.key, key, klen) == 0, "Incorrect key value");
}
check(vlen == info.value[0].iov_len, "Incorrect value length");
check(memcmp(info.value[0].iov_base, val, vlen) == 0,
"Data mismatch");
return SUCCESS;
}
// Testcases //////////////////////////////////////////////////////////////////
static enum test_result test_set_tap_param(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1)
{
set_param(h, h1, protocol_binary_engine_param_tap, "tap_keepalive", "600");
checkeq(600, get_int_stat(h, h1, "ep_tap_keepalive"),
"Incorrect tap_keepalive value.");
set_param(h, h1, protocol_binary_engine_param_tap, "tap_keepalive", "5000");
checkeq(PROTOCOL_BINARY_RESPONSE_EINVAL, last_status.load(),
"Expected an invalid value error due to exceeding a max value allowed");
return SUCCESS;
}
static enum test_result test_tap_noop_config_default(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1)
{
h1->reset_stats(h, NULL);
checkeq(200, get_int_stat(h, h1, "ep_tap_noop_interval", "tap"),
"Expected tap_noop_interval == 200");
return SUCCESS;
}
static enum test_result test_tap_noop_config(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1)
{
h1->reset_stats(h, NULL);
checkeq(10, get_int_stat(h, h1, "ep_tap_noop_interval", "tap"),
"Expected tap_noop_interval == 10");
return SUCCESS;
}
static enum test_result test_tap_notify(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1)
{
int ii = 0;
char buffer[1024];
ENGINE_ERROR_CODE r;
const void *cookie = testHarness.create_cookie();
memset(&buffer, 0, sizeof(buffer));
do {
std::stringstream ss;
ss << "Key-"<< ++ii;
std::string key = ss.str();
char eng_specific[9];
memset(eng_specific, 0, sizeof(eng_specific));
r = h1->tap_notify(h, cookie, eng_specific, sizeof(eng_specific), 1, 0,
TAP_MUTATION, 0, key.c_str(), key.length(), 0, 0, 0x1,
0, buffer, 1024, 0);
} while (r == ENGINE_SUCCESS);
checkeq(ENGINE_TMPFAIL, r, "non-acking streams should etmpfail");
testHarness.destroy_cookie(cookie);
return SUCCESS;
}
static enum test_result test_tap_rcvr_mutate(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
char eng_specific[9];
memset(eng_specific, 0, sizeof(eng_specific));
std::vector<char> data(8192, 'x');
for (size_t i = 0; i < 8192; ++i) {
checkeq(ENGINE_SUCCESS,
h1->tap_notify(h, NULL, eng_specific, sizeof(eng_specific),
1, 0, TAP_MUTATION, 1, "key", 3, 828, 0, 1,
PROTOCOL_BINARY_RAW_BYTES,
data.data(), i, 0),
"Failed tap notify.");
check_key_value(h, h1, "key", data.data(), i);
}
return SUCCESS;
}
static enum test_result test_tap_rcvr_checkpoint(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
char eng_specific[64];
memset(eng_specific, 0, sizeof(eng_specific));
check(set_vbucket_state(h, h1, 1, vbucket_state_replica), "Failed to set vbucket state.");
for (uint64_t checkpoint_id = 0; checkpoint_id < 10; checkpoint_id++) {
checkeq(ENGINE_SUCCESS,
h1->tap_notify(h, NULL, eng_specific, sizeof(eng_specific),
1, 0, TAP_CHECKPOINT_START, 1, "", 0, 828, 0, 1,
PROTOCOL_BINARY_RAW_BYTES,
&checkpoint_id, sizeof(checkpoint_id), 1),
"Failed tap notify.");
checkeq(ENGINE_SUCCESS,
h1->tap_notify(h, NULL, eng_specific, sizeof(eng_specific),
1, 0, TAP_CHECKPOINT_END, 1, "", 0, 828, 0, 1,
PROTOCOL_BINARY_RAW_BYTES,
&checkpoint_id, sizeof(checkpoint_id), 1),
"Failed tap notify.");
}
return SUCCESS;
}
static enum test_result test_tap_rcvr_set_vbstate(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
char eng_specific[8];
vbucket_state_t vb_state = static_cast<vbucket_state_t>(htonl(vbucket_state_active));
memcpy(eng_specific, &vb_state, sizeof(vb_state));
check(set_vbucket_state(h, h1, 1, vbucket_state_replica), "Failed to set vbucket state.");
// Get the vbucket UUID before vbucket takeover.
uint64_t vb_uuid = get_ull_stat(h, h1, "vb_1:0:id", "failovers");
checkeq(ENGINE_SUCCESS,
h1->tap_notify(h, NULL, eng_specific, sizeof(vb_state),
1, 0, TAP_VBUCKET_SET, 1, "", 0, 828, 0, 1,
PROTOCOL_BINARY_RAW_BYTES,
"", 0, 1),
"Failed tap notify.");
// Get the vbucket UUID after vbucket takeover.
check(get_ull_stat(h, h1, "vb_1:0:id", "failovers") != vb_uuid,
"A new vbucket uuid should be created after TAP-based vbucket takeover.");
return SUCCESS;
}
static enum test_result test_tap_rcvr_mutate_dead(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
char eng_specific[9];
memset(eng_specific, 0, sizeof(eng_specific));
checkeq(ENGINE_NOT_MY_VBUCKET,
h1->tap_notify(h, NULL, eng_specific, sizeof(eng_specific),
1, 0, TAP_MUTATION, 1, "key", 3, 828, 0, 1,
PROTOCOL_BINARY_RAW_BYTES,
"data", 4, 1),
"Expected not my vbucket.");
return SUCCESS;
}
static enum test_result test_tap_rcvr_mutate_pending(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
check(set_vbucket_state(h, h1, 1, vbucket_state_pending), "Failed to set vbucket state.");
char eng_specific[9];
memset(eng_specific, 0, sizeof(eng_specific));
checkeq(ENGINE_SUCCESS,
h1->tap_notify(h, NULL, eng_specific, sizeof(eng_specific),
1, 0, TAP_MUTATION, 1, "key", 3, 828, 0, 1,
PROTOCOL_BINARY_RAW_BYTES,
"data", 4, 1),
"Expected expected success.");
return SUCCESS;
}
static enum test_result test_tap_rcvr_mutate_replica(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
check(set_vbucket_state(h, h1, 1, vbucket_state_replica), "Failed to set vbucket state.");
char eng_specific[9];
memset(eng_specific, 0, sizeof(eng_specific));
checkeq(ENGINE_SUCCESS,
h1->tap_notify(h, NULL, eng_specific, sizeof(eng_specific),
1, 0, TAP_MUTATION, 1, "key", 3, 828, 0, 1,
PROTOCOL_BINARY_RAW_BYTES,
"data", 4, 1),
"Expected expected success.");
return SUCCESS;
}
static enum test_result test_tap_rcvr_mutate_replica_locked(ENGINE_HANDLE *h,
ENGINE_HANDLE_V1 *h1) {
check(set_vbucket_state(h, h1, 0, vbucket_state_active),
"Failed to set vbucket state.");
item *i = NULL;
checkeq(ENGINE_SUCCESS,
store(h, h1, NULL, OPERATION_SET,"key", "value", &i),
"Failed to fail to store an item.");
h1->release(h, NULL, i);
item* locked;
checkeq(ENGINE_SUCCESS,
getl(h, h1, nullptr, &locked, "key", 0, 10),
"expected the key to be locked...");
h1->release(h, nullptr, locked);
check(set_vbucket_state(h, h1, 0, vbucket_state_replica),
"Failed to set vbucket state.");
char eng_specific[9];
memset(eng_specific, 0, sizeof(eng_specific));
checkeq(ENGINE_SUCCESS,
h1->tap_notify(h, NULL, eng_specific, sizeof(eng_specific),
1, 0, TAP_MUTATION, 1, "key", 3, 828, 0, 1,
PROTOCOL_BINARY_RAW_BYTES,
"data", 4, 0),
"Expected expected success.");
return SUCCESS;
}
static enum test_result test_tap_rcvr_delete(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
char* eng_specific[8];
memset(eng_specific, 0, sizeof(eng_specific));
checkeq(ENGINE_SUCCESS,
h1->tap_notify(h, NULL, eng_specific, sizeof(eng_specific),
1, 0, TAP_DELETION, 0, "key", 3, 0, 0, 1,
PROTOCOL_BINARY_RAW_BYTES,
0, 0, 0),
"Failed tap notify.");
return SUCCESS;
}
static enum test_result test_tap_rcvr_delete_dead(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
char* eng_specific[8];
memset(eng_specific, 0, sizeof(eng_specific));
checkeq(ENGINE_NOT_MY_VBUCKET,
h1->tap_notify(h, NULL, eng_specific, sizeof(eng_specific),
1, 0, TAP_DELETION, 1, "key", 3, 0, 0, 1,
PROTOCOL_BINARY_RAW_BYTES,
NULL, 0, 1),
"Expected not my vbucket.");
return SUCCESS;
}
static enum test_result test_tap_rcvr_delete_pending(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
check(set_vbucket_state(h, h1, 1, vbucket_state_pending), "Failed to set vbucket state.");
char* eng_specific[8];
memset(eng_specific, 0, sizeof(eng_specific));
checkeq(ENGINE_SUCCESS,
h1->tap_notify(h, NULL, eng_specific, sizeof(eng_specific),
1, 0, TAP_DELETION, 1, "key", 3, 0, 0, 1,
PROTOCOL_BINARY_RAW_BYTES,
NULL, 0, 1),
"Expected expected success.");
return SUCCESS;
}
static enum test_result test_tap_rcvr_delete_replica(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
check(set_vbucket_state(h, h1, 1, vbucket_state_replica), "Failed to set vbucket state.");
char* eng_specific[8];
memset(eng_specific, 0, sizeof(eng_specific));
checkeq(ENGINE_SUCCESS,
h1->tap_notify(h, NULL, eng_specific, sizeof(eng_specific),
1, 0, TAP_DELETION, 1, "key", 3, 0, 0, 1,
PROTOCOL_BINARY_RAW_BYTES,
NULL, 0, 1),
"Expected expected success.");
return SUCCESS;
}
static enum test_result test_tap_rcvr_delete_replica_locked(ENGINE_HANDLE *h,
ENGINE_HANDLE_V1 *h1) {
item *i = NULL;
checkeq(ENGINE_SUCCESS,
store(h, h1, NULL, OPERATION_SET,"key", "value", &i),
"Failed to fail to store an item.");
h1->release(h, NULL, i);
item* locked = nullptr;
checkeq(ENGINE_SUCCESS,
getl(h, h1, nullptr, &locked, "key", 0, 10), // 10 secs of lock expiration
"expected the key to be locked...");
h1->release(h, nullptr, locked);
check(set_vbucket_state(h, h1, 0, vbucket_state_replica),
"Failed to set vbucket state.");
char* eng_specific[8];
memset(eng_specific, 0, sizeof(eng_specific));
checkeq(ENGINE_SUCCESS,
h1->tap_notify(h, NULL, eng_specific, sizeof(eng_specific),
1, 0, TAP_DELETION, 1, "key", 3, 0, 0, 1,
PROTOCOL_BINARY_RAW_BYTES,
NULL, 0, 0),
"Expected expected success.");
return SUCCESS;
}
static enum test_result test_tap_stream(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
const int num_keys = 30;
bool keys[num_keys];
int initialPersisted = get_int_stat(h, h1, "ep_total_persisted");
for (int ii = 0; ii < num_keys; ++ii) {
keys[ii] = false;
std::stringstream ss;
ss << ii;
checkeq(ENGINE_SUCCESS,
store(h, h1, NULL, OPERATION_SET, ss.str().c_str(),
"value", NULL, 0, 0),
"Failed to store an item.");
}
useconds_t sleepTime = 128;
while (get_int_stat(h, h1, "ep_total_persisted")
< initialPersisted + num_keys) {
decayingSleep(&sleepTime);
}
for (int ii = 0; ii < num_keys; ++ii) {
std::stringstream ss;
ss << ii;
evict_key(h, h1, ss.str().c_str(), 0, "Ejected.");
}
const void *cookie = testHarness.create_cookie();
testHarness.lock_cookie(cookie);
std::string name = "tap_client_thread";
TAP_ITERATOR iter = h1->get_tap_iterator(h, cookie, name.c_str(),
name.length(),
TAP_CONNECT_FLAG_DUMP, NULL,
0);
check(iter != NULL, "Failed to create a tap iterator");
item *it;
void *engine_specific;
uint16_t nengine_specific;
uint8_t ttl;
uint16_t flags;
uint32_t seqno;
uint16_t vbucket;
tap_event_t event;
std::string key;
uint16_t unlikely_vbucket_identifier = 17293;
do {
vbucket = unlikely_vbucket_identifier;
event = iter(h, cookie, &it, &engine_specific,
&nengine_specific, &ttl, &flags,
&seqno, &vbucket);
switch (event) {
case TAP_PAUSE:
testHarness.waitfor_cookie(cookie);
break;
case TAP_OPAQUE:
case TAP_NOOP:
break;
case TAP_MUTATION:
testHarness.unlock_cookie(cookie);
check(get_key(h, h1, it, key), "Failed to read out the key");
keys[atoi(key.c_str())] = true;
cb_assert(vbucket != unlikely_vbucket_identifier);
check(verify_item(h, h1, it, NULL, 0, "value", 5) == SUCCESS,
"Unexpected item arrived on tap stream");
h1->release(h, cookie, it);
testHarness.lock_cookie(cookie);
break;
case TAP_DISCONNECT:
break;
default:
std::cerr << "Unexpected event: " << event << std::endl;
return FAIL;
}
} while (event != TAP_DISCONNECT);
for (int ii = 0; ii < num_keys; ++ii) {
check(keys[ii], "Failed to receive key");
}
testHarness.unlock_cookie(cookie);
testHarness.destroy_cookie(cookie);
check(get_int_stat(h, h1, "ep_tap_total_fetched", "tap") != 0,
"http://bugs.northscale.com/show_bug.cgi?id=1695");
h1->reset_stats(h, NULL);
check(get_int_stat(h, h1, "ep_tap_total_fetched", "tap") == 0,
"Expected reset stats to clear ep_tap_total_fetched");
return SUCCESS;
}
static enum test_result test_tap_agg_stats(ENGINE_HANDLE *h,
ENGINE_HANDLE_V1 *h1) {
std::vector<const void*> cookies;
cookies.push_back(createTapConn(h, h1, "replica_a"));
cookies.push_back(createTapConn(h, h1, "replica_b"));
cookies.push_back(createTapConn(h, h1, "replica_c"));
cookies.push_back(createTapConn(h, h1, "rebalance_a"));
cookies.push_back(createTapConn(h, h1, "userconnn"));
checkeq(3, get_int_stat(h, h1, "replica:count", "tapagg _"),
"Incorrect replica count on tap agg");
checkeq(1, get_int_stat(h, h1, "rebalance:count", "tapagg _"),
"Incorrect rebalance count on tap agg");
checkeq(5, get_int_stat(h, h1, "_total:count", "tapagg _"),
"Incorrect total count on tap agg");
for (auto& cookie : cookies) {
testHarness.unlock_cookie(cookie);
testHarness.destroy_cookie(cookie);
}
return SUCCESS;
}
static enum test_result test_tap_sends_deleted(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
/* MB-20115: This test times out with forestdb as backend as
* deleted items don't get reflected in the final item
* count because forestdb doesn't provide accurate
* item count until a wal flush happens.
*/
std::string backend = get_str_stat(h, h1, "ep_backend");
if (backend == "forestdb") {
return SKIPPED;
}
const int num_keys = 5;
for (int ii = 0; ii < num_keys; ++ii) {
std::stringstream ss;
ss << "key" << ii;
checkeq(ENGINE_SUCCESS,
store(h, h1, NULL, OPERATION_SET, ss.str().c_str(),
"value", NULL, 0, 0),
"Failed to store an item.");
}
wait_for_flusher_to_settle(h, h1);
for (int ii = 0; ii < num_keys - 2; ++ii) {
std::stringstream ss;
ss << "key" << ii;
checkeq(ENGINE_SUCCESS, del(h, h1, ss.str().c_str(), 0, 0), "Delete failed");
}
wait_for_flusher_to_settle(h, h1);
wait_for_stat_to_be(h, h1, "curr_items", 2);
const void *cookie = testHarness.create_cookie();
testHarness.lock_cookie(cookie);
std::string name = "tap_client_thread";
TAP_ITERATOR iter = h1->get_tap_iterator(h, cookie, name.c_str(),
name.length(),
TAP_CONNECT_FLAG_DUMP, NULL,
0);
check(iter != NULL, "Failed to create a tap iterator");
int num_mutations = 0;
int num_deletes = 0;
item *it;
void *engine_specific;
uint16_t nengine_specific;
uint8_t ttl;
uint16_t flags;
uint32_t seqno;
uint16_t vbucket;
tap_event_t event;
do {
event = iter(h, cookie, &it, &engine_specific,
&nengine_specific, &ttl, &flags,
&seqno, &vbucket);
switch (event) {
case TAP_PAUSE:
testHarness.waitfor_cookie(cookie);
break;
case TAP_OPAQUE:
case TAP_NOOP:
case TAP_DISCONNECT:
break;
case TAP_MUTATION:
h1->release(h, NULL, it);
num_mutations++;
break;
case TAP_DELETION:
h1->release(h, NULL, it);
num_deletes++;
break;
default:
std::cerr << "Unexpected event: " << event << std::endl;
return FAIL;
}
} while (event != TAP_DISCONNECT);
checkeq(2, num_mutations, "Incorrect number of remaining mutations");
checkeq((num_keys - 2), num_deletes, "Incorrect number of deletes");
testHarness.unlock_cookie(cookie);
testHarness.destroy_cookie(cookie);
return SUCCESS;
}
static enum test_result test_sent_from_vb(ENGINE_HANDLE *h,
ENGINE_HANDLE_V1 *h1) {
const int num_keys = 5;
for (int ii = 0; ii < num_keys; ++ii) {
std::stringstream ss;
ss << "key" << ii;
checkeq(ENGINE_SUCCESS,
store(h, h1, NULL, OPERATION_SET, ss.str().c_str(),
"value", NULL, 0, 0),
"Failed to store an item.");
}
wait_for_flusher_to_settle(h, h1);
const void *cookie = testHarness.create_cookie();
testHarness.lock_cookie(cookie);
std::string name = "tap_client_thread";
TAP_ITERATOR iter = h1->get_tap_iterator(h, cookie, name.c_str(),
name.length(),
TAP_CONNECT_FLAG_DUMP, NULL,
0);
check(iter != NULL, "Failed to create a tap iterator");
item *it;
void *engine_specific;
uint16_t nengine_specific;
uint8_t ttl;
uint16_t flags;
uint32_t seqno;
uint16_t vbucket;
tap_event_t event;
do {
event = iter(h, cookie, &it, &engine_specific,
&nengine_specific, &ttl, &flags,
&seqno, &vbucket);
switch (event) {
case TAP_PAUSE:
testHarness.waitfor_cookie(cookie);
break;
case TAP_OPAQUE:
case TAP_NOOP:
case TAP_DISCONNECT:
break;
case TAP_MUTATION:
case TAP_DELETION:
h1->release(h, NULL, it);
break;
default:
std::cerr << "Unexpected event: " << event << std::endl;
return FAIL;
}
} while (event != TAP_DISCONNECT);
checkeq(5, get_int_stat(h, h1,
"eq_tapq:tap_client_thread:sent_from_vb_0",
"tap"),
"Incorrect number of items sent");
std::map<uint16_t, uint64_t> vbmap;
vbmap[0] = 0;
changeVBFilter(h, h1, "tap_client_thread", vbmap);
checkeq(0, get_int_stat(h, h1,
"eq_tapq:tap_client_thread:sent_from_vb_0",
"tap"),
"Incorrect number of items sent");
testHarness.unlock_cookie(cookie);
testHarness.destroy_cookie(cookie);
return SUCCESS;
}
static enum test_result test_tap_takeover(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
const int num_keys = 30;
bool keys[num_keys];
int initialPersisted = get_int_stat(h, h1, "ep_total_persisted");
int initializedKeys = 0;
memset(keys, 0, sizeof(keys));
for (; initializedKeys < num_keys / 2; ++initializedKeys) {
keys[initializedKeys] = false;
std::stringstream ss;
ss << initializedKeys;
checkeq(ENGINE_SUCCESS,
store(h, h1, NULL, OPERATION_SET, ss.str().c_str(),
"value", NULL, 0, 0),
"Failed to store an item.");
}
useconds_t sleepTime = 128;
while (get_int_stat(h, h1, "ep_total_persisted")
< initialPersisted + initializedKeys) {
decayingSleep(&sleepTime);
}
for (int ii = 0; ii < initializedKeys; ++ii) {
std::stringstream ss;
ss << ii;
evict_key(h, h1, ss.str().c_str(), 0, "Ejected.");
}
uint16_t vbucketfilter[2];
vbucketfilter[0] = ntohs(1);
vbucketfilter[1] = ntohs(0);
const void *cookie = testHarness.create_cookie();
testHarness.lock_cookie(cookie);
std::string name = "tap_client_thread";
TAP_ITERATOR iter = h1->get_tap_iterator(h, cookie, name.c_str(),
name.length(),
TAP_CONNECT_FLAG_TAKEOVER_VBUCKETS |
TAP_CONNECT_FLAG_LIST_VBUCKETS,
static_cast<void*>(vbucketfilter),
4);
check(iter != NULL, "Failed to create a tap iterator");
item *it;
void *engine_specific;
uint16_t nengine_specific;
uint8_t ttl;
uint16_t flags;
uint32_t seqno;
uint16_t vbucket;
tap_event_t event;
std::string key;
uint16_t unlikely_vbucket_identifier = 17293;
bool allows_more_mutations(true);
do {
vbucket = unlikely_vbucket_identifier;
event = iter(h, cookie, &it, &engine_specific,
&nengine_specific, &ttl, &flags,
&seqno, &vbucket);
if (initializedKeys < num_keys) {
keys[initializedKeys] = false;
std::stringstream ss;
ss << initializedKeys;
checkeq(ENGINE_SUCCESS,
store(h, h1, NULL, OPERATION_SET, ss.str().c_str(),
"value", NULL, 0, 0),
"Failed to store an item.");
++initializedKeys;
}
switch (event) {
case TAP_PAUSE:
testHarness.waitfor_cookie(cookie);
break;
case TAP_OPAQUE:
case TAP_NOOP:
break;
case TAP_MUTATION:
// This will be false if we've seen a vbucket set state.
cb_assert(allows_more_mutations);
check(get_key(h, h1, it, key), "Failed to read out the key");
keys[atoi(key.c_str())] = true;
cb_assert(vbucket != unlikely_vbucket_identifier);
check(verify_item(h, h1, it, NULL, 0, "value", 5) == SUCCESS,
"Unexpected item arrived on tap stream");
h1->release(h, cookie, it);
break;
case TAP_DISCONNECT:
break;
case TAP_VBUCKET_SET:
cb_assert(nengine_specific == 4);
vbucket_state_t state;
memcpy(&state, engine_specific, nengine_specific);
state = static_cast<vbucket_state_t>(ntohl(state));
if (state == vbucket_state_active) {
allows_more_mutations = false;
}
break;
default:
std::cerr << "Unexpected event: " << event << std::endl;
return FAIL;
}
} while (event != TAP_DISCONNECT);
for (int ii = 0; ii < num_keys; ++ii) {
check(keys[ii], "Failed to receive key");
}
testHarness.unlock_cookie(cookie);
testHarness.destroy_cookie(cookie);
check(get_int_stat(h, h1, "ep_tap_total_fetched", "tap") != 0,
"http://bugs.northscale.com/show_bug.cgi?id=1695");
h1->reset_stats(h, NULL);
check(get_int_stat(h, h1, "ep_tap_total_fetched", "tap") == 0,
"Expected reset stats to clear ep_tap_total_fetched");
return SUCCESS;
}
static enum test_result test_tap_filter_stream(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
uint16_t vbid;
int num_vbstate_persist = get_int_stat(h, h1, "ep_persist_vbstate_total");
/* vb 0 is already set to active before the test begins. Start from vbid 1 */
for (vbid = 1; vbid < 4; ++vbid) {
check(set_vbucket_state(h, h1, vbid, vbucket_state_active),
"Failed to set vbucket state.");
}
/* Ensure that all the vbucket states are persisted */
wait_for_stat_to_be(h, h1, "ep_persist_vbstate_total", num_vbstate_persist + 3);
const int num_keys = 40;
bool keys[num_keys];
for (int ii = 0; ii < num_keys; ++ii) {
keys[ii] = false;
std::stringstream ss;
ss << ii;
checkeq(ENGINE_SUCCESS,
store(h, h1, NULL, OPERATION_SET, ss.str().c_str(),
"value", NULL, 0, ii % 4),
"Failed to store an item.");
}
const void *cookie = testHarness.create_cookie();
testHarness.lock_cookie(cookie);
std::map<uint16_t, uint64_t> filtermap;
filtermap[0] = 0;
filtermap[1] = 0;
filtermap[2] = 0;
uint16_t numOfVBs = htons(2); // Start with vbuckets 0 and 1
char *userdata = static_cast<char*>(cb_calloc(1, 28));
char *ptr = userdata;
memcpy(ptr, &numOfVBs, sizeof(uint16_t));
ptr += sizeof(uint16_t);
for (int i = 0; i < 2; ++i) { // vbucket ids
uint16_t vb = htons(i);
memcpy(ptr, &vb, sizeof(uint16_t));
ptr += sizeof(uint16_t);
}
memcpy(ptr, &numOfVBs, sizeof(uint16_t));
ptr += sizeof(uint16_t);
for (int i = 0; i < 2; ++i) { // vbucket ids and their checkpoint ids
uint16_t vb = htons(i);
memcpy(ptr, &vb, sizeof(uint16_t));
ptr += sizeof(uint16_t);
uint64_t chkid = htonll(filtermap[i]);
memcpy(ptr, &chkid, sizeof(uint64_t));
ptr += sizeof(uint64_t);
}
std::string name = "tap_client_thread";
TAP_ITERATOR iter = h1->get_tap_iterator(h, cookie, name.c_str(),
name.length(),
TAP_CONNECT_FLAG_LIST_VBUCKETS |
TAP_CONNECT_CHECKPOINT,
static_cast<void*>(userdata),
28); // userdata length
check(iter != NULL, "Failed to create a tap iterator");
item *it;
void *engine_specific;
uint16_t nengine_specific;
uint8_t ttl;
uint16_t flags;
uint32_t seqno;
uint16_t vbucket;
tap_event_t event;
int found = 0;
uint16_t unlikely_vbucket_identifier = 17293;
std::string key;
bool done = false;
bool filter_change_done = false;
do {
vbucket = unlikely_vbucket_identifier;
event = iter(h, cookie, &it, &engine_specific,
&nengine_specific, &ttl, &flags,
&seqno, &vbucket);
switch (event) {
case TAP_PAUSE:
done = true;
// Check if all the items except for vbucket 3 are received
for (int ii = 0; ii < num_keys; ++ii) {
if ((ii % 4) != 3 && !keys[ii]) {
done = false;
break;
}
}
if (!done) {
testHarness.waitfor_cookie(cookie);
}
break;
case TAP_NOOP:
break;
case TAP_OPAQUE:
if (nengine_specific == sizeof(uint32_t)) {
uint32_t opaque_code;
memcpy(&opaque_code, engine_specific, sizeof(opaque_code));
opaque_code = ntohl(opaque_code);
if (opaque_code == TAP_OPAQUE_COMPLETE_VB_FILTER_CHANGE) {
filter_change_done = true;
}
}
break;
case TAP_CHECKPOINT_START:
case TAP_CHECKPOINT_END:
h1->release(h, cookie, it);
break;
case TAP_MUTATION:
check(get_key(h, h1, it, key), "Failed to read out the key");
vbid = atoi(key.c_str()) % 4;
checkeq(vbid, vbucket, "Incorrect vbucket id");
check(vbid != 3,
"Received an item for a vbucket we don't subscribe to");
keys[atoi(key.c_str())] = true;
++found;
cb_assert(vbucket != unlikely_vbucket_identifier);
check(verify_item(h, h1, it, NULL, 0, "value", 5) == SUCCESS,
"Unexpected item arrived on tap stream");
h1->release(h, cookie, it);
// We've got some of the elements.. Let's change the filter
// and get the rest
if (found == 10) {
changeVBFilter(h, h1, name, filtermap);
checkeq(PROTOCOL_BINARY_RESPONSE_SUCCESS, last_status.load(),
"Expected success response from changing the TAP VB filter.");
}
break;
case TAP_DISCONNECT:
done = true;
break;
default:
std::cerr << "Unexpected event: " << event << std::endl;
return FAIL;
}
} while (!done);
testHarness.unlock_cookie(cookie);
testHarness.destroy_cookie(cookie);
cb_assert(filter_change_done);
checkeq(0,
get_int_stat(h, h1, "eq_tapq:tap_client_thread:qlen", "tap"),
"queue should be empty");
cb_free(userdata);
return SUCCESS;
}
static enum test_result test_tap_config(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
checkeq(ENGINE_SUCCESS, h1->get_stats(h, NULL, "tap", 3, add_stats),
"Failed to get stats.");
check(vals.find("ep_tap_backoff_period") != vals.end(), "Missing stat");
check(vals.find("ep_tap_ack_interval") != vals.end(), "Missing stat");
check(vals.find("ep_tap_ack_window_size") != vals.end(), "Missing stat");
check(vals.find("ep_tap_ack_grace_period") != vals.end(), "Missing stat");
std::string s = vals["ep_tap_backoff_period"];
check(strcmp(s.c_str(), "0.05") == 0, "Incorrect backoff value");
s = vals["ep_tap_ack_interval"];
check(strcmp(s.c_str(), "10") == 0, "Incorrect interval value");
s = vals["ep_tap_ack_window_size"];
check(strcmp(s.c_str(), "2") == 0, "Incorrect window size value");
s = vals["ep_tap_ack_grace_period"];
check(strcmp(s.c_str(), "10") == 0, "Incorrect grace period value");
return SUCCESS;
}
static enum test_result test_tap_default_config(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
checkeq(ENGINE_SUCCESS, h1->get_stats(h, NULL, "tap", 3, add_stats),
"Failed to get stats.");
check(vals.find("ep_tap_backoff_period") != vals.end(), "Missing stat");
check(vals.find("ep_tap_ack_interval") != vals.end(), "Missing stat");
check(vals.find("ep_tap_ack_window_size") != vals.end(), "Missing stat");
check(vals.find("ep_tap_ack_grace_period") != vals.end(), "Missing stat");
std::string s = vals["ep_tap_backoff_period"];
check(strcmp(s.c_str(), "5") == 0, "Incorrect backoff value");
s = vals["ep_tap_ack_interval"];
check(strcmp(s.c_str(), "1000") == 0, "Incorrect interval value");
s = vals["ep_tap_ack_window_size"];
check(strcmp(s.c_str(), "10") == 0, "Incorrect window size value");
s = vals["ep_tap_ack_grace_period"];
check(strcmp(s.c_str(), "300") == 0, "Incorrect grace period value");
return SUCCESS;
}
static enum test_result test_tap_ack_stream(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1)
{
const int nkeys = 10;
bool receivedKeys[nkeys];
bool nackKeys[nkeys];
for (int i = 0; i < nkeys; ++i) {
nackKeys[i] = true;
receivedKeys[i] = false;
std::stringstream ss;
ss << i;
checkeq(ENGINE_SUCCESS,
store(h, h1, NULL, OPERATION_SET, ss.str().c_str(),
"value", NULL, 0, 0),
"Failed to store an item.");
}
wait_for_flusher_to_settle(h, h1);
for (int i = 0; i < nkeys; ++i) {
std::stringstream ss;
ss << i;
item_info info;
check(get_item_info(h, h1, &info, ss.str().c_str()), "Verify items");
}
const void *cookie = testHarness.create_cookie();
testHarness.lock_cookie(cookie);
uint16_t vbucketfilter[2];
vbucketfilter[0] = htons(1);
vbucketfilter[1] = htons(0);
std::string name = "tap_ack";
TAP_ITERATOR iter = h1->get_tap_iterator(h, cookie, name.c_str(),
name.length(),
TAP_CONNECT_FLAG_LIST_VBUCKETS |
TAP_CONNECT_CHECKPOINT |
TAP_CONNECT_SUPPORT_ACK |
TAP_CONNECT_FLAG_DUMP,
static_cast<void*>(vbucketfilter),
4);
check(iter != NULL, "Failed to create a tap iterator");
item *it;
void *engine_specific;
uint16_t nengine_specific;
uint8_t ttl;
uint16_t flags;
uint32_t seqno;
uint16_t vbucket;
tap_event_t event;
std::string key;
bool done = false;
int index;
int numRollbacks = 1000;
do {
if (numRollbacks > 0) {
if (random() % 4 == 0) {
iter = NULL;
}
}
if (iter == NULL) {
testHarness.unlock_cookie(cookie);
iter = h1->get_tap_iterator(h, cookie, name.c_str(),
name.length(),
TAP_CONNECT_FLAG_LIST_VBUCKETS |
TAP_CONNECT_CHECKPOINT |
TAP_CONNECT_SUPPORT_ACK |
TAP_CONNECT_FLAG_DUMP,
static_cast<void*>(vbucketfilter),
4);
check(iter != NULL, "Failed to create a tap iterator");
testHarness.lock_cookie(cookie);
}
event = iter(h, cookie, &it, &engine_specific,
&nengine_specific, &ttl, &flags,
&seqno, &vbucket);
switch (event) {
case TAP_PAUSE:
testHarness.waitfor_cookie(cookie);
break;
case TAP_OPAQUE:
if (numRollbacks > 0) {
if (random() % 4 == 0) {