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