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
;
75 if (ptr
->flags
.use_udp
)
82 *error
= memcached_set_error(*ptr
, MEMCACHED_NOT_SUPPORTED
, MEMCACHED_AT
);
86 uint64_t query_id
= ptr
->query_id
;
90 *error
= memcached_mget_by_key_real(ptr
, group_key
, group_key_length
,
91 (const char * const *)&key
, &key_length
,
93 assert_msg(ptr
->query_id
== query_id
+1, "Programmer error, the query_id was not incremented.");
96 if (memcached_failed(*error
))
98 if (memcached_has_current_error(*ptr
)) // Find the most accurate error
100 *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
)
115 *error
= MEMCACHED_NOTFOUND
;
119 if (ptr
->get_key_failure
&& *error
== MEMCACHED_NOTFOUND
)
121 memcached_result_reset(&ptr
->result
);
122 memcached_return_t rc
= ptr
->get_key_failure(ptr
, key
, key_length
, &ptr
->result
);
124 /* On all failure drop to returning NULL */
125 if (rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
)
127 if (rc
== MEMCACHED_BUFFERED
)
129 uint64_t latch
; /* We use latch to track the state of the original socket */
130 latch
= memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
);
132 memcached_behavior_set(ptr
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
134 rc
= memcached_set(ptr
, key
, key_length
,
135 (memcached_result_value(&ptr
->result
)),
136 (memcached_result_length(&ptr
->result
)),
138 (memcached_result_flags(&ptr
->result
)));
140 if (rc
== MEMCACHED_BUFFERED
&& latch
== 0)
142 memcached_behavior_set(ptr
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 0);
147 rc
= memcached_set(ptr
, key
, key_length
,
148 (memcached_result_value(&ptr
->result
)),
149 (memcached_result_length(&ptr
->result
)),
151 (memcached_result_flags(&ptr
->result
)));
154 if (rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
)
157 *value_length
= memcached_result_length(&ptr
->result
);
158 *flags
= memcached_result_flags(&ptr
->result
);
159 return memcached_string_take_value(&ptr
->result
.value
);
163 assert_msg(ptr
->query_id
== query_id
+1, "Programmer error, the query_id was not incremented.");
169 uint32_t dummy_flags
;
170 memcached_return_t dummy_error
;
172 char *dummy_value
= memcached_fetch(ptr
, NULL
, NULL
,
173 &dummy_length
, &dummy_flags
,
175 assert_msg(dummy_value
== 0, "memcached_fetch() returned additional values beyond the single get it expected");
176 assert_msg(dummy_length
== 0, "memcached_fetch() returned additional values beyond the single get it expected");
177 assert_msg(ptr
->query_id
== query_id
+1, "Programmer error, the query_id was not incremented.");
182 memcached_return_t
memcached_mget(memcached_st
*ptr
,
183 const char * const *keys
,
184 const size_t *key_length
,
185 size_t number_of_keys
)
187 return memcached_mget_by_key(ptr
, NULL
, 0, keys
, key_length
, number_of_keys
);
190 static memcached_return_t
binary_mget_by_key(memcached_st
*ptr
,
191 uint32_t master_server_key
,
192 bool is_group_key_set
,
193 const char * const *keys
,
194 const size_t *key_length
,
195 size_t number_of_keys
,
198 static memcached_return_t
memcached_mget_by_key_real(memcached_st
*ptr
,
199 const char *group_key
,
200 size_t group_key_length
,
201 const char * const *keys
,
202 const size_t *key_length
,
203 size_t number_of_keys
,
206 bool failures_occured_in_sending
= false;
207 const char *get_command
= "get ";
208 uint8_t get_command_length
= 4;
209 unsigned int master_server_key
= (unsigned int)-1; /* 0 is a valid server id! */
211 memcached_return_t rc
;
212 if (memcached_failed(rc
= initialize_query(ptr
)))
217 if (ptr
->flags
.use_udp
)
219 return memcached_set_error(*ptr
, MEMCACHED_NOT_SUPPORTED
, MEMCACHED_AT
);
222 LIBMEMCACHED_MEMCACHED_MGET_START();
224 if (number_of_keys
== 0)
226 return memcached_set_error(*ptr
, MEMCACHED_NOTFOUND
, MEMCACHED_AT
, memcached_literal_param("number_of_keys was zero"));
229 if (memcached_failed(memcached_key_test(*ptr
, keys
, key_length
, number_of_keys
)))
231 return memcached_set_error(*ptr
, MEMCACHED_BAD_KEY_PROVIDED
, MEMCACHED_AT
, memcached_literal_param("A bad key value was provided"));
234 bool is_group_key_set
= false;
235 if (group_key
and group_key_length
)
237 if (memcached_failed(memcached_key_test(*ptr
, (const char * const *)&group_key
, &group_key_length
, 1)))
239 return memcached_set_error(*ptr
, MEMCACHED_BAD_KEY_PROVIDED
, MEMCACHED_AT
, memcached_literal_param("A bad group key was provided."));
242 master_server_key
= memcached_generate_hash_with_redistribution(ptr
, group_key
, group_key_length
);
243 is_group_key_set
= true;
247 Here is where we pay for the non-block API. We need to remove any data sitting
248 in the queue before we start our get.
250 It might be optimum to bounce the connection if count > some number.
252 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
254 memcached_server_write_instance_st instance
=
255 memcached_server_instance_fetch(ptr
, x
);
257 if (memcached_server_response_count(instance
))
259 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
261 if (ptr
->flags
.no_block
)
263 memcached_io_write(instance
);
266 while(memcached_server_response_count(instance
))
268 (void)memcached_response(instance
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, &ptr
->result
);
273 if (ptr
->flags
.binary_protocol
)
275 return binary_mget_by_key(ptr
, master_server_key
, is_group_key_set
, keys
,
276 key_length
, number_of_keys
, mget_mode
);
279 if (ptr
->flags
.support_cas
)
281 get_command
= "gets ";
282 get_command_length
= 5;
286 If a server fails we warn about errors and start all over with sending keys
289 WATCHPOINT_ASSERT(rc
== MEMCACHED_SUCCESS
);
290 size_t hosts_connected
= 0;
291 for (uint32_t x
= 0; x
< number_of_keys
; x
++)
293 memcached_server_write_instance_st instance
;
296 if (is_group_key_set
)
298 server_key
= master_server_key
;
302 server_key
= memcached_generate_hash_with_redistribution(ptr
, keys
[x
], key_length
[x
]);
305 instance
= memcached_server_instance_fetch(ptr
, server_key
);
307 struct libmemcached_io_vector_st vector
[]=
309 { get_command
, get_command_length
},
310 { memcached_array_string(ptr
->_namespace
), memcached_array_size(ptr
->_namespace
) },
311 { keys
[x
], key_length
[x
] },
312 { memcached_literal_param(" ") }
316 if (memcached_server_response_count(instance
) == 0)
318 rc
= memcached_connect(instance
);
320 if (memcached_failed(rc
))
322 memcached_set_error(*instance
, rc
, MEMCACHED_AT
);
327 if ((memcached_io_writev(instance
, vector
, 4, false)) == -1)
329 failures_occured_in_sending
= true;
332 WATCHPOINT_ASSERT(instance
->cursor_active
== 0);
333 memcached_server_response_increment(instance
);
334 WATCHPOINT_ASSERT(instance
->cursor_active
== 1);
338 if ((memcached_io_writev(instance
, (vector
+ 1), 3, false)) == -1)
340 memcached_server_response_reset(instance
);
341 failures_occured_in_sending
= true;
347 if (hosts_connected
== 0)
349 LIBMEMCACHED_MEMCACHED_MGET_END();
351 if (memcached_failed(rc
))
354 return memcached_set_error(*ptr
, MEMCACHED_NO_SERVERS
, MEMCACHED_AT
);
359 Should we muddle on if some servers are dead?
361 bool success_happened
= false;
362 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
364 memcached_server_write_instance_st instance
=
365 memcached_server_instance_fetch(ptr
, x
);
367 if (memcached_server_response_count(instance
))
369 /* We need to do something about non-connnected hosts in the future */
370 if ((memcached_io_write(instance
, "\r\n", 2, true)) == -1)
372 failures_occured_in_sending
= true;
376 success_happened
= true;
381 LIBMEMCACHED_MEMCACHED_MGET_END();
383 if (failures_occured_in_sending
&& success_happened
)
385 return MEMCACHED_SOME_ERRORS
;
388 if (success_happened
)
389 return MEMCACHED_SUCCESS
;
391 return MEMCACHED_FAILURE
; // Complete failure occurred
394 memcached_return_t
memcached_mget_by_key(memcached_st
*ptr
,
395 const char *group_key
,
396 size_t group_key_length
,
397 const char * const *keys
,
398 const size_t *key_length
,
399 size_t number_of_keys
)
401 return memcached_mget_by_key_real(ptr
, group_key
, group_key_length
, keys
,
402 key_length
, number_of_keys
, true);
405 memcached_return_t
memcached_mget_execute(memcached_st
*ptr
,
406 const char * const *keys
,
407 const size_t *key_length
,
408 size_t number_of_keys
,
409 memcached_execute_fn
*callback
,
411 unsigned int number_of_callbacks
)
413 return memcached_mget_execute_by_key(ptr
, NULL
, 0, keys
, key_length
,
414 number_of_keys
, callback
,
415 context
, number_of_callbacks
);
418 memcached_return_t
memcached_mget_execute_by_key(memcached_st
*ptr
,
419 const char *group_key
,
420 size_t group_key_length
,
421 const char * const *keys
,
422 const size_t *key_length
,
423 size_t number_of_keys
,
424 memcached_execute_fn
*callback
,
426 unsigned int number_of_callbacks
)
428 if ((ptr
->flags
.binary_protocol
) == 0)
430 return MEMCACHED_NOT_SUPPORTED
;
433 memcached_return_t rc
;
434 memcached_callback_st
*original_callbacks
= ptr
->callbacks
;
435 memcached_callback_st cb
= {
442 rc
= memcached_mget_by_key(ptr
, group_key
, group_key_length
, keys
,
443 key_length
, number_of_keys
);
444 ptr
->callbacks
= original_callbacks
;
448 static memcached_return_t
simple_binary_mget(memcached_st
*ptr
,
449 uint32_t master_server_key
,
450 bool is_group_key_set
,
451 const char * const *keys
,
452 const size_t *key_length
,
453 size_t number_of_keys
, bool mget_mode
)
455 memcached_return_t rc
= MEMCACHED_NOTFOUND
;
457 bool flush
= (number_of_keys
== 1);
460 If a server fails we warn about errors and start all over with sending keys
463 for (uint32_t x
= 0; x
< number_of_keys
; ++x
)
467 if (is_group_key_set
)
469 server_key
= master_server_key
;
473 server_key
= memcached_generate_hash_with_redistribution(ptr
, keys
[x
], key_length
[x
]);
476 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, server_key
);
478 if (memcached_server_response_count(instance
) == 0)
480 rc
= memcached_connect(instance
);
481 if (memcached_failed(rc
))
487 protocol_binary_request_getk request
= { }; //= {.bytes= {0}};
488 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
490 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_GETKQ
;
492 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_GETK
;
494 memcached_return_t vk
;
495 vk
= memcached_validate_key_length(key_length
[x
],
496 ptr
->flags
.binary_protocol
);
497 unlikely (vk
!= MEMCACHED_SUCCESS
)
501 memcached_io_reset(instance
);
507 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
[x
] + memcached_array_size(ptr
->_namespace
)));
508 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
509 request
.message
.header
.request
.bodylen
= htonl((uint32_t)( key_length
[x
] + memcached_array_size(ptr
->_namespace
)));
511 struct libmemcached_io_vector_st vector
[]=
513 { request
.bytes
, sizeof(request
.bytes
) },
514 { memcached_array_string(ptr
->_namespace
), memcached_array_size(ptr
->_namespace
) },
515 { keys
[x
], key_length
[x
] }
518 if (memcached_io_writev(instance
, vector
, 3, flush
) == -1)
520 memcached_server_response_reset(instance
);
521 rc
= MEMCACHED_SOME_ERRORS
;
525 /* We just want one pending response per server */
526 memcached_server_response_reset(instance
);
527 memcached_server_response_increment(instance
);
528 if ((x
> 0 && x
== ptr
->io_key_prefetch
) && memcached_flush_buffers(ptr
) != MEMCACHED_SUCCESS
)
530 rc
= MEMCACHED_SOME_ERRORS
;
537 Send a noop command to flush the buffers
539 protocol_binary_request_noop request
= {}; //= {.bytes= {0}};
540 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
541 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_NOOP
;
542 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
544 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); ++x
)
546 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, x
);
548 if (memcached_server_response_count(instance
))
550 if (memcached_io_write(instance
) == false)
552 memcached_server_response_reset(instance
);
553 memcached_io_reset(instance
);
554 rc
= MEMCACHED_SOME_ERRORS
;
557 if (memcached_io_write(instance
, request
.bytes
,
558 sizeof(request
.bytes
), true) == -1)
560 memcached_server_response_reset(instance
);
561 memcached_io_reset(instance
);
562 rc
= MEMCACHED_SOME_ERRORS
;
572 static memcached_return_t
replication_binary_mget(memcached_st
*ptr
,
575 const char *const *keys
,
576 const size_t *key_length
,
577 size_t number_of_keys
)
579 memcached_return_t rc
= MEMCACHED_NOTFOUND
;
581 uint64_t randomize_read
= memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ
);
584 start
= (uint32_t)random() % (uint32_t)(ptr
->number_of_replicas
+ 1);
586 /* Loop for each replica */
587 for (uint32_t replica
= 0; replica
<= ptr
->number_of_replicas
; ++replica
)
591 for (uint32_t x
= 0; x
< number_of_keys
; ++x
)
593 if (hash
[x
] == memcached_server_count(ptr
))
594 continue; /* Already successfully sent */
596 uint32_t server
= hash
[x
] + replica
;
598 /* In case of randomized reads */
599 if (randomize_read
&& ((server
+ start
) <= (hash
[x
] + ptr
->number_of_replicas
)))
602 while (server
>= memcached_server_count(ptr
))
604 server
-= memcached_server_count(ptr
);
607 if (dead_servers
[server
])
612 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, server
);
614 if (memcached_server_response_count(instance
) == 0)
616 rc
= memcached_connect(instance
);
618 if (memcached_failed(rc
))
620 memcached_io_reset(instance
);
621 dead_servers
[server
]= true;
627 protocol_binary_request_getk request
= {};
628 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
629 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_GETK
;
630 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
[x
] + memcached_array_size(ptr
->_namespace
)));
631 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
632 request
.message
.header
.request
.bodylen
= htonl((uint32_t)(key_length
[x
] + memcached_array_size(ptr
->_namespace
)));
635 * We need to disable buffering to actually know that the request was
636 * successfully sent to the server (so that we should expect a result
637 * back). It would be nice to do this in buffered mode, but then it
638 * would be complex to handle all error situations if we got to send
639 * some of the messages, and then we failed on writing out some others
640 * and we used the callback interface from memcached_mget_execute so
641 * that we might have processed some of the responses etc. For now,
642 * just make sure we work _correctly_
644 struct libmemcached_io_vector_st vector
[]=
646 { request
.bytes
, sizeof(request
.bytes
) },
647 { memcached_array_string(ptr
->_namespace
), memcached_array_size(ptr
->_namespace
) },
648 { keys
[x
], key_length
[x
] }
651 if (memcached_io_writev(instance
, vector
, 3, true) == -1)
653 memcached_io_reset(instance
);
654 dead_servers
[server
]= true;
659 memcached_server_response_increment(instance
);
660 hash
[x
]= memcached_server_count(ptr
);
672 static memcached_return_t
binary_mget_by_key(memcached_st
*ptr
,
673 uint32_t master_server_key
,
674 bool is_group_key_set
,
675 const char * const *keys
,
676 const size_t *key_length
,
677 size_t number_of_keys
,
680 if (ptr
->number_of_replicas
== 0)
682 return simple_binary_mget(ptr
, master_server_key
, is_group_key_set
,
683 keys
, key_length
, number_of_keys
, mget_mode
);
686 uint32_t* hash
= libmemcached_xvalloc(ptr
, number_of_keys
, uint32_t);
687 bool* dead_servers
= libmemcached_xcalloc(ptr
, memcached_server_count(ptr
), bool);
689 if (hash
== NULL
|| dead_servers
== NULL
)
691 libmemcached_free(ptr
, hash
);
692 libmemcached_free(ptr
, dead_servers
);
693 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
696 if (is_group_key_set
)
698 for (size_t x
= 0; x
< number_of_keys
; x
++)
700 hash
[x
]= master_server_key
;
705 for (size_t x
= 0; x
< number_of_keys
; x
++)
707 hash
[x
]= memcached_generate_hash_with_redistribution(ptr
, keys
[x
], key_length
[x
]);
711 memcached_return_t rc
= replication_binary_mget(ptr
, hash
, dead_servers
, keys
,
712 key_length
, number_of_keys
);
714 WATCHPOINT_IFERROR(rc
);
715 libmemcached_free(ptr
, hash
);
716 libmemcached_free(ptr
, dead_servers
);
718 return MEMCACHED_SUCCESS
;