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!
20 static ssize_t
io_flush(memcached_server_write_instance_st ptr
,
21 memcached_return_t
*error
);
22 static void increment_udp_message_id(memcached_server_write_instance_st ptr
);
24 static memcached_return_t
io_wait(memcached_server_write_instance_st ptr
,
25 memc_read_or_write read_or_write
)
33 if (read_or_write
== MEM_WRITE
) /* write */
36 WATCHPOINT_SET(ptr
->io_wait_count
.write
++);
40 WATCHPOINT_SET(ptr
->io_wait_count
.read
++);
44 ** We are going to block on write, but at least on Solaris we might block
45 ** on write if we haven't read anything from our input buffer..
46 ** Try to purge the input buffer if we don't do any flow control in the
47 ** application layer (just sending a lot of data etc)
48 ** The test is moved down in the purge function to avoid duplication of
51 if (read_or_write
== MEM_WRITE
)
53 memcached_return_t rc
= memcached_purge(ptr
);
54 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_STORED
)
55 return MEMCACHED_FAILURE
;
59 while (--loop_max
) // While loop is for ERESTART or EINTR
61 error
= poll(&fds
, 1, ptr
->root
->poll_timeout
);
66 WATCHPOINT_IF_LABELED_NUMBER(read_or_write
&& loop_max
< 4, "read() times we had to loop, decremented down from 5", loop_max
);
67 WATCHPOINT_IF_LABELED_NUMBER(!read_or_write
&& loop_max
< 4, "write() times we had to loop, decremented down from 5", loop_max
);
69 return MEMCACHED_SUCCESS
;
70 case 0: // Timeout occured, we let the while() loop do its thing.
71 return MEMCACHED_TIMEOUT
;
73 WATCHPOINT_ERRNO(get_socket_errno());
74 switch (get_socket_errno())
76 #ifdef TARGET_OS_LINUX
82 if (fds
.revents
& POLLERR
)
85 socklen_t len
= sizeof (err
);
86 (void)getsockopt(ptr
->fd
, SOL_SOCKET
, SO_ERROR
, &err
, &len
);
87 ptr
->cached_errno
= (err
== 0) ? get_socket_errno() : err
;
91 ptr
->cached_errno
= get_socket_errno();
93 memcached_quit_server(ptr
, true);
95 return MEMCACHED_FAILURE
;
100 /* Imposssible for anything other then -1 */
101 WATCHPOINT_ASSERT(error
== -1);
102 ptr
->cached_errno
= get_socket_errno();
103 memcached_quit_server(ptr
, true);
105 return MEMCACHED_FAILURE
;
108 memcached_return_t
memcached_io_wait_for_write(memcached_server_write_instance_st ptr
)
110 return io_wait(ptr
, MEM_WRITE
);
114 * Try to fill the input buffer for a server with as much
117 * @param ptr the server to pack
119 static bool repack_input_buffer(memcached_server_write_instance_st ptr
)
121 if (ptr
->read_ptr
!= ptr
->read_buffer
)
123 /* Move all of the data to the beginning of the buffer so
124 ** that we can fit more data into the buffer...
126 memmove(ptr
->read_buffer
, ptr
->read_ptr
, ptr
->read_buffer_length
);
127 ptr
->read_ptr
= ptr
->read_buffer
;
128 ptr
->read_data_length
= ptr
->read_buffer_length
;
131 /* There is room in the buffer, try to fill it! */
132 if (ptr
->read_buffer_length
!= MEMCACHED_MAX_BUFFER
)
134 /* Just try a single read to grab what's available */
135 ssize_t nr
= recv(ptr
->fd
,
136 ptr
->read_ptr
+ ptr
->read_data_length
,
137 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
= recv(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
, 0);
253 if (data_read
== SOCKET_ERROR
)
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
= recv(ptr
->fd
, ptr
->read_buffer
, MEMCACHED_MAX_BUFFER
, 0);
283 else if (data_read
== SOCKET_ERROR
)
285 ptr
->cached_errno
= get_socket_errno();
286 memcached_return_t rc
= MEMCACHED_ERRNO
;
287 switch (get_socket_errno())
294 #ifdef TARGET_OS_LINUX
297 if ((rc
= io_wait(ptr
, MEM_READ
)) == MEMCACHED_SUCCESS
)
303 memcached_quit_server(ptr
, true);
312 EOF. Any data received so far is incomplete
313 so discard it. This always reads by byte in case of TCP
314 and protocol enforcement happens at memcached_response()
315 looking for '\n'. We do not care for UDB which requests 8 bytes
316 at once. Generally, this means that connection went away. Since
317 for blocking I/O we do not return 0 and for non-blocking case
318 it will return EGAIN if data is not immediatly available.
320 WATCHPOINT_STRING("We had a zero length recv()");
321 memcached_quit_server(ptr
, true);
323 return MEMCACHED_UNKNOWN_READ_FAILURE
;
327 ptr
->io_bytes_sent
= 0;
328 ptr
->read_data_length
= (size_t) data_read
;
329 ptr
->read_buffer_length
= (size_t) data_read
;
330 ptr
->read_ptr
= ptr
->read_buffer
;
337 difference
= (length
> ptr
->read_buffer_length
) ? ptr
->read_buffer_length
: length
;
339 memcpy(buffer_ptr
, ptr
->read_ptr
, difference
);
340 length
-= difference
;
341 ptr
->read_ptr
+= difference
;
342 ptr
->read_buffer_length
-= difference
;
343 buffer_ptr
+= difference
;
347 *buffer_ptr
= *ptr
->read_ptr
;
349 ptr
->read_buffer_length
--;
355 ptr
->server_failure_counter
= 0;
356 *nread
= (ssize_t
)(buffer_ptr
- (char*)buffer
);
357 return MEMCACHED_SUCCESS
;
360 static ssize_t
_io_write(memcached_server_write_instance_st ptr
,
361 const void *buffer
, size_t length
, bool with_flush
)
363 size_t original_length
;
364 const char* buffer_ptr
;
366 WATCHPOINT_ASSERT(ptr
->fd
!= INVALID_SOCKET
);
368 original_length
= length
;
371 /* more writable data is coming if a flush isn't required, so delay send */
374 memcached_io_cork_push(ptr
);
383 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
385 //UDP does not support partial writes
386 buffer_end
= MAX_UDP_DATAGRAM_LENGTH
;
387 should_write
= length
;
388 if (ptr
->write_buffer_offset
+ should_write
> buffer_end
)
395 buffer_end
= MEMCACHED_MAX_BUFFER
;
396 should_write
= buffer_end
- ptr
->write_buffer_offset
;
397 should_write
= (should_write
< length
) ? should_write
: length
;
400 write_ptr
= ptr
->write_buffer
+ ptr
->write_buffer_offset
;
401 memcpy(write_ptr
, buffer_ptr
, should_write
);
402 ptr
->write_buffer_offset
+= should_write
;
403 buffer_ptr
+= should_write
;
404 length
-= should_write
;
406 if (ptr
->write_buffer_offset
== buffer_end
&& ptr
->type
!= MEMCACHED_CONNECTION_UDP
)
408 memcached_return_t rc
;
411 WATCHPOINT_ASSERT(ptr
->fd
!= INVALID_SOCKET
);
412 sent_length
= io_flush(ptr
, &rc
);
413 if (sent_length
== -1)
418 /* If io_flush calls memcached_purge, sent_length may be 0 */
419 unlikely (sent_length
!= 0)
421 WATCHPOINT_ASSERT(sent_length
== (ssize_t
)buffer_end
);
428 memcached_return_t rc
;
429 WATCHPOINT_ASSERT(ptr
->fd
!= INVALID_SOCKET
);
430 if (io_flush(ptr
, &rc
) == -1)
435 memcached_io_cork_pop(ptr
);
438 return (ssize_t
) original_length
;
441 ssize_t
memcached_io_write(memcached_server_write_instance_st ptr
,
442 const void *buffer
, size_t length
, bool with_flush
)
444 return _io_write(ptr
, buffer
, length
, with_flush
);
447 ssize_t
memcached_io_writev(memcached_server_write_instance_st ptr
,
448 const struct libmemcached_io_vector_st
*vector
,
449 size_t number_of
, bool with_flush
)
453 for (size_t x
= 0; x
< number_of
; x
++, vector
++)
457 if ((returnable
= _io_write(ptr
, vector
->buffer
, vector
->length
, false)) == -1)
466 if (memcached_io_write(ptr
, NULL
, 0, true) == -1)
476 memcached_return_t
memcached_io_close(memcached_server_write_instance_st ptr
)
478 if (ptr
->fd
== INVALID_SOCKET
)
480 return MEMCACHED_SUCCESS
;
483 /* in case of death shutdown to avoid blocking at close() */
484 if (shutdown(ptr
->fd
, SHUT_RDWR
) == SOCKET_ERROR
&& get_socket_errno() != ENOTCONN
)
486 WATCHPOINT_NUMBER(ptr
->fd
);
487 WATCHPOINT_ERRNO(get_socket_errno());
488 WATCHPOINT_ASSERT(get_socket_errno());
491 if (closesocket(ptr
->fd
) == SOCKET_ERROR
)
493 WATCHPOINT_ERRNO(get_socket_errno());
496 return MEMCACHED_SUCCESS
;
499 memcached_server_write_instance_st
memcached_io_get_readable_server(memcached_st
*memc
)
501 #define MAX_SERVERS_TO_POLL 100
502 struct pollfd fds
[MAX_SERVERS_TO_POLL
];
503 unsigned int host_index
= 0;
506 x
< memcached_server_count(memc
) && host_index
< MAX_SERVERS_TO_POLL
;
509 memcached_server_write_instance_st instance
=
510 memcached_server_instance_fetch(memc
, x
);
512 if (instance
->read_buffer_length
> 0) /* I have data in the buffer */
515 if (memcached_server_response_count(instance
) > 0)
517 fds
[host_index
].events
= POLLIN
;
518 fds
[host_index
].revents
= 0;
519 fds
[host_index
].fd
= instance
->fd
;
526 /* We have 0 or 1 server with pending events.. */
527 for (uint32_t x
= 0; x
< memcached_server_count(memc
); ++x
)
529 memcached_server_write_instance_st instance
=
530 memcached_server_instance_fetch(memc
, x
);
532 if (memcached_server_response_count(instance
) > 0)
541 int err
= poll(fds
, host_index
, memc
->poll_timeout
);
544 memcached_set_errno(memc
, get_socket_errno(), NULL
);
549 for (size_t x
= 0; x
< host_index
; ++x
)
551 if (fds
[x
].revents
& POLLIN
)
553 for (uint32_t y
= 0; y
< memcached_server_count(memc
); ++y
)
555 memcached_server_write_instance_st instance
=
556 memcached_server_instance_fetch(memc
, y
);
558 if (instance
->fd
== fds
[x
].fd
)
568 static ssize_t
io_flush(memcached_server_write_instance_st ptr
,
569 memcached_return_t
*error
)
572 ** We might want to purge the input buffer if we haven't consumed
573 ** any output yet... The test for the limits is the purge is inline
574 ** in the purge function to avoid duplicating the logic..
577 memcached_return_t rc
;
578 WATCHPOINT_ASSERT(ptr
->fd
!= INVALID_SOCKET
);
579 rc
= memcached_purge(ptr
);
581 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_STORED
)
587 size_t return_length
;
588 char *local_write_ptr
= ptr
->write_buffer
;
589 size_t write_length
= ptr
->write_buffer_offset
;
591 *error
= MEMCACHED_SUCCESS
;
593 WATCHPOINT_ASSERT(ptr
->fd
!= INVALID_SOCKET
);
595 // UDP Sanity check, make sure that we are not sending somthing too big
596 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
&& write_length
> MAX_UDP_DATAGRAM_LENGTH
)
601 if (ptr
->write_buffer_offset
== 0 || (ptr
->type
== MEMCACHED_CONNECTION_UDP
602 && ptr
->write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
))
605 /* Looking for memory overflows */
607 if (write_length
== MEMCACHED_MAX_BUFFER
)
608 WATCHPOINT_ASSERT(ptr
->write_buffer
== local_write_ptr
);
609 WATCHPOINT_ASSERT((ptr
->write_buffer
+ MEMCACHED_MAX_BUFFER
) >= (local_write_ptr
+ write_length
));
615 WATCHPOINT_ASSERT(ptr
->fd
!= INVALID_SOCKET
);
616 WATCHPOINT_ASSERT(write_length
> 0);
618 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
619 increment_udp_message_id(ptr
);
621 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
622 sent_length
= send(ptr
->fd
, local_write_ptr
, write_length
, MSG_NOSIGNAL
|MSG_DONTWAIT
);
623 if (sent_length
== SOCKET_ERROR
)
625 ptr
->cached_errno
= get_socket_errno();
626 #if 0 // @todo I should look at why we hit this bit of code hard frequently
627 WATCHPOINT_ERRNO(get_socket_errno());
628 WATCHPOINT_NUMBER(get_socket_errno());
630 switch (get_socket_errno())
640 * We may be blocked on write because the input buffer
641 * is full. Let's check if we have room in our input
642 * buffer for more data and retry the write before
645 if (repack_input_buffer(ptr
) ||
646 process_input_buffer(ptr
))
649 memcached_return_t rc
;
650 rc
= io_wait(ptr
, MEM_WRITE
);
652 if (rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_TIMEOUT
)
655 memcached_quit_server(ptr
, true);
661 memcached_quit_server(ptr
, true);
662 *error
= MEMCACHED_ERRNO
;
663 WATCHPOINT_ASSERT(ptr
->fd
== -1);
668 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
&&
669 (size_t)sent_length
!= write_length
)
671 memcached_quit_server(ptr
, true);
675 ptr
->io_bytes_sent
+= (uint32_t) sent_length
;
677 local_write_ptr
+= sent_length
;
678 write_length
-= (uint32_t) sent_length
;
679 return_length
+= (uint32_t) sent_length
;
682 WATCHPOINT_ASSERT(write_length
== 0);
683 // Need to study this assert() WATCHPOINT_ASSERT(return_length ==
684 // ptr->write_buffer_offset);
686 // if we are a udp server, the begining of the buffer is reserverd for
687 // the upd frame header
688 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
689 ptr
->write_buffer_offset
= UDP_DATAGRAM_HEADER_LENGTH
;
691 ptr
->write_buffer_offset
= 0;
693 return (ssize_t
) return_length
;
697 Eventually we will just kill off the server with the problem.
699 void memcached_io_reset(memcached_server_write_instance_st ptr
)
701 memcached_quit_server(ptr
, true);
705 * Read a given number of bytes from the server and place it into a specific
706 * buffer. Reset the IO channel on this server if an error occurs.
708 memcached_return_t
memcached_safe_read(memcached_server_write_instance_st ptr
,
715 while (offset
< size
)
718 memcached_return_t rc
= memcached_io_read(ptr
, data
+ offset
, size
- offset
,
720 if (rc
!= MEMCACHED_SUCCESS
)
723 offset
+= (size_t) nread
;
726 return MEMCACHED_SUCCESS
;
729 memcached_return_t
memcached_io_readline(memcached_server_write_instance_st ptr
,
733 bool line_complete
= false;
736 while (!line_complete
)
738 if (ptr
->read_buffer_length
== 0)
741 * We don't have any data in the buffer, so let's fill the read
742 * buffer. Call the standard read function to avoid duplicating
746 memcached_return_t rc
= memcached_io_read(ptr
, buffer_ptr
, 1, &nread
);
747 if (rc
!= MEMCACHED_SUCCESS
)
750 if (*buffer_ptr
== '\n')
757 /* Now let's look in the buffer and copy as we go! */
758 while (ptr
->read_buffer_length
&& total_nr
< size
&& !line_complete
)
760 *buffer_ptr
= *ptr
->read_ptr
;
761 if (*buffer_ptr
== '\n')
762 line_complete
= true;
763 --ptr
->read_buffer_length
;
769 if (total_nr
== size
)
770 return MEMCACHED_PROTOCOL_ERROR
;
773 return MEMCACHED_SUCCESS
;
777 * The udp request id consists of two seperate sections
779 * 2) The message number
780 * The thread id should only be set when the memcached_st struct is created
781 * and should not be changed.
783 * The message num is incremented for each new message we send, this function
784 * extracts the message number from message_id, increments it and then
785 * writes the new value back into the header
787 static void increment_udp_message_id(memcached_server_write_instance_st ptr
)
789 struct udp_datagram_header_st
*header
= (struct udp_datagram_header_st
*)ptr
->write_buffer
;
790 uint16_t cur_req
= get_udp_datagram_request_id(header
);
791 int msg_num
= get_msg_num_from_request_id(cur_req
);
792 int thread_id
= get_thread_id_from_request_id(cur_req
);
794 if (((++msg_num
) & UDP_REQUEST_ID_THREAD_MASK
) != 0)
797 header
->request_id
= htons((uint16_t) (thread_id
| msg_num
));
800 memcached_return_t
memcached_io_init_udp_header(memcached_server_write_instance_st ptr
, uint16_t thread_id
)
802 if (thread_id
> UDP_REQUEST_ID_MAX_THREAD_ID
)
803 return MEMCACHED_FAILURE
;
805 struct udp_datagram_header_st
*header
= (struct udp_datagram_header_st
*)ptr
->write_buffer
;
806 header
->request_id
= htons((uint16_t) (generate_udp_request_thread_id(thread_id
)));
807 header
->num_datagrams
= htons(1);
808 header
->sequence_number
= htons(0);
810 return MEMCACHED_SUCCESS
;