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>
15 #include <sys/socket.h>
16 #include <netinet/tcp.h>
18 static bool set_flag(uint64_t data
)
21 return data
? true : false;
25 This function is used to modify the behavior of running client.
27 We quit all connections so we can reset the sockets.
30 memcached_return_t
memcached_behavior_set(memcached_st
*ptr
,
31 const memcached_behavior_t flag
,
36 case MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
:
37 ptr
->number_of_replicas
= (uint32_t)data
;
39 case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
:
40 ptr
->io_msg_watermark
= (uint32_t) data
;
42 case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK
:
43 ptr
->io_bytes_watermark
= (uint32_t)data
;
45 case MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH
:
46 ptr
->io_key_prefetch
= (uint32_t)data
;
48 case MEMCACHED_BEHAVIOR_SND_TIMEOUT
:
49 ptr
->snd_timeout
= (int32_t)data
;
51 case MEMCACHED_BEHAVIOR_RCV_TIMEOUT
:
52 ptr
->rcv_timeout
= (int32_t)data
;
54 case MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
:
55 ptr
->server_failure_limit
= (uint32_t)data
;
57 case MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
:
60 ptr
->flags
.verify_key
= false;
62 ptr
->flags
.binary_protocol
= set_flag(data
);
64 case MEMCACHED_BEHAVIOR_SUPPORT_CAS
:
65 ptr
->flags
.support_cas
= set_flag(data
);
67 case MEMCACHED_BEHAVIOR_NO_BLOCK
:
68 ptr
->flags
.no_block
= set_flag(data
);
71 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
:
72 ptr
->flags
.buffer_requests
= set_flag(data
);
75 case MEMCACHED_BEHAVIOR_USE_UDP
:
76 if (memcached_server_count(ptr
))
78 return MEMCACHED_FAILURE
;
80 ptr
->flags
.use_udp
= set_flag(data
);
83 ptr
->flags
.no_reply
= set_flag(data
);
86 case MEMCACHED_BEHAVIOR_TCP_NODELAY
:
87 ptr
->flags
.tcp_nodelay
= set_flag(data
);
90 case MEMCACHED_BEHAVIOR_DISTRIBUTION
:
91 return memcached_behavior_set_distribution(ptr
, (memcached_server_distribution_t
)data
);
92 case MEMCACHED_BEHAVIOR_KETAMA
:
96 (void)memcached_behavior_set_key_hash(ptr
, MEMCACHED_HASH_MD5
);
97 (void)memcached_behavior_set_distribution_hash(ptr
, MEMCACHED_HASH_MD5
);
98 (void)memcached_behavior_set_distribution(ptr
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
);
102 (void)memcached_behavior_set_key_hash(ptr
, MEMCACHED_HASH_DEFAULT
);
103 (void)memcached_behavior_set_distribution_hash(ptr
, MEMCACHED_HASH_DEFAULT
);
104 (void)memcached_behavior_set_distribution(ptr
, MEMCACHED_DISTRIBUTION_MODULA
);
109 case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
:
111 (void)memcached_behavior_set_key_hash(ptr
, MEMCACHED_HASH_MD5
);
112 (void)memcached_behavior_set_distribution_hash(ptr
, MEMCACHED_HASH_MD5
);
113 ptr
->flags
.ketama_weighted
= set_flag(data
);
115 @note We try to keep the same distribution going. This should be deprecated and rewritten.
117 return memcached_behavior_set_distribution(ptr
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
);
119 case MEMCACHED_BEHAVIOR_HASH
:
120 return memcached_behavior_set_key_hash(ptr
, (memcached_hash_t
)(data
));
121 case MEMCACHED_BEHAVIOR_KETAMA_HASH
:
122 return memcached_behavior_set_distribution_hash(ptr
, (memcached_hash_t
)(data
));
123 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
:
124 ptr
->flags
.use_cache_lookups
= set_flag(data
);
127 case MEMCACHED_BEHAVIOR_VERIFY_KEY
:
128 if (ptr
->flags
.binary_protocol
)
129 return MEMCACHED_FAILURE
;
130 ptr
->flags
.verify_key
= set_flag(data
);
132 case MEMCACHED_BEHAVIOR_SORT_HOSTS
:
134 ptr
->flags
.use_sort_hosts
= set_flag(data
);
135 run_distribution(ptr
);
139 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT
:
140 ptr
->poll_timeout
= (int32_t)data
;
142 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
:
143 ptr
->connect_timeout
= (int32_t)data
;
145 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
:
146 ptr
->retry_timeout
= (int32_t)data
;
148 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
:
149 ptr
->send_size
= (int32_t)data
;
152 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
:
153 ptr
->recv_size
= (int32_t)data
;
156 case MEMCACHED_BEHAVIOR_USER_DATA
:
157 return MEMCACHED_FAILURE
;
158 case MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY
:
159 ptr
->flags
.hash_with_prefix_key
= set_flag(data
);
161 case MEMCACHED_BEHAVIOR_NOREPLY
:
162 ptr
->flags
.no_reply
= set_flag(data
);
164 case MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
:
165 ptr
->flags
.auto_eject_hosts
= set_flag(data
);
167 case MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ
:
168 srandom((uint32_t) time(NULL
));
169 ptr
->flags
.randomize_replica_read
= set_flag(data
);
171 case MEMCACHED_BEHAVIOR_CORK
:
173 memcached_server_instance_st
*instance
;
174 bool action
= set_flag(data
);
178 ptr
->flags
.cork
= set_flag(false);
179 return MEMCACHED_SUCCESS
;
182 instance
= memcached_server_instance_fetch(ptr
, 0);
184 return MEMCACHED_NO_SERVERS
;
187 /* We just try the first host, and if it is down we return zero */
188 memcached_return_t rc
;
189 rc
= memcached_connect(instance
);
190 if (rc
!= MEMCACHED_SUCCESS
)
196 memcached_ternary_t enabled
;
197 enabled
= test_cork(instance
, true);
202 return ptr
->cached_errno
? MEMCACHED_ERRNO
: MEMCACHED_FAILURE
;
205 enabled
= test_cork(instance
, false);
207 if (enabled
== false) // Possible bug in OS?
209 memcached_quit_server(instance
, false); // We should reset everything on this error.
210 return MEMCACHED_ERRNO
; // Errno will be true because we will have already set it.
212 ptr
->flags
.cork
= true;
213 ptr
->flags
.tcp_nodelay
= true;
214 memcached_quit(ptr
); // We go on and reset the connections.
219 return MEMCACHED_NOT_SUPPORTED
;
223 case MEMCACHED_BEHAVIOR_MAX
:
225 /* Shouldn't get here */
226 WATCHPOINT_ASSERT(0);
227 return MEMCACHED_FAILURE
;
230 return MEMCACHED_SUCCESS
;
233 uint64_t memcached_behavior_get(memcached_st
*ptr
,
234 const memcached_behavior_t flag
)
238 case MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
:
239 return ptr
->number_of_replicas
;
240 case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
:
241 return ptr
->io_msg_watermark
;
242 case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK
:
243 return ptr
->io_bytes_watermark
;
244 case MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH
:
245 return ptr
->io_key_prefetch
;
246 case MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
:
247 return ptr
->flags
.binary_protocol
;
248 case MEMCACHED_BEHAVIOR_SUPPORT_CAS
:
249 return ptr
->flags
.support_cas
;
250 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
:
251 return ptr
->flags
.use_cache_lookups
;
252 case MEMCACHED_BEHAVIOR_NO_BLOCK
:
253 return ptr
->flags
.no_block
;
254 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
:
255 return ptr
->flags
.buffer_requests
;
256 case MEMCACHED_BEHAVIOR_USE_UDP
:
257 return ptr
->flags
.use_udp
;
258 case MEMCACHED_BEHAVIOR_TCP_NODELAY
:
259 return ptr
->flags
.tcp_nodelay
;
260 case MEMCACHED_BEHAVIOR_VERIFY_KEY
:
261 return ptr
->flags
.verify_key
;
262 case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
:
263 return ptr
->flags
.ketama_weighted
;
264 case MEMCACHED_BEHAVIOR_DISTRIBUTION
:
265 return ptr
->distribution
;
266 case MEMCACHED_BEHAVIOR_KETAMA
:
267 return (ptr
->distribution
== MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
) ? (uint64_t) 1 : 0;
268 case MEMCACHED_BEHAVIOR_HASH
:
269 return hashkit_get_function(&ptr
->hashkit
);
270 case MEMCACHED_BEHAVIOR_KETAMA_HASH
:
271 return hashkit_get_function(&ptr
->distribution_hashkit
);
272 case MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
:
273 return ptr
->server_failure_limit
;
274 case MEMCACHED_BEHAVIOR_SORT_HOSTS
:
275 return ptr
->flags
.use_sort_hosts
;
276 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT
:
277 return (uint64_t)ptr
->poll_timeout
;
278 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
:
279 return (uint64_t)ptr
->connect_timeout
;
280 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
:
281 return (uint64_t)ptr
->retry_timeout
;
282 case MEMCACHED_BEHAVIOR_SND_TIMEOUT
:
283 return (uint64_t)ptr
->snd_timeout
;
284 case MEMCACHED_BEHAVIOR_RCV_TIMEOUT
:
285 return (uint64_t)ptr
->rcv_timeout
;
286 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
:
289 socklen_t sock_length
= sizeof(int);
290 memcached_server_instance_st
*instance
;
292 if (ptr
->send_size
!= -1) // If value is -1 then we are using the default
293 return (uint64_t) ptr
->send_size
;
295 instance
= memcached_server_instance_fetch(ptr
, 0);
297 if (instance
) // If we have an instance we test, otherwise we just set and pray
300 /* We just try the first host, and if it is down we return zero */
301 if ((memcached_connect(instance
)) != MEMCACHED_SUCCESS
)
304 if (getsockopt(instance
->fd
, SOL_SOCKET
,
305 SO_SNDBUF
, &sock_size
, &sock_length
))
306 return 0; /* Zero means error */
309 return (uint64_t) sock_size
;
311 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
:
314 socklen_t sock_length
= sizeof(int);
315 memcached_server_instance_st
*instance
;
317 if (ptr
->recv_size
!= -1) // If value is -1 then we are using the default
318 return (uint64_t) ptr
->recv_size
;
320 instance
= memcached_server_instance_fetch(ptr
, 0);
327 /* We just try the first host, and if it is down we return zero */
328 if ((memcached_connect(instance
)) != MEMCACHED_SUCCESS
)
331 if (getsockopt(instance
->fd
, SOL_SOCKET
,
332 SO_RCVBUF
, &sock_size
, &sock_length
))
333 return 0; /* Zero means error */
337 return (uint64_t) sock_size
;
339 case MEMCACHED_BEHAVIOR_USER_DATA
:
340 return MEMCACHED_FAILURE
;
341 case MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY
:
342 return ptr
->flags
.hash_with_prefix_key
;
343 case MEMCACHED_BEHAVIOR_NOREPLY
:
344 return ptr
->flags
.no_reply
;
345 case MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
:
346 return ptr
->flags
.auto_eject_hosts
;
347 case MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ
:
348 return ptr
->flags
.randomize_replica_read
;
349 case MEMCACHED_BEHAVIOR_CORK
:
350 return ptr
->flags
.cork
;
351 case MEMCACHED_BEHAVIOR_MAX
:
353 WATCHPOINT_ASSERT(0); /* Programming mistake if it gets this far */
361 memcached_return_t
memcached_behavior_set_distribution(memcached_st
*ptr
, memcached_server_distribution_t type
)
363 if (type
< MEMCACHED_DISTRIBUTION_CONSISTENT_MAX
)
365 ptr
->distribution
= type
;
366 run_distribution(ptr
);
370 return MEMCACHED_FAILURE
;
373 return MEMCACHED_SUCCESS
;
377 memcached_server_distribution_t
memcached_behavior_get_distribution(memcached_st
*ptr
)
379 return ptr
->distribution
;
382 memcached_return_t
memcached_behavior_set_key_hash(memcached_st
*ptr
, memcached_hash_t type
)
385 rc
= hashkit_set_function(&ptr
->hashkit
, (hashkit_hash_algorithm_t
)type
);
387 return rc
== HASHKIT_SUCCESS
? MEMCACHED_SUCCESS
: MEMCACHED_FAILURE
;
390 memcached_hash_t
memcached_behavior_get_key_hash(memcached_st
*ptr
)
392 return (memcached_hash_t
)hashkit_get_function(&ptr
->hashkit
);
395 memcached_return_t
memcached_behavior_set_distribution_hash(memcached_st
*ptr
, memcached_hash_t type
)
398 rc
= hashkit_set_function(&ptr
->distribution_hashkit
, (hashkit_hash_algorithm_t
)type
);
400 return rc
== HASHKIT_SUCCESS
? MEMCACHED_SUCCESS
: MEMCACHED_FAILURE
;
403 memcached_hash_t
memcached_behavior_get_distribution_hash(memcached_st
*ptr
)
405 return (memcached_hash_t
)hashkit_get_function(&ptr
->distribution_hashkit
);