forked from memcached/memcached
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy_internal.c
1734 lines (1563 loc) · 55.1 KB
/
proxy_internal.c
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 -*- */
// Functions related to local command execution.
#include "proxy.h"
#include "storage.h"
#define PROXY_STORAGE_GET 0
#define PROXY_STORAGE_MG 1
#define _DO_CAS true
#define _NO_CAS false
#define _DO_TOUCH true
#define _NO_TOUCH false
static int _store_item_copy_from_buf(item *d_it, char *buf, const int len) {
if (d_it->it_flags & ITEM_CHUNKED) {
item_chunk *dch = (item_chunk *) ITEM_schunk(d_it);
int done = 0;
// Fill dch's via a flat data buffer
while (len > done && dch) {
int todo = (dch->size - dch->used < len - done)
? dch->size - dch->used : len - done;
memcpy(dch->data + dch->used, buf + done, todo);
done += todo;
dch->used += todo;
assert(dch->used <= dch->size);
if (dch->size == dch->used) {
item_chunk *tch = do_item_alloc_chunk(dch, len - done);
if (tch) {
dch = tch;
} else {
return -1;
}
}
}
assert(len == done);
} else {
memcpy(ITEM_data(d_it), buf, len);
}
return 0;
}
// TODO (v2): out_string() needs to change to just take a *resp, but I don't
// want to do the huge refactor in this change series. So for now we have a
// custom out_string().
static void pout_string(mc_resp *resp, const char *str) {
size_t len;
bool skip = resp->skip;
assert(resp != NULL);
// if response was original filled with something, but we're now writing
// out an error or similar, have to reset the object first.
resp_reset(resp);
// We blank the response "just in case", but if we're not intending on
// sending it lets not rewrite it.
if (skip) {
resp->skip = true;
return;
}
// Fill response object with static string.
len = strlen(str);
if ((len + 2) > WRITE_BUFFER_SIZE) {
/* ought to be always enough. just fail for simplicity */
str = "SERVER_ERROR output line too long";
len = strlen(str);
}
memcpy(resp->wbuf, str, len);
memcpy(resp->wbuf + len, "\r\n", 2);
resp_add_iov(resp, resp->wbuf, len + 2);
return;
}
// For meta commands error strings override the quiet flag.
static void pout_errstring(mc_resp *resp, const char *str) {
resp->skip = false;
pout_string(resp, str);
}
#ifdef EXTSTORE
static void _storage_get_item_cb(void *e, obj_io *eio, int ret) {
io_pending_proxy_t *io = (io_pending_proxy_t *)eio->data;
assert(io->active == true);
mc_resp *resp = io->tresp;
item *read_it = (item *)eio->buf;
bool miss = false;
if (ret < 1) {
miss = true;
} else {
uint32_t crc2;
uint32_t crc = (uint32_t) read_it->exptime;
crc2 = crc32c(0, (char *)read_it+STORE_OFFSET, eio->len-STORE_OFFSET);
if (crc != crc2) {
miss = true;
io->badcrc = true;
}
}
if (miss && !resp->skip) {
resp->iovcnt = 1;
if (io->gettype == PROXY_STORAGE_GET) {
if (io->ascii_multiget) {
resp->iov[0].iov_len = 0;
resp->iov[0].iov_base = "";
resp->tosend = 0;
} else {
resp->iov[0].iov_len = 5;
resp->iov[0].iov_base = "END\r\n";
resp->tosend = 5;
}
} else if (io->gettype == PROXY_STORAGE_MG) {
resp->iov[0].iov_len = 4;
resp->iov[0].iov_base = "EN\r\n";
resp->tosend = 5;
} else {
assert(1 == 0);
}
}
if (!miss) {
resp->iov[io->iovec_data].iov_base = ITEM_data(read_it);
}
io->miss = miss;
io->active = false;
// in proxy mode we tend to return IO's as they happen so we can keep
// latency down more.
return_io_pending((io_pending_t *)io);
}
// TODO (v2): if the item is smaller than resp->wbuf[] shouldn't we just read
// directly into there? item only necessary for recache.
static int proxy_storage_get(LIBEVENT_THREAD *t, item *it, mc_resp *resp,
int type) {
#ifdef NEED_ALIGN
item_hdr hdr;
memcpy(&hdr, ITEM_data(it), sizeof(hdr));
#else
item_hdr *hdr = (item_hdr *)ITEM_data(it);
#endif
size_t ntotal = ITEM_ntotal(it);
io_pending_proxy_t *io = do_cache_alloc(t->io_cache);
// this is a re-cast structure, so assert that we never outsize it.
assert(sizeof(io_pending_t) >= sizeof(io_pending_proxy_t));
memset(io, 0, sizeof(io_pending_proxy_t));
io->active = true;
// io_pending owns the reference for this object now.
io->hdr_it = it;
io->tresp = resp; // our mc_resp is a temporary object.
io->io_queue_type = IO_QUEUE_EXTSTORE;
io->io_type = IO_PENDING_TYPE_EXTSTORE; // proxy specific sub-type.
io->gettype = type;
io->thread = t;
io->return_cb = proxy_return_rctx_cb;
io->finalize_cb = proxy_finalize_rctx_cb;
obj_io *eio = &io->eio;
eio->buf = malloc(ntotal);
if (eio->buf == NULL) {
do_cache_free(t->io_cache, io);
return -1;
}
io->iovec_data = resp->iovcnt;
resp_add_iov(resp, "", it->nbytes);
// We can't bail out anymore, so mc_resp owns the IO from here.
resp->io_pending = (io_pending_t *)io;
// reference ourselves for the callback.
eio->data = (void *)io;
// Now, fill in io->io based on what was in our header.
#ifdef NEED_ALIGN
eio->page_version = hdr.page_version;
eio->page_id = hdr.page_id;
eio->offset = hdr.offset;
#else
eio->page_version = hdr->page_version;
eio->page_id = hdr->page_id;
eio->offset = hdr->offset;
#endif
eio->len = ntotal;
eio->mode = OBJ_IO_READ;
eio->cb = _storage_get_item_cb;
pthread_mutex_lock(&t->stats.mutex);
t->stats.get_extstore++;
pthread_mutex_unlock(&t->stats.mutex);
return 0;
}
#endif // EXTSTORE
/* client flags == 0 means use no storage for client flags */
static inline int make_ascii_get_suffix(char *suffix, item *it, bool return_cas, int nbytes) {
char *p = suffix;
*p = ' ';
p++;
if (FLAGS_SIZE(it) == 0) {
*p = '0';
p++;
} else {
p = itoa_u64(*((client_flags_t *) ITEM_suffix(it)), p);
}
*p = ' ';
p = itoa_u32(nbytes-2, p+1);
if (return_cas) {
*p = ' ';
p = itoa_u64(ITEM_get_cas(it), p+1);
}
*p = '\r';
*(p+1) = '\n';
*(p+2) = '\0';
return (p - suffix) + 2;
}
static void process_get_cmd(LIBEVENT_THREAD *t, mcp_parser_t *pr, mc_resp *resp, bool return_cas, bool should_touch) {
const char *key = &pr->request[pr->tokens[pr->keytoken]];
int nkey = pr->klen;
rel_time_t exptime = 0;
bool overflow = false; // unused.
if (nkey > KEY_MAX_LENGTH) {
pout_string(resp, "CLIENT_ERROR bad command line format");
return;
}
item *it = limited_get(key, nkey, t, exptime, should_touch, DO_UPDATE, &overflow);
if (it) {
int nbytes = it->nbytes;;
nbytes = it->nbytes;
char *p = resp->wbuf;
memcpy(p, "VALUE ", 6);
p += 6;
memcpy(p, ITEM_key(it), it->nkey);
p += it->nkey;
p += make_ascii_get_suffix(p, it, return_cas, nbytes);
resp_add_iov(resp, resp->wbuf, p - resp->wbuf);
#ifdef EXTSTORE
if (it->it_flags & ITEM_HDR) {
if (proxy_storage_get(t, it, resp, PROXY_STORAGE_GET) != 0) {
pthread_mutex_lock(&t->stats.mutex);
t->stats.get_oom_extstore++;
pthread_mutex_unlock(&t->stats.mutex);
item_remove(it);
proxy_out_errstring(resp, PROXY_SERVER_ERROR, "out of memory writing get response");
return;
}
} else if ((it->it_flags & ITEM_CHUNKED) == 0) {
resp_add_iov(resp, ITEM_data(it), it->nbytes);
} else {
resp_add_chunked_iov(resp, it, it->nbytes);
}
#else
if ((it->it_flags & ITEM_CHUNKED) == 0) {
resp_add_iov(resp, ITEM_data(it), it->nbytes);
} else {
resp_add_chunked_iov(resp, it, it->nbytes);
}
#endif
/* item_get() has incremented it->refcount for us */
pthread_mutex_lock(&t->stats.mutex);
if (should_touch) {
t->stats.touch_cmds++;
t->stats.slab_stats[ITEM_clsid(it)].touch_hits++;
} else {
t->stats.lru_hits[it->slabs_clsid]++;
t->stats.get_cmds++;
}
pthread_mutex_unlock(&t->stats.mutex);
#ifdef EXTSTORE
/* If ITEM_HDR, an io_wrap owns the reference. */
if ((it->it_flags & ITEM_HDR) == 0) {
resp->item = it;
}
#else
resp->item = it;
#endif
} else {
pthread_mutex_lock(&t->stats.mutex);
if (should_touch) {
t->stats.touch_cmds++;
t->stats.touch_misses++;
} else {
t->stats.get_misses++;
t->stats.get_cmds++;
}
pthread_mutex_unlock(&t->stats.mutex);
}
resp_add_iov(resp, "END\r\n", 5);
return;
}
static void process_update_cmd(LIBEVENT_THREAD *t, mcp_parser_t *pr, mc_resp *resp, int comm, bool handle_cas) {
const char *key = &pr->request[pr->tokens[pr->keytoken]];
size_t nkey = pr->klen;
client_flags_t flags;
int32_t exptime_int = 0;
rel_time_t exptime = 0;
uint64_t req_cas_id = 0;
item *it;
assert(resp != NULL);
if (nkey > KEY_MAX_LENGTH) {
pout_string(resp, "CLIENT_ERROR bad command line format");
return;
}
// TODO (v2): these safe_str* functions operate on C _strings_, but these
// tokens simply end with a space or carriage return/newline, so we either
// need custom functions or validate harder that these calls won't bite us
// later.
if (! (safe_strtoflags(&pr->request[pr->tokens[2]], &flags)
&& safe_strtol(&pr->request[pr->tokens[3]], &exptime_int))) {
pout_string(resp, "CLIENT_ERROR bad command line format");
return;
}
exptime = realtime(EXPTIME_TO_POSITIVE_TIME(exptime_int));
// does cas value exist?
if (handle_cas) {
if (!safe_strtoull(&pr->request[pr->tokens[5]], &req_cas_id)) {
pout_string(resp, "CLIENT_ERROR bad command line format");
return;
}
}
// vlen is validated from the main parser.
if (settings.detail_enabled) {
stats_prefix_record_set(key, nkey);
}
it = item_alloc(key, nkey, flags, exptime, pr->vlen);
if (it == 0) {
//enum store_item_type status;
if (! item_size_ok(nkey, flags, pr->vlen)) {
pout_string(resp, "SERVER_ERROR object too large for cache");
//status = TOO_LARGE;
pthread_mutex_lock(&t->stats.mutex);
t->stats.store_too_large++;
pthread_mutex_unlock(&t->stats.mutex);
} else {
pout_string(resp, "SERVER_ERROR out of memory storing object");
//status = NO_MEMORY;
pthread_mutex_lock(&t->stats.mutex);
t->stats.store_no_memory++;
pthread_mutex_unlock(&t->stats.mutex);
}
//LOGGER_LOG(c->thread->l, LOG_MUTATIONS, LOGGER_ITEM_STORE,
// NULL, status, comm, key, nkey, 0, 0, c->sfd);
/* Avoid stale data persisting in cache because we failed alloc.
* Unacceptable for SET. Anywhere else too? */
if (comm == NREAD_SET) {
it = item_get(key, nkey, t, DONT_UPDATE);
if (it) {
item_unlink(it);
STORAGE_delete(t->storage, it);
item_remove(it);
}
}
return;
}
ITEM_set_cas(it, req_cas_id);
pthread_mutex_lock(&t->stats.mutex);
t->stats.slab_stats[ITEM_clsid(it)].set_cmds++;
pthread_mutex_unlock(&t->stats.mutex);
// complete_nread_proxy() does the data chunk check so all we need to do
// is copy the data.
if (_store_item_copy_from_buf(it, pr->vbuf, it->nbytes) != 0) {
pout_string(resp, "SERVER_ERROR out of memory storing object");
item_remove(it);
return;
}
int ret = store_item(it, comm, t, NULL, NULL, CAS_NO_STALE);
switch (ret) {
case STORED:
pout_string(resp, "STORED");
break;
case EXISTS:
pout_string(resp, "EXISTS");
break;
case NOT_FOUND:
pout_string(resp, "NOT_FOUND");
break;
case NOT_STORED:
pout_string(resp, "NOT_STORED");
break;
default:
pout_string(resp, "SERVER_ERROR Unhandled storage type.");
}
// We don't need to hold a reference since the item was fully read.
item_remove(it);
}
static void process_arithmetic_cmd(LIBEVENT_THREAD *t, mcp_parser_t *pr, mc_resp *resp, const bool incr) {
char temp[INCR_MAX_STORAGE_LEN];
uint64_t delta;
const char *key = &pr->request[pr->tokens[pr->keytoken]];
size_t nkey = pr->klen;
assert(t != NULL);
if (nkey > KEY_MAX_LENGTH) {
pout_string(resp, "CLIENT_ERROR bad command line format");
return;
}
if (!safe_strtoull(&pr->request[pr->tokens[2]], &delta)) {
pout_string(resp, "CLIENT_ERROR invalid numeric delta argument");
return;
}
switch(add_delta(t, key, nkey, incr, delta, temp, NULL)) {
case OK:
pout_string(resp, temp);
break;
case NON_NUMERIC:
pout_string(resp, "CLIENT_ERROR cannot increment or decrement non-numeric value");
break;
case EOM:
pout_string(resp, "SERVER_ERROR out of memory");
break;
case DELTA_ITEM_NOT_FOUND:
pthread_mutex_lock(&t->stats.mutex);
if (incr) {
t->stats.incr_misses++;
} else {
t->stats.decr_misses++;
}
pthread_mutex_unlock(&t->stats.mutex);
pout_string(resp, "NOT_FOUND");
break;
case DELTA_ITEM_CAS_MISMATCH:
break; /* Should never get here */
}
}
static void process_delete_cmd(LIBEVENT_THREAD *t, mcp_parser_t *pr, mc_resp *resp) {
const char *key = &pr->request[pr->tokens[pr->keytoken]];
size_t nkey = pr->klen;
item *it;
uint32_t hv;
assert(t != NULL);
// NOTE: removed a compatibility bodge from a decade ago.
// delete used to take a "delay" argument, which was removed, but some
// ancient php clients always sent a 0 argument, which would then fail.
// It's been long enough that I don't want to carry this forward into the
// new parser.
if (nkey > KEY_MAX_LENGTH) {
pout_string(resp, "CLIENT_ERROR bad command line format");
return;
}
it = item_get_locked(key, nkey, t, DONT_UPDATE, &hv);
if (it) {
//MEMCACHED_COMMAND_DELETE(c->sfd, ITEM_key(it), it->nkey);
pthread_mutex_lock(&t->stats.mutex);
t->stats.slab_stats[ITEM_clsid(it)].delete_hits++;
pthread_mutex_unlock(&t->stats.mutex);
do_item_unlink(it, hv);
STORAGE_delete(t->storage, it);
do_item_remove(it); /* release our reference */
pout_string(resp, "DELETED");
} else {
pthread_mutex_lock(&t->stats.mutex);
t->stats.delete_misses++;
pthread_mutex_unlock(&t->stats.mutex);
pout_string(resp, "NOT_FOUND");
}
item_unlock(hv);
}
static void process_touch_cmd(LIBEVENT_THREAD *t, mcp_parser_t *pr, mc_resp *resp) {
const char *key = &pr->request[pr->tokens[pr->keytoken]];
size_t nkey = pr->klen;
int32_t exptime_int = 0;
rel_time_t exptime = 0;
item *it;
assert(t != NULL);
if (nkey > KEY_MAX_LENGTH) {
pout_string(resp, "CLIENT_ERROR bad command line format");
return;
}
if (!safe_strtol(&pr->request[pr->tokens[2]], &exptime_int)) {
pout_string(resp, "CLIENT_ERROR invalid exptime argument");
return;
}
exptime = realtime(EXPTIME_TO_POSITIVE_TIME(exptime_int));
it = item_touch(key, nkey, exptime, t);
if (it) {
pthread_mutex_lock(&t->stats.mutex);
t->stats.touch_cmds++;
t->stats.slab_stats[ITEM_clsid(it)].touch_hits++;
pthread_mutex_unlock(&t->stats.mutex);
pout_string(resp, "TOUCHED");
item_remove(it);
} else {
pthread_mutex_lock(&t->stats.mutex);
t->stats.touch_cmds++;
t->stats.touch_misses++;
pthread_mutex_unlock(&t->stats.mutex);
pout_string(resp, "NOT_FOUND");
}
}
/*** meta command handlers ***/
// FIXME: macro or public interface, this is copypasted.
static int _process_token_len(mcp_parser_t *pr, size_t token) {
const char *s = pr->request + pr->tokens[token];
const char *e = pr->request + pr->tokens[token+1];
// start of next token is after any space delimiters, so back those out.
while (*(e-1) == ' ') {
e--;
}
return e - s;
}
#define META_SPACE(p) { \
*p = ' '; \
p++; \
}
#define META_CHAR(p, c) { \
*p = ' '; \
*(p+1) = c; \
p += 2; \
}
// FIXME: binary key support.
#define META_KEY(p, key, nkey, bin) { \
META_CHAR(p, 'k'); \
memcpy(p, key, nkey); \
p += nkey; \
}
#define MFLAG_MAX_OPT_LENGTH 20
#define MFLAG_MAX_OPAQUE_LENGTH 32
struct _meta_flags {
unsigned int has_error :1; // flipped if we found an error during parsing.
unsigned int no_update :1;
unsigned int locked :1;
unsigned int vivify :1;
unsigned int la :1;
unsigned int hit :1;
unsigned int value :1;
unsigned int set_stale :1;
unsigned int no_reply :1;
unsigned int has_cas :1;
unsigned int new_ttl :1;
unsigned int key_binary:1;
char mode; // single character mode switch, common to ms/ma
rel_time_t exptime;
rel_time_t autoviv_exptime;
rel_time_t recache_time;
client_flags_t client_flags;
uint64_t req_cas_id;
uint64_t delta; // ma
uint64_t initial; // ma
};
static int _meta_flag_preparse(mcp_parser_t *pr, const size_t start,
struct _meta_flags *of, char **errstr) {
unsigned int i;
//size_t ret;
int32_t tmp_int;
uint8_t seen[127] = {0};
// Start just past the key token. Look at first character of each token.
for (i = start; i < pr->ntokens; i++) {
uint8_t o = (uint8_t)pr->request[pr->tokens[i]];
// zero out repeat flags so we don't over-parse for return data.
if (o >= 127 || seen[o] != 0) {
*errstr = "CLIENT_ERROR duplicate flag";
return -1;
}
seen[o] = 1;
switch (o) {
// base64 decode the key in-place, as the binary should always be
// shorter and the conversion code buffers bytes.
// TODO: we need temporary space for the binary key decode since
// request should be const.
/*case 'b':
ret = base64_decode((unsigned char *)tokens[KEY_TOKEN].value, tokens[KEY_TOKEN].length,
(unsigned char *)tokens[KEY_TOKEN].value, tokens[KEY_TOKEN].length);
if (ret == 0) {
// Failed to decode
*errstr = "CLIENT_ERROR error decoding key";
of->has_error = 1;
}
tokens[KEY_TOKEN].length = ret;
of->key_binary = 1;
break;*/
/* Negative exptimes can underflow and end up immortal. realtime() will
immediately expire values that are greater than REALTIME_MAXDELTA, but less
than process_started, so lets aim for that. */
case 'N':
of->locked = 1;
of->vivify = 1;
if (!safe_strtol(&pr->request[pr->tokens[i]+1], &tmp_int)) {
*errstr = "CLIENT_ERROR bad token in command line format";
of->has_error = 1;
} else {
of->autoviv_exptime = realtime(EXPTIME_TO_POSITIVE_TIME(tmp_int));
}
break;
case 'T':
of->locked = 1;
if (!safe_strtol(&pr->request[pr->tokens[i]+1], &tmp_int)) {
*errstr = "CLIENT_ERROR bad token in command line format";
of->has_error = 1;
} else {
of->exptime = realtime(EXPTIME_TO_POSITIVE_TIME(tmp_int));
of->new_ttl = true;
}
break;
case 'R':
of->locked = 1;
if (!safe_strtol(&pr->request[pr->tokens[i]+1], &tmp_int)) {
*errstr = "CLIENT_ERROR bad token in command line format";
of->has_error = 1;
} else {
of->recache_time = realtime(EXPTIME_TO_POSITIVE_TIME(tmp_int));
}
break;
case 'l':
of->la = 1;
of->locked = 1; // need locked to delay LRU bump
break;
case 'O':
case 'P':
case 'L':
break;
case 'k': // known but no special handling
case 's':
case 't':
case 'c':
case 'f':
break;
case 'v':
of->value = 1;
break;
case 'h':
of->locked = 1; // need locked to delay LRU bump
break;
case 'u':
of->no_update = 1;
break;
case 'q':
of->no_reply = 1;
break;
// mset-related.
case 'F':
if (!safe_strtoflags(&pr->request[pr->tokens[i]+1], &of->client_flags)) {
of->has_error = true;
}
break;
case 'C': // mset, mdelete, marithmetic
if (!safe_strtoull(&pr->request[pr->tokens[i]+1], &of->req_cas_id)) {
*errstr = "CLIENT_ERROR bad token in command line format";
of->has_error = true;
} else {
of->has_cas = true;
}
break;
case 'M': // mset and marithmetic mode switch
// FIXME: this used to error if the token isn't a single byte.
// It probably should still?
of->mode = pr->request[pr->tokens[i]+1];
break;
case 'J': // marithmetic initial value
if (!safe_strtoull(&pr->request[pr->tokens[i]+1], &of->initial)) {
*errstr = "CLIENT_ERROR invalid numeric initial value";
of->has_error = 1;
}
break;
case 'D': // marithmetic delta value
if (!safe_strtoull(&pr->request[pr->tokens[i]+1], &of->delta)) {
*errstr = "CLIENT_ERROR invalid numeric delta value";
of->has_error = 1;
}
break;
case 'I':
of->set_stale = 1;
break;
default: // unknown flag, bail.
*errstr = "CLIENT_ERROR invalid flag";
return -1;
}
}
return of->has_error ? -1 : 0;
}
static void process_mget_cmd(LIBEVENT_THREAD *t, mcp_parser_t *pr, mc_resp *resp) {
const char *key = &pr->request[pr->tokens[pr->keytoken]];
size_t nkey = pr->klen;
item *it;
unsigned int i = 0;
struct _meta_flags of = {0}; // option bitflags.
uint32_t hv; // cached hash value for unlocking an item.
bool failed = false;
bool item_created = false;
bool won_token = false;
bool ttl_set = false;
char *errstr = "CLIENT_ERROR bad command line format";
assert(t != NULL);
char *p = resp->wbuf;
int tlen = 0;
// FIXME: still needed?
//WANT_TOKENS_MIN(ntokens, 3);
if (nkey > KEY_MAX_LENGTH) {
pout_string(resp, "CLIENT_ERROR bad command line format");
return;
}
if (pr->ntokens > MFLAG_MAX_OPT_LENGTH) {
// TODO: ensure the command tokenizer gives us at least this many
pout_errstring(resp, "CLIENT_ERROR options flags are too long");
return;
}
// scrubs duplicated options and sets flags for how to load the item.
// we pass in the first token that should be a flag.
if (_meta_flag_preparse(pr, 2, &of, &errstr) != 0) {
pout_errstring(resp, errstr);
return;
}
bool overflow = false;
if (!of.locked) {
it = limited_get(key, nkey, t, 0, false, !of.no_update, &overflow);
} else {
// If we had to lock the item, we're doing our own bump later.
it = limited_get_locked(key, nkey, t, DONT_UPDATE, &hv, &overflow);
}
// Since we're a new protocol, we can actually inform users that refcount
// overflow is happening by straight up throwing an error.
// We definitely don't want to re-autovivify by accident.
if (overflow) {
assert(it == NULL);
pout_errstring(resp, "SERVER_ERROR refcount overflow during fetch");
return;
}
if (it == NULL && of.vivify) {
// Fill in the exptime during parsing later.
it = item_alloc(key, nkey, 0, realtime(0), 2);
// We don't actually need any of do_store_item's logic:
// - already fetched and missed an existing item.
// - lock is still held.
// - not append/prepend/replace
// - not testing CAS
if (it != NULL) {
// I look forward to the day I get rid of this :)
memcpy(ITEM_data(it), "\r\n", 2);
// NOTE: This initializes the CAS value.
do_item_link(it, hv);
item_created = true;
}
}
// don't have to check result of add_iov() since the iov size defaults are
// enough.
if (it) {
if (of.value) {
memcpy(p, "VA ", 3);
p = itoa_u32(it->nbytes-2, p+3);
} else {
memcpy(p, "HD", 2);
p += 2;
}
for (i = pr->keytoken+1; i < pr->ntokens; i++) {
switch (pr->request[pr->tokens[i]]) {
case 'T':
ttl_set = true;
it->exptime = of.exptime;
break;
case 'N':
if (item_created) {
it->exptime = of.autoviv_exptime;
won_token = true;
}
break;
case 'R':
// If we haven't autovivified and supplied token is less
// than current TTL, mark a win.
if ((it->it_flags & ITEM_TOKEN_SENT) == 0
&& !item_created
&& it->exptime != 0
&& it->exptime < of.recache_time) {
won_token = true;
}
break;
case 's':
META_CHAR(p, 's');
p = itoa_u32(it->nbytes-2, p);
break;
case 't':
// TTL remaining as of this request.
// needs to be relative because server clocks may not be in sync.
META_CHAR(p, 't');
if (it->exptime == 0) {
*p = '-';
*(p+1) = '1';
p += 2;
} else {
p = itoa_u32(it->exptime - current_time, p);
}
break;
case 'c':
META_CHAR(p, 'c');
p = itoa_u64(ITEM_get_cas(it), p);
break;
case 'f':
META_CHAR(p, 'f');
if (FLAGS_SIZE(it) == 0) {
*p = '0';
p++;
} else {
p = itoa_u64(*((client_flags_t *) ITEM_suffix(it)), p);
}
break;
case 'l':
META_CHAR(p, 'l');
p = itoa_u32(current_time - it->time, p);
break;
case 'h':
META_CHAR(p, 'h');
if (it->it_flags & ITEM_FETCHED) {
*p = '1';
} else {
*p = '0';
}
p++;
break;
case 'O':
tlen = _process_token_len(pr, i);
if (tlen > MFLAG_MAX_OPAQUE_LENGTH) {
errstr = "CLIENT_ERROR opaque token too long";
goto error;
}
META_SPACE(p);
memcpy(p, &pr->request[pr->tokens[i]], tlen);
p += tlen;
break;
case 'k':
META_KEY(p, ITEM_key(it), it->nkey, (it->it_flags & ITEM_KEY_BINARY));
break;
}
}
// Has this item already sent a token?
// Important to do this here so we don't send W with Z.
// Isn't critical, but easier for client authors to understand.
if (it->it_flags & ITEM_TOKEN_SENT) {
META_CHAR(p, 'Z');
}
if (it->it_flags & ITEM_STALE) {
META_CHAR(p, 'X');
// FIXME: think hard about this. is this a default, or a flag?
if ((it->it_flags & ITEM_TOKEN_SENT) == 0) {
// If we're stale but no token already sent, now send one.
won_token = true;
}
}
if (won_token) {
// Mark a win into the flag buffer.
META_CHAR(p, 'W');
it->it_flags |= ITEM_TOKEN_SENT;
}
*p = '\r';
*(p+1) = '\n';
*(p+2) = '\0';
p += 2;
// finally, chain in the buffer.
resp_add_iov(resp, resp->wbuf, p - resp->wbuf);
if (of.value) {
#ifdef EXTSTORE
if (it->it_flags & ITEM_HDR) {
if (proxy_storage_get(t, it, resp, PROXY_STORAGE_MG) != 0) {
pthread_mutex_lock(&t->stats.mutex);
t->stats.get_oom_extstore++;
pthread_mutex_unlock(&t->stats.mutex);
failed = true;
}
} else if ((it->it_flags & ITEM_CHUNKED) == 0) {
resp_add_iov(resp, ITEM_data(it), it->nbytes);
} else {
resp_add_chunked_iov(resp, it, it->nbytes);
}
#else
if ((it->it_flags & ITEM_CHUNKED) == 0) {
resp_add_iov(resp, ITEM_data(it), it->nbytes);
} else {
resp_add_chunked_iov(resp, it, it->nbytes);
}
#endif
}
// need to hold the ref at least because of the key above.
#ifdef EXTSTORE
if (!failed) {
if ((it->it_flags & ITEM_HDR) != 0 && of.value) {
// Only have extstore clean if header and returning value.
resp->item = NULL;
} else {
resp->item = it;
}
} else {
// Failed to set up extstore fetch.
if (of.locked) {
do_item_remove(it);
} else {
item_remove(it);
}
}
#else
resp->item = it;
#endif
} else {
failed = true;
}
if (of.locked) {
// Delayed bump so we could get fetched/last access time pre-update.
if (!of.no_update && it != NULL) {
do_item_bump(t, it, hv);
}
item_unlock(hv);
}
// we count this command as a normal one if we've gotten this far.
// TODO: for autovivify case, miss never happens. Is this okay?
if (!failed) {
pthread_mutex_lock(&t->stats.mutex);
if (ttl_set) {
t->stats.touch_cmds++;
t->stats.slab_stats[ITEM_clsid(it)].touch_hits++;
} else {
t->stats.lru_hits[it->slabs_clsid]++;
t->stats.get_cmds++;
}
pthread_mutex_unlock(&t->stats.mutex);
} else {
pthread_mutex_lock(&t->stats.mutex);
if (ttl_set) {
t->stats.touch_cmds++;
t->stats.touch_misses++;
} else {
t->stats.get_misses++;
t->stats.get_cmds++;
}
pthread_mutex_unlock(&t->stats.mutex);