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