2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
8 * Summary: Get functions for libmemcached
15 What happens if no servers exist?
17 char *memcached_get(memcached_st
*ptr
, const char *key
,
21 memcached_return_t
*error
)
23 return memcached_get_by_key(ptr
, NULL
, 0, key
, key_length
, value_length
,
27 static memcached_return_t
memcached_mget_by_key_real(memcached_st
*ptr
,
28 const char *master_key
,
29 size_t master_key_length
,
30 const char * const *keys
,
31 const size_t *key_length
,
32 size_t number_of_keys
,
35 char *memcached_get_by_key(memcached_st
*ptr
,
36 const char *master_key
,
37 size_t master_key_length
,
38 const char *key
, size_t key_length
,
41 memcached_return_t
*error
)
46 memcached_return_t dummy_error
;
48 unlikely (ptr
->flags
.use_udp
)
50 *error
= MEMCACHED_NOT_SUPPORTED
;
55 *error
= memcached_mget_by_key_real(ptr
, master_key
, master_key_length
,
56 (const char * const *)&key
,
57 &key_length
, 1, false);
59 value
= memcached_fetch(ptr
, NULL
, NULL
,
60 value_length
, flags
, error
);
61 /* This is for historical reasons */
62 if (*error
== MEMCACHED_END
)
63 *error
= MEMCACHED_NOTFOUND
;
67 if (ptr
->get_key_failure
&& *error
== MEMCACHED_NOTFOUND
)
69 memcached_return_t rc
;
71 memcached_result_reset(&ptr
->result
);
72 rc
= ptr
->get_key_failure(ptr
, key
, key_length
, &ptr
->result
);
74 /* On all failure drop to returning NULL */
75 if (rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
)
77 if (rc
== MEMCACHED_BUFFERED
)
79 uint64_t latch
; /* We use latch to track the state of the original socket */
80 latch
= memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
);
82 memcached_behavior_set(ptr
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
84 rc
= memcached_set(ptr
, key
, key_length
,
85 (memcached_result_value(&ptr
->result
)),
86 (memcached_result_length(&ptr
->result
)),
88 (memcached_result_flags(&ptr
->result
)));
90 if (rc
== MEMCACHED_BUFFERED
&& latch
== 0)
91 memcached_behavior_set(ptr
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 0);
95 rc
= memcached_set(ptr
, key
, key_length
,
96 (memcached_result_value(&ptr
->result
)),
97 (memcached_result_length(&ptr
->result
)),
99 (memcached_result_flags(&ptr
->result
)));
102 if (rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
)
105 *value_length
= memcached_result_length(&ptr
->result
);
106 *flags
= memcached_result_flags(&ptr
->result
);
107 return memcached_string_c_copy(&ptr
->result
.value
);
115 (void)memcached_fetch(ptr
, NULL
, NULL
,
116 &dummy_length
, &dummy_flags
,
118 WATCHPOINT_ASSERT(dummy_length
== 0);
123 memcached_return_t
memcached_mget(memcached_st
*ptr
,
124 const char * const *keys
,
125 const size_t *key_length
,
126 size_t number_of_keys
)
128 return memcached_mget_by_key(ptr
, NULL
, 0, keys
, key_length
, number_of_keys
);
131 static memcached_return_t
binary_mget_by_key(memcached_st
*ptr
,
132 uint32_t master_server_key
,
133 bool is_master_key_set
,
134 const char * const *keys
,
135 const size_t *key_length
,
136 size_t number_of_keys
,
139 static memcached_return_t
memcached_mget_by_key_real(memcached_st
*ptr
,
140 const char *master_key
,
141 size_t master_key_length
,
142 const char * const *keys
,
143 const size_t *key_length
,
144 size_t number_of_keys
,
147 memcached_return_t rc
= MEMCACHED_NOTFOUND
;
148 const char *get_command
= "get ";
149 uint8_t get_command_length
= 4;
150 unsigned int master_server_key
= (unsigned int)-1; /* 0 is a valid server id! */
151 bool is_master_key_set
= false;
153 unlikely (ptr
->flags
.use_udp
)
154 return MEMCACHED_NOT_SUPPORTED
;
156 LIBMEMCACHED_MEMCACHED_MGET_START();
158 if (number_of_keys
== 0)
159 return MEMCACHED_NOTFOUND
;
161 if (memcached_server_count(ptr
) == 0)
162 return MEMCACHED_NO_SERVERS
;
164 if (ptr
->flags
.verify_key
&& (memcached_key_test(keys
, key_length
, number_of_keys
) == MEMCACHED_BAD_KEY_PROVIDED
))
165 return MEMCACHED_BAD_KEY_PROVIDED
;
167 if (master_key
&& master_key_length
)
169 if (ptr
->flags
.verify_key
&& (memcached_key_test((const char * const *)&master_key
, &master_key_length
, 1) == MEMCACHED_BAD_KEY_PROVIDED
))
170 return MEMCACHED_BAD_KEY_PROVIDED
;
171 master_server_key
= memcached_generate_hash_with_redistribution(ptr
, master_key
, master_key_length
);
172 is_master_key_set
= true;
176 Here is where we pay for the non-block API. We need to remove any data sitting
177 in the queue before we start our get.
179 It might be optimum to bounce the connection if count > some number.
181 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
183 memcached_server_write_instance_st instance
=
184 memcached_server_instance_fetch(ptr
, x
);
186 if (memcached_server_response_count(instance
))
188 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
190 if (ptr
->flags
.no_block
)
191 (void)memcached_io_write(instance
, NULL
, 0, true);
193 while(memcached_server_response_count(instance
))
194 (void)memcached_response(instance
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, &ptr
->result
);
198 if (ptr
->flags
.binary_protocol
)
200 return binary_mget_by_key(ptr
, master_server_key
, is_master_key_set
, keys
,
201 key_length
, number_of_keys
, mget_mode
);
204 if (ptr
->flags
.support_cas
)
206 get_command
= "gets ";
207 get_command_length
= 5;
211 If a server fails we warn about errors and start all over with sending keys
214 for (uint32_t x
= 0; x
< number_of_keys
; x
++)
216 memcached_server_write_instance_st instance
;
219 if (is_master_key_set
)
221 server_key
= master_server_key
;
225 server_key
= memcached_generate_hash_with_redistribution(ptr
, keys
[x
], key_length
[x
]);
228 instance
= memcached_server_instance_fetch(ptr
, server_key
);
230 struct __write_vector_st vector
[]=
232 { .length
= get_command_length
, .buffer
= get_command
},
233 { .length
= ptr
->prefix_key_length
, .buffer
= ptr
->prefix_key
},
234 { .length
= key_length
[x
], .buffer
= keys
[x
] },
235 { .length
= 1, .buffer
= " " }
239 if (memcached_server_response_count(instance
) == 0)
241 rc
= memcached_connect(instance
);
243 if (rc
!= MEMCACHED_SUCCESS
)
246 if ((memcached_io_writev(instance
, vector
, 4, false)) == -1)
248 rc
= MEMCACHED_SOME_ERRORS
;
251 WATCHPOINT_ASSERT(instance
->cursor_active
== 0);
252 memcached_server_response_increment(instance
);
253 WATCHPOINT_ASSERT(instance
->cursor_active
== 1);
257 if ((memcached_io_writev(instance
, (vector
+ 1), 3, false)) == -1)
259 memcached_server_response_reset(instance
);
260 rc
= MEMCACHED_SOME_ERRORS
;
267 Should we muddle on if some servers are dead?
269 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
271 memcached_server_write_instance_st instance
=
272 memcached_server_instance_fetch(ptr
, x
);
274 if (memcached_server_response_count(instance
))
276 /* We need to do something about non-connnected hosts in the future */
277 if ((memcached_io_write(instance
, "\r\n", 2, true)) == -1)
279 rc
= MEMCACHED_SOME_ERRORS
;
284 LIBMEMCACHED_MEMCACHED_MGET_END();
288 memcached_return_t
memcached_mget_by_key(memcached_st
*ptr
,
289 const char *master_key
,
290 size_t master_key_length
,
291 const char * const *keys
,
292 const size_t *key_length
,
293 size_t number_of_keys
)
295 return memcached_mget_by_key_real(ptr
, master_key
, master_key_length
, keys
,
296 key_length
, number_of_keys
, true);
299 memcached_return_t
memcached_mget_execute(memcached_st
*ptr
,
300 const char * const *keys
,
301 const size_t *key_length
,
302 size_t number_of_keys
,
303 memcached_execute_fn
*callback
,
305 unsigned int number_of_callbacks
)
307 return memcached_mget_execute_by_key(ptr
, NULL
, 0, keys
, key_length
,
308 number_of_keys
, callback
,
309 context
, number_of_callbacks
);
312 memcached_return_t
memcached_mget_execute_by_key(memcached_st
*ptr
,
313 const char *master_key
,
314 size_t master_key_length
,
315 const char * const *keys
,
316 const size_t *key_length
,
317 size_t number_of_keys
,
318 memcached_execute_fn
*callback
,
320 unsigned int number_of_callbacks
)
322 if ((ptr
->flags
.binary_protocol
) == 0)
323 return MEMCACHED_NOT_SUPPORTED
;
325 memcached_return_t rc
;
326 memcached_callback_st
*original_callbacks
= ptr
->callbacks
;
327 memcached_callback_st cb
= {
330 .number_of_callback
= number_of_callbacks
334 rc
= memcached_mget_by_key(ptr
, master_key
, master_key_length
, keys
,
335 key_length
, number_of_keys
);
336 ptr
->callbacks
= original_callbacks
;
340 static memcached_return_t
simple_binary_mget(memcached_st
*ptr
,
341 uint32_t master_server_key
,
342 bool is_master_key_set
,
343 const char * const *keys
,
344 const size_t *key_length
,
345 size_t number_of_keys
, bool mget_mode
)
347 memcached_return_t rc
= MEMCACHED_NOTFOUND
;
349 int flush
= number_of_keys
== 1;
352 If a server fails we warn about errors and start all over with sending keys
355 for (uint32_t x
= 0; x
< number_of_keys
; ++x
)
358 memcached_server_write_instance_st instance
;
360 if (is_master_key_set
)
362 server_key
= master_server_key
;
366 server_key
= memcached_generate_hash_with_redistribution(ptr
, keys
[x
], key_length
[x
]);
369 instance
= memcached_server_instance_fetch(ptr
, server_key
);
371 if (memcached_server_response_count(instance
) == 0)
373 rc
= memcached_connect(instance
);
374 if (rc
!= MEMCACHED_SUCCESS
)
378 protocol_binary_request_getk request
= {.bytes
= {0}};
379 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
381 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_GETKQ
;
383 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_GETK
;
385 memcached_return_t vk
;
386 vk
= memcached_validate_key_length(key_length
[x
],
387 ptr
->flags
.binary_protocol
);
388 unlikely (vk
!= MEMCACHED_SUCCESS
)
392 memcached_io_reset(instance
);
398 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
[x
] + ptr
->prefix_key_length
));
399 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
400 request
.message
.header
.request
.bodylen
= htonl((uint32_t)( key_length
[x
] + ptr
->prefix_key_length
));
402 struct __write_vector_st vector
[]=
404 { .length
= sizeof(request
.bytes
), .buffer
= request
.bytes
},
405 { .length
= ptr
->prefix_key_length
, .buffer
= ptr
->prefix_key
},
406 { .length
= key_length
[x
], .buffer
= keys
[x
] }
409 if (memcached_io_writev(instance
, vector
, 3, flush
) == -1)
411 memcached_server_response_reset(instance
);
412 rc
= MEMCACHED_SOME_ERRORS
;
416 /* We just want one pending response per server */
417 memcached_server_response_reset(instance
);
418 memcached_server_response_increment(instance
);
419 if ((x
> 0 && x
== ptr
->io_key_prefetch
) && memcached_flush_buffers(ptr
) != MEMCACHED_SUCCESS
)
421 rc
= MEMCACHED_SOME_ERRORS
;
428 * Send a noop command to flush the buffers
430 protocol_binary_request_noop request
= {.bytes
= {0}};
431 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
432 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_NOOP
;
433 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
435 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); ++x
)
437 memcached_server_write_instance_st instance
=
438 memcached_server_instance_fetch(ptr
, x
);
440 if (memcached_server_response_count(instance
))
442 if (memcached_io_write(instance
, NULL
, 0, true) == -1)
444 memcached_server_response_reset(instance
);
445 memcached_io_reset(instance
);
446 rc
= MEMCACHED_SOME_ERRORS
;
449 if (memcached_io_write(instance
, request
.bytes
,
450 sizeof(request
.bytes
), true) == -1)
452 memcached_server_response_reset(instance
);
453 memcached_io_reset(instance
);
454 rc
= MEMCACHED_SOME_ERRORS
;
464 static memcached_return_t
replication_binary_mget(memcached_st
*ptr
,
467 const char *const *keys
,
468 const size_t *key_length
,
469 size_t number_of_keys
)
471 memcached_return_t rc
= MEMCACHED_NOTFOUND
;
473 uint64_t randomize_read
= memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ
);
476 start
= (uint32_t)random() % (uint32_t)(ptr
->number_of_replicas
+ 1);
478 /* Loop for each replica */
479 for (uint32_t replica
= 0; replica
<= ptr
->number_of_replicas
; ++replica
)
483 for (uint32_t x
= 0; x
< number_of_keys
; ++x
)
485 memcached_server_write_instance_st instance
;
487 if (hash
[x
] == memcached_server_count(ptr
))
488 continue; /* Already successfully sent */
490 uint32_t server
= hash
[x
] + replica
;
492 /* In case of randomized reads */
493 if (randomize_read
&& ((server
+ start
) <= (hash
[x
] + ptr
->number_of_replicas
)))
496 while (server
>= memcached_server_count(ptr
))
497 server
-= memcached_server_count(ptr
);
499 if (dead_servers
[server
])
502 instance
= memcached_server_instance_fetch(ptr
, server
);
504 if (memcached_server_response_count(instance
) == 0)
506 rc
= memcached_connect(instance
);
507 if (rc
!= MEMCACHED_SUCCESS
)
509 memcached_io_reset(instance
);
510 dead_servers
[server
]= true;
516 protocol_binary_request_getk request
= {
517 .message
.header
.request
= {
518 .magic
= PROTOCOL_BINARY_REQ
,
519 .opcode
= PROTOCOL_BINARY_CMD_GETK
,
520 .keylen
= htons((uint16_t)(key_length
[x
] + ptr
->prefix_key_length
)),
521 .datatype
= PROTOCOL_BINARY_RAW_BYTES
,
522 .bodylen
= htonl((uint32_t)(key_length
[x
] + ptr
->prefix_key_length
))
527 * We need to disable buffering to actually know that the request was
528 * successfully sent to the server (so that we should expect a result
529 * back). It would be nice to do this in buffered mode, but then it
530 * would be complex to handle all error situations if we got to send
531 * some of the messages, and then we failed on writing out some others
532 * and we used the callback interface from memcached_mget_execute so
533 * that we might have processed some of the responses etc. For now,
534 * just make sure we work _correctly_
536 struct __write_vector_st vector
[]=
538 { .length
= sizeof(request
.bytes
), .buffer
= request
.bytes
},
539 { .length
= ptr
->prefix_key_length
, .buffer
= ptr
->prefix_key
},
540 { .length
= key_length
[x
], .buffer
= keys
[x
] }
543 if (memcached_io_writev(instance
, vector
, 3, true) == -1)
545 memcached_io_reset(instance
);
546 dead_servers
[server
]= true;
551 memcached_server_response_increment(instance
);
552 hash
[x
]= memcached_server_count(ptr
);
562 static memcached_return_t
binary_mget_by_key(memcached_st
*ptr
,
563 uint32_t master_server_key
,
564 bool is_master_key_set
,
565 const char * const *keys
,
566 const size_t *key_length
,
567 size_t number_of_keys
,
570 memcached_return_t rc
;
572 if (ptr
->number_of_replicas
== 0)
574 rc
= simple_binary_mget(ptr
, master_server_key
, is_master_key_set
,
575 keys
, key_length
, number_of_keys
, mget_mode
);
582 hash
= libmemcached_malloc(ptr
, sizeof(uint32_t) * number_of_keys
);
583 dead_servers
= libmemcached_calloc(ptr
, memcached_server_count(ptr
), sizeof(bool));
585 if (hash
== NULL
|| dead_servers
== NULL
)
587 libmemcached_free(ptr
, hash
);
588 libmemcached_free(ptr
, dead_servers
);
589 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
592 if (is_master_key_set
)
594 for (size_t x
= 0; x
< number_of_keys
; x
++)
596 hash
[x
]= master_server_key
;
601 for (size_t x
= 0; x
< number_of_keys
; x
++)
603 hash
[x
]= memcached_generate_hash_with_redistribution(ptr
, keys
[x
], key_length
[x
]);
607 rc
= replication_binary_mget(ptr
, hash
, dead_servers
, keys
,
608 key_length
, number_of_keys
);
610 libmemcached_free(ptr
, hash
);
611 libmemcached_free(ptr
, dead_servers
);
613 return MEMCACHED_SUCCESS
;