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: Change the behavior of the memcached connection.
14 #include <sys/types.h>
16 static bool set_flag(uint64_t data
)
19 return data
? true : false;
23 This function is used to modify the behavior of running client.
25 We quit all connections so we can reset the sockets.
28 memcached_return_t
memcached_behavior_set(memcached_st
*ptr
,
29 const memcached_behavior_t flag
,
34 case MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
:
35 ptr
->number_of_replicas
= (uint32_t)data
;
37 case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
:
38 ptr
->io_msg_watermark
= (uint32_t) data
;
40 case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK
:
41 ptr
->io_bytes_watermark
= (uint32_t)data
;
43 case MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH
:
44 ptr
->io_key_prefetch
= (uint32_t)data
;
46 case MEMCACHED_BEHAVIOR_SND_TIMEOUT
:
47 ptr
->snd_timeout
= (int32_t)data
;
49 case MEMCACHED_BEHAVIOR_RCV_TIMEOUT
:
50 ptr
->rcv_timeout
= (int32_t)data
;
52 case MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
:
53 ptr
->server_failure_limit
= (uint32_t)data
;
55 case MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
:
56 memcached_quit(ptr
); // We need t shutdown all of the connections to make sure we do the correct protocol
59 ptr
->flags
.verify_key
= false;
61 ptr
->flags
.binary_protocol
= set_flag(data
);
63 case MEMCACHED_BEHAVIOR_SUPPORT_CAS
:
64 ptr
->flags
.support_cas
= set_flag(data
);
66 case MEMCACHED_BEHAVIOR_NO_BLOCK
:
67 ptr
->flags
.no_block
= set_flag(data
);
70 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
:
71 ptr
->flags
.buffer_requests
= set_flag(data
);
74 case MEMCACHED_BEHAVIOR_USE_UDP
:
75 if (memcached_server_count(ptr
))
77 return MEMCACHED_FAILURE
;
79 ptr
->flags
.use_udp
= set_flag(data
);
82 ptr
->flags
.no_reply
= set_flag(data
);
85 case MEMCACHED_BEHAVIOR_TCP_NODELAY
:
86 ptr
->flags
.tcp_nodelay
= set_flag(data
);
89 case MEMCACHED_BEHAVIOR_TCP_KEEPALIVE
:
90 ptr
->flags
.tcp_keepalive
= set_flag(data
);
93 case MEMCACHED_BEHAVIOR_DISTRIBUTION
:
94 return memcached_behavior_set_distribution(ptr
, (memcached_server_distribution_t
)data
);
95 case MEMCACHED_BEHAVIOR_KETAMA
:
99 (void)memcached_behavior_set_key_hash(ptr
, MEMCACHED_HASH_MD5
);
100 (void)memcached_behavior_set_distribution_hash(ptr
, MEMCACHED_HASH_MD5
);
101 (void)memcached_behavior_set_distribution(ptr
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
);
105 (void)memcached_behavior_set_key_hash(ptr
, MEMCACHED_HASH_DEFAULT
);
106 (void)memcached_behavior_set_distribution_hash(ptr
, MEMCACHED_HASH_DEFAULT
);
107 (void)memcached_behavior_set_distribution(ptr
, MEMCACHED_DISTRIBUTION_MODULA
);
112 case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
:
114 (void)memcached_behavior_set_key_hash(ptr
, MEMCACHED_HASH_MD5
);
115 (void)memcached_behavior_set_distribution_hash(ptr
, MEMCACHED_HASH_MD5
);
116 ptr
->flags
.ketama_weighted
= set_flag(data
);
118 @note We try to keep the same distribution going. This should be deprecated and rewritten.
120 return memcached_behavior_set_distribution(ptr
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
);
122 case MEMCACHED_BEHAVIOR_HASH
:
123 return memcached_behavior_set_key_hash(ptr
, (memcached_hash_t
)(data
));
124 case MEMCACHED_BEHAVIOR_KETAMA_HASH
:
125 return memcached_behavior_set_distribution_hash(ptr
, (memcached_hash_t
)(data
));
126 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
:
127 ptr
->flags
.use_cache_lookups
= set_flag(data
);
130 case MEMCACHED_BEHAVIOR_VERIFY_KEY
:
131 if (ptr
->flags
.binary_protocol
)
132 return MEMCACHED_FAILURE
;
133 ptr
->flags
.verify_key
= set_flag(data
);
135 case MEMCACHED_BEHAVIOR_SORT_HOSTS
:
137 ptr
->flags
.use_sort_hosts
= set_flag(data
);
138 run_distribution(ptr
);
142 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT
:
143 ptr
->poll_timeout
= (int32_t)data
;
145 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
:
146 ptr
->connect_timeout
= (int32_t)data
;
148 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
:
149 ptr
->retry_timeout
= (int32_t)data
;
151 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
:
152 ptr
->send_size
= (int32_t)data
;
155 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
:
156 ptr
->recv_size
= (int32_t)data
;
159 case MEMCACHED_BEHAVIOR_TCP_KEEPIDLE
:
160 ptr
->tcp_keepidle
= (uint32_t)data
;
163 case MEMCACHED_BEHAVIOR_USER_DATA
:
164 return MEMCACHED_FAILURE
;
165 case MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY
:
166 ptr
->flags
.hash_with_prefix_key
= set_flag(data
);
168 case MEMCACHED_BEHAVIOR_NOREPLY
:
169 ptr
->flags
.no_reply
= set_flag(data
);
171 case MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
:
172 ptr
->flags
.auto_eject_hosts
= set_flag(data
);
174 case MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ
:
175 srandom((uint32_t) time(NULL
));
176 ptr
->flags
.randomize_replica_read
= set_flag(data
);
178 case MEMCACHED_BEHAVIOR_CORK
:
180 memcached_server_write_instance_st instance
;
181 bool action
= set_flag(data
);
185 ptr
->flags
.cork
= set_flag(false);
186 return MEMCACHED_SUCCESS
;
189 instance
= memcached_server_instance_fetch(ptr
, 0);
191 return MEMCACHED_NO_SERVERS
;
194 /* We just try the first host, and if it is down we return zero */
195 memcached_return_t rc
;
196 rc
= memcached_connect(instance
);
197 if (rc
!= MEMCACHED_SUCCESS
)
203 memcached_ternary_t enabled
;
204 enabled
= test_cork(instance
, true);
209 return ptr
->cached_errno
? MEMCACHED_ERRNO
: MEMCACHED_FAILURE
;
212 enabled
= test_cork(instance
, false);
214 if (enabled
== false) // Possible bug in OS?
216 memcached_quit_server(instance
, false); // We should reset everything on this error.
217 return MEMCACHED_ERRNO
; // Errno will be true because we will have already set it.
219 ptr
->flags
.cork
= true;
220 ptr
->flags
.tcp_nodelay
= true;
221 memcached_quit(ptr
); // We go on and reset the connections.
226 return MEMCACHED_NOT_SUPPORTED
;
230 case MEMCACHED_BEHAVIOR_MAX
:
232 /* Shouldn't get here */
233 WATCHPOINT_ASSERT(0);
234 return MEMCACHED_FAILURE
;
237 return MEMCACHED_SUCCESS
;
240 bool _is_auto_eject_host(const memcached_st
*ptr
)
242 return ptr
->flags
.auto_eject_hosts
;
245 uint64_t memcached_behavior_get(memcached_st
*ptr
,
246 const memcached_behavior_t flag
)
250 case MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
:
251 return ptr
->number_of_replicas
;
252 case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
:
253 return ptr
->io_msg_watermark
;
254 case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK
:
255 return ptr
->io_bytes_watermark
;
256 case MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH
:
257 return ptr
->io_key_prefetch
;
258 case MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
:
259 return ptr
->flags
.binary_protocol
;
260 case MEMCACHED_BEHAVIOR_SUPPORT_CAS
:
261 return ptr
->flags
.support_cas
;
262 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
:
263 return ptr
->flags
.use_cache_lookups
;
264 case MEMCACHED_BEHAVIOR_NO_BLOCK
:
265 return ptr
->flags
.no_block
;
266 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
:
267 return ptr
->flags
.buffer_requests
;
268 case MEMCACHED_BEHAVIOR_USE_UDP
:
269 return ptr
->flags
.use_udp
;
270 case MEMCACHED_BEHAVIOR_TCP_NODELAY
:
271 return ptr
->flags
.tcp_nodelay
;
272 case MEMCACHED_BEHAVIOR_VERIFY_KEY
:
273 return ptr
->flags
.verify_key
;
274 case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
:
275 return ptr
->flags
.ketama_weighted
;
276 case MEMCACHED_BEHAVIOR_DISTRIBUTION
:
277 return ptr
->distribution
;
278 case MEMCACHED_BEHAVIOR_KETAMA
:
279 return (ptr
->distribution
== MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
) ? (uint64_t) 1 : 0;
280 case MEMCACHED_BEHAVIOR_HASH
:
281 return hashkit_get_function(&ptr
->hashkit
);
282 case MEMCACHED_BEHAVIOR_KETAMA_HASH
:
283 return hashkit_get_function(&ptr
->distribution_hashkit
);
284 case MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
:
285 return ptr
->server_failure_limit
;
286 case MEMCACHED_BEHAVIOR_SORT_HOSTS
:
287 return ptr
->flags
.use_sort_hosts
;
288 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT
:
289 return (uint64_t)ptr
->poll_timeout
;
290 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
:
291 return (uint64_t)ptr
->connect_timeout
;
292 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
:
293 return (uint64_t)ptr
->retry_timeout
;
294 case MEMCACHED_BEHAVIOR_SND_TIMEOUT
:
295 return (uint64_t)ptr
->snd_timeout
;
296 case MEMCACHED_BEHAVIOR_RCV_TIMEOUT
:
297 return (uint64_t)ptr
->rcv_timeout
;
298 case MEMCACHED_BEHAVIOR_TCP_KEEPIDLE
:
299 return (uint64_t)ptr
->tcp_keepidle
;
300 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
:
303 socklen_t sock_length
= sizeof(int);
304 memcached_server_write_instance_st instance
;
306 if (ptr
->send_size
!= -1) // If value is -1 then we are using the default
307 return (uint64_t) ptr
->send_size
;
309 instance
= memcached_server_instance_fetch(ptr
, 0);
311 if (instance
) // If we have an instance we test, otherwise we just set and pray
314 /* We just try the first host, and if it is down we return zero */
315 if ((memcached_connect(instance
)) != MEMCACHED_SUCCESS
)
318 if (getsockopt(instance
->fd
, SOL_SOCKET
,
319 SO_SNDBUF
, &sock_size
, &sock_length
))
320 return 0; /* Zero means error */
323 return (uint64_t) sock_size
;
325 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
:
328 socklen_t sock_length
= sizeof(int);
329 memcached_server_write_instance_st instance
;
331 if (ptr
->recv_size
!= -1) // If value is -1 then we are using the default
332 return (uint64_t) ptr
->recv_size
;
334 instance
= memcached_server_instance_fetch(ptr
, 0);
341 /* We just try the first host, and if it is down we return zero */
342 if ((memcached_connect(instance
)) != MEMCACHED_SUCCESS
)
345 if (getsockopt(instance
->fd
, SOL_SOCKET
,
346 SO_RCVBUF
, &sock_size
, &sock_length
))
347 return 0; /* Zero means error */
351 return (uint64_t) sock_size
;
353 case MEMCACHED_BEHAVIOR_USER_DATA
:
354 return MEMCACHED_FAILURE
;
355 case MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY
:
356 return ptr
->flags
.hash_with_prefix_key
;
357 case MEMCACHED_BEHAVIOR_NOREPLY
:
358 return ptr
->flags
.no_reply
;
359 case MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
:
360 return ptr
->flags
.auto_eject_hosts
;
361 case MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ
:
362 return ptr
->flags
.randomize_replica_read
;
363 case MEMCACHED_BEHAVIOR_CORK
:
364 return ptr
->flags
.cork
;
365 case MEMCACHED_BEHAVIOR_TCP_KEEPALIVE
:
366 return ptr
->flags
.tcp_keepalive
;
367 case MEMCACHED_BEHAVIOR_MAX
:
369 WATCHPOINT_ASSERT(0); /* Programming mistake if it gets this far */
377 memcached_return_t
memcached_behavior_set_distribution(memcached_st
*ptr
, memcached_server_distribution_t type
)
379 if (type
< MEMCACHED_DISTRIBUTION_CONSISTENT_MAX
)
381 ptr
->distribution
= type
;
382 run_distribution(ptr
);
386 return MEMCACHED_FAILURE
;
389 return MEMCACHED_SUCCESS
;
393 memcached_server_distribution_t
memcached_behavior_get_distribution(memcached_st
*ptr
)
395 return ptr
->distribution
;
398 memcached_return_t
memcached_behavior_set_key_hash(memcached_st
*ptr
, memcached_hash_t type
)
401 rc
= hashkit_set_function(&ptr
->hashkit
, (hashkit_hash_algorithm_t
)type
);
403 return rc
== HASHKIT_SUCCESS
? MEMCACHED_SUCCESS
: MEMCACHED_FAILURE
;
406 memcached_hash_t
memcached_behavior_get_key_hash(memcached_st
*ptr
)
408 return (memcached_hash_t
)hashkit_get_function(&ptr
->hashkit
);
411 memcached_return_t
memcached_behavior_set_distribution_hash(memcached_st
*ptr
, memcached_hash_t type
)
414 rc
= hashkit_set_function(&ptr
->distribution_hashkit
, (hashkit_hash_algorithm_t
)type
);
416 return rc
== HASHKIT_SUCCESS
? MEMCACHED_SUCCESS
: MEMCACHED_FAILURE
;
419 memcached_hash_t
memcached_behavior_get_distribution_hash(memcached_st
*ptr
)
421 return (memcached_hash_t
)hashkit_get_function(&ptr
->distribution_hashkit
);