1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are
13 * * Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above
17 * copyright notice, this list of conditions and the following disclaimer
18 * in the documentation and/or other materials provided with the
21 * * The names of its contributors may not be used to endorse or
22 * promote products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <libmemcached/common.h>
47 static ssize_t
io_flush(memcached_server_write_instance_st ptr
,
48 const bool with_flush
,
49 memcached_return_t
*error
);
50 static void increment_udp_message_id(memcached_server_write_instance_st ptr
);
52 static memcached_return_t
io_wait(memcached_server_write_instance_st ptr
,
53 memc_read_or_write read_or_write
)
61 if (read_or_write
== MEM_WRITE
) /* write */
64 WATCHPOINT_SET(ptr
->io_wait_count
.write
++);
68 WATCHPOINT_SET(ptr
->io_wait_count
.read
++);
72 ** We are going to block on write, but at least on Solaris we might block
73 ** on write if we haven't read anything from our input buffer..
74 ** Try to purge the input buffer if we don't do any flow control in the
75 ** application layer (just sending a lot of data etc)
76 ** The test is moved down in the purge function to avoid duplication of
79 if (read_or_write
== MEM_WRITE
)
81 memcached_return_t rc
= memcached_purge(ptr
);
82 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_STORED
)
84 return MEMCACHED_FAILURE
;
89 while (--loop_max
) // While loop is for ERESTART or EINTR
91 if (ptr
->root
->poll_timeout
) // Mimic 0 causes timeout behavior (not all platforms do this)
93 error
= poll(&fds
, 1, ptr
->root
->poll_timeout
);
103 WATCHPOINT_IF_LABELED_NUMBER(read_or_write
&& loop_max
< 4, "read() times we had to loop, decremented down from 5", loop_max
);
104 WATCHPOINT_IF_LABELED_NUMBER(!read_or_write
&& loop_max
< 4, "write() times we had to loop, decremented down from 5", loop_max
);
106 return MEMCACHED_SUCCESS
;
108 case 0: // Timeout occured, we let the while() loop do its thing.
109 return memcached_set_error(*ptr
, MEMCACHED_TIMEOUT
, MEMCACHED_AT
);
112 WATCHPOINT_ERRNO(get_socket_errno());
113 switch (get_socket_errno())
115 #ifdef TARGET_OS_LINUX
123 return memcached_set_error(*ptr
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
);
126 return memcached_set_error(*ptr
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
, memcached_literal_param("RLIMIT_NOFILE exceeded, or if OSX the timeout value was invalid"));
129 if (fds
.revents
& POLLERR
)
132 socklen_t len
= sizeof (err
);
133 (void)getsockopt(ptr
->fd
, SOL_SOCKET
, SO_ERROR
, &err
, &len
);
134 ptr
->cached_errno
= (err
== 0) ? get_socket_errno() : err
;
138 ptr
->cached_errno
= get_socket_errno();
140 memcached_quit_server(ptr
, true);
142 return memcached_set_errno(*ptr
, get_socket_errno(), MEMCACHED_AT
);
147 /* Imposssible for anything other then -1 */
148 WATCHPOINT_ASSERT(error
== -1);
149 ptr
->cached_errno
= get_socket_errno();
150 memcached_quit_server(ptr
, true);
152 return memcached_set_error(*ptr
, MEMCACHED_FAILURE
, MEMCACHED_AT
);
155 memcached_return_t
memcached_io_wait_for_write(memcached_server_write_instance_st ptr
)
157 return io_wait(ptr
, MEM_WRITE
);
161 * Try to fill the input buffer for a server with as much
164 * @param ptr the server to pack
166 static bool repack_input_buffer(memcached_server_write_instance_st ptr
)
168 if (ptr
->read_ptr
!= ptr
->read_buffer
)
170 /* Move all of the data to the beginning of the buffer so
171 ** that we can fit more data into the buffer...
173 memmove(ptr
->read_buffer
, ptr
->read_ptr
, ptr
->read_buffer_length
);
174 ptr
->read_ptr
= ptr
->read_buffer
;
175 ptr
->read_data_length
= ptr
->read_buffer_length
;
178 /* There is room in the buffer, try to fill it! */
179 if (ptr
->read_buffer_length
!= MEMCACHED_MAX_BUFFER
)
181 /* Just try a single read to grab what's available */
182 ssize_t nr
= recv(ptr
->fd
,
183 ptr
->read_ptr
+ ptr
->read_data_length
,
184 MEMCACHED_MAX_BUFFER
- ptr
->read_data_length
,
189 ptr
->read_data_length
+= (size_t)nr
;
190 ptr
->read_buffer_length
+= (size_t)nr
;
198 * If the we have callbacks connected to this server structure
199 * we may start process the input queue and fire the callbacks
200 * for the incomming messages. This function is _only_ called
201 * when the input buffer is full, so that we _know_ that we have
202 * at least _one_ message to process.
204 * @param ptr the server to star processing iput messages for
205 * @return true if we processed anything, false otherwise
207 static bool process_input_buffer(memcached_server_write_instance_st ptr
)
210 ** We might be able to process some of the response messages if we
211 ** have a callback set up
213 if (ptr
->root
->callbacks
!= NULL
&& ptr
->root
->flags
.use_udp
== false)
216 * We might have responses... try to read them out and fire
219 memcached_callback_st cb
= *ptr
->root
->callbacks
;
221 memcached_set_processing_input((memcached_st
*)ptr
->root
, true);
223 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
224 memcached_return_t error
;
225 memcached_st
*root
= (memcached_st
*)ptr
->root
;
226 error
= memcached_response(ptr
, buffer
, sizeof(buffer
),
229 memcached_set_processing_input(root
, false);
231 if (error
== MEMCACHED_SUCCESS
)
233 for (unsigned int x
= 0; x
< cb
.number_of_callback
; x
++)
235 error
= (*cb
.callback
[x
])(ptr
->root
, &root
->result
, cb
.context
);
236 if (error
!= MEMCACHED_SUCCESS
)
240 /* @todo what should I do with the error message??? */
242 /* @todo what should I do with other error messages?? */
249 #if 0 // Dead code, this should be removed.
250 void memcached_io_preread(memcached_st
*ptr
)
256 for (x
= 0; x
< memcached_server_count(ptr
); x
++)
258 if (memcached_server_response_count(ptr
, x
) &&
259 ptr
->hosts
[x
].read_data_length
< MEMCACHED_MAX_BUFFER
)
263 data_read
= recv(ptr
->hosts
[x
].fd
,
264 ptr
->hosts
[x
].read_ptr
+ ptr
->hosts
[x
].read_data_length
,
265 MEMCACHED_MAX_BUFFER
- ptr
->hosts
[x
].read_data_length
, 0);
266 if (data_read
== SOCKET_ERROR
)
269 ptr
->hosts
[x
].read_buffer_length
+= data_read
;
270 ptr
->hosts
[x
].read_data_length
+= data_read
;
276 memcached_return_t
memcached_io_read(memcached_server_write_instance_st ptr
,
277 void *buffer
, size_t length
, ssize_t
*nread
)
281 buffer_ptr
= static_cast<char *>(buffer
);
285 if (not ptr
->read_buffer_length
)
291 data_read
= recv(ptr
->fd
, ptr
->read_buffer
, MEMCACHED_MAX_BUFFER
, 0);
296 else if (data_read
== SOCKET_ERROR
)
298 switch (get_socket_errno())
305 #ifdef TARGET_OS_LINUX
308 if (memcached_success(io_wait(ptr
, MEM_READ
)))
317 memcached_quit_server(ptr
, true);
319 return memcached_set_errno(*ptr
, get_socket_errno(), MEMCACHED_AT
);
326 EOF. Any data received so far is incomplete
327 so discard it. This always reads by byte in case of TCP
328 and protocol enforcement happens at memcached_response()
329 looking for '\n'. We do not care for UDB which requests 8 bytes
330 at once. Generally, this means that connection went away. Since
331 for blocking I/O we do not return 0 and for non-blocking case
332 it will return EGAIN if data is not immediatly available.
334 WATCHPOINT_STRING("We had a zero length recv()");
335 memcached_quit_server(ptr
, true);
337 return memcached_set_error(*ptr
, MEMCACHED_UNKNOWN_READ_FAILURE
, MEMCACHED_AT
);
341 ptr
->io_bytes_sent
= 0;
342 ptr
->read_data_length
= (size_t) data_read
;
343 ptr
->read_buffer_length
= (size_t) data_read
;
344 ptr
->read_ptr
= ptr
->read_buffer
;
351 difference
= (length
> ptr
->read_buffer_length
) ? ptr
->read_buffer_length
: length
;
353 memcpy(buffer_ptr
, ptr
->read_ptr
, difference
);
354 length
-= difference
;
355 ptr
->read_ptr
+= difference
;
356 ptr
->read_buffer_length
-= difference
;
357 buffer_ptr
+= difference
;
361 *buffer_ptr
= *ptr
->read_ptr
;
363 ptr
->read_buffer_length
--;
369 ptr
->server_failure_counter
= 0;
370 *nread
= (ssize_t
)(buffer_ptr
- (char*)buffer
);
371 return MEMCACHED_SUCCESS
;
374 static ssize_t
_io_write(memcached_server_write_instance_st ptr
,
375 const void *buffer
, size_t length
, bool with_flush
)
377 size_t original_length
;
378 const char* buffer_ptr
;
380 WATCHPOINT_ASSERT(ptr
->fd
!= INVALID_SOCKET
);
382 original_length
= length
;
383 buffer_ptr
= static_cast<const char *>(buffer
);
391 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
393 //UDP does not support partial writes
394 buffer_end
= MAX_UDP_DATAGRAM_LENGTH
;
395 should_write
= length
;
396 if (ptr
->write_buffer_offset
+ should_write
> buffer_end
)
403 buffer_end
= MEMCACHED_MAX_BUFFER
;
404 should_write
= buffer_end
- ptr
->write_buffer_offset
;
405 should_write
= (should_write
< length
) ? should_write
: length
;
408 write_ptr
= ptr
->write_buffer
+ ptr
->write_buffer_offset
;
409 memcpy(write_ptr
, buffer_ptr
, should_write
);
410 ptr
->write_buffer_offset
+= should_write
;
411 buffer_ptr
+= should_write
;
412 length
-= should_write
;
414 if (ptr
->write_buffer_offset
== buffer_end
&& ptr
->type
!= MEMCACHED_CONNECTION_UDP
)
416 WATCHPOINT_ASSERT(ptr
->fd
!= INVALID_SOCKET
);
418 memcached_return_t rc
;
419 ssize_t sent_length
= io_flush(ptr
, with_flush
, &rc
);
420 if (sent_length
== -1)
425 /* If io_flush calls memcached_purge, sent_length may be 0 */
426 unlikely (sent_length
!= 0)
428 WATCHPOINT_ASSERT(sent_length
== (ssize_t
)buffer_end
);
435 memcached_return_t rc
;
436 WATCHPOINT_ASSERT(ptr
->fd
!= INVALID_SOCKET
);
437 if (io_flush(ptr
, with_flush
, &rc
) == -1)
443 return (ssize_t
) original_length
;
446 ssize_t
memcached_io_write(memcached_server_write_instance_st ptr
,
447 const void *buffer
, size_t length
, bool with_flush
)
449 return _io_write(ptr
, buffer
, length
, with_flush
);
452 ssize_t
memcached_io_writev(memcached_server_write_instance_st ptr
,
453 const struct libmemcached_io_vector_st
*vector
,
454 size_t number_of
, bool with_flush
)
458 for (size_t x
= 0; x
< number_of
; x
++, vector
++)
462 if ((returnable
= _io_write(ptr
, vector
->buffer
, vector
->length
, false)) == -1)
471 if (memcached_io_write(ptr
, NULL
, 0, true) == -1)
481 void memcached_io_close(memcached_server_write_instance_st ptr
)
483 if (ptr
->fd
== INVALID_SOCKET
)
488 /* in case of death shutdown to avoid blocking at close() */
489 if (shutdown(ptr
->fd
, SHUT_RDWR
) == SOCKET_ERROR
&& get_socket_errno() != ENOTCONN
)
491 WATCHPOINT_NUMBER(ptr
->fd
);
492 WATCHPOINT_ERRNO(get_socket_errno());
493 WATCHPOINT_ASSERT(get_socket_errno());
496 if (closesocket(ptr
->fd
) == SOCKET_ERROR
)
498 WATCHPOINT_ERRNO(get_socket_errno());
500 ptr
->state
= MEMCACHED_SERVER_STATE_NEW
;
501 ptr
->fd
= INVALID_SOCKET
;
504 memcached_server_write_instance_st
memcached_io_get_readable_server(memcached_st
*memc
)
506 #define MAX_SERVERS_TO_POLL 100
507 struct pollfd fds
[MAX_SERVERS_TO_POLL
];
508 unsigned int host_index
= 0;
511 x
< memcached_server_count(memc
) && host_index
< MAX_SERVERS_TO_POLL
;
514 memcached_server_write_instance_st instance
=
515 memcached_server_instance_fetch(memc
, x
);
517 if (instance
->read_buffer_length
> 0) /* I have data in the buffer */
520 if (memcached_server_response_count(instance
) > 0)
522 fds
[host_index
].events
= POLLIN
;
523 fds
[host_index
].revents
= 0;
524 fds
[host_index
].fd
= instance
->fd
;
531 /* We have 0 or 1 server with pending events.. */
532 for (uint32_t x
= 0; x
< memcached_server_count(memc
); ++x
)
534 memcached_server_write_instance_st instance
=
535 memcached_server_instance_fetch(memc
, x
);
537 if (memcached_server_response_count(instance
) > 0)
546 switch (poll(fds
, host_index
, memc
->poll_timeout
))
549 memcached_set_errno(*memc
, get_socket_errno(), MEMCACHED_AT
);
555 for (size_t x
= 0; x
< host_index
; ++x
)
557 if (fds
[x
].revents
& POLLIN
)
559 for (uint32_t y
= 0; y
< memcached_server_count(memc
); ++y
)
561 memcached_server_write_instance_st instance
=
562 memcached_server_instance_fetch(memc
, y
);
564 if (instance
->fd
== fds
[x
].fd
)
574 static ssize_t
io_flush(memcached_server_write_instance_st ptr
,
575 const bool with_flush
,
576 memcached_return_t
*error
)
579 ** We might want to purge the input buffer if we haven't consumed
580 ** any output yet... The test for the limits is the purge is inline
581 ** in the purge function to avoid duplicating the logic..
584 memcached_return_t rc
;
585 WATCHPOINT_ASSERT(ptr
->fd
!= INVALID_SOCKET
);
586 rc
= memcached_purge(ptr
);
588 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_STORED
)
594 size_t return_length
;
595 char *local_write_ptr
= ptr
->write_buffer
;
596 size_t write_length
= ptr
->write_buffer_offset
;
598 *error
= MEMCACHED_SUCCESS
;
600 WATCHPOINT_ASSERT(ptr
->fd
!= INVALID_SOCKET
);
602 // UDP Sanity check, make sure that we are not sending somthing too big
603 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
&& write_length
> MAX_UDP_DATAGRAM_LENGTH
)
605 *error
= MEMCACHED_WRITE_FAILURE
;
609 if (ptr
->write_buffer_offset
== 0 || (ptr
->type
== MEMCACHED_CONNECTION_UDP
610 && ptr
->write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
))
615 /* Looking for memory overflows */
617 if (write_length
== MEMCACHED_MAX_BUFFER
)
618 WATCHPOINT_ASSERT(ptr
->write_buffer
== local_write_ptr
);
619 WATCHPOINT_ASSERT((ptr
->write_buffer
+ MEMCACHED_MAX_BUFFER
) >= (local_write_ptr
+ write_length
));
625 WATCHPOINT_ASSERT(ptr
->fd
!= INVALID_SOCKET
);
626 WATCHPOINT_ASSERT(write_length
> 0);
628 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
629 increment_udp_message_id(ptr
);
631 WATCHPOINT_ASSERT(ptr
->fd
!= INVALID_SOCKET
);
634 sent_length
= send(ptr
->fd
, local_write_ptr
, write_length
, MSG_NOSIGNAL
|MSG_DONTWAIT
);
638 sent_length
= send(ptr
->fd
, local_write_ptr
, write_length
, MSG_NOSIGNAL
|MSG_DONTWAIT
|MSG_MORE
);
641 if (sent_length
== SOCKET_ERROR
)
643 ptr
->cached_errno
= get_socket_errno();
644 #if 0 // @todo I should look at why we hit this bit of code hard frequently
645 WATCHPOINT_ERRNO(get_socket_errno());
646 WATCHPOINT_NUMBER(get_socket_errno());
648 switch (get_socket_errno())
658 * We may be blocked on write because the input buffer
659 * is full. Let's check if we have room in our input
660 * buffer for more data and retry the write before
663 if (repack_input_buffer(ptr
) ||
664 process_input_buffer(ptr
))
667 memcached_return_t rc
= io_wait(ptr
, MEM_WRITE
);
668 if (memcached_success(rc
))
672 else if (rc
== MEMCACHED_TIMEOUT
)
674 *error
= memcached_set_error(*ptr
, MEMCACHED_TIMEOUT
, MEMCACHED_AT
);
678 memcached_quit_server(ptr
, true);
679 *error
= memcached_set_errno(*ptr
, get_socket_errno(), MEMCACHED_AT
);
685 memcached_quit_server(ptr
, true);
686 *error
= memcached_set_errno(*ptr
, get_socket_errno(), MEMCACHED_AT
);
687 WATCHPOINT_ASSERT(ptr
->fd
== -1);
692 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
and
693 (size_t)sent_length
!= write_length
)
695 memcached_quit_server(ptr
, true);
696 *error
= memcached_set_error(*ptr
, MEMCACHED_WRITE_FAILURE
, MEMCACHED_AT
);
700 ptr
->io_bytes_sent
+= (uint32_t) sent_length
;
702 local_write_ptr
+= sent_length
;
703 write_length
-= (uint32_t) sent_length
;
704 return_length
+= (uint32_t) sent_length
;
707 WATCHPOINT_ASSERT(write_length
== 0);
708 // Need to study this assert() WATCHPOINT_ASSERT(return_length ==
709 // ptr->write_buffer_offset);
711 // if we are a udp server, the begining of the buffer is reserverd for
712 // the upd frame header
713 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
714 ptr
->write_buffer_offset
= UDP_DATAGRAM_HEADER_LENGTH
;
716 ptr
->write_buffer_offset
= 0;
718 return (ssize_t
) return_length
;
722 Eventually we will just kill off the server with the problem.
724 void memcached_io_reset(memcached_server_write_instance_st ptr
)
726 memcached_quit_server(ptr
, true);
730 * Read a given number of bytes from the server and place it into a specific
731 * buffer. Reset the IO channel on this server if an error occurs.
733 memcached_return_t
memcached_safe_read(memcached_server_write_instance_st ptr
,
738 char *data
= static_cast<char *>(dta
);
740 while (offset
< size
)
743 memcached_return_t rc
= memcached_io_read(ptr
, data
+ offset
, size
- offset
,
745 if (rc
!= MEMCACHED_SUCCESS
)
750 offset
+= (size_t) nread
;
753 return MEMCACHED_SUCCESS
;
756 memcached_return_t
memcached_io_readline(memcached_server_write_instance_st ptr
,
760 bool line_complete
= false;
763 while (!line_complete
)
765 if (ptr
->read_buffer_length
== 0)
768 * We don't have any data in the buffer, so let's fill the read
769 * buffer. Call the standard read function to avoid duplicating
773 memcached_return_t rc
= memcached_io_read(ptr
, buffer_ptr
, 1, &nread
);
774 if (rc
!= MEMCACHED_SUCCESS
)
779 if (*buffer_ptr
== '\n')
786 /* Now let's look in the buffer and copy as we go! */
787 while (ptr
->read_buffer_length
&& total_nr
< size
&& !line_complete
)
789 *buffer_ptr
= *ptr
->read_ptr
;
790 if (*buffer_ptr
== '\n')
791 line_complete
= true;
792 --ptr
->read_buffer_length
;
798 if (total_nr
== size
)
799 return MEMCACHED_PROTOCOL_ERROR
;
802 return MEMCACHED_SUCCESS
;
806 * The udp request id consists of two seperate sections
808 * 2) The message number
809 * The thread id should only be set when the memcached_st struct is created
810 * and should not be changed.
812 * The message num is incremented for each new message we send, this function
813 * extracts the message number from message_id, increments it and then
814 * writes the new value back into the header
816 static void increment_udp_message_id(memcached_server_write_instance_st ptr
)
818 struct udp_datagram_header_st
*header
= (struct udp_datagram_header_st
*)ptr
->write_buffer
;
819 uint16_t cur_req
= get_udp_datagram_request_id(header
);
820 int msg_num
= get_msg_num_from_request_id(cur_req
);
821 int thread_id
= get_thread_id_from_request_id(cur_req
);
823 if (((++msg_num
) & UDP_REQUEST_ID_THREAD_MASK
) != 0)
826 header
->request_id
= htons((uint16_t) (thread_id
| msg_num
));
829 memcached_return_t
memcached_io_init_udp_header(memcached_server_write_instance_st ptr
, uint16_t thread_id
)
831 if (thread_id
> UDP_REQUEST_ID_MAX_THREAD_ID
)
832 return MEMCACHED_FAILURE
;
834 struct udp_datagram_header_st
*header
= (struct udp_datagram_header_st
*)ptr
->write_buffer
;
835 header
->request_id
= htons((uint16_t) (generate_udp_request_thread_id(thread_id
)));
836 header
->num_datagrams
= htons(1);
837 header
->sequence_number
= htons(0);
839 return MEMCACHED_SUCCESS
;