1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011-2013 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
__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 const bool mget_mode
);
60 char *memcached_get_by_key(memcached_st
*shell
,
61 const char *group_key
,
62 size_t group_key_length
,
63 const char *key
, size_t key_length
,
66 memcached_return_t
*error
)
68 Memcached
* ptr
= memcached2Memcached(shell
);
69 memcached_return_t unused
;
78 query_id
= ptr
->query_id
;
82 *error
= __mget_by_key_real(ptr
, group_key
, group_key_length
,
83 (const char * const *)&key
, &key_length
,
87 assert_msg(ptr
->query_id
== query_id
+1, "Programmer error, the query_id was not incremented.");
90 if (memcached_failed(*error
))
94 if (memcached_has_current_error(*ptr
)) // Find the most accurate error
96 *error
= memcached_last_error(ptr
);
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.");
112 /* This is for historical reasons */
113 if (*error
== MEMCACHED_END
)
115 *error
= MEMCACHED_NOTFOUND
;
119 if (ptr
->get_key_failure
and *error
== MEMCACHED_NOTFOUND
)
121 memcached_result_st key_failure_result
;
122 memcached_result_st
* result_ptr
= memcached_result_create(ptr
, &key_failure_result
);
123 memcached_return_t rc
= ptr
->get_key_failure(ptr
, key
, key_length
, result_ptr
);
125 /* On all failure drop to returning NULL */
126 if (rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
)
128 if (rc
== MEMCACHED_BUFFERED
)
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
);
134 memcached_behavior_set(ptr
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
137 rc
= memcached_set(ptr
, key
, key_length
,
138 (memcached_result_value(result_ptr
)),
139 (memcached_result_length(result_ptr
)),
141 (memcached_result_flags(result_ptr
)));
143 if (rc
== MEMCACHED_BUFFERED
and latch
== 0)
145 memcached_behavior_set(ptr
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 0);
150 rc
= memcached_set(ptr
, key
, key_length
,
151 (memcached_result_value(result_ptr
)),
152 (memcached_result_length(result_ptr
)),
154 (memcached_result_flags(result_ptr
)));
157 if (rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
)
160 *value_length
= memcached_result_length(result_ptr
);
161 *flags
= memcached_result_flags(result_ptr
);
162 char *result_value
= memcached_string_take_value(&result_ptr
->value
);
163 memcached_result_free(result_ptr
);
169 memcached_result_free(result_ptr
);
171 assert_msg(ptr
->query_id
== query_id
+1, "Programmer error, the query_id was not incremented.");
179 memcached_return_t
memcached_mget(memcached_st
*ptr
,
180 const char * const *keys
,
181 const size_t *key_length
,
182 size_t number_of_keys
)
184 return memcached_mget_by_key(ptr
, NULL
, 0, keys
, key_length
, number_of_keys
);
187 static memcached_return_t
binary_mget_by_key(memcached_st
*ptr
,
188 const uint32_t master_server_key
,
189 const bool is_group_key_set
,
190 const char * const *keys
,
191 const size_t *key_length
,
192 const size_t number_of_keys
,
193 const bool mget_mode
);
195 static memcached_return_t
__mget_by_key_real(memcached_st
*ptr
,
196 const char *group_key
,
197 const size_t group_key_length
,
198 const char * const *keys
,
199 const size_t *key_length
,
200 size_t number_of_keys
,
201 const bool mget_mode
)
203 bool failures_occured_in_sending
= false;
204 const char *get_command
= "get";
205 uint8_t get_command_length
= 3;
206 unsigned int master_server_key
= (unsigned int)-1; /* 0 is a valid server id! */
208 memcached_return_t rc
;
209 if (memcached_failed(rc
= initialize_query(ptr
, true)))
214 if (memcached_is_udp(ptr
))
216 return memcached_set_error(*ptr
, MEMCACHED_NOT_SUPPORTED
, MEMCACHED_AT
);
219 LIBMEMCACHED_MEMCACHED_MGET_START();
221 if (number_of_keys
== 0)
223 return memcached_set_error(*ptr
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
, memcached_literal_param("Numbers of keys provided was zero"));
226 if (memcached_failed((rc
= memcached_key_test(*ptr
, keys
, key_length
, number_of_keys
))))
228 assert(memcached_last_error(ptr
) == rc
);
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 memcached_instance_st
* 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 memcached_instance_st
* 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 memcached_instance_st
* 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 __mget_by_key_real(ptr
, group_key
, group_key_length
, keys
, key_length
, number_of_keys
, true);
400 memcached_return_t
memcached_mget_execute(memcached_st
*ptr
,
401 const char * const *keys
,
402 const size_t *key_length
,
403 size_t number_of_keys
,
404 memcached_execute_fn
*callback
,
406 unsigned int number_of_callbacks
)
408 return memcached_mget_execute_by_key(ptr
, NULL
, 0, keys
, key_length
,
409 number_of_keys
, callback
,
410 context
, number_of_callbacks
);
413 memcached_return_t
memcached_mget_execute_by_key(memcached_st
*shell
,
414 const char *group_key
,
415 size_t group_key_length
,
416 const char * const *keys
,
417 const size_t *key_length
,
418 size_t number_of_keys
,
419 memcached_execute_fn
*callback
,
421 unsigned int number_of_callbacks
)
423 Memcached
* ptr
= memcached2Memcached(shell
);
424 memcached_return_t rc
;
425 if (memcached_failed(rc
= initialize_query(ptr
, false)))
430 if (memcached_is_udp(ptr
))
432 return memcached_set_error(*ptr
, MEMCACHED_NOT_SUPPORTED
, MEMCACHED_AT
);
435 if (memcached_is_binary(ptr
) == false)
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()"));
441 memcached_callback_st
*original_callbacks
= ptr
->callbacks
;
442 memcached_callback_st 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
;
456 static memcached_return_t
simple_binary_mget(memcached_st
*ptr
,
457 const uint32_t master_server_key
,
458 bool is_group_key_set
,
459 const char * const *keys
,
460 const size_t *key_length
,
461 const size_t number_of_keys
, const bool mget_mode
)
463 memcached_return_t rc
= MEMCACHED_NOTFOUND
;
465 bool flush
= (number_of_keys
== 1);
467 if (memcached_failed(rc
= memcached_key_test(*ptr
, keys
, key_length
, number_of_keys
)))
473 If a server fails we warn about errors and start all over with sending keys
476 for (uint32_t x
= 0; x
< number_of_keys
; ++x
)
480 if (is_group_key_set
)
482 server_key
= master_server_key
;
486 server_key
= memcached_generate_hash_with_redistribution(ptr
, keys
[x
], key_length
[x
]);
489 memcached_instance_st
* instance
= memcached_instance_fetch(ptr
, server_key
);
491 if (instance
->response_count() == 0)
493 rc
= memcached_connect(instance
);
494 if (memcached_failed(rc
))
500 protocol_binary_request_getk request
= { }; //= {.bytes= {0}};
501 initialize_binary_request(instance
, request
.message
.header
);
504 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_GETKQ
;
508 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_GETK
;
513 memcached_return_t vk
= memcached_validate_key_length(key_length
[x
], ptr
->flags
.binary_protocol
);
514 if (memcached_failed(rc
= memcached_key_test(*memc
, (const char **)&key
, &key_length
, 1)))
516 memcached_set_error(ptr
, vk
, MEMCACHED_AT
, memcached_literal_param("Key was too long."));
520 memcached_io_reset(instance
);
528 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
[x
] + memcached_array_size(ptr
->_namespace
)));
529 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
530 request
.message
.header
.request
.bodylen
= htonl((uint32_t)( key_length
[x
] + memcached_array_size(ptr
->_namespace
)));
532 libmemcached_io_vector_st vector
[]=
534 { request
.bytes
, sizeof(request
.bytes
) },
535 { memcached_array_string(ptr
->_namespace
), memcached_array_size(ptr
->_namespace
) },
536 { keys
[x
], key_length
[x
] }
539 if (memcached_io_writev(instance
, vector
, 3, flush
) == false)
541 memcached_server_response_reset(instance
);
542 rc
= MEMCACHED_SOME_ERRORS
;
546 /* We just want one pending response per server */
547 memcached_server_response_reset(instance
);
548 memcached_server_response_increment(instance
);
549 if ((x
> 0 and x
== ptr
->io_key_prefetch
) and memcached_flush_buffers(ptr
) != MEMCACHED_SUCCESS
)
551 rc
= MEMCACHED_SOME_ERRORS
;
558 Send a noop command to flush the buffers
560 protocol_binary_request_noop request
= {}; //= {.bytes= {0}};
561 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_NOOP
;
562 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
564 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); ++x
)
566 memcached_instance_st
* instance
= memcached_instance_fetch(ptr
, x
);
568 if (instance
->response_count())
570 initialize_binary_request(instance
, request
.message
.header
);
571 if ((memcached_io_write(instance
) == false) or
572 (memcached_io_write(instance
, request
.bytes
, sizeof(request
.bytes
), true) == -1))
574 memcached_instance_response_reset(instance
);
575 memcached_io_reset(instance
);
576 rc
= MEMCACHED_SOME_ERRORS
;
585 static memcached_return_t
replication_binary_mget(memcached_st
*ptr
,
588 const char *const *keys
,
589 const size_t *key_length
,
590 const size_t number_of_keys
)
592 memcached_return_t rc
= MEMCACHED_NOTFOUND
;
594 uint64_t randomize_read
= memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ
);
598 start
= (uint32_t)random() % (uint32_t)(ptr
->number_of_replicas
+ 1);
601 /* Loop for each replica */
602 for (uint32_t replica
= 0; replica
<= ptr
->number_of_replicas
; ++replica
)
606 for (uint32_t x
= 0; x
< number_of_keys
; ++x
)
608 if (hash
[x
] == memcached_server_count(ptr
))
610 continue; /* Already successfully sent */
613 uint32_t server
= hash
[x
] +replica
;
615 /* In case of randomized reads */
616 if (randomize_read
and ((server
+ start
) <= (hash
[x
] + ptr
->number_of_replicas
)))
621 while (server
>= memcached_server_count(ptr
))
623 server
-= memcached_server_count(ptr
);
626 if (dead_servers
[server
])
631 memcached_instance_st
* instance
= memcached_instance_fetch(ptr
, server
);
633 if (instance
->response_count() == 0)
635 rc
= memcached_connect(instance
);
637 if (memcached_failed(rc
))
639 memcached_io_reset(instance
);
640 dead_servers
[server
]= true;
646 protocol_binary_request_getk request
= {};
647 initialize_binary_request(instance
, request
.message
.header
);
648 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_GETK
;
649 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
[x
] + memcached_array_size(ptr
->_namespace
)));
650 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
651 request
.message
.header
.request
.bodylen
= htonl((uint32_t)(key_length
[x
] + memcached_array_size(ptr
->_namespace
)));
654 * We need to disable buffering to actually know that the request was
655 * successfully sent to the server (so that we should expect a result
656 * back). It would be nice to do this in buffered mode, but then it
657 * would be complex to handle all error situations if we got to send
658 * some of the messages, and then we failed on writing out some others
659 * and we used the callback interface from memcached_mget_execute so
660 * that we might have processed some of the responses etc. For now,
661 * just make sure we work _correctly_
663 libmemcached_io_vector_st vector
[]=
665 { request
.bytes
, sizeof(request
.bytes
) },
666 { memcached_array_string(ptr
->_namespace
), memcached_array_size(ptr
->_namespace
) },
667 { keys
[x
], key_length
[x
] }
670 if (memcached_io_writev(instance
, vector
, 3, true) == false)
672 memcached_io_reset(instance
);
673 dead_servers
[server
]= true;
678 memcached_server_response_increment(instance
);
679 hash
[x
]= memcached_server_count(ptr
);
691 static memcached_return_t
binary_mget_by_key(memcached_st
*ptr
,
692 const uint32_t master_server_key
,
693 bool is_group_key_set
,
694 const char * const *keys
,
695 const size_t *key_length
,
696 const size_t number_of_keys
,
697 const bool mget_mode
)
699 if (ptr
->number_of_replicas
== 0)
701 return simple_binary_mget(ptr
, master_server_key
, is_group_key_set
,
702 keys
, key_length
, number_of_keys
, mget_mode
);
705 uint32_t* hash
= libmemcached_xvalloc(ptr
, number_of_keys
, uint32_t);
706 bool* dead_servers
= libmemcached_xcalloc(ptr
, memcached_server_count(ptr
), bool);
708 if (hash
== NULL
or dead_servers
== NULL
)
710 libmemcached_free(ptr
, hash
);
711 libmemcached_free(ptr
, dead_servers
);
712 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
715 if (is_group_key_set
)
717 for (size_t x
= 0; x
< number_of_keys
; x
++)
719 hash
[x
]= master_server_key
;
724 for (size_t x
= 0; x
< number_of_keys
; x
++)
726 hash
[x
]= memcached_generate_hash_with_redistribution(ptr
, keys
[x
], key_length
[x
]);
730 memcached_return_t rc
= replication_binary_mget(ptr
, hash
, dead_servers
, keys
,
731 key_length
, number_of_keys
);
733 WATCHPOINT_IFERROR(rc
);
734 libmemcached_free(ptr
, hash
);
735 libmemcached_free(ptr
, dead_servers
);
737 return MEMCACHED_SUCCESS
;