forked from RedisLabs/memtier_benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.cpp
1251 lines (1056 loc) · 39.8 KB
/
protocol.cpp
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
/*
* Copyright (C) 2011-2017 Redis Labs Ltd.
*
* This file is part of memtier_benchmark.
*
* memtier_benchmark is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2.
*
* memtier_benchmark is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with memtier_benchmark. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef HAVE_ASSERT_H
#include <assert.h>
#endif
#include "protocol.h"
#include "memtier_benchmark.h"
#include "libmemcached_protocol/binary.h"
/////////////////////////////////////////////////////////////////////////
abstract_protocol::abstract_protocol() :
m_read_buf(NULL), m_write_buf(NULL), m_keep_value(false)
{
}
abstract_protocol::~abstract_protocol()
{
}
void abstract_protocol::set_buffers(struct evbuffer* read_buf, struct evbuffer* write_buf)
{
m_read_buf = read_buf;
m_write_buf = write_buf;
}
void abstract_protocol::set_keep_value(bool flag)
{
m_keep_value = flag;
}
/////////////////////////////////////////////////////////////////////////
protocol_response::protocol_response()
: m_status(NULL), m_mbulk_value(NULL), m_value(NULL), m_value_len(0), m_hits(0), m_error(false)
{
}
protocol_response::~protocol_response()
{
clear();
}
void protocol_response::set_error(bool error)
{
m_error = error;
}
bool protocol_response::is_error(void)
{
return m_error;
}
void protocol_response::set_status(const char* status)
{
if (m_status != NULL)
free((void *)m_status);
m_status = status;
}
const char* protocol_response::get_status(void)
{
return m_status;
}
void protocol_response::set_value(const char* value, unsigned int value_len)
{
if (m_value != NULL)
free((void *)m_value);
m_value = value;
m_value_len = value_len;
}
const char* protocol_response::get_value(unsigned int* value_len)
{
assert(value_len != NULL);
*value_len = m_value_len;
return m_value;
}
void protocol_response::set_total_len(unsigned int total_len)
{
m_total_len = total_len;
}
unsigned int protocol_response::get_total_len(void)
{
return m_total_len;
}
void protocol_response::incr_hits(void)
{
m_hits++;
}
unsigned int protocol_response::get_hits(void)
{
return m_hits;
}
void protocol_response::clear(void)
{
if (m_status != NULL) {
free((void *)m_status);
m_status = NULL;
}
if (m_value != NULL) {
free((void *)m_value);
m_value = NULL;
}
if (m_mbulk_value != NULL) {
delete m_mbulk_value;
m_mbulk_value = NULL;
}
m_value_len = 0;
m_total_len = 0;
m_hits = 0;
m_error = 0;
}
void protocol_response::set_mbulk_value(mbulk_size_el* element) {
m_mbulk_value = element;
}
mbulk_size_el* protocol_response::get_mbulk_value() {
return m_mbulk_value;
}
/////////////////////////////////////////////////////////////////////////
class redis_protocol : public abstract_protocol {
protected:
enum response_state { rs_initial, rs_read_bulk, rs_read_line, rs_end_bulk };
response_state m_response_state;
long m_bulk_len;
size_t m_response_len;
unsigned int m_total_bulks_count;
mbulk_size_el* m_current_mbulk;
public:
redis_protocol() : m_response_state(rs_initial), m_bulk_len(0), m_response_len(0), m_total_bulks_count(0), m_current_mbulk(NULL) { }
virtual redis_protocol* clone(void) { return new redis_protocol(); }
virtual int select_db(int db);
virtual int authenticate(const char *credentials);
virtual int write_command_cluster_slots();
virtual int write_command_set(const char *key, int key_len, const char *value, int value_len, int expiry, unsigned int offset);
virtual int write_command_get(const char *key, int key_len, unsigned int offset);
virtual int write_command_multi_get(const keylist *keylist);
virtual int write_command_wait(unsigned int num_slaves, unsigned int timeout);
virtual int parse_response(void);
// handle arbitrary command
virtual bool format_arbitrary_command(arbitrary_command &cmd);
int write_arbitrary_command(const command_arg *arg);
int write_arbitrary_command(const char *val, int val_len);
};
int redis_protocol::select_db(int db)
{
int size = 0;
char db_str[20];
snprintf(db_str, sizeof(db_str)-1, "%d", db);
size = evbuffer_add_printf(m_write_buf,
"*2\r\n"
"$6\r\n"
"SELECT\r\n"
"$%u\r\n"
"%s\r\n",
(unsigned int)strlen(db_str), db_str);
return size;
}
int redis_protocol::authenticate(const char *credentials)
{
int size = 0;
assert(credentials != NULL);
/* Credentials may be one of:
* <PASSWORD> For Redis <6.0 simple AUTH commands.
* <USER>:<PASSWORD> For Redis 6.0+ AUTH with both username and password.
*
* A :<PASSWORD> will be handled as a special case of quoting a password that
* contains a colon.
*/
const char *user = NULL;
const char *password;
if (credentials[0] == ':') {
password = credentials + 1;
} else {
password = strchr(credentials, ':');
if (!password) {
password = credentials;
} else {
user = credentials;
password++;
}
}
if (!user) {
size = evbuffer_add_printf(m_write_buf,
"*2\r\n"
"$4\r\n"
"AUTH\r\n"
"$%zu\r\n"
"%s\r\n",
strlen(password), password);
} else {
size_t user_len = password - user - 1;
size = evbuffer_add_printf(m_write_buf,
"*3\r\n"
"$4\r\n"
"AUTH\r\n"
"$%zu\r\n"
"%.*s\r\n"
"$%zu\r\n"
"%s\r\n",
user_len,
(int) user_len,
user,
strlen(password),
password);
}
return size;
}
int redis_protocol::write_command_cluster_slots()
{
int size = 0;
size = evbuffer_add(m_write_buf,
"*2\r\n"
"$7\r\n"
"CLUSTER\r\n"
"$5\r\n"
"SLOTS\r\n",
28);
return size;
}
int redis_protocol::write_command_set(const char *key, int key_len, const char *value, int value_len, int expiry, unsigned int offset)
{
assert(key != NULL);
assert(key_len > 0);
assert(value != NULL);
assert(value_len > 0);
int size = 0;
if (!expiry && !offset) {
size = evbuffer_add_printf(m_write_buf,
"*3\r\n"
"$3\r\n"
"SET\r\n"
"$%u\r\n", key_len);
evbuffer_add(m_write_buf, key, key_len);
size += key_len;
size += evbuffer_add_printf(m_write_buf,
"\r\n"
"$%u\r\n", value_len);
} else if(offset) {
char offset_str[30];
snprintf(offset_str, sizeof(offset_str)-1, "%u", offset);
size = evbuffer_add_printf(m_write_buf,
"*4\r\n"
"$8\r\n"
"SETRANGE\r\n"
"$%u\r\n", key_len);
evbuffer_add(m_write_buf, key, key_len);
size += key_len;
size += evbuffer_add_printf(m_write_buf,
"\r\n"
"$%u\r\n"
"%s\r\n"
"$%u\r\n", (unsigned int) strlen(offset_str), offset_str, value_len);
} else {
char expiry_str[30];
snprintf(expiry_str, sizeof(expiry_str)-1, "%u", expiry);
size = evbuffer_add_printf(m_write_buf,
"*4\r\n"
"$5\r\n"
"SETEX\r\n"
"$%u\r\n", key_len);
evbuffer_add(m_write_buf, key, key_len);
size += key_len;
size += evbuffer_add_printf(m_write_buf,
"\r\n"
"$%u\r\n"
"%s\r\n"
"$%u\r\n", (unsigned int) strlen(expiry_str), expiry_str, value_len);
}
evbuffer_add(m_write_buf, value, value_len);
evbuffer_add(m_write_buf, "\r\n", 2);
size += value_len + 2;
return size;
}
int redis_protocol::write_command_multi_get(const keylist *keylist)
{
fprintf(stderr, "error: multi get not implemented for redis yet!\n");
assert(0);
}
int redis_protocol::write_command_get(const char *key, int key_len, unsigned int offset)
{
assert(key != NULL);
assert(key_len > 0);
int size = 0;
if (!offset) {
size = evbuffer_add_printf(m_write_buf,
"*2\r\n"
"$3\r\n"
"GET\r\n"
"$%u\r\n", key_len);
evbuffer_add(m_write_buf, key, key_len);
evbuffer_add(m_write_buf, "\r\n", 2);
size += key_len + 2;
} else {
char offset_str[30];
snprintf(offset_str, sizeof(offset_str)-1, "%u", offset);
size = evbuffer_add_printf(m_write_buf,
"*4\r\n"
"$8\r\n"
"GETRANGE\r\n"
"$%u\r\n", key_len);
evbuffer_add(m_write_buf, key, key_len);
size += key_len;
size += evbuffer_add_printf(m_write_buf,
"\r\n"
"$%u\r\n"
"%s\r\n"
"$2\r\n"
"-1\r\n", (unsigned int) strlen(offset_str), offset_str);
}
return size;
}
/*
* Utility function to get the number of digits in a number
*/
static int get_number_length(unsigned int num)
{
if (num < 10) return 1;
if (num < 100) return 2;
if (num < 1000) return 3;
if (num < 10000) return 4;
if (num < 100000) return 5;
if (num < 1000000) return 6;
if (num < 10000000) return 7;
if (num < 100000000) return 8;
if (num < 1000000000) return 9;
return 10;
}
int redis_protocol::write_command_wait(unsigned int num_slaves, unsigned int timeout)
{
int size = 0;
size = evbuffer_add_printf(m_write_buf,
"*3\r\n"
"$4\r\n"
"WAIT\r\n"
"$%u\r\n"
"%u\r\n"
"$%u\r\n"
"%u\r\n",
get_number_length(num_slaves), num_slaves,
get_number_length(timeout), timeout);
return size;
}
int redis_protocol::parse_response(void)
{
char *line;
size_t res_len;
while (true) {
switch (m_response_state) {
case rs_initial:
// clear last response
m_last_response.clear();
m_response_len = 0;
m_total_bulks_count = 0;
m_response_state = rs_read_line;
break;
case rs_read_line:
line = evbuffer_readln(m_read_buf, &res_len, EVBUFFER_EOL_CRLF_STRICT);
// maybe we didn't get it yet?
if (line == NULL) {
return 0;
}
// count CRLF
m_response_len += res_len + 2;
if (line[0] == '*') {
int count = strtol(line + 1, NULL, 10);
// in case of nested mbulk, the mbulk is one of the total bulks
if (m_total_bulks_count > 0) {
m_total_bulks_count--;
}
// from bulks counter perspective every count < 0 is equal to 0, because it's not followed by bulks.
if (count < 0) {
count = 0;
}
if (m_keep_value) {
mbulk_size_el* new_mbulk_size = new mbulk_size_el();
new_mbulk_size->bulks_count = count;
new_mbulk_size->upper_level = m_current_mbulk;
// update first mbulk as the response mbulk, or insert it to current mbulk
if (m_last_response.get_mbulk_value() == NULL) {
m_last_response.set_mbulk_value(new_mbulk_size);
} else {
m_current_mbulk->add_new_element(new_mbulk_size);
}
// update current mbulk
m_current_mbulk = new_mbulk_size->get_next_mbulk();
}
m_last_response.set_status(line);
m_total_bulks_count += count;
if (m_total_bulks_count == 0) {
m_last_response.set_total_len(m_response_len);
m_response_state = rs_initial;
return 1;
}
} else if (line[0] == '$') {
// if it's single bulk (not part of mbulk), we count it here
if (m_total_bulks_count == 0) {
m_total_bulks_count++;
}
m_bulk_len = strtol(line + 1, NULL, 10);
m_last_response.set_status(line);
/*
* only on negative bulk, the data ends right after the first CRLF ($-1\r\n), so
* we skip on rs_read_bulk and jump into rs_end_bulk
*/
if (m_bulk_len < 0) {
m_response_state = rs_end_bulk;
} else {
m_response_state = rs_read_bulk;
}
} else if (line[0] == '+' || line[0] == '-' || line[0] == ':') {
// if it's single bulk (not part of mbulk), we count it here
if (m_total_bulks_count == 0) {
m_total_bulks_count++;
}
// if we are not inside mbulk, the status will be kept in m_status anyway
if (m_keep_value && m_current_mbulk) {
char *bulk_value = strdup(line);
assert(bulk_value != NULL);
bulk_el* new_bulk = new bulk_el();
new_bulk->value = bulk_value;
new_bulk->value_len = strlen(bulk_value);
// insert it to current mbulk
m_current_mbulk->add_new_element(new_bulk);
m_current_mbulk = m_current_mbulk->get_next_mbulk();
}
if (line[0] == '-')
m_last_response.set_error(true);
m_last_response.set_status(line);
m_total_bulks_count--;
if (m_total_bulks_count == 0) {
m_last_response.set_total_len(m_response_len);
m_response_state = rs_initial;
return 1;
}
} else {
benchmark_debug_log("unsupported response: '%s'.\n", line);
free(line);
return -1;
}
break;
case rs_read_bulk:
if (evbuffer_get_length(m_read_buf) >= (unsigned long)(m_bulk_len + 2)) {
m_response_len += m_bulk_len + 2;
/*
* KNOWN ISSUE:
* in case of key with zero size (SET X "") that will return $0,
* we are not counting it as "hit", and the report will be wrong.
* currently this is a limitation because GETRANGE returns $0 for
* such key as well as non existing key or existing key without data
* in the requested range
*/
if (m_bulk_len > 0) {
m_last_response.incr_hits();
}
m_response_state = rs_end_bulk;
} else {
return 0;
}
break;
case rs_end_bulk:
if (m_keep_value) {
/*
* keep bulk value - in case we need to save bulk value it depends
* if it's inside a mbulk or not.
* in case of receiving just bulk as a response, we save it directly to the m_last_response,
* otherwise we insert it to the current mbulk element
*/
char *bulk_value = NULL;
int ret;
if (m_bulk_len > 0) {
bulk_value = (char *) malloc(m_bulk_len);
assert(bulk_value != NULL);
ret = evbuffer_remove(m_read_buf, bulk_value, m_bulk_len);
assert(ret != -1);
}
// drain last CRLF, zero bulk also includes it ($0\r\n\r\n)
if (m_bulk_len >= 0) {
ret = evbuffer_drain(m_read_buf, 2);
assert(ret != -1);
}
// in case we are inside mbulk
if (m_current_mbulk) {
bulk_el* new_bulk = new bulk_el();
new_bulk->value = bulk_value;
// negative bulk len counted as empty bulk
new_bulk->value_len = m_bulk_len > 0 ? m_bulk_len : 0;
// insert it to current mbulk
m_current_mbulk->add_new_element(new_bulk);
m_current_mbulk = m_current_mbulk->get_next_mbulk();
} else {
// negative bulk len counted as empty bulk
m_last_response.set_value(bulk_value, m_bulk_len > 0 ? m_bulk_len : 0);
}
} else {
// just drain the buffer, include the CRLF
if (m_bulk_len >= 0) {
int ret = evbuffer_drain(m_read_buf, m_bulk_len + 2);
assert(ret != -1);
}
}
m_total_bulks_count--;
if (m_total_bulks_count == 0) {
m_last_response.set_total_len(m_response_len);
m_response_state = rs_initial;
return 1;
} else {
m_response_state = rs_read_line;
}
break;
default:
return -1;
}
}
return -1;
}
int redis_protocol::write_arbitrary_command(const command_arg *arg) {
evbuffer_add(m_write_buf, arg->data.c_str(), arg->data.length());
return arg->data.length();
}
int redis_protocol::write_arbitrary_command(const char *rand_val, int rand_val_len) {
int size = 0;
size = evbuffer_add_printf(m_write_buf, "$%d\r\n", rand_val_len);
evbuffer_add(m_write_buf, rand_val, rand_val_len);
size += rand_val_len;
evbuffer_add(m_write_buf, "\r\n", 2);
size += 2;
return size;
}
bool redis_protocol::format_arbitrary_command(arbitrary_command &cmd) {
for (unsigned int i = 0; i < cmd.command_args.size(); i++) {
command_arg* current_arg = &cmd.command_args[i];
current_arg->type = const_type;
// check arg type
if (current_arg->data.find(KEY_PLACEHOLDER) != std::string::npos) {
if (current_arg->data.length() != strlen(KEY_PLACEHOLDER)) {
benchmark_error_log("error: key placeholder can't combined with other data\n");
return false;
}
current_arg->type = key_type;
} else if (current_arg->data.find(DATA_PLACEHOLDER) != std::string::npos) {
if (current_arg->data.length() != strlen(DATA_PLACEHOLDER)) {
benchmark_error_log("error: data placeholder can't combined with other data\n");
return false;
}
current_arg->type = data_type;
}
// we expect that first arg is the COMMAND name
assert(i != 0 || (i == 0 && current_arg->type == const_type && "first arg is not command name?"));
if (current_arg->type == const_type) {
char buffer[20];
int buffer_len;
// if it's first arg we add also the mbulk size
if (i == 0) {
buffer_len = snprintf(buffer, 20, "*%zd\r\n$%zd\r\n", cmd.command_args.size(), current_arg->data.length());
} else {
buffer_len = snprintf(buffer, 20, "$%zd\r\n", current_arg->data.length());
}
current_arg->data.insert(0, buffer, buffer_len);
current_arg->data += "\r\n";
}
}
return true;
}
/////////////////////////////////////////////////////////////////////////
class memcache_text_protocol : public abstract_protocol {
protected:
enum response_state { rs_initial, rs_read_section, rs_read_value, rs_read_end };
response_state m_response_state;
unsigned int m_value_len;
size_t m_response_len;
public:
memcache_text_protocol() : m_response_state(rs_initial), m_value_len(0), m_response_len(0) { }
virtual memcache_text_protocol* clone(void) { return new memcache_text_protocol(); }
virtual int select_db(int db);
virtual int authenticate(const char *credentials);
virtual int write_command_cluster_slots();
virtual int write_command_set(const char *key, int key_len, const char *value, int value_len, int expiry, unsigned int offset);
virtual int write_command_get(const char *key, int key_len, unsigned int offset);
virtual int write_command_multi_get(const keylist *keylist);
virtual int write_command_wait(unsigned int num_slaves, unsigned int timeout);
virtual int parse_response(void);
// handle arbitrary command
virtual bool format_arbitrary_command(arbitrary_command& cmd);
virtual int write_arbitrary_command(const command_arg *arg);
virtual int write_arbitrary_command(const char *val, int val_len);
};
int memcache_text_protocol::select_db(int db)
{
assert(0);
}
int memcache_text_protocol::authenticate(const char *credentials)
{
assert(0);
}
int memcache_text_protocol::write_command_cluster_slots()
{
assert(0);
}
int memcache_text_protocol::write_command_set(const char *key, int key_len, const char *value, int value_len, int expiry, unsigned int offset)
{
assert(key != NULL);
assert(key_len > 0);
assert(value != NULL);
assert(value_len > 0);
int size = 0;
size = evbuffer_add_printf(m_write_buf,
"set %.*s 0 %u %u\r\n", key_len, key, expiry, value_len);
evbuffer_add(m_write_buf, value, value_len);
evbuffer_add(m_write_buf, "\r\n", 2);
size += value_len + 2;
return size;
}
int memcache_text_protocol::write_command_get(const char *key, int key_len, unsigned int offset)
{
assert(key != NULL);
assert(key_len > 0);
int size = 0;
size = evbuffer_add_printf(m_write_buf,
"get %.*s\r\n", key_len, key);
return size;
}
int memcache_text_protocol::write_command_multi_get(const keylist *keylist)
{
assert(keylist != NULL);
assert(keylist->get_keys_count() > 0);
int n = 0;
int size = 0;
n = evbuffer_add(m_write_buf, "get", 3);
assert(n != -1);
size = 3;
for (unsigned int i = 0; i < keylist->get_keys_count(); i++) {
const char *key;
unsigned int key_len;
n = evbuffer_add(m_write_buf, " ", 1);
assert(n != -1);
size++;
key = keylist->get_key(i, &key_len);
assert(key != NULL);
n = evbuffer_add(m_write_buf, key, key_len);
assert(n != -1);
size += key_len;
}
n = evbuffer_add(m_write_buf, "\r\n", 2);
assert(n != -1);
size += 2;
return size;
}
int memcache_text_protocol::write_command_wait(unsigned int num_slaves, unsigned int timeout)
{
fprintf(stderr, "error: WAIT command not implemented for memcache!\n");
assert(0);
}
int memcache_text_protocol::parse_response(void)
{
char *line;
size_t tmplen;
while (true) {
switch (m_response_state) {
case rs_initial:
m_last_response.clear();
m_response_state = rs_read_section;
m_response_len = 0;
break;
case rs_read_section:
line = evbuffer_readln(m_read_buf, &tmplen, EVBUFFER_EOL_CRLF_STRICT);
if (!line)
return 0;
m_response_len += tmplen + 2; // For CRLF
if (m_last_response.get_status() == NULL) {
m_last_response.set_status(line);
}
m_last_response.set_total_len((unsigned int) m_response_len); // for now...
if (memcmp(line, "VALUE", 5) == 0) {
char prefix[50];
char key[256];
unsigned int flags;
unsigned int cas;
int res = sscanf(line, "%s %s %u %u %u", prefix, key, &flags, &m_value_len, &cas);
if (res < 4|| res > 5) {
benchmark_debug_log("unexpected VALUE response: %s\n", line);
if (m_last_response.get_status() != line)
free(line);
return -1;
}
m_response_state = rs_read_value;
continue;
} else if (memcmp(line, "END", 3) == 0 ||
memcmp(line, "STORED", 6) == 0) {
if (m_last_response.get_status() != line)
free(line);
m_response_state = rs_read_end;
break;
} else {
m_last_response.set_error(true);
benchmark_debug_log("unknown response: %s\n", line);
return -1;
}
break;
case rs_read_value:
if (evbuffer_get_length(m_read_buf) >= m_value_len + 2) {
if (m_keep_value) {
char *value = (char *) malloc(m_value_len);
assert(value != NULL);
int ret = evbuffer_remove(m_read_buf, value, m_value_len);
assert((unsigned int) ret == 0);
m_last_response.set_value(value, m_value_len);
} else {
int ret = evbuffer_drain(m_read_buf, m_value_len);
assert((unsigned int) ret == 0);
}
int ret = evbuffer_drain(m_read_buf, 2);
assert((unsigned int) ret == 0);
m_last_response.incr_hits();
m_response_len += m_value_len + 2;
m_response_state = rs_read_section;
} else {
return 0;
}
break;
case rs_read_end:
m_response_state = rs_initial;
return 1;
default:
benchmark_debug_log("unknown response state %d.\n", m_response_state);
return -1;
}
}
return -1;
}
bool memcache_text_protocol::format_arbitrary_command(arbitrary_command& cmd) {
assert(0);
}
int memcache_text_protocol::write_arbitrary_command(const command_arg *arg) {
assert(0);
}
int memcache_text_protocol::write_arbitrary_command(const char *val, int val_len) {
assert(0);
}
/////////////////////////////////////////////////////////////////////////
class memcache_binary_protocol : public abstract_protocol {
protected:
enum response_state { rs_initial, rs_read_body };
response_state m_response_state;
protocol_binary_response_no_extras m_response_hdr;
size_t m_response_len;
const char* status_text(void);
public:
memcache_binary_protocol() : m_response_state(rs_initial), m_response_len(0) { }
virtual memcache_binary_protocol* clone(void) { return new memcache_binary_protocol(); }
virtual int select_db(int db);
virtual int authenticate(const char *credentials);
virtual int write_command_cluster_slots();
virtual int write_command_set(const char *key, int key_len, const char *value, int value_len, int expiry, unsigned int offset);
virtual int write_command_get(const char *key, int key_len, unsigned int offset);
virtual int write_command_multi_get(const keylist *keylist);
virtual int write_command_wait(unsigned int num_slaves, unsigned int timeout);
virtual int parse_response(void);
// handle arbitrary command
virtual bool format_arbitrary_command(arbitrary_command& cmd);
virtual int write_arbitrary_command(const command_arg *arg);
virtual int write_arbitrary_command(const char *val, int val_len);
};
int memcache_binary_protocol::select_db(int db)
{
assert(0);
}
int memcache_binary_protocol::authenticate(const char *credentials)
{
protocol_binary_request_no_extras req;
char nullbyte = '\0';
const char mechanism[] = "PLAIN";
int mechanism_len = sizeof(mechanism) - 1;
const char *colon;
const char *user;
int user_len;
const char *passwd;
int passwd_len;
assert(credentials != NULL);
colon = strchr(credentials, ':');
assert(colon != NULL);
user = credentials;
user_len = colon - user;
passwd = colon + 1;
passwd_len = strlen(passwd);
memset(&req, 0, sizeof(req));
req.message.header.request.magic = PROTOCOL_BINARY_REQ;
req.message.header.request.opcode = PROTOCOL_BINARY_CMD_SASL_AUTH;
req.message.header.request.keylen = htons(mechanism_len);
req.message.header.request.datatype = PROTOCOL_BINARY_RAW_BYTES;
req.message.header.request.bodylen = htonl(mechanism_len + user_len + passwd_len + 2);
evbuffer_add(m_write_buf, &req, sizeof(req));
evbuffer_add(m_write_buf, mechanism, mechanism_len);
evbuffer_add(m_write_buf, &nullbyte, 1);
evbuffer_add(m_write_buf, user, user_len);
evbuffer_add(m_write_buf, &nullbyte, 1);
evbuffer_add(m_write_buf, passwd, passwd_len);
return sizeof(req) + user_len + passwd_len + 2 + sizeof(mechanism) - 1;
}
int memcache_binary_protocol::write_command_cluster_slots()
{
assert(0);
}
int memcache_binary_protocol::write_command_set(const char *key, int key_len, const char *value, int value_len, int expiry, unsigned int offset)
{
assert(key != NULL);
assert(key_len > 0);
assert(value != NULL);
assert(value_len > 0);
protocol_binary_request_set req;
memset(&req, 0, sizeof(req));
req.message.header.request.magic = PROTOCOL_BINARY_REQ;
req.message.header.request.opcode = PROTOCOL_BINARY_CMD_SET;
req.message.header.request.keylen = htons(key_len);
req.message.header.request.datatype = PROTOCOL_BINARY_RAW_BYTES;
req.message.header.request.bodylen = htonl(sizeof(req.message.body) + value_len + key_len);
req.message.header.request.extlen = sizeof(req.message.body);
req.message.body.expiration = htonl(expiry);
evbuffer_add(m_write_buf, &req, sizeof(req));
evbuffer_add(m_write_buf, key, key_len);
evbuffer_add(m_write_buf, value, value_len);
return sizeof(req) + key_len + value_len;
}
int memcache_binary_protocol::write_command_get(const char *key, int key_len, unsigned int offset)
{
assert(key != NULL);
assert(key_len > 0);
protocol_binary_request_get req;
memset(&req, 0, sizeof(req));
req.message.header.request.magic = PROTOCOL_BINARY_REQ;
req.message.header.request.opcode = PROTOCOL_BINARY_CMD_GET;
req.message.header.request.keylen = htons(key_len);
req.message.header.request.datatype = PROTOCOL_BINARY_RAW_BYTES;
req.message.header.request.bodylen = htonl(key_len);
req.message.header.request.extlen = 0;
evbuffer_add(m_write_buf, &req, sizeof(req));