2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
8 * Summary: Server IO, Not public!
14 #include <sys/select.h>
22 static ssize_t
io_flush(memcached_server_instance_st
*ptr
, memcached_return_t
*error
);
23 static void increment_udp_message_id(memcached_server_instance_st
*ptr
);
25 static memcached_return_t
io_wait(memcached_server_instance_st
*ptr
,
26 memc_read_or_write read_or_write
)
34 unlikely (read_or_write
== MEM_WRITE
) /* write */
38 ** We are going to block on write, but at least on Solaris we might block
39 ** on write if we haven't read anything from our input buffer..
40 ** Try to purge the input buffer if we don't do any flow control in the
41 ** application layer (just sending a lot of data etc)
42 ** The test is moved down in the purge function to avoid duplication of
45 if (read_or_write
== MEM_WRITE
)
47 memcached_return_t rc
= memcached_purge(ptr
);
48 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_STORED
)
49 return MEMCACHED_FAILURE
;
52 int timeout
= ptr
->root
->poll_timeout
;
53 if (ptr
->root
->flags
.no_block
== false)
56 error
= poll(&fds
, 1, timeout
);
59 return MEMCACHED_SUCCESS
;
61 return MEMCACHED_TIMEOUT
;
63 /* Imposssible for anything other then -1 */
64 WATCHPOINT_ASSERT(error
== -1);
65 memcached_quit_server(ptr
, 1);
67 return MEMCACHED_FAILURE
;
71 * Try to fill the input buffer for a server with as much
74 * @param ptr the server to pack
76 static bool repack_input_buffer(memcached_server_instance_st
*ptr
)
78 if (ptr
->read_ptr
!= ptr
->read_buffer
)
80 /* Move all of the data to the beginning of the buffer so
81 ** that we can fit more data into the buffer...
83 memmove(ptr
->read_buffer
, ptr
->read_ptr
, ptr
->read_buffer_length
);
84 ptr
->read_ptr
= ptr
->read_buffer
;
85 ptr
->read_data_length
= ptr
->read_buffer_length
;
88 /* There is room in the buffer, try to fill it! */
89 if (ptr
->read_buffer_length
!= MEMCACHED_MAX_BUFFER
)
91 /* Just try a single read to grab what's available */
92 ssize_t nr
= read(ptr
->fd
,
93 ptr
->read_ptr
+ ptr
->read_data_length
,
94 MEMCACHED_MAX_BUFFER
- ptr
->read_data_length
);
98 ptr
->read_data_length
+= (size_t)nr
;
99 ptr
->read_buffer_length
+= (size_t)nr
;
107 * If the we have callbacks connected to this server structure
108 * we may start process the input queue and fire the callbacks
109 * for the incomming messages. This function is _only_ called
110 * when the input buffer is full, so that we _know_ that we have
111 * at least _one_ message to process.
113 * @param ptr the server to star processing iput messages for
114 * @return true if we processed anything, false otherwise
116 static bool process_input_buffer(memcached_server_instance_st
*ptr
)
119 ** We might be able to process some of the response messages if we
120 ** have a callback set up
122 if (ptr
->root
->callbacks
!= NULL
&& ptr
->root
->flags
.use_udp
== false)
125 * We might have responses... try to read them out and fire
128 memcached_callback_st cb
= *ptr
->root
->callbacks
;
130 memcached_set_processing_input((memcached_st
*)ptr
->root
, true);
132 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
133 memcached_return_t error
;
134 memcached_st
*root
= (memcached_st
*)ptr
->root
;
135 error
= memcached_response(ptr
, buffer
, sizeof(buffer
),
138 memcached_set_processing_input(root
, false);
140 if (error
== MEMCACHED_SUCCESS
)
142 for (unsigned int x
= 0; x
< cb
.number_of_callback
; x
++)
144 error
= (*cb
.callback
[x
])(ptr
->root
, &root
->result
, cb
.context
);
145 if (error
!= MEMCACHED_SUCCESS
)
149 /* @todo what should I do with the error message??? */
151 /* @todo what should I do with other error messages?? */
158 static inline void memcached_io_cork_push(memcached_server_st
*ptr
)
161 if (ptr
->root
->flags
.cork
== false || ptr
->state
.is_corked
)
165 int err
= setsockopt(ptr
->fd
, IPPROTO_TCP
, CORK
,
166 &enable
, (socklen_t
)sizeof(int));
168 ptr
->state
.is_corked
= true;
170 WATCHPOINT_ASSERT(ptr
->state
.is_corked
== true);
176 static inline void memcached_io_cork_pop(memcached_server_st
*ptr
)
179 if (ptr
->root
->flags
.cork
== false || ptr
->state
.is_corked
== false)
183 int err
= setsockopt(ptr
->fd
, IPPROTO_TCP
, CORK
,
184 &enable
, (socklen_t
)sizeof(int));
186 ptr
->state
.is_corked
= false;
188 WATCHPOINT_ASSERT(ptr
->state
.is_corked
== false);
195 void memcached_io_preread(memcached_st
*ptr
)
201 for (x
= 0; x
< memcached_server_count(ptr
); x
++)
203 if (memcached_server_response_count(ptr
, x
) &&
204 ptr
->hosts
[x
].read_data_length
< MEMCACHED_MAX_BUFFER
)
208 data_read
= read(ptr
->hosts
[x
].fd
,
209 ptr
->hosts
[x
].read_ptr
+ ptr
->hosts
[x
].read_data_length
,
210 MEMCACHED_MAX_BUFFER
- ptr
->hosts
[x
].read_data_length
);
214 ptr
->hosts
[x
].read_buffer_length
+= data_read
;
215 ptr
->hosts
[x
].read_data_length
+= data_read
;
221 memcached_return_t
memcached_io_read(memcached_server_instance_st
*ptr
,
222 void *buffer
, size_t length
, ssize_t
*nread
)
230 if (!ptr
->read_buffer_length
)
236 data_read
= read(ptr
->fd
, ptr
->read_buffer
, MEMCACHED_MAX_BUFFER
);
239 else if (data_read
== -1)
241 ptr
->cached_errno
= errno
;
242 memcached_return_t rc
= MEMCACHED_UNKNOWN_READ_FAILURE
;
247 if ((rc
= io_wait(ptr
, MEM_READ
)) == MEMCACHED_SUCCESS
)
253 memcached_quit_server(ptr
, 1);
262 EOF. Any data received so far is incomplete
263 so discard it. This always reads by byte in case of TCP
264 and protocol enforcement happens at memcached_response()
265 looking for '\n'. We do not care for UDB which requests 8 bytes
266 at once. Generally, this means that connection went away. Since
267 for blocking I/O we do not return 0 and for non-blocking case
268 it will return EGAIN if data is not immediatly available.
270 memcached_quit_server(ptr
, 1);
272 return MEMCACHED_UNKNOWN_READ_FAILURE
;
276 ptr
->io_bytes_sent
= 0;
277 ptr
->read_data_length
= (size_t) data_read
;
278 ptr
->read_buffer_length
= (size_t) data_read
;
279 ptr
->read_ptr
= ptr
->read_buffer
;
286 difference
= (length
> ptr
->read_buffer_length
) ? ptr
->read_buffer_length
: length
;
288 memcpy(buffer_ptr
, ptr
->read_ptr
, difference
);
289 length
-= difference
;
290 ptr
->read_ptr
+= difference
;
291 ptr
->read_buffer_length
-= difference
;
292 buffer_ptr
+= difference
;
296 *buffer_ptr
= *ptr
->read_ptr
;
298 ptr
->read_buffer_length
--;
304 ptr
->server_failure_counter
= 0;
305 *nread
= (ssize_t
)(buffer_ptr
- (char*)buffer
);
306 return MEMCACHED_SUCCESS
;
309 ssize_t
memcached_io_write(memcached_server_instance_st
*ptr
,
310 const void *buffer
, size_t length
, char with_flush
)
312 size_t original_length
;
313 const char* buffer_ptr
;
315 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
317 original_length
= length
;
320 /* more writable data is coming if a flush isn't required, so delay send */
323 memcached_io_cork_push(ptr
);
332 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
334 //UDP does not support partial writes
335 buffer_end
= MAX_UDP_DATAGRAM_LENGTH
;
336 should_write
= length
;
337 if (ptr
->write_buffer_offset
+ should_write
> buffer_end
)
342 buffer_end
= MEMCACHED_MAX_BUFFER
;
343 should_write
= buffer_end
- ptr
->write_buffer_offset
;
344 should_write
= (should_write
< length
) ? should_write
: length
;
347 write_ptr
= ptr
->write_buffer
+ ptr
->write_buffer_offset
;
348 memcpy(write_ptr
, buffer_ptr
, should_write
);
349 ptr
->write_buffer_offset
+= should_write
;
350 buffer_ptr
+= should_write
;
351 length
-= should_write
;
353 if (ptr
->write_buffer_offset
== buffer_end
&& ptr
->type
!= MEMCACHED_CONNECTION_UDP
)
355 memcached_return_t rc
;
358 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
359 sent_length
= io_flush(ptr
, &rc
);
360 if (sent_length
== -1)
363 /* If io_flush calls memcached_purge, sent_length may be 0 */
364 unlikely (sent_length
!= 0)
366 WATCHPOINT_ASSERT(sent_length
== (ssize_t
)buffer_end
);
373 memcached_return_t rc
;
374 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
375 if (io_flush(ptr
, &rc
) == -1)
380 memcached_io_cork_pop(ptr
);
383 return (ssize_t
) original_length
;
386 memcached_return_t
memcached_io_close(memcached_server_instance_st
*ptr
)
390 return MEMCACHED_SUCCESS
;
393 /* in case of death shutdown to avoid blocking at close() */
394 if (shutdown(ptr
->fd
, SHUT_RDWR
) == -1 && errno
!= ENOTCONN
)
396 WATCHPOINT_NUMBER(ptr
->fd
);
397 WATCHPOINT_ERRNO(errno
);
398 WATCHPOINT_ASSERT(errno
);
401 if (close(ptr
->fd
) == -1)
403 WATCHPOINT_ERRNO(errno
);
406 return MEMCACHED_SUCCESS
;
409 memcached_server_instance_st
*memcached_io_get_readable_server(memcached_st
*memc
)
411 #define MAX_SERVERS_TO_POLL 100
412 struct pollfd fds
[MAX_SERVERS_TO_POLL
];
413 unsigned int host_index
= 0;
416 x
< memcached_server_count(memc
) && host_index
< MAX_SERVERS_TO_POLL
;
419 memcached_server_instance_st
*instance
=
420 memcached_server_instance_fetch(memc
, x
);
422 if (instance
->read_buffer_length
> 0) /* I have data in the buffer */
425 if (memcached_server_response_count(instance
) > 0)
427 fds
[host_index
].events
= POLLIN
;
428 fds
[host_index
].revents
= 0;
429 fds
[host_index
].fd
= instance
->fd
;
436 /* We have 0 or 1 server with pending events.. */
437 for (uint32_t x
= 0; x
< memcached_server_count(memc
); ++x
)
439 memcached_server_instance_st
*instance
=
440 memcached_server_instance_fetch(memc
, x
);
442 if (memcached_server_response_count(instance
) > 0)
451 int err
= poll(fds
, host_index
, memc
->poll_timeout
);
454 memc
->cached_errno
= errno
;
459 for (size_t x
= 0; x
< host_index
; ++x
)
461 if (fds
[x
].revents
& POLLIN
)
463 for (uint32_t y
= 0; y
< memcached_server_count(memc
); ++y
)
465 memcached_server_instance_st
*instance
=
466 memcached_server_instance_fetch(memc
, y
);
468 if (instance
->fd
== fds
[x
].fd
)
478 static ssize_t
io_flush(memcached_server_instance_st
*ptr
,
479 memcached_return_t
*error
)
482 ** We might want to purge the input buffer if we haven't consumed
483 ** any output yet... The test for the limits is the purge is inline
484 ** in the purge function to avoid duplicating the logic..
487 memcached_return_t rc
;
488 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
489 rc
= memcached_purge(ptr
);
491 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_STORED
)
495 size_t return_length
;
496 char *local_write_ptr
= ptr
->write_buffer
;
497 size_t write_length
= ptr
->write_buffer_offset
;
499 *error
= MEMCACHED_SUCCESS
;
501 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
503 // UDP Sanity check, make sure that we are not sending somthing too big
504 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
&& write_length
> MAX_UDP_DATAGRAM_LENGTH
)
507 if (ptr
->write_buffer_offset
== 0 || (ptr
->type
== MEMCACHED_CONNECTION_UDP
508 && ptr
->write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
))
511 /* Looking for memory overflows */
513 if (write_length
== MEMCACHED_MAX_BUFFER
)
514 WATCHPOINT_ASSERT(ptr
->write_buffer
== local_write_ptr
);
515 WATCHPOINT_ASSERT((ptr
->write_buffer
+ MEMCACHED_MAX_BUFFER
) >= (local_write_ptr
+ write_length
));
521 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
522 WATCHPOINT_ASSERT(write_length
> 0);
524 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
525 increment_udp_message_id(ptr
);
526 sent_length
= write(ptr
->fd
, local_write_ptr
, write_length
);
528 if (sent_length
== -1)
530 ptr
->cached_errno
= errno
;
538 * We may be blocked on write because the input buffer
539 * is full. Let's check if we have room in our input
540 * buffer for more data and retry the write before
543 if (repack_input_buffer(ptr
) ||
544 process_input_buffer(ptr
))
547 memcached_return_t rc
;
548 rc
= io_wait(ptr
, MEM_WRITE
);
550 if (rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_TIMEOUT
)
553 memcached_quit_server(ptr
, 1);
557 memcached_quit_server(ptr
, 1);
558 *error
= MEMCACHED_ERRNO
;
563 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
&&
564 (size_t)sent_length
!= write_length
)
566 memcached_quit_server(ptr
, 1);
570 ptr
->io_bytes_sent
+= (uint32_t) sent_length
;
572 local_write_ptr
+= sent_length
;
573 write_length
-= (uint32_t) sent_length
;
574 return_length
+= (uint32_t) sent_length
;
577 WATCHPOINT_ASSERT(write_length
== 0);
578 // Need to study this assert() WATCHPOINT_ASSERT(return_length ==
579 // ptr->write_buffer_offset);
581 // if we are a udp server, the begining of the buffer is reserverd for
582 // the upd frame header
583 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
584 ptr
->write_buffer_offset
= UDP_DATAGRAM_HEADER_LENGTH
;
586 ptr
->write_buffer_offset
= 0;
588 return (ssize_t
) return_length
;
592 Eventually we will just kill off the server with the problem.
594 void memcached_io_reset(memcached_server_instance_st
*ptr
)
596 memcached_quit_server(ptr
, 1);
600 * Read a given number of bytes from the server and place it into a specific
601 * buffer. Reset the IO channel on this server if an error occurs.
603 memcached_return_t
memcached_safe_read(memcached_server_instance_st
*ptr
,
610 while (offset
< size
)
613 memcached_return_t rc
= memcached_io_read(ptr
, data
+ offset
, size
- offset
,
615 if (rc
!= MEMCACHED_SUCCESS
)
618 offset
+= (size_t) nread
;
621 return MEMCACHED_SUCCESS
;
624 memcached_return_t
memcached_io_readline(memcached_server_instance_st
*ptr
,
628 bool line_complete
= false;
631 while (!line_complete
)
633 if (ptr
->read_buffer_length
== 0)
636 * We don't have any data in the buffer, so let's fill the read
637 * buffer. Call the standard read function to avoid duplicating
641 memcached_return_t rc
= memcached_io_read(ptr
, buffer_ptr
, 1, &nread
);
642 if (rc
!= MEMCACHED_SUCCESS
)
645 if (*buffer_ptr
== '\n')
652 /* Now let's look in the buffer and copy as we go! */
653 while (ptr
->read_buffer_length
&& total_nr
< size
&& !line_complete
)
655 *buffer_ptr
= *ptr
->read_ptr
;
656 if (*buffer_ptr
== '\n')
657 line_complete
= true;
658 --ptr
->read_buffer_length
;
664 if (total_nr
== size
)
665 return MEMCACHED_PROTOCOL_ERROR
;
668 return MEMCACHED_SUCCESS
;
672 * The udp request id consists of two seperate sections
674 * 2) The message number
675 * The thread id should only be set when the memcached_st struct is created
676 * and should not be changed.
678 * The message num is incremented for each new message we send, this function
679 * extracts the message number from message_id, increments it and then
680 * writes the new value back into the header
682 static void increment_udp_message_id(memcached_server_instance_st
*ptr
)
684 struct udp_datagram_header_st
*header
= (struct udp_datagram_header_st
*)ptr
->write_buffer
;
685 uint16_t cur_req
= get_udp_datagram_request_id(header
);
686 int msg_num
= get_msg_num_from_request_id(cur_req
);
687 int thread_id
= get_thread_id_from_request_id(cur_req
);
689 if (((++msg_num
) & UDP_REQUEST_ID_THREAD_MASK
) != 0)
692 header
->request_id
= htons((uint16_t) (thread_id
| msg_num
));
695 memcached_return_t
memcached_io_init_udp_header(memcached_server_instance_st
*ptr
, uint16_t thread_id
)
697 if (thread_id
> UDP_REQUEST_ID_MAX_THREAD_ID
)
698 return MEMCACHED_FAILURE
;
700 struct udp_datagram_header_st
*header
= (struct udp_datagram_header_st
*)ptr
->write_buffer
;
701 header
->request_id
= htons((uint16_t) (generate_udp_request_thread_id(thread_id
)));
702 header
->num_datagrams
= htons(1);
703 header
->sequence_number
= htons(0);
705 return MEMCACHED_SUCCESS
;