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>
19 This function is used to modify the behavior of running client.
21 We quit all connections so we can reset the sockets.
24 memcached_return_t
memcached_behavior_set(memcached_st
*ptr
,
25 memcached_behavior_t flag
,
30 case MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
:
31 ptr
->number_of_replicas
= (uint32_t)data
;
33 case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
:
34 ptr
->io_msg_watermark
= (uint32_t) data
;
36 case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK
:
37 ptr
->io_bytes_watermark
= (uint32_t)data
;
39 case MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH
:
40 ptr
->io_key_prefetch
= (uint32_t)data
;
42 case MEMCACHED_BEHAVIOR_SND_TIMEOUT
:
43 ptr
->snd_timeout
= (int32_t)data
;
45 case MEMCACHED_BEHAVIOR_RCV_TIMEOUT
:
46 ptr
->rcv_timeout
= (int32_t)data
;
48 case MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
:
49 ptr
->server_failure_limit
= (uint32_t)data
;
51 case MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
:
53 ptr
->flags
.verify_key
= false;
55 ptr
->flags
.binary_protocol
= data
? true : false;
57 case MEMCACHED_BEHAVIOR_SUPPORT_CAS
:
58 ptr
->flags
.support_cas
= data
? true: false;
60 case MEMCACHED_BEHAVIOR_NO_BLOCK
:
61 ptr
->flags
.no_block
= data
? true: false;
64 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
:
65 ptr
->flags
.buffer_requests
= data
? true : false;
68 case MEMCACHED_BEHAVIOR_USE_UDP
:
69 if (ptr
->number_of_hosts
)
70 return MEMCACHED_FAILURE
;
71 ptr
->flags
.use_udp
= data
? true : false;
74 ptr
->flags
.no_reply
= data
? true : false;
77 case MEMCACHED_BEHAVIOR_TCP_NODELAY
:
78 ptr
->flags
.tcp_nodelay
= data
? true : false;
81 case MEMCACHED_BEHAVIOR_DISTRIBUTION
:
83 ptr
->distribution
= (memcached_server_distribution_t
)(data
);
84 if (ptr
->distribution
== MEMCACHED_DISTRIBUTION_RANDOM
)
86 srandom((uint32_t) time(NULL
));
88 run_distribution(ptr
);
91 case MEMCACHED_BEHAVIOR_KETAMA
:
95 ptr
->hash
= MEMCACHED_HASH_MD5
;
96 ptr
->distribution
= MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
;
101 ptr
->distribution
= 0;
103 run_distribution(ptr
);
106 case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
:
108 ptr
->hash
= MEMCACHED_HASH_MD5
;
109 ptr
->distribution
= MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
;
110 ptr
->flags
.ketama_weighted
= data
? true : false;
111 run_distribution(ptr
);
114 case MEMCACHED_BEHAVIOR_KETAMA_COMPAT_MODE
:
117 case MEMCACHED_KETAMA_COMPAT_LIBMEMCACHED
:
118 ptr
->hash
= MEMCACHED_HASH_MD5
;
119 ptr
->distribution
= MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
;
121 case MEMCACHED_KETAMA_COMPAT_SPY
:
122 ptr
->hash
= MEMCACHED_HASH_MD5
;
123 ptr
->distribution
= MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
;
126 return MEMCACHED_FAILURE
;
128 run_distribution(ptr
);
130 case MEMCACHED_BEHAVIOR_HASH
:
131 #ifndef HAVE_HSIEH_HASH
132 if ((memcached_hash_t
)(data
) == MEMCACHED_HASH_HSIEH
)
133 return MEMCACHED_FAILURE
;
135 ptr
->hash
= (memcached_hash_t
)(data
);
137 case MEMCACHED_BEHAVIOR_KETAMA_HASH
:
138 ptr
->hash_continuum
= (memcached_hash_t
)(data
);
139 run_distribution(ptr
);
141 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
:
142 ptr
->flags
.use_cache_lookups
= data
? true : false;
145 case MEMCACHED_BEHAVIOR_VERIFY_KEY
:
146 if (ptr
->flags
.binary_protocol
)
148 ptr
->flags
.verify_key
= data
? true : false;
150 case MEMCACHED_BEHAVIOR_SORT_HOSTS
:
152 ptr
->flags
.use_sort_hosts
= data
? true : false;
153 run_distribution(ptr
);
157 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT
:
158 ptr
->poll_timeout
= (int32_t)data
;
160 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
:
161 ptr
->connect_timeout
= (int32_t)data
;
163 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
:
164 ptr
->retry_timeout
= (int32_t)data
;
166 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
:
167 ptr
->send_size
= (int32_t)data
;
170 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
:
171 ptr
->recv_size
= (int32_t)data
;
174 case MEMCACHED_BEHAVIOR_USER_DATA
:
175 return MEMCACHED_FAILURE
;
176 case MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY
:
177 ptr
->flags
.hash_with_prefix_key
= data
? true : false;
179 case MEMCACHED_BEHAVIOR_NOREPLY
:
180 ptr
->flags
.no_reply
= data
? true : false;
182 case MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
:
183 ptr
->flags
.auto_eject_hosts
= data
? true : false;
185 case MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ
:
186 srandom((uint32_t) time(NULL
));
187 ptr
->flags
.randomize_replica_read
= data
? true : false;
190 /* Shouldn't get here */
191 WATCHPOINT_ASSERT(flag
);
195 return MEMCACHED_SUCCESS
;
198 uint64_t memcached_behavior_get(memcached_st
*ptr
,
199 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_KETAMA_COMPAT_MODE
:
234 switch (ptr
->distribution
)
236 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
:
237 return MEMCACHED_KETAMA_COMPAT_LIBMEMCACHED
;
238 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
:
239 return MEMCACHED_KETAMA_COMPAT_SPY
;
240 case MEMCACHED_DISTRIBUTION_MODULA
:
241 case MEMCACHED_DISTRIBUTION_CONSISTENT
:
242 case MEMCACHED_DISTRIBUTION_RANDOM
:
247 case MEMCACHED_BEHAVIOR_HASH
:
249 case MEMCACHED_BEHAVIOR_KETAMA_HASH
:
250 return ptr
->hash_continuum
;
251 case MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
:
252 return ptr
->server_failure_limit
;
253 case MEMCACHED_BEHAVIOR_SORT_HOSTS
:
254 return ptr
->flags
.use_sort_hosts
;
255 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT
:
256 return (uint64_t)ptr
->poll_timeout
;
257 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
:
258 return (uint64_t)ptr
->connect_timeout
;
259 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
:
260 return (uint64_t)ptr
->retry_timeout
;
261 case MEMCACHED_BEHAVIOR_SND_TIMEOUT
:
262 return (uint64_t)ptr
->snd_timeout
;
263 case MEMCACHED_BEHAVIOR_RCV_TIMEOUT
:
264 return (uint64_t)ptr
->rcv_timeout
;
265 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
:
268 socklen_t sock_length
= sizeof(int);
271 /* We just try the first host, and if it is down we return zero */
272 if ((memcached_connect(&ptr
->hosts
[0])) != MEMCACHED_SUCCESS
)
275 if (getsockopt(ptr
->hosts
[0].fd
, SOL_SOCKET
,
276 SO_SNDBUF
, &sock_size
, &sock_length
))
277 return 0; /* Zero means error */
279 return (uint64_t) sock_size
;
281 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
:
284 socklen_t sock_length
= sizeof(int);
287 /* We just try the first host, and if it is down we return zero */
288 if ((memcached_connect(&ptr
->hosts
[0])) != MEMCACHED_SUCCESS
)
291 if (getsockopt(ptr
->hosts
[0].fd
, SOL_SOCKET
,
292 SO_RCVBUF
, &sock_size
, &sock_length
))
293 return 0; /* Zero means error */
295 return (uint64_t) sock_size
;
297 case MEMCACHED_BEHAVIOR_USER_DATA
:
298 return MEMCACHED_FAILURE
;
299 case MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY
:
300 return ptr
->flags
.hash_with_prefix_key
;
301 case MEMCACHED_BEHAVIOR_NOREPLY
:
302 return ptr
->flags
.no_reply
;
303 case MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
:
304 return ptr
->flags
.auto_eject_hosts
;
305 case MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ
:
306 return ptr
->flags
.randomize_replica_read
;
308 WATCHPOINT_ASSERT(flag
);
312 WATCHPOINT_ASSERT(0); /* Programming mistake if it gets this far */