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;
24 static memcached_return_t
set_hash(memcached_hash_t
*store
, memcached_hash_t type
)
26 #ifndef HAVE_HSIEH_HASH
27 if (type
== MEMCACHED_HASH_HSIEH
)
28 return MEMCACHED_FAILURE
;
30 if (type
< MEMCACHED_HASH_MAX
)
36 return MEMCACHED_FAILURE
;
39 return MEMCACHED_SUCCESS
;
42 This function is used to modify the behavior of running client.
44 We quit all connections so we can reset the sockets.
47 memcached_return_t
memcached_behavior_set(memcached_st
*ptr
,
48 const memcached_behavior_t flag
,
53 case MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
:
54 ptr
->number_of_replicas
= (uint32_t)data
;
56 case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
:
57 ptr
->io_msg_watermark
= (uint32_t) data
;
59 case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK
:
60 ptr
->io_bytes_watermark
= (uint32_t)data
;
62 case MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH
:
63 ptr
->io_key_prefetch
= (uint32_t)data
;
65 case MEMCACHED_BEHAVIOR_SND_TIMEOUT
:
66 ptr
->snd_timeout
= (int32_t)data
;
68 case MEMCACHED_BEHAVIOR_RCV_TIMEOUT
:
69 ptr
->rcv_timeout
= (int32_t)data
;
71 case MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
:
72 ptr
->server_failure_limit
= (uint32_t)data
;
74 case MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
:
77 ptr
->flags
.verify_key
= false;
79 ptr
->flags
.binary_protocol
= set_flag(data
);
81 case MEMCACHED_BEHAVIOR_SUPPORT_CAS
:
82 ptr
->flags
.support_cas
= set_flag(data
);
84 case MEMCACHED_BEHAVIOR_NO_BLOCK
:
85 ptr
->flags
.no_block
= set_flag(data
);
88 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
:
89 ptr
->flags
.buffer_requests
= set_flag(data
);
92 case MEMCACHED_BEHAVIOR_USE_UDP
:
93 if (memcached_server_count(ptr
))
95 return MEMCACHED_FAILURE
;
97 ptr
->flags
.use_udp
= set_flag(data
);
100 ptr
->flags
.no_reply
= set_flag(data
);
103 case MEMCACHED_BEHAVIOR_TCP_NODELAY
:
104 ptr
->flags
.tcp_nodelay
= set_flag(data
);
107 case MEMCACHED_BEHAVIOR_DISTRIBUTION
:
108 return memcached_behavior_set_distribution(ptr
, (memcached_server_distribution_t
)data
);
109 case MEMCACHED_BEHAVIOR_KETAMA
:
113 (void)memcached_behavior_set_key_hash(ptr
, MEMCACHED_HASH_MD5
);
114 (void)memcached_behavior_set_distribution_hash(ptr
, MEMCACHED_HASH_MD5
);
115 (void)memcached_behavior_set_distribution(ptr
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
);
119 (void)memcached_behavior_set_key_hash(ptr
, MEMCACHED_HASH_DEFAULT
);
120 (void)memcached_behavior_set_distribution_hash(ptr
, MEMCACHED_HASH_DEFAULT
);
121 (void)memcached_behavior_set_distribution(ptr
, MEMCACHED_DISTRIBUTION_MODULA
);
126 case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
:
128 (void)memcached_behavior_set_key_hash(ptr
, MEMCACHED_HASH_MD5
);
129 (void)memcached_behavior_set_distribution_hash(ptr
, MEMCACHED_HASH_MD5
);
130 ptr
->flags
.ketama_weighted
= set_flag(data
);
132 @note We try to keep the same distribution going. This should be deprecated and rewritten.
134 return memcached_behavior_set_distribution(ptr
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
);
136 case MEMCACHED_BEHAVIOR_HASH
:
137 return memcached_behavior_set_key_hash(ptr
, (memcached_hash_t
)(data
));
138 case MEMCACHED_BEHAVIOR_KETAMA_HASH
:
139 return memcached_behavior_set_distribution_hash(ptr
, (memcached_hash_t
)(data
));
140 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
:
141 ptr
->flags
.use_cache_lookups
= set_flag(data
);
144 case MEMCACHED_BEHAVIOR_VERIFY_KEY
:
145 if (ptr
->flags
.binary_protocol
)
146 return MEMCACHED_FAILURE
;
147 ptr
->flags
.verify_key
= set_flag(data
);
149 case MEMCACHED_BEHAVIOR_SORT_HOSTS
:
151 ptr
->flags
.use_sort_hosts
= set_flag(data
);
152 run_distribution(ptr
);
156 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT
:
157 ptr
->poll_timeout
= (int32_t)data
;
159 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
:
160 ptr
->connect_timeout
= (int32_t)data
;
162 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
:
163 ptr
->retry_timeout
= (int32_t)data
;
165 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
:
166 ptr
->send_size
= (int32_t)data
;
169 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
:
170 ptr
->recv_size
= (int32_t)data
;
173 case MEMCACHED_BEHAVIOR_USER_DATA
:
174 return MEMCACHED_FAILURE
;
175 case MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY
:
176 ptr
->flags
.hash_with_prefix_key
= set_flag(data
);
178 case MEMCACHED_BEHAVIOR_NOREPLY
:
179 ptr
->flags
.no_reply
= set_flag(data
);
181 case MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
:
182 ptr
->flags
.auto_eject_hosts
= set_flag(data
);
184 case MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ
:
185 srandom((uint32_t) time(NULL
));
186 ptr
->flags
.randomize_replica_read
= set_flag(data
);
188 case MEMCACHED_BEHAVIOR_MAX
:
190 /* Shouldn't get here */
191 WATCHPOINT_ASSERT(0);
192 return MEMCACHED_FAILURE
;
195 return MEMCACHED_SUCCESS
;
198 uint64_t memcached_behavior_get(memcached_st
*ptr
,
199 const memcached_behavior_t flag
)
203 case MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
:
204 return ptr
->number_of_replicas
;
205 case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
:
206 return ptr
->io_msg_watermark
;
207 case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK
:
208 return ptr
->io_bytes_watermark
;
209 case MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH
:
210 return ptr
->io_key_prefetch
;
211 case MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
:
212 return ptr
->flags
.binary_protocol
;
213 case MEMCACHED_BEHAVIOR_SUPPORT_CAS
:
214 return ptr
->flags
.support_cas
;
215 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
:
216 return ptr
->flags
.use_cache_lookups
;
217 case MEMCACHED_BEHAVIOR_NO_BLOCK
:
218 return ptr
->flags
.no_block
;
219 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
:
220 return ptr
->flags
.buffer_requests
;
221 case MEMCACHED_BEHAVIOR_USE_UDP
:
222 return ptr
->flags
.use_udp
;
223 case MEMCACHED_BEHAVIOR_TCP_NODELAY
:
224 return ptr
->flags
.tcp_nodelay
;
225 case MEMCACHED_BEHAVIOR_VERIFY_KEY
:
226 return ptr
->flags
.verify_key
;
227 case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
:
228 return ptr
->flags
.ketama_weighted
;
229 case MEMCACHED_BEHAVIOR_DISTRIBUTION
:
230 return ptr
->distribution
;
231 case MEMCACHED_BEHAVIOR_KETAMA
:
232 return (ptr
->distribution
== MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
) ? (uint64_t) 1 : 0;
233 case MEMCACHED_BEHAVIOR_HASH
:
235 case MEMCACHED_BEHAVIOR_KETAMA_HASH
:
236 return ptr
->distribution_hash
;
237 case MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
:
238 return ptr
->server_failure_limit
;
239 case MEMCACHED_BEHAVIOR_SORT_HOSTS
:
240 return ptr
->flags
.use_sort_hosts
;
241 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT
:
242 return (uint64_t)ptr
->poll_timeout
;
243 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
:
244 return (uint64_t)ptr
->connect_timeout
;
245 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
:
246 return (uint64_t)ptr
->retry_timeout
;
247 case MEMCACHED_BEHAVIOR_SND_TIMEOUT
:
248 return (uint64_t)ptr
->snd_timeout
;
249 case MEMCACHED_BEHAVIOR_RCV_TIMEOUT
:
250 return (uint64_t)ptr
->rcv_timeout
;
251 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
:
254 socklen_t sock_length
= sizeof(int);
255 memcached_server_instance_st
*instance
;
257 instance
= memcached_server_instance_fetch(ptr
, 0);
260 /* We just try the first host, and if it is down we return zero */
261 if ((memcached_connect(instance
)) != MEMCACHED_SUCCESS
)
264 if (getsockopt(instance
->fd
, SOL_SOCKET
,
265 SO_SNDBUF
, &sock_size
, &sock_length
))
266 return 0; /* Zero means error */
268 return (uint64_t) sock_size
;
270 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
:
273 socklen_t sock_length
= sizeof(int);
274 memcached_server_instance_st
*instance
;
276 instance
= memcached_server_instance_fetch(ptr
, 0);
281 /* We just try the first host, and if it is down we return zero */
282 if ((memcached_connect(instance
)) != MEMCACHED_SUCCESS
)
285 if (getsockopt(instance
->fd
, SOL_SOCKET
,
286 SO_RCVBUF
, &sock_size
, &sock_length
))
287 return 0; /* Zero means error */
289 return (uint64_t) sock_size
;
291 case MEMCACHED_BEHAVIOR_USER_DATA
:
292 return MEMCACHED_FAILURE
;
293 case MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY
:
294 return ptr
->flags
.hash_with_prefix_key
;
295 case MEMCACHED_BEHAVIOR_NOREPLY
:
296 return ptr
->flags
.no_reply
;
297 case MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
:
298 return ptr
->flags
.auto_eject_hosts
;
299 case MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ
:
300 return ptr
->flags
.randomize_replica_read
;
301 case MEMCACHED_BEHAVIOR_MAX
:
303 WATCHPOINT_ASSERT(0); /* Programming mistake if it gets this far */
311 memcached_return_t
memcached_behavior_set_distribution(memcached_st
*ptr
, memcached_server_distribution_t type
)
313 if (type
< MEMCACHED_DISTRIBUTION_CONSISTENT_MAX
)
315 ptr
->distribution
= type
;
316 run_distribution(ptr
);
320 return MEMCACHED_FAILURE
;
323 return MEMCACHED_SUCCESS
;
327 memcached_server_distribution_t
memcached_behavior_get_distribution(memcached_st
*ptr
)
329 return ptr
->distribution
;
332 memcached_return_t
memcached_behavior_set_key_hash(memcached_st
*ptr
, memcached_hash_t type
)
334 return set_hash(&ptr
->hash
, type
);
337 memcached_hash_t
memcached_behavior_get_key_hash(memcached_st
*ptr
)
342 memcached_return_t
memcached_behavior_set_distribution_hash(memcached_st
*ptr
, memcached_hash_t type
)
344 return set_hash(&ptr
->distribution_hash
, type
);
347 memcached_hash_t
memcached_behavior_get_distribution_hash(memcached_st
*ptr
)
349 return ptr
->distribution_hash
;