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
*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
and *error
== MEMCACHED_NOTFOUND
)
122 memcached_result_st key_failure_result
;
123 memcached_result_st
* result_ptr
= memcached_result_create(ptr
, &key_failure_result
);
124 memcached_return_t rc
= ptr
->get_key_failure(ptr
, key
, key_length
, result_ptr
);
126 /* On all failure drop to returning NULL */
127 if (rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
)
129 if (rc
== MEMCACHED_BUFFERED
)
131 uint64_t latch
; /* We use latch to track the state of the original socket */
132 latch
= memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
);
135 memcached_behavior_set(ptr
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
138 rc
= memcached_set(ptr
, key
, key_length
,
139 (memcached_result_value(result_ptr
)),
140 (memcached_result_length(result_ptr
)),
142 (memcached_result_flags(result_ptr
)));
144 if (rc
== MEMCACHED_BUFFERED
and latch
== 0)
146 memcached_behavior_set(ptr
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 0);
151 rc
= memcached_set(ptr
, key
, key_length
,
152 (memcached_result_value(result_ptr
)),
153 (memcached_result_length(result_ptr
)),
155 (memcached_result_flags(result_ptr
)));
158 if (rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
)
161 *value_length
= memcached_result_length(result_ptr
);
162 *flags
= memcached_result_flags(result_ptr
);
163 char *result_value
= memcached_string_take_value(&result_ptr
->value
);
164 memcached_result_free(result_ptr
);
170 memcached_result_free(result_ptr
);
172 assert_msg(ptr
->query_id
== query_id
+1, "Programmer error, the query_id was not incremented.");
180 memcached_return_t
memcached_mget(memcached_st
*ptr
,
181 const char * const *keys
,
182 const size_t *key_length
,
183 size_t number_of_keys
)
185 return memcached_mget_by_key(ptr
, NULL
, 0, keys
, key_length
, number_of_keys
);
188 static memcached_return_t
binary_mget_by_key(memcached_st
*ptr
,
189 uint32_t master_server_key
,
190 bool is_group_key_set
,
191 const char * const *keys
,
192 const size_t *key_length
,
193 size_t number_of_keys
,
196 static memcached_return_t
memcached_mget_by_key_real(memcached_st
*ptr
,
197 const char *group_key
,
198 size_t group_key_length
,
199 const char * const *keys
,
200 const size_t *key_length
,
201 size_t number_of_keys
,
204 bool failures_occured_in_sending
= false;
205 const char *get_command
= "get";
206 uint8_t get_command_length
= 3;
207 unsigned int master_server_key
= (unsigned int)-1; /* 0 is a valid server id! */
209 memcached_return_t rc
;
210 if (memcached_failed(rc
= initialize_query(ptr
, true)))
215 if (memcached_is_udp(ptr
))
217 return memcached_set_error(*ptr
, MEMCACHED_NOT_SUPPORTED
, MEMCACHED_AT
);
220 LIBMEMCACHED_MEMCACHED_MGET_START();
222 if (number_of_keys
== 0)
224 return memcached_set_error(*ptr
, MEMCACHED_NOTFOUND
, MEMCACHED_AT
, memcached_literal_param("number_of_keys was zero"));
227 if (memcached_failed(memcached_key_test(*ptr
, keys
, key_length
, number_of_keys
)))
229 return memcached_last_error(ptr
);
232 bool is_group_key_set
= false;
233 if (group_key
and group_key_length
)
235 master_server_key
= memcached_generate_hash_with_redistribution(ptr
, group_key
, group_key_length
);
236 is_group_key_set
= true;
240 Here is where we pay for the non-block API. We need to remove any data sitting
241 in the queue before we start our get.
243 It might be optimum to bounce the connection if count > some number.
245 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
247 org::libmemcached::Instance
* instance
= memcached_instance_fetch(ptr
, x
);
249 if (memcached_instance_response_count(instance
))
251 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
253 if (ptr
->flags
.no_block
)
255 memcached_io_write(instance
);
258 while(memcached_instance_response_count(instance
))
260 (void)memcached_response(instance
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, &ptr
->result
);
265 if (memcached_is_binary(ptr
))
267 return binary_mget_by_key(ptr
, master_server_key
, is_group_key_set
, keys
,
268 key_length
, number_of_keys
, mget_mode
);
271 if (ptr
->flags
.support_cas
)
274 get_command_length
= 4;
278 If a server fails we warn about errors and start all over with sending keys
281 WATCHPOINT_ASSERT(rc
== MEMCACHED_SUCCESS
);
282 size_t hosts_connected
= 0;
283 for (uint32_t x
= 0; x
< number_of_keys
; x
++)
287 if (is_group_key_set
)
289 server_key
= master_server_key
;
293 server_key
= memcached_generate_hash_with_redistribution(ptr
, keys
[x
], key_length
[x
]);
296 org::libmemcached::Instance
* instance
= memcached_instance_fetch(ptr
, server_key
);
298 libmemcached_io_vector_st vector
[]=
300 { get_command
, get_command_length
},
301 { memcached_literal_param(" ") },
302 { memcached_array_string(ptr
->_namespace
), memcached_array_size(ptr
->_namespace
) },
303 { keys
[x
], key_length
[x
] }
307 if (memcached_instance_response_count(instance
) == 0)
309 rc
= memcached_connect(instance
);
311 if (memcached_failed(rc
))
313 memcached_set_error(*instance
, rc
, MEMCACHED_AT
);
318 if ((memcached_io_writev(instance
, vector
, 4, false)) == false)
320 failures_occured_in_sending
= true;
323 WATCHPOINT_ASSERT(instance
->cursor_active_
== 0);
324 memcached_instance_response_increment(instance
);
325 WATCHPOINT_ASSERT(instance
->cursor_active_
== 1);
329 if ((memcached_io_writev(instance
, (vector
+ 1), 3, false)) == false)
331 memcached_instance_response_reset(instance
);
332 failures_occured_in_sending
= true;
338 if (hosts_connected
== 0)
340 LIBMEMCACHED_MEMCACHED_MGET_END();
342 if (memcached_failed(rc
))
347 return memcached_set_error(*ptr
, MEMCACHED_NO_SERVERS
, MEMCACHED_AT
);
352 Should we muddle on if some servers are dead?
354 bool success_happened
= false;
355 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
357 org::libmemcached::Instance
* instance
= memcached_instance_fetch(ptr
, x
);
359 if (memcached_instance_response_count(instance
))
361 /* We need to do something about non-connnected hosts in the future */
362 if ((memcached_io_write(instance
, "\r\n", 2, true)) == -1)
364 failures_occured_in_sending
= true;
368 success_happened
= true;
373 LIBMEMCACHED_MEMCACHED_MGET_END();
375 if (failures_occured_in_sending
and success_happened
)
377 return MEMCACHED_SOME_ERRORS
;
380 if (success_happened
)
382 return MEMCACHED_SUCCESS
;
385 return MEMCACHED_FAILURE
; // Complete failure occurred
388 memcached_return_t
memcached_mget_by_key(memcached_st
*ptr
,
389 const char *group_key
,
390 size_t group_key_length
,
391 const char * const *keys
,
392 const size_t *key_length
,
393 size_t number_of_keys
)
395 return memcached_mget_by_key_real(ptr
, group_key
, group_key_length
, keys
,
396 key_length
, number_of_keys
, true);
399 memcached_return_t
memcached_mget_execute(memcached_st
*ptr
,
400 const char * const *keys
,
401 const size_t *key_length
,
402 size_t number_of_keys
,
403 memcached_execute_fn
*callback
,
405 unsigned int number_of_callbacks
)
407 return memcached_mget_execute_by_key(ptr
, NULL
, 0, keys
, key_length
,
408 number_of_keys
, callback
,
409 context
, number_of_callbacks
);
412 memcached_return_t
memcached_mget_execute_by_key(memcached_st
*ptr
,
413 const char *group_key
,
414 size_t group_key_length
,
415 const char * const *keys
,
416 const size_t *key_length
,
417 size_t number_of_keys
,
418 memcached_execute_fn
*callback
,
420 unsigned int number_of_callbacks
)
422 memcached_return_t rc
;
423 if (memcached_failed(rc
= initialize_query(ptr
, false)))
428 if (memcached_is_udp(ptr
))
430 return memcached_set_error(*ptr
, MEMCACHED_NOT_SUPPORTED
, MEMCACHED_AT
);
433 if (memcached_is_binary(ptr
) == false)
435 return memcached_set_error(*ptr
, MEMCACHED_NOT_SUPPORTED
, MEMCACHED_AT
,
436 memcached_literal_param("ASCII protocol is not supported for memcached_mget_execute_by_key()"));
439 memcached_callback_st
*original_callbacks
= ptr
->callbacks
;
440 memcached_callback_st cb
= {
447 rc
= memcached_mget_by_key(ptr
, group_key
, group_key_length
, keys
,
448 key_length
, number_of_keys
);
449 ptr
->callbacks
= original_callbacks
;
453 static memcached_return_t
simple_binary_mget(memcached_st
*ptr
,
454 uint32_t master_server_key
,
455 bool is_group_key_set
,
456 const char * const *keys
,
457 const size_t *key_length
,
458 size_t number_of_keys
, bool mget_mode
)
460 memcached_return_t rc
= MEMCACHED_NOTFOUND
;
462 bool flush
= (number_of_keys
== 1);
465 If a server fails we warn about errors and start all over with sending keys
468 for (uint32_t x
= 0; x
< number_of_keys
; ++x
)
472 if (is_group_key_set
)
474 server_key
= master_server_key
;
478 server_key
= memcached_generate_hash_with_redistribution(ptr
, keys
[x
], key_length
[x
]);
481 org::libmemcached::Instance
* instance
= memcached_instance_fetch(ptr
, server_key
);
483 if (memcached_instance_response_count(instance
) == 0)
485 rc
= memcached_connect(instance
);
486 if (memcached_failed(rc
))
492 protocol_binary_request_getk request
= { }; //= {.bytes= {0}};
493 initialize_binary_request(instance
, request
.message
.header
);
496 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_GETKQ
;
500 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_GETK
;
503 memcached_return_t vk
;
504 vk
= memcached_validate_key_length(key_length
[x
],
505 ptr
->flags
.binary_protocol
);
506 if (vk
!= MEMCACHED_SUCCESS
)
510 memcached_io_reset(instance
);
516 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
[x
] + memcached_array_size(ptr
->_namespace
)));
517 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
518 request
.message
.header
.request
.bodylen
= htonl((uint32_t)( key_length
[x
] + memcached_array_size(ptr
->_namespace
)));
520 libmemcached_io_vector_st vector
[]=
522 { request
.bytes
, sizeof(request
.bytes
) },
523 { memcached_array_string(ptr
->_namespace
), memcached_array_size(ptr
->_namespace
) },
524 { keys
[x
], key_length
[x
] }
527 if (memcached_io_writev(instance
, vector
, 3, flush
) == false)
529 memcached_server_response_reset(instance
);
530 rc
= MEMCACHED_SOME_ERRORS
;
534 /* We just want one pending response per server */
535 memcached_server_response_reset(instance
);
536 memcached_server_response_increment(instance
);
537 if ((x
> 0 and x
== ptr
->io_key_prefetch
) and memcached_flush_buffers(ptr
) != MEMCACHED_SUCCESS
)
539 rc
= MEMCACHED_SOME_ERRORS
;
546 Send a noop command to flush the buffers
548 protocol_binary_request_noop request
= {}; //= {.bytes= {0}};
549 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_NOOP
;
550 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
552 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); ++x
)
554 org::libmemcached::Instance
* instance
= memcached_instance_fetch(ptr
, x
);
555 initialize_binary_request(instance
, request
.message
.header
);
557 if (memcached_instance_response_count(instance
))
559 if (memcached_io_write(instance
) == false)
561 memcached_instance_response_reset(instance
);
562 memcached_io_reset(instance
);
563 rc
= MEMCACHED_SOME_ERRORS
;
566 if (memcached_io_write(instance
, request
.bytes
,
567 sizeof(request
.bytes
), true) == -1)
569 memcached_instance_response_reset(instance
);
570 memcached_io_reset(instance
);
571 rc
= MEMCACHED_SOME_ERRORS
;
581 static memcached_return_t
replication_binary_mget(memcached_st
*ptr
,
584 const char *const *keys
,
585 const size_t *key_length
,
586 size_t number_of_keys
)
588 memcached_return_t rc
= MEMCACHED_NOTFOUND
;
590 uint64_t randomize_read
= memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ
);
594 start
= (uint32_t)random() % (uint32_t)(ptr
->number_of_replicas
+ 1);
597 /* Loop for each replica */
598 for (uint32_t replica
= 0; replica
<= ptr
->number_of_replicas
; ++replica
)
602 for (uint32_t x
= 0; x
< number_of_keys
; ++x
)
604 if (hash
[x
] == memcached_server_count(ptr
))
605 continue; /* Already successfully sent */
607 uint32_t server
= hash
[x
] + replica
;
609 /* In case of randomized reads */
610 if (randomize_read
and ((server
+ start
) <= (hash
[x
] + ptr
->number_of_replicas
)))
615 while (server
>= memcached_server_count(ptr
))
617 server
-= memcached_server_count(ptr
);
620 if (dead_servers
[server
])
625 org::libmemcached::Instance
* instance
= memcached_instance_fetch(ptr
, server
);
627 if (memcached_instance_response_count(instance
) == 0)
629 rc
= memcached_connect(instance
);
631 if (memcached_failed(rc
))
633 memcached_io_reset(instance
);
634 dead_servers
[server
]= true;
640 protocol_binary_request_getk request
= {};
641 initialize_binary_request(instance
, request
.message
.header
);
642 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_GETK
;
643 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
[x
] + memcached_array_size(ptr
->_namespace
)));
644 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
645 request
.message
.header
.request
.bodylen
= htonl((uint32_t)(key_length
[x
] + memcached_array_size(ptr
->_namespace
)));
648 * We need to disable buffering to actually know that the request was
649 * successfully sent to the server (so that we should expect a result
650 * back). It would be nice to do this in buffered mode, but then it
651 * would be complex to handle all error situations if we got to send
652 * some of the messages, and then we failed on writing out some others
653 * and we used the callback interface from memcached_mget_execute so
654 * that we might have processed some of the responses etc. For now,
655 * just make sure we work _correctly_
657 libmemcached_io_vector_st vector
[]=
659 { request
.bytes
, sizeof(request
.bytes
) },
660 { memcached_array_string(ptr
->_namespace
), memcached_array_size(ptr
->_namespace
) },
661 { keys
[x
], key_length
[x
] }
664 if (memcached_io_writev(instance
, vector
, 3, true) == false)
666 memcached_io_reset(instance
);
667 dead_servers
[server
]= true;
672 memcached_server_response_increment(instance
);
673 hash
[x
]= memcached_server_count(ptr
);
685 static memcached_return_t
binary_mget_by_key(memcached_st
*ptr
,
686 uint32_t master_server_key
,
687 bool is_group_key_set
,
688 const char * const *keys
,
689 const size_t *key_length
,
690 size_t number_of_keys
,
693 if (ptr
->number_of_replicas
== 0)
695 return simple_binary_mget(ptr
, master_server_key
, is_group_key_set
,
696 keys
, key_length
, number_of_keys
, mget_mode
);
699 uint32_t* hash
= libmemcached_xvalloc(ptr
, number_of_keys
, uint32_t);
700 bool* dead_servers
= libmemcached_xcalloc(ptr
, memcached_server_count(ptr
), bool);
702 if (hash
== NULL
or dead_servers
== NULL
)
704 libmemcached_free(ptr
, hash
);
705 libmemcached_free(ptr
, dead_servers
);
706 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
709 if (is_group_key_set
)
711 for (size_t x
= 0; x
< number_of_keys
; x
++)
713 hash
[x
]= master_server_key
;
718 for (size_t x
= 0; x
< number_of_keys
; x
++)
720 hash
[x
]= memcached_generate_hash_with_redistribution(ptr
, keys
[x
], key_length
[x
]);
724 memcached_return_t rc
= replication_binary_mget(ptr
, hash
, dead_servers
, keys
,
725 key_length
, number_of_keys
);
727 WATCHPOINT_IFERROR(rc
);
728 libmemcached_free(ptr
, hash
);
729 libmemcached_free(ptr
, dead_servers
);
731 return MEMCACHED_SUCCESS
;