2 Basic socket buffered IO
6 #include "memcached_io.h"
7 #include <sys/select.h>
15 static ssize_t
io_flush(memcached_server_st
*ptr
, memcached_return
*error
);
16 static void increment_udp_message_id(memcached_server_st
*ptr
);
18 static memcached_return
io_wait(memcached_server_st
*ptr
,
19 memc_read_or_write read_or_write
)
21 struct pollfd fds
[1]= {
28 unlikely (read_or_write
== MEM_WRITE
) /* write */
29 fds
[0].events
= POLLOUT
;
32 ** We are going to block on write, but at least on Solaris we might block
33 ** on write if we haven't read anything from our input buffer..
34 ** Try to purge the input buffer if we don't do any flow control in the
35 ** application layer (just sending a lot of data etc)
36 ** The test is moved down in the purge function to avoid duplication of
39 if (read_or_write
== MEM_WRITE
)
41 memcached_return rc
= memcached_purge(ptr
);
42 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_STORED
)
43 return MEMCACHED_FAILURE
;
46 int timeout
= ptr
->root
->poll_timeout
;
47 if ((ptr
->root
->flags
& MEM_NO_BLOCK
) == 0)
50 error
= poll(fds
, 1, timeout
);
53 return MEMCACHED_SUCCESS
;
55 return MEMCACHED_TIMEOUT
;
57 /* Imposssible for anything other then -1 */
58 WATCHPOINT_ASSERT(error
== -1);
59 memcached_quit_server(ptr
, 1);
61 return MEMCACHED_FAILURE
;
65 * Try to fill the input buffer for a server with as much
68 * @param ptr the server to pack
70 static bool repack_input_buffer(memcached_server_st
*ptr
)
72 if (ptr
->read_ptr
!= ptr
->read_buffer
)
74 /* Move all of the data to the beginning of the buffer so
75 ** that we can fit more data into the buffer...
77 memmove(ptr
->read_buffer
, ptr
->read_ptr
, ptr
->read_buffer_length
);
78 ptr
->read_ptr
= ptr
->read_buffer
;
79 ptr
->read_data_length
= ptr
->read_buffer_length
;
82 /* There is room in the buffer, try to fill it! */
83 if (ptr
->read_buffer_length
!= MEMCACHED_MAX_BUFFER
)
85 /* Just try a single read to grab what's available */
86 ssize_t nr
= read(ptr
->fd
,
87 ptr
->read_ptr
+ ptr
->read_data_length
,
88 MEMCACHED_MAX_BUFFER
- ptr
->read_data_length
);
92 ptr
->read_data_length
+= (size_t)nr
;
93 ptr
->read_buffer_length
+= (size_t)nr
;
101 * If the we have callbacks connected to this server structure
102 * we may start process the input queue and fire the callbacks
103 * for the incomming messages. This function is _only_ called
104 * when the input buffer is full, so that we _know_ that we have
105 * at least _one_ message to process.
107 * @param ptr the server to star processing iput messages for
108 * @return true if we processed anything, false otherwise
110 static bool process_input_buffer(memcached_server_st
*ptr
)
113 ** We might be able to process some of the response messages if we
114 ** have a callback set up
116 if (ptr
->root
->callbacks
!= NULL
&& (ptr
->root
->flags
& MEM_USE_UDP
) == 0)
119 * We might have responses... try to read them out and fire
122 memcached_callback_st cb
= *ptr
->root
->callbacks
;
124 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
125 memcached_return error
;
126 error
= memcached_response(ptr
, buffer
, sizeof(buffer
),
128 if (error
== MEMCACHED_SUCCESS
)
130 for (int x
= 0; x
< cb
.number_of_callback
; x
++)
132 error
= (*cb
.callback
[x
])(ptr
->root
, &ptr
->root
->result
, cb
.context
);
133 if (error
!= MEMCACHED_SUCCESS
)
137 /* @todo what should I do with the error message??? */
139 /* @todo what should I do with other error messages?? */
147 void memcached_io_preread(memcached_st
*ptr
)
153 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
155 if (memcached_server_response_count(ptr
, x
) &&
156 ptr
->hosts
[x
].read_data_length
< MEMCACHED_MAX_BUFFER
)
160 data_read
= read(ptr
->hosts
[x
].fd
,
161 ptr
->hosts
[x
].read_ptr
+ ptr
->hosts
[x
].read_data_length
,
162 MEMCACHED_MAX_BUFFER
- ptr
->hosts
[x
].read_data_length
);
166 ptr
->hosts
[x
].read_buffer_length
+= data_read
;
167 ptr
->hosts
[x
].read_data_length
+= data_read
;
173 memcached_return
memcached_io_read(memcached_server_st
*ptr
,
174 void *buffer
, size_t length
, ssize_t
*nread
)
182 if (!ptr
->read_buffer_length
)
188 data_read
= read(ptr
->fd
, ptr
->read_buffer
, MEMCACHED_MAX_BUFFER
);
191 else if (data_read
== -1)
193 ptr
->cached_errno
= errno
;
194 memcached_return rc
= MEMCACHED_UNKNOWN_READ_FAILURE
;
199 if ((rc
= io_wait(ptr
, MEM_READ
)) == MEMCACHED_SUCCESS
)
205 memcached_quit_server(ptr
, 1);
214 EOF. Any data received so far is incomplete
215 so discard it. This always reads by byte in case of TCP
216 and protocol enforcement happens at memcached_response()
217 looking for '\n'. We do not care for UDB which requests 8 bytes
218 at once. Generally, this means that connection went away. Since
219 for blocking I/O we do not return 0 and for non-blocking case
220 it will return EGAIN if data is not immediatly available.
222 memcached_quit_server(ptr
, 1);
224 return MEMCACHED_UNKNOWN_READ_FAILURE
;
228 ptr
->io_bytes_sent
= 0;
229 ptr
->read_data_length
= (size_t) data_read
;
230 ptr
->read_buffer_length
= (size_t) data_read
;
231 ptr
->read_ptr
= ptr
->read_buffer
;
238 difference
= (length
> ptr
->read_buffer_length
) ? ptr
->read_buffer_length
: length
;
240 memcpy(buffer_ptr
, ptr
->read_ptr
, difference
);
241 length
-= difference
;
242 ptr
->read_ptr
+= difference
;
243 ptr
->read_buffer_length
-= difference
;
244 buffer_ptr
+= difference
;
248 *buffer_ptr
= *ptr
->read_ptr
;
250 ptr
->read_buffer_length
--;
256 ptr
->server_failure_counter
= 0;
257 *nread
= (ssize_t
)(buffer_ptr
- (char*)buffer
);
258 return MEMCACHED_SUCCESS
;
261 ssize_t
memcached_io_write(memcached_server_st
*ptr
,
262 const void *buffer
, size_t length
, char with_flush
)
264 size_t original_length
;
265 const char* buffer_ptr
;
267 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
269 original_length
= length
;
278 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
280 //UDP does not support partial writes
281 buffer_end
= MAX_UDP_DATAGRAM_LENGTH
;
282 should_write
= length
;
283 if (ptr
->write_buffer_offset
+ should_write
> buffer_end
)
288 buffer_end
= MEMCACHED_MAX_BUFFER
;
289 should_write
= buffer_end
- ptr
->write_buffer_offset
;
290 should_write
= (should_write
< length
) ? should_write
: length
;
293 write_ptr
= ptr
->write_buffer
+ ptr
->write_buffer_offset
;
294 memcpy(write_ptr
, buffer_ptr
, should_write
);
295 ptr
->write_buffer_offset
+= should_write
;
296 buffer_ptr
+= should_write
;
297 length
-= should_write
;
299 if (ptr
->write_buffer_offset
== buffer_end
&& ptr
->type
!= MEMCACHED_CONNECTION_UDP
)
304 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
305 sent_length
= io_flush(ptr
, &rc
);
306 if (sent_length
== -1)
309 /* If io_flush calls memcached_purge, sent_length may be 0 */
310 unlikely (sent_length
!= 0)
312 WATCHPOINT_ASSERT(sent_length
== (ssize_t
)buffer_end
);
320 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
321 if (io_flush(ptr
, &rc
) == -1)
325 return (ssize_t
) original_length
;
328 memcached_return
memcached_io_close(memcached_server_st
*ptr
)
333 return MEMCACHED_SUCCESS
;
335 /* in case of death shutdown to avoid blocking at close() */
338 r
= shutdown(ptr
->fd
, SHUT_RDWR
);
341 if (r
&& errno
!= ENOTCONN
)
343 WATCHPOINT_NUMBER(ptr
->fd
);
344 WATCHPOINT_ERRNO(errno
);
345 WATCHPOINT_ASSERT(errno
);
353 WATCHPOINT_ERRNO(errno
);
356 return MEMCACHED_SUCCESS
;
359 memcached_server_st
*memcached_io_get_readable_server(memcached_st
*memc
)
361 #define MAX_SERVERS_TO_POLL 100
362 struct pollfd fds
[MAX_SERVERS_TO_POLL
];
363 unsigned int host_index
= 0;
365 for (unsigned int x
= 0;
366 x
< memc
->number_of_hosts
&& host_index
< MAX_SERVERS_TO_POLL
;
369 if (memc
->hosts
[x
].read_buffer_length
> 0) /* I have data in the buffer */
370 return &memc
->hosts
[x
];
372 if (memcached_server_response_count(&memc
->hosts
[x
]) > 0)
374 fds
[host_index
].events
= POLLIN
;
375 fds
[host_index
].revents
= 0;
376 fds
[host_index
].fd
= memc
->hosts
[x
].fd
;
383 /* We have 0 or 1 server with pending events.. */
384 for (unsigned int x
= 0; x
< memc
->number_of_hosts
; ++x
)
385 if (memcached_server_response_count(&memc
->hosts
[x
]) > 0)
386 return &memc
->hosts
[x
];
391 int err
= poll(fds
, host_index
, memc
->poll_timeout
);
394 memc
->cached_errno
= errno
;
399 for (unsigned int x
= 0; x
< host_index
; ++x
)
400 if (fds
[x
].revents
& POLLIN
)
401 for (unsigned int y
= 0; y
< memc
->number_of_hosts
; ++y
)
402 if (memc
->hosts
[y
].fd
== fds
[x
].fd
)
403 return &memc
->hosts
[y
];
409 static ssize_t
io_flush(memcached_server_st
*ptr
,
410 memcached_return
*error
)
413 ** We might want to purge the input buffer if we haven't consumed
414 ** any output yet... The test for the limits is the purge is inline
415 ** in the purge function to avoid duplicating the logic..
419 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
420 rc
= memcached_purge(ptr
);
422 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_STORED
)
426 size_t return_length
;
427 char *local_write_ptr
= ptr
->write_buffer
;
428 size_t write_length
= ptr
->write_buffer_offset
;
430 *error
= MEMCACHED_SUCCESS
;
432 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
434 // UDP Sanity check, make sure that we are not sending somthing too big
435 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
&& write_length
> MAX_UDP_DATAGRAM_LENGTH
)
438 if (ptr
->write_buffer_offset
== 0 || (ptr
->type
== MEMCACHED_CONNECTION_UDP
439 && ptr
->write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
))
442 /* Looking for memory overflows */
444 if (write_length
== MEMCACHED_MAX_BUFFER
)
445 WATCHPOINT_ASSERT(ptr
->write_buffer
== local_write_ptr
);
446 WATCHPOINT_ASSERT((ptr
->write_buffer
+ MEMCACHED_MAX_BUFFER
) >= (local_write_ptr
+ write_length
));
452 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
453 WATCHPOINT_ASSERT(write_length
> 0);
455 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
456 increment_udp_message_id(ptr
);
457 sent_length
= write(ptr
->fd
, local_write_ptr
, write_length
);
459 if (sent_length
== -1)
461 ptr
->cached_errno
= errno
;
469 * We may be blocked on write because the input buffer
470 * is full. Let's check if we have room in our input
471 * buffer for more data and retry the write before
474 if (repack_input_buffer(ptr
) ||
475 process_input_buffer(ptr
))
479 rc
= io_wait(ptr
, MEM_WRITE
);
481 if (rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_TIMEOUT
)
484 memcached_quit_server(ptr
, 1);
488 memcached_quit_server(ptr
, 1);
489 *error
= MEMCACHED_ERRNO
;
494 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
&&
495 (size_t)sent_length
!= write_length
)
497 memcached_quit_server(ptr
, 1);
501 ptr
->io_bytes_sent
+= (uint32_t) sent_length
;
503 local_write_ptr
+= sent_length
;
504 write_length
-= (uint32_t) sent_length
;
505 return_length
+= (uint32_t) sent_length
;
508 WATCHPOINT_ASSERT(write_length
== 0);
509 // Need to study this assert() WATCHPOINT_ASSERT(return_length ==
510 // ptr->write_buffer_offset);
512 // if we are a udp server, the begining of the buffer is reserverd for
513 // the upd frame header
514 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
515 ptr
->write_buffer_offset
= UDP_DATAGRAM_HEADER_LENGTH
;
517 ptr
->write_buffer_offset
= 0;
519 return (ssize_t
) return_length
;
523 Eventually we will just kill off the server with the problem.
525 void memcached_io_reset(memcached_server_st
*ptr
)
527 memcached_quit_server(ptr
, 1);
531 * Read a given number of bytes from the server and place it into a specific
532 * buffer. Reset the IO channel on this server if an error occurs.
534 memcached_return
memcached_safe_read(memcached_server_st
*ptr
,
541 while (offset
< size
)
544 memcached_return rc
= memcached_io_read(ptr
, data
+ offset
, size
- offset
,
546 if (rc
!= MEMCACHED_SUCCESS
)
549 offset
+= (size_t) nread
;
552 return MEMCACHED_SUCCESS
;
555 memcached_return
memcached_io_readline(memcached_server_st
*ptr
,
559 bool line_complete
= false;
562 while (!line_complete
)
564 if (ptr
->read_buffer_length
== 0)
567 * We don't have any data in the buffer, so let's fill the read
568 * buffer. Call the standard read function to avoid duplicating
572 memcached_return rc
= memcached_io_read(ptr
, buffer_ptr
, 1, &nread
);
573 if (rc
!= MEMCACHED_SUCCESS
)
576 if (*buffer_ptr
== '\n')
583 /* Now let's look in the buffer and copy as we go! */
584 while (ptr
->read_buffer_length
&& total_nr
< size
&& !line_complete
)
586 *buffer_ptr
= *ptr
->read_ptr
;
587 if (*buffer_ptr
== '\n')
588 line_complete
= true;
589 --ptr
->read_buffer_length
;
595 if (total_nr
== size
)
596 return MEMCACHED_PROTOCOL_ERROR
;
599 return MEMCACHED_SUCCESS
;
603 * The udp request id consists of two seperate sections
605 * 2) The message number
606 * The thread id should only be set when the memcached_st struct is created
607 * and should not be changed.
609 * The message num is incremented for each new message we send, this function
610 * extracts the message number from message_id, increments it and then
611 * writes the new value back into the header
613 static void increment_udp_message_id(memcached_server_st
*ptr
)
615 struct udp_datagram_header_st
*header
= (struct udp_datagram_header_st
*)ptr
->write_buffer
;
616 uint16_t cur_req
= get_udp_datagram_request_id(header
);
617 int msg_num
= get_msg_num_from_request_id(cur_req
);
618 int thread_id
= get_thread_id_from_request_id(cur_req
);
620 if (((++msg_num
) & UDP_REQUEST_ID_THREAD_MASK
) != 0)
623 header
->request_id
= htons((uint16_t) (thread_id
| msg_num
));
626 memcached_return
memcached_io_init_udp_header(memcached_server_st
*ptr
, uint16_t thread_id
)
628 if (thread_id
> UDP_REQUEST_ID_MAX_THREAD_ID
)
629 return MEMCACHED_FAILURE
;
631 struct udp_datagram_header_st
*header
= (struct udp_datagram_header_st
*)ptr
->write_buffer
;
632 header
->request_id
= htons((uint16_t) (generate_udp_request_thread_id(thread_id
)));
633 header
->num_datagrams
= htons(1);
634 header
->sequence_number
= htons(0);
636 return MEMCACHED_SUCCESS
;