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