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_CORK
:
190 memcached_server_instance_st
*instance
;
191 bool action
= set_flag(data
);
195 ptr
->flags
.cork
= set_flag(false);
196 return MEMCACHED_SUCCESS
;
199 instance
= memcached_server_instance_fetch(ptr
, 0);
201 return MEMCACHED_NO_SERVERS
;
204 /* We just try the first host, and if it is down we return zero */
205 memcached_return_t rc
;
206 rc
= memcached_connect(instance
);
207 if (rc
!= MEMCACHED_SUCCESS
)
213 memcached_ternary_t enabled
;
214 enabled
= test_cork(instance
, true);
219 return ptr
->cached_errno
? MEMCACHED_ERRNO
: MEMCACHED_FAILURE
;
222 enabled
= test_cork(instance
, false);
224 if (enabled
== false) // Possible bug in OS?
226 memcached_quit_server(instance
, false); // We should reset everything on this error.
227 return MEMCACHED_ERRNO
; // Errno will be true because we will have already set it.
229 ptr
->flags
.cork
= true;
230 ptr
->flags
.tcp_nodelay
= true;
231 memcached_quit(ptr
); // We go on and reset the connections.
236 return MEMCACHED_NOT_SUPPORTED
;
240 case MEMCACHED_BEHAVIOR_MAX
:
242 /* Shouldn't get here */
243 WATCHPOINT_ASSERT(0);
244 return MEMCACHED_FAILURE
;
247 return MEMCACHED_SUCCESS
;
250 uint64_t memcached_behavior_get(memcached_st
*ptr
,
251 const memcached_behavior_t flag
)
255 case MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
:
256 return ptr
->number_of_replicas
;
257 case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
:
258 return ptr
->io_msg_watermark
;
259 case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK
:
260 return ptr
->io_bytes_watermark
;
261 case MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH
:
262 return ptr
->io_key_prefetch
;
263 case MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
:
264 return ptr
->flags
.binary_protocol
;
265 case MEMCACHED_BEHAVIOR_SUPPORT_CAS
:
266 return ptr
->flags
.support_cas
;
267 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
:
268 return ptr
->flags
.use_cache_lookups
;
269 case MEMCACHED_BEHAVIOR_NO_BLOCK
:
270 return ptr
->flags
.no_block
;
271 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
:
272 return ptr
->flags
.buffer_requests
;
273 case MEMCACHED_BEHAVIOR_USE_UDP
:
274 return ptr
->flags
.use_udp
;
275 case MEMCACHED_BEHAVIOR_TCP_NODELAY
:
276 return ptr
->flags
.tcp_nodelay
;
277 case MEMCACHED_BEHAVIOR_VERIFY_KEY
:
278 return ptr
->flags
.verify_key
;
279 case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
:
280 return ptr
->flags
.ketama_weighted
;
281 case MEMCACHED_BEHAVIOR_DISTRIBUTION
:
282 return ptr
->distribution
;
283 case MEMCACHED_BEHAVIOR_KETAMA
:
284 return (ptr
->distribution
== MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
) ? (uint64_t) 1 : 0;
285 case MEMCACHED_BEHAVIOR_HASH
:
287 case MEMCACHED_BEHAVIOR_KETAMA_HASH
:
288 return ptr
->distribution_hash
;
289 case MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
:
290 return ptr
->server_failure_limit
;
291 case MEMCACHED_BEHAVIOR_SORT_HOSTS
:
292 return ptr
->flags
.use_sort_hosts
;
293 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT
:
294 return (uint64_t)ptr
->poll_timeout
;
295 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
:
296 return (uint64_t)ptr
->connect_timeout
;
297 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
:
298 return (uint64_t)ptr
->retry_timeout
;
299 case MEMCACHED_BEHAVIOR_SND_TIMEOUT
:
300 return (uint64_t)ptr
->snd_timeout
;
301 case MEMCACHED_BEHAVIOR_RCV_TIMEOUT
:
302 return (uint64_t)ptr
->rcv_timeout
;
303 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
:
306 socklen_t sock_length
= sizeof(int);
307 memcached_server_instance_st
*instance
;
309 if (ptr
->send_size
!= -1) // If value is -1 then we are using the default
310 return (uint64_t) ptr
->send_size
;
312 instance
= memcached_server_instance_fetch(ptr
, 0);
314 if (instance
) // If we have an instance we test, otherwise we just set and pray
317 /* We just try the first host, and if it is down we return zero */
318 if ((memcached_connect(instance
)) != MEMCACHED_SUCCESS
)
321 if (getsockopt(instance
->fd
, SOL_SOCKET
,
322 SO_SNDBUF
, &sock_size
, &sock_length
))
323 return 0; /* Zero means error */
326 return (uint64_t) sock_size
;
328 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
:
331 socklen_t sock_length
= sizeof(int);
332 memcached_server_instance_st
*instance
;
334 if (ptr
->recv_size
!= -1) // If value is -1 then we are using the default
335 return (uint64_t) ptr
->recv_size
;
337 instance
= memcached_server_instance_fetch(ptr
, 0);
344 /* We just try the first host, and if it is down we return zero */
345 if ((memcached_connect(instance
)) != MEMCACHED_SUCCESS
)
348 if (getsockopt(instance
->fd
, SOL_SOCKET
,
349 SO_RCVBUF
, &sock_size
, &sock_length
))
350 return 0; /* Zero means error */
354 return (uint64_t) sock_size
;
356 case MEMCACHED_BEHAVIOR_USER_DATA
:
357 return MEMCACHED_FAILURE
;
358 case MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY
:
359 return ptr
->flags
.hash_with_prefix_key
;
360 case MEMCACHED_BEHAVIOR_NOREPLY
:
361 return ptr
->flags
.no_reply
;
362 case MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
:
363 return ptr
->flags
.auto_eject_hosts
;
364 case MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ
:
365 return ptr
->flags
.randomize_replica_read
;
366 case MEMCACHED_BEHAVIOR_CORK
:
367 return ptr
->flags
.cork
;
368 case MEMCACHED_BEHAVIOR_MAX
:
370 WATCHPOINT_ASSERT(0); /* Programming mistake if it gets this far */
378 memcached_return_t
memcached_behavior_set_distribution(memcached_st
*ptr
, memcached_server_distribution_t type
)
380 if (type
< MEMCACHED_DISTRIBUTION_CONSISTENT_MAX
)
382 ptr
->distribution
= type
;
383 run_distribution(ptr
);
387 return MEMCACHED_FAILURE
;
390 return MEMCACHED_SUCCESS
;
394 memcached_server_distribution_t
memcached_behavior_get_distribution(memcached_st
*ptr
)
396 return ptr
->distribution
;
399 memcached_return_t
memcached_behavior_set_key_hash(memcached_st
*ptr
, memcached_hash_t type
)
401 return set_hash(&ptr
->hash
, type
);
404 memcached_hash_t
memcached_behavior_get_key_hash(memcached_st
*ptr
)
409 memcached_return_t
memcached_behavior_set_distribution_hash(memcached_st
*ptr
, memcached_hash_t type
)
411 return set_hash(&ptr
->distribution_hash
, type
);
414 memcached_hash_t
memcached_behavior_get_distribution_hash(memcached_st
*ptr
)
416 return ptr
->distribution_hash
;