Updating for simplified IO flush.
[awesomized/libmemcached] / libmemcached / get.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38 #include <libmemcached/common.h>
39
40 /*
41 What happens if no servers exist?
42 */
43 char *memcached_get(memcached_st *ptr, const char *key,
44 size_t key_length,
45 size_t *value_length,
46 uint32_t *flags,
47 memcached_return_t *error)
48 {
49 return memcached_get_by_key(ptr, NULL, 0, key, key_length, value_length,
50 flags, error);
51 }
52
53 static memcached_return_t memcached_mget_by_key_real(memcached_st *ptr,
54 const char *group_key,
55 size_t group_key_length,
56 const char * const *keys,
57 const size_t *key_length,
58 size_t number_of_keys,
59 bool mget_mode);
60
61 char *memcached_get_by_key(memcached_st *ptr,
62 const char *group_key,
63 size_t group_key_length,
64 const char *key, size_t key_length,
65 size_t *value_length,
66 uint32_t *flags,
67 memcached_return_t *error)
68 {
69 memcached_return_t unused;
70 if (error == NULL)
71 {
72 error= &unused;
73 }
74
75 if (ptr->flags.use_udp)
76 {
77 if (value_length)
78 {
79 *value_length= 0;
80 }
81
82 *error= memcached_set_error(*ptr, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT);
83 return NULL;
84 }
85
86 uint64_t query_id= ptr->query_id;
87 (void)query_id;
88
89 /* Request the key */
90 *error= memcached_mget_by_key_real(ptr, group_key, group_key_length,
91 (const char * const *)&key, &key_length,
92 1, false);
93 assert_msg(ptr->query_id == query_id +1, "Programmer error, the query_id was not incremented.");
94
95
96 if (memcached_failed(*error))
97 {
98 if (memcached_has_current_error(*ptr)) // Find the most accurate error
99 {
100 *error= memcached_last_error(ptr);
101 }
102
103 if (value_length)
104 *value_length= 0;
105
106 return NULL;
107 }
108
109 char *value= memcached_fetch(ptr, NULL, NULL,
110 value_length, flags, error);
111 assert_msg(ptr->query_id == query_id +1, "Programmer error, the query_id was not incremented.");
112
113 /* This is for historical reasons */
114 if (*error == MEMCACHED_END)
115 *error= MEMCACHED_NOTFOUND;
116
117 if (value == NULL)
118 {
119 if (ptr->get_key_failure && *error == MEMCACHED_NOTFOUND)
120 {
121 memcached_result_reset(&ptr->result);
122 memcached_return_t rc= ptr->get_key_failure(ptr, key, key_length, &ptr->result);
123
124 /* On all failure drop to returning NULL */
125 if (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED)
126 {
127 if (rc == MEMCACHED_BUFFERED)
128 {
129 uint64_t latch; /* We use latch to track the state of the original socket */
130 latch= memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS);
131 if (latch == 0)
132 memcached_behavior_set(ptr, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1);
133
134 rc= memcached_set(ptr, key, key_length,
135 (memcached_result_value(&ptr->result)),
136 (memcached_result_length(&ptr->result)),
137 0,
138 (memcached_result_flags(&ptr->result)));
139
140 if (rc == MEMCACHED_BUFFERED && latch == 0)
141 {
142 memcached_behavior_set(ptr, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 0);
143 }
144 }
145 else
146 {
147 rc= memcached_set(ptr, key, key_length,
148 (memcached_result_value(&ptr->result)),
149 (memcached_result_length(&ptr->result)),
150 0,
151 (memcached_result_flags(&ptr->result)));
152 }
153
154 if (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED)
155 {
156 *error= rc;
157 *value_length= memcached_result_length(&ptr->result);
158 *flags= memcached_result_flags(&ptr->result);
159 return memcached_string_take_value(&ptr->result.value);
160 }
161 }
162 }
163 assert_msg(ptr->query_id == query_id +1, "Programmer error, the query_id was not incremented.");
164
165 return NULL;
166 }
167
168 size_t dummy_length;
169 uint32_t dummy_flags;
170 memcached_return_t dummy_error;
171
172 char *dummy_value= memcached_fetch(ptr, NULL, NULL,
173 &dummy_length, &dummy_flags,
174 &dummy_error);
175 assert_msg(dummy_value == 0, "memcached_fetch() returned additional values beyond the single get it expected");
176 assert_msg(dummy_length == 0, "memcached_fetch() returned additional values beyond the single get it expected");
177 assert_msg(ptr->query_id == query_id +1, "Programmer error, the query_id was not incremented.");
178
179 return value;
180 }
181
182 memcached_return_t memcached_mget(memcached_st *ptr,
183 const char * const *keys,
184 const size_t *key_length,
185 size_t number_of_keys)
186 {
187 return memcached_mget_by_key(ptr, NULL, 0, keys, key_length, number_of_keys);
188 }
189
190 static memcached_return_t binary_mget_by_key(memcached_st *ptr,
191 uint32_t master_server_key,
192 bool is_group_key_set,
193 const char * const *keys,
194 const size_t *key_length,
195 size_t number_of_keys,
196 bool mget_mode);
197
198 static memcached_return_t memcached_mget_by_key_real(memcached_st *ptr,
199 const char *group_key,
200 size_t group_key_length,
201 const char * const *keys,
202 const size_t *key_length,
203 size_t number_of_keys,
204 bool mget_mode)
205 {
206 bool failures_occured_in_sending= false;
207 const char *get_command= "get ";
208 uint8_t get_command_length= 4;
209 unsigned int master_server_key= (unsigned int)-1; /* 0 is a valid server id! */
210
211 memcached_return_t rc;
212 if (memcached_failed(rc= initialize_query(ptr)))
213 {
214 return rc;
215 }
216
217 if (ptr->flags.use_udp)
218 {
219 return memcached_set_error(*ptr, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT);
220 }
221
222 LIBMEMCACHED_MEMCACHED_MGET_START();
223
224 if (number_of_keys == 0)
225 {
226 return memcached_set_error(*ptr, MEMCACHED_NOTFOUND, MEMCACHED_AT, memcached_literal_param("number_of_keys was zero"));
227 }
228
229 if (memcached_failed(memcached_key_test(*ptr, keys, key_length, number_of_keys)))
230 {
231 return memcached_set_error(*ptr, MEMCACHED_BAD_KEY_PROVIDED, MEMCACHED_AT, memcached_literal_param("A bad key value was provided"));
232 }
233
234 bool is_group_key_set= false;
235 if (group_key and group_key_length)
236 {
237 if (memcached_failed(memcached_key_test(*ptr, (const char * const *)&group_key, &group_key_length, 1)))
238 {
239 return memcached_set_error(*ptr, MEMCACHED_BAD_KEY_PROVIDED, MEMCACHED_AT, memcached_literal_param("A bad group key was provided."));
240 }
241
242 master_server_key= memcached_generate_hash_with_redistribution(ptr, group_key, group_key_length);
243 is_group_key_set= true;
244 }
245
246 /*
247 Here is where we pay for the non-block API. We need to remove any data sitting
248 in the queue before we start our get.
249
250 It might be optimum to bounce the connection if count > some number.
251 */
252 for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
253 {
254 memcached_server_write_instance_st instance=
255 memcached_server_instance_fetch(ptr, x);
256
257 if (memcached_server_response_count(instance))
258 {
259 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
260
261 if (ptr->flags.no_block)
262 {
263 memcached_io_write(instance);
264 }
265
266 while(memcached_server_response_count(instance))
267 {
268 (void)memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, &ptr->result);
269 }
270 }
271 }
272
273 if (ptr->flags.binary_protocol)
274 {
275 return binary_mget_by_key(ptr, master_server_key, is_group_key_set, keys,
276 key_length, number_of_keys, mget_mode);
277 }
278
279 if (ptr->flags.support_cas)
280 {
281 get_command= "gets ";
282 get_command_length= 5;
283 }
284
285 /*
286 If a server fails we warn about errors and start all over with sending keys
287 to the server.
288 */
289 WATCHPOINT_ASSERT(rc == MEMCACHED_SUCCESS);
290 size_t hosts_connected= 0;
291 for (uint32_t x= 0; x < number_of_keys; x++)
292 {
293 memcached_server_write_instance_st instance;
294 uint32_t server_key;
295
296 if (is_group_key_set)
297 {
298 server_key= master_server_key;
299 }
300 else
301 {
302 server_key= memcached_generate_hash_with_redistribution(ptr, keys[x], key_length[x]);
303 }
304
305 instance= memcached_server_instance_fetch(ptr, server_key);
306
307 struct libmemcached_io_vector_st vector[]=
308 {
309 { get_command, get_command_length },
310 { memcached_array_string(ptr->_namespace), memcached_array_size(ptr->_namespace) },
311 { keys[x], key_length[x] },
312 { memcached_literal_param(" ") }
313 };
314
315
316 if (memcached_server_response_count(instance) == 0)
317 {
318 rc= memcached_connect(instance);
319
320 if (memcached_failed(rc))
321 {
322 memcached_set_error(*instance, rc, MEMCACHED_AT);
323 continue;
324 }
325 hosts_connected++;
326
327 if ((memcached_io_writev(instance, vector, 4, false)) == -1)
328 {
329 failures_occured_in_sending= true;
330 continue;
331 }
332 WATCHPOINT_ASSERT(instance->cursor_active == 0);
333 memcached_server_response_increment(instance);
334 WATCHPOINT_ASSERT(instance->cursor_active == 1);
335 }
336 else
337 {
338 if ((memcached_io_writev(instance, (vector + 1), 3, false)) == -1)
339 {
340 memcached_server_response_reset(instance);
341 failures_occured_in_sending= true;
342 continue;
343 }
344 }
345 }
346
347 if (hosts_connected == 0)
348 {
349 LIBMEMCACHED_MEMCACHED_MGET_END();
350
351 if (memcached_failed(rc))
352 return rc;
353
354 return memcached_set_error(*ptr, MEMCACHED_NO_SERVERS, MEMCACHED_AT);
355 }
356
357
358 /*
359 Should we muddle on if some servers are dead?
360 */
361 bool success_happened= false;
362 for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
363 {
364 memcached_server_write_instance_st instance=
365 memcached_server_instance_fetch(ptr, x);
366
367 if (memcached_server_response_count(instance))
368 {
369 /* We need to do something about non-connnected hosts in the future */
370 if ((memcached_io_write(instance, "\r\n", 2, true)) == -1)
371 {
372 failures_occured_in_sending= true;
373 }
374 else
375 {
376 success_happened= true;
377 }
378 }
379 }
380
381 LIBMEMCACHED_MEMCACHED_MGET_END();
382
383 if (failures_occured_in_sending && success_happened)
384 {
385 return MEMCACHED_SOME_ERRORS;
386 }
387
388 if (success_happened)
389 return MEMCACHED_SUCCESS;
390
391 return MEMCACHED_FAILURE; // Complete failure occurred
392 }
393
394 memcached_return_t memcached_mget_by_key(memcached_st *ptr,
395 const char *group_key,
396 size_t group_key_length,
397 const char * const *keys,
398 const size_t *key_length,
399 size_t number_of_keys)
400 {
401 return memcached_mget_by_key_real(ptr, group_key, group_key_length, keys,
402 key_length, number_of_keys, true);
403 }
404
405 memcached_return_t memcached_mget_execute(memcached_st *ptr,
406 const char * const *keys,
407 const size_t *key_length,
408 size_t number_of_keys,
409 memcached_execute_fn *callback,
410 void *context,
411 unsigned int number_of_callbacks)
412 {
413 return memcached_mget_execute_by_key(ptr, NULL, 0, keys, key_length,
414 number_of_keys, callback,
415 context, number_of_callbacks);
416 }
417
418 memcached_return_t memcached_mget_execute_by_key(memcached_st *ptr,
419 const char *group_key,
420 size_t group_key_length,
421 const char * const *keys,
422 const size_t *key_length,
423 size_t number_of_keys,
424 memcached_execute_fn *callback,
425 void *context,
426 unsigned int number_of_callbacks)
427 {
428 if ((ptr->flags.binary_protocol) == 0)
429 {
430 return MEMCACHED_NOT_SUPPORTED;
431 }
432
433 memcached_return_t rc;
434 memcached_callback_st *original_callbacks= ptr->callbacks;
435 memcached_callback_st cb= {
436 callback,
437 context,
438 number_of_callbacks
439 };
440
441 ptr->callbacks= &cb;
442 rc= memcached_mget_by_key(ptr, group_key, group_key_length, keys,
443 key_length, number_of_keys);
444 ptr->callbacks= original_callbacks;
445 return rc;
446 }
447
448 static memcached_return_t simple_binary_mget(memcached_st *ptr,
449 uint32_t master_server_key,
450 bool is_group_key_set,
451 const char * const *keys,
452 const size_t *key_length,
453 size_t number_of_keys, bool mget_mode)
454 {
455 memcached_return_t rc= MEMCACHED_NOTFOUND;
456
457 bool flush= (number_of_keys == 1);
458
459 /*
460 If a server fails we warn about errors and start all over with sending keys
461 to the server.
462 */
463 for (uint32_t x= 0; x < number_of_keys; ++x)
464 {
465 uint32_t server_key;
466
467 if (is_group_key_set)
468 {
469 server_key= master_server_key;
470 }
471 else
472 {
473 server_key= memcached_generate_hash_with_redistribution(ptr, keys[x], key_length[x]);
474 }
475
476 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, server_key);
477
478 if (memcached_server_response_count(instance) == 0)
479 {
480 rc= memcached_connect(instance);
481 if (memcached_failed(rc))
482 {
483 continue;
484 }
485 }
486
487 protocol_binary_request_getk request= { }; //= {.bytes= {0}};
488 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
489 if (mget_mode)
490 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_GETKQ;
491 else
492 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_GETK;
493
494 memcached_return_t vk;
495 vk= memcached_validate_key_length(key_length[x],
496 ptr->flags.binary_protocol);
497 unlikely (vk != MEMCACHED_SUCCESS)
498 {
499 if (x > 0)
500 {
501 memcached_io_reset(instance);
502 }
503
504 return vk;
505 }
506
507 request.message.header.request.keylen= htons((uint16_t)(key_length[x] + memcached_array_size(ptr->_namespace)));
508 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
509 request.message.header.request.bodylen= htonl((uint32_t)( key_length[x] + memcached_array_size(ptr->_namespace)));
510
511 struct libmemcached_io_vector_st vector[]=
512 {
513 { request.bytes, sizeof(request.bytes) },
514 { memcached_array_string(ptr->_namespace), memcached_array_size(ptr->_namespace) },
515 { keys[x], key_length[x] }
516 };
517
518 if (memcached_io_writev(instance, vector, 3, flush) == -1)
519 {
520 memcached_server_response_reset(instance);
521 rc= MEMCACHED_SOME_ERRORS;
522 continue;
523 }
524
525 /* We just want one pending response per server */
526 memcached_server_response_reset(instance);
527 memcached_server_response_increment(instance);
528 if ((x > 0 && x == ptr->io_key_prefetch) && memcached_flush_buffers(ptr) != MEMCACHED_SUCCESS)
529 {
530 rc= MEMCACHED_SOME_ERRORS;
531 }
532 }
533
534 if (mget_mode)
535 {
536 /*
537 Send a noop command to flush the buffers
538 */
539 protocol_binary_request_noop request= {}; //= {.bytes= {0}};
540 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
541 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_NOOP;
542 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
543
544 for (uint32_t x= 0; x < memcached_server_count(ptr); ++x)
545 {
546 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x);
547
548 if (memcached_server_response_count(instance))
549 {
550 if (memcached_io_write(instance) == false)
551 {
552 memcached_server_response_reset(instance);
553 memcached_io_reset(instance);
554 rc= MEMCACHED_SOME_ERRORS;
555 }
556
557 if (memcached_io_write(instance, request.bytes,
558 sizeof(request.bytes), true) == -1)
559 {
560 memcached_server_response_reset(instance);
561 memcached_io_reset(instance);
562 rc= MEMCACHED_SOME_ERRORS;
563 }
564 }
565 }
566 }
567
568
569 return rc;
570 }
571
572 static memcached_return_t replication_binary_mget(memcached_st *ptr,
573 uint32_t* hash,
574 bool* dead_servers,
575 const char *const *keys,
576 const size_t *key_length,
577 size_t number_of_keys)
578 {
579 memcached_return_t rc= MEMCACHED_NOTFOUND;
580 uint32_t start= 0;
581 uint64_t randomize_read= memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ);
582
583 if (randomize_read)
584 start= (uint32_t)random() % (uint32_t)(ptr->number_of_replicas + 1);
585
586 /* Loop for each replica */
587 for (uint32_t replica= 0; replica <= ptr->number_of_replicas; ++replica)
588 {
589 bool success= true;
590
591 for (uint32_t x= 0; x < number_of_keys; ++x)
592 {
593 if (hash[x] == memcached_server_count(ptr))
594 continue; /* Already successfully sent */
595
596 uint32_t server= hash[x] + replica;
597
598 /* In case of randomized reads */
599 if (randomize_read && ((server + start) <= (hash[x] + ptr->number_of_replicas)))
600 server += start;
601
602 while (server >= memcached_server_count(ptr))
603 {
604 server -= memcached_server_count(ptr);
605 }
606
607 if (dead_servers[server])
608 {
609 continue;
610 }
611
612 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, server);
613
614 if (memcached_server_response_count(instance) == 0)
615 {
616 rc= memcached_connect(instance);
617
618 if (memcached_failed(rc))
619 {
620 memcached_io_reset(instance);
621 dead_servers[server]= true;
622 success= false;
623 continue;
624 }
625 }
626
627 protocol_binary_request_getk request= {};
628 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
629 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_GETK;
630 request.message.header.request.keylen= htons((uint16_t)(key_length[x] + memcached_array_size(ptr->_namespace)));
631 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
632 request.message.header.request.bodylen= htonl((uint32_t)(key_length[x] + memcached_array_size(ptr->_namespace)));
633
634 /*
635 * We need to disable buffering to actually know that the request was
636 * successfully sent to the server (so that we should expect a result
637 * back). It would be nice to do this in buffered mode, but then it
638 * would be complex to handle all error situations if we got to send
639 * some of the messages, and then we failed on writing out some others
640 * and we used the callback interface from memcached_mget_execute so
641 * that we might have processed some of the responses etc. For now,
642 * just make sure we work _correctly_
643 */
644 struct libmemcached_io_vector_st vector[]=
645 {
646 { request.bytes, sizeof(request.bytes) },
647 { memcached_array_string(ptr->_namespace), memcached_array_size(ptr->_namespace) },
648 { keys[x], key_length[x] }
649 };
650
651 if (memcached_io_writev(instance, vector, 3, true) == -1)
652 {
653 memcached_io_reset(instance);
654 dead_servers[server]= true;
655 success= false;
656 continue;
657 }
658
659 memcached_server_response_increment(instance);
660 hash[x]= memcached_server_count(ptr);
661 }
662
663 if (success)
664 {
665 break;
666 }
667 }
668
669 return rc;
670 }
671
672 static memcached_return_t binary_mget_by_key(memcached_st *ptr,
673 uint32_t master_server_key,
674 bool is_group_key_set,
675 const char * const *keys,
676 const size_t *key_length,
677 size_t number_of_keys,
678 bool mget_mode)
679 {
680 if (ptr->number_of_replicas == 0)
681 {
682 return simple_binary_mget(ptr, master_server_key, is_group_key_set,
683 keys, key_length, number_of_keys, mget_mode);
684 }
685
686 uint32_t* hash= libmemcached_xvalloc(ptr, number_of_keys, uint32_t);
687 bool* dead_servers= libmemcached_xcalloc(ptr, memcached_server_count(ptr), bool);
688
689 if (hash == NULL || dead_servers == NULL)
690 {
691 libmemcached_free(ptr, hash);
692 libmemcached_free(ptr, dead_servers);
693 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
694 }
695
696 if (is_group_key_set)
697 {
698 for (size_t x= 0; x < number_of_keys; x++)
699 {
700 hash[x]= master_server_key;
701 }
702 }
703 else
704 {
705 for (size_t x= 0; x < number_of_keys; x++)
706 {
707 hash[x]= memcached_generate_hash_with_redistribution(ptr, keys[x], key_length[x]);
708 }
709 }
710
711 memcached_return_t rc= replication_binary_mget(ptr, hash, dead_servers, keys,
712 key_length, number_of_keys);
713
714 WATCHPOINT_IFERROR(rc);
715 libmemcached_free(ptr, hash);
716 libmemcached_free(ptr, dead_servers);
717
718 return MEMCACHED_SUCCESS;
719 }