00b505026c5d5b9f1a6d3ac50f103e1c29a9c2cc
[awesomized/libmemcached] / libmemcached / memcached_io.c
1 /*
2 Basic socket buffered IO
3 */
4
5 #include "common.h"
6 #include "memcached_io.h"
7 #include <sys/select.h>
8 #include <poll.h>
9
10 typedef enum {
11 MEM_READ,
12 MEM_WRITE,
13 } memc_read_or_write;
14
15 static ssize_t io_flush(memcached_server_st *ptr, memcached_return *error);
16 static void increment_udp_message_id(memcached_server_st *ptr);
17
18 static memcached_return io_wait(memcached_server_st *ptr,
19 memc_read_or_write read_or_write)
20 {
21 struct pollfd fds[1];
22 short flags= 0;
23 int error;
24
25 if (read_or_write == MEM_WRITE) /* write */
26 flags= POLLOUT;
27 else
28 flags= POLLIN;
29
30 memset(&fds, 0, sizeof(struct pollfd));
31 fds[0].fd= ptr->fd;
32 fds[0].events= flags;
33
34 /*
35 ** We are going to block on write, but at least on Solaris we might block
36 ** on write if we haven't read anything from our input buffer..
37 ** Try to purge the input buffer if we don't do any flow control in the
38 ** application layer (just sending a lot of data etc)
39 ** The test is moved down in the purge function to avoid duplication of
40 ** the test.
41 */
42 if (read_or_write == MEM_WRITE)
43 {
44 memcached_return rc=memcached_purge(ptr);
45 if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_STORED)
46 return MEMCACHED_FAILURE;
47 }
48
49 error= poll(fds, 1, ptr->root->poll_timeout);
50
51 if (error == 1)
52 return MEMCACHED_SUCCESS;
53 else if (error == 0)
54 {
55 return MEMCACHED_TIMEOUT;
56 }
57
58 /* Imposssible for anything other then -1 */
59 WATCHPOINT_ASSERT(error == -1);
60 memcached_quit_server(ptr, 1);
61
62 return MEMCACHED_FAILURE;
63
64 }
65
66 #ifdef UNUSED
67 void memcached_io_preread(memcached_st *ptr)
68 {
69 unsigned int x;
70
71 return;
72
73 for (x= 0; x < ptr->number_of_hosts; x++)
74 {
75 if (memcached_server_response_count(ptr, x) &&
76 ptr->hosts[x].read_data_length < MEMCACHED_MAX_BUFFER )
77 {
78 size_t data_read;
79
80 data_read= read(ptr->hosts[x].fd,
81 ptr->hosts[x].read_ptr + ptr->hosts[x].read_data_length,
82 MEMCACHED_MAX_BUFFER - ptr->hosts[x].read_data_length);
83 if (data_read == -1)
84 continue;
85
86 ptr->hosts[x].read_buffer_length+= data_read;
87 ptr->hosts[x].read_data_length+= data_read;
88 }
89 }
90 }
91 #endif
92
93 memcached_return memcached_io_read(memcached_server_st *ptr,
94 void *buffer, size_t length, ssize_t *nread)
95 {
96 char *buffer_ptr;
97
98 buffer_ptr= buffer;
99
100 while (length)
101 {
102 if (!ptr->read_buffer_length)
103 {
104 ssize_t data_read;
105
106 while (1)
107 {
108 data_read= read(ptr->fd, ptr->read_buffer, MEMCACHED_MAX_BUFFER);
109 if (data_read > 0)
110 break;
111 else if (data_read == -1)
112 {
113 ptr->cached_errno= errno;
114 memcached_return rc= MEMCACHED_UNKNOWN_READ_FAILURE;
115 switch (errno)
116 {
117 case EAGAIN:
118 case EINTR:
119 if ((rc= io_wait(ptr, MEM_READ)) == MEMCACHED_SUCCESS)
120 continue;
121 /* fall through */
122
123 default:
124 {
125 memcached_quit_server(ptr, 1);
126 *nread= -1;
127 return rc;
128 }
129 }
130 }
131 else
132 {
133 /*
134 EOF. Any data received so far is incomplete
135 so discard it. This always reads by byte in case of TCP
136 and protocol enforcement happens at memcached_response()
137 looking for '\n'. We do not care for UDB which requests 8 bytes
138 at once. Generally, this means that connection went away. Since
139 for blocking I/O we do not return 0 and for non-blocking case
140 it will return EGAIN if data is not immediatly available.
141 */
142 memcached_quit_server(ptr, 1);
143 *nread= -1;
144 return MEMCACHED_UNKNOWN_READ_FAILURE;
145 }
146 }
147
148 ptr->io_bytes_sent = 0;
149 ptr->read_data_length= data_read;
150 ptr->read_buffer_length= data_read;
151 ptr->read_ptr= ptr->read_buffer;
152 }
153
154 if (length > 1)
155 {
156 size_t difference;
157
158 difference= (length > ptr->read_buffer_length) ? ptr->read_buffer_length : length;
159
160 memcpy(buffer_ptr, ptr->read_ptr, difference);
161 length -= difference;
162 ptr->read_ptr+= difference;
163 ptr->read_buffer_length-= difference;
164 buffer_ptr+= difference;
165 }
166 else
167 {
168 *buffer_ptr= *ptr->read_ptr;
169 ptr->read_ptr++;
170 ptr->read_buffer_length--;
171 buffer_ptr++;
172 break;
173 }
174 }
175
176 ptr->server_failure_counter= 0;
177 *nread = (size_t)(buffer_ptr - (char*)buffer);
178 return MEMCACHED_SUCCESS;
179 }
180
181 ssize_t memcached_io_write(memcached_server_st *ptr,
182 const void *buffer, size_t length, char with_flush)
183 {
184 size_t original_length;
185 const char* buffer_ptr;
186
187 WATCHPOINT_ASSERT(ptr->fd != -1);
188
189 original_length= length;
190 buffer_ptr= buffer;
191
192 while (length)
193 {
194 char *write_ptr;
195 size_t should_write;
196 size_t buffer_end;
197
198 if (ptr->type == MEMCACHED_CONNECTION_UDP)
199 {
200 //UDP does not support partial writes
201 buffer_end= MAX_UDP_DATAGRAM_LENGTH;
202 should_write= length;
203 if (ptr->write_buffer_offset + should_write > buffer_end)
204 return -1;
205 }
206 else
207 {
208 buffer_end= MEMCACHED_MAX_BUFFER;
209 should_write= buffer_end - ptr->write_buffer_offset;
210 should_write= (should_write < length) ? should_write : length;
211 }
212
213 write_ptr= ptr->write_buffer + ptr->write_buffer_offset;
214 memcpy(write_ptr, buffer_ptr, should_write);
215 ptr->write_buffer_offset+= should_write;
216 buffer_ptr+= should_write;
217 length-= should_write;
218
219 if (ptr->write_buffer_offset == buffer_end && ptr->type != MEMCACHED_CONNECTION_UDP)
220 {
221 memcached_return rc;
222 ssize_t sent_length;
223
224 WATCHPOINT_ASSERT(ptr->fd != -1);
225 sent_length= io_flush(ptr, &rc);
226 if (sent_length == -1)
227 return -1;
228
229 /* If io_flush calls memcached_purge, sent_length may be 0 */
230 if (sent_length != 0)
231 WATCHPOINT_ASSERT(sent_length == buffer_end);
232 }
233 }
234
235 if (with_flush)
236 {
237 memcached_return rc;
238 WATCHPOINT_ASSERT(ptr->fd != -1);
239 if (io_flush(ptr, &rc) == -1)
240 return -1;
241 }
242
243 return original_length;
244 }
245
246 memcached_return memcached_io_close(memcached_server_st *ptr)
247 {
248 int r;
249
250 if (ptr->fd == -1)
251 return MEMCACHED_SUCCESS;
252
253 /* in case of death shutdown to avoid blocking at close() */
254 if (1)
255 {
256 r= shutdown(ptr->fd, SHUT_RDWR);
257
258 #ifdef HAVE_DEBUG
259 if (r && errno != ENOTCONN)
260 {
261 WATCHPOINT_NUMBER(ptr->fd);
262 WATCHPOINT_ERRNO(errno);
263 WATCHPOINT_ASSERT(errno);
264 }
265 #endif
266 }
267
268 r= close(ptr->fd);
269 #ifdef HAVE_DEBUG
270 if (r != 0)
271 WATCHPOINT_ERRNO(errno);
272 #endif
273
274 return MEMCACHED_SUCCESS;
275 }
276
277 memcached_server_st *memcached_io_get_readable_server(memcached_st *memc)
278 {
279 #define MAX_SERVERS_TO_POLL 100
280 struct pollfd fds[MAX_SERVERS_TO_POLL];
281 int index= 0;
282
283 for (int x= 0; x< memc->number_of_hosts && index < MAX_SERVERS_TO_POLL; ++x)
284 {
285 if (memc->hosts[x].read_buffer_length > 0) /* I have data in the buffer */
286 return &memc->hosts[x];
287
288 if (memcached_server_response_count(&memc->hosts[x]) > 0)
289 {
290 fds[index].events = POLLIN;
291 fds[index].revents = 0;
292 fds[index].fd = memc->hosts[x].fd;
293 ++index;
294 }
295 }
296
297 if (index < 2)
298 {
299 /* We have 0 or 1 server with pending events.. */
300 for (int x= 0; x< memc->number_of_hosts; ++x)
301 if (memcached_server_response_count(&memc->hosts[x]) > 0)
302 return &memc->hosts[x];
303
304 return NULL;
305 }
306
307 int err= poll(fds, index, memc->poll_timeout);
308 switch (err) {
309 case -1:
310 memc->cached_errno = errno;
311 /* FALLTHROUGH */
312 case 0:
313 break;
314 default:
315 for (int x= 0; x < index; ++x)
316 if (fds[x].revents & POLLIN)
317 for (int y= 0; y < memc->number_of_hosts; ++y)
318 if (memc->hosts[y].fd == fds[x].fd)
319 return &memc->hosts[y];
320 }
321
322 return NULL;
323 }
324
325 static ssize_t io_flush(memcached_server_st *ptr,
326 memcached_return *error)
327 {
328 /*
329 ** We might want to purge the input buffer if we haven't consumed
330 ** any output yet... The test for the limits is the purge is inline
331 ** in the purge function to avoid duplicating the logic..
332 */
333 {
334 memcached_return rc;
335 WATCHPOINT_ASSERT(ptr->fd != -1);
336 rc= memcached_purge(ptr);
337
338 if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_STORED)
339 return -1;
340 }
341 ssize_t sent_length;
342 size_t return_length;
343 char *local_write_ptr= ptr->write_buffer;
344 size_t write_length= ptr->write_buffer_offset;
345
346 *error= MEMCACHED_SUCCESS;
347
348 WATCHPOINT_ASSERT(ptr->fd != -1);
349
350 // UDP Sanity check, make sure that we are not sending somthing too big
351 if (ptr->type == MEMCACHED_CONNECTION_UDP && write_length > MAX_UDP_DATAGRAM_LENGTH)
352 return -1;
353
354 if (ptr->write_buffer_offset == 0 || (ptr->type == MEMCACHED_CONNECTION_UDP
355 && ptr->write_buffer_offset == UDP_DATAGRAM_HEADER_LENGTH))
356 return 0;
357
358 /* Looking for memory overflows */
359 #if defined(HAVE_DEBUG)
360 if (write_length == MEMCACHED_MAX_BUFFER)
361 WATCHPOINT_ASSERT(ptr->write_buffer == local_write_ptr);
362 WATCHPOINT_ASSERT((ptr->write_buffer + MEMCACHED_MAX_BUFFER) >= (local_write_ptr + write_length));
363 #endif
364
365 return_length= 0;
366 while (write_length)
367 {
368 WATCHPOINT_ASSERT(ptr->fd != -1);
369 WATCHPOINT_ASSERT(write_length > 0);
370 sent_length= 0;
371 if (ptr->type == MEMCACHED_CONNECTION_UDP)
372 increment_udp_message_id(ptr);
373 sent_length= write(ptr->fd, local_write_ptr, write_length);
374
375 if (sent_length == -1)
376 {
377 ptr->cached_errno= errno;
378 switch (errno)
379 {
380 case ENOBUFS:
381 continue;
382 case EAGAIN:
383 {
384 memcached_return rc;
385 rc= io_wait(ptr, MEM_WRITE);
386
387 if (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_TIMEOUT)
388 continue;
389
390 memcached_quit_server(ptr, 1);
391 return -1;
392 }
393 default:
394 memcached_quit_server(ptr, 1);
395 *error= MEMCACHED_ERRNO;
396 return -1;
397 }
398 }
399
400 if (ptr->type == MEMCACHED_CONNECTION_UDP && sent_length != write_length)
401 {
402 memcached_quit_server(ptr, 1);
403 return -1;
404 }
405
406 ptr->io_bytes_sent += sent_length;
407
408 local_write_ptr+= sent_length;
409 write_length-= sent_length;
410 return_length+= sent_length;
411 }
412
413 WATCHPOINT_ASSERT(write_length == 0);
414 // Need to study this assert() WATCHPOINT_ASSERT(return_length ==
415 // ptr->write_buffer_offset);
416
417 // if we are a udp server, the begining of the buffer is reserverd for
418 // the upd frame header
419 if (ptr->type == MEMCACHED_CONNECTION_UDP)
420 ptr->write_buffer_offset= UDP_DATAGRAM_HEADER_LENGTH;
421 else
422 ptr->write_buffer_offset= 0;
423
424 return return_length;
425 }
426
427 /*
428 Eventually we will just kill off the server with the problem.
429 */
430 void memcached_io_reset(memcached_server_st *ptr)
431 {
432 memcached_quit_server(ptr, 1);
433 }
434
435 /**
436 * Read a given number of bytes from the server and place it into a specific
437 * buffer. Reset the IO channel on this server if an error occurs.
438 */
439 memcached_return memcached_safe_read(memcached_server_st *ptr,
440 void *dta,
441 size_t size)
442 {
443 size_t offset= 0;
444 char *data= dta;
445
446 while (offset < size)
447 {
448 ssize_t nread;
449 memcached_return rc= memcached_io_read(ptr, data + offset, size - offset,
450 &nread);
451 if (rc != MEMCACHED_SUCCESS)
452 return rc;
453
454 offset+= nread;
455 }
456
457 return MEMCACHED_SUCCESS;
458 }
459
460 memcached_return memcached_io_readline(memcached_server_st *ptr,
461 char *buffer_ptr,
462 size_t size)
463 {
464 bool line_complete= false;
465 int total_nr= 0;
466
467 while (!line_complete)
468 {
469 if (ptr->read_buffer_length == 0)
470 {
471 /*
472 * We don't have any data in the buffer, so let's fill the read
473 * buffer. Call the standard read function to avoid duplicating
474 * the logic.
475 */
476 ssize_t nread;
477 memcached_return rc= memcached_io_read(ptr, buffer_ptr, 1, &nread);
478 if (rc != MEMCACHED_SUCCESS)
479 return rc;
480
481 if (*buffer_ptr == '\n')
482 line_complete= true;
483
484 ++buffer_ptr;
485 ++total_nr;
486 }
487
488 /* Now let's look in the buffer and copy as we go! */
489 while (ptr->read_buffer_length && total_nr < size && !line_complete)
490 {
491 *buffer_ptr = *ptr->read_ptr;
492 if (*buffer_ptr == '\n')
493 line_complete = true;
494 --ptr->read_buffer_length;
495 ++ptr->read_ptr;
496 ++total_nr;
497 ++buffer_ptr;
498 }
499
500 if (total_nr == size)
501 return MEMCACHED_PROTOCOL_ERROR;
502 }
503
504 return MEMCACHED_SUCCESS;
505 }
506
507 /*
508 * The udp request id consists of two seperate sections
509 * 1) The thread id
510 * 2) The message number
511 * The thread id should only be set when the memcached_st struct is created
512 * and should not be changed.
513 *
514 * The message num is incremented for each new message we send, this function
515 * extracts the message number from message_id, increments it and then
516 * writes the new value back into the header
517 */
518 static void increment_udp_message_id(memcached_server_st *ptr)
519 {
520 struct udp_datagram_header_st *header= (struct udp_datagram_header_st *)ptr->write_buffer;
521 uint16_t cur_req= get_udp_datagram_request_id(header);
522 uint16_t msg_num= get_msg_num_from_request_id(cur_req);
523 uint16_t thread_id= get_thread_id_from_request_id(cur_req);
524
525 if (((++msg_num) & UDP_REQUEST_ID_THREAD_MASK) != 0)
526 msg_num= 0;
527
528 header->request_id= htons(thread_id | msg_num);
529 }
530
531 memcached_return memcached_io_init_udp_header(memcached_server_st *ptr, uint16_t thread_id)
532 {
533 if (thread_id > UDP_REQUEST_ID_MAX_THREAD_ID)
534 return MEMCACHED_FAILURE;
535
536 struct udp_datagram_header_st *header= (struct udp_datagram_header_st *)ptr->write_buffer;
537 header->request_id= htons(generate_udp_request_thread_id(thread_id));
538 header->num_datagrams= htons(1);
539 header->sequence_number= htons(0);
540
541 return MEMCACHED_SUCCESS;
542 }