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
)
262 (void)memcached_io_write(instance
, NULL
, 0, true);
264 while(memcached_server_response_count(instance
))
265 (void)memcached_response(instance
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, &ptr
->result
);
269 if (ptr
->flags
.binary_protocol
)
271 return binary_mget_by_key(ptr
, master_server_key
, is_group_key_set
, keys
,
272 key_length
, number_of_keys
, mget_mode
);
275 if (ptr
->flags
.support_cas
)
277 get_command
= "gets ";
278 get_command_length
= 5;
282 If a server fails we warn about errors and start all over with sending keys
285 WATCHPOINT_ASSERT(rc
== MEMCACHED_SUCCESS
);
286 size_t hosts_connected
= 0;
287 for (uint32_t x
= 0; x
< number_of_keys
; x
++)
289 memcached_server_write_instance_st instance
;
292 if (is_group_key_set
)
294 server_key
= master_server_key
;
298 server_key
= memcached_generate_hash_with_redistribution(ptr
, keys
[x
], key_length
[x
]);
301 instance
= memcached_server_instance_fetch(ptr
, server_key
);
303 struct libmemcached_io_vector_st vector
[]=
305 { get_command
, get_command_length
},
306 { memcached_array_string(ptr
->_namespace
), memcached_array_size(ptr
->_namespace
) },
307 { keys
[x
], key_length
[x
] },
308 { memcached_literal_param(" ") }
312 if (memcached_server_response_count(instance
) == 0)
314 rc
= memcached_connect(instance
);
316 if (memcached_failed(rc
))
318 memcached_set_error(*instance
, rc
, MEMCACHED_AT
);
323 if ((memcached_io_writev(instance
, vector
, 4, false)) == -1)
325 failures_occured_in_sending
= true;
328 WATCHPOINT_ASSERT(instance
->cursor_active
== 0);
329 memcached_server_response_increment(instance
);
330 WATCHPOINT_ASSERT(instance
->cursor_active
== 1);
334 if ((memcached_io_writev(instance
, (vector
+ 1), 3, false)) == -1)
336 memcached_server_response_reset(instance
);
337 failures_occured_in_sending
= true;
343 if (hosts_connected
== 0)
345 LIBMEMCACHED_MEMCACHED_MGET_END();
347 if (memcached_failed(rc
))
350 return memcached_set_error(*ptr
, MEMCACHED_NO_SERVERS
, MEMCACHED_AT
);
355 Should we muddle on if some servers are dead?
357 bool success_happened
= false;
358 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
360 memcached_server_write_instance_st instance
=
361 memcached_server_instance_fetch(ptr
, x
);
363 if (memcached_server_response_count(instance
))
365 /* We need to do something about non-connnected hosts in the future */
366 if ((memcached_io_write(instance
, "\r\n", 2, true)) == -1)
368 failures_occured_in_sending
= true;
372 success_happened
= true;
377 LIBMEMCACHED_MEMCACHED_MGET_END();
379 if (failures_occured_in_sending
&& success_happened
)
381 return MEMCACHED_SOME_ERRORS
;
384 if (success_happened
)
385 return MEMCACHED_SUCCESS
;
387 return MEMCACHED_FAILURE
; // Complete failure occurred
390 memcached_return_t
memcached_mget_by_key(memcached_st
*ptr
,
391 const char *group_key
,
392 size_t group_key_length
,
393 const char * const *keys
,
394 const size_t *key_length
,
395 size_t number_of_keys
)
397 return memcached_mget_by_key_real(ptr
, group_key
, group_key_length
, keys
,
398 key_length
, number_of_keys
, true);
401 memcached_return_t
memcached_mget_execute(memcached_st
*ptr
,
402 const char * const *keys
,
403 const size_t *key_length
,
404 size_t number_of_keys
,
405 memcached_execute_fn
*callback
,
407 unsigned int number_of_callbacks
)
409 return memcached_mget_execute_by_key(ptr
, NULL
, 0, keys
, key_length
,
410 number_of_keys
, callback
,
411 context
, number_of_callbacks
);
414 memcached_return_t
memcached_mget_execute_by_key(memcached_st
*ptr
,
415 const char *group_key
,
416 size_t group_key_length
,
417 const char * const *keys
,
418 const size_t *key_length
,
419 size_t number_of_keys
,
420 memcached_execute_fn
*callback
,
422 unsigned int number_of_callbacks
)
424 if ((ptr
->flags
.binary_protocol
) == 0)
426 return MEMCACHED_NOT_SUPPORTED
;
429 memcached_return_t rc
;
430 memcached_callback_st
*original_callbacks
= ptr
->callbacks
;
431 memcached_callback_st cb
= {
438 rc
= memcached_mget_by_key(ptr
, group_key
, group_key_length
, keys
,
439 key_length
, number_of_keys
);
440 ptr
->callbacks
= original_callbacks
;
444 static memcached_return_t
simple_binary_mget(memcached_st
*ptr
,
445 uint32_t master_server_key
,
446 bool is_group_key_set
,
447 const char * const *keys
,
448 const size_t *key_length
,
449 size_t number_of_keys
, bool mget_mode
)
451 memcached_return_t rc
= MEMCACHED_NOTFOUND
;
453 bool flush
= (number_of_keys
== 1);
456 If a server fails we warn about errors and start all over with sending keys
459 for (uint32_t x
= 0; x
< number_of_keys
; ++x
)
463 if (is_group_key_set
)
465 server_key
= master_server_key
;
469 server_key
= memcached_generate_hash_with_redistribution(ptr
, keys
[x
], key_length
[x
]);
472 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, server_key
);
474 if (memcached_server_response_count(instance
) == 0)
476 rc
= memcached_connect(instance
);
477 if (memcached_failed(rc
))
483 protocol_binary_request_getk request
= { }; //= {.bytes= {0}};
484 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
486 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_GETKQ
;
488 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_GETK
;
490 memcached_return_t vk
;
491 vk
= memcached_validate_key_length(key_length
[x
],
492 ptr
->flags
.binary_protocol
);
493 unlikely (vk
!= MEMCACHED_SUCCESS
)
497 memcached_io_reset(instance
);
503 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
[x
] + memcached_array_size(ptr
->_namespace
)));
504 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
505 request
.message
.header
.request
.bodylen
= htonl((uint32_t)( key_length
[x
] + memcached_array_size(ptr
->_namespace
)));
507 struct libmemcached_io_vector_st vector
[]=
509 { request
.bytes
, sizeof(request
.bytes
) },
510 { memcached_array_string(ptr
->_namespace
), memcached_array_size(ptr
->_namespace
) },
511 { keys
[x
], key_length
[x
] }
514 if (memcached_io_writev(instance
, vector
, 3, flush
) == -1)
516 memcached_server_response_reset(instance
);
517 rc
= MEMCACHED_SOME_ERRORS
;
521 /* We just want one pending response per server */
522 memcached_server_response_reset(instance
);
523 memcached_server_response_increment(instance
);
524 if ((x
> 0 && x
== ptr
->io_key_prefetch
) && memcached_flush_buffers(ptr
) != MEMCACHED_SUCCESS
)
526 rc
= MEMCACHED_SOME_ERRORS
;
533 Send a noop command to flush the buffers
535 protocol_binary_request_noop request
= {}; //= {.bytes= {0}};
536 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
537 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_NOOP
;
538 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
540 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); ++x
)
542 memcached_server_write_instance_st instance
=
543 memcached_server_instance_fetch(ptr
, x
);
545 if (memcached_server_response_count(instance
))
547 if (memcached_io_write(instance
, NULL
, 0, true) == -1)
549 memcached_server_response_reset(instance
);
550 memcached_io_reset(instance
);
551 rc
= MEMCACHED_SOME_ERRORS
;
554 if (memcached_io_write(instance
, request
.bytes
,
555 sizeof(request
.bytes
), true) == -1)
557 memcached_server_response_reset(instance
);
558 memcached_io_reset(instance
);
559 rc
= MEMCACHED_SOME_ERRORS
;
569 static memcached_return_t
replication_binary_mget(memcached_st
*ptr
,
572 const char *const *keys
,
573 const size_t *key_length
,
574 size_t number_of_keys
)
576 memcached_return_t rc
= MEMCACHED_NOTFOUND
;
578 uint64_t randomize_read
= memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ
);
581 start
= (uint32_t)random() % (uint32_t)(ptr
->number_of_replicas
+ 1);
583 /* Loop for each replica */
584 for (uint32_t replica
= 0; replica
<= ptr
->number_of_replicas
; ++replica
)
588 for (uint32_t x
= 0; x
< number_of_keys
; ++x
)
590 if (hash
[x
] == memcached_server_count(ptr
))
591 continue; /* Already successfully sent */
593 uint32_t server
= hash
[x
] + replica
;
595 /* In case of randomized reads */
596 if (randomize_read
&& ((server
+ start
) <= (hash
[x
] + ptr
->number_of_replicas
)))
599 while (server
>= memcached_server_count(ptr
))
601 server
-= memcached_server_count(ptr
);
604 if (dead_servers
[server
])
609 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, server
);
611 if (memcached_server_response_count(instance
) == 0)
613 rc
= memcached_connect(instance
);
615 if (memcached_failed(rc
))
617 memcached_io_reset(instance
);
618 dead_servers
[server
]= true;
624 protocol_binary_request_getk request
= {};
625 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
626 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_GETK
;
627 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
[x
] + memcached_array_size(ptr
->_namespace
)));
628 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
629 request
.message
.header
.request
.bodylen
= htonl((uint32_t)(key_length
[x
] + memcached_array_size(ptr
->_namespace
)));
632 * We need to disable buffering to actually know that the request was
633 * successfully sent to the server (so that we should expect a result
634 * back). It would be nice to do this in buffered mode, but then it
635 * would be complex to handle all error situations if we got to send
636 * some of the messages, and then we failed on writing out some others
637 * and we used the callback interface from memcached_mget_execute so
638 * that we might have processed some of the responses etc. For now,
639 * just make sure we work _correctly_
641 struct libmemcached_io_vector_st vector
[]=
643 { request
.bytes
, sizeof(request
.bytes
) },
644 { memcached_array_string(ptr
->_namespace
), memcached_array_size(ptr
->_namespace
) },
645 { keys
[x
], key_length
[x
] }
648 if (memcached_io_writev(instance
, vector
, 3, true) == -1)
650 memcached_io_reset(instance
);
651 dead_servers
[server
]= true;
656 memcached_server_response_increment(instance
);
657 hash
[x
]= memcached_server_count(ptr
);
669 static memcached_return_t
binary_mget_by_key(memcached_st
*ptr
,
670 uint32_t master_server_key
,
671 bool is_group_key_set
,
672 const char * const *keys
,
673 const size_t *key_length
,
674 size_t number_of_keys
,
677 if (ptr
->number_of_replicas
== 0)
679 return simple_binary_mget(ptr
, master_server_key
, is_group_key_set
,
680 keys
, key_length
, number_of_keys
, mget_mode
);
683 uint32_t* hash
= libmemcached_xvalloc(ptr
, number_of_keys
, uint32_t);
684 bool* dead_servers
= libmemcached_xcalloc(ptr
, memcached_server_count(ptr
), bool);
686 if (hash
== NULL
|| dead_servers
== NULL
)
688 libmemcached_free(ptr
, hash
);
689 libmemcached_free(ptr
, dead_servers
);
690 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
693 if (is_group_key_set
)
695 for (size_t x
= 0; x
< number_of_keys
; x
++)
697 hash
[x
]= master_server_key
;
702 for (size_t x
= 0; x
< number_of_keys
; x
++)
704 hash
[x
]= memcached_generate_hash_with_redistribution(ptr
, keys
[x
], key_length
[x
]);
708 memcached_return_t rc
= replication_binary_mget(ptr
, hash
, dead_servers
, keys
,
709 key_length
, number_of_keys
);
711 WATCHPOINT_IFERROR(rc
);
712 libmemcached_free(ptr
, hash
);
713 libmemcached_free(ptr
, dead_servers
);
715 return MEMCACHED_SUCCESS
;