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
),
87 0, memcached_result_flags(&ptr
->result
));
89 if (rc
== MEMCACHED_BUFFERED
&& latch
== 0)
90 memcached_behavior_set(ptr
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 0);
94 rc
= memcached_set(ptr
, key
, key_length
,
95 memcached_result_value(&ptr
->result
),
96 memcached_result_length(&ptr
->result
),
97 0, memcached_result_flags(&ptr
->result
));
100 if (rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
)
103 *value_length
= memcached_result_length(&ptr
->result
);
104 *flags
= memcached_result_flags(&ptr
->result
);
105 return memcached_string_c_copy(&ptr
->result
.value
);
113 (void)memcached_fetch(ptr
, NULL
, NULL
,
114 &dummy_length
, &dummy_flags
,
116 WATCHPOINT_ASSERT(dummy_length
== 0);
121 memcached_return_t
memcached_mget(memcached_st
*ptr
,
122 const char * const *keys
,
123 const size_t *key_length
,
124 size_t number_of_keys
)
126 return memcached_mget_by_key(ptr
, NULL
, 0, keys
, key_length
, number_of_keys
);
129 static memcached_return_t
binary_mget_by_key(memcached_st
*ptr
,
130 unsigned int master_server_key
,
131 bool is_master_key_set
,
132 const char * const *keys
,
133 const size_t *key_length
,
134 size_t number_of_keys
,
137 static memcached_return_t
memcached_mget_by_key_real(memcached_st
*ptr
,
138 const char *master_key
,
139 size_t master_key_length
,
140 const char * const *keys
,
141 const size_t *key_length
,
142 size_t number_of_keys
,
146 memcached_return_t rc
= MEMCACHED_NOTFOUND
;
147 const char *get_command
= "get ";
148 uint8_t get_command_length
= 4;
149 unsigned int master_server_key
= (unsigned int)-1; /* 0 is a valid server id! */
150 bool is_master_key_set
= false;
152 unlikely (ptr
->flags
.use_udp
)
153 return MEMCACHED_NOT_SUPPORTED
;
155 LIBMEMCACHED_MEMCACHED_MGET_START();
156 ptr
->cursor_server
= 0;
158 if (number_of_keys
== 0)
159 return MEMCACHED_NOTFOUND
;
161 if (ptr
->number_of_hosts
== 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(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 (x
= 0; x
< ptr
->number_of_hosts
; x
++)
183 if (memcached_server_response_count(&ptr
->hosts
[x
]))
185 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
187 if (ptr
->flags
.no_block
)
188 (void)memcached_io_write(&ptr
->hosts
[x
], NULL
, 0, 1);
190 while(memcached_server_response_count(&ptr
->hosts
[x
]))
191 (void)memcached_response(&ptr
->hosts
[x
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, &ptr
->result
);
195 if (ptr
->flags
.binary_protocol
)
196 return binary_mget_by_key(ptr
, master_server_key
, is_master_key_set
, keys
,
197 key_length
, number_of_keys
, mget_mode
);
199 if (ptr
->flags
.support_cas
)
201 get_command
= "gets ";
202 get_command_length
= 5;
206 If a server fails we warn about errors and start all over with sending keys
209 for (x
= 0; x
< number_of_keys
; x
++)
211 unsigned int server_key
;
213 if (is_master_key_set
)
214 server_key
= master_server_key
;
216 server_key
= memcached_generate_hash(ptr
, keys
[x
], key_length
[x
]);
218 if (memcached_server_response_count(&ptr
->hosts
[server_key
]) == 0)
220 rc
= memcached_connect(&ptr
->hosts
[server_key
]);
222 if (rc
!= MEMCACHED_SUCCESS
)
225 if ((memcached_io_write(&ptr
->hosts
[server_key
], get_command
, get_command_length
, 0)) == -1)
227 rc
= MEMCACHED_SOME_ERRORS
;
230 WATCHPOINT_ASSERT(ptr
->hosts
[server_key
].cursor_active
== 0);
231 memcached_server_response_increment(&ptr
->hosts
[server_key
]);
232 WATCHPOINT_ASSERT(ptr
->hosts
[server_key
].cursor_active
== 1);
235 /* Only called when we have a prefix key */
236 if (ptr
->prefix_key
[0] != 0)
238 if ((memcached_io_write(&ptr
->hosts
[server_key
], ptr
->prefix_key
, ptr
->prefix_key_length
, 0)) == -1)
240 memcached_server_response_reset(&ptr
->hosts
[server_key
]);
241 rc
= MEMCACHED_SOME_ERRORS
;
246 if ((memcached_io_write(&ptr
->hosts
[server_key
], keys
[x
], key_length
[x
], 0)) == -1)
248 memcached_server_response_reset(&ptr
->hosts
[server_key
]);
249 rc
= MEMCACHED_SOME_ERRORS
;
253 if ((memcached_io_write(&ptr
->hosts
[server_key
], " ", 1, 0)) == -1)
255 memcached_server_response_reset(&ptr
->hosts
[server_key
]);
256 rc
= MEMCACHED_SOME_ERRORS
;
262 Should we muddle on if some servers are dead?
264 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
266 if (memcached_server_response_count(&ptr
->hosts
[x
]))
268 /* We need to do something about non-connnected hosts in the future */
269 if ((memcached_io_write(&ptr
->hosts
[x
], "\r\n", 2, 1)) == -1)
271 rc
= MEMCACHED_SOME_ERRORS
;
276 LIBMEMCACHED_MEMCACHED_MGET_END();
280 memcached_return_t
memcached_mget_by_key(memcached_st
*ptr
,
281 const char *master_key
,
282 size_t master_key_length
,
283 const char * const *keys
,
284 const size_t *key_length
,
285 size_t number_of_keys
)
287 return memcached_mget_by_key_real(ptr
, master_key
, master_key_length
, keys
,
288 key_length
, number_of_keys
, true);
291 memcached_return_t
memcached_mget_execute(memcached_st
*ptr
,
292 const char * const *keys
,
293 const size_t *key_length
,
294 size_t number_of_keys
,
295 memcached_execute_fn
*callback
,
297 unsigned int number_of_callbacks
)
299 return memcached_mget_execute_by_key(ptr
, NULL
, 0, keys
, key_length
,
300 number_of_keys
, callback
,
301 context
, number_of_callbacks
);
304 memcached_return_t
memcached_mget_execute_by_key(memcached_st
*ptr
,
305 const char *master_key
,
306 size_t master_key_length
,
307 const char * const *keys
,
308 const size_t *key_length
,
309 size_t number_of_keys
,
310 memcached_execute_fn
*callback
,
312 unsigned int number_of_callbacks
)
314 if ((ptr
->flags
.binary_protocol
) == 0)
315 return MEMCACHED_NOT_SUPPORTED
;
317 memcached_return_t rc
;
318 memcached_callback_st
*original_callbacks
= ptr
->callbacks
;
319 memcached_callback_st cb
= {
322 .number_of_callback
= number_of_callbacks
326 rc
= memcached_mget_by_key(ptr
, master_key
, master_key_length
, keys
,
327 key_length
, number_of_keys
);
328 ptr
->callbacks
= original_callbacks
;
332 static memcached_return_t
simple_binary_mget(memcached_st
*ptr
,
333 unsigned int master_server_key
,
334 bool is_master_key_set
,
335 const char * const *keys
,
336 const size_t *key_length
,
337 size_t number_of_keys
, bool mget_mode
)
339 memcached_return_t rc
= MEMCACHED_NOTFOUND
;
342 int flush
= number_of_keys
== 1;
345 If a server fails we warn about errors and start all over with sending keys
348 for (x
= 0; x
< number_of_keys
; x
++)
350 unsigned int server_key
;
352 if (is_master_key_set
)
353 server_key
= master_server_key
;
355 server_key
= memcached_generate_hash(ptr
, keys
[x
], key_length
[x
]);
357 if (memcached_server_response_count(&ptr
->hosts
[server_key
]) == 0)
359 rc
= memcached_connect(&ptr
->hosts
[server_key
]);
360 if (rc
!= MEMCACHED_SUCCESS
)
364 protocol_binary_request_getk request
= {.bytes
= {0}};
365 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
367 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_GETKQ
;
369 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_GETK
;
371 memcached_return_t vk
;
372 vk
= memcached_validate_key_length(key_length
[x
],
373 ptr
->flags
.binary_protocol
);
374 unlikely (vk
!= MEMCACHED_SUCCESS
)
377 memcached_io_reset(&ptr
->hosts
[server_key
]);
381 request
.message
.header
.request
.keylen
= htons((uint16_t)key_length
[x
]);
382 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
383 request
.message
.header
.request
.bodylen
= htonl((uint32_t) key_length
[x
]);
385 if ((memcached_io_write(&ptr
->hosts
[server_key
], request
.bytes
,
386 sizeof(request
.bytes
), 0) == -1) ||
387 (memcached_io_write(&ptr
->hosts
[server_key
], keys
[x
],
388 key_length
[x
], (char) flush
) == -1))
390 memcached_server_response_reset(&ptr
->hosts
[server_key
]);
391 rc
= MEMCACHED_SOME_ERRORS
;
395 /* We just want one pending response per server */
396 memcached_server_response_reset(&ptr
->hosts
[server_key
]);
397 memcached_server_response_increment(&ptr
->hosts
[server_key
]);
398 if ((x
> 0 && x
== ptr
->io_key_prefetch
) &&
399 memcached_flush_buffers(ptr
) != MEMCACHED_SUCCESS
)
400 rc
= MEMCACHED_SOME_ERRORS
;
406 * Send a noop command to flush the buffers
408 protocol_binary_request_noop request
= {.bytes
= {0}};
409 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
410 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_NOOP
;
411 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
413 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
414 if (memcached_server_response_count(&ptr
->hosts
[x
]))
416 if (memcached_io_write(&ptr
->hosts
[x
], NULL
, 0, 1) == -1)
418 memcached_server_response_reset(&ptr
->hosts
[x
]);
419 memcached_io_reset(&ptr
->hosts
[x
]);
420 rc
= MEMCACHED_SOME_ERRORS
;
423 if (memcached_io_write(&ptr
->hosts
[x
], request
.bytes
,
424 sizeof(request
.bytes
), 1) == -1)
426 memcached_server_response_reset(&ptr
->hosts
[x
]);
427 memcached_io_reset(&ptr
->hosts
[x
]);
428 rc
= MEMCACHED_SOME_ERRORS
;
437 static memcached_return_t
replication_binary_mget(memcached_st
*ptr
,
440 const char *const *keys
,
441 const size_t *key_length
,
442 size_t number_of_keys
)
444 memcached_return_t rc
= MEMCACHED_NOTFOUND
;
445 uint32_t x
, start
= 0;
446 uint64_t randomize_read
= memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ
);
449 start
= (uint32_t)random() % (uint32_t)(ptr
->number_of_replicas
+ 1);
451 /* Loop for each replica */
452 for (uint32_t replica
= 0; replica
<= ptr
->number_of_replicas
; ++replica
)
456 for (x
= 0; x
< number_of_keys
; ++x
)
458 if (hash
[x
] == ptr
->number_of_hosts
)
459 continue; /* Already successfully sent */
461 uint32_t server
= hash
[x
] + replica
;
463 /* In case of randomized reads */
464 if (randomize_read
&& ((server
+ start
) <= (hash
[x
] + ptr
->number_of_replicas
)))
467 while (server
>= ptr
->number_of_hosts
)
468 server
-= ptr
->number_of_hosts
;
470 if (dead_servers
[server
])
473 if (memcached_server_response_count(&ptr
->hosts
[server
]) == 0)
475 rc
= memcached_connect(&ptr
->hosts
[server
]);
476 if (rc
!= MEMCACHED_SUCCESS
)
478 memcached_io_reset(&ptr
->hosts
[server
]);
479 dead_servers
[server
]= true;
485 protocol_binary_request_getk request
= {
486 .message
.header
.request
= {
487 .magic
= PROTOCOL_BINARY_REQ
,
488 .opcode
= PROTOCOL_BINARY_CMD_GETK
,
489 .keylen
= htons((uint16_t)key_length
[x
]),
490 .datatype
= PROTOCOL_BINARY_RAW_BYTES
,
491 .bodylen
= htonl((uint32_t)key_length
[x
])
496 * We need to disable buffering to actually know that the request was
497 * successfully sent to the server (so that we should expect a result
498 * back). It would be nice to do this in buffered mode, but then it
499 * would be complex to handle all error situations if we got to send
500 * some of the messages, and then we failed on writing out some others
501 * and we used the callback interface from memcached_mget_execute so
502 * that we might have processed some of the responses etc. For now,
503 * just make sure we work _correctly_
505 if ((memcached_io_write(&ptr
->hosts
[server
], request
.bytes
,
506 sizeof(request
.bytes
), 0) == -1) ||
507 (memcached_io_write(&ptr
->hosts
[server
], keys
[x
],
508 key_length
[x
], 1) == -1))
510 memcached_io_reset(&ptr
->hosts
[server
]);
511 dead_servers
[server
]= true;
516 memcached_server_response_increment(&ptr
->hosts
[server
]);
517 hash
[x
]= ptr
->number_of_hosts
;
527 static memcached_return_t
binary_mget_by_key(memcached_st
*ptr
,
528 unsigned int master_server_key
,
529 bool is_master_key_set
,
530 const char * const *keys
,
531 const size_t *key_length
,
532 size_t number_of_keys
,
535 memcached_return_t rc
;
537 if (ptr
->number_of_replicas
== 0)
539 rc
= simple_binary_mget(ptr
, master_server_key
, is_master_key_set
,
540 keys
, key_length
, number_of_keys
, mget_mode
);
547 hash
= ptr
->call_malloc(ptr
, sizeof(uint32_t) * number_of_keys
);
548 dead_servers
= ptr
->call_calloc(ptr
, ptr
->number_of_hosts
, sizeof(bool));
550 if (hash
== NULL
|| dead_servers
== NULL
)
552 ptr
->call_free(ptr
, hash
);
553 ptr
->call_free(ptr
, dead_servers
);
554 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
557 if (is_master_key_set
)
558 for (unsigned int x
= 0; x
< number_of_keys
; x
++)
559 hash
[x
]= master_server_key
;
561 for (unsigned int x
= 0; x
< number_of_keys
; x
++)
562 hash
[x
]= memcached_generate_hash(ptr
, keys
[x
], key_length
[x
]);
564 rc
= replication_binary_mget(ptr
, hash
, dead_servers
, keys
,
565 key_length
, number_of_keys
);
567 ptr
->call_free(ptr
, hash
);
568 ptr
->call_free(ptr
, dead_servers
);
570 return MEMCACHED_SUCCESS
;