/
ep_engine.cc
4010 lines (3582 loc) · 164 KB
/
ep_engine.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 2010 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.
*/
#include "config.h"
#include <assert.h>
#include <fcntl.h>
#include <memcached/engine.h>
#include <memcached/protocol_binary.h>
#include <memcached/util.h>
#include <stdarg.h>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <limits>
#include <string>
#include <vector>
#include "backfill.h"
#include "ep_engine.h"
#include "htresizer.h"
#include "memory_tracker.h"
#include "stats-info.h"
#include "statsnap.h"
#define STATWRITER_NAMESPACE core_engine
#include "statwriter.h"
#undef STATWRITER_NAMESPACE
#include "tapthrottle.h"
#include "warmup.h"
static ALLOCATOR_HOOKS_API *hooksApi;
static SERVER_LOG_API *loggerApi;
static size_t percentOf(size_t val, double percent) {
return static_cast<size_t>(static_cast<double>(val) * percent);
}
/**
* Helper function to avoid typing in the long cast all over the place
* @param handle pointer to the engine
* @return the engine as a class
*/
static inline EventuallyPersistentEngine* getHandle(ENGINE_HANDLE* handle)
{
EventuallyPersistentEngine* ret;
ret = reinterpret_cast<EventuallyPersistentEngine*>(handle);
ObjectRegistry::onSwitchThread(ret);
return ret;
}
static inline void releaseHandle(ENGINE_HANDLE* handle) {
(void) handle;
ObjectRegistry::onSwitchThread(NULL);
}
/**
* Call the response callback and return the appropriate value so that
* the core knows what to do..
*/
static ENGINE_ERROR_CODE sendResponse(ADD_RESPONSE 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)
{
ENGINE_ERROR_CODE rv = ENGINE_FAILED;
EventuallyPersistentEngine *e = ObjectRegistry::onSwitchThread(NULL, true);
if (response(key, keylen, ext, extlen, body, bodylen, datatype,
status, cas, cookie)) {
rv = ENGINE_SUCCESS;
}
ObjectRegistry::onSwitchThread(e);
return rv;
}
template <typename T>
static void validate(T v, T l, T h) {
if (v < l || v > h) {
throw std::runtime_error("value out of range.");
}
}
// The Engine API specifies C linkage for the functions..
extern "C" {
static const engine_info* EvpGetInfo(ENGINE_HANDLE* handle)
{
engine_info* info = getHandle(handle)->getInfo();
releaseHandle(handle);
return info;
}
static ENGINE_ERROR_CODE EvpInitialize(ENGINE_HANDLE* handle,
const char* config_str)
{
ENGINE_ERROR_CODE err_code = getHandle(handle)->initialize(config_str);
releaseHandle(handle);
return err_code;
}
static void EvpDestroy(ENGINE_HANDLE* handle, const bool force)
{
getHandle(handle)->destroy(force);
delete getHandle(handle);
releaseHandle(NULL);
}
static ENGINE_ERROR_CODE EvpItemAllocate(ENGINE_HANDLE* handle,
const void* cookie,
item **itm,
const void* key,
const size_t nkey,
const size_t nbytes,
const int flags,
const rel_time_t exptime)
{
ENGINE_ERROR_CODE err_code = getHandle(handle)->itemAllocate(cookie, itm, key,
nkey, nbytes, flags,
exptime);
releaseHandle(handle);
return err_code;
}
static ENGINE_ERROR_CODE EvpItemDelete(ENGINE_HANDLE* handle,
const void* cookie,
const void* key,
const size_t nkey,
uint64_t* cas,
uint16_t vbucket)
{
ENGINE_ERROR_CODE err_code = getHandle(handle)->itemDelete(cookie, key, nkey,
cas, vbucket);
releaseHandle(handle);
return err_code;
}
static void EvpItemRelease(ENGINE_HANDLE* handle,
const void *cookie,
item* itm)
{
getHandle(handle)->itemRelease(cookie, itm);
releaseHandle(handle);
}
static ENGINE_ERROR_CODE EvpGet(ENGINE_HANDLE* handle,
const void* cookie,
item** itm,
const void* key,
const int nkey,
uint16_t vbucket)
{
ENGINE_ERROR_CODE err_code = getHandle(handle)->get(cookie, itm, key, nkey, vbucket);
releaseHandle(handle);
return err_code;
}
static ENGINE_ERROR_CODE EvpGetStats(ENGINE_HANDLE* handle,
const void* cookie,
const char* stat_key,
int nkey,
ADD_STAT add_stat)
{
ENGINE_ERROR_CODE err_code = getHandle(handle)->getStats(cookie, stat_key, nkey,
add_stat);
releaseHandle(handle);
return err_code;
}
static ENGINE_ERROR_CODE EvpStore(ENGINE_HANDLE* handle,
const void *cookie,
item* itm,
uint64_t *cas,
ENGINE_STORE_OPERATION operation,
uint16_t vbucket)
{
ENGINE_ERROR_CODE err_code = getHandle(handle)->store(cookie, itm, cas, operation,
vbucket);
releaseHandle(handle);
return err_code;
}
static ENGINE_ERROR_CODE EvpArithmetic(ENGINE_HANDLE* handle,
const void* cookie,
const void* key,
const int nkey,
const bool increment,
const bool create,
const uint64_t delta,
const uint64_t initial,
const rel_time_t exptime,
uint64_t *cas,
uint64_t *result,
uint16_t vbucket)
{
ENGINE_ERROR_CODE ecode = getHandle(handle)->arithmetic(cookie, key, nkey, increment,
create, delta, initial,
exptime, cas, result, vbucket);
releaseHandle(handle);
return ecode;
}
static ENGINE_ERROR_CODE EvpFlush(ENGINE_HANDLE* handle,
const void* cookie, time_t when)
{
ENGINE_ERROR_CODE err_code = getHandle(handle)->flush(cookie, when);
releaseHandle(handle);
return err_code;
}
static void EvpResetStats(ENGINE_HANDLE* handle, const void *)
{
getHandle(handle)->resetStats();
releaseHandle(handle);
}
static protocol_binary_response_status stopFlusher(EventuallyPersistentEngine *e,
const char **msg,
size_t *msg_size) {
return e->stopFlusher(msg, msg_size);
}
static protocol_binary_response_status startFlusher(EventuallyPersistentEngine *e,
const char **msg,
size_t *msg_size) {
return e->startFlusher(msg, msg_size);
}
static protocol_binary_response_status setTapParam(EventuallyPersistentEngine *e,
const char *keyz, const char *valz,
const char **msg, size_t *) {
protocol_binary_response_status rv = PROTOCOL_BINARY_RESPONSE_SUCCESS;
try {
int v = atoi(valz);
if (strcmp(keyz, "tap_keepalive") == 0) {
validate(v, 0, MAX_TAP_KEEP_ALIVE);
e->setTapKeepAlive(static_cast<uint32_t>(v));
} else if (strcmp(keyz, "tap_throttle_threshold") == 0) {
e->getConfiguration().setTapThrottleThreshold(v);
} else if (strcmp(keyz, "tap_throttle_queue_cap") == 0) {
e->getConfiguration().setTapThrottleQueueCap(v);
} else if (strcmp(keyz, "tap_throttle_cap_pcnt") == 0) {
e->getConfiguration().setTapThrottleCapPcnt(v);
} else {
*msg = "Unknown config param";
rv = PROTOCOL_BINARY_RESPONSE_KEY_ENOENT;
}
} catch(std::runtime_error &ignored_exception) {
*msg = "Value out of range.";
rv = PROTOCOL_BINARY_RESPONSE_EINVAL;
}
return rv;
}
static protocol_binary_response_status setCheckpointParam(EventuallyPersistentEngine *e,
const char *keyz,
const char *valz,
const char **msg,
size_t *) {
protocol_binary_response_status rv = PROTOCOL_BINARY_RESPONSE_SUCCESS;
try {
int v = atoi(valz);
if (strcmp(keyz, "chk_max_items") == 0) {
validate(v, MIN_CHECKPOINT_ITEMS, MAX_CHECKPOINT_ITEMS);
e->getConfiguration().setChkMaxItems(v);
} else if (strcmp(keyz, "chk_period") == 0) {
validate(v, MIN_CHECKPOINT_PERIOD, MAX_CHECKPOINT_PERIOD);
e->getConfiguration().setChkPeriod(v);
} else if (strcmp(keyz, "max_checkpoints") == 0) {
validate(v, DEFAULT_MAX_CHECKPOINTS, MAX_CHECKPOINTS_UPPER_BOUND);
e->getConfiguration().setMaxCheckpoints(v);
} else if (strcmp(keyz, "item_num_based_new_chk") == 0) {
if (strcmp(valz, "true") == 0) {
e->getConfiguration().setItemNumBasedNewChk(true);
} else {
e->getConfiguration().setItemNumBasedNewChk(false);
}
} else if (strcmp(keyz, "inconsistent_slave_chk") == 0) {
if (strcmp(valz, "true") == 0) {
e->getConfiguration().setInconsistentSlaveChk(true);
} else {
e->getConfiguration().setInconsistentSlaveChk(false);
}
} else if (strcmp(keyz, "keep_closed_chks") == 0) {
if (strcmp(valz, "true") == 0) {
e->getConfiguration().setKeepClosedChks(true);
} else {
e->getConfiguration().setKeepClosedChks(false);
}
} else {
*msg = "Unknown config param";
rv = PROTOCOL_BINARY_RESPONSE_KEY_ENOENT;
}
} catch(std::runtime_error &ignored_exception) {
*msg = "Value out of range.";
rv = PROTOCOL_BINARY_RESPONSE_EINVAL;
}
return rv;
}
static protocol_binary_response_status setFlushParam(EventuallyPersistentEngine *e,
const char *keyz, const char *valz,
const char **msg,
size_t *) {
protocol_binary_response_status rv = PROTOCOL_BINARY_RESPONSE_SUCCESS;
// Handle the actual mutation.
try {
int v = atoi(valz);
if (strcmp(keyz, "max_txn_size") == 0) {
e->getConfiguration().setMaxTxnSize(v);
} else if (strcmp(keyz, "bg_fetch_delay") == 0) {
e->getConfiguration().setBgFetchDelay(v);
} else if (strcmp(keyz, "flushall_enabled") == 0) {
if (strcmp(valz, "true") == 0) {
e->getConfiguration().setFlushallEnabled(true);
} else if(strcmp(valz, "false") == 0) {
e->getConfiguration().setFlushallEnabled(false);
} else {
throw std::runtime_error("value out of range.");
}
} else if (strcmp(keyz, "max_size") == 0) {
// Want more bits than int.
char *ptr = NULL;
// TODO: This parser isn't perfect.
uint64_t vsize = strtoull(valz, &ptr, 10);
validate(vsize, static_cast<uint64_t>(0),
std::numeric_limits<uint64_t>::max());
e->getConfiguration().setMaxSize(vsize);
e->getConfiguration().setMemLowWat(percentOf(vsize, 0.75));
e->getConfiguration().setMemHighWat(percentOf(vsize, 0.85));
} else if (strcmp(keyz, "mem_low_wat") == 0) {
// Want more bits than int.
char *ptr = NULL;
// TODO: This parser isn't perfect.
uint64_t vsize = strtoull(valz, &ptr, 10);
validate(vsize, static_cast<uint64_t>(0),
std::numeric_limits<uint64_t>::max());
e->getConfiguration().setMemLowWat(vsize);
} else if (strcmp(keyz, "mem_high_wat") == 0) {
// Want more bits than int.
char *ptr = NULL;
// TODO: This parser isn't perfect.
uint64_t vsize = strtoull(valz, &ptr, 10);
validate(vsize, static_cast<uint64_t>(0),
std::numeric_limits<uint64_t>::max());
e->getConfiguration().setMemHighWat(vsize);
} else if (strcmp(keyz, "mutation_mem_threshold") == 0) {
validate(v, 0, 100);
e->getConfiguration().setMutationMemThreshold(v);
} else if (strcmp(keyz, "timing_log") == 0) {
EPStats &stats = e->getEpStats();
std::ostream *old = stats.timingLog;
stats.timingLog = NULL;
delete old;
if (strcmp(valz, "off") == 0) {
LOG(EXTENSION_LOG_INFO, "Disabled timing log.");
} else {
std::ofstream *tmp(new std::ofstream(valz));
if (tmp->good()) {
LOG(EXTENSION_LOG_INFO,
"Logging detailed timings to ``%s''.", valz);
stats.timingLog = tmp;
} else {
LOG(EXTENSION_LOG_WARNING,
"Error setting detailed timing log to ``%s'': %s",
valz, strerror(errno));
delete tmp;
}
}
} else if (strcmp(keyz, "exp_pager_stime") == 0) {
char *ptr = NULL;
// TODO: This parser isn't perfect.
uint64_t vsize = strtoull(valz, &ptr, 10);
validate(vsize, static_cast<uint64_t>(0),
std::numeric_limits<uint64_t>::max());
e->getConfiguration().setExpPagerStime((size_t)vsize);
} else if (strcmp(keyz, "couch_response_timeout") == 0) {
e->getConfiguration().setCouchResponseTimeout(v);
} else if (strcmp(keyz, "klog_max_log_size") == 0) {
char *ptr = NULL;
uint64_t msize = strtoull(valz, &ptr, 10);
validate(msize, static_cast<uint64_t>(0),
std::numeric_limits<uint64_t>::max());
e->getConfiguration().setKlogMaxLogSize((size_t)msize);
} else if (strcmp(keyz, "klog_max_entry_ratio") == 0) {
validate(v, 0, std::numeric_limits<int>::max());
e->getConfiguration().setKlogMaxEntryRatio(v);
} else if (strcmp(keyz, "klog_compactor_queue_cap") == 0) {
validate(v, 0, std::numeric_limits<int>::max());
e->getConfiguration().setKlogCompactorQueueCap(v);
} else if (strcmp(keyz, "alog_sleep_time") == 0) {
e->getConfiguration().setAlogSleepTime(v);
} else if (strcmp(keyz, "alog_task_time") == 0) {
e->getConfiguration().setAlogTaskTime(v);
} else if (strcmp(keyz, "pager_active_vb_pcnt") == 0) {
e->getConfiguration().setPagerActiveVbPcnt(v);
} else if (strcmp(keyz, "warmup_min_memory_threshold") == 0) {
e->getConfiguration().setWarmupMinMemoryThreshold(v);
} else if (strcmp(keyz, "warmup_min_items_threshold") == 0) {
e->getConfiguration().setWarmupMinItemsThreshold(v);
} else {
*msg = "Unknown config param";
rv = PROTOCOL_BINARY_RESPONSE_KEY_ENOENT;
}
} catch(std::runtime_error &ignored_exception) {
*msg = "Value out of range.";
rv = PROTOCOL_BINARY_RESPONSE_EINVAL;
}
return rv;
}
static protocol_binary_response_status evictKey(EventuallyPersistentEngine *e,
protocol_binary_request_header *request,
const char **msg,
size_t *msg_size) {
protocol_binary_request_no_extras *req =
(protocol_binary_request_no_extras*)request;
char keyz[256];
// Read the key.
int keylen = ntohs(req->message.header.request.keylen);
if (keylen >= (int)sizeof(keyz)) {
*msg = "Key is too large.";
return PROTOCOL_BINARY_RESPONSE_EINVAL;
}
memcpy(keyz, ((char*)request) + sizeof(req->message.header), keylen);
keyz[keylen] = 0x00;
uint16_t vbucket = ntohs(request->request.vbucket);
std::string key(keyz, keylen);
LOG(EXTENSION_LOG_DEBUG, "Manually evicting object with key %s\n", keyz);
protocol_binary_response_status rv = e->evictKey(key, vbucket, msg, msg_size);
if (rv == PROTOCOL_BINARY_RESPONSE_NOT_MY_VBUCKET ||
rv == PROTOCOL_BINARY_RESPONSE_KEY_ENOENT) {
if (e->isDegradedMode()) {
return PROTOCOL_BINARY_RESPONSE_ETMPFAIL;
}
}
return rv;
}
ENGINE_ERROR_CODE getLocked(EventuallyPersistentEngine *e,
protocol_binary_request_header *req,
const void *cookie,
Item **itm,
const char **msg,
size_t *,
protocol_binary_response_status *res) {
uint8_t extlen = req->request.extlen;
if (extlen != 0 && extlen != 4) {
*msg = "Invalid packet format (extlen may be 0 or 4)";
*res = PROTOCOL_BINARY_RESPONSE_EINVAL;
return ENGINE_EINVAL;
}
protocol_binary_request_getl *grequest =
(protocol_binary_request_getl*)req;
*res = PROTOCOL_BINARY_RESPONSE_SUCCESS;
const char *keyp = reinterpret_cast<const char*>(req->bytes);
keyp += sizeof(req->bytes) + extlen;
std::string key(keyp, ntohs(req->request.keylen));
uint16_t vbucket = ntohs(req->request.vbucket);
RememberingCallback<GetValue> getCb;
uint32_t max_timeout = (unsigned int)e->getGetlMaxTimeout();
uint32_t default_timeout = (unsigned int)e->getGetlDefaultTimeout();
uint32_t lockTimeout = default_timeout;
if (extlen == 4) {
lockTimeout = ntohl(grequest->message.body.expiration);
}
if (lockTimeout > max_timeout || lockTimeout < 1) {
LOG(EXTENSION_LOG_DEBUG, "Illegal value for lock timeout specified "
"%u. Using default value: %u\n", lockTimeout, default_timeout);
lockTimeout = default_timeout;
}
bool gotLock = e->getLocked(key, vbucket, getCb,
ep_current_time(),
lockTimeout, cookie);
getCb.waitForValue();
ENGINE_ERROR_CODE rv = getCb.val.getStatus();
if (rv == ENGINE_SUCCESS) {
*itm = getCb.val.getValue();
} else if (rv == ENGINE_EWOULDBLOCK) {
// need to wait for value
return rv;
} else if (!gotLock){
*msg = "LOCK_ERROR";
*res = PROTOCOL_BINARY_RESPONSE_ETMPFAIL;
return ENGINE_TMPFAIL;
} else {
if (e->isDegradedMode()) {
*msg = "LOCK_TMP_ERROR";
*res = PROTOCOL_BINARY_RESPONSE_ETMPFAIL;
return ENGINE_TMPFAIL;
}
RCPtr<VBucket> vb = e->getVBucket(vbucket);
if (!vb) {
*msg = "That's not my bucket.";
*res = PROTOCOL_BINARY_RESPONSE_NOT_MY_VBUCKET;
return ENGINE_NOT_MY_VBUCKET;
}
*msg = "NOT_FOUND";
*res = PROTOCOL_BINARY_RESPONSE_KEY_ENOENT;
return ENGINE_KEY_ENOENT;
}
return rv;
}
static protocol_binary_response_status unlockKey(EventuallyPersistentEngine *e,
protocol_binary_request_header *request,
const char **msg,
size_t *)
{
protocol_binary_request_no_extras *req =
(protocol_binary_request_no_extras*)request;
protocol_binary_response_status res = PROTOCOL_BINARY_RESPONSE_SUCCESS;
char keyz[256];
// Read the key.
int keylen = ntohs(req->message.header.request.keylen);
if (keylen >= (int)sizeof(keyz)) {
*msg = "Key is too large.";
return PROTOCOL_BINARY_RESPONSE_EINVAL;
}
memcpy(keyz, ((char*)request) + sizeof(req->message.header), keylen);
keyz[keylen] = 0x00;
uint16_t vbucket = ntohs(request->request.vbucket);
std::string key(keyz, keylen);
LOG(EXTENSION_LOG_DEBUG, "Executing unl for key %s\n", keyz);
RememberingCallback<GetValue> getCb;
uint64_t cas = ntohll(request->request.cas);
ENGINE_ERROR_CODE rv = e->unlockKey(key, vbucket, cas, ep_current_time());
if (rv == ENGINE_SUCCESS) {
*msg = "UNLOCKED";
} else if (rv == ENGINE_TMPFAIL){
*msg = "UNLOCK_ERROR";
res = PROTOCOL_BINARY_RESPONSE_ETMPFAIL;
} else {
if (e->isDegradedMode()) {
*msg = "LOCK_TMP_ERROR";
return PROTOCOL_BINARY_RESPONSE_ETMPFAIL;
}
RCPtr<VBucket> vb = e->getVBucket(vbucket);
if (!vb) {
*msg = "That's not my bucket.";
res = PROTOCOL_BINARY_RESPONSE_NOT_MY_VBUCKET;
}
*msg = "NOT_FOUND";
res = PROTOCOL_BINARY_RESPONSE_KEY_ENOENT;
}
return res;
}
static protocol_binary_response_status setParam(EventuallyPersistentEngine *e,
protocol_binary_request_set_param *req,
const char **msg,
size_t *msg_size) {
size_t keylen = ntohs(req->message.header.request.keylen);
uint8_t extlen = req->message.header.request.extlen;
size_t vallen = ntohl(req->message.header.request.bodylen);
engine_param_t paramtype =
static_cast<engine_param_t>(ntohl(req->message.body.param_type));
if (keylen == 0 || (vallen - keylen - extlen) == 0) {
return PROTOCOL_BINARY_RESPONSE_EINVAL;
}
const char *keyp = reinterpret_cast<const char*>(req->bytes) + sizeof(req->bytes);
const char *valuep = keyp + keylen;
vallen -= (keylen + extlen);
char keyz[32];
char valz[512];
// Read the key.
if (keylen >= sizeof(keyz)) {
*msg = "Key is too large.";
return PROTOCOL_BINARY_RESPONSE_EINVAL;
}
memcpy(keyz, keyp, keylen);
keyz[keylen] = 0x00;
// Read the value.
if (vallen >= sizeof(valz)) {
*msg = "Value is too large.";
return PROTOCOL_BINARY_RESPONSE_EINVAL;
}
memcpy(valz, valuep, vallen);
valz[vallen] = 0x00;
protocol_binary_response_status rv;
switch (paramtype) {
case engine_param_flush:
rv = setFlushParam(e, keyz, valz, msg, msg_size);
break;
case engine_param_tap:
rv = setTapParam(e, keyz, valz, msg, msg_size);
break;
case engine_param_checkpoint:
rv = setCheckpointParam(e, keyz, valz, msg, msg_size);
break;
default:
rv = PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND;
}
return rv;
}
static ENGINE_ERROR_CODE getVBucket(EventuallyPersistentEngine *e,
const void *cookie,
protocol_binary_request_header *request,
ADD_RESPONSE response) {
protocol_binary_request_get_vbucket *req =
reinterpret_cast<protocol_binary_request_get_vbucket*>(request);
assert(req);
uint16_t vbucket = ntohs(req->message.header.request.vbucket);
RCPtr<VBucket> vb = e->getVBucket(vbucket);
if (!vb) {
const std::string msg("That's not my bucket.");
return sendResponse(response, NULL, 0, NULL, 0, msg.c_str(), msg.length(),
PROTOCOL_BINARY_RAW_BYTES,
PROTOCOL_BINARY_RESPONSE_NOT_MY_VBUCKET, 0, cookie);
} else {
vbucket_state_t state = (vbucket_state_t)ntohl(vb->getState());
return sendResponse(response, NULL, 0, NULL, 0, &state, sizeof(state),
PROTOCOL_BINARY_RAW_BYTES,
PROTOCOL_BINARY_RESPONSE_SUCCESS, 0, cookie);
}
}
static ENGINE_ERROR_CODE setVBucket(EventuallyPersistentEngine *e,
const void *cookie,
protocol_binary_request_header *request,
ADD_RESPONSE response)
{
protocol_binary_request_set_vbucket *req =
reinterpret_cast<protocol_binary_request_set_vbucket*>(request);
size_t bodylen = ntohl(req->message.header.request.bodylen)
- ntohs(req->message.header.request.keylen);
if (bodylen != sizeof(vbucket_state_t)) {
const std::string msg("Incorrect packet format");
sendResponse(response, NULL, 0, NULL, 0, msg.c_str(), msg.length(),
PROTOCOL_BINARY_RAW_BYTES,
PROTOCOL_BINARY_RESPONSE_EINVAL, 0, cookie);
}
vbucket_state_t state;
memcpy(&state, &req->message.body.state, sizeof(state));
state = static_cast<vbucket_state_t>(ntohl(state));
if (!is_valid_vbucket_state_t(state)) {
const std::string msg("Invalid vbucket state");
sendResponse(response, NULL, 0, NULL, 0, msg.c_str(), msg.length(),
PROTOCOL_BINARY_RAW_BYTES,
PROTOCOL_BINARY_RESPONSE_EINVAL, 0, cookie);
}
uint16_t vb = ntohs(req->message.header.request.vbucket);
if(e->setVBucketState(vb, state) == ENGINE_ERANGE) {
const std::string msg("VBucket number too big");
return sendResponse(response, NULL, 0, NULL, 0, msg.c_str(),
msg.length(), PROTOCOL_BINARY_RAW_BYTES,
PROTOCOL_BINARY_RESPONSE_ERANGE, 0, cookie);
}
return sendResponse(response, NULL, 0, NULL, 0, NULL, 0,
PROTOCOL_BINARY_RAW_BYTES,
PROTOCOL_BINARY_RESPONSE_SUCCESS, 0, cookie);
}
static ENGINE_ERROR_CODE delVBucket(EventuallyPersistentEngine *e,
const void *cookie,
protocol_binary_request_header *req,
ADD_RESPONSE response) {
std::string msg = "";
protocol_binary_response_status res = PROTOCOL_BINARY_RESPONSE_SUCCESS;
uint16_t vbucket = ntohs(req->request.vbucket);
if (ntohs(req->request.keylen) > 0 || req->request.extlen > 0) {
msg = "Incorrect packet format.";
return sendResponse(response, NULL, 0, NULL, 0, msg.c_str(), msg.length(),
PROTOCOL_BINARY_RAW_BYTES,
PROTOCOL_BINARY_RESPONSE_EINVAL, 0, cookie);
}
bool sync = false;
uint32_t bodylen = ntohl(req->request.bodylen);
if (bodylen > 0) {
const char* ptr = reinterpret_cast<const char*>(req->bytes) +
sizeof(req->bytes);
if (bodylen == 7 && strncmp(ptr, "async=0", bodylen) == 0) {
sync = true;
}
}
ENGINE_ERROR_CODE err;
void* es = e->getEngineSpecific(cookie);
if (sync) {
if (es == NULL) {
err = e->deleteVBucket(vbucket, cookie);
e->storeEngineSpecific(cookie, e);
} else {
e->storeEngineSpecific(cookie, NULL);
LOG(EXTENSION_LOG_INFO, "Completed sync deletion of vbucket %u",
(unsigned)vbucket);
err = ENGINE_SUCCESS;
}
} else {
err = e->deleteVBucket(vbucket);
}
switch (err) {
case ENGINE_SUCCESS:
LOG(EXTENSION_LOG_WARNING,
"Deletion of vbucket %d was completed.", vbucket);
break;
case ENGINE_NOT_MY_VBUCKET:
LOG(EXTENSION_LOG_WARNING, "Deletion of vbucket %d failed "
"because the vbucket doesn't exist!!!", vbucket);
msg = "Failed to delete vbucket. Bucket not found.";
res = PROTOCOL_BINARY_RESPONSE_NOT_MY_VBUCKET;
break;
case ENGINE_EINVAL:
LOG(EXTENSION_LOG_WARNING, "Deletion of vbucket %d failed "
"because the vbucket is not in a dead state\n", vbucket);
msg = "Failed to delete vbucket. Must be in the dead state.";
res = PROTOCOL_BINARY_RESPONSE_EINVAL;
break;
case ENGINE_EWOULDBLOCK:
LOG(EXTENSION_LOG_WARNING, "Requst to vbucket %d deletion is in"
" EWOULDBLOCK until the database file is removed from disk",
vbucket);
return ENGINE_EWOULDBLOCK;
default:
LOG(EXTENSION_LOG_WARNING, "Deletion of vbucket %d failed "
"because of unknown reasons\n", vbucket);
msg = "Failed to delete vbucket. Unknown reason.";
res = PROTOCOL_BINARY_RESPONSE_EINTERNAL;
}
return sendResponse(response, NULL, 0, NULL, 0, msg.c_str(), msg.length(),
PROTOCOL_BINARY_RAW_BYTES, res, 0, cookie);
}
static ENGINE_ERROR_CODE getReplicaCmd(EventuallyPersistentEngine *e,
protocol_binary_request_header *request,
const void *cookie,
Item **it,
const char **msg,
protocol_binary_response_status *res) {
EventuallyPersistentStore *eps = e->getEpStore();
protocol_binary_request_no_extras *req =
(protocol_binary_request_no_extras*)request;
int keylen = ntohs(req->message.header.request.keylen);
uint16_t vbucket = ntohs(req->message.header.request.vbucket);
ENGINE_ERROR_CODE error_code;
std::string keystr(((char *)request) + sizeof(req->message.header), keylen);
GetValue rv(eps->getReplica(keystr, vbucket, cookie, true));
if ((error_code = rv.getStatus()) != ENGINE_SUCCESS) {
if (error_code == ENGINE_NOT_MY_VBUCKET) {
*msg = "That's not my bucket.";
*res = PROTOCOL_BINARY_RESPONSE_NOT_MY_VBUCKET;
} else if (error_code == ENGINE_TMPFAIL) {
*msg = "NOT_FOUND";
*res = PROTOCOL_BINARY_RESPONSE_KEY_ENOENT;
} else {
return error_code;
}
} else {
*it = rv.getValue();
*res = PROTOCOL_BINARY_RESPONSE_SUCCESS;
}
return ENGINE_SUCCESS;
}
static ENGINE_ERROR_CODE processUnknownCommand(EventuallyPersistentEngine *h,
const void* cookie,
protocol_binary_request_header *request,
ADD_RESPONSE response)
{
protocol_binary_response_status res = PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND;
const char *msg = NULL;
size_t msg_size = 0;
Item *itm = NULL;
EPStats &stats = h->getEpStats();
ENGINE_ERROR_CODE rv = ENGINE_SUCCESS;
switch (request->request.opcode) {
case PROTOCOL_BINARY_CMD_GET_VBUCKET:
{
BlockTimer timer(&stats.getVbucketCmdHisto);
rv = getVBucket(h, cookie, request, response);
return rv;
}
case PROTOCOL_BINARY_CMD_DEL_VBUCKET:
{
BlockTimer timer(&stats.delVbucketCmdHisto);
rv = delVBucket(h, cookie, request, response);
return rv;
}
case PROTOCOL_BINARY_CMD_SET_VBUCKET:
{
BlockTimer timer(&stats.setVbucketCmdHisto);
rv = setVBucket(h, cookie, request, response);
return rv;
}
case PROTOCOL_BINARY_CMD_TOUCH:
case PROTOCOL_BINARY_CMD_GAT:
case PROTOCOL_BINARY_CMD_GATQ:
{
rv = h->touch(cookie, request, response);
return rv;
}
case CMD_STOP_PERSISTENCE:
res = stopFlusher(h, &msg, &msg_size);
break;
case CMD_START_PERSISTENCE:
res = startFlusher(h, &msg, &msg_size);
break;
case CMD_SET_PARAM:
res = setParam(h, reinterpret_cast<protocol_binary_request_set_param*>(request),
&msg, &msg_size);
break;
case CMD_EVICT_KEY:
res = evictKey(h, request, &msg, &msg_size);
break;
case CMD_GET_LOCKED:
rv = getLocked(h, request, cookie, &itm, &msg, &msg_size, &res);
if (rv == ENGINE_EWOULDBLOCK) {
// we dont have the value for the item yet
return rv;
}
break;
case CMD_UNLOCK_KEY:
res = unlockKey(h, request, &msg, &msg_size);
break;
case CMD_OBSERVE:
return h->observe(cookie, request, response);
case CMD_DEREGISTER_TAP_CLIENT:
{
rv = h->deregisterTapClient(cookie, request, response);
return rv;
}
case CMD_RESET_REPLICATION_CHAIN:
{
rv = h->resetReplicationChain(cookie, request, response);
return rv;
}
case CMD_CHANGE_VB_FILTER:
{
rv = h->changeTapVBFilter(cookie, request, response);
return rv;
}
case CMD_LAST_CLOSED_CHECKPOINT:
case CMD_CREATE_CHECKPOINT:
case CMD_EXTEND_CHECKPOINT:
case CMD_CHECKPOINT_PERSISTENCE:
{
rv = h->handleCheckpointCmds(cookie, request, response);
return rv;
}
case CMD_GET_META:
case CMD_GETQ_META:
{
rv = h->getMeta(cookie,
reinterpret_cast<protocol_binary_request_get_meta*>(request),
response);
return rv;
}
case CMD_SET_WITH_META:
case CMD_SETQ_WITH_META:
case CMD_ADD_WITH_META:
case CMD_ADDQ_WITH_META:
{
rv = h->setWithMeta(cookie,
reinterpret_cast<protocol_binary_request_set_with_meta*>(request),
response);
return rv;
}
case CMD_DEL_WITH_META:
case CMD_DELQ_WITH_META:
{
rv = h->deleteWithMeta(cookie,
reinterpret_cast<protocol_binary_request_delete_with_meta*>(request),
response);
return rv;
}
case CMD_GET_REPLICA:
rv = getReplicaCmd(h, request, cookie, &itm, &msg, &res);
if (rv != ENGINE_SUCCESS) {
return rv;
}
break;
case CMD_ENABLE_TRAFFIC:
case CMD_DISABLE_TRAFFIC:
{
rv = h->handleTrafficControlCmd(cookie, request, response);
return rv;
}
}
// Send a special response for getl since we don't want to send the key
if (itm && request->request.opcode == CMD_GET_LOCKED) {
uint32_t flags = itm->getFlags();
rv = sendResponse(response, NULL, 0, (const void *)&flags, sizeof(uint32_t),
static_cast<const void *>(itm->getData()),
itm->getNBytes(),
PROTOCOL_BINARY_RAW_BYTES,
static_cast<uint16_t>(res), itm->getCas(),
cookie);
delete itm;
} else if (itm) {
const std::string &key = itm->getKey();
uint32_t flags = itm->getFlags();
rv = sendResponse(response, static_cast<const void *>(key.data()),
itm->getNKey(),
(const void *)&flags, sizeof(uint32_t),
static_cast<const void *>(itm->getData()),
itm->getNBytes(),
PROTOCOL_BINARY_RAW_BYTES,
static_cast<uint16_t>(res), itm->getCas(),
cookie);
delete itm;
} else {
msg_size = (msg_size > 0 || msg == NULL) ? msg_size : strlen(msg);
rv = sendResponse(response, NULL, 0, NULL, 0,
msg, static_cast<uint16_t>(msg_size),
PROTOCOL_BINARY_RAW_BYTES,
static_cast<uint16_t>(res), 0, cookie);
}
return rv;
}
static ENGINE_ERROR_CODE EvpUnknownCommand(ENGINE_HANDLE* handle,
const void* cookie,
protocol_binary_request_header *request,
ADD_RESPONSE response)
{
ENGINE_ERROR_CODE err_code = processUnknownCommand(getHandle(handle), cookie,
request, response);
releaseHandle(handle);
return err_code;
}
static void EvpItemSetCas(ENGINE_HANDLE* , const void *,
item *itm, uint64_t cas) {
static_cast<Item*>(itm)->setCas(cas);
}