Updating for 1.0.2 release
[awesomized/libmemcached] / libmemcached / behavior.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
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
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
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.
35 *
36 */
37
38 #include <libmemcached/common.h>
39 #include <libmemcached/options.hpp>
40 #include <libmemcached/virtual_bucket.h>
41
42 #include <ctime>
43 #include <sys/types.h>
44
45 /*
46 This function is used to modify the behavior of running client.
47
48 We quit all connections so we can reset the sockets.
49 */
50
51 memcached_return_t memcached_behavior_set(memcached_st *ptr,
52 const memcached_behavior_t flag,
53 uint64_t data)
54 {
55 if (not ptr)
56 {
57 return MEMCACHED_INVALID_ARGUMENTS;
58 }
59
60 switch (flag)
61 {
62 case MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS:
63 ptr->number_of_replicas= (uint32_t)data;
64 break;
65
66 case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK:
67 ptr->io_msg_watermark= (uint32_t) data;
68 break;
69
70 case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK:
71 ptr->io_bytes_watermark= (uint32_t)data;
72 break;
73
74 case MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH:
75 ptr->io_key_prefetch = (uint32_t)data;
76 break;
77
78 case MEMCACHED_BEHAVIOR_SND_TIMEOUT:
79 ptr->snd_timeout= (int32_t)data;
80 break;
81
82 case MEMCACHED_BEHAVIOR_RCV_TIMEOUT:
83 ptr->rcv_timeout= (int32_t)data;
84 break;
85
86 case MEMCACHED_BEHAVIOR_REMOVE_FAILED_SERVERS:
87 ptr->flags.auto_eject_hosts= bool(data);
88
89 case MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT:
90 if (data == 0)
91 {
92 return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT,
93 memcached_literal_param("MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT requires a value greater then zero."));
94 }
95 ptr->server_failure_limit= uint32_t(data);
96 break;
97
98 case MEMCACHED_BEHAVIOR_BINARY_PROTOCOL:
99 send_quit(ptr); // We need t shutdown all of the connections to make sure we do the correct protocol
100 if (data)
101 {
102 ptr->flags.verify_key= false;
103 }
104 ptr->flags.binary_protocol= bool(data);
105 break;
106
107 case MEMCACHED_BEHAVIOR_SUPPORT_CAS:
108 ptr->flags.support_cas= bool(data);
109 break;
110
111 case MEMCACHED_BEHAVIOR_NO_BLOCK:
112 ptr->flags.no_block= bool(data);
113 send_quit(ptr);
114 break;
115
116 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS:
117 if (ptr->flags.use_udp)
118 {
119 return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT,
120 memcached_literal_param("MEMCACHED_BEHAVIOR_BUFFER_REQUESTS cannot be set while MEMCACHED_BEHAVIOR_USE_UDP is enabled."));
121 }
122 ptr->flags.buffer_requests= bool(data);
123 send_quit(ptr);
124 break;
125
126 case MEMCACHED_BEHAVIOR_USE_UDP:
127 send_quit(ptr); // We need t shutdown all of the connections to make sure we do the correct protocol
128 ptr->flags.use_udp= bool(data);
129 if (bool(data))
130 {
131 ptr->flags.no_reply= true;
132 ptr->flags.buffer_requests= false;
133 }
134 break;
135
136 case MEMCACHED_BEHAVIOR_TCP_NODELAY:
137 ptr->flags.tcp_nodelay= bool(data);
138 send_quit(ptr);
139 break;
140
141 case MEMCACHED_BEHAVIOR_TCP_KEEPALIVE:
142 ptr->flags.tcp_keepalive= bool(data);
143 send_quit(ptr);
144 break;
145
146 case MEMCACHED_BEHAVIOR_DISTRIBUTION:
147 return memcached_behavior_set_distribution(ptr, (memcached_server_distribution_t)data);
148
149 case MEMCACHED_BEHAVIOR_KETAMA:
150 {
151 if (data) // Turn on
152 return memcached_behavior_set_distribution(ptr, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA);
153
154 return memcached_behavior_set_distribution(ptr, MEMCACHED_DISTRIBUTION_MODULA);
155 }
156
157 case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED:
158 {
159 (void)memcached_behavior_set_key_hash(ptr, MEMCACHED_HASH_MD5);
160 (void)memcached_behavior_set_distribution_hash(ptr, MEMCACHED_HASH_MD5);
161 ptr->ketama.weighted= bool(data);
162 /**
163 @note We try to keep the same distribution going. This should be deprecated and rewritten.
164 */
165 return memcached_behavior_set_distribution(ptr, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA);
166 }
167
168 case MEMCACHED_BEHAVIOR_HASH:
169 return memcached_behavior_set_key_hash(ptr, (memcached_hash_t)(data));
170
171 case MEMCACHED_BEHAVIOR_KETAMA_HASH:
172 return memcached_behavior_set_distribution_hash(ptr, (memcached_hash_t)(data));
173
174 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS:
175 return memcached_set_error(*ptr, MEMCACHED_DEPRECATED, MEMCACHED_AT,
176 memcached_literal_param("MEMCACHED_BEHAVIOR_CACHE_LOOKUPS has been deprecated."));
177
178 case MEMCACHED_BEHAVIOR_VERIFY_KEY:
179 if (ptr->flags.binary_protocol)
180 return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT,
181 memcached_literal_param("MEMCACHED_BEHAVIOR_VERIFY_KEY if the binary protocol has been enabled."));
182 ptr->flags.verify_key= bool(data);
183 break;
184
185 case MEMCACHED_BEHAVIOR_SORT_HOSTS:
186 {
187 ptr->flags.use_sort_hosts= bool(data);
188 run_distribution(ptr);
189
190 break;
191 }
192
193 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT:
194 ptr->poll_timeout= (int32_t)data;
195 break;
196
197 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT:
198 ptr->connect_timeout= (int32_t)data;
199 break;
200
201 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT:
202 if (data == 0)
203 {
204 return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT,
205 memcached_literal_param("MEMCACHED_BEHAVIOR_RETRY_TIMEOUT requires a value greater then zero."));
206 }
207 ptr->retry_timeout= int32_t(data);
208 break;
209
210 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE:
211 ptr->send_size= (int32_t)data;
212 send_quit(ptr);
213 break;
214
215 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE:
216 ptr->recv_size= (int32_t)data;
217 send_quit(ptr);
218 break;
219
220 case MEMCACHED_BEHAVIOR_TCP_KEEPIDLE:
221 ptr->tcp_keepidle= (uint32_t)data;
222 send_quit(ptr);
223 break;
224
225 case MEMCACHED_BEHAVIOR_USER_DATA:
226 return memcached_set_error(*ptr, MEMCACHED_DEPRECATED, MEMCACHED_AT,
227 memcached_literal_param("MEMCACHED_BEHAVIOR_USER_DATA deprecated."));
228
229 case MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY:
230 ptr->flags.hash_with_namespace= bool(data);
231 break;
232
233 case MEMCACHED_BEHAVIOR_NOREPLY:
234 if (ptr->flags.use_udp and bool(data) == false)
235 {
236 return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT,
237 memcached_literal_param("MEMCACHED_BEHAVIOR_NOREPLY cannot be disabled while MEMCACHED_BEHAVIOR_USE_UDP is enabled."));
238 }
239 ptr->flags.no_reply= bool(data);
240 break;
241
242 case MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS:
243 ptr->flags.auto_eject_hosts= bool(data);
244 break;
245
246 case MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ:
247 srandom((uint32_t) time(NULL));
248 ptr->flags.randomize_replica_read= bool(data);
249 break;
250
251 case MEMCACHED_BEHAVIOR_CORK:
252 return memcached_set_error(*ptr, MEMCACHED_DEPRECATED, MEMCACHED_AT,
253 memcached_literal_param("MEMCACHED_BEHAVIOR_CORK is now incorporated into the driver by default."));
254
255 case MEMCACHED_BEHAVIOR_LOAD_FROM_FILE:
256 return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT,
257 memcached_literal_param("MEMCACHED_BEHAVIOR_LOAD_FROM_FILE can not be set with memcached_behavior_set()"));
258
259 case MEMCACHED_BEHAVIOR_MAX:
260 default:
261 /* Shouldn't get here */
262 assert_msg(0, "Invalid behavior passed to memcached_behavior_set()");
263 return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT,
264 memcached_literal_param("Invalid behavior passed to memcached_behavior_set()"));
265 }
266
267 return MEMCACHED_SUCCESS;
268 }
269
270 bool _is_auto_eject_host(const memcached_st *ptr)
271 {
272 return ptr->flags.auto_eject_hosts;
273 }
274
275 uint64_t memcached_behavior_get(memcached_st *ptr,
276 const memcached_behavior_t flag)
277 {
278 if (not ptr)
279 {
280 return MEMCACHED_INVALID_ARGUMENTS;
281 }
282
283 switch (flag)
284 {
285 case MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS:
286 return ptr->number_of_replicas;
287
288 case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK:
289 return ptr->io_msg_watermark;
290
291 case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK:
292 return ptr->io_bytes_watermark;
293
294 case MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH:
295 return ptr->io_key_prefetch;
296
297 case MEMCACHED_BEHAVIOR_BINARY_PROTOCOL:
298 return ptr->flags.binary_protocol;
299
300 case MEMCACHED_BEHAVIOR_SUPPORT_CAS:
301 return ptr->flags.support_cas;
302
303 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS:
304 return true;
305
306 case MEMCACHED_BEHAVIOR_NO_BLOCK:
307 return ptr->flags.no_block;
308
309 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS:
310 return ptr->flags.buffer_requests;
311
312 case MEMCACHED_BEHAVIOR_USE_UDP:
313 return ptr->flags.use_udp;
314
315 case MEMCACHED_BEHAVIOR_TCP_NODELAY:
316 return ptr->flags.tcp_nodelay;
317
318 case MEMCACHED_BEHAVIOR_VERIFY_KEY:
319 return ptr->flags.verify_key;
320
321 case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED:
322 return ptr->ketama.weighted;
323
324 case MEMCACHED_BEHAVIOR_DISTRIBUTION:
325 return ptr->distribution;
326
327 case MEMCACHED_BEHAVIOR_KETAMA:
328 return (ptr->distribution == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA) ? (uint64_t) 1 : 0;
329
330 case MEMCACHED_BEHAVIOR_HASH:
331 return hashkit_get_function(&ptr->hashkit);
332
333 case MEMCACHED_BEHAVIOR_KETAMA_HASH:
334 return hashkit_get_function(&ptr->hashkit);
335
336 case MEMCACHED_BEHAVIOR_REMOVE_FAILED_SERVERS:
337 case MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT:
338 return ptr->server_failure_limit;
339
340 case MEMCACHED_BEHAVIOR_SORT_HOSTS:
341 return ptr->flags.use_sort_hosts;
342
343 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT:
344 return (uint64_t)ptr->poll_timeout;
345
346 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT:
347 return (uint64_t)ptr->connect_timeout;
348
349 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT:
350 return (uint64_t)ptr->retry_timeout;
351
352 case MEMCACHED_BEHAVIOR_SND_TIMEOUT:
353 return (uint64_t)ptr->snd_timeout;
354
355 case MEMCACHED_BEHAVIOR_RCV_TIMEOUT:
356 return (uint64_t)ptr->rcv_timeout;
357
358 case MEMCACHED_BEHAVIOR_TCP_KEEPIDLE:
359 return (uint64_t)ptr->tcp_keepidle;
360
361 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE:
362 {
363 int sock_size= 0;
364 socklen_t sock_length= sizeof(int);
365
366 if (ptr->send_size != -1) // If value is -1 then we are using the default
367 return (uint64_t) ptr->send_size;
368
369 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, 0);
370
371 if (instance) // If we have an instance we test, otherwise we just set and pray
372 {
373 /* REFACTOR */
374 /* We just try the first host, and if it is down we return zero */
375 if (memcached_failed(memcached_connect(instance)))
376 {
377 return 0;
378 }
379
380 if (memcached_failed(memcached_io_wait_for_write(instance)))
381 {
382 return 0;
383 }
384
385 if (getsockopt(instance->fd, SOL_SOCKET, SO_SNDBUF, &sock_size, &sock_length) < 0)
386 {
387 memcached_set_errno(*ptr, errno, MEMCACHED_AT);
388 return 0; /* Zero means error */
389 }
390 }
391
392 return (uint64_t) sock_size;
393 }
394
395 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE:
396 {
397 int sock_size= 0;
398 socklen_t sock_length= sizeof(int);
399
400 if (ptr->recv_size != -1) // If value is -1 then we are using the default
401 return (uint64_t) ptr->recv_size;
402
403 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, 0);
404
405 /**
406 @note REFACTOR
407 */
408 if (instance)
409 {
410 /* We just try the first host, and if it is down we return zero */
411 if (memcached_failed(memcached_connect(instance)))
412 {
413 return 0;
414 }
415
416 if (memcached_failed(memcached_io_wait_for_write(instance)))
417 {
418 return 0;
419 }
420
421 if (getsockopt(instance->fd, SOL_SOCKET, SO_RCVBUF, &sock_size, &sock_length) < 0)
422 {
423 memcached_set_errno(*ptr, errno, MEMCACHED_AT);
424 return 0; /* Zero means error */
425 }
426
427 }
428
429 return (uint64_t) sock_size;
430 }
431
432 case MEMCACHED_BEHAVIOR_USER_DATA:
433 memcached_set_error(*ptr, MEMCACHED_DEPRECATED, MEMCACHED_AT,
434 memcached_literal_param("MEMCACHED_BEHAVIOR_USER_DATA deprecated."));
435 return 0;
436
437 case MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY:
438 return ptr->flags.hash_with_namespace;
439
440 case MEMCACHED_BEHAVIOR_NOREPLY:
441 return ptr->flags.no_reply;
442
443 case MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS:
444 return ptr->flags.auto_eject_hosts;
445
446 case MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ:
447 return ptr->flags.randomize_replica_read;
448
449 case MEMCACHED_BEHAVIOR_CORK:
450 #ifdef HAVE_MSG_MORE
451 return true;
452 #else
453 return false;
454 #endif
455
456 case MEMCACHED_BEHAVIOR_TCP_KEEPALIVE:
457 return ptr->flags.tcp_keepalive;
458
459 case MEMCACHED_BEHAVIOR_LOAD_FROM_FILE:
460 return bool(memcached_parse_filename(ptr));
461
462 case MEMCACHED_BEHAVIOR_MAX:
463 default:
464 assert_msg(0, "Invalid behavior passed to memcached_behavior_set()");
465 return 0;
466 }
467
468 /* NOTREACHED */
469 }
470
471
472 memcached_return_t memcached_behavior_set_distribution(memcached_st *ptr, memcached_server_distribution_t type)
473 {
474 if (type < MEMCACHED_DISTRIBUTION_CONSISTENT_MAX)
475 {
476 if (MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED)
477 {
478 ptr->ketama.weighted= true;
479 }
480 else
481 {
482 ptr->ketama.weighted= false;
483 }
484
485 ptr->distribution= type;
486 run_distribution(ptr);
487
488 return MEMCACHED_SUCCESS;
489 }
490
491 return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT,
492 memcached_literal_param("Invalid memcached_server_distribution_t"));
493 }
494
495
496 memcached_server_distribution_t memcached_behavior_get_distribution(memcached_st *ptr)
497 {
498 return ptr->distribution;
499 }
500
501 memcached_return_t memcached_behavior_set_key_hash(memcached_st *ptr, memcached_hash_t type)
502 {
503 if (hashkit_success(hashkit_set_function(&ptr->hashkit, (hashkit_hash_algorithm_t)type)))
504 {
505 return MEMCACHED_SUCCESS;
506 }
507
508 return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT,
509 memcached_literal_param("Invalid memcached_hash_t()"));
510 }
511
512 memcached_hash_t memcached_behavior_get_key_hash(memcached_st *ptr)
513 {
514 return (memcached_hash_t)hashkit_get_function(&ptr->hashkit);
515 }
516
517 memcached_return_t memcached_behavior_set_distribution_hash(memcached_st *ptr, memcached_hash_t type)
518 {
519 if (hashkit_success(hashkit_set_distribution_function(&ptr->hashkit, (hashkit_hash_algorithm_t)type)))
520 return MEMCACHED_SUCCESS;
521
522 return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT,
523 memcached_literal_param("Invalid memcached_hash_t()"));
524 }
525
526 memcached_hash_t memcached_behavior_get_distribution_hash(memcached_st *ptr)
527 {
528 return (memcached_hash_t)hashkit_get_function(&ptr->hashkit);
529 }
530
531 const char *libmemcached_string_behavior(const memcached_behavior_t flag)
532 {
533 switch (flag)
534 {
535 case MEMCACHED_BEHAVIOR_NO_BLOCK: return "MEMCACHED_BEHAVIOR_NO_BLOCK";
536 case MEMCACHED_BEHAVIOR_TCP_NODELAY: return "MEMCACHED_BEHAVIOR_TCP_NODELAY";
537 case MEMCACHED_BEHAVIOR_HASH: return "MEMCACHED_BEHAVIOR_HASH";
538 case MEMCACHED_BEHAVIOR_KETAMA: return "MEMCACHED_BEHAVIOR_KETAMA";
539 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE: return "MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE";
540 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE: return "MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE";
541 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS: return "MEMCACHED_BEHAVIOR_CACHE_LOOKUPS";
542 case MEMCACHED_BEHAVIOR_SUPPORT_CAS: return "MEMCACHED_BEHAVIOR_SUPPORT_CAS";
543 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT: return "MEMCACHED_BEHAVIOR_POLL_TIMEOUT";
544 case MEMCACHED_BEHAVIOR_DISTRIBUTION: return "MEMCACHED_BEHAVIOR_DISTRIBUTION";
545 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS: return "MEMCACHED_BEHAVIOR_BUFFER_REQUESTS";
546 case MEMCACHED_BEHAVIOR_USER_DATA: return "MEMCACHED_BEHAVIOR_USER_DATA";
547 case MEMCACHED_BEHAVIOR_SORT_HOSTS: return "MEMCACHED_BEHAVIOR_SORT_HOSTS";
548 case MEMCACHED_BEHAVIOR_VERIFY_KEY: return "MEMCACHED_BEHAVIOR_VERIFY_KEY";
549 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT: return "MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT";
550 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT: return "MEMCACHED_BEHAVIOR_RETRY_TIMEOUT";
551 case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED: return "MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED";
552 case MEMCACHED_BEHAVIOR_KETAMA_HASH: return "MEMCACHED_BEHAVIOR_KETAMA_HASH";
553 case MEMCACHED_BEHAVIOR_BINARY_PROTOCOL: return "MEMCACHED_BEHAVIOR_BINARY_PROTOCOL";
554 case MEMCACHED_BEHAVIOR_SND_TIMEOUT: return "MEMCACHED_BEHAVIOR_SND_TIMEOUT";
555 case MEMCACHED_BEHAVIOR_RCV_TIMEOUT: return "MEMCACHED_BEHAVIOR_RCV_TIMEOUT";
556 case MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT: return "MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT";
557 case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK: return "MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK";
558 case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK: return "MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK";
559 case MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH: return "MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH";
560 case MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY: return "MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY";
561 case MEMCACHED_BEHAVIOR_NOREPLY: return "MEMCACHED_BEHAVIOR_NOREPLY";
562 case MEMCACHED_BEHAVIOR_USE_UDP: return "MEMCACHED_BEHAVIOR_USE_UDP";
563 case MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS: return "MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS";
564 case MEMCACHED_BEHAVIOR_REMOVE_FAILED_SERVERS: return "MEMCACHED_BEHAVIOR_REMOVE_FAILED_SERVERS";
565 case MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS: return "MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS";
566 case MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ: return "MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ";
567 case MEMCACHED_BEHAVIOR_CORK: return "MEMCACHED_BEHAVIOR_CORK";
568 case MEMCACHED_BEHAVIOR_TCP_KEEPALIVE: return "MEMCACHED_BEHAVIOR_TCP_KEEPALIVE";
569 case MEMCACHED_BEHAVIOR_TCP_KEEPIDLE: return "MEMCACHED_BEHAVIOR_TCP_KEEPIDLE";
570 case MEMCACHED_BEHAVIOR_LOAD_FROM_FILE: return "MEMCACHED_BEHAVIOR_LOAD_FROM_FILE";
571 default:
572 case MEMCACHED_BEHAVIOR_MAX: return "INVALID memcached_behavior_t";
573 }
574 }
575
576 const char *libmemcached_string_distribution(const memcached_server_distribution_t flag)
577 {
578 switch (flag)
579 {
580 case MEMCACHED_DISTRIBUTION_MODULA: return "MEMCACHED_DISTRIBUTION_MODULA";
581 case MEMCACHED_DISTRIBUTION_CONSISTENT: return "MEMCACHED_DISTRIBUTION_CONSISTENT";
582 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA: return "MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA";
583 case MEMCACHED_DISTRIBUTION_RANDOM: return "MEMCACHED_DISTRIBUTION_RANDOM";
584 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY: return "MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY";
585 case MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED: return "MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED";
586 case MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET: return "MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET";
587 default:
588 case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX: return "INVALID memcached_server_distribution_t";
589 }
590 }
591
592 memcached_return_t memcached_bucket_set(memcached_st *self,
593 const uint32_t *host_map,
594 const uint32_t *forward_map,
595 const uint32_t buckets,
596 const uint32_t replicas)
597 {
598 memcached_return_t rc;
599
600 if (not self)
601 return MEMCACHED_INVALID_ARGUMENTS;
602
603 if (not host_map)
604 return MEMCACHED_INVALID_ARGUMENTS;
605
606 memcached_server_distribution_t old= memcached_behavior_get_distribution(self);
607
608 if (memcached_failed(rc =memcached_behavior_set_distribution(self, MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET)))
609 {
610 return rc;
611 }
612
613 if (memcached_failed(rc= memcached_virtual_bucket_create(self, host_map, forward_map, buckets, replicas)))
614 {
615 memcached_behavior_set_distribution(self, old);
616 }
617
618 return rc;
619 }