2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
16 #include "mem_config.h"
24 #include <netinet/tcp.h>
25 #include <netinet/in.h>
27 #if defined(HAVE_ARPA_INET_H)
28 # include <arpa/inet.h>
31 #if defined(HAVE_SYS_TIME_H)
32 # include <sys/time.h>
35 #if defined(HAVE_TIME_H)
39 #include "ms_setting.h"
40 #include "ms_thread.h"
41 #include "ms_atomic.h"
44 /* /usr/include/netinet/in.h defines macros from ntohs() to _bswap_nn to
45 * optimize the conversion functions, but the prototypes generate warnings
46 * from gcc. The conversion methods isn't the bottleneck for my app, so
47 * just remove the warnings by undef'ing the optimization ..
55 /* for network write */
56 #define TRANSMIT_COMPLETE 0
57 #define TRANSMIT_INCOMPLETE 1
58 #define TRANSMIT_SOFT_ERROR 2
59 #define TRANSMIT_HARD_ERROR 3
61 /* for generating key */
62 #define KEY_PREFIX_BASE 0x1010101010101010 /* not include ' ' '\r' '\n' '\0' */
63 #define KEY_PREFIX_MASK 0x1010101010101010
65 /* For parse the value length return by server */
67 #define VALUELEN_TOKEN 3
69 /* global increasing counter, to ensure the key prefix unique */
70 static uint64_t key_prefix_seq
= KEY_PREFIX_BASE
;
72 /* global increasing counter, generating request id for UDP */
73 static ATOMIC
uint32_t udp_request_id
= 0;
75 extern pthread_key_t ms_thread_key
;
77 /* generate upd request id */
78 static uint32_t ms_get_udp_request_id(void);
80 /* connect initialize */
81 static void ms_task_init(ms_conn_t
*c
);
82 static int ms_conn_udp_init(ms_conn_t
*c
, const bool is_udp
);
83 static int ms_conn_sock_init(ms_conn_t
*c
);
84 static int ms_conn_event_init(ms_conn_t
*c
);
85 static int ms_conn_init(ms_conn_t
*c
, const int init_state
, const int read_buffer_size
,
87 static void ms_warmup_num_init(ms_conn_t
*c
);
88 static int ms_item_win_init(ms_conn_t
*c
);
90 /* connection close */
91 void ms_conn_free(ms_conn_t
*c
);
92 static void ms_conn_close(ms_conn_t
*c
);
94 /* create network connection */
95 static int ms_new_socket(struct addrinfo
*ai
);
96 static void ms_maximize_sndbuf(const int sfd
);
97 static int ms_network_connect(ms_conn_t
*c
, char *srv_host_name
, const int srv_port
,
98 const bool is_udp
, int *ret_sfd
);
99 static int ms_reconn(ms_conn_t
*c
);
102 static int ms_tokenize_command(char *command
, token_t
*tokens
, const int max_tokens
);
103 static int ms_ascii_process_line(ms_conn_t
*c
, char *command
);
104 static int ms_try_read_line(ms_conn_t
*c
);
105 static int ms_sort_udp_packet(ms_conn_t
*c
, char *buf
, int rbytes
);
106 static int ms_udp_read(ms_conn_t
*c
, char *buf
, int len
);
107 static int ms_try_read_network(ms_conn_t
*c
);
108 static void ms_verify_value(ms_conn_t
*c
, ms_mlget_task_item_t
*mlget_item
, char *value
, int vlen
);
109 static void ms_ascii_complete_nread(ms_conn_t
*c
);
110 static void ms_bin_complete_nread(ms_conn_t
*c
);
111 static void ms_complete_nread(ms_conn_t
*c
);
114 static int ms_add_msghdr(ms_conn_t
*c
);
115 static int ms_ensure_iov_space(ms_conn_t
*c
);
116 static int ms_add_iov(ms_conn_t
*c
, const void *buf
, int len
);
117 static int ms_build_udp_headers(ms_conn_t
*c
);
118 static int ms_transmit(ms_conn_t
*c
);
120 /* status adjustment */
121 static void ms_conn_shrink(ms_conn_t
*c
);
122 static void ms_conn_set_state(ms_conn_t
*c
, int state
);
123 static bool ms_update_event(ms_conn_t
*c
, const int new_flags
);
124 static uint32_t ms_get_rep_sock_index(ms_conn_t
*c
, int cmd
);
125 static uint32_t ms_get_next_sock_index(ms_conn_t
*c
);
126 static int ms_update_conn_sock_event(ms_conn_t
*c
);
127 static bool ms_need_yield(ms_conn_t
*c
);
128 static void ms_update_start_time(ms_conn_t
*c
);
131 static void ms_drive_machine(ms_conn_t
*c
);
132 void ms_event_handler(const int fd
, const short which
, void *arg
);
135 static int ms_build_ascii_write_buf_set(ms_conn_t
*c
, ms_task_item_t
*item
);
136 static int ms_build_ascii_write_buf_get(ms_conn_t
*c
, ms_task_item_t
*item
);
137 static int ms_build_ascii_write_buf_mlget(ms_conn_t
*c
);
139 /* binary protocol */
140 static int ms_bin_process_response(ms_conn_t
*c
);
141 static void ms_add_bin_header(ms_conn_t
*c
, uint8_t opcode
, uint8_t hdr_len
, uint16_t key_len
,
143 static void ms_add_key_to_iov(ms_conn_t
*c
, ms_task_item_t
*item
);
144 static int ms_build_bin_write_buf_set(ms_conn_t
*c
, ms_task_item_t
*item
);
145 static int ms_build_bin_write_buf_get(ms_conn_t
*c
, ms_task_item_t
*item
);
146 static int ms_build_bin_write_buf_mlget(ms_conn_t
*c
);
149 * each key has two parts, prefix and suffix. The suffix is a
150 * string random get form the character table. The prefix is a
151 * uint64_t variable. And the prefix must be unique. we use the
152 * prefix to identify a key. And the prefix can't include
153 * character ' ' '\r' '\n' '\0'.
157 uint64_t ms_get_key_prefix(void) {
160 pthread_mutex_lock(&ms_global
.seq_mutex
);
161 key_prefix_seq
|= KEY_PREFIX_MASK
;
162 key_prefix
= key_prefix_seq
;
164 pthread_mutex_unlock(&ms_global
.seq_mutex
);
167 } /* ms_get_key_prefix */
170 * get an unique udp request id
172 * @return an unique UDP request id
174 static uint32_t ms_get_udp_request_id(void) {
175 return atomic_add_32_nv(&udp_request_id
, 1);
179 * initialize current task structure
181 * @param c, pointer of the concurrency
183 static void ms_task_init(ms_conn_t
*c
) {
184 c
->curr_task
.cmd
= CMD_NULL
;
185 c
->curr_task
.item
= 0;
186 c
->curr_task
.verify
= false;
187 c
->curr_task
.finish_verify
= true;
188 c
->curr_task
.get_miss
= true;
190 c
->curr_task
.get_opt
= 0;
191 c
->curr_task
.set_opt
= 0;
192 c
->curr_task
.cycle_undo_get
= 0;
193 c
->curr_task
.cycle_undo_set
= 0;
194 c
->curr_task
.verified_get
= 0;
195 c
->curr_task
.overwrite_set
= 0;
199 * initialize udp for the connection structure
201 * @param c, pointer of the concurrency
202 * @param is_udp, whether it's udp
204 * @return int, if success, return EXIT_SUCCESS, else return -1
206 static int ms_conn_udp_init(ms_conn_t
*c
, const bool is_udp
) {
211 c
->rudpsize
= UDP_DATA_BUFFER_SIZE
;
222 if (c
->udp
|| (!c
->udp
&& ms_setting
.facebook_test
)) {
223 c
->rudpbuf
= (char *) malloc((size_t) c
->rudpsize
);
224 c
->udppkt
= (ms_udppkt_t
*) malloc(MAX_UDP_PACKET
* sizeof(ms_udppkt_t
));
226 if ((c
->rudpbuf
== NULL
) || (c
->udppkt
== NULL
)) {
231 fprintf(stderr
, "malloc()\n");
234 memset(c
->udppkt
, 0, MAX_UDP_PACKET
* sizeof(ms_udppkt_t
));
238 } /* ms_conn_udp_init */
241 * initialize the connection structure
243 * @param c, pointer of the concurrency
244 * @param init_state, (conn_read, conn_write, conn_closing)
245 * @param read_buffer_size
246 * @param is_udp, whether it's udp
248 * @return int, if success, return EXIT_SUCCESS, else return -1
250 static int ms_conn_init(ms_conn_t
*c
, const int init_state
, const int read_buffer_size
,
254 c
->rbuf
= c
->wbuf
= 0;
258 c
->rsize
= read_buffer_size
;
259 c
->wsize
= WRITE_BUFFER_SIZE
;
260 c
->iovsize
= IOV_LIST_INITIAL
;
261 c
->msgsize
= MSG_LIST_INITIAL
;
263 /* for replication, each connection need connect all the server */
264 if (ms_setting
.rep_write_srv
> 0) {
265 c
->total_sfds
= ms_setting
.srv_cnt
* ms_setting
.sock_per_conn
;
267 c
->total_sfds
= ms_setting
.sock_per_conn
;
271 c
->rbuf
= (char *) malloc((size_t) c
->rsize
);
272 c
->wbuf
= (char *) malloc((size_t) c
->wsize
);
273 c
->iov
= (struct iovec
*) malloc(sizeof(struct iovec
) * (size_t) c
->iovsize
);
274 c
->msglist
= (struct msghdr
*) malloc(sizeof(struct msghdr
) * (size_t) c
->msgsize
);
275 if (ms_setting
.mult_key_num
> 1) {
276 c
->mlget_task
.mlget_item
= (ms_mlget_task_item_t
*) malloc(sizeof(ms_mlget_task_item_t
)
277 * (size_t) ms_setting
.mult_key_num
);
279 c
->tcpsfd
= (int *) malloc((size_t) c
->total_sfds
* sizeof(int));
281 if ((c
->rbuf
== NULL
) || (c
->wbuf
== NULL
) || (c
->iov
== NULL
) || (c
->msglist
== NULL
)
282 || (c
->tcpsfd
== NULL
)
283 || ((ms_setting
.mult_key_num
> 1) && (c
->mlget_task
.mlget_item
== NULL
)))
293 if (c
->mlget_task
.mlget_item
)
294 free(c
->mlget_task
.mlget_item
);
297 fprintf(stderr
, "malloc()\n");
301 c
->state
= init_state
;
309 c
->cur_idx
= c
->total_sfds
; /* default index is a invalid value */
313 c
->change_sfd
= false;
315 c
->precmd
.cmd
= c
->currcmd
.cmd
= CMD_NULL
;
316 c
->precmd
.isfinish
= true; /* default the previous command finished */
317 c
->currcmd
.isfinish
= false;
318 c
->precmd
.retstat
= c
->currcmd
.retstat
= MCD_FAILURE
;
319 c
->precmd
.key_prefix
= c
->currcmd
.key_prefix
= 0;
321 c
->mlget_task
.mlget_num
= 0;
322 c
->mlget_task
.value_index
= -1; /* default invalid value */
324 if (ms_setting
.binary_prot_
) {
325 c
->protocol
= binary_prot
;
327 c
->protocol
= ascii_prot
;
331 if (ms_conn_udp_init(c
, is_udp
)) {
335 /* initialize task */
338 if (!(ms_setting
.facebook_test
&& is_udp
)) {
339 atomic_add_32(&ms_stats
.active_conns
, 1);
346 * when doing 100% get operation, it could preset some objects
347 * to warmup the server. this function is used to initialize the
348 * number of the objects to preset.
350 * @param c, pointer of the concurrency
352 static void ms_warmup_num_init(ms_conn_t
*c
) {
353 /* no set operation, preset all the items in the window */
354 if (ms_setting
.cmd_distr
[CMD_SET
].cmd_prop
< PROP_ERROR
) {
355 c
->warmup_num
= c
->win_size
;
356 c
->remain_warmup_num
= c
->warmup_num
;
359 c
->remain_warmup_num
= c
->warmup_num
;
361 } /* ms_warmup_num_init */
364 * each connection has an item window, this function initialize
365 * the window. The window is used to generate task.
367 * @param c, pointer of the concurrency
369 * @return int, if success, return EXIT_SUCCESS, else return -1
371 static int ms_item_win_init(ms_conn_t
*c
) {
372 ms_thread_t
*ms_thread
= pthread_getspecific(ms_thread_key
);
375 c
->win_size
= (int) ms_setting
.win_size
;
377 c
->exec_num
= ms_thread
->thread_ctx
->exec_num_perconn
;
378 c
->remain_exec_num
= c
->exec_num
;
380 c
->item_win
= (ms_task_item_t
*) malloc(sizeof(ms_task_item_t
) * (size_t) c
->win_size
);
381 if (c
->item_win
== NULL
) {
382 fprintf(stderr
, "Can't allocate task item array for conn.\n");
385 memset(c
->item_win
, 0, sizeof(ms_task_item_t
) * (size_t) c
->win_size
);
387 for (int i
= 0; i
< c
->win_size
; i
++) {
388 c
->item_win
[i
].key_size
= (int) ms_setting
.distr
[i
].key_size
;
389 c
->item_win
[i
].key_prefix
= ms_get_key_prefix();
390 c
->item_win
[i
].key_suffix_offset
= ms_setting
.distr
[i
].key_offset
;
391 c
->item_win
[i
].value_size
= (int) ms_setting
.distr
[i
].value_size
;
392 c
->item_win
[i
].value_offset
= INVALID_OFFSET
; /* default in invalid offset */
393 c
->item_win
[i
].client_time
= 0;
395 /* set expire time base on the proportion */
396 if (exp_cnt
< ms_setting
.exp_ver_per
* i
) {
397 c
->item_win
[i
].exp_time
= FIXED_EXPIRE_TIME
;
400 c
->item_win
[i
].exp_time
= 0;
404 ms_warmup_num_init(c
);
407 } /* ms_item_win_init */
410 * each connection structure can include one or more sock
411 * handlers. this function create these socks and connect the
414 * @param c, pointer of the concurrency
416 * @return int, if success, return EXIT_SUCCESS, else return -1
418 static int ms_conn_sock_init(ms_conn_t
*c
) {
419 ms_thread_t
*ms_thread
= pthread_getspecific(ms_thread_key
);
422 uint32_t srv_idx
= 0;
427 for (i
= 0; i
< c
->total_sfds
; i
++) {
429 if (ms_setting
.rep_write_srv
> 0) {
430 /* for replication, each connection need connect all the server */
431 srv_idx
= i
% ms_setting
.srv_cnt
;
433 /* all the connections in a thread connects the same server */
434 srv_idx
= ms_thread
->thread_ctx
->srv_idx
;
437 if (ms_network_connect(c
, ms_setting
.servers
[srv_idx
].srv_host_name
,
438 ms_setting
.servers
[srv_idx
].srv_port
, ms_setting
.udp
, &ret_sfd
)
448 if (!ms_setting
.udp
) {
449 c
->tcpsfd
[i
] = ret_sfd
;
455 /* initialize udp sock handler if necessary */
456 if (ms_setting
.facebook_test
) {
458 if (ms_network_connect(c
, ms_setting
.servers
[srv_idx
].srv_host_name
,
459 ms_setting
.servers
[srv_idx
].srv_port
, true, &ret_sfd
)
468 if ((i
!= c
->total_sfds
) || (ms_setting
.facebook_test
&& (c
->udpsfd
== 0))) {
469 if (ms_setting
.udp
) {
472 for (uint32_t j
= 0; j
< i
; j
++) {
485 } /* ms_conn_sock_init */
488 * each connection is managed by libevent, this function
489 * initialize the event of the connection structure.
491 * @param c, pointer of the concurrency
493 * @return int, if success, return EXIT_SUCCESS, else return -1
495 static int ms_conn_event_init(ms_conn_t
*c
) {
496 ms_thread_t
*ms_thread
= pthread_getspecific(ms_thread_key
);
497 short event_flags
= EV_WRITE
| EV_PERSIST
;
499 event_set(&c
->event
, c
->sfd
, event_flags
, ms_event_handler
, (void *) c
);
500 event_base_set(ms_thread
->base
, &c
->event
);
501 c
->ev_flags
= event_flags
;
503 if (event_add(&c
->event
, NULL
) == -1) {
508 } /* ms_conn_event_init */
511 * setup a connection, each connection structure of each
512 * thread must call this function to initialize.
514 * @param c, pointer of the concurrency
516 * @return int, if success, return EXIT_SUCCESS, else return -1
518 int ms_setup_conn(ms_conn_t
*c
) {
519 if (ms_item_win_init(c
)) {
523 if (ms_conn_init(c
, conn_write
, DATA_BUFFER_SIZE
, ms_setting
.udp
)) {
527 if (ms_conn_sock_init(c
)) {
531 if (ms_conn_event_init(c
)) {
536 } /* ms_setup_conn */
539 * Frees a connection.
541 * @param c, pointer of the concurrency
543 void ms_conn_free(ms_conn_t
*c
) {
544 ms_thread_t
*ms_thread
= pthread_getspecific(ms_thread_key
);
556 if (c
->mlget_task
.mlget_item
)
557 free(c
->mlget_task
.mlget_item
);
567 if (--ms_thread
->nactive_conn
== 0) {
568 free(ms_thread
->conn
);
576 * @param c, pointer of the concurrency
578 static void ms_conn_close(ms_conn_t
*c
) {
579 ms_thread_t
*ms_thread
= pthread_getspecific(ms_thread_key
);
582 /* delete the event, the socket and the connection */
583 event_del(&c
->event
);
585 for (uint32_t i
= 0; i
< c
->total_sfds
; i
++) {
586 if (c
->tcpsfd
[i
] > 0) {
592 if (ms_setting
.facebook_test
) {
596 atomic_dec_32(&ms_stats
.active_conns
);
600 if (ms_setting
.run_time
== 0) {
601 pthread_mutex_lock(&ms_global
.run_lock
.lock
);
602 ms_global
.run_lock
.count
++;
603 pthread_cond_signal(&ms_global
.run_lock
.cond
);
604 pthread_mutex_unlock(&ms_global
.run_lock
.lock
);
607 if (ms_thread
->nactive_conn
== 0) {
608 event_base_loopbreak(ms_thread
->base
);
610 } /* ms_conn_close */
615 * @param ai, server address information
617 * @return int, if success, return EXIT_SUCCESS, else return -1
619 static int ms_new_socket(struct addrinfo
*ai
) {
622 if ((sfd
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
)) == -1) {
623 fprintf(stderr
, "socket() error: %s.\n", strerror(errno
));
628 } /* ms_new_socket */
631 * Sets a socket's send buffer size to the maximum allowed by the system.
633 * @param sfd, file descriptor of socket
635 static void ms_maximize_sndbuf(const int sfd
) {
636 socklen_t intsize
= sizeof(int);
637 unsigned int last_good
= 0;
638 unsigned int min
, max
, avg
;
639 unsigned int old_size
;
641 /* Start with the default size. */
642 if (getsockopt(sfd
, SOL_SOCKET
, SO_SNDBUF
, &old_size
, &intsize
)) {
643 fprintf(stderr
, "getsockopt(SO_SNDBUF)\n");
647 /* Binary-search for the real maximum. */
649 max
= MAX_SENDBUF_SIZE
;
652 avg
= ((unsigned int) (min
+ max
)) / 2;
653 if (setsockopt(sfd
, SOL_SOCKET
, SO_SNDBUF
, (void *) &avg
, intsize
) == 0) {
661 } /* ms_maximize_sndbuf */
664 * socket connects the server
666 * @param c, pointer of the concurrency
667 * @param srv_host_name, the host name of the server
668 * @param srv_port, port of server
669 * @param is_udp, whether it's udp
670 * @param ret_sfd, the connected socket file descriptor
672 * @return int, if success, return EXIT_SUCCESS, else return -1
674 static int ms_network_connect(ms_conn_t
*c
, char *srv_host_name
, const int srv_port
,
675 const bool is_udp
, int *ret_sfd
) {
677 struct linger ling
= {0, 0};
679 struct addrinfo
*next
;
680 struct addrinfo hints
;
681 char port_buf
[NI_MAXSERV
];
688 * the memset call clears nonstandard fields in some impementations
689 * that otherwise mess things up.
691 memset(&hints
, 0, sizeof(hints
));
693 hints
.ai_flags
= AI_PASSIVE
| AI_ADDRCONFIG
;
695 hints
.ai_flags
= AI_PASSIVE
;
696 #endif /* AI_ADDRCONFIG */
698 hints
.ai_protocol
= IPPROTO_UDP
;
699 hints
.ai_socktype
= SOCK_DGRAM
;
700 hints
.ai_family
= AF_INET
; /* This left here because of issues with OSX 10.5 */
702 hints
.ai_family
= AF_UNSPEC
;
703 hints
.ai_protocol
= IPPROTO_TCP
;
704 hints
.ai_socktype
= SOCK_STREAM
;
707 snprintf(port_buf
, NI_MAXSERV
, "%d", srv_port
);
708 error
= getaddrinfo(srv_host_name
, port_buf
, &hints
, &ai
);
710 if (error
!= EAI_SYSTEM
)
711 fprintf(stderr
, "getaddrinfo(): %s.\n", gai_strerror(error
));
713 perror("getaddrinfo()");
718 for (next
= ai
; next
; next
= next
->ai_next
) {
719 if ((sfd
= ms_new_socket(next
)) == -1) {
724 setsockopt(sfd
, SOL_SOCKET
, SO_REUSEADDR
, (void *) &flags
, sizeof(flags
));
726 ms_maximize_sndbuf(sfd
);
728 setsockopt(sfd
, SOL_SOCKET
, SO_KEEPALIVE
, (void *) &flags
, sizeof(flags
));
729 setsockopt(sfd
, SOL_SOCKET
, SO_LINGER
, (void *) &ling
, sizeof(ling
));
730 setsockopt(sfd
, IPPROTO_TCP
, TCP_NODELAY
, (void *) &flags
, sizeof(flags
));
734 c
->srv_recv_addr_size
= sizeof(struct sockaddr
);
735 memcpy(&c
->srv_recv_addr
, next
->ai_addr
, c
->srv_recv_addr_size
);
737 if (connect(sfd
, next
->ai_addr
, next
->ai_addrlen
) == -1) {
744 if (((flags
= fcntl(sfd
, F_GETFL
, 0)) < 0) || (fcntl(sfd
, F_SETFL
, flags
| O_NONBLOCK
) < 0)) {
745 fprintf(stderr
, "setting O_NONBLOCK\n");
760 /* Return zero if we detected no errors in starting up connections */
762 } /* ms_network_connect */
765 * reconnect a disconnected sock
767 * @param c, pointer of the concurrency
769 * @return int, if success, return EXIT_SUCCESS, else return -1
771 static int ms_reconn(ms_conn_t
*c
) {
772 ms_thread_t
*ms_thread
= pthread_getspecific(ms_thread_key
);
773 uint32_t srv_idx
= 0;
774 uint32_t srv_conn_cnt
= 0;
776 if (ms_setting
.rep_write_srv
> 0) {
777 srv_idx
= c
->cur_idx
% ms_setting
.srv_cnt
;
778 srv_conn_cnt
= ms_setting
.sock_per_conn
* ms_setting
.nconns
;
780 srv_idx
= ms_thread
->thread_ctx
->srv_idx
;
781 srv_conn_cnt
= ms_setting
.nconns
/ ms_setting
.srv_cnt
;
784 /* close the old socket handler */
786 c
->tcpsfd
[c
->cur_idx
] = 0;
788 if (atomic_add_32_nv(&ms_setting
.servers
[srv_idx
].disconn_cnt
, 1) % srv_conn_cnt
== 0) {
789 gettimeofday(&ms_setting
.servers
[srv_idx
].disconn_time
, NULL
);
790 fprintf(stderr
, "Server %s:%d disconnect\n", ms_setting
.servers
[srv_idx
].srv_host_name
,
791 ms_setting
.servers
[srv_idx
].srv_port
);
794 if (ms_setting
.rep_write_srv
> 0) {
797 for (i
= 0; i
< c
->total_sfds
; i
++) {
803 /* all socks disconnect */
804 if (i
== c
->total_sfds
) {
809 /* reconnect success, break the loop */
810 if (ms_network_connect(c
, ms_setting
.servers
[srv_idx
].srv_host_name
,
811 ms_setting
.servers
[srv_idx
].srv_port
, ms_setting
.udp
, &c
->sfd
)
814 c
->tcpsfd
[c
->cur_idx
] = c
->sfd
;
815 if (atomic_add_32_nv(&ms_setting
.servers
[srv_idx
].reconn_cnt
, 1) % (uint32_t) srv_conn_cnt
817 gettimeofday(&ms_setting
.servers
[srv_idx
].reconn_time
, NULL
);
818 int reconn_time
= (int) (ms_setting
.servers
[srv_idx
].reconn_time
.tv_sec
819 - ms_setting
.servers
[srv_idx
].disconn_time
.tv_sec
);
820 fprintf(stderr
, "Server %s:%d reconnect after %ds\n",
821 ms_setting
.servers
[srv_idx
].srv_host_name
, ms_setting
.servers
[srv_idx
].srv_port
,
827 if (ms_setting
.rep_write_srv
== 0 && c
->total_sfds
> 0) {
828 /* wait a second and reconnect */
831 } while (ms_setting
.rep_write_srv
== 0 && c
->total_sfds
> 0);
834 if ((c
->total_sfds
> 1) && (c
->tcpsfd
[c
->cur_idx
] == 0)) {
843 * reconnect several disconnected socks in the connection
844 * structure, the ever-1-second timer of the thread will check
845 * whether some socks in the connections disconnect. if
846 * disconnect, reconnect the sock.
848 * @param c, pointer of the concurrency
850 * @return int, if success, return EXIT_SUCCESS, else return -1
852 int ms_reconn_socks(ms_conn_t
*c
) {
853 ms_thread_t
*ms_thread
= pthread_getspecific(ms_thread_key
);
854 uint32_t srv_idx
= 0;
856 uint32_t srv_conn_cnt
= 0;
857 struct timeval cur_time
;
861 if ((c
->total_sfds
== 1) || (c
->total_sfds
== c
->alive_sfds
)) {
865 for (uint32_t i
= 0; i
< c
->total_sfds
; i
++) {
866 if (c
->tcpsfd
[i
] == 0) {
867 gettimeofday(&cur_time
, NULL
);
870 * For failover test of replication, reconnect the socks after
871 * it disconnects more than 5 seconds, Otherwise memslap will
872 * block at connect() function and the work threads can't work
875 if (cur_time
.tv_sec
- ms_setting
.servers
[srv_idx
].disconn_time
.tv_sec
< 5) {
879 if (ms_setting
.rep_write_srv
> 0) {
880 srv_idx
= i
% ms_setting
.srv_cnt
;
881 srv_conn_cnt
= ms_setting
.sock_per_conn
* ms_setting
.nconns
;
883 srv_idx
= ms_thread
->thread_ctx
->srv_idx
;
884 srv_conn_cnt
= ms_setting
.nconns
/ ms_setting
.srv_cnt
;
887 if (ms_network_connect(c
, ms_setting
.servers
[srv_idx
].srv_host_name
,
888 ms_setting
.servers
[srv_idx
].srv_port
, ms_setting
.udp
, &ret_sfd
)
891 c
->tcpsfd
[i
] = ret_sfd
;
894 if (atomic_add_32_nv(&ms_setting
.servers
[srv_idx
].reconn_cnt
, 1) % (uint32_t) srv_conn_cnt
896 gettimeofday(&ms_setting
.servers
[srv_idx
].reconn_time
, NULL
);
897 int reconn_time
= (int) (ms_setting
.servers
[srv_idx
].reconn_time
.tv_sec
898 - ms_setting
.servers
[srv_idx
].disconn_time
.tv_sec
);
899 fprintf(stderr
, "Server %s:%d reconnect after %ds\n",
900 ms_setting
.servers
[srv_idx
].srv_host_name
, ms_setting
.servers
[srv_idx
].srv_port
,
908 } /* ms_reconn_socks */
911 * Tokenize the command string by replacing whitespace with '\0' and update
912 * the token array tokens with pointer to start of each token and length.
913 * Returns total number of tokens. The last valid token is the terminal
914 * token (value points to the first unprocessed character of the string and
919 * while(ms_tokenize_command(command, ncommand, tokens, max_tokens) > 0) {
920 * for(int ix = 0; tokens[ix].length; ix++) {
923 * ncommand = tokens[ix].value - command;
924 * command = tokens[ix].value;
927 * @param command, the command string to token
928 * @param tokens, array to store tokens
929 * @param max_tokens, maximum tokens number
931 * @return int, the number of tokens
933 static int ms_tokenize_command(char *command
, token_t
*tokens
, const int max_tokens
) {
937 assert(command
&& tokens
&& max_tokens
> 1);
939 for (s
= e
= command
; ntokens
< max_tokens
- 1; ++e
) {
942 tokens
[ntokens
].value
= s
;
943 tokens
[ntokens
].length
= (size_t)(e
- s
);
948 } else if (*e
== '\0') {
950 tokens
[ntokens
].value
= s
;
951 tokens
[ntokens
].length
= (size_t)(e
- s
);
955 break; /* string end */
960 } /* ms_tokenize_command */
963 * parse the response of server.
965 * @param c, pointer of the concurrency
966 * @param command, the string responded by server
968 * @return int, if the command completed return EXIT_SUCCESS, else return
971 static int ms_ascii_process_line(ms_conn_t
*c
, char *command
) {
974 char *buffer
= command
;
979 * for command get, we store the returned value into local buffer
980 * then continue in ms_complete_nread().
984 case 'V': /* VALUE || VERSION */
985 if (buffer
[1] == 'A') /* VALUE */ {
986 token_t tokens
[MAX_TOKENS
];
987 ms_tokenize_command(command
, tokens
, MAX_TOKENS
);
989 value_len
= strtol(tokens
[VALUELEN_TOKEN
].value
, NULL
, 10);
991 printf("<%d ERROR %s\n", c
->sfd
, strerror(errno
));
993 memcpy(&c
->currcmd
.key_prefix
, tokens
[KEY_TOKEN
].value
, sizeof(c
->currcmd
.key_prefix
));
996 * We read the \r\n into the string since not doing so is more
997 * cycles then the waster of memory to do so.
999 * We are null terminating through, which will most likely make
1000 * some people lazy about using the return length.
1002 c
->rvbytes
= (int) (value_len
+ 2);
1010 c
->currcmd
.retstat
= MCD_SUCCESS
;
1013 case 'S': /* STORED STATS SERVER_ERROR */
1014 if (buffer
[2] == 'A') /* STORED STATS */ { /* STATS*/
1015 c
->currcmd
.retstat
= MCD_STAT
;
1016 } else if (buffer
[1] == 'E') {
1018 printf("<%d %s\n", c
->sfd
, buffer
);
1020 c
->currcmd
.retstat
= MCD_SERVER_ERROR
;
1021 } else if (buffer
[1] == 'T') {
1023 c
->currcmd
.retstat
= MCD_STORED
;
1025 c
->currcmd
.retstat
= MCD_UNKNOWN_READ_FAILURE
;
1029 case 'D': /* DELETED DATA */
1030 if (buffer
[1] == 'E') {
1031 c
->currcmd
.retstat
= MCD_DELETED
;
1033 c
->currcmd
.retstat
= MCD_UNKNOWN_READ_FAILURE
;
1038 case 'N': /* NOT_FOUND NOT_STORED*/
1039 if (buffer
[4] == 'F') {
1040 c
->currcmd
.retstat
= MCD_NOTFOUND
;
1041 } else if (buffer
[4] == 'S') {
1042 printf("<%d %s\n", c
->sfd
, buffer
);
1043 c
->currcmd
.retstat
= MCD_NOTSTORED
;
1045 c
->currcmd
.retstat
= MCD_UNKNOWN_READ_FAILURE
;
1049 case 'E': /* PROTOCOL ERROR or END */
1050 if (buffer
[1] == 'N') {
1052 c
->currcmd
.retstat
= MCD_END
;
1053 } else if (buffer
[1] == 'R') {
1054 printf("<%d ERROR\n", c
->sfd
);
1055 c
->currcmd
.retstat
= MCD_PROTOCOL_ERROR
;
1056 } else if (buffer
[1] == 'X') {
1057 c
->currcmd
.retstat
= MCD_DATA_EXISTS
;
1058 printf("<%d %s\n", c
->sfd
, buffer
);
1060 c
->currcmd
.retstat
= MCD_UNKNOWN_READ_FAILURE
;
1064 case 'C': /* CLIENT ERROR */
1065 printf("<%d %s\n", c
->sfd
, buffer
);
1066 c
->currcmd
.retstat
= MCD_CLIENT_ERROR
;
1070 c
->currcmd
.retstat
= MCD_UNKNOWN_READ_FAILURE
;
1075 } /* ms_ascii_process_line */
1078 * after one operation completes, reset the concurrency
1080 * @param c, pointer of the concurrency
1081 * @param timeout, whether it's timeout
1083 void ms_reset_conn(ms_conn_t
*c
, bool timeout
) {
1087 if ((c
->packets
> 0) && (c
->packets
< MAX_UDP_PACKET
)) {
1088 memset(c
->udppkt
, 0, sizeof(ms_udppkt_t
) * (size_t) c
->packets
);
1097 c
->currcmd
.isfinish
= true;
1098 c
->ctnwrite
= false;
1104 ms_conn_set_state(c
, conn_write
);
1105 memcpy(&c
->precmd
, &c
->currcmd
, sizeof(ms_cmdstat_t
)); /* replicate command state */
1108 ms_drive_machine(c
);
1110 } /* ms_reset_conn */
1113 * if we have a complete line in the buffer, process it.
1115 * @param c, pointer of the concurrency
1117 * @return int, if success, return EXIT_SUCCESS, else return -1
1119 static int ms_try_read_line(ms_conn_t
*c
) {
1120 if (c
->protocol
== binary_prot
) {
1121 /* Do we have the complete packet header? */
1122 if ((uint64_t) c
->rbytes
< sizeof(c
->binary_header
)) {
1123 /* need more data! */
1124 return EXIT_SUCCESS
;
1127 if (((long) (c
->rcurr
)) % 8) {
1128 /* must realign input buffer */
1129 memmove(c
->rbuf
, c
->rcurr
, c
->rbytes
);
1131 if (settings
.verbose
) {
1132 fprintf(stderr
, "%d: Realign input buffer.\n", c
->sfd
);
1136 protocol_binary_response_header
*rsp
;
1137 rsp
= (protocol_binary_response_header
*) c
->rcurr
;
1139 c
->binary_header
= *rsp
;
1140 c
->binary_header
.response
.extlen
= rsp
->response
.extlen
;
1141 c
->binary_header
.response
.keylen
= ntohs(rsp
->response
.keylen
);
1142 c
->binary_header
.response
.bodylen
= ntohl(rsp
->response
.bodylen
);
1143 c
->binary_header
.response
.status
= ntohs(rsp
->response
.status
);
1145 if (c
->binary_header
.response
.magic
!= PROTOCOL_BINARY_RES
) {
1146 fprintf(stderr
, "Invalid magic: %x\n", c
->binary_header
.response
.magic
);
1147 ms_conn_set_state(c
, conn_closing
);
1148 return EXIT_SUCCESS
;
1151 /* process this complete response */
1152 if (ms_bin_process_response(c
) == 0) {
1153 /* current operation completed */
1154 ms_reset_conn(c
, false);
1157 c
->rbytes
-= (int32_t) sizeof(c
->binary_header
);
1158 c
->rcurr
+= sizeof(c
->binary_header
);
1165 assert(c
->rcurr
<= (c
->rbuf
+ c
->rsize
));
1168 return EXIT_SUCCESS
;
1170 el
= memchr(c
->rcurr
, '\n', (size_t) c
->rbytes
);
1172 return EXIT_SUCCESS
;
1175 if (((el
- c
->rcurr
) > 1) && (*(el
- 1) == '\r')) {
1180 assert(cont
<= (c
->rcurr
+ c
->rbytes
));
1182 /* process this complete line */
1183 if (ms_ascii_process_line(c
, c
->rcurr
) == 0) {
1184 /* current operation completed */
1185 ms_reset_conn(c
, false);
1188 /* current operation didn't complete */
1189 c
->rbytes
-= (int32_t)(cont
- c
->rcurr
);
1193 assert(c
->rcurr
<= (c
->rbuf
+ c
->rsize
));
1197 } /* ms_try_read_line */
1200 * because the packet of UDP can't ensure the order, the
1201 * function is used to sort the received udp packet.
1203 * @param c, pointer of the concurrency
1204 * @param buf, the buffer to store the ordered packages data
1205 * @param rbytes, the maximum capacity of the buffer
1207 * @return int, if success, return the copy bytes, else return
1210 static int ms_sort_udp_packet(ms_conn_t
*c
, char *buf
, int rbytes
) {
1213 uint16_t req_id
= 0;
1214 uint16_t seq_num
= 0;
1215 uint16_t packets
= 0;
1216 unsigned char *header
= NULL
;
1218 /* no enough data */
1221 assert(c
->rudpbytes
>= UDP_HEADER_SIZE
);
1223 /* calculate received packets count */
1224 if (c
->rudpbytes
% UDP_MAX_PAYLOAD_SIZE
>= UDP_HEADER_SIZE
) {
1225 /* the last packet has some data */
1226 c
->recvpkt
= c
->rudpbytes
/ UDP_MAX_PAYLOAD_SIZE
+ 1;
1228 c
->recvpkt
= c
->rudpbytes
/ UDP_MAX_PAYLOAD_SIZE
;
1231 /* get the total packets count if necessary */
1232 if (c
->packets
== 0) {
1233 c
->packets
= HEADER_TO_PACKETS((unsigned char *) c
->rudpbuf
);
1236 /* build the ordered packet array */
1237 for (int i
= c
->pktcurr
; i
< c
->recvpkt
; i
++) {
1238 header
= (unsigned char *) c
->rudpbuf
+ i
* UDP_MAX_PAYLOAD_SIZE
;
1239 req_id
= (uint16_t) HEADER_TO_REQID(header
);
1240 assert(req_id
== c
->request_id
% (1 << 16));
1242 packets
= (uint16_t) HEADER_TO_PACKETS(header
);
1243 assert(c
->packets
== HEADER_TO_PACKETS(header
));
1245 seq_num
= (uint16_t) HEADER_TO_SEQNUM(header
);
1246 c
->udppkt
[seq_num
].header
= header
;
1247 c
->udppkt
[seq_num
].data
= (char *) header
+ UDP_HEADER_SIZE
;
1249 if (i
== c
->recvpkt
- 1) {
1250 /* last received packet */
1251 if (c
->rudpbytes
% UDP_MAX_PAYLOAD_SIZE
== 0) {
1252 c
->udppkt
[seq_num
].rbytes
= UDP_MAX_PAYLOAD_SIZE
- UDP_HEADER_SIZE
;
1255 c
->udppkt
[seq_num
].rbytes
= c
->rudpbytes
% UDP_MAX_PAYLOAD_SIZE
- UDP_HEADER_SIZE
;
1258 c
->udppkt
[seq_num
].rbytes
= UDP_MAX_PAYLOAD_SIZE
- UDP_HEADER_SIZE
;
1263 for (int i
= c
->ordcurr
; i
< c
->recvpkt
; i
++) {
1264 /* there is some data to copy */
1265 if ((c
->udppkt
[i
].data
) && (c
->udppkt
[i
].copybytes
< c
->udppkt
[i
].rbytes
)) {
1266 header
= c
->udppkt
[i
].header
;
1267 len
= c
->udppkt
[i
].rbytes
- c
->udppkt
[i
].copybytes
;
1268 if (len
> rbytes
- wbytes
) {
1269 len
= rbytes
- wbytes
;
1272 assert(len
<= rbytes
- wbytes
);
1273 assert(i
== HEADER_TO_SEQNUM(header
));
1275 memcpy(buf
+ wbytes
, c
->udppkt
[i
].data
+ c
->udppkt
[i
].copybytes
, (size_t) len
);
1277 c
->udppkt
[i
].copybytes
+= len
;
1279 if ((c
->udppkt
[i
].copybytes
== c
->udppkt
[i
].rbytes
)
1280 && (c
->udppkt
[i
].rbytes
== UDP_MAX_PAYLOAD_SIZE
- UDP_HEADER_SIZE
))
1282 /* finish copying all the data of this packet, next */
1286 /* last received packet, and finish copying all the data */
1287 if ((c
->recvpkt
== c
->packets
) && (i
== c
->recvpkt
- 1)
1288 && (c
->udppkt
[i
].copybytes
== c
->udppkt
[i
].rbytes
))
1293 /* no space to copy data */
1294 if (wbytes
>= rbytes
) {
1298 /* it doesn't finish reading all the data of the packet from network */
1299 if ((i
!= c
->recvpkt
- 1) && (c
->udppkt
[i
].rbytes
< UDP_MAX_PAYLOAD_SIZE
- UDP_HEADER_SIZE
)) {
1303 /* no data to copy */
1309 return wbytes
== 0 ? -1 : wbytes
;
1310 } /* ms_sort_udp_packet */
1313 * encapsulate upd read like tcp read
1315 * @param c, pointer of the concurrency
1316 * @param buf, read buffer
1317 * @param len, length to read
1319 * @return int, if success, return the read bytes, else return
1322 static int ms_udp_read(ms_conn_t
*c
, char *buf
, int len
) {
1331 if (c
->rudpbytes
+ UDP_MAX_PAYLOAD_SIZE
> c
->rudpsize
) {
1332 char *new_rbuf
= realloc(c
->rudpbuf
, (size_t) c
->rudpsize
* 2);
1334 fprintf(stderr
, "Couldn't realloc input buffer.\n");
1335 c
->rudpbytes
= 0; /* ignore what we read */
1338 c
->rudpbuf
= new_rbuf
;
1342 avail
= c
->rudpsize
- c
->rudpbytes
;
1343 /* UDP each time read a packet, 1400 bytes */
1344 res
= (int) read(c
->sfd
, c
->rudpbuf
+ c
->rudpbytes
, (size_t) avail
);
1347 atomic_add_size(&ms_stats
.bytes_read
, res
);
1348 c
->rudpbytes
+= res
;
1358 /* "connection" closed */
1363 /* no data to read */
1368 /* copy data to read buffer */
1370 copybytes
= ms_sort_udp_packet(c
, buf
, len
);
1373 if (copybytes
== -1) {
1374 atomic_add_size(&ms_stats
.pkt_disorder
, 1);
1381 * read from network as much as we can, handle buffer overflow and connection
1383 * before reading, move the remaining incomplete fragment of a command
1384 * (if any) to the beginning of the buffer.
1385 * return EXIT_SUCCESS if there's nothing to read on the first read.
1389 * read from network as much as we can, handle buffer overflow and connection
1390 * close. before reading, move the remaining incomplete fragment of a command
1391 * (if any) to the beginning of the buffer.
1393 * @param c, pointer of the concurrency
1396 * return EXIT_SUCCESS if there's nothing to read on the first read.
1397 * return EXIT_FAILURE if get data
1398 * return -1 if error happens
1400 static int ms_try_read_network(ms_conn_t
*c
) {
1407 if ((c
->rcurr
!= c
->rbuf
)
1408 && (!c
->readval
|| (c
->rvbytes
> c
->rsize
- (c
->rcurr
- c
->rbuf
))
1409 || (c
->readval
&& (c
->rcurr
- c
->rbuf
> c
->rbytes
))))
1411 if (c
->rbytes
) /* otherwise there's nothing to copy */
1412 memmove(c
->rbuf
, c
->rcurr
, (size_t) c
->rbytes
);
1417 if (c
->rbytes
>= c
->rsize
) {
1418 char *new_rbuf
= realloc(c
->rbuf
, (size_t) c
->rsize
* 2);
1420 fprintf(stderr
, "Couldn't realloc input buffer.\n");
1421 c
->rbytes
= 0; /* ignore what we read */
1424 c
->rcurr
= c
->rbuf
= new_rbuf
;
1428 avail
= c
->rsize
- c
->rbytes
- (c
->rcurr
- c
->rbuf
);
1434 res
= (int32_t) ms_udp_read(c
, c
->rcurr
+ c
->rbytes
, (int32_t) avail
);
1436 res
= (int) read(c
->sfd
, c
->rcurr
+ c
->rbytes
, (size_t) avail
);
1441 atomic_add_size(&ms_stats
.bytes_read
, res
);
1452 /* connection closed */
1453 ms_conn_set_state(c
, conn_closing
);
1457 if ((errno
== EAGAIN
) || (errno
== EWOULDBLOCK
))
1459 /* Should close on unhandled errors. */
1460 ms_conn_set_state(c
, conn_closing
);
1466 } /* ms_try_read_network */
1469 * after get the object from server, verify the value if
1472 * @param c, pointer of the concurrency
1473 * @param mlget_item, pointer of mulit-get task item structure
1474 * @param value, received value string
1475 * @param vlen, received value string length
1477 static void ms_verify_value(ms_conn_t
*c
, ms_mlget_task_item_t
*mlget_item
, char *value
, int vlen
) {
1478 if (c
->curr_task
.verify
) {
1479 assert(c
->curr_task
.item
->value_offset
!= INVALID_OFFSET
);
1480 char *orignval
= &ms_setting
.char_block
[c
->curr_task
.item
->value_offset
];
1481 char *orignkey
= &ms_setting
.char_block
[c
->curr_task
.item
->key_suffix_offset
];
1483 /* verify expire time if necessary */
1484 if (c
->curr_task
.item
->exp_time
> 0) {
1485 struct timeval curr_time
;
1486 gettimeofday(&curr_time
, NULL
);
1488 /* object expired but get it now */
1489 if (curr_time
.tv_sec
- c
->curr_task
.item
->client_time
1490 > c
->curr_task
.item
->exp_time
+ EXPIRE_TIME_ERROR
)
1492 atomic_add_size(&ms_stats
.exp_get
, 1);
1494 if (ms_setting
.verbose
) {
1497 strftime(set_time
, 64, "%Y-%m-%d %H:%M:%S", localtime(&c
->curr_task
.item
->client_time
));
1498 strftime(cur_time
, 64, "%Y-%m-%d %H:%M:%S", localtime(&curr_time
.tv_sec
));
1500 "\n<%d expire time verification failed, "
1501 "object expired but get it now\n"
1503 "\tkey: %" PRIx64
" %.*s\n"
1504 "\tset time: %s current time: %s "
1505 "diff time: %d expire time: %d\n"
1506 "\texpected data: \n"
1507 "\treceived data len: %d\n"
1508 "\treceived data: %.*s\n",
1509 c
->sfd
, c
->curr_task
.item
->key_size
, c
->curr_task
.item
->key_prefix
,
1510 c
->curr_task
.item
->key_size
- (int) KEY_PREFIX_SIZE
, orignkey
, set_time
, cur_time
,
1511 (int) (curr_time
.tv_sec
- c
->curr_task
.item
->client_time
),
1512 c
->curr_task
.item
->exp_time
, vlen
, vlen
, value
);
1517 if ((c
->curr_task
.item
->value_size
!= vlen
) || (memcmp(orignval
, value
, (size_t) vlen
)))
1519 atomic_add_size(&ms_stats
.vef_failed
, 1);
1521 if (ms_setting
.verbose
) {
1523 "\n<%d data verification failed\n"
1525 "\tkey: %" PRIx64
" %.*s\n"
1526 "\texpected data len: %d\n"
1527 "\texpected data: %.*s\n"
1528 "\treceived data len: %d\n"
1529 "\treceived data: %.*s\n",
1530 c
->sfd
, c
->curr_task
.item
->key_size
, c
->curr_task
.item
->key_prefix
,
1531 c
->curr_task
.item
->key_size
- (int) KEY_PREFIX_SIZE
, orignkey
,
1532 c
->curr_task
.item
->value_size
, c
->curr_task
.item
->value_size
, orignval
, vlen
,
1539 c
->curr_task
.finish_verify
= true;
1542 mlget_item
->finish_verify
= true;
1545 } /* ms_verify_value */
1548 * For ASCII protocol, after store the data into the local
1549 * buffer, run this function to handle the data.
1551 * @param c, pointer of the concurrency
1553 static void ms_ascii_complete_nread(ms_conn_t
*c
) {
1555 assert(c
->rbytes
>= c
->rvbytes
);
1556 assert(c
->protocol
== ascii_prot
);
1557 if (c
->rvbytes
> 2) {
1558 assert(c
->rcurr
[c
->rvbytes
- 1] == '\n' && c
->rcurr
[c
->rvbytes
- 2] == '\r');
1562 ms_mlget_task_item_t
*mlget_item
= NULL
;
1563 if (((ms_setting
.mult_key_num
> 1) && (c
->mlget_task
.mlget_num
>= ms_setting
.mult_key_num
))
1564 || ((c
->remain_exec_num
== 0) && (c
->mlget_task
.mlget_num
> 0)))
1566 c
->mlget_task
.value_index
++;
1567 mlget_item
= &c
->mlget_task
.mlget_item
[c
->mlget_task
.value_index
];
1569 if (mlget_item
->item
->key_prefix
== c
->currcmd
.key_prefix
) {
1570 c
->curr_task
.item
= mlget_item
->item
;
1571 c
->curr_task
.verify
= mlget_item
->verify
;
1572 c
->curr_task
.finish_verify
= mlget_item
->finish_verify
;
1573 mlget_item
->get_miss
= false;
1575 /* Try to find the task item in multi-get task array */
1576 for (int i
= 0; i
< c
->mlget_task
.mlget_num
; i
++) {
1577 mlget_item
= &c
->mlget_task
.mlget_item
[i
];
1578 if (mlget_item
->item
->key_prefix
== c
->currcmd
.key_prefix
) {
1579 c
->curr_task
.item
= mlget_item
->item
;
1580 c
->curr_task
.verify
= mlget_item
->verify
;
1581 c
->curr_task
.finish_verify
= mlget_item
->finish_verify
;
1582 mlget_item
->get_miss
= false;
1590 ms_verify_value(c
, mlget_item
, c
->rcurr
, c
->rvbytes
- 2);
1592 c
->curr_task
.get_miss
= false;
1593 c
->rbytes
-= c
->rvbytes
;
1594 c
->rcurr
= c
->rcurr
+ c
->rvbytes
;
1595 assert(c
->rcurr
<= (c
->rbuf
+ c
->rsize
));
1598 } /* ms_ascii_complete_nread */
1601 * For binary protocol, after store the data into the local
1602 * buffer, run this function to handle the data.
1604 * @param c, pointer of the concurrency
1606 static void ms_bin_complete_nread(ms_conn_t
*c
) {
1608 assert(c
->rbytes
>= c
->rvbytes
);
1609 assert(c
->protocol
== binary_prot
);
1611 int extlen
= c
->binary_header
.response
.extlen
;
1612 int keylen
= c
->binary_header
.response
.keylen
;
1613 uint8_t opcode
= c
->binary_header
.response
.opcode
;
1615 /* not get command or not include value, just return */
1616 if (((opcode
!= PROTOCOL_BINARY_CMD_GET
) && (opcode
!= PROTOCOL_BINARY_CMD_GETQ
))
1617 || (c
->rvbytes
<= extlen
+ keylen
))
1620 if (c
->binary_header
.response
.opcode
== PROTOCOL_BINARY_CMD_GET
) {
1621 c
->currcmd
.retstat
= MCD_END
;
1622 c
->curr_task
.get_miss
= true;
1627 ms_reset_conn(c
, false);
1632 ms_mlget_task_item_t
*mlget_item
= NULL
;
1633 if (((ms_setting
.mult_key_num
> 1) && (c
->mlget_task
.mlget_num
>= ms_setting
.mult_key_num
))
1634 || ((c
->remain_exec_num
== 0) && (c
->mlget_task
.mlget_num
> 0)))
1636 c
->mlget_task
.value_index
++;
1637 mlget_item
= &c
->mlget_task
.mlget_item
[c
->mlget_task
.value_index
];
1639 c
->curr_task
.item
= mlget_item
->item
;
1640 c
->curr_task
.verify
= mlget_item
->verify
;
1641 c
->curr_task
.finish_verify
= mlget_item
->finish_verify
;
1642 mlget_item
->get_miss
= false;
1645 ms_verify_value(c
, mlget_item
, c
->rcurr
+ extlen
+ keylen
, c
->rvbytes
- extlen
- keylen
);
1647 c
->currcmd
.retstat
= MCD_END
;
1648 c
->curr_task
.get_miss
= false;
1649 c
->rbytes
-= c
->rvbytes
;
1650 c
->rcurr
= c
->rcurr
+ c
->rvbytes
;
1651 assert(c
->rcurr
<= (c
->rbuf
+ c
->rsize
));
1655 if (ms_setting
.mult_key_num
> 1) {
1656 /* multi-get have check all the item */
1657 if (c
->mlget_task
.value_index
== c
->mlget_task
.mlget_num
- 1) {
1658 ms_reset_conn(c
, false);
1662 ms_reset_conn(c
, false);
1664 } /* ms_bin_complete_nread */
1667 * we get here after reading the value of get commands.
1669 * @param c, pointer of the concurrency
1671 static void ms_complete_nread(ms_conn_t
*c
) {
1673 assert(c
->rbytes
>= c
->rvbytes
);
1674 assert(c
->protocol
== ascii_prot
|| c
->protocol
== binary_prot
);
1676 if (c
->protocol
== binary_prot
) {
1677 ms_bin_complete_nread(c
);
1679 ms_ascii_complete_nread(c
);
1681 } /* ms_complete_nread */
1684 * Adds a message header to a connection.
1686 * @param c, pointer of the concurrency
1688 * @return int, if success, return EXIT_SUCCESS, else return -1
1690 static int ms_add_msghdr(ms_conn_t
*c
) {
1695 if (c
->msgsize
== c
->msgused
) {
1696 msg
= realloc(c
->msglist
, (size_t) c
->msgsize
* 2 * sizeof(struct msghdr
));
1704 msg
= c
->msglist
+ c
->msgused
;
1707 * this wipes msg_iovlen, msg_control, msg_controllen, and
1708 * msg_flags, the last 3 of which aren't defined on solaris:
1710 memset(msg
, 0, sizeof(struct msghdr
));
1712 msg
->msg_iov
= &c
->iov
[c
->iovused
];
1714 if (c
->udp
&& (c
->srv_recv_addr_size
> 0)) {
1715 msg
->msg_name
= &c
->srv_recv_addr
;
1716 msg
->msg_namelen
= c
->srv_recv_addr_size
;
1723 /* Leave room for the UDP header, which we'll fill in later. */
1724 return ms_add_iov(c
, NULL
, UDP_HEADER_SIZE
);
1727 return EXIT_SUCCESS
;
1728 } /* ms_add_msghdr */
1731 * Ensures that there is room for another structure iovec in a connection's
1734 * @param c, pointer of the concurrency
1736 * @return int, if success, return EXIT_SUCCESS, else return -1
1738 static int ms_ensure_iov_space(ms_conn_t
*c
) {
1741 if (c
->iovused
>= c
->iovsize
) {
1743 struct iovec
*new_iov
=
1744 (struct iovec
*) realloc(c
->iov
, ((size_t) c
->iovsize
* 2) * sizeof(struct iovec
));
1751 /* Point all the msghdr structures at the new list. */
1752 for (i
= 0, iovnum
= 0; i
< c
->msgused
; i
++) {
1753 c
->msglist
[i
].msg_iov
= &c
->iov
[iovnum
];
1754 iovnum
+= (int) c
->msglist
[i
].msg_iovlen
;
1758 return EXIT_SUCCESS
;
1759 } /* ms_ensure_iov_space */
1762 * Adds data to the list of pending data that will be written out to a
1765 * @param c, pointer of the concurrency
1766 * @param buf, the buffer includes data to send
1767 * @param len, the data length in the buffer
1769 * @return int, if success, return EXIT_SUCCESS, else return -1
1771 static int ms_add_iov(ms_conn_t
*c
, const void *buf
, int len
) {
1779 m
= &c
->msglist
[c
->msgused
- 1];
1782 * Limit UDP packets, to UDP_MAX_PAYLOAD_SIZE bytes.
1784 limit_to_mtu
= c
->udp
;
1787 /* We may need to start a new msghdr if this one is full. */
1788 if ((m
->msg_iovlen
== IOV_MAX
) || (limit_to_mtu
&& (c
->msgbytes
>= UDP_MAX_SEND_PAYLOAD_SIZE
)))
1791 m
= &c
->msglist
[c
->msgused
- 1];
1795 if (ms_ensure_iov_space(c
))
1798 /* If the fragment is too big to fit in the datagram, split it up */
1799 if (limit_to_mtu
&& (len
+ c
->msgbytes
> UDP_MAX_SEND_PAYLOAD_SIZE
)) {
1800 leftover
= len
+ c
->msgbytes
- UDP_MAX_SEND_PAYLOAD_SIZE
;
1806 m
= &c
->msglist
[c
->msgused
- 1];
1807 m
->msg_iov
[m
->msg_iovlen
].iov_base
= (void *) buf
;
1808 m
->msg_iov
[m
->msg_iovlen
].iov_len
= (size_t) len
;
1814 buf
= ((char *) buf
) + len
;
1816 } while (leftover
> 0);
1818 return EXIT_SUCCESS
;
1822 * Constructs a set of UDP headers and attaches them to the outgoing messages.
1824 * @param c, pointer of the concurrency
1826 * @return int, if success, return EXIT_SUCCESS, else return -1
1828 static int ms_build_udp_headers(ms_conn_t
*c
) {
1834 c
->request_id
= ms_get_udp_request_id();
1836 if (c
->msgused
> c
->hdrsize
) {
1839 new_hdrbuf
= realloc(c
->hdrbuf
, (size_t) c
->msgused
* 2 * UDP_HEADER_SIZE
);
1841 new_hdrbuf
= malloc((size_t) c
->msgused
* 2 * UDP_HEADER_SIZE
);
1845 c
->hdrbuf
= (unsigned char *) new_hdrbuf
;
1846 c
->hdrsize
= c
->msgused
* 2;
1849 /* If this is a multi-packet request, drop it. */
1850 if (c
->udp
&& (c
->msgused
> 1)) {
1851 fprintf(stderr
, "multi-packet request for UDP not supported.\n");
1856 for (i
= 0; i
< c
->msgused
; i
++) {
1857 c
->msglist
[i
].msg_iov
[0].iov_base
= (void *) hdr
;
1858 c
->msglist
[i
].msg_iov
[0].iov_len
= UDP_HEADER_SIZE
;
1859 *hdr
++ = (unsigned char) (c
->request_id
/ 256);
1860 *hdr
++ = (unsigned char) (c
->request_id
% 256);
1861 *hdr
++ = (unsigned char) (i
/ 256);
1862 *hdr
++ = (unsigned char) (i
% 256);
1863 *hdr
++ = (unsigned char) (c
->msgused
/ 256);
1864 *hdr
++ = (unsigned char) (c
->msgused
% 256);
1865 *hdr
++ = (unsigned char) 1; /* support facebook memcached */
1866 *hdr
++ = (unsigned char) 0;
1867 assert(hdr
== ((unsigned char *) c
->msglist
[i
].msg_iov
[0].iov_base
+ UDP_HEADER_SIZE
));
1870 return EXIT_SUCCESS
;
1871 } /* ms_build_udp_headers */
1874 * Transmit the next chunk of data from our list of msgbuf structures.
1876 * @param c, pointer of the concurrency
1878 * @return TRANSMIT_COMPLETE All done writing.
1879 * TRANSMIT_INCOMPLETE More data remaining to write.
1880 * TRANSMIT_SOFT_ERROR Can't write any more right now.
1881 * TRANSMIT_HARD_ERROR Can't write (c->state is set to conn_closing)
1883 static int ms_transmit(ms_conn_t
*c
) {
1886 if ((c
->msgcurr
< c
->msgused
) && (c
->msglist
[c
->msgcurr
].msg_iovlen
== 0)) {
1887 /* Finished writing the current msg; advance to the next. */
1891 if (c
->msgcurr
< c
->msgused
) {
1893 struct msghdr
*m
= &c
->msglist
[c
->msgcurr
];
1895 res
= sendmsg(c
->sfd
, m
, 0);
1897 atomic_add_size(&ms_stats
.bytes_written
, res
);
1899 /* We've written some of the data. Remove the completed
1900 * iovec entries from the list of pending writes. */
1901 while (m
->msg_iovlen
> 0 && res
>= (ssize_t
) m
->msg_iov
->iov_len
) {
1902 res
-= (ssize_t
) m
->msg_iov
->iov_len
;
1907 /* Might have written just part of the last iovec entry;
1908 * adjust it so the next write will do the rest. */
1910 m
->msg_iov
->iov_base
= (void *) ((unsigned char *) m
->msg_iov
->iov_base
+ res
);
1911 m
->msg_iov
->iov_len
-= (size_t) res
;
1913 return TRANSMIT_INCOMPLETE
;
1915 if ((res
== -1) && ((errno
== EAGAIN
) || (errno
== EWOULDBLOCK
))) {
1916 if (!ms_update_event(c
, EV_WRITE
| EV_PERSIST
)) {
1917 fprintf(stderr
, "Couldn't update event.\n");
1918 ms_conn_set_state(c
, conn_closing
);
1919 return TRANSMIT_HARD_ERROR
;
1921 return TRANSMIT_SOFT_ERROR
;
1924 /* if res==0 or res==-1 and error is not EAGAIN or EWOULDBLOCK,
1925 * we have a real error, on which we close the connection */
1926 fprintf(stderr
, "Failed to write, and not due to blocking.\n");
1928 ms_conn_set_state(c
, conn_closing
);
1929 return TRANSMIT_HARD_ERROR
;
1931 return TRANSMIT_COMPLETE
;
1936 * Shrinks a connection's buffers if they're too big. This prevents
1937 * periodic large "mget" response from server chewing lots of client
1940 * This should only be called in between requests since it can wipe output
1943 * @param c, pointer of the concurrency
1945 static void ms_conn_shrink(ms_conn_t
*c
) {
1951 if ((c
->rsize
> READ_BUFFER_HIGHWAT
) && (c
->rbytes
< DATA_BUFFER_SIZE
)) {
1954 if (c
->rcurr
!= c
->rbuf
)
1955 memmove(c
->rbuf
, c
->rcurr
, (size_t) c
->rbytes
);
1957 newbuf
= (char *) realloc((void *) c
->rbuf
, DATA_BUFFER_SIZE
);
1961 c
->rsize
= DATA_BUFFER_SIZE
;
1966 if (c
->udp
&& (c
->rudpsize
> UDP_DATA_BUFFER_HIGHWAT
)
1967 && (c
->rudpbytes
+ UDP_MAX_PAYLOAD_SIZE
< UDP_DATA_BUFFER_SIZE
))
1969 char *new_rbuf
= (char *) realloc(c
->rudpbuf
, (size_t) c
->rudpsize
* 2);
1971 c
->rudpbuf
= new_rbuf
;
1972 c
->rudpsize
= UDP_DATA_BUFFER_SIZE
;
1974 /* TODO check error condition? */
1977 if (c
->msgsize
> MSG_LIST_HIGHWAT
) {
1978 struct msghdr
*newbuf
=
1979 (struct msghdr
*) realloc((void *) c
->msglist
, MSG_LIST_INITIAL
* sizeof(c
->msglist
[0]));
1981 c
->msglist
= newbuf
;
1982 c
->msgsize
= MSG_LIST_INITIAL
;
1984 /* TODO check error condition? */
1987 if (c
->iovsize
> IOV_LIST_HIGHWAT
) {
1988 struct iovec
*newbuf
=
1989 (struct iovec
*) realloc((void *) c
->iov
, IOV_LIST_INITIAL
* sizeof(c
->iov
[0]));
1992 c
->iovsize
= IOV_LIST_INITIAL
;
1994 /* TODO check return value */
1996 } /* ms_conn_shrink */
1999 * Sets a connection's current state in the state machine. Any special
2000 * processing that needs to happen on certain state transitions can
2003 * @param c, pointer of the concurrency
2004 * @param state, connection state
2006 static void ms_conn_set_state(ms_conn_t
*c
, int state
) {
2009 if (state
!= c
->state
) {
2010 if (state
== conn_read
) {
2015 } /* ms_conn_set_state */
2018 * update the event if socks change state. for example: when
2019 * change the listen scoket read event to sock write event, or
2020 * change socket handler, we could call this function.
2022 * @param c, pointer of the concurrency
2023 * @param new_flags, new event flags
2025 * @return bool, if success, return true, else return false
2027 static bool ms_update_event(ms_conn_t
*c
, const int new_flags
) {
2030 struct event_base
*base
= c
->event
.ev_base
;
2031 if ((c
->ev_flags
== new_flags
) && (ms_setting
.rep_write_srv
== 0)
2032 && (!ms_setting
.facebook_test
|| (c
->total_sfds
== 1)))
2037 if (event_del(&c
->event
) == -1) {
2038 /* try to delete the event again */
2039 if (event_del(&c
->event
) == -1) {
2044 event_set(&c
->event
, c
->sfd
, (short) new_flags
, ms_event_handler
, (void *) c
);
2045 event_base_set(base
, &c
->event
);
2046 c
->ev_flags
= (short) new_flags
;
2048 if (event_add(&c
->event
, NULL
) == -1) {
2053 } /* ms_update_event */
2056 * If user want to get the expected throughput, we could limit
2057 * the performance of memslap. we could give up some work and
2058 * just wait a short time. The function is used to check this
2061 * @param c, pointer of the concurrency
2063 * @return bool, if success, return true, else return false
2065 static bool ms_need_yield(ms_conn_t
*c
) {
2066 ms_thread_t
*ms_thread
= pthread_getspecific(ms_thread_key
);
2068 int64_t time_diff
= 0;
2069 struct timeval curr_time
;
2070 ms_task_t
*task
= &c
->curr_task
;
2072 if (ms_setting
.expected_tps
> 0) {
2073 gettimeofday(&curr_time
, NULL
);
2074 time_diff
= ms_time_diff(&ms_thread
->startup_time
, &curr_time
);
2075 tps
= (int64_t)(((task
->get_opt
+ task
->set_opt
) / (uint64_t) time_diff
) * 1000000);
2077 /* current throughput is greater than expected throughput */
2078 if (tps
> ms_thread
->thread_ctx
->tps_perconn
) {
2084 } /* ms_need_yield */
2087 * used to update the start time of each operation
2089 * @param c, pointer of the concurrency
2091 static void ms_update_start_time(ms_conn_t
*c
) {
2092 ms_task_item_t
*item
= c
->curr_task
.item
;
2094 if ((ms_setting
.stat_freq
> 0) || c
->udp
|| ((c
->currcmd
.cmd
== CMD_SET
) && (item
->exp_time
> 0)))
2096 gettimeofday(&c
->start_time
, NULL
);
2097 if ((c
->currcmd
.cmd
== CMD_SET
) && (item
->exp_time
> 0)) {
2098 /* record the current time */
2099 item
->client_time
= c
->start_time
.tv_sec
;
2102 } /* ms_update_start_time */
2105 * run the state machine
2107 * @param c, pointer of the concurrency
2109 static void ms_drive_machine(ms_conn_t
*c
) {
2118 if (c
->rbytes
>= c
->rvbytes
) {
2119 ms_complete_nread(c
);
2123 if (ms_try_read_line(c
)) {
2128 if (ms_try_read_network(c
)) {
2132 /* doesn't read all the response data, wait event wake up */
2133 if (!c
->currcmd
.isfinish
) {
2134 if (!ms_update_event(c
, EV_READ
| EV_PERSIST
)) {
2135 fprintf(stderr
, "Couldn't update event.\n");
2136 ms_conn_set_state(c
, conn_closing
);
2143 /* we have no command line and no data to read from network, next write */
2144 ms_conn_set_state(c
, conn_write
);
2145 memcpy(&c
->precmd
, &c
->currcmd
, sizeof(ms_cmdstat_t
)); /* replicate command state */
2150 if (!c
->ctnwrite
&& ms_need_yield(c
)) {
2153 if (!ms_update_event(c
, EV_WRITE
| EV_PERSIST
)) {
2154 fprintf(stderr
, "Couldn't update event.\n");
2155 ms_conn_set_state(c
, conn_closing
);
2162 if (!c
->ctnwrite
&& (ms_exec_task(c
))) {
2163 ms_conn_set_state(c
, conn_closing
);
2167 /* record the start time before starting to send data if necessary */
2168 if (!c
->ctnwrite
|| (c
->change_sfd
&& c
->ctnwrite
)) {
2169 if (c
->change_sfd
) {
2170 c
->change_sfd
= false;
2172 ms_update_start_time(c
);
2175 /* change sfd if necessary */
2176 if (c
->change_sfd
) {
2182 /* execute task until nothing need be written to network */
2183 if (!c
->ctnwrite
&& (c
->msgcurr
== c
->msgused
)) {
2184 if (!ms_update_event(c
, EV_WRITE
| EV_PERSIST
)) {
2185 fprintf(stderr
, "Couldn't update event.\n");
2186 ms_conn_set_state(c
, conn_closing
);
2193 switch (ms_transmit(c
)) {
2194 case TRANSMIT_COMPLETE
:
2195 /* we have no data to write to network, next wait repose */
2196 if (!ms_update_event(c
, EV_READ
| EV_PERSIST
)) {
2197 fprintf(stderr
, "Couldn't update event.\n");
2198 ms_conn_set_state(c
, conn_closing
);
2199 c
->ctnwrite
= false;
2202 ms_conn_set_state(c
, conn_read
);
2203 c
->ctnwrite
= false;
2207 case TRANSMIT_INCOMPLETE
:
2209 break; /* Continue in state machine. */
2211 case TRANSMIT_HARD_ERROR
:
2212 c
->ctnwrite
= false;
2215 case TRANSMIT_SOFT_ERROR
:
2227 /* recovery mode, need reconnect if connection close */
2228 if (ms_setting
.reconnect
2229 && (!ms_global
.time_out
|| ((ms_setting
.run_time
== 0) && (c
->remain_exec_num
> 0))))
2237 ms_reset_conn(c
, false);
2239 if (c
->total_sfds
== 1) {
2240 if (!ms_update_event(c
, EV_WRITE
| EV_PERSIST
)) {
2241 fprintf(stderr
, "Couldn't update event.\n");
2242 ms_conn_set_state(c
, conn_closing
);
2258 } /* ms_drive_machine */
2261 * the event handler of each thread
2263 * @param fd, the file descriptor of socket
2264 * @param which, event flag
2265 * @param arg, argument
2267 void ms_event_handler(const int fd
, const short which
, void *arg
) {
2268 ms_conn_t
*c
= (ms_conn_t
*) arg
;
2276 fprintf(stderr
, "Catastrophic: event fd: %d doesn't match conn fd: %d\n", fd
, c
->sfd
);
2280 assert(fd
== c
->sfd
);
2282 ms_drive_machine(c
);
2284 /* wait for next event */
2285 } /* ms_event_handler */
2288 * get the next socket descriptor index to run for replication
2290 * @param c, pointer of the concurrency
2291 * @param cmd, command(get or set )
2293 * @return int, if success, return the index, else return EXIT_SUCCESS
2295 static uint32_t ms_get_rep_sock_index(ms_conn_t
*c
, int cmd
) {
2296 uint32_t sock_index
= 0;
2299 if (c
->total_sfds
== 1) {
2300 return EXIT_SUCCESS
;
2303 if (ms_setting
.rep_write_srv
== 0) {
2308 if (cmd
== CMD_SET
) {
2309 for (i
= 0; i
< ms_setting
.rep_write_srv
; i
++) {
2310 if (c
->tcpsfd
[i
] > 0) {
2315 if (i
== ms_setting
.rep_write_srv
) {
2316 /* random get one replication server to read */
2317 sock_index
= (uint32_t) random() % c
->total_sfds
;
2319 /* random get one replication writing server to write */
2320 sock_index
= (uint32_t) random() % ms_setting
.rep_write_srv
;
2322 } else if (cmd
== CMD_GET
) {
2323 /* random get one replication server to read */
2324 sock_index
= (uint32_t) random() % c
->total_sfds
;
2326 } while (c
->tcpsfd
[sock_index
] == 0);
2329 } /* ms_get_rep_sock_index */
2332 * get the next socket descriptor index to run
2334 * @param c, pointer of the concurrency
2336 * @return int, return the index
2338 static uint32_t ms_get_next_sock_index(ms_conn_t
*c
) {
2339 uint32_t sock_index
= 0;
2342 sock_index
= (++c
->cur_idx
== c
->total_sfds
) ? 0 : c
->cur_idx
;
2343 } while (c
->tcpsfd
[sock_index
] == 0);
2346 } /* ms_get_next_sock_index */
2349 * update socket event of the connections
2351 * @param c, pointer of the concurrency
2353 * @return int, if success, return EXIT_SUCCESS, else return -1
2355 static int ms_update_conn_sock_event(ms_conn_t
*c
) {
2358 switch (c
->currcmd
.cmd
) {
2360 if (ms_setting
.facebook_test
&& c
->udp
) {
2361 c
->sfd
= c
->tcpsfd
[0];
2363 c
->change_sfd
= true;
2368 if (ms_setting
.facebook_test
&& !c
->udp
) {
2371 c
->change_sfd
= true;
2379 if (!c
->udp
&& (c
->total_sfds
> 1)) {
2380 if (c
->cur_idx
!= c
->total_sfds
) {
2381 if (ms_setting
.rep_write_srv
== 0) {
2382 c
->cur_idx
= ms_get_next_sock_index(c
);
2384 c
->cur_idx
= ms_get_rep_sock_index(c
, c
->currcmd
.cmd
);
2387 /* must select the first sock of the connection at the beginning */
2391 c
->sfd
= c
->tcpsfd
[c
->cur_idx
];
2393 c
->change_sfd
= true;
2396 if (c
->change_sfd
) {
2397 if (!ms_update_event(c
, EV_WRITE
| EV_PERSIST
)) {
2398 fprintf(stderr
, "Couldn't update event.\n");
2399 ms_conn_set_state(c
, conn_closing
);
2404 return EXIT_SUCCESS
;
2405 } /* ms_update_conn_sock_event */
2408 * for ASCII protocol, this function build the set command
2409 * string and send the command.
2411 * @param c, pointer of the concurrency
2412 * @param item, pointer of task item which includes the object
2415 * @return int, if success, return EXIT_SUCCESS, else return -1
2417 static int ms_build_ascii_write_buf_set(ms_conn_t
*c
, ms_task_item_t
*item
) {
2420 char *buffer
= c
->wbuf
;
2422 write_len
= snprintf(buffer
, c
->wsize
, " %u %d %d\r\n", 0, item
->exp_time
, item
->value_size
);
2424 if (write_len
> c
->wsize
|| write_len
< 0) {
2425 /* ought to be always enough. just fail for simplicity */
2426 fprintf(stderr
, "output command line too long.\n");
2430 if (item
->value_offset
== INVALID_OFFSET
) {
2431 value_offset
= item
->key_suffix_offset
;
2433 value_offset
= item
->value_offset
;
2436 if ((ms_add_iov(c
, "set ", 4))
2437 || (ms_add_iov(c
, (char *) &item
->key_prefix
, (int) KEY_PREFIX_SIZE
))
2438 || (ms_add_iov(c
, &ms_setting
.char_block
[item
->key_suffix_offset
],
2439 item
->key_size
- (int) KEY_PREFIX_SIZE
)
2441 || (ms_add_iov(c
, buffer
, write_len
))
2442 || (ms_add_iov(c
, &ms_setting
.char_block
[value_offset
], item
->value_size
))
2443 || (ms_add_iov(c
, "\r\n", 2)) || (c
->udp
&& (ms_build_udp_headers(c
))))
2448 return EXIT_SUCCESS
;
2449 } /* ms_build_ascii_write_buf_set */
2452 * used to send set command to server
2454 * @param c, pointer of the concurrency
2455 * @param item, pointer of task item which includes the object
2458 * @return int, if success, return EXIT_SUCCESS, else return -1
2460 int ms_mcd_set(ms_conn_t
*c
, ms_task_item_t
*item
) {
2463 c
->currcmd
.cmd
= CMD_SET
;
2464 c
->currcmd
.isfinish
= false;
2465 c
->currcmd
.retstat
= MCD_FAILURE
;
2467 if (ms_update_conn_sock_event(c
)) {
2474 if (ms_add_msghdr(c
)) {
2475 fprintf(stderr
, "Out of memory preparing request.");
2479 /* binary protocol */
2480 if (c
->protocol
== binary_prot
) {
2481 if (ms_build_bin_write_buf_set(c
, item
)) {
2485 if (ms_build_ascii_write_buf_set(c
, item
)) {
2490 atomic_add_size(&ms_stats
.obj_bytes
, item
->key_size
+ item
->value_size
);
2491 atomic_add_size(&ms_stats
.cmd_set
, 1);
2493 return EXIT_SUCCESS
;
2497 * for ASCII protocol, this function build the get command
2498 * string and send the command.
2500 * @param c, pointer of the concurrency
2501 * @param item, pointer of task item which includes the object
2504 * @return int, if success, return EXIT_SUCCESS, else return -1
2506 static int ms_build_ascii_write_buf_get(ms_conn_t
*c
, ms_task_item_t
*item
) {
2507 if ((ms_add_iov(c
, "get ", 4))
2508 || (ms_add_iov(c
, (char *) &item
->key_prefix
, (int) KEY_PREFIX_SIZE
))
2509 || (ms_add_iov(c
, &ms_setting
.char_block
[item
->key_suffix_offset
],
2510 item
->key_size
- (int) KEY_PREFIX_SIZE
)
2512 || (ms_add_iov(c
, "\r\n", 2)) || (c
->udp
&& (ms_build_udp_headers(c
))))
2517 return EXIT_SUCCESS
;
2518 } /* ms_build_ascii_write_buf_get */
2521 * used to send the get command to server
2523 * @param c, pointer of the concurrency
2524 * @param item, pointer of task item which includes the object
2527 * @return int, if success, return EXIT_SUCCESS, else return -1
2529 int ms_mcd_get(ms_conn_t
*c
, ms_task_item_t
*item
) {
2532 c
->currcmd
.cmd
= CMD_GET
;
2533 c
->currcmd
.isfinish
= false;
2534 c
->currcmd
.retstat
= MCD_FAILURE
;
2536 if (ms_update_conn_sock_event(c
)) {
2543 if (ms_add_msghdr(c
)) {
2544 fprintf(stderr
, "Out of memory preparing request.");
2548 /* binary protocol */
2549 if (c
->protocol
== binary_prot
) {
2550 if (ms_build_bin_write_buf_get(c
, item
)) {
2554 if (ms_build_ascii_write_buf_get(c
, item
)) {
2559 atomic_add_size(&ms_stats
.cmd_get
, 1);
2561 return EXIT_SUCCESS
;
2565 * for ASCII protocol, this function build the multi-get command
2566 * string and send the command.
2568 * @param c, pointer of the concurrency
2570 * @return int, if success, return EXIT_SUCCESS, else return -1
2572 static int ms_build_ascii_write_buf_mlget(ms_conn_t
*c
) {
2573 ms_task_item_t
*item
;
2575 if (ms_add_iov(c
, "get", 3)) {
2579 for (int i
= 0; i
< c
->mlget_task
.mlget_num
; i
++) {
2580 item
= c
->mlget_task
.mlget_item
[i
].item
;
2582 if ((ms_add_iov(c
, " ", 1))
2583 || (ms_add_iov(c
, (char *) &item
->key_prefix
, (int) KEY_PREFIX_SIZE
))
2584 || (ms_add_iov(c
, &ms_setting
.char_block
[item
->key_suffix_offset
],
2585 item
->key_size
- (int) KEY_PREFIX_SIZE
)
2592 if ((ms_add_iov(c
, "\r\n", 2)) || (c
->udp
&& (ms_build_udp_headers(c
)))) {
2596 return EXIT_SUCCESS
;
2597 } /* ms_build_ascii_write_buf_mlget */
2600 * used to send the multi-get command to server
2602 * @param c, pointer of the concurrency
2604 * @return int, if success, return EXIT_SUCCESS, else return -1
2606 int ms_mcd_mlget(ms_conn_t
*c
) {
2607 ms_task_item_t
*item
;
2610 assert(c
->mlget_task
.mlget_num
>= 1);
2612 c
->currcmd
.cmd
= CMD_GET
;
2613 c
->currcmd
.isfinish
= false;
2614 c
->currcmd
.retstat
= MCD_FAILURE
;
2616 if (ms_update_conn_sock_event(c
)) {
2623 if (ms_add_msghdr(c
)) {
2624 fprintf(stderr
, "Out of memory preparing request.");
2628 /* binary protocol */
2629 if (c
->protocol
== binary_prot
) {
2630 if (ms_build_bin_write_buf_mlget(c
)) {
2634 if (ms_build_ascii_write_buf_mlget(c
)) {
2639 /* decrease operation time of each item */
2640 for (int i
= 0; i
< c
->mlget_task
.mlget_num
; i
++) {
2641 item
= c
->mlget_task
.mlget_item
[i
].item
;
2642 atomic_add_size(&ms_stats
.cmd_get
, 1);
2647 return EXIT_SUCCESS
;
2648 } /* ms_mcd_mlget */
2651 * binary protocol support
2655 * for binary protocol, parse the response of server
2657 * @param c, pointer of the concurrency
2659 * @return int, if success, return EXIT_SUCCESS, else return -1
2661 static int ms_bin_process_response(ms_conn_t
*c
) {
2662 const char *errstr
= NULL
;
2666 uint32_t bodylen
= c
->binary_header
.response
.bodylen
;
2667 uint8_t opcode
= c
->binary_header
.response
.opcode
;
2668 uint16_t status
= c
->binary_header
.response
.status
;
2671 c
->rvbytes
= (int32_t) bodylen
;
2673 return EXIT_FAILURE
;
2676 case PROTOCOL_BINARY_RESPONSE_SUCCESS
:
2677 if (opcode
== PROTOCOL_BINARY_CMD_SET
) {
2678 c
->currcmd
.retstat
= MCD_STORED
;
2679 } else if (opcode
== PROTOCOL_BINARY_CMD_DELETE
) {
2680 c
->currcmd
.retstat
= MCD_DELETED
;
2681 } else if (opcode
== PROTOCOL_BINARY_CMD_GET
) {
2682 c
->currcmd
.retstat
= MCD_END
;
2686 case PROTOCOL_BINARY_RESPONSE_ENOMEM
:
2687 errstr
= "Out of memory";
2688 c
->currcmd
.retstat
= MCD_SERVER_ERROR
;
2691 case PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND
:
2692 errstr
= "Unknown command";
2693 c
->currcmd
.retstat
= MCD_UNKNOWN_READ_FAILURE
;
2696 case PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
:
2697 errstr
= "Not found";
2698 c
->currcmd
.retstat
= MCD_NOTFOUND
;
2701 case PROTOCOL_BINARY_RESPONSE_EINVAL
:
2702 errstr
= "Invalid arguments";
2703 c
->currcmd
.retstat
= MCD_PROTOCOL_ERROR
;
2706 case PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
:
2707 errstr
= "Data exists for key.";
2710 case PROTOCOL_BINARY_RESPONSE_E2BIG
:
2711 errstr
= "Too large.";
2712 c
->currcmd
.retstat
= MCD_SERVER_ERROR
;
2715 case PROTOCOL_BINARY_RESPONSE_NOT_STORED
:
2716 errstr
= "Not stored.";
2717 c
->currcmd
.retstat
= MCD_NOTSTORED
;
2721 errstr
= "Unknown error";
2722 c
->currcmd
.retstat
= MCD_UNKNOWN_READ_FAILURE
;
2727 fprintf(stderr
, "%s\n", errstr
);
2731 return EXIT_SUCCESS
;
2732 } /* ms_bin_process_response */
2734 /* build binary header and add the header to the buffer to send */
2737 * build binary header and add the header to the buffer to send
2739 * @param c, pointer of the concurrency
2740 * @param opcode, operation code
2741 * @param hdr_len, length of header
2742 * @param key_len, length of key
2743 * @param body_len. length of body
2745 static void ms_add_bin_header(ms_conn_t
*c
, uint8_t opcode
, uint8_t hdr_len
, uint16_t key_len
,
2746 uint32_t body_len
) {
2747 protocol_binary_request_header
*header
;
2751 header
= (protocol_binary_request_header
*) c
->wcurr
;
2753 header
->request
.magic
= (uint8_t) PROTOCOL_BINARY_REQ
;
2754 header
->request
.opcode
= (uint8_t) opcode
;
2755 header
->request
.keylen
= htons(key_len
);
2757 header
->request
.extlen
= (uint8_t) hdr_len
;
2758 header
->request
.datatype
= (uint8_t) PROTOCOL_BINARY_RAW_BYTES
;
2759 header
->request
.vbucket
= 0;
2761 header
->request
.bodylen
= htonl(body_len
);
2762 header
->request
.opaque
= 0;
2763 header
->request
.cas
= 0;
2765 ms_add_iov(c
, c
->wcurr
, sizeof(header
->request
));
2766 } /* ms_add_bin_header */
2769 * add the key to the socket write buffer array
2771 * @param c, pointer of the concurrency
2772 * @param item, pointer of task item which includes the object
2775 static void ms_add_key_to_iov(ms_conn_t
*c
, ms_task_item_t
*item
) {
2776 ms_add_iov(c
, (char *) &item
->key_prefix
, (int) KEY_PREFIX_SIZE
);
2777 ms_add_iov(c
, &ms_setting
.char_block
[item
->key_suffix_offset
],
2778 item
->key_size
- (int) KEY_PREFIX_SIZE
);
2782 * for binary protocol, this function build the set command
2783 * and add the command to send buffer array.
2785 * @param c, pointer of the concurrency
2786 * @param item, pointer of task item which includes the object
2789 * @return int, if success, return EXIT_SUCCESS, else return -1
2791 static int ms_build_bin_write_buf_set(ms_conn_t
*c
, ms_task_item_t
*item
) {
2792 assert(c
->wbuf
== c
->wcurr
);
2795 protocol_binary_request_set
*rep
= (protocol_binary_request_set
*) c
->wcurr
;
2796 uint16_t keylen
= (uint16_t) item
->key_size
;
2798 (uint32_t) sizeof(rep
->message
.body
) + (uint32_t) keylen
+ (uint32_t) item
->value_size
;
2800 ms_add_bin_header(c
, PROTOCOL_BINARY_CMD_SET
, sizeof(rep
->message
.body
), keylen
, bodylen
);
2801 rep
->message
.body
.flags
= 0;
2802 rep
->message
.body
.expiration
= htonl((uint32_t) item
->exp_time
);
2803 ms_add_iov(c
, &rep
->message
.body
, sizeof(rep
->message
.body
));
2804 ms_add_key_to_iov(c
, item
);
2806 if (item
->value_offset
== INVALID_OFFSET
) {
2807 value_offset
= item
->key_suffix_offset
;
2809 value_offset
= item
->value_offset
;
2811 ms_add_iov(c
, &ms_setting
.char_block
[value_offset
], item
->value_size
);
2813 return EXIT_SUCCESS
;
2814 } /* ms_build_bin_write_buf_set */
2817 * for binary protocol, this function build the get command and
2818 * add the command to send buffer array.
2820 * @param c, pointer of the concurrency
2821 * @param item, pointer of task item which includes the object
2824 * @return int, if success, return EXIT_SUCCESS, else return -1
2826 static int ms_build_bin_write_buf_get(ms_conn_t
*c
, ms_task_item_t
*item
) {
2827 assert(c
->wbuf
== c
->wcurr
);
2829 ms_add_bin_header(c
, PROTOCOL_BINARY_CMD_GET
, 0, (uint16_t) item
->key_size
,
2830 (uint32_t) item
->key_size
);
2831 ms_add_key_to_iov(c
, item
);
2833 return EXIT_SUCCESS
;
2834 } /* ms_build_bin_write_buf_get */
2837 * for binary protocol, this function build the multi-get
2838 * command and add the command to send buffer array.
2840 * @param c, pointer of the concurrency
2841 * @param item, pointer of task item which includes the object
2844 * @return int, if success, return EXIT_SUCCESS, else return -1
2846 static int ms_build_bin_write_buf_mlget(ms_conn_t
*c
) {
2847 ms_task_item_t
*item
;
2849 assert(c
->wbuf
== c
->wcurr
);
2851 for (int i
= 0; i
< c
->mlget_task
.mlget_num
; i
++) {
2852 item
= c
->mlget_task
.mlget_item
[i
].item
;
2855 ms_add_bin_header(c
, PROTOCOL_BINARY_CMD_GET
, 0, (uint16_t) item
->key_size
,
2856 (uint32_t) item
->key_size
);
2857 ms_add_key_to_iov(c
, item
);
2858 c
->wcurr
+= sizeof(protocol_binary_request_get
);
2863 return EXIT_SUCCESS
;
2864 } /* ms_build_bin_write_buf_mlget */