ICC fixes
[awesomized/libmemcached] / libmemcached / io.c
1 /* LibMemcached
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary: Server IO, Not public!
9 *
10 */
11
12
13 #include "common.h"
14 #include <sys/select.h>
15 #include <poll.h>
16
17 typedef enum {
18 MEM_READ,
19 MEM_WRITE
20 } memc_read_or_write;
21
22 static ssize_t io_flush(memcached_server_st *ptr, memcached_return_t *error);
23 static void increment_udp_message_id(memcached_server_st *ptr);
24
25 static memcached_return_t io_wait(memcached_server_st *ptr,
26 memc_read_or_write read_or_write)
27 {
28 struct pollfd fds= {
29 .fd= ptr->fd,
30 .events = POLLIN
31 };
32 int error;
33
34 unlikely (read_or_write == MEM_WRITE) /* write */
35 fds.events= POLLOUT;
36
37 /*
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
43 ** the test.
44 */
45 if (read_or_write == MEM_WRITE)
46 {
47 memcached_return_t rc= memcached_purge(ptr);
48 if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_STORED)
49 return MEMCACHED_FAILURE;
50 }
51
52 int timeout= ptr->root->poll_timeout;
53 if (ptr->root->flags.no_block == false)
54 timeout= -1;
55
56 error= poll(&fds, 1, timeout);
57
58 if (error == 1)
59 return MEMCACHED_SUCCESS;
60 else if (error == 0)
61 return MEMCACHED_TIMEOUT;
62
63 /* Imposssible for anything other then -1 */
64 WATCHPOINT_ASSERT(error == -1);
65 memcached_quit_server(ptr, 1);
66
67 return MEMCACHED_FAILURE;
68 }
69
70 /**
71 * Try to fill the input buffer for a server with as much
72 * data as possible.
73 *
74 * @param ptr the server to pack
75 */
76 static bool repack_input_buffer(memcached_server_st *ptr)
77 {
78 if (ptr->read_ptr != ptr->read_buffer)
79 {
80 /* Move all of the data to the beginning of the buffer so
81 ** that we can fit more data into the buffer...
82 */
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;
86 }
87
88 /* There is room in the buffer, try to fill it! */
89 if (ptr->read_buffer_length != MEMCACHED_MAX_BUFFER)
90 {
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);
95
96 if (nr > 0)
97 {
98 ptr->read_data_length+= (size_t)nr;
99 ptr->read_buffer_length+= (size_t)nr;
100 return true;
101 }
102 }
103 return false;
104 }
105
106 /**
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.
112 *
113 * @param ptr the server to star processing iput messages for
114 * @return true if we processed anything, false otherwise
115 */
116 static bool process_input_buffer(memcached_server_st *ptr)
117 {
118 /*
119 ** We might be able to process some of the response messages if we
120 ** have a callback set up
121 */
122 if (ptr->root->callbacks != NULL && ptr->root->flags.use_udp == false)
123 {
124 /*
125 * We might have responses... try to read them out and fire
126 * callbacks
127 */
128 memcached_callback_st cb= *ptr->root->callbacks;
129
130 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
131 memcached_return_t error;
132 error= memcached_response(ptr, buffer, sizeof(buffer),
133 &ptr->root->result);
134 if (error == MEMCACHED_SUCCESS)
135 {
136 for (unsigned int x= 0; x < cb.number_of_callback; x++)
137 {
138 error= (*cb.callback[x])(ptr->root, &ptr->root->result, cb.context);
139 if (error != MEMCACHED_SUCCESS)
140 break;
141 }
142
143 /* @todo what should I do with the error message??? */
144 }
145 /* @todo what should I do with other error messages?? */
146 return true;
147 }
148
149 return false;
150 }
151
152 #ifdef UNUSED
153 void memcached_io_preread(memcached_st *ptr)
154 {
155 unsigned int x;
156
157 return;
158
159 for (x= 0; x < ptr->number_of_hosts; x++)
160 {
161 if (memcached_server_response_count(ptr, x) &&
162 ptr->hosts[x].read_data_length < MEMCACHED_MAX_BUFFER )
163 {
164 size_t data_read;
165
166 data_read= read(ptr->hosts[x].fd,
167 ptr->hosts[x].read_ptr + ptr->hosts[x].read_data_length,
168 MEMCACHED_MAX_BUFFER - ptr->hosts[x].read_data_length);
169 if (data_read == -1)
170 continue;
171
172 ptr->hosts[x].read_buffer_length+= data_read;
173 ptr->hosts[x].read_data_length+= data_read;
174 }
175 }
176 }
177 #endif
178
179 memcached_return_t memcached_io_read(memcached_server_st *ptr,
180 void *buffer, size_t length, ssize_t *nread)
181 {
182 char *buffer_ptr;
183
184 buffer_ptr= buffer;
185
186 while (length)
187 {
188 if (!ptr->read_buffer_length)
189 {
190 ssize_t data_read;
191
192 while (1)
193 {
194 data_read= read(ptr->fd, ptr->read_buffer, MEMCACHED_MAX_BUFFER);
195 if (data_read > 0)
196 break;
197 else if (data_read == -1)
198 {
199 ptr->cached_errno= errno;
200 memcached_return_t rc= MEMCACHED_UNKNOWN_READ_FAILURE;
201 switch (errno)
202 {
203 case EAGAIN:
204 case EINTR:
205 if ((rc= io_wait(ptr, MEM_READ)) == MEMCACHED_SUCCESS)
206 continue;
207 /* fall through */
208
209 default:
210 {
211 memcached_quit_server(ptr, 1);
212 *nread= -1;
213 return rc;
214 }
215 }
216 }
217 else
218 {
219 /*
220 EOF. Any data received so far is incomplete
221 so discard it. This always reads by byte in case of TCP
222 and protocol enforcement happens at memcached_response()
223 looking for '\n'. We do not care for UDB which requests 8 bytes
224 at once. Generally, this means that connection went away. Since
225 for blocking I/O we do not return 0 and for non-blocking case
226 it will return EGAIN if data is not immediatly available.
227 */
228 memcached_quit_server(ptr, 1);
229 *nread= -1;
230 return MEMCACHED_UNKNOWN_READ_FAILURE;
231 }
232 }
233
234 ptr->io_bytes_sent = 0;
235 ptr->read_data_length= (size_t) data_read;
236 ptr->read_buffer_length= (size_t) data_read;
237 ptr->read_ptr= ptr->read_buffer;
238 }
239
240 if (length > 1)
241 {
242 size_t difference;
243
244 difference= (length > ptr->read_buffer_length) ? ptr->read_buffer_length : length;
245
246 memcpy(buffer_ptr, ptr->read_ptr, difference);
247 length -= difference;
248 ptr->read_ptr+= difference;
249 ptr->read_buffer_length-= difference;
250 buffer_ptr+= difference;
251 }
252 else
253 {
254 *buffer_ptr= *ptr->read_ptr;
255 ptr->read_ptr++;
256 ptr->read_buffer_length--;
257 buffer_ptr++;
258 break;
259 }
260 }
261
262 ptr->server_failure_counter= 0;
263 *nread = (ssize_t)(buffer_ptr - (char*)buffer);
264 return MEMCACHED_SUCCESS;
265 }
266
267 ssize_t memcached_io_write(memcached_server_st *ptr,
268 const void *buffer, size_t length, char with_flush)
269 {
270 size_t original_length;
271 const char* buffer_ptr;
272
273 WATCHPOINT_ASSERT(ptr->fd != -1);
274
275 original_length= length;
276 buffer_ptr= buffer;
277
278 while (length)
279 {
280 char *write_ptr;
281 size_t should_write;
282 size_t buffer_end;
283
284 if (ptr->type == MEMCACHED_CONNECTION_UDP)
285 {
286 //UDP does not support partial writes
287 buffer_end= MAX_UDP_DATAGRAM_LENGTH;
288 should_write= length;
289 if (ptr->write_buffer_offset + should_write > buffer_end)
290 return -1;
291 }
292 else
293 {
294 buffer_end= MEMCACHED_MAX_BUFFER;
295 should_write= buffer_end - ptr->write_buffer_offset;
296 should_write= (should_write < length) ? should_write : length;
297 }
298
299 write_ptr= ptr->write_buffer + ptr->write_buffer_offset;
300 memcpy(write_ptr, buffer_ptr, should_write);
301 ptr->write_buffer_offset+= should_write;
302 buffer_ptr+= should_write;
303 length-= should_write;
304
305 if (ptr->write_buffer_offset == buffer_end && ptr->type != MEMCACHED_CONNECTION_UDP)
306 {
307 memcached_return_t rc;
308 ssize_t sent_length;
309
310 WATCHPOINT_ASSERT(ptr->fd != -1);
311 sent_length= io_flush(ptr, &rc);
312 if (sent_length == -1)
313 return -1;
314
315 /* If io_flush calls memcached_purge, sent_length may be 0 */
316 unlikely (sent_length != 0)
317 {
318 WATCHPOINT_ASSERT(sent_length == (ssize_t)buffer_end);
319 }
320 }
321 }
322
323 if (with_flush)
324 {
325 memcached_return_t rc;
326 WATCHPOINT_ASSERT(ptr->fd != -1);
327 if (io_flush(ptr, &rc) == -1)
328 return -1;
329 }
330
331 return (ssize_t) original_length;
332 }
333
334 memcached_return_t memcached_io_close(memcached_server_st *ptr)
335 {
336 #ifdef DEBUG
337 int r;
338 #endif
339
340 if (ptr->fd == -1)
341 return MEMCACHED_SUCCESS;
342
343 /* in case of death shutdown to avoid blocking at close() */
344 if (1)
345 {
346 #ifdef DEBUG
347 r=
348 #endif
349 shutdown(ptr->fd, SHUT_RDWR);
350
351 #ifdef DEBUG
352 if (r && errno != ENOTCONN)
353 {
354 WATCHPOINT_NUMBER(ptr->fd);
355 WATCHPOINT_ERRNO(errno);
356 WATCHPOINT_ASSERT(errno);
357 }
358 #endif
359 }
360
361 #ifdef DEBUG
362 r=
363 #endif
364 close(ptr->fd);
365 #ifdef DEBUG
366 if (r != 0)
367 WATCHPOINT_ERRNO(errno);
368 #endif
369
370 return MEMCACHED_SUCCESS;
371 }
372
373 memcached_server_st *memcached_io_get_readable_server(memcached_st *memc)
374 {
375 #define MAX_SERVERS_TO_POLL 100
376 struct pollfd fds[MAX_SERVERS_TO_POLL];
377 unsigned int host_index= 0;
378
379 for (unsigned int x= 0;
380 x< memc->number_of_hosts && host_index < MAX_SERVERS_TO_POLL;
381 ++x)
382 {
383 if (memc->hosts[x].read_buffer_length > 0) /* I have data in the buffer */
384 return &memc->hosts[x];
385
386 if (memcached_server_response_count(&memc->hosts[x]) > 0)
387 {
388 fds[host_index].events = POLLIN;
389 fds[host_index].revents = 0;
390 fds[host_index].fd = memc->hosts[x].fd;
391 ++host_index;
392 }
393 }
394
395 if (host_index < 2)
396 {
397 /* We have 0 or 1 server with pending events.. */
398 for (unsigned int x= 0; x< memc->number_of_hosts; ++x)
399 if (memcached_server_response_count(&memc->hosts[x]) > 0)
400 return &memc->hosts[x];
401
402 return NULL;
403 }
404
405 int err= poll(fds, host_index, memc->poll_timeout);
406 switch (err) {
407 case -1:
408 memc->cached_errno = errno;
409 /* FALLTHROUGH */
410 case 0:
411 break;
412 default:
413 for (unsigned int x= 0; x < host_index; ++x)
414 if (fds[x].revents & POLLIN)
415 for (unsigned int y= 0; y < memc->number_of_hosts; ++y)
416 if (memc->hosts[y].fd == fds[x].fd)
417 return &memc->hosts[y];
418 }
419
420 return NULL;
421 }
422
423 static ssize_t io_flush(memcached_server_st *ptr,
424 memcached_return_t *error)
425 {
426 /*
427 ** We might want to purge the input buffer if we haven't consumed
428 ** any output yet... The test for the limits is the purge is inline
429 ** in the purge function to avoid duplicating the logic..
430 */
431 {
432 memcached_return_t rc;
433 WATCHPOINT_ASSERT(ptr->fd != -1);
434 rc= memcached_purge(ptr);
435
436 if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_STORED)
437 return -1;
438 }
439 ssize_t sent_length;
440 size_t return_length;
441 char *local_write_ptr= ptr->write_buffer;
442 size_t write_length= ptr->write_buffer_offset;
443
444 *error= MEMCACHED_SUCCESS;
445
446 WATCHPOINT_ASSERT(ptr->fd != -1);
447
448 // UDP Sanity check, make sure that we are not sending somthing too big
449 if (ptr->type == MEMCACHED_CONNECTION_UDP && write_length > MAX_UDP_DATAGRAM_LENGTH)
450 return -1;
451
452 if (ptr->write_buffer_offset == 0 || (ptr->type == MEMCACHED_CONNECTION_UDP
453 && ptr->write_buffer_offset == UDP_DATAGRAM_HEADER_LENGTH))
454 return 0;
455
456 /* Looking for memory overflows */
457 #if defined(DEBUG)
458 if (write_length == MEMCACHED_MAX_BUFFER)
459 WATCHPOINT_ASSERT(ptr->write_buffer == local_write_ptr);
460 WATCHPOINT_ASSERT((ptr->write_buffer + MEMCACHED_MAX_BUFFER) >= (local_write_ptr + write_length));
461 #endif
462
463 return_length= 0;
464 while (write_length)
465 {
466 WATCHPOINT_ASSERT(ptr->fd != -1);
467 WATCHPOINT_ASSERT(write_length > 0);
468 sent_length= 0;
469 if (ptr->type == MEMCACHED_CONNECTION_UDP)
470 increment_udp_message_id(ptr);
471 sent_length= write(ptr->fd, local_write_ptr, write_length);
472
473 if (sent_length == -1)
474 {
475 ptr->cached_errno= errno;
476 switch (errno)
477 {
478 case ENOBUFS:
479 continue;
480 case EAGAIN:
481 {
482 /*
483 * We may be blocked on write because the input buffer
484 * is full. Let's check if we have room in our input
485 * buffer for more data and retry the write before
486 * waiting..
487 */
488 if (repack_input_buffer(ptr) ||
489 process_input_buffer(ptr))
490 continue;
491
492 memcached_return_t rc;
493 rc= io_wait(ptr, MEM_WRITE);
494
495 if (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_TIMEOUT)
496 continue;
497
498 memcached_quit_server(ptr, 1);
499 return -1;
500 }
501 default:
502 memcached_quit_server(ptr, 1);
503 *error= MEMCACHED_ERRNO;
504 return -1;
505 }
506 }
507
508 if (ptr->type == MEMCACHED_CONNECTION_UDP &&
509 (size_t)sent_length != write_length)
510 {
511 memcached_quit_server(ptr, 1);
512 return -1;
513 }
514
515 ptr->io_bytes_sent += (uint32_t) sent_length;
516
517 local_write_ptr+= sent_length;
518 write_length-= (uint32_t) sent_length;
519 return_length+= (uint32_t) sent_length;
520 }
521
522 WATCHPOINT_ASSERT(write_length == 0);
523 // Need to study this assert() WATCHPOINT_ASSERT(return_length ==
524 // ptr->write_buffer_offset);
525
526 // if we are a udp server, the begining of the buffer is reserverd for
527 // the upd frame header
528 if (ptr->type == MEMCACHED_CONNECTION_UDP)
529 ptr->write_buffer_offset= UDP_DATAGRAM_HEADER_LENGTH;
530 else
531 ptr->write_buffer_offset= 0;
532
533 return (ssize_t) return_length;
534 }
535
536 /*
537 Eventually we will just kill off the server with the problem.
538 */
539 void memcached_io_reset(memcached_server_st *ptr)
540 {
541 memcached_quit_server(ptr, 1);
542 }
543
544 /**
545 * Read a given number of bytes from the server and place it into a specific
546 * buffer. Reset the IO channel on this server if an error occurs.
547 */
548 memcached_return_t memcached_safe_read(memcached_server_st *ptr,
549 void *dta,
550 size_t size)
551 {
552 size_t offset= 0;
553 char *data= dta;
554
555 while (offset < size)
556 {
557 ssize_t nread;
558 memcached_return_t rc= memcached_io_read(ptr, data + offset, size - offset,
559 &nread);
560 if (rc != MEMCACHED_SUCCESS)
561 return rc;
562
563 offset+= (size_t) nread;
564 }
565
566 return MEMCACHED_SUCCESS;
567 }
568
569 memcached_return_t memcached_io_readline(memcached_server_st *ptr,
570 char *buffer_ptr,
571 size_t size)
572 {
573 bool line_complete= false;
574 size_t total_nr= 0;
575
576 while (!line_complete)
577 {
578 if (ptr->read_buffer_length == 0)
579 {
580 /*
581 * We don't have any data in the buffer, so let's fill the read
582 * buffer. Call the standard read function to avoid duplicating
583 * the logic.
584 */
585 ssize_t nread;
586 memcached_return_t rc= memcached_io_read(ptr, buffer_ptr, 1, &nread);
587 if (rc != MEMCACHED_SUCCESS)
588 return rc;
589
590 if (*buffer_ptr == '\n')
591 line_complete= true;
592
593 ++buffer_ptr;
594 ++total_nr;
595 }
596
597 /* Now let's look in the buffer and copy as we go! */
598 while (ptr->read_buffer_length && total_nr < size && !line_complete)
599 {
600 *buffer_ptr = *ptr->read_ptr;
601 if (*buffer_ptr == '\n')
602 line_complete = true;
603 --ptr->read_buffer_length;
604 ++ptr->read_ptr;
605 ++total_nr;
606 ++buffer_ptr;
607 }
608
609 if (total_nr == size)
610 return MEMCACHED_PROTOCOL_ERROR;
611 }
612
613 return MEMCACHED_SUCCESS;
614 }
615
616 /*
617 * The udp request id consists of two seperate sections
618 * 1) The thread id
619 * 2) The message number
620 * The thread id should only be set when the memcached_st struct is created
621 * and should not be changed.
622 *
623 * The message num is incremented for each new message we send, this function
624 * extracts the message number from message_id, increments it and then
625 * writes the new value back into the header
626 */
627 static void increment_udp_message_id(memcached_server_st *ptr)
628 {
629 struct udp_datagram_header_st *header= (struct udp_datagram_header_st *)ptr->write_buffer;
630 uint16_t cur_req= get_udp_datagram_request_id(header);
631 int msg_num= get_msg_num_from_request_id(cur_req);
632 int thread_id= get_thread_id_from_request_id(cur_req);
633
634 if (((++msg_num) & UDP_REQUEST_ID_THREAD_MASK) != 0)
635 msg_num= 0;
636
637 header->request_id= htons((uint16_t) (thread_id | msg_num));
638 }
639
640 memcached_return_t memcached_io_init_udp_header(memcached_server_st *ptr, uint16_t thread_id)
641 {
642 if (thread_id > UDP_REQUEST_ID_MAX_THREAD_ID)
643 return MEMCACHED_FAILURE;
644
645 struct udp_datagram_header_st *header= (struct udp_datagram_header_st *)ptr->write_buffer;
646 header->request_id= htons((uint16_t) (generate_udp_request_thread_id(thread_id)));
647 header->num_datagrams= htons(1);
648 header->sequence_number= htons(0);
649
650 return MEMCACHED_SUCCESS;
651 }