1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011-2012 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
*shell
,
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
* ptr
= memcached2Memcached(shell
);
70 memcached_return_t unused
;
79 query_id
= ptr
->query_id
;
83 *error
= memcached_mget_by_key_real(ptr
, group_key
, group_key_length
,
84 (const char * const *)&key
, &key_length
,
88 assert_msg(ptr
->query_id
== query_id
+1, "Programmer error, the query_id was not incremented.");
91 if (memcached_failed(*error
))
95 if (memcached_has_current_error(*ptr
)) // Find the most accurate error
97 *error
= memcached_last_error(ptr
);
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.");
113 /* This is for historical reasons */
114 if (*error
== MEMCACHED_END
)
116 *error
= MEMCACHED_NOTFOUND
;
121 if (ptr
->get_key_failure
and *error
== MEMCACHED_NOTFOUND
)
123 memcached_result_st key_failure_result
;
124 memcached_result_st
* result_ptr
= memcached_result_create(ptr
, &key_failure_result
);
125 memcached_return_t rc
= ptr
->get_key_failure(ptr
, key
, key_length
, result_ptr
);
127 /* On all failure drop to returning NULL */
128 if (rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
)
130 if (rc
== MEMCACHED_BUFFERED
)
132 uint64_t latch
; /* We use latch to track the state of the original socket */
133 latch
= memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
);
136 memcached_behavior_set(ptr
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
139 rc
= memcached_set(ptr
, key
, key_length
,
140 (memcached_result_value(result_ptr
)),
141 (memcached_result_length(result_ptr
)),
143 (memcached_result_flags(result_ptr
)));
145 if (rc
== MEMCACHED_BUFFERED
and latch
== 0)
147 memcached_behavior_set(ptr
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 0);
152 rc
= memcached_set(ptr
, key
, key_length
,
153 (memcached_result_value(result_ptr
)),
154 (memcached_result_length(result_ptr
)),
156 (memcached_result_flags(result_ptr
)));
159 if (rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
)
162 *value_length
= memcached_result_length(result_ptr
);
163 *flags
= memcached_result_flags(result_ptr
);
164 char *result_value
= memcached_string_take_value(&result_ptr
->value
);
165 memcached_result_free(result_ptr
);
171 memcached_result_free(result_ptr
);
173 assert_msg(ptr
->query_id
== query_id
+1, "Programmer error, the query_id was not incremented.");
181 memcached_return_t
memcached_mget(memcached_st
*ptr
,
182 const char * const *keys
,
183 const size_t *key_length
,
184 size_t number_of_keys
)
186 return memcached_mget_by_key(ptr
, NULL
, 0, keys
, key_length
, number_of_keys
);
189 static memcached_return_t
binary_mget_by_key(memcached_st
*ptr
,
190 uint32_t master_server_key
,
191 bool is_group_key_set
,
192 const char * const *keys
,
193 const size_t *key_length
,
194 size_t number_of_keys
,
197 static memcached_return_t
memcached_mget_by_key_real(memcached_st
*ptr
,
198 const char *group_key
,
199 size_t group_key_length
,
200 const char * const *keys
,
201 const size_t *key_length
,
202 size_t number_of_keys
,
205 bool failures_occured_in_sending
= false;
206 const char *get_command
= "get";
207 uint8_t get_command_length
= 3;
208 unsigned int master_server_key
= (unsigned int)-1; /* 0 is a valid server id! */
210 memcached_return_t rc
;
211 if (memcached_failed(rc
= initialize_query(ptr
, true)))
216 if (memcached_is_udp(ptr
))
218 return memcached_set_error(*ptr
, MEMCACHED_NOT_SUPPORTED
, MEMCACHED_AT
);
221 LIBMEMCACHED_MEMCACHED_MGET_START();
223 if (number_of_keys
== 0)
225 return memcached_set_error(*ptr
, MEMCACHED_NOTFOUND
, MEMCACHED_AT
, memcached_literal_param("number_of_keys was zero"));
228 if (memcached_failed(memcached_key_test(*ptr
, keys
, key_length
, number_of_keys
)))
230 return memcached_last_error(ptr
);
233 bool is_group_key_set
= false;
234 if (group_key
and group_key_length
)
236 master_server_key
= memcached_generate_hash_with_redistribution(ptr
, group_key
, group_key_length
);
237 is_group_key_set
= true;
241 Here is where we pay for the non-block API. We need to remove any data sitting
242 in the queue before we start our get.
244 It might be optimum to bounce the connection if count > some number.
246 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
248 org::libmemcached::Instance
* instance
= memcached_instance_fetch(ptr
, x
);
250 if (instance
->response_count())
252 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
254 if (ptr
->flags
.no_block
)
256 memcached_io_write(instance
);
259 while(instance
->response_count())
261 (void)memcached_response(instance
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, &ptr
->result
);
266 if (memcached_is_binary(ptr
))
268 return binary_mget_by_key(ptr
, master_server_key
, is_group_key_set
, keys
,
269 key_length
, number_of_keys
, mget_mode
);
272 if (ptr
->flags
.support_cas
)
275 get_command_length
= 4;
279 If a server fails we warn about errors and start all over with sending keys
282 WATCHPOINT_ASSERT(rc
== MEMCACHED_SUCCESS
);
283 size_t hosts_connected
= 0;
284 for (uint32_t x
= 0; x
< number_of_keys
; x
++)
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 org::libmemcached::Instance
* instance
= memcached_instance_fetch(ptr
, server_key
);
299 libmemcached_io_vector_st vector
[]=
301 { get_command
, get_command_length
},
302 { memcached_literal_param(" ") },
303 { memcached_array_string(ptr
->_namespace
), memcached_array_size(ptr
->_namespace
) },
304 { keys
[x
], key_length
[x
] }
308 if (instance
->response_count() == 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
, 1, false)) == false)
321 failures_occured_in_sending
= true;
324 WATCHPOINT_ASSERT(instance
->cursor_active_
== 0);
325 memcached_instance_response_increment(instance
);
326 WATCHPOINT_ASSERT(instance
->cursor_active_
== 1);
330 if ((memcached_io_writev(instance
, (vector
+ 1), 3, false)) == false)
332 memcached_instance_response_reset(instance
);
333 failures_occured_in_sending
= true;
339 if (hosts_connected
== 0)
341 LIBMEMCACHED_MEMCACHED_MGET_END();
343 if (memcached_failed(rc
))
348 return memcached_set_error(*ptr
, MEMCACHED_NO_SERVERS
, MEMCACHED_AT
);
353 Should we muddle on if some servers are dead?
355 bool success_happened
= false;
356 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
358 org::libmemcached::Instance
* instance
= memcached_instance_fetch(ptr
, x
);
360 if (instance
->response_count())
362 /* We need to do something about non-connnected hosts in the future */
363 if ((memcached_io_write(instance
, "\r\n", 2, true)) == -1)
365 failures_occured_in_sending
= true;
369 success_happened
= true;
374 LIBMEMCACHED_MEMCACHED_MGET_END();
376 if (failures_occured_in_sending
and success_happened
)
378 return MEMCACHED_SOME_ERRORS
;
381 if (success_happened
)
383 return MEMCACHED_SUCCESS
;
386 return MEMCACHED_FAILURE
; // Complete failure occurred
389 memcached_return_t
memcached_mget_by_key(memcached_st
*shell
,
390 const char *group_key
,
391 size_t group_key_length
,
392 const char * const *keys
,
393 const size_t *key_length
,
394 size_t number_of_keys
)
396 Memcached
* ptr
= memcached2Memcached(shell
);
397 return memcached_mget_by_key_real(ptr
, group_key
, group_key_length
, keys
,
398 key_length
, number_of_keys
, true);
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
,
407 unsigned int number_of_callbacks
)
409 return memcached_mget_execute_by_key(ptr
, NULL
, 0, keys
, key_length
,
410 number_of_keys
, callback
,
411 context
, number_of_callbacks
);
414 memcached_return_t
memcached_mget_execute_by_key(memcached_st
*shell
,
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
,
422 unsigned int number_of_callbacks
)
424 Memcached
* ptr
= memcached2Memcached(shell
);
425 memcached_return_t rc
;
426 if (memcached_failed(rc
= initialize_query(ptr
, false)))
431 if (memcached_is_udp(ptr
))
433 return memcached_set_error(*ptr
, MEMCACHED_NOT_SUPPORTED
, MEMCACHED_AT
);
436 if (memcached_is_binary(ptr
) == false)
438 return memcached_set_error(*ptr
, MEMCACHED_NOT_SUPPORTED
, MEMCACHED_AT
,
439 memcached_literal_param("ASCII protocol is not supported for memcached_mget_execute_by_key()"));
442 memcached_callback_st
*original_callbacks
= ptr
->callbacks
;
443 memcached_callback_st cb
= {
450 rc
= memcached_mget_by_key(ptr
, group_key
, group_key_length
, keys
,
451 key_length
, number_of_keys
);
452 ptr
->callbacks
= original_callbacks
;
456 static memcached_return_t
simple_binary_mget(memcached_st
*ptr
,
457 uint32_t master_server_key
,
458 bool is_group_key_set
,
459 const char * const *keys
,
460 const size_t *key_length
,
461 size_t number_of_keys
, bool mget_mode
)
463 memcached_return_t rc
= MEMCACHED_NOTFOUND
;
465 bool flush
= (number_of_keys
== 1);
468 If a server fails we warn about errors and start all over with sending keys
471 for (uint32_t x
= 0; x
< number_of_keys
; ++x
)
475 if (is_group_key_set
)
477 server_key
= master_server_key
;
481 server_key
= memcached_generate_hash_with_redistribution(ptr
, keys
[x
], key_length
[x
]);
484 org::libmemcached::Instance
* instance
= memcached_instance_fetch(ptr
, server_key
);
486 if (instance
->response_count() == 0)
488 rc
= memcached_connect(instance
);
489 if (memcached_failed(rc
))
495 protocol_binary_request_getk request
= { }; //= {.bytes= {0}};
496 initialize_binary_request(instance
, request
.message
.header
);
499 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_GETKQ
;
503 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_GETK
;
506 memcached_return_t vk
;
507 vk
= memcached_validate_key_length(key_length
[x
],
508 ptr
->flags
.binary_protocol
);
509 if (vk
!= MEMCACHED_SUCCESS
)
513 memcached_io_reset(instance
);
519 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
[x
] + memcached_array_size(ptr
->_namespace
)));
520 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
521 request
.message
.header
.request
.bodylen
= htonl((uint32_t)( key_length
[x
] + memcached_array_size(ptr
->_namespace
)));
523 libmemcached_io_vector_st vector
[]=
525 { request
.bytes
, sizeof(request
.bytes
) },
526 { memcached_array_string(ptr
->_namespace
), memcached_array_size(ptr
->_namespace
) },
527 { keys
[x
], key_length
[x
] }
530 if (memcached_io_writev(instance
, vector
, 3, flush
) == false)
532 memcached_server_response_reset(instance
);
533 rc
= MEMCACHED_SOME_ERRORS
;
537 /* We just want one pending response per server */
538 memcached_server_response_reset(instance
);
539 memcached_server_response_increment(instance
);
540 if ((x
> 0 and x
== ptr
->io_key_prefetch
) and memcached_flush_buffers(ptr
) != MEMCACHED_SUCCESS
)
542 rc
= MEMCACHED_SOME_ERRORS
;
549 Send a noop command to flush the buffers
551 protocol_binary_request_noop request
= {}; //= {.bytes= {0}};
552 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_NOOP
;
553 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
555 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); ++x
)
557 org::libmemcached::Instance
* instance
= memcached_instance_fetch(ptr
, x
);
558 initialize_binary_request(instance
, request
.message
.header
);
560 if (instance
->response_count())
562 if (memcached_io_write(instance
) == false)
564 memcached_instance_response_reset(instance
);
565 memcached_io_reset(instance
);
566 rc
= MEMCACHED_SOME_ERRORS
;
569 if (memcached_io_write(instance
, request
.bytes
,
570 sizeof(request
.bytes
), true) == -1)
572 memcached_instance_response_reset(instance
);
573 memcached_io_reset(instance
);
574 rc
= MEMCACHED_SOME_ERRORS
;
584 static memcached_return_t
replication_binary_mget(memcached_st
*ptr
,
587 const char *const *keys
,
588 const size_t *key_length
,
589 size_t number_of_keys
)
591 memcached_return_t rc
= MEMCACHED_NOTFOUND
;
593 uint64_t randomize_read
= memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ
);
597 start
= (uint32_t)random() % (uint32_t)(ptr
->number_of_replicas
+ 1);
600 /* Loop for each replica */
601 for (uint32_t replica
= 0; replica
<= ptr
->number_of_replicas
; ++replica
)
605 for (uint32_t x
= 0; x
< number_of_keys
; ++x
)
607 if (hash
[x
] == memcached_server_count(ptr
))
608 continue; /* Already successfully sent */
610 uint32_t server
= hash
[x
] + replica
;
612 /* In case of randomized reads */
613 if (randomize_read
and ((server
+ start
) <= (hash
[x
] + ptr
->number_of_replicas
)))
618 while (server
>= memcached_server_count(ptr
))
620 server
-= memcached_server_count(ptr
);
623 if (dead_servers
[server
])
628 org::libmemcached::Instance
* instance
= memcached_instance_fetch(ptr
, server
);
630 if (instance
->response_count() == 0)
632 rc
= memcached_connect(instance
);
634 if (memcached_failed(rc
))
636 memcached_io_reset(instance
);
637 dead_servers
[server
]= true;
643 protocol_binary_request_getk request
= {};
644 initialize_binary_request(instance
, request
.message
.header
);
645 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_GETK
;
646 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
[x
] + memcached_array_size(ptr
->_namespace
)));
647 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
648 request
.message
.header
.request
.bodylen
= htonl((uint32_t)(key_length
[x
] + memcached_array_size(ptr
->_namespace
)));
651 * We need to disable buffering to actually know that the request was
652 * successfully sent to the server (so that we should expect a result
653 * back). It would be nice to do this in buffered mode, but then it
654 * would be complex to handle all error situations if we got to send
655 * some of the messages, and then we failed on writing out some others
656 * and we used the callback interface from memcached_mget_execute so
657 * that we might have processed some of the responses etc. For now,
658 * just make sure we work _correctly_
660 libmemcached_io_vector_st vector
[]=
662 { request
.bytes
, sizeof(request
.bytes
) },
663 { memcached_array_string(ptr
->_namespace
), memcached_array_size(ptr
->_namespace
) },
664 { keys
[x
], key_length
[x
] }
667 if (memcached_io_writev(instance
, vector
, 3, true) == false)
669 memcached_io_reset(instance
);
670 dead_servers
[server
]= true;
675 memcached_server_response_increment(instance
);
676 hash
[x
]= memcached_server_count(ptr
);
688 static memcached_return_t
binary_mget_by_key(memcached_st
*ptr
,
689 uint32_t master_server_key
,
690 bool is_group_key_set
,
691 const char * const *keys
,
692 const size_t *key_length
,
693 size_t number_of_keys
,
696 if (ptr
->number_of_replicas
== 0)
698 return simple_binary_mget(ptr
, master_server_key
, is_group_key_set
,
699 keys
, key_length
, number_of_keys
, mget_mode
);
702 uint32_t* hash
= libmemcached_xvalloc(ptr
, number_of_keys
, uint32_t);
703 bool* dead_servers
= libmemcached_xcalloc(ptr
, memcached_server_count(ptr
), bool);
705 if (hash
== NULL
or dead_servers
== NULL
)
707 libmemcached_free(ptr
, hash
);
708 libmemcached_free(ptr
, dead_servers
);
709 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
712 if (is_group_key_set
)
714 for (size_t x
= 0; x
< number_of_keys
; x
++)
716 hash
[x
]= master_server_key
;
721 for (size_t x
= 0; x
< number_of_keys
; x
++)
723 hash
[x
]= memcached_generate_hash_with_redistribution(ptr
, keys
[x
], key_length
[x
]);
727 memcached_return_t rc
= replication_binary_mget(ptr
, hash
, dead_servers
, keys
,
728 key_length
, number_of_keys
);
730 WATCHPOINT_IFERROR(rc
);
731 libmemcached_free(ptr
, hash
);
732 libmemcached_free(ptr
, dead_servers
);
734 return MEMCACHED_SUCCESS
;