/
ep_test_apis.cc
818 lines (705 loc) · 29.6 KB
/
ep_test_apis.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
/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* Copyright 2010 NorthScale, 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.
*/
#include "config.h"
#include <assert.h>
#include <iostream>
#include <stdlib.h>
#include <sstream>
#include <string.h>
#include <memcached/util.h>
#include "ep_test_apis.h"
#define check(expr, msg) \
static_cast<void>((expr) ? 0 : abort_msg(#expr, msg, __LINE__))
extern "C" bool abort_msg(const char *expr, const char *msg, int line);
std::map<std::string, std::string> vals;
bool dump_stats = false;
protocol_binary_response_status last_status =
static_cast<protocol_binary_response_status>(0);
uint32_t last_bodylen = 0;
char *last_key = NULL;
char *last_body = NULL;
bool last_deleted_flag = false;
uint64_t last_cas = 0;
ItemMetaData last_meta;
extern "C" bool add_response_get_meta(const void *key, uint16_t keylen,
const void *ext, uint8_t extlen,
const void *body, uint32_t bodylen,
uint8_t datatype, uint16_t status,
uint64_t cas, const void *cookie);
void encodeExt(char *buffer, uint32_t val);
void encodeWithMetaExt(char *buffer, ItemMetaData *meta);
void decayingSleep(useconds_t *sleepTime) {
static const useconds_t maxSleepTime = 500000;
usleep(*sleepTime);
*sleepTime = std::min(*sleepTime << 1, maxSleepTime);
}
bool add_response(const void *key, uint16_t keylen, const void *ext,
uint8_t extlen, const void *body, uint32_t bodylen,
uint8_t datatype, uint16_t status, uint64_t cas,
const void *cookie) {
(void)ext;
(void)extlen;
(void)datatype;
(void)cookie;
last_bodylen = bodylen;
last_status = static_cast<protocol_binary_response_status>(status);
if (last_body) {
free(last_body);
last_body = NULL;
}
if (bodylen > 0) {
last_body = static_cast<char*>(malloc(bodylen + 1));
assert(last_body);
memcpy(last_body, body, bodylen);
last_body[bodylen] = '\0';
}
if (last_key) {
free(last_key);
last_key = NULL;
}
if (keylen > 0) {
last_key = static_cast<char*>(malloc(keylen + 1));
assert(last_key);
memcpy(last_key, key, keylen);
last_key[keylen] = '\0';
}
last_cas = cas;
return true;
}
bool add_response_get_meta(const void *key, uint16_t keylen, const void *ext,
uint8_t extlen, const void *body, uint32_t bodylen,
uint8_t datatype, uint16_t status, uint64_t cas,
const void *cookie) {
(void)datatype;
(void)cookie;
const uint8_t* ext_bytes = reinterpret_cast<const uint8_t*> (ext);
if (ext && extlen > 0) {
uint32_t flags;
memcpy(&flags, ext_bytes, 4);
last_deleted_flag = ntohl(flags) & GET_META_ITEM_DELETED_FLAG;
memcpy(&last_meta.flags, ext_bytes + 4, 4);
memcpy(&last_meta.exptime, ext_bytes + 8, 4);
last_meta.exptime = ntohl(last_meta.exptime);
memcpy(&last_meta.seqno, ext_bytes + 12, 8);
last_meta.seqno = memcached_ntohll(last_meta.seqno);
last_meta.cas = cas;
}
return add_response(key, keylen, ext, extlen, body, bodylen, datatype,
status, cas, cookie);
}
bool add_response_ret_meta(const void *key, uint16_t keylen, const void *ext,
uint8_t extlen, const void *body, uint32_t bodylen,
uint8_t datatype, uint16_t status, uint64_t cas,
const void *cookie) {
(void)datatype;
(void)cookie;
const uint8_t* ext_bytes = reinterpret_cast<const uint8_t*> (ext);
if (ext && extlen == 16) {
memcpy(&last_meta.flags, ext_bytes, 4);
memcpy(&last_meta.exptime, ext_bytes + 4, 4);
last_meta.exptime = ntohl(last_meta.exptime);
memcpy(&last_meta.seqno, ext_bytes + 8, 8);
last_meta.seqno = memcached_ntohll(last_meta.seqno);
last_meta.cas = cas;
}
return add_response(key, keylen, ext, extlen, body, bodylen, datatype,
status, cas, cookie);
}
void add_stats(const char *key, const uint16_t klen, const char *val,
const uint32_t vlen, const void *cookie) {
(void)cookie;
std::string k(key, klen);
std::string v(val, vlen);
if (dump_stats) {
std::cout << "stat[" << k << "] = " << v << std::endl;
}
vals[k] = v;
}
void encodeExt(char *buffer, uint32_t val) {
val = htonl(val);
memcpy(buffer, (char*)&val, sizeof(val));
}
void encodeWithMetaExt(char *buffer, ItemMetaData *meta) {
uint32_t flags = meta->flags;
uint32_t exp = htonl(meta->exptime);
uint64_t seqno = htonll(meta->seqno);
uint64_t cas = htonll(meta->cas);
memcpy(buffer, (char*)&flags, sizeof(flags));
memcpy(buffer + 4, (char*)&exp, sizeof(exp));
memcpy(buffer + 8, (char*)&seqno, sizeof(seqno));
memcpy(buffer + 16, (char*)&cas, sizeof(cas));
}
protocol_binary_request_header* createPacket(uint8_t opcode,
uint16_t vbid,
uint64_t cas,
const char *ext,
uint32_t extlen,
const char *key,
uint32_t keylen,
const char *val,
uint32_t vallen) {
char *pkt_raw;
uint32_t headerlen = sizeof(protocol_binary_request_header);
pkt_raw = static_cast<char*>(calloc(1, headerlen + extlen + keylen + vallen));
assert(pkt_raw);
protocol_binary_request_header *req =
(protocol_binary_request_header*)pkt_raw;
req->request.opcode = opcode;
req->request.keylen = htons(keylen);
req->request.extlen = extlen;
req->request.vbucket = htons(vbid);
req->request.bodylen = htonl(keylen + vallen + extlen);
req->request.cas = ntohll(cas);
if (extlen > 0) {
memcpy(pkt_raw + headerlen, ext, extlen);
}
if (keylen > 0) {
memcpy(pkt_raw + headerlen + extlen, key, keylen);
}
if (vallen > 0) {
memcpy(pkt_raw + headerlen + extlen + keylen, val, vallen);
}
return req;
}
void add_with_meta(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char *key,
const size_t keylen, const char *val, const size_t vallen,
const uint32_t vb, ItemMetaData *itemMeta,
bool skipConflictResolution) {
int blen = skipConflictResolution ? 28 : 24;
char ext[blen];
encodeWithMetaExt(ext, itemMeta);
if (skipConflictResolution) {
uint32_t flag = SKIP_CONFLICT_RESOLUTION_FLAG;
flag = htonl(flag);
memcpy(ext + 24, (char*)&flag, sizeof(flag));
}
protocol_binary_request_header *pkt;
pkt = createPacket(CMD_ADD_WITH_META, vb, 0, ext, blen, key, keylen,
val, vallen);
check(h1->unknown_command(h, NULL, pkt, add_response) == ENGINE_SUCCESS,
"Expected to be able to store with meta");
}
void changeVBFilter(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, std::string name,
std::map<uint16_t, uint64_t> &filtermap) {
std::stringstream value;
uint16_t vbs = htons(filtermap.size());
std::map<uint16_t, uint64_t>::iterator it;
value.write((char*) &vbs, sizeof(uint16_t));
for (it = filtermap.begin(); it != filtermap.end(); it++) {
uint16_t vb = htons(it->first);
uint64_t chkid = htonll(it->second);
value.write((char*) &vb, sizeof(uint16_t));
value.write((char*) &chkid, sizeof(uint64_t));
}
protocol_binary_request_header *request;
request = createPacket(CMD_CHANGE_VB_FILTER, 0, 0, NULL, 0, name.c_str(),
name.length(), value.str().data(), value.str().length());
check(h1->unknown_command(h, NULL, request, add_response) == ENGINE_SUCCESS,
"Failed to change the TAP VB filter.");
free(request);
}
void createCheckpoint(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
protocol_binary_request_header *request = createPacket(CMD_CREATE_CHECKPOINT);
check(h1->unknown_command(h, NULL, request, add_response) == ENGINE_SUCCESS,
"Failed to create a new checkpoint.");
free(request);
}
ENGINE_ERROR_CODE del(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char *key,
uint64_t cas, uint16_t vbucket, const void* cookie) {
return h1->remove(h, cookie, key, strlen(key), &cas, vbucket);
}
void del_with_meta(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char *key,
const size_t keylen, const uint32_t vb,
ItemMetaData *itemMeta, uint64_t cas_for_delete,
bool skipConflictResolution) {
int blen = skipConflictResolution ? 28 : 24;
char ext[blen];
encodeWithMetaExt(ext, itemMeta);
if (skipConflictResolution) {
uint32_t flag = SKIP_CONFLICT_RESOLUTION_FLAG;
flag = htonl(flag);
memcpy(ext + 24, (char*)&flag, sizeof(flag));
}
protocol_binary_request_header *pkt;
pkt = createPacket(CMD_DEL_WITH_META, vb, cas_for_delete, ext, blen, key,
keylen);
check(h1->unknown_command(h, NULL, pkt, add_response) == ENGINE_SUCCESS,
"Expected to be able to delete with meta");
}
void evict_key(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char *key,
uint16_t vbucketId, const char *msg, bool expectError) {
int nonResidentItems = get_int_stat(h, h1, "ep_num_non_resident");
int numEjectedItems = get_int_stat(h, h1, "ep_num_value_ejects");
protocol_binary_request_header *pkt = createPacket(CMD_EVICT_KEY, 0, 0,
NULL, 0, key, strlen(key));
pkt->request.vbucket = htons(vbucketId);
check(h1->unknown_command(h, NULL, pkt, add_response) == ENGINE_SUCCESS,
"Failed to evict key.");
if (expectError) {
check(last_status == PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS,
"Expected exists when evicting key.");
} else {
if (strcmp(last_body, "Already ejected.") != 0) {
nonResidentItems++;
numEjectedItems++;
}
check(last_status == PROTOCOL_BINARY_RESPONSE_SUCCESS,
"Expected success evicting key.");
}
check(get_int_stat(h, h1, "ep_num_non_resident") == nonResidentItems,
"Incorrect number of non-resident items");
check(get_int_stat(h, h1, "ep_num_value_ejects") == numEjectedItems,
"Incorrect number of ejected items");
if (msg != NULL && strcmp(last_body, msg) != 0) {
fprintf(stderr, "Expected evict to return ``%s'', but it returned ``%s''\n",
msg, last_body);
abort();
}
}
size_t estimateVBucketMove(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
uint16_t vbid, const char* tap_name) {
std::stringstream ss;
ss << "tap-vbtakeover " << vbid;
if (tap_name) {
ss << " " << tap_name;
}
return get_int_stat(h, h1, "estimate", ss.str().c_str());
}
void extendCheckpoint(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
uint32_t checkpoint_num) {
char val[4];
encodeExt(val, checkpoint_num);
protocol_binary_request_header *request;
request = createPacket(CMD_EXTEND_CHECKPOINT, 0, 0, NULL, 0, NULL, 0, val, 4);
check(h1->unknown_command(h, NULL, request, add_response) == ENGINE_SUCCESS,
"Failed to extend the open checkpoint.");
free(request);
}
ENGINE_ERROR_CODE checkpointPersistence(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
uint64_t checkpoint_id) {
checkpoint_id = htonll(checkpoint_id);
protocol_binary_request_header *request;
request = createPacket(CMD_CHECKPOINT_PERSISTENCE, 0, 0, NULL, 0, NULL, 0,
(const char *)&checkpoint_id, sizeof(uint64_t));
ENGINE_ERROR_CODE rv = h1->unknown_command(h, NULL, request, add_response);
free(request);
return rv;
}
void gat(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char* key,
uint16_t vb, uint32_t exp, bool quiet) {
char ext[4];
uint8_t opcode = quiet ? PROTOCOL_BINARY_CMD_GATQ : PROTOCOL_BINARY_CMD_GAT;
uint32_t keylen = key ? strlen(key) : 0;
protocol_binary_request_header *request;
encodeExt(ext, exp);
request = createPacket(opcode, vb, 0, ext, 4, key, keylen);
check(h1->unknown_command(h, NULL, request, add_response) == ENGINE_SUCCESS,
"Failed to call gat");
free(request);
}
bool get_item_info(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, item_info *info,
const char* key, uint16_t vb) {
item *i = NULL;
if (h1->get(h, NULL, &i, key, strlen(key), vb) != ENGINE_SUCCESS) {
return false;
}
info->nvalue = 1;
if (!h1->get_item_info(h, NULL, i, info)) {
h1->release(h, NULL, i);
fprintf(stderr, "get_item_info failed\n");
return false;
}
h1->release(h, NULL, i);
return true;
}
bool get_key(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, item *i,
std::string &key) {
item_info info;
info.nvalue = 1;
if (!h1->get_item_info(h, NULL, i, &info)) {
fprintf(stderr, "get_item_info failed\n");
return false;
}
key.assign((const char*)info.key, info.nkey);
return true;
}
void getl(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char* key,
uint16_t vb, uint32_t lock_timeout) {
char ext[4];
uint32_t keylen = key ? strlen(key) : 0;
protocol_binary_request_header *request;
encodeExt(ext, lock_timeout);
request = createPacket(CMD_GET_LOCKED, vb, 0, ext, 4, key, keylen);
check(h1->unknown_command(h, NULL, request, add_response) == ENGINE_SUCCESS,
"Failed to call getl");
free(request);
}
bool get_meta(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char* key) {
protocol_binary_request_header *req = createPacket(CMD_GET_META, 0, 0, NULL,
0, key, strlen(key));
ENGINE_ERROR_CODE ret = h1->unknown_command(h, NULL, req,
add_response_get_meta);
check(ret == ENGINE_SUCCESS, "Expected get_meta call to be successful");
if (last_status == PROTOCOL_BINARY_RESPONSE_SUCCESS) {
return true;
}
return false;
}
void observe(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
std::map<std::string, uint16_t> obskeys) {
std::stringstream value;
std::map<std::string, uint16_t>::iterator it;
for (it = obskeys.begin(); it != obskeys.end(); it++) {
uint16_t vb = htons(it->second);
uint16_t keylen = htons(it->first.length());
value.write((char*) &vb, sizeof(uint16_t));
value.write((char*) &keylen, sizeof(uint16_t));
value.write(it->first.c_str(), it->first.length());
}
protocol_binary_request_header *request;
request = createPacket(CMD_OBSERVE, 0, 0, NULL, 0, NULL, 0,
value.str().data(), value.str().length());
check(h1->unknown_command(h, NULL, request, add_response) == ENGINE_SUCCESS,
"Observe call failed");
free(request);
}
void get_replica(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char* key,
uint16_t vbid) {
protocol_binary_request_header *pkt;
pkt = createPacket(CMD_GET_REPLICA, vbid, 0, NULL, 0, key, strlen(key));
check(h1->unknown_command(h, NULL, pkt, add_response) == ENGINE_SUCCESS,
"Get Replica Failed");
free(pkt);
}
protocol_binary_request_header* prepare_get_replica(ENGINE_HANDLE *h,
ENGINE_HANDLE_V1 *h1,
vbucket_state_t state,
bool makeinvalidkey) {
uint16_t id = 0;
const char *key = "k0";
protocol_binary_request_header *pkt;
pkt = createPacket(CMD_GET_REPLICA, id, 0, NULL, 0, key, strlen(key));
if (!makeinvalidkey) {
item *i = NULL;
check(store(h, h1, NULL, OPERATION_SET, key, "replicadata", &i, 0, id)
== ENGINE_SUCCESS, "Get Replica Failed");
h1->release(h, NULL, i);
check(set_vbucket_state(h, h1, id, state),
"Failed to set vbucket active state, Get Replica Failed");
}
return pkt;
}
bool set_param(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, engine_param_t paramtype,
const char *param, const char *val) {
char ext[4];
protocol_binary_request_header *pkt;
encodeExt(ext, static_cast<uint32_t>(paramtype));
pkt = createPacket(CMD_SET_PARAM, 0, 0, ext, sizeof(engine_param_t), param,
strlen(param), val, strlen(val));
if (h1->unknown_command(h, NULL, pkt, add_response) != ENGINE_SUCCESS) {
return false;
}
return last_status == PROTOCOL_BINARY_RESPONSE_SUCCESS;
}
bool set_vbucket_state(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
uint16_t vb, vbucket_state_t state) {
char ext[4];
protocol_binary_request_header *pkt;
encodeExt(ext, static_cast<uint32_t>(state));
pkt = createPacket(PROTOCOL_BINARY_CMD_SET_VBUCKET, vb, 0, ext, 4);
if (h1->unknown_command(h, NULL, pkt, add_response) != ENGINE_SUCCESS) {
return false;
}
return last_status == PROTOCOL_BINARY_RESPONSE_SUCCESS;
}
void set_with_meta(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char *key,
const size_t keylen, const char *val, const size_t vallen,
const uint32_t vb, ItemMetaData *itemMeta,
uint64_t cas_for_set, bool skipConflictResolution) {
int blen = skipConflictResolution ? 28 : 24;
char ext[blen];
encodeWithMetaExt(ext, itemMeta);
if (skipConflictResolution) {
uint32_t flag = SKIP_CONFLICT_RESOLUTION_FLAG;
flag = htonl(flag);
memcpy(ext + 24, (char*)&flag, sizeof(flag));
}
protocol_binary_request_header *pkt;
pkt = createPacket(CMD_SET_WITH_META, vb, cas_for_set, ext, blen, key, keylen,
val, vallen);
check(h1->unknown_command(h, NULL, pkt, add_response) == ENGINE_SUCCESS,
"Expected to be able to store with meta");
}
void return_meta(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char *key,
const size_t keylen, const char *val, const size_t vallen,
const uint32_t vb, const uint64_t cas, const uint32_t flags,
const uint32_t exp, const uint32_t type) {
char ext[12];
encodeExt(ext, type);
encodeExt(ext + 4, flags);
encodeExt(ext + 8, exp);
protocol_binary_request_header *pkt;
pkt = createPacket(CMD_RETURN_META, vb, cas, ext, 12, key, keylen, val,
vallen);
check(h1->unknown_command(h, NULL, pkt, add_response_ret_meta)
== ENGINE_SUCCESS, "Expected to be able to store ret meta");
free(pkt);
}
void set_ret_meta(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char *key,
const size_t keylen, const char *val, const size_t vallen,
const uint32_t vb, const uint64_t cas, const uint32_t flags,
const uint32_t exp) {
return_meta(h, h1, key, keylen, val, vallen, vb, cas, flags, exp,
SET_RET_META);
}
void add_ret_meta(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char *key,
const size_t keylen, const char *val, const size_t vallen,
const uint32_t vb, const uint64_t cas, const uint32_t flags,
const uint32_t exp) {
return_meta(h, h1, key, keylen, val, vallen, vb, cas, flags, exp,
ADD_RET_META);
}
void del_ret_meta(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char *key,
const size_t keylen, const uint32_t vb, const uint64_t cas) {
return_meta(h, h1, key, keylen, NULL, 0, vb, cas, 0, 0,
DEL_RET_META);
}
void disable_traffic(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
protocol_binary_request_header *pkt = createPacket(CMD_DISABLE_TRAFFIC);
check(h1->unknown_command(h, NULL, pkt, add_response) == ENGINE_SUCCESS,
"Failed to send data traffic command to the server");
check(last_status == PROTOCOL_BINARY_RESPONSE_SUCCESS,
"Failed to disable data traffic");
free(pkt);
}
void enable_traffic(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
protocol_binary_request_header *pkt = createPacket(CMD_ENABLE_TRAFFIC);
check(h1->unknown_command(h, NULL, pkt, add_response) == ENGINE_SUCCESS,
"Failed to send data traffic command to the server");
check(last_status == PROTOCOL_BINARY_RESPONSE_SUCCESS,
"Failed to enable data traffic");
free(pkt);
}
void start_persistence(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
protocol_binary_request_header *pkt = createPacket(CMD_START_PERSISTENCE);
check(h1->unknown_command(h, NULL, pkt, add_response) == ENGINE_SUCCESS,
"Failed to stop persistence.");
check(last_status == PROTOCOL_BINARY_RESPONSE_SUCCESS,
"Error starting persistence.");
}
void stop_persistence(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
while (true) {
useconds_t sleepTime = 128;
if (get_str_stat(h, h1, "ep_flusher_state", 0) == "running") {
break;
}
decayingSleep(&sleepTime);
}
protocol_binary_request_header *pkt = createPacket(CMD_STOP_PERSISTENCE);
check(h1->unknown_command(h, NULL, pkt, add_response) == ENGINE_SUCCESS,
"Failed to stop persistence.");
check(last_status == PROTOCOL_BINARY_RESPONSE_SUCCESS,
"Error stopping persistence.");
}
ENGINE_ERROR_CODE store(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
const void *cookie, ENGINE_STORE_OPERATION op,
const char *key, const char *value, item **outitem,
uint64_t casIn, uint16_t vb, uint32_t exp) {
return storeCasVb11(h, h1, cookie, op, key, value, strlen(value),
9258, outitem, casIn, vb, exp);
}
ENGINE_ERROR_CODE storeCasVb11(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
const void *cookie, ENGINE_STORE_OPERATION op,
const char *key, const char *value, size_t vlen,
uint32_t flags, item **outitem, uint64_t casIn,
uint16_t vb, uint32_t exp) {
item *it = NULL;
uint64_t cas = 0;
ENGINE_ERROR_CODE rv = h1->allocate(h, cookie, &it,
key, strlen(key),
vlen, flags, exp);
check(rv == ENGINE_SUCCESS, "Allocation failed.");
item_info info;
info.nvalue = 1;
if (!h1->get_item_info(h, cookie, it, &info)) {
abort();
}
memcpy(info.value[0].iov_base, value, vlen);
h1->item_set_cas(h, cookie, it, casIn);
rv = h1->store(h, cookie, it, &cas, op, vb);
if (outitem) {
*outitem = it;
} else {
h1->release(h, NULL, it);
}
return rv;
}
void touch(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char* key,
uint16_t vb, uint32_t exp) {
char ext[4];
uint32_t keylen = key ? strlen(key) : 0;
protocol_binary_request_header *request;
encodeExt(ext, exp);
request = createPacket(PROTOCOL_BINARY_CMD_TOUCH, vb, 0, ext, 4, key, keylen);
check(h1->unknown_command(h, NULL, request, add_response) == ENGINE_SUCCESS,
"Failed to call touch");
free(request);
}
void unl(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char* key,
uint16_t vb, uint64_t cas) {
uint32_t keylen = key ? strlen(key) : 0;
protocol_binary_request_header *request;
request = createPacket(CMD_UNLOCK_KEY, vb, cas, NULL, 0, key, keylen);
check(h1->unknown_command(h, NULL, request, add_response) == ENGINE_SUCCESS,
"Failed to call unl");
free(request);
}
void vbucketDelete(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, uint16_t vb,
const char* args) {
uint32_t argslen = args ? strlen(args) : 0;
protocol_binary_request_header *pkt =
createPacket(PROTOCOL_BINARY_CMD_DEL_VBUCKET, vb, 0, NULL, 0, NULL, 0,
args, argslen);
check(h1->unknown_command(h, NULL, pkt, add_response) == ENGINE_SUCCESS,
"Failed to request delete bucket");
}
ENGINE_ERROR_CODE verify_key(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
const char* key, uint16_t vbucket) {
item *i = NULL;
ENGINE_ERROR_CODE rv = h1->get(h, NULL, &i, key, strlen(key), vbucket);
if (rv == ENGINE_SUCCESS) {
h1->release(h, NULL, i);
}
return rv;
}
bool verify_vbucket_missing(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
uint16_t vb) {
char vbid[8];
snprintf(vbid, sizeof(vbid), "vb_%d", vb);
std::string vbstr(vbid);
// Try up to three times to verify the bucket is missing. Bucket
// state changes are async.
vals.clear();
check(h1->get_stats(h, NULL, NULL, 0, add_stats) == ENGINE_SUCCESS,
"Failed to get stats.");
if (vals.find(vbstr) == vals.end()) {
return true;
}
std::cerr << "Expected bucket missing, got " << vals[vbstr] << std::endl;
return false;
}
bool verify_vbucket_state(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, uint16_t vb,
vbucket_state_t expected, bool mute) {
protocol_binary_request_header *pkt;
pkt = createPacket(PROTOCOL_BINARY_CMD_GET_VBUCKET, vb, 0);
ENGINE_ERROR_CODE errcode = h1->unknown_command(h, NULL, pkt, add_response);
if (errcode != ENGINE_SUCCESS) {
if (!mute) {
fprintf(stderr, "Error code when getting vbucket %d\n", errcode);
}
return false;
}
if (last_status != PROTOCOL_BINARY_RESPONSE_SUCCESS) {
if (!mute) {
fprintf(stderr, "Last protocol status was %d (%s)\n",
last_status, last_body ? last_body : "unknown");
}
return false;
}
vbucket_state_t state;
memcpy(&state, last_body, sizeof(state));
state = static_cast<vbucket_state_t>(ntohl(state));
return state == expected;
}
int get_int_stat(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char *statname,
const char *statkey) {
vals.clear();
check(h1->get_stats(h, NULL, statkey, statkey == NULL ? 0 : strlen(statkey),
add_stats) == ENGINE_SUCCESS, "Failed to get stats.");
std::string s = vals[statname];
return atoi(s.c_str());
}
std::string get_str_stat(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
const char *statname, const char *statkey) {
vals.clear();
check(h1->get_stats(h, NULL, statkey, statkey == NULL ? 0 : strlen(statkey),
add_stats) == ENGINE_SUCCESS, "Failed to get stats.");
std::string s = vals[statname];
return s;
}
void verify_curr_items(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, int exp,
const char *msg) {
int curr_items = get_int_stat(h, h1, "curr_items");
if (curr_items != exp) {
std::cerr << "Expected "<< exp << " curr_items after " << msg
<< ", got " << curr_items << std::endl;
abort();
}
}
void wait_for_stat_change(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
const char *stat, int initial) {
useconds_t sleepTime = 128;
while (get_int_stat(h, h1, stat) == initial) {
decayingSleep(&sleepTime);
}
}
void wait_for_stat_to_be(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
const char *stat, int final, const char* stat_key) {
useconds_t sleepTime = 128;
while (get_int_stat(h, h1, stat, stat_key) != final) {
decayingSleep(&sleepTime);
}
}
void wait_for_memory_usage_below(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
int mem_threshold) {
useconds_t sleepTime = 128;
while (get_int_stat(h, h1, "mem_used") > mem_threshold) {
decayingSleep(&sleepTime);
}
}
bool wait_for_warmup_complete(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
while (h1->get_stats(h, NULL, "warmup", 6, add_stats) == ENGINE_SUCCESS) {
useconds_t sleepTime = 128;
std::string s = vals["ep_warmup_thread"];
if (strcmp(s.c_str(), "complete") == 0) {
break;
}
decayingSleep(&sleepTime);
vals.clear();
}
return true;
}
void wait_for_flusher_to_settle(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
useconds_t sleepTime = 128;
while (get_int_stat(h, h1, "ep_queue_size") > 0) {
decayingSleep(&sleepTime);
}
wait_for_stat_change(h, h1, "ep_commit_num", 0);
}
void wait_for_persisted_value(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
const char *key, const char *val,
uint16_t vbucketId) {
item *i = NULL;
int commitNum = get_int_stat(h, h1, "ep_commit_num");
check(ENGINE_SUCCESS == store(h, h1, NULL, OPERATION_SET, key, val, &i, 0,
vbucketId),
"Failed to store an item.");
// Wait for persistence...
wait_for_flusher_to_settle(h, h1);
wait_for_stat_change(h, h1, "ep_commit_num", commitNum);
h1->release(h, NULL, i);
}