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);
257 /* We just try the first host, and if it is down we return zero */
258 if ((memcached_connect(&ptr
->hosts
[0])) != MEMCACHED_SUCCESS
)
261 if (getsockopt(ptr
->hosts
[0].fd
, SOL_SOCKET
,
262 SO_SNDBUF
, &sock_size
, &sock_length
))
263 return 0; /* Zero means error */
265 return (uint64_t) sock_size
;
267 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
:
270 socklen_t sock_length
= sizeof(int);
275 /* We just try the first host, and if it is down we return zero */
276 if ((memcached_connect(&ptr
->hosts
[0])) != MEMCACHED_SUCCESS
)
279 if (getsockopt(ptr
->hosts
[0].fd
, SOL_SOCKET
,
280 SO_RCVBUF
, &sock_size
, &sock_length
))
281 return 0; /* Zero means error */
283 return (uint64_t) sock_size
;
285 case MEMCACHED_BEHAVIOR_USER_DATA
:
286 return MEMCACHED_FAILURE
;
287 case MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY
:
288 return ptr
->flags
.hash_with_prefix_key
;
289 case MEMCACHED_BEHAVIOR_NOREPLY
:
290 return ptr
->flags
.no_reply
;
291 case MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
:
292 return ptr
->flags
.auto_eject_hosts
;
293 case MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ
:
294 return ptr
->flags
.randomize_replica_read
;
295 case MEMCACHED_BEHAVIOR_MAX
:
297 WATCHPOINT_ASSERT(0); /* Programming mistake if it gets this far */
305 memcached_return_t
memcached_behavior_set_distribution(memcached_st
*ptr
, memcached_server_distribution_t type
)
307 if (type
< MEMCACHED_DISTRIBUTION_CONSISTENT_MAX
)
309 ptr
->distribution
= type
;
310 run_distribution(ptr
);
314 return MEMCACHED_FAILURE
;
317 return MEMCACHED_SUCCESS
;
321 memcached_server_distribution_t
memcached_behavior_get_distribution(memcached_st
*ptr
)
323 return ptr
->distribution
;
326 memcached_return_t
memcached_behavior_set_key_hash(memcached_st
*ptr
, memcached_hash_t type
)
328 return set_hash(&ptr
->hash
, type
);
331 memcached_hash_t
memcached_behavior_get_key_hash(memcached_st
*ptr
)
336 memcached_return_t
memcached_behavior_set_distribution_hash(memcached_st
*ptr
, memcached_hash_t type
)
338 return set_hash(&ptr
->distribution_hash
, type
);
341 memcached_hash_t
memcached_behavior_get_distribution_hash(memcached_st
*ptr
)
343 return ptr
->distribution_hash
;