1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
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.
38 #include <libmemcached/common.h>
41 What happens if no servers exist?
43 char *memcached_get(memcached_st
*ptr
, const char *key
,
47 memcached_return_t
*error
)
49 return memcached_get_by_key(ptr
, NULL
, 0, key
, key_length
, value_length
,
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
,
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
,
67 memcached_return_t
*error
)
69 memcached_return_t unused
;
73 unlikely (ptr
->flags
.use_udp
)
78 *error
= memcached_set_error(*ptr
, MEMCACHED_NOT_SUPPORTED
, MEMCACHED_AT
);
82 uint64_t query_id
= ptr
->query_id
;
86 *error
= memcached_mget_by_key_real(ptr
, group_key
, group_key_length
,
87 (const char * const *)&key
, &key_length
,
89 assert_msg(ptr
->query_id
== query_id
+1, "Programmer error, the query_id was not incremented.");
92 if (memcached_failed(*error
))
94 if (memcached_has_current_error(*ptr
)) // Find the most accurate error
96 *error
= memcached_last_error(ptr
);
105 char *value
= memcached_fetch(ptr
, NULL
, NULL
,
106 value_length
, flags
, error
);
107 assert_msg(ptr
->query_id
== query_id
+1, "Programmer error, the query_id was not incremented.");
109 /* This is for historical reasons */
110 if (*error
== MEMCACHED_END
)
111 *error
= MEMCACHED_NOTFOUND
;
115 if (ptr
->get_key_failure
&& *error
== MEMCACHED_NOTFOUND
)
117 memcached_result_reset(&ptr
->result
);
118 memcached_return_t rc
= ptr
->get_key_failure(ptr
, key
, key_length
, &ptr
->result
);
120 /* On all failure drop to returning NULL */
121 if (rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
)
123 if (rc
== MEMCACHED_BUFFERED
)
125 uint64_t latch
; /* We use latch to track the state of the original socket */
126 latch
= memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
);
128 memcached_behavior_set(ptr
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
130 rc
= memcached_set(ptr
, key
, key_length
,
131 (memcached_result_value(&ptr
->result
)),
132 (memcached_result_length(&ptr
->result
)),
134 (memcached_result_flags(&ptr
->result
)));
136 if (rc
== MEMCACHED_BUFFERED
&& latch
== 0)
138 memcached_behavior_set(ptr
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 0);
143 rc
= memcached_set(ptr
, key
, key_length
,
144 (memcached_result_value(&ptr
->result
)),
145 (memcached_result_length(&ptr
->result
)),
147 (memcached_result_flags(&ptr
->result
)));
150 if (rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
)
153 *value_length
= memcached_result_length(&ptr
->result
);
154 *flags
= memcached_result_flags(&ptr
->result
);
155 return memcached_string_take_value(&ptr
->result
.value
);
159 assert_msg(ptr
->query_id
== query_id
+1, "Programmer error, the query_id was not incremented.");
165 uint32_t dummy_flags
;
166 memcached_return_t dummy_error
;
168 char *dummy_value
= memcached_fetch(ptr
, NULL
, NULL
,
169 &dummy_length
, &dummy_flags
,
171 assert_msg(dummy_value
== 0, "memcached_fetch() returned additional values beyond the single get it expected");
172 assert_msg(dummy_length
== 0, "memcached_fetch() returned additional values beyond the single get it expected");
173 assert_msg(ptr
->query_id
== query_id
+1, "Programmer error, the query_id was not incremented.");
178 memcached_return_t
memcached_mget(memcached_st
*ptr
,
179 const char * const *keys
,
180 const size_t *key_length
,
181 size_t number_of_keys
)
183 return memcached_mget_by_key(ptr
, NULL
, 0, keys
, key_length
, number_of_keys
);
186 static memcached_return_t
binary_mget_by_key(memcached_st
*ptr
,
187 uint32_t master_server_key
,
188 bool is_group_key_set
,
189 const char * const *keys
,
190 const size_t *key_length
,
191 size_t number_of_keys
,
194 static memcached_return_t
memcached_mget_by_key_real(memcached_st
*ptr
,
195 const char *group_key
,
196 size_t group_key_length
,
197 const char * const *keys
,
198 const size_t *key_length
,
199 size_t number_of_keys
,
202 bool failures_occured_in_sending
= false;
203 const char *get_command
= "get ";
204 uint8_t get_command_length
= 4;
205 unsigned int master_server_key
= (unsigned int)-1; /* 0 is a valid server id! */
207 memcached_return_t rc
;
208 if (memcached_failed(rc
= initialize_query(ptr
)))
213 if (ptr
->flags
.use_udp
)
215 return memcached_set_error(*ptr
, MEMCACHED_NOT_SUPPORTED
, MEMCACHED_AT
);
218 LIBMEMCACHED_MEMCACHED_MGET_START();
220 if (number_of_keys
== 0)
222 return memcached_set_error(*ptr
, MEMCACHED_NOTFOUND
, MEMCACHED_AT
, memcached_literal_param("number_of_keys was zero"));
225 if (memcached_failed(memcached_key_test(*ptr
, keys
, key_length
, number_of_keys
)))
227 return memcached_set_error(*ptr
, MEMCACHED_BAD_KEY_PROVIDED
, MEMCACHED_AT
, memcached_literal_param("A bad key value was provided"));
230 bool is_group_key_set
= false;
231 if (group_key
and group_key_length
)
233 if (memcached_failed(memcached_key_test(*ptr
, (const char * const *)&group_key
, &group_key_length
, 1)))
235 return memcached_set_error(*ptr
, MEMCACHED_BAD_KEY_PROVIDED
, MEMCACHED_AT
, memcached_literal_param("A bad group key was provided."));
238 master_server_key
= memcached_generate_hash_with_redistribution(ptr
, group_key
, group_key_length
);
239 is_group_key_set
= true;
243 Here is where we pay for the non-block API. We need to remove any data sitting
244 in the queue before we start our get.
246 It might be optimum to bounce the connection if count > some number.
248 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
250 memcached_server_write_instance_st instance
=
251 memcached_server_instance_fetch(ptr
, x
);
253 if (memcached_server_response_count(instance
))
255 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
257 if (ptr
->flags
.no_block
)
258 (void)memcached_io_write(instance
, NULL
, 0, true);
260 while(memcached_server_response_count(instance
))
261 (void)memcached_response(instance
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, &ptr
->result
);
265 if (ptr
->flags
.binary_protocol
)
267 return binary_mget_by_key(ptr
, master_server_key
, is_group_key_set
, keys
,
268 key_length
, number_of_keys
, mget_mode
);
271 if (ptr
->flags
.support_cas
)
273 get_command
= "gets ";
274 get_command_length
= 5;
278 If a server fails we warn about errors and start all over with sending keys
281 WATCHPOINT_ASSERT(rc
== MEMCACHED_SUCCESS
);
282 size_t hosts_connected
= 0;
283 for (uint32_t x
= 0; x
< number_of_keys
; x
++)
285 memcached_server_write_instance_st instance
;
288 if (is_group_key_set
)
290 server_key
= master_server_key
;
294 server_key
= memcached_generate_hash_with_redistribution(ptr
, keys
[x
], key_length
[x
]);
297 instance
= memcached_server_instance_fetch(ptr
, server_key
);
299 struct libmemcached_io_vector_st vector
[]=
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(" ") }
308 if (memcached_server_response_count(instance
) == 0)
310 rc
= memcached_connect(instance
);
312 if (memcached_failed(rc
))
314 memcached_set_error(*instance
, rc
, MEMCACHED_AT
);
319 if ((memcached_io_writev(instance
, vector
, 4, false)) == -1)
321 failures_occured_in_sending
= true;
324 WATCHPOINT_ASSERT(instance
->cursor_active
== 0);
325 memcached_server_response_increment(instance
);
326 WATCHPOINT_ASSERT(instance
->cursor_active
== 1);
330 if ((memcached_io_writev(instance
, (vector
+ 1), 3, false)) == -1)
332 memcached_server_response_reset(instance
);
333 failures_occured_in_sending
= true;
339 if (hosts_connected
== 0)
341 LIBMEMCACHED_MEMCACHED_MGET_END();
343 if (memcached_failed(rc
))
346 return memcached_set_error(*ptr
, MEMCACHED_NO_SERVERS
, MEMCACHED_AT
);
351 Should we muddle on if some servers are dead?
353 bool success_happened
= false;
354 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
356 memcached_server_write_instance_st instance
=
357 memcached_server_instance_fetch(ptr
, x
);
359 if (memcached_server_response_count(instance
))
361 /* We need to do something about non-connnected hosts in the future */
362 if ((memcached_io_write(instance
, "\r\n", 2, true)) == -1)
364 failures_occured_in_sending
= true;
368 success_happened
= true;
373 LIBMEMCACHED_MEMCACHED_MGET_END();
375 if (failures_occured_in_sending
&& success_happened
)
377 return MEMCACHED_SOME_ERRORS
;
380 if (success_happened
)
381 return MEMCACHED_SUCCESS
;
383 return MEMCACHED_FAILURE
; // Complete failure occurred
386 memcached_return_t
memcached_mget_by_key(memcached_st
*ptr
,
387 const char *group_key
,
388 size_t group_key_length
,
389 const char * const *keys
,
390 const size_t *key_length
,
391 size_t number_of_keys
)
393 return memcached_mget_by_key_real(ptr
, group_key
, group_key_length
, keys
,
394 key_length
, number_of_keys
, true);
397 memcached_return_t
memcached_mget_execute(memcached_st
*ptr
,
398 const char * const *keys
,
399 const size_t *key_length
,
400 size_t number_of_keys
,
401 memcached_execute_fn
*callback
,
403 unsigned int number_of_callbacks
)
405 return memcached_mget_execute_by_key(ptr
, NULL
, 0, keys
, key_length
,
406 number_of_keys
, callback
,
407 context
, number_of_callbacks
);
410 memcached_return_t
memcached_mget_execute_by_key(memcached_st
*ptr
,
411 const char *group_key
,
412 size_t group_key_length
,
413 const char * const *keys
,
414 const size_t *key_length
,
415 size_t number_of_keys
,
416 memcached_execute_fn
*callback
,
418 unsigned int number_of_callbacks
)
420 if ((ptr
->flags
.binary_protocol
) == 0)
421 return MEMCACHED_NOT_SUPPORTED
;
423 memcached_return_t rc
;
424 memcached_callback_st
*original_callbacks
= ptr
->callbacks
;
425 memcached_callback_st cb
= {
432 rc
= memcached_mget_by_key(ptr
, group_key
, group_key_length
, keys
,
433 key_length
, number_of_keys
);
434 ptr
->callbacks
= original_callbacks
;
438 static memcached_return_t
simple_binary_mget(memcached_st
*ptr
,
439 uint32_t master_server_key
,
440 bool is_group_key_set
,
441 const char * const *keys
,
442 const size_t *key_length
,
443 size_t number_of_keys
, bool mget_mode
)
445 memcached_return_t rc
= MEMCACHED_NOTFOUND
;
447 bool flush
= (number_of_keys
== 1);
450 If a server fails we warn about errors and start all over with sending keys
453 for (uint32_t x
= 0; x
< number_of_keys
; ++x
)
457 if (is_group_key_set
)
459 server_key
= master_server_key
;
463 server_key
= memcached_generate_hash_with_redistribution(ptr
, keys
[x
], key_length
[x
]);
466 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, server_key
);
468 if (memcached_server_response_count(instance
) == 0)
470 rc
= memcached_connect(instance
);
471 if (memcached_failed(rc
))
477 protocol_binary_request_getk request
= { }; //= {.bytes= {0}};
478 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
480 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_GETKQ
;
482 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_GETK
;
484 memcached_return_t vk
;
485 vk
= memcached_validate_key_length(key_length
[x
],
486 ptr
->flags
.binary_protocol
);
487 unlikely (vk
!= MEMCACHED_SUCCESS
)
491 memcached_io_reset(instance
);
497 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
[x
] + memcached_array_size(ptr
->_namespace
)));
498 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
499 request
.message
.header
.request
.bodylen
= htonl((uint32_t)( key_length
[x
] + memcached_array_size(ptr
->_namespace
)));
501 struct libmemcached_io_vector_st vector
[]=
503 { request
.bytes
, sizeof(request
.bytes
) },
504 { memcached_array_string(ptr
->_namespace
), memcached_array_size(ptr
->_namespace
) },
505 { keys
[x
], key_length
[x
] }
508 if (memcached_io_writev(instance
, vector
, 3, flush
) == -1)
510 memcached_server_response_reset(instance
);
511 rc
= MEMCACHED_SOME_ERRORS
;
515 /* We just want one pending response per server */
516 memcached_server_response_reset(instance
);
517 memcached_server_response_increment(instance
);
518 if ((x
> 0 && x
== ptr
->io_key_prefetch
) && memcached_flush_buffers(ptr
) != MEMCACHED_SUCCESS
)
520 rc
= MEMCACHED_SOME_ERRORS
;
527 Send a noop command to flush the buffers
529 protocol_binary_request_noop request
= {}; //= {.bytes= {0}};
530 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
531 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_NOOP
;
532 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
534 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); ++x
)
536 memcached_server_write_instance_st instance
=
537 memcached_server_instance_fetch(ptr
, x
);
539 if (memcached_server_response_count(instance
))
541 if (memcached_io_write(instance
, NULL
, 0, true) == -1)
543 memcached_server_response_reset(instance
);
544 memcached_io_reset(instance
);
545 rc
= MEMCACHED_SOME_ERRORS
;
548 if (memcached_io_write(instance
, request
.bytes
,
549 sizeof(request
.bytes
), true) == -1)
551 memcached_server_response_reset(instance
);
552 memcached_io_reset(instance
);
553 rc
= MEMCACHED_SOME_ERRORS
;
563 static memcached_return_t
replication_binary_mget(memcached_st
*ptr
,
566 const char *const *keys
,
567 const size_t *key_length
,
568 size_t number_of_keys
)
570 memcached_return_t rc
= MEMCACHED_NOTFOUND
;
572 uint64_t randomize_read
= memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ
);
575 start
= (uint32_t)random() % (uint32_t)(ptr
->number_of_replicas
+ 1);
577 /* Loop for each replica */
578 for (uint32_t replica
= 0; replica
<= ptr
->number_of_replicas
; ++replica
)
582 for (uint32_t x
= 0; x
< number_of_keys
; ++x
)
584 if (hash
[x
] == memcached_server_count(ptr
))
585 continue; /* Already successfully sent */
587 uint32_t server
= hash
[x
] + replica
;
589 /* In case of randomized reads */
590 if (randomize_read
&& ((server
+ start
) <= (hash
[x
] + ptr
->number_of_replicas
)))
593 while (server
>= memcached_server_count(ptr
))
595 server
-= memcached_server_count(ptr
);
598 if (dead_servers
[server
])
603 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, server
);
605 if (memcached_server_response_count(instance
) == 0)
607 rc
= memcached_connect(instance
);
609 if (memcached_failed(rc
))
611 memcached_io_reset(instance
);
612 dead_servers
[server
]= true;
618 protocol_binary_request_getk request
= {};
619 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
620 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_GETK
;
621 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
[x
] + memcached_array_size(ptr
->_namespace
)));
622 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
623 request
.message
.header
.request
.bodylen
= htonl((uint32_t)(key_length
[x
] + memcached_array_size(ptr
->_namespace
)));
626 * We need to disable buffering to actually know that the request was
627 * successfully sent to the server (so that we should expect a result
628 * back). It would be nice to do this in buffered mode, but then it
629 * would be complex to handle all error situations if we got to send
630 * some of the messages, and then we failed on writing out some others
631 * and we used the callback interface from memcached_mget_execute so
632 * that we might have processed some of the responses etc. For now,
633 * just make sure we work _correctly_
635 struct libmemcached_io_vector_st vector
[]=
637 { request
.bytes
, sizeof(request
.bytes
) },
638 { memcached_array_string(ptr
->_namespace
), memcached_array_size(ptr
->_namespace
) },
639 { keys
[x
], key_length
[x
] }
642 if (memcached_io_writev(instance
, vector
, 3, true) == -1)
644 memcached_io_reset(instance
);
645 dead_servers
[server
]= true;
650 memcached_server_response_increment(instance
);
651 hash
[x
]= memcached_server_count(ptr
);
663 static memcached_return_t
binary_mget_by_key(memcached_st
*ptr
,
664 uint32_t master_server_key
,
665 bool is_group_key_set
,
666 const char * const *keys
,
667 const size_t *key_length
,
668 size_t number_of_keys
,
671 if (ptr
->number_of_replicas
== 0)
673 return simple_binary_mget(ptr
, master_server_key
, is_group_key_set
,
674 keys
, key_length
, number_of_keys
, mget_mode
);
677 uint32_t* hash
= static_cast<uint32_t*>(libmemcached_malloc(ptr
, sizeof(uint32_t) * number_of_keys
));
678 bool* dead_servers
= static_cast<bool*>(libmemcached_calloc(ptr
, memcached_server_count(ptr
), sizeof(bool)));
680 if (hash
== NULL
|| dead_servers
== NULL
)
682 libmemcached_free(ptr
, hash
);
683 libmemcached_free(ptr
, dead_servers
);
684 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
687 if (is_group_key_set
)
689 for (size_t x
= 0; x
< number_of_keys
; x
++)
691 hash
[x
]= master_server_key
;
696 for (size_t x
= 0; x
< number_of_keys
; x
++)
698 hash
[x
]= memcached_generate_hash_with_redistribution(ptr
, keys
[x
], key_length
[x
]);
702 memcached_return_t rc
= replication_binary_mget(ptr
, hash
, dead_servers
, keys
,
703 key_length
, number_of_keys
);
705 WATCHPOINT_IFERROR(rc
);
706 libmemcached_free(ptr
, hash
);
707 libmemcached_free(ptr
, dead_servers
);
709 return MEMCACHED_SUCCESS
;