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
;
78 query_id
= ptr
->query_id
;
82 *error
= memcached_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
;
120 if (ptr
->get_key_failure
&& *error
== MEMCACHED_NOTFOUND
)
122 memcached_result_reset(&ptr
->result
);
123 memcached_return_t rc
= ptr
->get_key_failure(ptr
, key
, key_length
, &ptr
->result
);
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(&ptr
->result
)),
139 (memcached_result_length(&ptr
->result
)),
141 (memcached_result_flags(&ptr
->result
)));
143 if (rc
== MEMCACHED_BUFFERED
&& latch
== 0)
145 memcached_behavior_set(ptr
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 0);
150 rc
= memcached_set(ptr
, key
, key_length
,
151 (memcached_result_value(&ptr
->result
)),
152 (memcached_result_length(&ptr
->result
)),
154 (memcached_result_flags(&ptr
->result
)));
157 if (rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
)
160 *value_length
= memcached_result_length(&ptr
->result
);
161 *flags
= memcached_result_flags(&ptr
->result
);
162 return memcached_string_take_value(&ptr
->result
.value
);
166 assert_msg(ptr
->query_id
== query_id
+1, "Programmer error, the query_id was not incremented.");
172 uint32_t dummy_flags
;
173 memcached_return_t dummy_error
;
175 char *dummy_value
= memcached_fetch(ptr
, NULL
, NULL
,
176 &dummy_length
, &dummy_flags
,
178 assert_msg(dummy_value
== 0, "memcached_fetch() returned additional values beyond the single get it expected");
179 assert_msg(dummy_length
== 0, "memcached_fetch() returned additional values beyond the single get it expected");
180 assert_msg(ptr
->query_id
== query_id
+1, "Programmer error, the query_id was not incremented.");
185 memcached_return_t
memcached_mget(memcached_st
*ptr
,
186 const char * const *keys
,
187 const size_t *key_length
,
188 size_t number_of_keys
)
190 return memcached_mget_by_key(ptr
, NULL
, 0, keys
, key_length
, number_of_keys
);
193 static memcached_return_t
binary_mget_by_key(memcached_st
*ptr
,
194 uint32_t master_server_key
,
195 bool is_group_key_set
,
196 const char * const *keys
,
197 const size_t *key_length
,
198 size_t number_of_keys
,
201 static memcached_return_t
memcached_mget_by_key_real(memcached_st
*ptr
,
202 const char *group_key
,
203 size_t group_key_length
,
204 const char * const *keys
,
205 const size_t *key_length
,
206 size_t number_of_keys
,
209 bool failures_occured_in_sending
= false;
210 const char *get_command
= "get ";
211 uint8_t get_command_length
= 4;
212 unsigned int master_server_key
= (unsigned int)-1; /* 0 is a valid server id! */
214 memcached_return_t rc
;
215 if (memcached_failed(rc
= initialize_query(ptr
, true)))
220 if (memcached_is_udp(ptr
))
222 return memcached_set_error(*ptr
, MEMCACHED_NOT_SUPPORTED
, MEMCACHED_AT
);
225 LIBMEMCACHED_MEMCACHED_MGET_START();
227 if (number_of_keys
== 0)
229 return memcached_set_error(*ptr
, MEMCACHED_NOTFOUND
, MEMCACHED_AT
, memcached_literal_param("number_of_keys was zero"));
232 if (memcached_failed(memcached_key_test(*ptr
, keys
, key_length
, number_of_keys
)))
234 return memcached_last_error(ptr
);
237 bool is_group_key_set
= false;
238 if (group_key
and group_key_length
)
240 master_server_key
= memcached_generate_hash_with_redistribution(ptr
, group_key
, group_key_length
);
241 is_group_key_set
= true;
245 Here is where we pay for the non-block API. We need to remove any data sitting
246 in the queue before we start our get.
248 It might be optimum to bounce the connection if count > some number.
250 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
252 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, x
);
254 if (memcached_server_response_count(instance
))
256 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
258 if (ptr
->flags
.no_block
)
260 memcached_io_write(instance
);
263 while(memcached_server_response_count(instance
))
265 (void)memcached_response(instance
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, &ptr
->result
);
270 if (memcached_is_binary(ptr
))
272 return binary_mget_by_key(ptr
, master_server_key
, is_group_key_set
, keys
,
273 key_length
, number_of_keys
, mget_mode
);
276 if (ptr
->flags
.support_cas
)
278 get_command
= "gets ";
279 get_command_length
= 5;
283 If a server fails we warn about errors and start all over with sending keys
286 WATCHPOINT_ASSERT(rc
== MEMCACHED_SUCCESS
);
287 size_t hosts_connected
= 0;
288 for (uint32_t x
= 0; x
< number_of_keys
; x
++)
290 memcached_server_write_instance_st instance
;
293 if (is_group_key_set
)
295 server_key
= master_server_key
;
299 server_key
= memcached_generate_hash_with_redistribution(ptr
, keys
[x
], key_length
[x
]);
302 instance
= memcached_server_instance_fetch(ptr
, server_key
);
304 libmemcached_io_vector_st vector
[]=
306 { get_command
, get_command_length
},
307 { memcached_array_string(ptr
->_namespace
), memcached_array_size(ptr
->_namespace
) },
308 { keys
[x
], key_length
[x
] },
309 { memcached_literal_param(" ") }
313 if (memcached_server_response_count(instance
) == 0)
315 rc
= memcached_connect(instance
);
317 if (memcached_failed(rc
))
319 memcached_set_error(*instance
, rc
, MEMCACHED_AT
);
324 if ((memcached_io_writev(instance
, vector
, 4, false)) == -1)
326 failures_occured_in_sending
= true;
329 WATCHPOINT_ASSERT(instance
->cursor_active
== 0);
330 memcached_server_response_increment(instance
);
331 WATCHPOINT_ASSERT(instance
->cursor_active
== 1);
335 if ((memcached_io_writev(instance
, (vector
+ 1), 3, false)) == -1)
337 memcached_server_response_reset(instance
);
338 failures_occured_in_sending
= true;
344 if (hosts_connected
== 0)
346 LIBMEMCACHED_MEMCACHED_MGET_END();
348 if (memcached_failed(rc
))
353 return memcached_set_error(*ptr
, MEMCACHED_NO_SERVERS
, MEMCACHED_AT
);
358 Should we muddle on if some servers are dead?
360 bool success_happened
= false;
361 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
363 memcached_server_write_instance_st instance
=
364 memcached_server_instance_fetch(ptr
, x
);
366 if (memcached_server_response_count(instance
))
368 /* We need to do something about non-connnected hosts in the future */
369 if ((memcached_io_write(instance
, "\r\n", 2, true)) == -1)
371 failures_occured_in_sending
= true;
375 success_happened
= true;
380 LIBMEMCACHED_MEMCACHED_MGET_END();
382 if (failures_occured_in_sending
&& success_happened
)
384 return MEMCACHED_SOME_ERRORS
;
387 if (success_happened
)
388 return MEMCACHED_SUCCESS
;
390 return MEMCACHED_FAILURE
; // Complete failure occurred
393 memcached_return_t
memcached_mget_by_key(memcached_st
*ptr
,
394 const char *group_key
,
395 size_t group_key_length
,
396 const char * const *keys
,
397 const size_t *key_length
,
398 size_t number_of_keys
)
400 return memcached_mget_by_key_real(ptr
, group_key
, group_key_length
, keys
,
401 key_length
, number_of_keys
, true);
404 memcached_return_t
memcached_mget_execute(memcached_st
*ptr
,
405 const char * const *keys
,
406 const size_t *key_length
,
407 size_t number_of_keys
,
408 memcached_execute_fn
*callback
,
410 unsigned int number_of_callbacks
)
412 return memcached_mget_execute_by_key(ptr
, NULL
, 0, keys
, key_length
,
413 number_of_keys
, callback
,
414 context
, number_of_callbacks
);
417 memcached_return_t
memcached_mget_execute_by_key(memcached_st
*ptr
,
418 const char *group_key
,
419 size_t group_key_length
,
420 const char * const *keys
,
421 const size_t *key_length
,
422 size_t number_of_keys
,
423 memcached_execute_fn
*callback
,
425 unsigned int number_of_callbacks
)
427 memcached_return_t rc
;
428 if (memcached_failed(rc
= initialize_query(ptr
, false)))
433 if (memcached_is_udp(ptr
))
435 return memcached_set_error(*ptr
, MEMCACHED_NOT_SUPPORTED
, MEMCACHED_AT
);
438 if (memcached_is_binary(ptr
) == false)
440 return memcached_set_error(*ptr
, MEMCACHED_NOT_SUPPORTED
, MEMCACHED_AT
,
441 memcached_literal_param("ASCII protocol is not supported for memcached_mget_execute_by_key()"));
444 memcached_callback_st
*original_callbacks
= ptr
->callbacks
;
445 memcached_callback_st cb
= {
452 rc
= memcached_mget_by_key(ptr
, group_key
, group_key_length
, keys
,
453 key_length
, number_of_keys
);
454 ptr
->callbacks
= original_callbacks
;
458 static memcached_return_t
simple_binary_mget(memcached_st
*ptr
,
459 uint32_t master_server_key
,
460 bool is_group_key_set
,
461 const char * const *keys
,
462 const size_t *key_length
,
463 size_t number_of_keys
, bool mget_mode
)
465 memcached_return_t rc
= MEMCACHED_NOTFOUND
;
467 bool flush
= (number_of_keys
== 1);
470 If a server fails we warn about errors and start all over with sending keys
473 for (uint32_t x
= 0; x
< number_of_keys
; ++x
)
477 if (is_group_key_set
)
479 server_key
= master_server_key
;
483 server_key
= memcached_generate_hash_with_redistribution(ptr
, keys
[x
], key_length
[x
]);
486 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, server_key
);
488 if (memcached_server_response_count(instance
) == 0)
490 rc
= memcached_connect(instance
);
491 if (memcached_failed(rc
))
497 protocol_binary_request_getk request
= { }; //= {.bytes= {0}};
498 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
501 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_GETKQ
;
505 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_GETK
;
508 memcached_return_t vk
;
509 vk
= memcached_validate_key_length(key_length
[x
],
510 ptr
->flags
.binary_protocol
);
511 if (vk
!= MEMCACHED_SUCCESS
)
515 memcached_io_reset(instance
);
521 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
[x
] + memcached_array_size(ptr
->_namespace
)));
522 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
523 request
.message
.header
.request
.bodylen
= htonl((uint32_t)( key_length
[x
] + memcached_array_size(ptr
->_namespace
)));
525 libmemcached_io_vector_st vector
[]=
527 { request
.bytes
, sizeof(request
.bytes
) },
528 { memcached_array_string(ptr
->_namespace
), memcached_array_size(ptr
->_namespace
) },
529 { keys
[x
], key_length
[x
] }
532 if (memcached_io_writev(instance
, vector
, 3, flush
) == -1)
534 memcached_server_response_reset(instance
);
535 rc
= MEMCACHED_SOME_ERRORS
;
539 /* We just want one pending response per server */
540 memcached_server_response_reset(instance
);
541 memcached_server_response_increment(instance
);
542 if ((x
> 0 && x
== ptr
->io_key_prefetch
) && memcached_flush_buffers(ptr
) != MEMCACHED_SUCCESS
)
544 rc
= MEMCACHED_SOME_ERRORS
;
551 Send a noop command to flush the buffers
553 protocol_binary_request_noop request
= {}; //= {.bytes= {0}};
554 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
555 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_NOOP
;
556 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
558 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); ++x
)
560 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, x
);
562 if (memcached_server_response_count(instance
))
564 if (memcached_io_write(instance
) == false)
566 memcached_server_response_reset(instance
);
567 memcached_io_reset(instance
);
568 rc
= MEMCACHED_SOME_ERRORS
;
571 if (memcached_io_write(instance
, request
.bytes
,
572 sizeof(request
.bytes
), true) == -1)
574 memcached_server_response_reset(instance
);
575 memcached_io_reset(instance
);
576 rc
= MEMCACHED_SOME_ERRORS
;
586 static memcached_return_t
replication_binary_mget(memcached_st
*ptr
,
589 const char *const *keys
,
590 const size_t *key_length
,
591 size_t number_of_keys
)
593 memcached_return_t rc
= MEMCACHED_NOTFOUND
;
595 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);
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
&& ((server
+ start
) <= (hash
[x
] + ptr
->number_of_replicas
)))
616 while (server
>= memcached_server_count(ptr
))
618 server
-= memcached_server_count(ptr
);
621 if (dead_servers
[server
])
626 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, server
);
628 if (memcached_server_response_count(instance
) == 0)
630 rc
= memcached_connect(instance
);
632 if (memcached_failed(rc
))
634 memcached_io_reset(instance
);
635 dead_servers
[server
]= true;
641 protocol_binary_request_getk request
= {};
642 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
643 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_GETK
;
644 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
[x
] + memcached_array_size(ptr
->_namespace
)));
645 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
646 request
.message
.header
.request
.bodylen
= htonl((uint32_t)(key_length
[x
] + memcached_array_size(ptr
->_namespace
)));
649 * We need to disable buffering to actually know that the request was
650 * successfully sent to the server (so that we should expect a result
651 * back). It would be nice to do this in buffered mode, but then it
652 * would be complex to handle all error situations if we got to send
653 * some of the messages, and then we failed on writing out some others
654 * and we used the callback interface from memcached_mget_execute so
655 * that we might have processed some of the responses etc. For now,
656 * just make sure we work _correctly_
658 libmemcached_io_vector_st vector
[]=
660 { request
.bytes
, sizeof(request
.bytes
) },
661 { memcached_array_string(ptr
->_namespace
), memcached_array_size(ptr
->_namespace
) },
662 { keys
[x
], key_length
[x
] }
665 if (memcached_io_writev(instance
, vector
, 3, true) == -1)
667 memcached_io_reset(instance
);
668 dead_servers
[server
]= true;
673 memcached_server_response_increment(instance
);
674 hash
[x
]= memcached_server_count(ptr
);
686 static memcached_return_t
binary_mget_by_key(memcached_st
*ptr
,
687 uint32_t master_server_key
,
688 bool is_group_key_set
,
689 const char * const *keys
,
690 const size_t *key_length
,
691 size_t number_of_keys
,
694 if (ptr
->number_of_replicas
== 0)
696 return simple_binary_mget(ptr
, master_server_key
, is_group_key_set
,
697 keys
, key_length
, number_of_keys
, mget_mode
);
700 uint32_t* hash
= libmemcached_xvalloc(ptr
, number_of_keys
, uint32_t);
701 bool* dead_servers
= libmemcached_xcalloc(ptr
, memcached_server_count(ptr
), bool);
703 if (hash
== NULL
|| dead_servers
== NULL
)
705 libmemcached_free(ptr
, hash
);
706 libmemcached_free(ptr
, dead_servers
);
707 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
710 if (is_group_key_set
)
712 for (size_t x
= 0; x
< number_of_keys
; x
++)
714 hash
[x
]= master_server_key
;
719 for (size_t x
= 0; x
< number_of_keys
; x
++)
721 hash
[x
]= memcached_generate_hash_with_redistribution(ptr
, keys
[x
], key_length
[x
]);
725 memcached_return_t rc
= replication_binary_mget(ptr
, hash
, dead_servers
, keys
,
726 key_length
, number_of_keys
);
728 WATCHPOINT_IFERROR(rc
);
729 libmemcached_free(ptr
, hash
);
730 libmemcached_free(ptr
, dead_servers
);
732 return MEMCACHED_SUCCESS
;