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_write_instance_st ptr
,
23 memcached_return_t
*error
);
24 static void increment_udp_message_id(memcached_server_write_instance_st ptr
);
26 static memcached_return_t
io_wait(memcached_server_write_instance_st ptr
,
27 memc_read_or_write read_or_write
)
35 if (read_or_write
== MEM_WRITE
) /* write */
38 WATCHPOINT_SET(ptr
->io_wait_count
.write
++);
42 WATCHPOINT_SET(ptr
->io_wait_count
.read
++);
46 ** We are going to block on write, but at least on Solaris we might block
47 ** on write if we haven't read anything from our input buffer..
48 ** Try to purge the input buffer if we don't do any flow control in the
49 ** application layer (just sending a lot of data etc)
50 ** The test is moved down in the purge function to avoid duplication of
53 if (read_or_write
== MEM_WRITE
)
55 memcached_return_t rc
= memcached_purge(ptr
);
56 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_STORED
)
57 return MEMCACHED_FAILURE
;
60 int timeout
= ptr
->root
->poll_timeout
;
61 if (ptr
->root
->flags
.no_block
== false)
65 while (--loop_max
) // While loop is for ERESTART or EINTR
67 error
= poll(&fds
, 1, timeout
);
72 WATCHPOINT_IF_LABELED_NUMBER(read_or_write
&& loop_max
< 4, "read() times we had to loop, decremented down from 5", loop_max
);
73 WATCHPOINT_IF_LABELED_NUMBER(!read_or_write
&& loop_max
< 4, "write() times we had to loop, decremented down from 5", loop_max
);
75 return MEMCACHED_SUCCESS
;
76 case 0: // Timeout occured, we let the while() loop do its thing.
77 return MEMCACHED_TIMEOUT
;
79 WATCHPOINT_ERRNO(errno
);
82 #ifdef TARGET_OS_LINUX
88 if (fds
.revents
& POLLERR
)
91 socklen_t len
= sizeof (err
);
92 (void)getsockopt(ptr
->fd
, SOL_SOCKET
, SO_ERROR
, &err
, &len
);
93 ptr
->cached_errno
= (err
== 0) ? errno
: err
;
97 ptr
->cached_errno
= errno
;
99 memcached_quit_server(ptr
, true);
101 return MEMCACHED_FAILURE
;
106 /* Imposssible for anything other then -1 */
107 WATCHPOINT_ASSERT(error
== -1);
108 ptr
->cached_errno
= errno
;
109 memcached_quit_server(ptr
, true);
111 return MEMCACHED_FAILURE
;
115 * Try to fill the input buffer for a server with as much
118 * @param ptr the server to pack
120 static bool repack_input_buffer(memcached_server_write_instance_st ptr
)
122 if (ptr
->read_ptr
!= ptr
->read_buffer
)
124 /* Move all of the data to the beginning of the buffer so
125 ** that we can fit more data into the buffer...
127 memmove(ptr
->read_buffer
, ptr
->read_ptr
, ptr
->read_buffer_length
);
128 ptr
->read_ptr
= ptr
->read_buffer
;
129 ptr
->read_data_length
= ptr
->read_buffer_length
;
132 /* There is room in the buffer, try to fill it! */
133 if (ptr
->read_buffer_length
!= MEMCACHED_MAX_BUFFER
)
135 /* Just try a single read to grab what's available */
136 ssize_t nr
= read(ptr
->fd
,
137 ptr
->read_ptr
+ ptr
->read_data_length
,
138 MEMCACHED_MAX_BUFFER
- ptr
->read_data_length
);
142 ptr
->read_data_length
+= (size_t)nr
;
143 ptr
->read_buffer_length
+= (size_t)nr
;
151 * If the we have callbacks connected to this server structure
152 * we may start process the input queue and fire the callbacks
153 * for the incomming messages. This function is _only_ called
154 * when the input buffer is full, so that we _know_ that we have
155 * at least _one_ message to process.
157 * @param ptr the server to star processing iput messages for
158 * @return true if we processed anything, false otherwise
160 static bool process_input_buffer(memcached_server_write_instance_st ptr
)
163 ** We might be able to process some of the response messages if we
164 ** have a callback set up
166 if (ptr
->root
->callbacks
!= NULL
&& ptr
->root
->flags
.use_udp
== false)
169 * We might have responses... try to read them out and fire
172 memcached_callback_st cb
= *ptr
->root
->callbacks
;
174 memcached_set_processing_input((memcached_st
*)ptr
->root
, true);
176 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
177 memcached_return_t error
;
178 memcached_st
*root
= (memcached_st
*)ptr
->root
;
179 error
= memcached_response(ptr
, buffer
, sizeof(buffer
),
182 memcached_set_processing_input(root
, false);
184 if (error
== MEMCACHED_SUCCESS
)
186 for (unsigned int x
= 0; x
< cb
.number_of_callback
; x
++)
188 error
= (*cb
.callback
[x
])(ptr
->root
, &root
->result
, cb
.context
);
189 if (error
!= MEMCACHED_SUCCESS
)
193 /* @todo what should I do with the error message??? */
195 /* @todo what should I do with other error messages?? */
202 static inline void memcached_io_cork_push(memcached_server_st
*ptr
)
206 if (ptr
->root
->flags
.cork
== false || ptr
->state
.is_corked
)
210 int err
= setsockopt(ptr
->fd
, IPPROTO_TCP
, CORK
,
211 &enable
, (socklen_t
)sizeof(int));
213 ptr
->state
.is_corked
= true;
215 WATCHPOINT_ASSERT(ptr
->state
.is_corked
== true);
219 static inline void memcached_io_cork_pop(memcached_server_st
*ptr
)
223 if (ptr
->root
->flags
.cork
== false || ptr
->state
.is_corked
== false)
227 int err
= setsockopt(ptr
->fd
, IPPROTO_TCP
, CORK
,
228 &enable
, (socklen_t
)sizeof(int));
230 ptr
->state
.is_corked
= false;
232 WATCHPOINT_ASSERT(ptr
->state
.is_corked
== false);
236 #if 0 // Dead code, this should be removed.
237 void memcached_io_preread(memcached_st
*ptr
)
243 for (x
= 0; x
< memcached_server_count(ptr
); x
++)
245 if (memcached_server_response_count(ptr
, x
) &&
246 ptr
->hosts
[x
].read_data_length
< MEMCACHED_MAX_BUFFER
)
250 data_read
= read(ptr
->hosts
[x
].fd
,
251 ptr
->hosts
[x
].read_ptr
+ ptr
->hosts
[x
].read_data_length
,
252 MEMCACHED_MAX_BUFFER
- ptr
->hosts
[x
].read_data_length
);
256 ptr
->hosts
[x
].read_buffer_length
+= data_read
;
257 ptr
->hosts
[x
].read_data_length
+= data_read
;
263 memcached_return_t
memcached_io_read(memcached_server_write_instance_st ptr
,
264 void *buffer
, size_t length
, ssize_t
*nread
)
272 if (!ptr
->read_buffer_length
)
278 data_read
= read(ptr
->fd
, ptr
->read_buffer
, MEMCACHED_MAX_BUFFER
);
283 else if (data_read
== -1)
285 ptr
->cached_errno
= errno
;
286 memcached_return_t rc
= MEMCACHED_ERRNO
;
291 #ifdef TARGET_OS_LINUX
294 if ((rc
= io_wait(ptr
, MEM_READ
)) == MEMCACHED_SUCCESS
)
300 memcached_quit_server(ptr
, true);
309 EOF. Any data received so far is incomplete
310 so discard it. This always reads by byte in case of TCP
311 and protocol enforcement happens at memcached_response()
312 looking for '\n'. We do not care for UDB which requests 8 bytes
313 at once. Generally, this means that connection went away. Since
314 for blocking I/O we do not return 0 and for non-blocking case
315 it will return EGAIN if data is not immediatly available.
317 WATCHPOINT_STRING("We had a zero length read()");
318 memcached_quit_server(ptr
, true);
320 return MEMCACHED_UNKNOWN_READ_FAILURE
;
324 ptr
->io_bytes_sent
= 0;
325 ptr
->read_data_length
= (size_t) data_read
;
326 ptr
->read_buffer_length
= (size_t) data_read
;
327 ptr
->read_ptr
= ptr
->read_buffer
;
334 difference
= (length
> ptr
->read_buffer_length
) ? ptr
->read_buffer_length
: length
;
336 memcpy(buffer_ptr
, ptr
->read_ptr
, difference
);
337 length
-= difference
;
338 ptr
->read_ptr
+= difference
;
339 ptr
->read_buffer_length
-= difference
;
340 buffer_ptr
+= difference
;
344 *buffer_ptr
= *ptr
->read_ptr
;
346 ptr
->read_buffer_length
--;
352 ptr
->server_failure_counter
= 0;
353 *nread
= (ssize_t
)(buffer_ptr
- (char*)buffer
);
354 return MEMCACHED_SUCCESS
;
357 static ssize_t
_io_write(memcached_server_write_instance_st ptr
,
358 const void *buffer
, size_t length
, bool with_flush
)
360 size_t original_length
;
361 const char* buffer_ptr
;
363 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
365 original_length
= length
;
368 /* more writable data is coming if a flush isn't required, so delay send */
371 memcached_io_cork_push(ptr
);
380 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
382 //UDP does not support partial writes
383 buffer_end
= MAX_UDP_DATAGRAM_LENGTH
;
384 should_write
= length
;
385 if (ptr
->write_buffer_offset
+ should_write
> buffer_end
)
390 buffer_end
= MEMCACHED_MAX_BUFFER
;
391 should_write
= buffer_end
- ptr
->write_buffer_offset
;
392 should_write
= (should_write
< length
) ? should_write
: length
;
395 write_ptr
= ptr
->write_buffer
+ ptr
->write_buffer_offset
;
396 memcpy(write_ptr
, buffer_ptr
, should_write
);
397 ptr
->write_buffer_offset
+= should_write
;
398 buffer_ptr
+= should_write
;
399 length
-= should_write
;
401 if (ptr
->write_buffer_offset
== buffer_end
&& ptr
->type
!= MEMCACHED_CONNECTION_UDP
)
403 memcached_return_t rc
;
406 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
407 sent_length
= io_flush(ptr
, &rc
);
408 if (sent_length
== -1)
411 /* If io_flush calls memcached_purge, sent_length may be 0 */
412 unlikely (sent_length
!= 0)
414 WATCHPOINT_ASSERT(sent_length
== (ssize_t
)buffer_end
);
421 memcached_return_t rc
;
422 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
423 if (io_flush(ptr
, &rc
) == -1)
428 memcached_io_cork_pop(ptr
);
431 return (ssize_t
) original_length
;
434 ssize_t
memcached_io_write(memcached_server_write_instance_st ptr
,
435 const void *buffer
, size_t length
, bool with_flush
)
437 return _io_write(ptr
, buffer
, length
, with_flush
);
440 ssize_t
memcached_io_writev(memcached_server_write_instance_st ptr
,
441 const struct __write_vector_st
*vector
,
442 size_t number_of
, bool with_flush
)
446 for (size_t x
= 0; x
< number_of
; x
++, vector
++)
450 if ((returnable
= _io_write(ptr
, vector
->buffer
, vector
->length
, false)) == -1)
459 if (memcached_io_write(ptr
, NULL
, 0, true) == -1)
469 memcached_return_t
memcached_io_close(memcached_server_write_instance_st ptr
)
473 return MEMCACHED_SUCCESS
;
476 /* in case of death shutdown to avoid blocking at close() */
477 if (shutdown(ptr
->fd
, SHUT_RDWR
) == -1 && errno
!= ENOTCONN
)
479 WATCHPOINT_NUMBER(ptr
->fd
);
480 WATCHPOINT_ERRNO(errno
);
481 WATCHPOINT_ASSERT(errno
);
484 if (close(ptr
->fd
) == -1)
486 WATCHPOINT_ERRNO(errno
);
489 return MEMCACHED_SUCCESS
;
492 memcached_server_write_instance_st
memcached_io_get_readable_server(memcached_st
*memc
)
494 #define MAX_SERVERS_TO_POLL 100
495 struct pollfd fds
[MAX_SERVERS_TO_POLL
];
496 unsigned int host_index
= 0;
499 x
< memcached_server_count(memc
) && host_index
< MAX_SERVERS_TO_POLL
;
502 memcached_server_write_instance_st instance
=
503 memcached_server_instance_fetch(memc
, x
);
505 if (instance
->read_buffer_length
> 0) /* I have data in the buffer */
508 if (memcached_server_response_count(instance
) > 0)
510 fds
[host_index
].events
= POLLIN
;
511 fds
[host_index
].revents
= 0;
512 fds
[host_index
].fd
= instance
->fd
;
519 /* We have 0 or 1 server with pending events.. */
520 for (uint32_t x
= 0; x
< memcached_server_count(memc
); ++x
)
522 memcached_server_write_instance_st instance
=
523 memcached_server_instance_fetch(memc
, x
);
525 if (memcached_server_response_count(instance
) > 0)
534 int err
= poll(fds
, host_index
, memc
->poll_timeout
);
537 memc
->cached_errno
= errno
;
542 for (size_t x
= 0; x
< host_index
; ++x
)
544 if (fds
[x
].revents
& POLLIN
)
546 for (uint32_t y
= 0; y
< memcached_server_count(memc
); ++y
)
548 memcached_server_write_instance_st instance
=
549 memcached_server_instance_fetch(memc
, y
);
551 if (instance
->fd
== fds
[x
].fd
)
561 static ssize_t
io_flush(memcached_server_write_instance_st ptr
,
562 memcached_return_t
*error
)
565 ** We might want to purge the input buffer if we haven't consumed
566 ** any output yet... The test for the limits is the purge is inline
567 ** in the purge function to avoid duplicating the logic..
570 memcached_return_t rc
;
571 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
572 rc
= memcached_purge(ptr
);
574 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_STORED
)
578 size_t return_length
;
579 char *local_write_ptr
= ptr
->write_buffer
;
580 size_t write_length
= ptr
->write_buffer_offset
;
582 *error
= MEMCACHED_SUCCESS
;
584 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
586 // UDP Sanity check, make sure that we are not sending somthing too big
587 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
&& write_length
> MAX_UDP_DATAGRAM_LENGTH
)
590 if (ptr
->write_buffer_offset
== 0 || (ptr
->type
== MEMCACHED_CONNECTION_UDP
591 && ptr
->write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
))
594 /* Looking for memory overflows */
596 if (write_length
== MEMCACHED_MAX_BUFFER
)
597 WATCHPOINT_ASSERT(ptr
->write_buffer
== local_write_ptr
);
598 WATCHPOINT_ASSERT((ptr
->write_buffer
+ MEMCACHED_MAX_BUFFER
) >= (local_write_ptr
+ write_length
));
604 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
605 WATCHPOINT_ASSERT(write_length
> 0);
607 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
608 increment_udp_message_id(ptr
);
609 sent_length
= write(ptr
->fd
, local_write_ptr
, write_length
);
611 if (sent_length
== -1)
613 ptr
->cached_errno
= errno
;
614 WATCHPOINT_ERRNO(errno
);
615 WATCHPOINT_NUMBER(errno
);
623 * We may be blocked on write because the input buffer
624 * is full. Let's check if we have room in our input
625 * buffer for more data and retry the write before
628 if (repack_input_buffer(ptr
) ||
629 process_input_buffer(ptr
))
632 memcached_return_t rc
;
633 rc
= io_wait(ptr
, MEM_WRITE
);
635 if (rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_TIMEOUT
)
638 memcached_quit_server(ptr
, true);
642 memcached_quit_server(ptr
, true);
643 *error
= MEMCACHED_ERRNO
;
648 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
&&
649 (size_t)sent_length
!= write_length
)
651 memcached_quit_server(ptr
, true);
655 ptr
->io_bytes_sent
+= (uint32_t) sent_length
;
657 local_write_ptr
+= sent_length
;
658 write_length
-= (uint32_t) sent_length
;
659 return_length
+= (uint32_t) sent_length
;
662 WATCHPOINT_ASSERT(write_length
== 0);
663 // Need to study this assert() WATCHPOINT_ASSERT(return_length ==
664 // ptr->write_buffer_offset);
666 // if we are a udp server, the begining of the buffer is reserverd for
667 // the upd frame header
668 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
669 ptr
->write_buffer_offset
= UDP_DATAGRAM_HEADER_LENGTH
;
671 ptr
->write_buffer_offset
= 0;
673 return (ssize_t
) return_length
;
677 Eventually we will just kill off the server with the problem.
679 void memcached_io_reset(memcached_server_write_instance_st ptr
)
681 memcached_quit_server(ptr
, true);
685 * Read a given number of bytes from the server and place it into a specific
686 * buffer. Reset the IO channel on this server if an error occurs.
688 memcached_return_t
memcached_safe_read(memcached_server_write_instance_st ptr
,
695 while (offset
< size
)
698 memcached_return_t rc
= memcached_io_read(ptr
, data
+ offset
, size
- offset
,
700 if (rc
!= MEMCACHED_SUCCESS
)
703 offset
+= (size_t) nread
;
706 return MEMCACHED_SUCCESS
;
709 memcached_return_t
memcached_io_readline(memcached_server_write_instance_st ptr
,
713 bool line_complete
= false;
716 while (!line_complete
)
718 if (ptr
->read_buffer_length
== 0)
721 * We don't have any data in the buffer, so let's fill the read
722 * buffer. Call the standard read function to avoid duplicating
726 memcached_return_t rc
= memcached_io_read(ptr
, buffer_ptr
, 1, &nread
);
727 if (rc
!= MEMCACHED_SUCCESS
)
730 if (*buffer_ptr
== '\n')
737 /* Now let's look in the buffer and copy as we go! */
738 while (ptr
->read_buffer_length
&& total_nr
< size
&& !line_complete
)
740 *buffer_ptr
= *ptr
->read_ptr
;
741 if (*buffer_ptr
== '\n')
742 line_complete
= true;
743 --ptr
->read_buffer_length
;
749 if (total_nr
== size
)
750 return MEMCACHED_PROTOCOL_ERROR
;
753 return MEMCACHED_SUCCESS
;
757 * The udp request id consists of two seperate sections
759 * 2) The message number
760 * The thread id should only be set when the memcached_st struct is created
761 * and should not be changed.
763 * The message num is incremented for each new message we send, this function
764 * extracts the message number from message_id, increments it and then
765 * writes the new value back into the header
767 static void increment_udp_message_id(memcached_server_write_instance_st ptr
)
769 struct udp_datagram_header_st
*header
= (struct udp_datagram_header_st
*)ptr
->write_buffer
;
770 uint16_t cur_req
= get_udp_datagram_request_id(header
);
771 int msg_num
= get_msg_num_from_request_id(cur_req
);
772 int thread_id
= get_thread_id_from_request_id(cur_req
);
774 if (((++msg_num
) & UDP_REQUEST_ID_THREAD_MASK
) != 0)
777 header
->request_id
= htons((uint16_t) (thread_id
| msg_num
));
780 memcached_return_t
memcached_io_init_udp_header(memcached_server_write_instance_st ptr
, uint16_t thread_id
)
782 if (thread_id
> UDP_REQUEST_ID_MAX_THREAD_ID
)
783 return MEMCACHED_FAILURE
;
785 struct udp_datagram_header_st
*header
= (struct udp_datagram_header_st
*)ptr
->write_buffer
;
786 header
->request_id
= htons((uint16_t) (generate_udp_request_thread_id(thread_id
)));
787 header
->num_datagrams
= htons(1);
788 header
->sequence_number
= htons(0);
790 return MEMCACHED_SUCCESS
;