Update for protocol (and a single fix).
[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 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 && *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 && 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 || 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)) == -1)
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)) == -1)
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 && success_happened)
372 {
373 return MEMCACHED_SOME_ERRORS;
374 }
375
376 if (success_happened)
377 return MEMCACHED_SUCCESS;
378
379 return MEMCACHED_FAILURE; // Complete failure occurred
380 }
381
382 memcached_return_t memcached_mget_by_key(memcached_st *ptr,
383 const char *group_key,
384 size_t group_key_length,
385 const char * const *keys,
386 const size_t *key_length,
387 size_t number_of_keys)
388 {
389 return memcached_mget_by_key_real(ptr, group_key, group_key_length, keys,
390 key_length, number_of_keys, true);
391 }
392
393 memcached_return_t memcached_mget_execute(memcached_st *ptr,
394 const char * const *keys,
395 const size_t *key_length,
396 size_t number_of_keys,
397 memcached_execute_fn *callback,
398 void *context,
399 unsigned int number_of_callbacks)
400 {
401 return memcached_mget_execute_by_key(ptr, NULL, 0, keys, key_length,
402 number_of_keys, callback,
403 context, number_of_callbacks);
404 }
405
406 memcached_return_t memcached_mget_execute_by_key(memcached_st *ptr,
407 const char *group_key,
408 size_t group_key_length,
409 const char * const *keys,
410 const size_t *key_length,
411 size_t number_of_keys,
412 memcached_execute_fn *callback,
413 void *context,
414 unsigned int number_of_callbacks)
415 {
416 memcached_return_t rc;
417 if (memcached_failed(rc= initialize_query(ptr, false)))
418 {
419 return rc;
420 }
421
422 if (memcached_is_udp(ptr))
423 {
424 return memcached_set_error(*ptr, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT);
425 }
426
427 if (memcached_is_binary(ptr) == false)
428 {
429 return memcached_set_error(*ptr, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT,
430 memcached_literal_param("ASCII protocol is not supported for memcached_mget_execute_by_key()"));
431 }
432
433 memcached_callback_st *original_callbacks= ptr->callbacks;
434 memcached_callback_st cb= {
435 callback,
436 context,
437 number_of_callbacks
438 };
439
440 ptr->callbacks= &cb;
441 rc= memcached_mget_by_key(ptr, group_key, group_key_length, keys,
442 key_length, number_of_keys);
443 ptr->callbacks= original_callbacks;
444 return rc;
445 }
446
447 static memcached_return_t simple_binary_mget(memcached_st *ptr,
448 uint32_t master_server_key,
449 bool is_group_key_set,
450 const char * const *keys,
451 const size_t *key_length,
452 size_t number_of_keys, bool mget_mode)
453 {
454 memcached_return_t rc= MEMCACHED_NOTFOUND;
455
456 bool flush= (number_of_keys == 1);
457
458 /*
459 If a server fails we warn about errors and start all over with sending keys
460 to the server.
461 */
462 for (uint32_t x= 0; x < number_of_keys; ++x)
463 {
464 uint32_t server_key;
465
466 if (is_group_key_set)
467 {
468 server_key= master_server_key;
469 }
470 else
471 {
472 server_key= memcached_generate_hash_with_redistribution(ptr, keys[x], key_length[x]);
473 }
474
475 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, server_key);
476
477 if (memcached_server_response_count(instance) == 0)
478 {
479 rc= memcached_connect(instance);
480 if (memcached_failed(rc))
481 {
482 continue;
483 }
484 }
485
486 protocol_binary_request_getk request= { }; //= {.bytes= {0}};
487 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
488 if (mget_mode)
489 {
490 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_GETKQ;
491 }
492 else
493 {
494 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_GETK;
495 }
496
497 memcached_return_t vk;
498 vk= memcached_validate_key_length(key_length[x],
499 ptr->flags.binary_protocol);
500 if (vk != MEMCACHED_SUCCESS)
501 {
502 if (x > 0)
503 {
504 memcached_io_reset(instance);
505 }
506
507 return vk;
508 }
509
510 request.message.header.request.keylen= htons((uint16_t)(key_length[x] + memcached_array_size(ptr->_namespace)));
511 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
512 request.message.header.request.bodylen= htonl((uint32_t)( key_length[x] + memcached_array_size(ptr->_namespace)));
513
514 libmemcached_io_vector_st vector[]=
515 {
516 { request.bytes, sizeof(request.bytes) },
517 { memcached_array_string(ptr->_namespace), memcached_array_size(ptr->_namespace) },
518 { keys[x], key_length[x] }
519 };
520
521 if (memcached_io_writev(instance, vector, 3, flush) == -1)
522 {
523 memcached_server_response_reset(instance);
524 rc= MEMCACHED_SOME_ERRORS;
525 continue;
526 }
527
528 /* We just want one pending response per server */
529 memcached_server_response_reset(instance);
530 memcached_server_response_increment(instance);
531 if ((x > 0 && x == ptr->io_key_prefetch) && memcached_flush_buffers(ptr) != MEMCACHED_SUCCESS)
532 {
533 rc= MEMCACHED_SOME_ERRORS;
534 }
535 }
536
537 if (mget_mode)
538 {
539 /*
540 Send a noop command to flush the buffers
541 */
542 protocol_binary_request_noop request= {}; //= {.bytes= {0}};
543 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
544 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_NOOP;
545 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
546
547 for (uint32_t x= 0; x < memcached_server_count(ptr); ++x)
548 {
549 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x);
550
551 if (memcached_server_response_count(instance))
552 {
553 if (memcached_io_write(instance) == false)
554 {
555 memcached_server_response_reset(instance);
556 memcached_io_reset(instance);
557 rc= MEMCACHED_SOME_ERRORS;
558 }
559
560 if (memcached_io_write(instance, request.bytes,
561 sizeof(request.bytes), true) == -1)
562 {
563 memcached_server_response_reset(instance);
564 memcached_io_reset(instance);
565 rc= MEMCACHED_SOME_ERRORS;
566 }
567 }
568 }
569 }
570
571
572 return rc;
573 }
574
575 static memcached_return_t replication_binary_mget(memcached_st *ptr,
576 uint32_t* hash,
577 bool* dead_servers,
578 const char *const *keys,
579 const size_t *key_length,
580 size_t number_of_keys)
581 {
582 memcached_return_t rc= MEMCACHED_NOTFOUND;
583 uint32_t start= 0;
584 uint64_t randomize_read= memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ);
585
586 if (randomize_read)
587 start= (uint32_t)random() % (uint32_t)(ptr->number_of_replicas + 1);
588
589 /* Loop for each replica */
590 for (uint32_t replica= 0; replica <= ptr->number_of_replicas; ++replica)
591 {
592 bool success= true;
593
594 for (uint32_t x= 0; x < number_of_keys; ++x)
595 {
596 if (hash[x] == memcached_server_count(ptr))
597 continue; /* Already successfully sent */
598
599 uint32_t server= hash[x] + replica;
600
601 /* In case of randomized reads */
602 if (randomize_read && ((server + start) <= (hash[x] + ptr->number_of_replicas)))
603 server += start;
604
605 while (server >= memcached_server_count(ptr))
606 {
607 server -= memcached_server_count(ptr);
608 }
609
610 if (dead_servers[server])
611 {
612 continue;
613 }
614
615 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, server);
616
617 if (memcached_server_response_count(instance) == 0)
618 {
619 rc= memcached_connect(instance);
620
621 if (memcached_failed(rc))
622 {
623 memcached_io_reset(instance);
624 dead_servers[server]= true;
625 success= false;
626 continue;
627 }
628 }
629
630 protocol_binary_request_getk request= {};
631 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
632 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_GETK;
633 request.message.header.request.keylen= htons((uint16_t)(key_length[x] + memcached_array_size(ptr->_namespace)));
634 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
635 request.message.header.request.bodylen= htonl((uint32_t)(key_length[x] + memcached_array_size(ptr->_namespace)));
636
637 /*
638 * We need to disable buffering to actually know that the request was
639 * successfully sent to the server (so that we should expect a result
640 * back). It would be nice to do this in buffered mode, but then it
641 * would be complex to handle all error situations if we got to send
642 * some of the messages, and then we failed on writing out some others
643 * and we used the callback interface from memcached_mget_execute so
644 * that we might have processed some of the responses etc. For now,
645 * just make sure we work _correctly_
646 */
647 libmemcached_io_vector_st vector[]=
648 {
649 { request.bytes, sizeof(request.bytes) },
650 { memcached_array_string(ptr->_namespace), memcached_array_size(ptr->_namespace) },
651 { keys[x], key_length[x] }
652 };
653
654 if (memcached_io_writev(instance, vector, 3, true) == -1)
655 {
656 memcached_io_reset(instance);
657 dead_servers[server]= true;
658 success= false;
659 continue;
660 }
661
662 memcached_server_response_increment(instance);
663 hash[x]= memcached_server_count(ptr);
664 }
665
666 if (success)
667 {
668 break;
669 }
670 }
671
672 return rc;
673 }
674
675 static memcached_return_t binary_mget_by_key(memcached_st *ptr,
676 uint32_t master_server_key,
677 bool is_group_key_set,
678 const char * const *keys,
679 const size_t *key_length,
680 size_t number_of_keys,
681 bool mget_mode)
682 {
683 if (ptr->number_of_replicas == 0)
684 {
685 return simple_binary_mget(ptr, master_server_key, is_group_key_set,
686 keys, key_length, number_of_keys, mget_mode);
687 }
688
689 uint32_t* hash= libmemcached_xvalloc(ptr, number_of_keys, uint32_t);
690 bool* dead_servers= libmemcached_xcalloc(ptr, memcached_server_count(ptr), bool);
691
692 if (hash == NULL || dead_servers == NULL)
693 {
694 libmemcached_free(ptr, hash);
695 libmemcached_free(ptr, dead_servers);
696 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
697 }
698
699 if (is_group_key_set)
700 {
701 for (size_t x= 0; x < number_of_keys; x++)
702 {
703 hash[x]= master_server_key;
704 }
705 }
706 else
707 {
708 for (size_t x= 0; x < number_of_keys; x++)
709 {
710 hash[x]= memcached_generate_hash_with_redistribution(ptr, keys[x], key_length[x]);
711 }
712 }
713
714 memcached_return_t rc= replication_binary_mget(ptr, hash, dead_servers, keys,
715 key_length, number_of_keys);
716
717 WATCHPOINT_IFERROR(rc);
718 libmemcached_free(ptr, hash);
719 libmemcached_free(ptr, dead_servers);
720
721 return MEMCACHED_SUCCESS;
722 }