-
Notifications
You must be signed in to change notification settings - Fork 33
/
cproxy_protocol_a2b.c
1784 lines (1509 loc) · 59 KB
/
cproxy_protocol_a2b.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 -*- */
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <assert.h>
#include <math.h>
#include "memcached.h"
#include "cproxy.h"
#include "work.h"
#include "log.h"
// Internal declarations.
//
static protocol_binary_request_noop req_noop = {
.bytes = {0}
};
#define CMD_TOKEN 0
#define KEY_TOKEN 1
#define MAX_TOKENS 9
// A2B means ascii-to-binary (or, ascii upstream and binary downstream).
//
struct A2BSpec {
char *line;
protocol_binary_command cmd;
protocol_binary_command cmdq;
int size; // Number of bytes in request header.
token_t tokens[MAX_TOKENS];
int ntokens;
bool noreply_allowed;
int num_optional; // Number of optional arguments in cmd.
bool broadcast; // True if cmd does scatter/gather.
};
// The a2b_specs are immutable after init.
//
// The arguments are carefully named with unique first characters.
//
struct A2BSpec a2b_specs[] = {
{ .line = "set <key> <flags> <exptime> <bytes> [noreply]",
.cmd = PROTOCOL_BINARY_CMD_SET,
.cmdq = PROTOCOL_BINARY_CMD_SETQ,
// The size should be...
// sizeof(protocol_binary_request_header) [24 bytes] +
// sizeof(protocol_binary_request_set.message.body) [8 bytes]
.size = sizeof(protocol_binary_request_header) + 8
},
{ .line = "add <key> <flags> <exptime> <bytes> [noreply]",
.cmd = PROTOCOL_BINARY_CMD_ADD,
.cmdq = PROTOCOL_BINARY_CMD_ADDQ,
// The size should be...
// sizeof(protocol_binary_request_header) [24 bytes] +
// sizeof(protocol_binary_request_add.message.body) [8 bytes]
.size = sizeof(protocol_binary_request_header) + 8
},
{ .line = "replace <key> <flags> <exptime> <bytes> [noreply]",
.cmd = PROTOCOL_BINARY_CMD_REPLACE,
.cmdq = PROTOCOL_BINARY_CMD_REPLACEQ,
// The size should be...
// sizeof(protocol_binary_request_header) [24 bytes] +
// sizeof(protocol_binary_request_replace.message.body) [8 bytes]
.size = sizeof(protocol_binary_request_header) + 8
},
{ .line = "append <key> <skip_flags> <skip_exptime> <bytes> [noreply]",
.cmd = PROTOCOL_BINARY_CMD_APPEND,
.cmdq = PROTOCOL_BINARY_CMD_APPENDQ,
.size = sizeof(protocol_binary_request_append)
},
{ .line = "prepend <key> <skip_flags> <skip_exptime> <bytes> [noreply]" ,
.cmd = PROTOCOL_BINARY_CMD_PREPEND,
.cmdq = PROTOCOL_BINARY_CMD_PREPENDQ,
.size = sizeof(protocol_binary_request_prepend)
},
{ .line = "cas <key> <flags> <exptime> <bytes> <cas> [noreply]",
.cmd = PROTOCOL_BINARY_CMD_SET,
.cmdq = PROTOCOL_BINARY_CMD_SETQ,
// The size should be...
// sizeof(protocol_binary_request_header) [24 bytes] +
// sizeof(protocol_binary_request_set.message.body) [8 bytes]
.size = sizeof(protocol_binary_request_header) + 8
},
{ .line = "delete <key> [noreply]",
.cmd = PROTOCOL_BINARY_CMD_DELETE,
.cmdq = PROTOCOL_BINARY_CMD_DELETEQ,
.size = sizeof(protocol_binary_request_delete)
},
{ .line = "incr <key> <value> [noreply]",
.cmd = PROTOCOL_BINARY_CMD_INCREMENT,
.cmdq = PROTOCOL_BINARY_CMD_INCREMENTQ,
// The size should be...
// sizeof(protocol_binary_request_header) [24 bytes] +
// sizeof(protocol_binary_request_incr.message.body) [20 bytes]
.size = sizeof(protocol_binary_request_header) + 20
},
{ .line = "decr <key> <value> [noreply]",
.cmd = PROTOCOL_BINARY_CMD_DECREMENT,
.cmdq = PROTOCOL_BINARY_CMD_DECREMENTQ,
// The size should be...
// sizeof(protocol_binary_request_header) [24 bytes] +
// sizeof(protocol_binary_request_decr.message.body) [20 bytes]
.size = sizeof(protocol_binary_request_header) + 20
},
{ .line = "flush_all [xpiration] [noreply]", // TODO: noreply tricky here.
.cmd = PROTOCOL_BINARY_CMD_FLUSH,
.cmdq = PROTOCOL_BINARY_CMD_FLUSHQ,
// The size should be...
// sizeof(protocol_binary_request_header) [24 bytes] +
// sizeof(protocol_binary_request_flush.message.body) [4 bytes]
.size = sizeof(protocol_binary_request_header) + 4,
.broadcast = true
},
{ .line = "get <key>*", // Multi-key GET/GETS
.cmd = PROTOCOL_BINARY_CMD_GETKQ,
.cmdq = -1,
.size = sizeof(protocol_binary_request_header)
},
{ .line = "get <key>", // Single-key GET/GETS.
.cmd = PROTOCOL_BINARY_CMD_GETK,
.cmdq = -1,
.size = sizeof(protocol_binary_request_header)
},
{ .line = "stats [args]*",
.cmd = PROTOCOL_BINARY_CMD_STAT,
.cmdq = -1,
.size = sizeof(protocol_binary_request_stats),
.broadcast = true
},
{ .line = "version",
.cmd = PROTOCOL_BINARY_CMD_VERSION,
.cmdq = -1,
.size = sizeof(protocol_binary_request_version)
},
{ .line = 0 } // NULL sentinel.
};
// These are immutable after init.
//
struct A2BSpec *a2b_spec_map[0x80] = {0}; // Lookup table by A2BSpec->cmd.
int a2b_size_max = 0; // Max header + extra frame bytes.
int a2b_fill_request(short cmd,
token_t *cmd_tokens,
int cmd_ntokens,
bool noreply,
protocol_binary_request_header *header,
uint8_t **out_key,
uint16_t *out_keylen,
uint8_t *out_extlen);
bool a2b_fill_request_token(struct A2BSpec *spec,
int cur_token,
token_t *cmd_tokens,
int cmd_ntokens,
protocol_binary_request_header *header,
uint8_t **out_key,
uint16_t *out_keylen,
uint8_t *out_extlen);
void a2b_process_downstream_response(conn *c);
int a2b_multiget_start(conn *c, char *cmd, int cmd_len);
int a2b_multiget_skey(conn *c, char *skey, int skey_len, int vbucket, int key_index);
int a2b_multiget_end(conn *c);
void a2b_set_opaque(conn *c, protocol_binary_request_header *header, bool noreply);
bool a2b_not_my_vbucket(conn *uc, conn *c,
protocol_binary_response_header *header);
void cproxy_init_a2b() {
memset(&req_noop, 0, sizeof(req_noop));
req_noop.message.header.request.magic = PROTOCOL_BINARY_REQ;
req_noop.message.header.request.opcode = PROTOCOL_BINARY_CMD_NOOP;
req_noop.message.header.request.datatype = PROTOCOL_BINARY_RAW_BYTES;
// Run through the a2b_specs to populate the a2b_spec_map.
//
int i = 0;
while (true) {
struct A2BSpec *spec = &a2b_specs[i];
if (spec->line == NULL) {
break;
}
spec->ntokens = scan_tokens(spec->line,
spec->tokens,
MAX_TOKENS, NULL);
assert(spec->ntokens > 1);
int noreply_index = spec->ntokens - 2;
if (spec->tokens[noreply_index].value &&
strcmp(spec->tokens[noreply_index].value,
"[noreply]") == 0) {
spec->noreply_allowed = true;
} else {
spec->noreply_allowed = false;
}
spec->num_optional = 0;
for (int j = 0; j < spec->ntokens; j++) {
if (spec->tokens[j].value &&
spec->tokens[j].value[0] == '[') {
spec->num_optional++;
}
}
if (a2b_size_max < spec->size) {
a2b_size_max = spec->size;
}
assert(spec->cmd < (sizeof(a2b_spec_map) /
sizeof(struct A2BSpec *)));
a2b_spec_map[spec->cmd] = spec;
i = i + 1;
}
}
int a2b_fill_request(short cmd,
token_t *cmd_tokens,
int cmd_ntokens,
bool noreply,
protocol_binary_request_header *header,
uint8_t **out_key,
uint16_t *out_keylen,
uint8_t *out_extlen) {
assert(header);
assert(cmd_tokens);
assert(cmd_ntokens > 1);
assert(cmd_tokens[CMD_TOKEN].value);
assert(cmd_tokens[CMD_TOKEN].length > 0);
assert(out_key);
assert(out_keylen);
assert(out_extlen);
struct A2BSpec *spec = a2b_spec_map[cmd];
if (spec != NULL) {
if (cmd_ntokens >= (spec->ntokens - spec->num_optional) &&
cmd_ntokens <= (spec->ntokens)) {
header->request.magic = PROTOCOL_BINARY_REQ;
if (noreply) {
assert(spec->cmd != (protocol_binary_command) -1);
header->request.opcode = spec->cmdq;
header->request.opaque = htonl(OPAQUE_IGNORE_REPLY);
if (settings.verbose > 2) {
moxi_log_write("a2b_fill_request OPAQUE_IGNORE_REPLY, cmdq: %x\n",
spec->cmdq);
}
} else {
header->request.opcode = spec->cmd;
}
// Start at 1 to skip the CMD_TOKEN.
//
for (int i = 1; i < cmd_ntokens - 1; i++) {
if (a2b_fill_request_token(spec, i,
cmd_tokens, cmd_ntokens,
header,
out_key,
out_keylen,
out_extlen) == false) {
return 0;
}
}
return spec->size; // Success.
}
} else {
if (settings.verbose > 2) {
moxi_log_write("a2b_fill_request unknown cmd: %x\n", cmd);
}
}
return 0;
}
bool a2b_fill_request_token(struct A2BSpec *spec,
int cur_token,
token_t *cmd_tokens,
int cmd_ntokens,
protocol_binary_request_header *header,
uint8_t **out_key,
uint16_t *out_keylen,
uint8_t *out_extlen) {
(void)cmd_ntokens;
assert(header);
assert(spec);
assert(spec->tokens);
assert(spec->ntokens > 1);
assert(spec->tokens[cur_token].value);
assert(cur_token > 0);
assert(cur_token < cmd_ntokens);
assert(cur_token < spec->ntokens);
uint64_t delta;
if (settings.verbose > 2) {
moxi_log_write("a2b_fill_request_token %s\n",
spec->tokens[cur_token].value);
}
char t = spec->tokens[cur_token].value[1];
switch (t) {
case 'k': // key
assert(out_key);
assert(out_keylen);
*out_key = (uint8_t *) cmd_tokens[cur_token].value;
*out_keylen = (uint16_t) cmd_tokens[cur_token].length;
header->request.keylen =
htons((uint16_t) cmd_tokens[cur_token].length);
break;
case 'v': // value (for incr/decr)
delta = 0;
if (safe_strtoull(cmd_tokens[cur_token].value, &delta)) {
assert(out_extlen);
header->request.extlen = *out_extlen = 20;
header->request.datatype = PROTOCOL_BINARY_RAW_BYTES;
protocol_binary_request_incr *req =
(protocol_binary_request_incr *) header;
req->message.body.delta = htonll(delta);
req->message.body.initial = 0;
req->message.body.expiration = 0xffffffff;
} else {
// TODO: Send back better error.
return false;
}
break;
case 'x': { // xpiration (for flush_all)
int32_t exptime_int = 0;
time_t exptime = 0;
if (safe_strtol(cmd_tokens[cur_token].value, &exptime_int)) {
/* Ubuntu 8.04 breaks when I pass exptime to safe_strtol */
exptime = exptime_int;
header->request.extlen = *out_extlen = 4;
header->request.datatype = PROTOCOL_BINARY_RAW_BYTES;
protocol_binary_request_flush *req =
(protocol_binary_request_flush *) header;
req->message.body.expiration = htonl(exptime);
}
break;
}
case 'a': // args (for stats)
assert(out_key);
assert(out_keylen);
*out_key = (uint8_t *) cmd_tokens[cur_token].value;
*out_keylen = (uint16_t) cmd_tokens[cur_token].length;
header->request.keylen =
htons((uint16_t) cmd_tokens[cur_token].length);
break;
// The noreply was handled in a2b_fill_request().
//
// case 'n': // noreply
//
// The above are handled by looking at the item struct.
//
// case 'f': // FALLTHRU, flags
// case 'e': // FALLTHRU, exptime
// case 'b': // FALLTHRU, bytes
// case 's': // FALLTHRU, skip_xxx
// case 'c': // FALLTHRU, cas
//
default:
break;
}
return true;
}
/* Called when we receive a binary response header from
* a downstream server, via try_read_command()/drive_machine().
*/
void cproxy_process_a2b_downstream(conn *c) {
assert(c != NULL);
assert(c->cmd >= 0);
assert(c->next == NULL);
assert(c->item == NULL);
assert(IS_BINARY(c->protocol));
assert(IS_PROXY(c->protocol));
// Snapshot rcurr, because the caller, try_read_command(), changes it.
//
c->cmd_start = c->rcurr;
protocol_binary_response_header *header =
(protocol_binary_response_header *) &c->binary_header;
header->response.status = (uint16_t) ntohs(header->response.status);
assert(header->response.magic == (uint8_t) PROTOCOL_BINARY_RES);
assert(header->response.opcode == c->cmd);
process_bin_noreply(c); // Map quiet c->cmd values into non-quiet.
int extlen = header->response.extlen;
int keylen = header->response.keylen;
uint32_t bodylen = header->response.bodylen;
assert(bodylen >= (uint32_t) keylen + extlen);
// Our approach is to read everything we can before
// getting into big switch/case statements for the
// actual processing.
//
// If status is non-zero (an err code), then bodylen should be small.
// If status is 0, then bodylen might be for a huge item during
// a GET family of response.
//
// If bodylen > extlen + keylen, then we should nread
// the ext+key and set ourselves up for a later item nread.
//
// We overload the meaning of the conn substates...
// - bin_reading_get_key means do nread for ext and key data.
// - bin_read_set_value means do nread for item data.
//
if (settings.verbose > 2) {
moxi_log_write("<%d cproxy_process_a2b_downstream %x %d %d %u\n",
c->sfd, c->cmd, extlen, keylen, bodylen);
}
if (keylen > 0 || extlen > 0) {
// One reason we reach here is during a
// GET/GETQ/GETK/GETKQ hit response, because extlen
// will be > 0 for the flags.
//
// Also, we reach here during a GETK miss response, since
// keylen will be > 0. Oddly, a GETK miss response will have
// a non-zero status of PROTOCOL_BINARY_RESPONSE_KEY_ENOENT,
// but won't have any extra error message string.
//
// Also, we reach here during a STAT response, with
// keylen > 0, extlen == 0, and bodylen == keylen.
//
assert(c->cmd == PROTOCOL_BINARY_CMD_GET ||
c->cmd == PROTOCOL_BINARY_CMD_GETK ||
c->cmd == PROTOCOL_BINARY_CMD_STAT);
bin_read_key(c, bin_reading_get_key, extlen);
} else {
assert(keylen == 0 && extlen == 0);
if (bodylen > 0) {
// We reach here on error response, version response,
// or incr/decr responses, which all have only (relatively
// small) body bytes, and with no ext bytes and no key bytes.
//
// For example, error responses will have 0 keylen,
// 0 extlen, with an error message string for the body.
//
// We'll just reuse the key-reading code path, rather
// than allocating an item.
//
assert(header->response.status != 0 ||
c->cmd == PROTOCOL_BINARY_CMD_VERSION ||
c->cmd == PROTOCOL_BINARY_CMD_INCREMENT ||
c->cmd == PROTOCOL_BINARY_CMD_DECREMENT);
bin_read_key(c, bin_reading_get_key, bodylen);
} else {
assert(keylen == 0 && extlen == 0 && bodylen == 0);
// We have the entire response in the header,
// such as due to a general success response,
// including a no-op response.
//
a2b_process_downstream_response(c);
}
}
}
/* We reach here after nread'ing a ext+key or item.
*/
void cproxy_process_a2b_downstream_nread(conn *c) {
assert(c != NULL);
assert(c->cmd >= 0);
assert(c->next == NULL);
assert(c->cmd_start != NULL);
assert(IS_BINARY(c->protocol));
assert(IS_PROXY(c->protocol));
downstream *d = c->extra;
assert(d);
protocol_binary_response_header *header =
(protocol_binary_response_header *) &c->binary_header;
int extlen = header->response.extlen;
int keylen = header->response.keylen;
uint32_t bodylen = header->response.bodylen;
if (settings.verbose > 2) {
moxi_log_write("<%d cproxy_process_a2b_downstream_nread %d %d, cmd %x %d %d\n",
c->sfd, c->ileft, c->isize, c->cmd, c->substate,
header->response.status);
}
if (c->substate == bin_reading_get_key &&
header->response.status == 0 &&
(c->cmd == PROTOCOL_BINARY_CMD_GET ||
c->cmd == PROTOCOL_BINARY_CMD_GETK ||
c->cmd == PROTOCOL_BINARY_CMD_STAT)) {
if (settings.verbose > 2) {
moxi_log_write("<%d cproxy_process_a2b_downstream_nread %d %d %x get/getk/stat\n",
c->sfd, c->ileft, c->isize, c->cmd);
}
assert(c->item == NULL);
// Alloc an item and continue with an item nread.
// We item_alloc() even if vlen is 0, so that later
// code can assume an item exists.
//
char *key = binary_get_key(c);
int vlen = bodylen - (keylen + extlen);
int flags = 0;
assert(key);
assert(keylen > 0);
assert(vlen >= 0);
if (c->cmd == PROTOCOL_BINARY_CMD_GET ||
c->cmd == PROTOCOL_BINARY_CMD_GETK) {
protocol_binary_response_get *response_get =
(protocol_binary_response_get *) binary_get_request(c);
assert(extlen == sizeof(response_get->message.body));
flags = ntohl(response_get->message.body.flags);
}
item *it = item_alloc(key, keylen, flags, 0, vlen + 2);
if (it != NULL) {
c->item = it;
c->ritem = ITEM_data(it);
c->rlbytes = vlen;
c->substate = bin_read_set_value;
uint64_t cas = CPROXY_NOT_CAS;
conn *uc = d->upstream_conn;
if (uc != NULL &&
uc->cmd_start != NULL &&
strncmp(uc->cmd_start, "gets ", 5) == 0) {
cas = header->response.cas;
}
ITEM_set_cas(it, cas);
conn_set_state(c, conn_nread);
} else {
d->ptd->stats.stats.err_oom++;
cproxy_close_conn(c);
}
} else {
a2b_process_downstream_response(c);
}
}
static void a2b_out_error(conn *uc, uint16_t status) {
switch (status) {
case PROTOCOL_BINARY_RESPONSE_SUCCESS:
out_string(uc, "OK");
break;
case PROTOCOL_BINARY_RESPONSE_KEY_ENOENT:
out_string(uc, "NOT_FOUND");
break;
case PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS:
out_string(uc, "EXISTS");
break;
case PROTOCOL_BINARY_RESPONSE_E2BIG:
out_string(uc, "SERVER_ERROR a2b e2big");
break;
case PROTOCOL_BINARY_RESPONSE_EINVAL:
out_string(uc, "SERVER_ERROR a2b einval");
break;
case PROTOCOL_BINARY_RESPONSE_NOT_STORED:
out_string(uc, "NOT_STORED");
break;
case PROTOCOL_BINARY_RESPONSE_DELTA_BADVAL:
out_string(uc, "SERVER_ERROR a2b delta_badval");
break;
case PROTOCOL_BINARY_RESPONSE_NOT_MY_VBUCKET:
out_string(uc, "SERVER_ERROR a2b not_my_vbucket");
break;
case PROTOCOL_BINARY_RESPONSE_AUTH_ERROR:
out_string(uc, "SERVER_ERROR a2b auth_error");
break;
case PROTOCOL_BINARY_RESPONSE_AUTH_CONTINUE:
out_string(uc, "SERVER_ERROR a2b auth_continue");
break;
case PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND:
out_string(uc, "SERVER_ERROR a2b unknown");
break;
case PROTOCOL_BINARY_RESPONSE_ENOMEM:
out_string(uc, "SERVER_ERROR a2b out of memory");
break;
case PROTOCOL_BINARY_RESPONSE_NOT_SUPPORTED:
out_string(uc, "SERVER_ERROR a2b not supported");
break;
case PROTOCOL_BINARY_RESPONSE_EINTERNAL:
out_string(uc, "SERVER_ERROR a2b einternal");
break;
case PROTOCOL_BINARY_RESPONSE_EBUSY:
out_string(uc, "SERVER_ERROR a2b ebusy");
break;
default:
out_string(uc, "SERVER_ERROR a2b error");
break;
}
}
/* Invoked when we have read a complete downstream binary response,
* including header, ext, key, and item data, as appropriate.
*/
void a2b_process_downstream_response(conn *c) {
assert(c != NULL);
assert(c->cmd >= 0);
assert(c->next == NULL);
assert(c->cmd_start != NULL);
assert(IS_BINARY(c->protocol));
assert(IS_PROXY(c->protocol));
protocol_binary_response_header *header =
(protocol_binary_response_header *) &c->binary_header;
uint32_t extlen = header->response.extlen;
uint32_t keylen = header->response.keylen;
uint32_t bodylen = header->response.bodylen;
uint16_t status = header->response.status;
if (settings.verbose > 2) {
moxi_log_write("<%d cproxy_process_a2b_downstream_response, cmd: %x, item: %d, status: %d\n",
c->sfd, c->cmd, (c->item != NULL), status);
}
// We reach here when we have the entire response,
// including header, ext, key, and possibly item data.
// Now we can get into big switch/case processing.
//
downstream *d = c->extra;
assert(d != NULL);
assert(d->ptd != NULL);
assert(d->ptd->proxy != NULL);
item *it = c->item;
// Clear c->item because we either move it to the upstream or
// item_remove() it on error.
//
c->item = NULL;
if (cproxy_binary_ignore_reply(c, header, it)) {
return;
}
conn *uc = d->upstream_conn;
// Handle not-my-vbucket error response.
//
if (status == PROTOCOL_BINARY_RESPONSE_NOT_MY_VBUCKET) {
assert(it == NULL);
if (a2b_not_my_vbucket(uc, c, header)) {
return;
}
}
switch (c->cmd) {
case PROTOCOL_BINARY_CMD_GETK:
if (settings.verbose > 2) {
moxi_log_write("%d: cproxy_process_a2b_downstream_response GETK "
"noreply: %d\n", c->sfd, c->noreply);
}
if (c->noreply == false) {
// Single-key GET/GETS.
//
if (status == 0) {
assert(it != NULL);
assert(it->nbytes >= 2);
assert(keylen > 0);
assert(extlen > 0);
if (bodylen >= keylen + extlen) {
*(ITEM_data(it) + it->nbytes - 2) = '\r';
*(ITEM_data(it) + it->nbytes - 1) = '\n';
multiget_ascii_downstream_response(d, it);
} else {
assert(false); // TODO.
}
item_remove(it);
}
conn_set_state(c, conn_pause);
cproxy_update_event_write(d, uc);
return;
}
// Multi-key GET/GETS.
//
// We should keep processing for a non-quiet
// terminating response (NO-OP).
//
conn_set_state(c, conn_new_cmd);
if (status != 0) {
assert(it == NULL);
if (status == PROTOCOL_BINARY_RESPONSE_KEY_ENOENT) {
return; // Swallow miss response.
}
// TODO: Handle error case. Should we pause the conn
// or keep looking for more responses?
//
assert(false);
return;
}
assert(status == 0);
assert(it != NULL);
assert(it->nbytes >= 2);
assert(keylen > 0);
assert(extlen > 0);
if (bodylen >= keylen + extlen) {
*(ITEM_data(it) + it->nbytes - 2) = '\r';
*(ITEM_data(it) + it->nbytes - 1) = '\n';
multiget_ascii_downstream_response(d, it);
} else {
assert(false); // TODO.
}
item_remove(it);
break;
case PROTOCOL_BINARY_CMD_FLUSH:
conn_set_state(c, conn_pause);
// TODO: Handle flush_all's expiration parameter against
// the front_cache.
//
// TODO: We flush the front_cache too often, inefficiently
// on every downstream FLUSH response, rather than on
// just the last FLUSH response.
//
if (uc != NULL) {
mcache_flush_all(&d->ptd->proxy->front_cache, 0);
}
break;
case PROTOCOL_BINARY_CMD_NOOP:
conn_set_state(c, conn_pause);
break;
case PROTOCOL_BINARY_CMD_SET: /* FALLTHROUGH */
case PROTOCOL_BINARY_CMD_ADD:
case PROTOCOL_BINARY_CMD_REPLACE:
case PROTOCOL_BINARY_CMD_APPEND:
case PROTOCOL_BINARY_CMD_PREPEND:
conn_set_state(c, conn_pause);
if (uc != NULL) {
assert(uc->next == NULL);
switch (status) {
case PROTOCOL_BINARY_RESPONSE_SUCCESS:
out_string(uc, "STORED");
break;
case PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS:
if (c->cmd == PROTOCOL_BINARY_CMD_ADD) {
out_string(uc, "NOT_STORED");
} else {
out_string(uc, "EXISTS");
}
break;
case PROTOCOL_BINARY_RESPONSE_KEY_ENOENT:
if (c->cmd == PROTOCOL_BINARY_CMD_REPLACE) {
out_string(uc, "NOT_STORED");
} else {
out_string(uc, "NOT_FOUND");
}
break;
default:
a2b_out_error(uc, status);
break;
}
cproxy_del_front_cache_key_ascii(d, uc->cmd_start);
cproxy_update_event_write(d, uc);
}
break;
case PROTOCOL_BINARY_CMD_DELETE:
conn_set_state(c, conn_pause);
if (uc != NULL) {
assert(uc->next == NULL);
switch (status) {
case 0:
out_string(uc, "DELETED");
break;
case PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS:
out_string(uc, "EXISTS");
break;
case PROTOCOL_BINARY_RESPONSE_KEY_ENOENT:
case PROTOCOL_BINARY_RESPONSE_NOT_STORED:
case PROTOCOL_BINARY_RESPONSE_ENOMEM: // TODO.
default:
out_string(uc, "NOT_FOUND");
break;
}
cproxy_del_front_cache_key_ascii(d, uc->cmd_start);
cproxy_update_event_write(d, uc);
}
break;
case PROTOCOL_BINARY_CMD_INCREMENT: /* FALLTHROUGH */
case PROTOCOL_BINARY_CMD_DECREMENT:
conn_set_state(c, conn_pause);
if (uc != NULL) {
assert(uc->next == NULL);
// TODO: Any weird alignment/padding issues on different
// platforms in this cast to worry about here?
//
protocol_binary_response_incr *response_incr =
(protocol_binary_response_incr *) c->cmd_start;
switch (status) {
case 0: {
char *s = add_conn_suffix(uc);
if (s != NULL) {
uint64_t v = mc_swap64(response_incr->message.body.value);
sprintf(s, "%llu", (unsigned long long) v);
out_string(uc, s);
} else {
d->ptd->stats.stats.err_oom++;
cproxy_close_conn(uc);
}
break;
}
case PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS: // Due to CAS.
out_string(uc, "EXISTS");
break;
case PROTOCOL_BINARY_RESPONSE_KEY_ENOENT:
out_string(uc, "NOT_FOUND");
break;
case PROTOCOL_BINARY_RESPONSE_NOT_STORED:
out_string(uc, "NOT_STORED");
break;
case PROTOCOL_BINARY_RESPONSE_ENOMEM: // TODO.
default:
out_string(uc, "SERVER_ERROR a2b arith error");
break;
}
cproxy_del_front_cache_key_ascii(d, uc->cmd_start);
cproxy_update_event_write(d, uc);
}
break;
case PROTOCOL_BINARY_CMD_VERSION:
conn_set_state(c, conn_pause);
if (uc != NULL) {
assert(uc->next == NULL);
if (header->response.status == 0 &&
header->response.keylen == 0 &&
header->response.extlen == 0 &&
header->response.bodylen > 0) {
char *s = add_conn_suffix(uc);
if (s != NULL) {
// TODO: Assuming bodylen is not that long.
memcpy(s, "VERSION ", 8);
memcpy(s + 8,
c->cmd_start + sizeof(protocol_binary_response_version),
header->response.bodylen);
s[8 + header->response.bodylen] = '\0';
out_string(uc, s);
} else {
d->ptd->stats.stats.err_oom++;
cproxy_close_conn(uc);
}
} else {
out_string(uc, "SERVER_ERROR");
}
cproxy_update_event_write(d, uc);
}
break;
case PROTOCOL_BINARY_CMD_STAT:
assert(c->noreply == false);
if (keylen > 0) {
assert(it != NULL); // Holds the stat value.
assert(it->nbytes > 2);
assert(bodylen > keylen);
assert(d->merger != NULL);
if (uc != NULL) {
assert(uc->next == NULL);
// TODO: Handle ITEM and PREFIX.
//
protocol_stats_merge_name_val(d->merger,
"STAT", 4,
ITEM_key(it), it->nkey,
ITEM_data(it), it->nbytes - 2);
}
item_remove(it);
conn_set_state(c, conn_new_cmd);
} else {
// Handle the stats terminator.
//
assert(it == NULL);
assert(bodylen == 0);
conn_set_state(c, conn_pause);
}
break;
case PROTOCOL_BINARY_CMD_QUIT:
default:
assert(false); // TODO: Handled unexpected responses.
break;
}
}
/* Do the actual work of forwarding the command from an
* upstream ascii conn to its assigned binary downstream.
*/
bool cproxy_forward_a2b_downstream(downstream *d) {
assert(d != NULL);
conn *uc = d->upstream_conn;
assert(uc != NULL);
assert(uc->state == conn_pause);
assert(uc->cmd_start != NULL);
assert(uc->thread != NULL);
assert(uc->thread->base != NULL);
assert(IS_ASCII(uc->protocol));
assert(IS_PROXY(uc->protocol));
int server_index = -1;
if (cproxy_is_broadcast_cmd(uc->cmd_curr) == false) {
char *key = NULL;
int key_len = 0;
if (ascii_scan_key(uc->cmd_start, &key, &key_len) &&
key != NULL &&
key_len > 0) {
server_index = cproxy_server_index(d, key, key_len, NULL);
}
}
int nc = cproxy_connect_downstream(d, uc->thread, server_index);
if (nc == -1) {
return true;
}
if (nc > 0) {
assert(d->downstream_conns != NULL);