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
,
148 memcached_return_t rc
= MEMCACHED_NOTFOUND
;
149 const char *get_command
= "get ";
150 uint8_t get_command_length
= 4;
151 unsigned int master_server_key
= (unsigned int)-1; /* 0 is a valid server id! */
152 bool is_master_key_set
= false;
154 unlikely (ptr
->flags
.use_udp
)
155 return MEMCACHED_NOT_SUPPORTED
;
157 LIBMEMCACHED_MEMCACHED_MGET_START();
159 if (number_of_keys
== 0)
160 return MEMCACHED_NOTFOUND
;
162 if (memcached_server_count(ptr
) == 0)
163 return MEMCACHED_NO_SERVERS
;
165 if (ptr
->flags
.verify_key
&& (memcached_key_test(keys
, key_length
, number_of_keys
) == MEMCACHED_BAD_KEY_PROVIDED
))
166 return MEMCACHED_BAD_KEY_PROVIDED
;
168 if (master_key
&& master_key_length
)
170 if (ptr
->flags
.verify_key
&& (memcached_key_test((const char * const *)&master_key
, &master_key_length
, 1) == MEMCACHED_BAD_KEY_PROVIDED
))
171 return MEMCACHED_BAD_KEY_PROVIDED
;
172 master_server_key
= memcached_generate_hash(ptr
, master_key
, master_key_length
);
173 is_master_key_set
= true;
177 Here is where we pay for the non-block API. We need to remove any data sitting
178 in the queue before we start our get.
180 It might be optimum to bounce the connection if count > some number.
182 for (x
= 0; x
< memcached_server_count(ptr
); x
++)
184 if (memcached_server_response_count(&ptr
->hosts
[x
]))
186 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
188 if (ptr
->flags
.no_block
)
189 (void)memcached_io_write(&ptr
->hosts
[x
], NULL
, 0, 1);
191 while(memcached_server_response_count(&ptr
->hosts
[x
]))
192 (void)memcached_response(&ptr
->hosts
[x
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, &ptr
->result
);
196 if (ptr
->flags
.binary_protocol
)
197 return binary_mget_by_key(ptr
, master_server_key
, is_master_key_set
, keys
,
198 key_length
, number_of_keys
, mget_mode
);
200 if (ptr
->flags
.support_cas
)
202 get_command
= "gets ";
203 get_command_length
= 5;
207 If a server fails we warn about errors and start all over with sending keys
210 for (x
= 0; x
< number_of_keys
; x
++)
212 memcached_server_instance_st
*instance
;
215 if (is_master_key_set
)
217 server_key
= master_server_key
;
221 server_key
= memcached_generate_hash(ptr
, keys
[x
], key_length
[x
]);
224 instance
= memcached_server_instance_fetch(ptr
, server_key
);
226 if (memcached_server_response_count(instance
) == 0)
228 rc
= memcached_connect(instance
);
230 if (rc
!= MEMCACHED_SUCCESS
)
233 if ((memcached_io_write(instance
, get_command
, get_command_length
, 0)) == -1)
235 rc
= MEMCACHED_SOME_ERRORS
;
238 WATCHPOINT_ASSERT(instance
->cursor_active
== 0);
239 memcached_server_response_increment(instance
);
240 WATCHPOINT_ASSERT(instance
->cursor_active
== 1);
243 /* Only called when we have a prefix key */
244 if (ptr
->prefix_key
[0] != 0)
246 if ((memcached_io_write(instance
, ptr
->prefix_key
, ptr
->prefix_key_length
, 0)) == -1)
248 memcached_server_response_reset(instance
);
249 rc
= MEMCACHED_SOME_ERRORS
;
254 if ((memcached_io_write(instance
, keys
[x
], key_length
[x
], 0)) == -1)
256 memcached_server_response_reset(instance
);
257 rc
= MEMCACHED_SOME_ERRORS
;
261 if ((memcached_io_write(instance
, " ", 1, 0)) == -1)
263 memcached_server_response_reset(instance
);
264 rc
= MEMCACHED_SOME_ERRORS
;
270 Should we muddle on if some servers are dead?
272 for (x
= 0; x
< memcached_server_count(ptr
); x
++)
274 if (memcached_server_response_count(&ptr
->hosts
[x
]))
276 /* We need to do something about non-connnected hosts in the future */
277 if ((memcached_io_write(&ptr
->hosts
[x
], "\r\n", 2, 1)) == -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
;
350 int flush
= number_of_keys
== 1;
353 If a server fails we warn about errors and start all over with sending keys
356 for (x
= 0; x
< number_of_keys
; x
++)
359 memcached_server_instance_st
*instance
;
361 if (is_master_key_set
)
363 server_key
= master_server_key
;
367 server_key
= memcached_generate_hash(ptr
, keys
[x
], key_length
[x
]);
370 instance
= memcached_server_instance_fetch(ptr
, server_key
);
372 if (memcached_server_response_count(instance
) == 0)
374 rc
= memcached_connect(instance
);
375 if (rc
!= MEMCACHED_SUCCESS
)
379 protocol_binary_request_getk request
= {.bytes
= {0}};
380 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
382 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_GETKQ
;
384 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_GETK
;
386 memcached_return_t vk
;
387 vk
= memcached_validate_key_length(key_length
[x
],
388 ptr
->flags
.binary_protocol
);
389 unlikely (vk
!= MEMCACHED_SUCCESS
)
393 memcached_io_reset(instance
);
399 request
.message
.header
.request
.keylen
= htons((uint16_t)key_length
[x
]);
400 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
401 request
.message
.header
.request
.bodylen
= htonl((uint32_t) key_length
[x
]);
403 if ((memcached_io_write(instance
, request
.bytes
,
404 sizeof(request
.bytes
), 0) == -1) ||
405 (memcached_io_write(instance
, keys
[x
],
406 key_length
[x
], (char) flush
) == -1))
408 memcached_server_response_reset(instance
);
409 rc
= MEMCACHED_SOME_ERRORS
;
413 /* We just want one pending response per server */
414 memcached_server_response_reset(instance
);
415 memcached_server_response_increment(instance
);
416 if ((x
> 0 && x
== ptr
->io_key_prefetch
) &&
417 memcached_flush_buffers(ptr
) != MEMCACHED_SUCCESS
)
418 rc
= MEMCACHED_SOME_ERRORS
;
424 * Send a noop command to flush the buffers
426 protocol_binary_request_noop request
= {.bytes
= {0}};
427 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
428 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_NOOP
;
429 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
431 for (x
= 0; x
< memcached_server_count(ptr
); x
++)
432 if (memcached_server_response_count(&ptr
->hosts
[x
]))
434 if (memcached_io_write(&ptr
->hosts
[x
], NULL
, 0, 1) == -1)
436 memcached_server_response_reset(&ptr
->hosts
[x
]);
437 memcached_io_reset(&ptr
->hosts
[x
]);
438 rc
= MEMCACHED_SOME_ERRORS
;
441 if (memcached_io_write(&ptr
->hosts
[x
], request
.bytes
,
442 sizeof(request
.bytes
), 1) == -1)
444 memcached_server_response_reset(&ptr
->hosts
[x
]);
445 memcached_io_reset(&ptr
->hosts
[x
]);
446 rc
= MEMCACHED_SOME_ERRORS
;
455 static memcached_return_t
replication_binary_mget(memcached_st
*ptr
,
458 const char *const *keys
,
459 const size_t *key_length
,
460 size_t number_of_keys
)
462 memcached_return_t rc
= MEMCACHED_NOTFOUND
;
463 uint32_t x
, start
= 0;
464 uint64_t randomize_read
= memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ
);
467 start
= (uint32_t)random() % (uint32_t)(ptr
->number_of_replicas
+ 1);
469 /* Loop for each replica */
470 for (uint32_t replica
= 0; replica
<= ptr
->number_of_replicas
; ++replica
)
474 for (x
= 0; x
< number_of_keys
; ++x
)
476 if (hash
[x
] == memcached_server_count(ptr
))
477 continue; /* Already successfully sent */
479 uint32_t server
= hash
[x
] + replica
;
481 /* In case of randomized reads */
482 if (randomize_read
&& ((server
+ start
) <= (hash
[x
] + ptr
->number_of_replicas
)))
485 while (server
>= memcached_server_count(ptr
))
486 server
-= memcached_server_count(ptr
);
488 if (dead_servers
[server
])
491 if (memcached_server_response_count(&ptr
->hosts
[server
]) == 0)
493 rc
= memcached_connect(&ptr
->hosts
[server
]);
494 if (rc
!= MEMCACHED_SUCCESS
)
496 memcached_io_reset(&ptr
->hosts
[server
]);
497 dead_servers
[server
]= true;
503 protocol_binary_request_getk request
= {
504 .message
.header
.request
= {
505 .magic
= PROTOCOL_BINARY_REQ
,
506 .opcode
= PROTOCOL_BINARY_CMD_GETK
,
507 .keylen
= htons((uint16_t)key_length
[x
]),
508 .datatype
= PROTOCOL_BINARY_RAW_BYTES
,
509 .bodylen
= htonl((uint32_t)key_length
[x
])
514 * We need to disable buffering to actually know that the request was
515 * successfully sent to the server (so that we should expect a result
516 * back). It would be nice to do this in buffered mode, but then it
517 * would be complex to handle all error situations if we got to send
518 * some of the messages, and then we failed on writing out some others
519 * and we used the callback interface from memcached_mget_execute so
520 * that we might have processed some of the responses etc. For now,
521 * just make sure we work _correctly_
523 if ((memcached_io_write(&ptr
->hosts
[server
], request
.bytes
,
524 sizeof(request
.bytes
), 0) == -1) ||
525 (memcached_io_write(&ptr
->hosts
[server
], keys
[x
],
526 key_length
[x
], 1) == -1))
528 memcached_io_reset(&ptr
->hosts
[server
]);
529 dead_servers
[server
]= true;
534 memcached_server_response_increment(&ptr
->hosts
[server
]);
535 hash
[x
]= memcached_server_count(ptr
);
545 static memcached_return_t
binary_mget_by_key(memcached_st
*ptr
,
546 uint32_t master_server_key
,
547 bool is_master_key_set
,
548 const char * const *keys
,
549 const size_t *key_length
,
550 size_t number_of_keys
,
553 memcached_return_t rc
;
555 if (ptr
->number_of_replicas
== 0)
557 rc
= simple_binary_mget(ptr
, master_server_key
, is_master_key_set
,
558 keys
, key_length
, number_of_keys
, mget_mode
);
565 hash
= ptr
->call_malloc(ptr
, sizeof(uint32_t) * number_of_keys
);
566 dead_servers
= ptr
->call_calloc(ptr
, memcached_server_count(ptr
), sizeof(bool));
568 if (hash
== NULL
|| dead_servers
== NULL
)
570 ptr
->call_free(ptr
, hash
);
571 ptr
->call_free(ptr
, dead_servers
);
572 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
575 if (is_master_key_set
)
576 for (unsigned int x
= 0; x
< number_of_keys
; x
++)
577 hash
[x
]= master_server_key
;
579 for (unsigned int x
= 0; x
< number_of_keys
; x
++)
580 hash
[x
]= memcached_generate_hash(ptr
, keys
[x
], key_length
[x
]);
582 rc
= replication_binary_mget(ptr
, hash
, dead_servers
, keys
,
583 key_length
, number_of_keys
);
585 ptr
->call_free(ptr
, hash
);
586 ptr
->call_free(ptr
, dead_servers
);
588 return MEMCACHED_SUCCESS
;