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
)
27 unlikely (read_or_write
== MEM_WRITE
) /* write */
31 ** We are going to block on write, but at least on Solaris we might block
32 ** on write if we haven't read anything from our input buffer..
33 ** Try to purge the input buffer if we don't do any flow control in the
34 ** application layer (just sending a lot of data etc)
35 ** The test is moved down in the purge function to avoid duplication of
38 if (read_or_write
== MEM_WRITE
)
40 memcached_return rc
= memcached_purge(ptr
);
41 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_STORED
)
42 return MEMCACHED_FAILURE
;
45 int timeout
= ptr
->root
->poll_timeout
;
46 if ((ptr
->root
->flags
& MEM_NO_BLOCK
) == 0)
49 error
= poll(&fds
, 1, timeout
);
52 return MEMCACHED_SUCCESS
;
54 return MEMCACHED_TIMEOUT
;
56 /* Imposssible for anything other then -1 */
57 WATCHPOINT_ASSERT(error
== -1);
58 memcached_quit_server(ptr
, 1);
60 return MEMCACHED_FAILURE
;
64 * Try to fill the input buffer for a server with as much
67 * @param ptr the server to pack
69 static bool repack_input_buffer(memcached_server_st
*ptr
)
71 if (ptr
->read_ptr
!= ptr
->read_buffer
)
73 /* Move all of the data to the beginning of the buffer so
74 ** that we can fit more data into the buffer...
76 memmove(ptr
->read_buffer
, ptr
->read_ptr
, ptr
->read_buffer_length
);
77 ptr
->read_ptr
= ptr
->read_buffer
;
78 ptr
->read_data_length
= ptr
->read_buffer_length
;
81 /* There is room in the buffer, try to fill it! */
82 if (ptr
->read_buffer_length
!= MEMCACHED_MAX_BUFFER
)
84 /* Just try a single read to grab what's available */
85 ssize_t nr
= read(ptr
->fd
,
86 ptr
->read_ptr
+ ptr
->read_data_length
,
87 MEMCACHED_MAX_BUFFER
- ptr
->read_data_length
);
91 ptr
->read_data_length
+= (size_t)nr
;
92 ptr
->read_buffer_length
+= (size_t)nr
;
100 * If the we have callbacks connected to this server structure
101 * we may start process the input queue and fire the callbacks
102 * for the incomming messages. This function is _only_ called
103 * when the input buffer is full, so that we _know_ that we have
104 * at least _one_ message to process.
106 * @param ptr the server to star processing iput messages for
107 * @return true if we processed anything, false otherwise
109 static bool process_input_buffer(memcached_server_st
*ptr
)
112 ** We might be able to process some of the response messages if we
113 ** have a callback set up
115 if (ptr
->root
->callbacks
!= NULL
&& (ptr
->root
->flags
& MEM_USE_UDP
) == 0)
118 * We might have responses... try to read them out and fire
121 memcached_callback_st cb
= *ptr
->root
->callbacks
;
123 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
124 memcached_return error
;
125 error
= memcached_response(ptr
, buffer
, sizeof(buffer
),
127 if (error
== MEMCACHED_SUCCESS
)
129 for (unsigned int x
= 0; x
< cb
.number_of_callback
; x
++)
131 error
= (*cb
.callback
[x
])(ptr
->root
, &ptr
->root
->result
, cb
.context
);
132 if (error
!= MEMCACHED_SUCCESS
)
136 /* @todo what should I do with the error message??? */
138 /* @todo what should I do with other error messages?? */
146 void memcached_io_preread(memcached_st
*ptr
)
152 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
154 if (memcached_server_response_count(ptr
, x
) &&
155 ptr
->hosts
[x
].read_data_length
< MEMCACHED_MAX_BUFFER
)
159 data_read
= read(ptr
->hosts
[x
].fd
,
160 ptr
->hosts
[x
].read_ptr
+ ptr
->hosts
[x
].read_data_length
,
161 MEMCACHED_MAX_BUFFER
- ptr
->hosts
[x
].read_data_length
);
165 ptr
->hosts
[x
].read_buffer_length
+= data_read
;
166 ptr
->hosts
[x
].read_data_length
+= data_read
;
172 memcached_return
memcached_io_read(memcached_server_st
*ptr
,
173 void *buffer
, size_t length
, ssize_t
*nread
)
181 if (!ptr
->read_buffer_length
)
187 data_read
= read(ptr
->fd
, ptr
->read_buffer
, MEMCACHED_MAX_BUFFER
);
190 else if (data_read
== -1)
192 ptr
->cached_errno
= errno
;
193 memcached_return rc
= MEMCACHED_UNKNOWN_READ_FAILURE
;
198 if ((rc
= io_wait(ptr
, MEM_READ
)) == MEMCACHED_SUCCESS
)
204 memcached_quit_server(ptr
, 1);
213 EOF. Any data received so far is incomplete
214 so discard it. This always reads by byte in case of TCP
215 and protocol enforcement happens at memcached_response()
216 looking for '\n'. We do not care for UDB which requests 8 bytes
217 at once. Generally, this means that connection went away. Since
218 for blocking I/O we do not return 0 and for non-blocking case
219 it will return EGAIN if data is not immediatly available.
221 memcached_quit_server(ptr
, 1);
223 return MEMCACHED_UNKNOWN_READ_FAILURE
;
227 ptr
->io_bytes_sent
= 0;
228 ptr
->read_data_length
= (size_t) data_read
;
229 ptr
->read_buffer_length
= (size_t) data_read
;
230 ptr
->read_ptr
= ptr
->read_buffer
;
237 difference
= (length
> ptr
->read_buffer_length
) ? ptr
->read_buffer_length
: length
;
239 memcpy(buffer_ptr
, ptr
->read_ptr
, difference
);
240 length
-= difference
;
241 ptr
->read_ptr
+= difference
;
242 ptr
->read_buffer_length
-= difference
;
243 buffer_ptr
+= difference
;
247 *buffer_ptr
= *ptr
->read_ptr
;
249 ptr
->read_buffer_length
--;
255 ptr
->server_failure_counter
= 0;
256 *nread
= (ssize_t
)(buffer_ptr
- (char*)buffer
);
257 return MEMCACHED_SUCCESS
;
260 ssize_t
memcached_io_write(memcached_server_st
*ptr
,
261 const void *buffer
, size_t length
, char with_flush
)
263 size_t original_length
;
264 const char* buffer_ptr
;
266 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
268 original_length
= length
;
277 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
279 //UDP does not support partial writes
280 buffer_end
= MAX_UDP_DATAGRAM_LENGTH
;
281 should_write
= length
;
282 if (ptr
->write_buffer_offset
+ should_write
> buffer_end
)
287 buffer_end
= MEMCACHED_MAX_BUFFER
;
288 should_write
= buffer_end
- ptr
->write_buffer_offset
;
289 should_write
= (should_write
< length
) ? should_write
: length
;
292 write_ptr
= ptr
->write_buffer
+ ptr
->write_buffer_offset
;
293 memcpy(write_ptr
, buffer_ptr
, should_write
);
294 ptr
->write_buffer_offset
+= should_write
;
295 buffer_ptr
+= should_write
;
296 length
-= should_write
;
298 if (ptr
->write_buffer_offset
== buffer_end
&& ptr
->type
!= MEMCACHED_CONNECTION_UDP
)
303 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
304 sent_length
= io_flush(ptr
, &rc
);
305 if (sent_length
== -1)
308 /* If io_flush calls memcached_purge, sent_length may be 0 */
309 unlikely (sent_length
!= 0)
311 WATCHPOINT_ASSERT(sent_length
== (ssize_t
)buffer_end
);
319 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
320 if (io_flush(ptr
, &rc
) == -1)
324 return (ssize_t
) original_length
;
327 memcached_return
memcached_io_close(memcached_server_st
*ptr
)
332 return MEMCACHED_SUCCESS
;
334 /* in case of death shutdown to avoid blocking at close() */
337 r
= shutdown(ptr
->fd
, SHUT_RDWR
);
340 if (r
&& errno
!= ENOTCONN
)
342 WATCHPOINT_NUMBER(ptr
->fd
);
343 WATCHPOINT_ERRNO(errno
);
344 WATCHPOINT_ASSERT(errno
);
352 WATCHPOINT_ERRNO(errno
);
355 return MEMCACHED_SUCCESS
;
358 memcached_server_st
*memcached_io_get_readable_server(memcached_st
*memc
)
360 #define MAX_SERVERS_TO_POLL 100
361 struct pollfd fds
[MAX_SERVERS_TO_POLL
];
362 unsigned int host_index
= 0;
364 for (unsigned int x
= 0;
365 x
< memc
->number_of_hosts
&& host_index
< MAX_SERVERS_TO_POLL
;
368 if (memc
->hosts
[x
].read_buffer_length
> 0) /* I have data in the buffer */
369 return &memc
->hosts
[x
];
371 if (memcached_server_response_count(&memc
->hosts
[x
]) > 0)
373 fds
[host_index
].events
= POLLIN
;
374 fds
[host_index
].revents
= 0;
375 fds
[host_index
].fd
= memc
->hosts
[x
].fd
;
382 /* We have 0 or 1 server with pending events.. */
383 for (unsigned int x
= 0; x
< memc
->number_of_hosts
; ++x
)
384 if (memcached_server_response_count(&memc
->hosts
[x
]) > 0)
385 return &memc
->hosts
[x
];
390 int err
= poll(fds
, host_index
, memc
->poll_timeout
);
393 memc
->cached_errno
= errno
;
398 for (unsigned int x
= 0; x
< host_index
; ++x
)
399 if (fds
[x
].revents
& POLLIN
)
400 for (unsigned int y
= 0; y
< memc
->number_of_hosts
; ++y
)
401 if (memc
->hosts
[y
].fd
== fds
[x
].fd
)
402 return &memc
->hosts
[y
];
408 static ssize_t
io_flush(memcached_server_st
*ptr
,
409 memcached_return
*error
)
412 ** We might want to purge the input buffer if we haven't consumed
413 ** any output yet... The test for the limits is the purge is inline
414 ** in the purge function to avoid duplicating the logic..
418 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
419 rc
= memcached_purge(ptr
);
421 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_STORED
)
425 size_t return_length
;
426 char *local_write_ptr
= ptr
->write_buffer
;
427 size_t write_length
= ptr
->write_buffer_offset
;
429 *error
= MEMCACHED_SUCCESS
;
431 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
433 // UDP Sanity check, make sure that we are not sending somthing too big
434 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
&& write_length
> MAX_UDP_DATAGRAM_LENGTH
)
437 if (ptr
->write_buffer_offset
== 0 || (ptr
->type
== MEMCACHED_CONNECTION_UDP
438 && ptr
->write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
))
441 /* Looking for memory overflows */
443 if (write_length
== MEMCACHED_MAX_BUFFER
)
444 WATCHPOINT_ASSERT(ptr
->write_buffer
== local_write_ptr
);
445 WATCHPOINT_ASSERT((ptr
->write_buffer
+ MEMCACHED_MAX_BUFFER
) >= (local_write_ptr
+ write_length
));
451 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
452 WATCHPOINT_ASSERT(write_length
> 0);
454 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
455 increment_udp_message_id(ptr
);
456 sent_length
= write(ptr
->fd
, local_write_ptr
, write_length
);
458 if (sent_length
== -1)
460 ptr
->cached_errno
= errno
;
468 * We may be blocked on write because the input buffer
469 * is full. Let's check if we have room in our input
470 * buffer for more data and retry the write before
473 if (repack_input_buffer(ptr
) ||
474 process_input_buffer(ptr
))
478 rc
= io_wait(ptr
, MEM_WRITE
);
480 if (rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_TIMEOUT
)
483 memcached_quit_server(ptr
, 1);
487 memcached_quit_server(ptr
, 1);
488 *error
= MEMCACHED_ERRNO
;
493 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
&&
494 (size_t)sent_length
!= write_length
)
496 memcached_quit_server(ptr
, 1);
500 ptr
->io_bytes_sent
+= (uint32_t) sent_length
;
502 local_write_ptr
+= sent_length
;
503 write_length
-= (uint32_t) sent_length
;
504 return_length
+= (uint32_t) sent_length
;
507 WATCHPOINT_ASSERT(write_length
== 0);
508 // Need to study this assert() WATCHPOINT_ASSERT(return_length ==
509 // ptr->write_buffer_offset);
511 // if we are a udp server, the begining of the buffer is reserverd for
512 // the upd frame header
513 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
514 ptr
->write_buffer_offset
= UDP_DATAGRAM_HEADER_LENGTH
;
516 ptr
->write_buffer_offset
= 0;
518 return (ssize_t
) return_length
;
522 Eventually we will just kill off the server with the problem.
524 void memcached_io_reset(memcached_server_st
*ptr
)
526 memcached_quit_server(ptr
, 1);
530 * Read a given number of bytes from the server and place it into a specific
531 * buffer. Reset the IO channel on this server if an error occurs.
533 memcached_return
memcached_safe_read(memcached_server_st
*ptr
,
540 while (offset
< size
)
543 memcached_return rc
= memcached_io_read(ptr
, data
+ offset
, size
- offset
,
545 if (rc
!= MEMCACHED_SUCCESS
)
548 offset
+= (size_t) nread
;
551 return MEMCACHED_SUCCESS
;
554 memcached_return
memcached_io_readline(memcached_server_st
*ptr
,
558 bool line_complete
= false;
561 while (!line_complete
)
563 if (ptr
->read_buffer_length
== 0)
566 * We don't have any data in the buffer, so let's fill the read
567 * buffer. Call the standard read function to avoid duplicating
571 memcached_return rc
= memcached_io_read(ptr
, buffer_ptr
, 1, &nread
);
572 if (rc
!= MEMCACHED_SUCCESS
)
575 if (*buffer_ptr
== '\n')
582 /* Now let's look in the buffer and copy as we go! */
583 while (ptr
->read_buffer_length
&& total_nr
< size
&& !line_complete
)
585 *buffer_ptr
= *ptr
->read_ptr
;
586 if (*buffer_ptr
== '\n')
587 line_complete
= true;
588 --ptr
->read_buffer_length
;
594 if (total_nr
== size
)
595 return MEMCACHED_PROTOCOL_ERROR
;
598 return MEMCACHED_SUCCESS
;
602 * The udp request id consists of two seperate sections
604 * 2) The message number
605 * The thread id should only be set when the memcached_st struct is created
606 * and should not be changed.
608 * The message num is incremented for each new message we send, this function
609 * extracts the message number from message_id, increments it and then
610 * writes the new value back into the header
612 static void increment_udp_message_id(memcached_server_st
*ptr
)
614 struct udp_datagram_header_st
*header
= (struct udp_datagram_header_st
*)ptr
->write_buffer
;
615 uint16_t cur_req
= get_udp_datagram_request_id(header
);
616 int msg_num
= get_msg_num_from_request_id(cur_req
);
617 int thread_id
= get_thread_id_from_request_id(cur_req
);
619 if (((++msg_num
) & UDP_REQUEST_ID_THREAD_MASK
) != 0)
622 header
->request_id
= htons((uint16_t) (thread_id
| msg_num
));
625 memcached_return
memcached_io_init_udp_header(memcached_server_st
*ptr
, uint16_t thread_id
)
627 if (thread_id
> UDP_REQUEST_ID_MAX_THREAD_ID
)
628 return MEMCACHED_FAILURE
;
630 struct udp_datagram_header_st
*header
= (struct udp_datagram_header_st
*)ptr
->write_buffer
;
631 header
->request_id
= htons((uint16_t) (generate_udp_request_thread_id(thread_id
)));
632 header
->num_datagrams
= htons(1);
633 header
->sequence_number
= htons(0);
635 return MEMCACHED_SUCCESS
;