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.");
174 memcached_return_t
memcached_mget(memcached_st
*ptr
,
175 const char * const *keys
,
176 const size_t *key_length
,
177 size_t number_of_keys
)
179 return memcached_mget_by_key(ptr
, NULL
, 0, keys
, key_length
, number_of_keys
);
182 static memcached_return_t
binary_mget_by_key(memcached_st
*ptr
,
183 uint32_t master_server_key
,
184 bool is_group_key_set
,
185 const char * const *keys
,
186 const size_t *key_length
,
187 size_t number_of_keys
,
190 static memcached_return_t
memcached_mget_by_key_real(memcached_st
*ptr
,
191 const char *group_key
,
192 size_t group_key_length
,
193 const char * const *keys
,
194 const size_t *key_length
,
195 size_t number_of_keys
,
198 bool failures_occured_in_sending
= false;
199 const char *get_command
= "get ";
200 uint8_t get_command_length
= 4;
201 unsigned int master_server_key
= (unsigned int)-1; /* 0 is a valid server id! */
203 memcached_return_t rc
;
204 if (memcached_failed(rc
= initialize_query(ptr
, true)))
209 if (memcached_is_udp(ptr
))
211 return memcached_set_error(*ptr
, MEMCACHED_NOT_SUPPORTED
, MEMCACHED_AT
);
214 LIBMEMCACHED_MEMCACHED_MGET_START();
216 if (number_of_keys
== 0)
218 return memcached_set_error(*ptr
, MEMCACHED_NOTFOUND
, MEMCACHED_AT
, memcached_literal_param("number_of_keys was zero"));
221 if (memcached_failed(memcached_key_test(*ptr
, keys
, key_length
, number_of_keys
)))
223 return memcached_last_error(ptr
);
226 bool is_group_key_set
= false;
227 if (group_key
and group_key_length
)
229 master_server_key
= memcached_generate_hash_with_redistribution(ptr
, group_key
, group_key_length
);
230 is_group_key_set
= true;
234 Here is where we pay for the non-block API. We need to remove any data sitting
235 in the queue before we start our get.
237 It might be optimum to bounce the connection if count > some number.
239 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
241 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, x
);
243 if (memcached_server_response_count(instance
))
245 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
247 if (ptr
->flags
.no_block
)
249 memcached_io_write(instance
);
252 while(memcached_server_response_count(instance
))
254 (void)memcached_response(instance
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, &ptr
->result
);
259 if (memcached_is_binary(ptr
))
261 return binary_mget_by_key(ptr
, master_server_key
, is_group_key_set
, keys
,
262 key_length
, number_of_keys
, mget_mode
);
265 if (ptr
->flags
.support_cas
)
267 get_command
= "gets ";
268 get_command_length
= 5;
272 If a server fails we warn about errors and start all over with sending keys
275 WATCHPOINT_ASSERT(rc
== MEMCACHED_SUCCESS
);
276 size_t hosts_connected
= 0;
277 for (uint32_t x
= 0; x
< number_of_keys
; x
++)
279 memcached_server_write_instance_st instance
;
282 if (is_group_key_set
)
284 server_key
= master_server_key
;
288 server_key
= memcached_generate_hash_with_redistribution(ptr
, keys
[x
], key_length
[x
]);
291 instance
= memcached_server_instance_fetch(ptr
, server_key
);
293 libmemcached_io_vector_st vector
[]=
295 { get_command
, get_command_length
},
296 { memcached_array_string(ptr
->_namespace
), memcached_array_size(ptr
->_namespace
) },
297 { keys
[x
], key_length
[x
] },
298 { memcached_literal_param(" ") }
302 if (memcached_server_response_count(instance
) == 0)
304 rc
= memcached_connect(instance
);
306 if (memcached_failed(rc
))
308 memcached_set_error(*instance
, rc
, MEMCACHED_AT
);
313 if ((memcached_io_writev(instance
, vector
, 4, false)) == -1)
315 failures_occured_in_sending
= true;
318 WATCHPOINT_ASSERT(instance
->cursor_active
== 0);
319 memcached_server_response_increment(instance
);
320 WATCHPOINT_ASSERT(instance
->cursor_active
== 1);
324 if ((memcached_io_writev(instance
, (vector
+ 1), 3, false)) == -1)
326 memcached_server_response_reset(instance
);
327 failures_occured_in_sending
= true;
333 if (hosts_connected
== 0)
335 LIBMEMCACHED_MEMCACHED_MGET_END();
337 if (memcached_failed(rc
))
342 return memcached_set_error(*ptr
, MEMCACHED_NO_SERVERS
, MEMCACHED_AT
);
347 Should we muddle on if some servers are dead?
349 bool success_happened
= false;
350 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
352 memcached_server_write_instance_st instance
=
353 memcached_server_instance_fetch(ptr
, x
);
355 if (memcached_server_response_count(instance
))
357 /* We need to do something about non-connnected hosts in the future */
358 if ((memcached_io_write(instance
, "\r\n", 2, true)) == -1)
360 failures_occured_in_sending
= true;
364 success_happened
= true;
369 LIBMEMCACHED_MEMCACHED_MGET_END();
371 if (failures_occured_in_sending
&& success_happened
)
373 return MEMCACHED_SOME_ERRORS
;
376 if (success_happened
)
377 return MEMCACHED_SUCCESS
;
379 return MEMCACHED_FAILURE
; // Complete failure occurred
382 memcached_return_t
memcached_mget_by_key(memcached_st
*ptr
,
383 const char *group_key
,
384 size_t group_key_length
,
385 const char * const *keys
,
386 const size_t *key_length
,
387 size_t number_of_keys
)
389 return memcached_mget_by_key_real(ptr
, group_key
, group_key_length
, keys
,
390 key_length
, number_of_keys
, true);
393 memcached_return_t
memcached_mget_execute(memcached_st
*ptr
,
394 const char * const *keys
,
395 const size_t *key_length
,
396 size_t number_of_keys
,
397 memcached_execute_fn
*callback
,
399 unsigned int number_of_callbacks
)
401 return memcached_mget_execute_by_key(ptr
, NULL
, 0, keys
, key_length
,
402 number_of_keys
, callback
,
403 context
, number_of_callbacks
);
406 memcached_return_t
memcached_mget_execute_by_key(memcached_st
*ptr
,
407 const char *group_key
,
408 size_t group_key_length
,
409 const char * const *keys
,
410 const size_t *key_length
,
411 size_t number_of_keys
,
412 memcached_execute_fn
*callback
,
414 unsigned int number_of_callbacks
)
416 memcached_return_t rc
;
417 if (memcached_failed(rc
= initialize_query(ptr
, false)))
422 if (memcached_is_udp(ptr
))
424 return memcached_set_error(*ptr
, MEMCACHED_NOT_SUPPORTED
, MEMCACHED_AT
);
427 if (memcached_is_binary(ptr
) == false)
429 return memcached_set_error(*ptr
, MEMCACHED_NOT_SUPPORTED
, MEMCACHED_AT
,
430 memcached_literal_param("ASCII protocol is not supported for memcached_mget_execute_by_key()"));
433 memcached_callback_st
*original_callbacks
= ptr
->callbacks
;
434 memcached_callback_st cb
= {
441 rc
= memcached_mget_by_key(ptr
, group_key
, group_key_length
, keys
,
442 key_length
, number_of_keys
);
443 ptr
->callbacks
= original_callbacks
;
447 static memcached_return_t
simple_binary_mget(memcached_st
*ptr
,
448 uint32_t master_server_key
,
449 bool is_group_key_set
,
450 const char * const *keys
,
451 const size_t *key_length
,
452 size_t number_of_keys
, bool mget_mode
)
454 memcached_return_t rc
= MEMCACHED_NOTFOUND
;
456 bool flush
= (number_of_keys
== 1);
459 If a server fails we warn about errors and start all over with sending keys
462 for (uint32_t x
= 0; x
< number_of_keys
; ++x
)
466 if (is_group_key_set
)
468 server_key
= master_server_key
;
472 server_key
= memcached_generate_hash_with_redistribution(ptr
, keys
[x
], key_length
[x
]);
475 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, server_key
);
477 if (memcached_server_response_count(instance
) == 0)
479 rc
= memcached_connect(instance
);
480 if (memcached_failed(rc
))
486 protocol_binary_request_getk request
= { }; //= {.bytes= {0}};
487 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
490 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_GETKQ
;
494 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_GETK
;
497 memcached_return_t vk
;
498 vk
= memcached_validate_key_length(key_length
[x
],
499 ptr
->flags
.binary_protocol
);
500 if (vk
!= MEMCACHED_SUCCESS
)
504 memcached_io_reset(instance
);
510 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
[x
] + memcached_array_size(ptr
->_namespace
)));
511 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
512 request
.message
.header
.request
.bodylen
= htonl((uint32_t)( key_length
[x
] + memcached_array_size(ptr
->_namespace
)));
514 libmemcached_io_vector_st vector
[]=
516 { request
.bytes
, sizeof(request
.bytes
) },
517 { memcached_array_string(ptr
->_namespace
), memcached_array_size(ptr
->_namespace
) },
518 { keys
[x
], key_length
[x
] }
521 if (memcached_io_writev(instance
, vector
, 3, flush
) == -1)
523 memcached_server_response_reset(instance
);
524 rc
= MEMCACHED_SOME_ERRORS
;
528 /* We just want one pending response per server */
529 memcached_server_response_reset(instance
);
530 memcached_server_response_increment(instance
);
531 if ((x
> 0 && x
== ptr
->io_key_prefetch
) && memcached_flush_buffers(ptr
) != MEMCACHED_SUCCESS
)
533 rc
= MEMCACHED_SOME_ERRORS
;
540 Send a noop command to flush the buffers
542 protocol_binary_request_noop request
= {}; //= {.bytes= {0}};
543 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
544 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_NOOP
;
545 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
547 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); ++x
)
549 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, x
);
551 if (memcached_server_response_count(instance
))
553 if (memcached_io_write(instance
) == false)
555 memcached_server_response_reset(instance
);
556 memcached_io_reset(instance
);
557 rc
= MEMCACHED_SOME_ERRORS
;
560 if (memcached_io_write(instance
, request
.bytes
,
561 sizeof(request
.bytes
), true) == -1)
563 memcached_server_response_reset(instance
);
564 memcached_io_reset(instance
);
565 rc
= MEMCACHED_SOME_ERRORS
;
575 static memcached_return_t
replication_binary_mget(memcached_st
*ptr
,
578 const char *const *keys
,
579 const size_t *key_length
,
580 size_t number_of_keys
)
582 memcached_return_t rc
= MEMCACHED_NOTFOUND
;
584 uint64_t randomize_read
= memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ
);
587 start
= (uint32_t)random() % (uint32_t)(ptr
->number_of_replicas
+ 1);
589 /* Loop for each replica */
590 for (uint32_t replica
= 0; replica
<= ptr
->number_of_replicas
; ++replica
)
594 for (uint32_t x
= 0; x
< number_of_keys
; ++x
)
596 if (hash
[x
] == memcached_server_count(ptr
))
597 continue; /* Already successfully sent */
599 uint32_t server
= hash
[x
] + replica
;
601 /* In case of randomized reads */
602 if (randomize_read
&& ((server
+ start
) <= (hash
[x
] + ptr
->number_of_replicas
)))
605 while (server
>= memcached_server_count(ptr
))
607 server
-= memcached_server_count(ptr
);
610 if (dead_servers
[server
])
615 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, server
);
617 if (memcached_server_response_count(instance
) == 0)
619 rc
= memcached_connect(instance
);
621 if (memcached_failed(rc
))
623 memcached_io_reset(instance
);
624 dead_servers
[server
]= true;
630 protocol_binary_request_getk request
= {};
631 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
632 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_GETK
;
633 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
[x
] + memcached_array_size(ptr
->_namespace
)));
634 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
635 request
.message
.header
.request
.bodylen
= htonl((uint32_t)(key_length
[x
] + memcached_array_size(ptr
->_namespace
)));
638 * We need to disable buffering to actually know that the request was
639 * successfully sent to the server (so that we should expect a result
640 * back). It would be nice to do this in buffered mode, but then it
641 * would be complex to handle all error situations if we got to send
642 * some of the messages, and then we failed on writing out some others
643 * and we used the callback interface from memcached_mget_execute so
644 * that we might have processed some of the responses etc. For now,
645 * just make sure we work _correctly_
647 libmemcached_io_vector_st vector
[]=
649 { request
.bytes
, sizeof(request
.bytes
) },
650 { memcached_array_string(ptr
->_namespace
), memcached_array_size(ptr
->_namespace
) },
651 { keys
[x
], key_length
[x
] }
654 if (memcached_io_writev(instance
, vector
, 3, true) == -1)
656 memcached_io_reset(instance
);
657 dead_servers
[server
]= true;
662 memcached_server_response_increment(instance
);
663 hash
[x
]= memcached_server_count(ptr
);
675 static memcached_return_t
binary_mget_by_key(memcached_st
*ptr
,
676 uint32_t master_server_key
,
677 bool is_group_key_set
,
678 const char * const *keys
,
679 const size_t *key_length
,
680 size_t number_of_keys
,
683 if (ptr
->number_of_replicas
== 0)
685 return simple_binary_mget(ptr
, master_server_key
, is_group_key_set
,
686 keys
, key_length
, number_of_keys
, mget_mode
);
689 uint32_t* hash
= libmemcached_xvalloc(ptr
, number_of_keys
, uint32_t);
690 bool* dead_servers
= libmemcached_xcalloc(ptr
, memcached_server_count(ptr
), bool);
692 if (hash
== NULL
|| dead_servers
== NULL
)
694 libmemcached_free(ptr
, hash
);
695 libmemcached_free(ptr
, dead_servers
);
696 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
699 if (is_group_key_set
)
701 for (size_t x
= 0; x
< number_of_keys
; x
++)
703 hash
[x
]= master_server_key
;
708 for (size_t x
= 0; x
< number_of_keys
; x
++)
710 hash
[x
]= memcached_generate_hash_with_redistribution(ptr
, keys
[x
], key_length
[x
]);
714 memcached_return_t rc
= replication_binary_mget(ptr
, hash
, dead_servers
, keys
,
715 key_length
, number_of_keys
);
717 WATCHPOINT_IFERROR(rc
);
718 libmemcached_free(ptr
, hash
);
719 libmemcached_free(ptr
, dead_servers
);
721 return MEMCACHED_SUCCESS
;