37c9f5299db70e05e9ecd1bbb0af164f44d91f69
[m6w6/libmemcached] / libmemcached / memcached_behavior.c
1 /* LibMemcached
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary: Change the behavior of the memcached connection.
9 *
10 */
11
12 #include "common.h"
13 #include <time.h>
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <netinet/tcp.h>
17
18 /*
19 This function is used to modify the behavior of running client.
20
21 We quit all connections so we can reset the sockets.
22 */
23
24 memcached_return_t memcached_behavior_set(memcached_st *ptr,
25 memcached_behavior_t flag,
26 uint64_t data)
27 {
28 switch (flag)
29 {
30 case MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS:
31 ptr->number_of_replicas= (uint32_t)data;
32 break;
33 case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK:
34 ptr->io_msg_watermark= (uint32_t) data;
35 break;
36 case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK:
37 ptr->io_bytes_watermark= (uint32_t)data;
38 break;
39 case MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH:
40 ptr->io_key_prefetch = (uint32_t)data;
41 break;
42 case MEMCACHED_BEHAVIOR_SND_TIMEOUT:
43 ptr->snd_timeout= (int32_t)data;
44 break;
45 case MEMCACHED_BEHAVIOR_RCV_TIMEOUT:
46 ptr->rcv_timeout= (int32_t)data;
47 break;
48 case MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT:
49 ptr->server_failure_limit= (uint32_t)data;
50 break;
51 case MEMCACHED_BEHAVIOR_BINARY_PROTOCOL:
52 if (data)
53 ptr->flags.verify_key= false;
54
55 ptr->flags.binary_protocol= data ? true : false;
56 break;
57 case MEMCACHED_BEHAVIOR_SUPPORT_CAS:
58 ptr->flags.support_cas= data ? true: false;
59 break;
60 case MEMCACHED_BEHAVIOR_NO_BLOCK:
61 ptr->flags.no_block= data ? true: false;
62 memcached_quit(ptr);
63 break;
64 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS:
65 ptr->flags.buffer_requests= data ? true : false;
66 memcached_quit(ptr);
67 break;
68 case MEMCACHED_BEHAVIOR_USE_UDP:
69 if (ptr->number_of_hosts)
70 return MEMCACHED_FAILURE;
71 ptr->flags.use_udp= data ? true : false;
72
73 if (data)
74 ptr->flags.no_reply= data ? true : false;
75 break;
76
77 case MEMCACHED_BEHAVIOR_TCP_NODELAY:
78 ptr->flags.tcp_nodelay= data ? true : false;
79 memcached_quit(ptr);
80 break;
81 case MEMCACHED_BEHAVIOR_DISTRIBUTION:
82 {
83 ptr->distribution= (memcached_server_distribution_t)(data);
84 if (ptr->distribution == MEMCACHED_DISTRIBUTION_RANDOM)
85 {
86 srandom((uint32_t) time(NULL));
87 }
88 run_distribution(ptr);
89 break;
90 }
91 case MEMCACHED_BEHAVIOR_KETAMA:
92 {
93 if (data)
94 {
95 ptr->hash= MEMCACHED_HASH_MD5;
96 ptr->distribution= MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA;
97 }
98 else
99 {
100 ptr->hash= 0;
101 ptr->distribution= 0;
102 }
103 run_distribution(ptr);
104 break;
105 }
106 case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED:
107 {
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);
112 break;
113 }
114 case MEMCACHED_BEHAVIOR_KETAMA_COMPAT_MODE:
115 switch (data)
116 {
117 case MEMCACHED_KETAMA_COMPAT_LIBMEMCACHED:
118 ptr->hash= MEMCACHED_HASH_MD5;
119 ptr->distribution= MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA;
120 break;
121 case MEMCACHED_KETAMA_COMPAT_SPY:
122 ptr->hash= MEMCACHED_HASH_MD5;
123 ptr->distribution= MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY;
124 break;
125 default:
126 return MEMCACHED_FAILURE;
127 }
128 run_distribution(ptr);
129 break;
130 case MEMCACHED_BEHAVIOR_HASH:
131 #ifndef HAVE_HSIEH_HASH
132 if ((memcached_hash_t)(data) == MEMCACHED_HASH_HSIEH)
133 return MEMCACHED_FAILURE;
134 #endif
135 ptr->hash= (memcached_hash_t)(data);
136 break;
137 case MEMCACHED_BEHAVIOR_KETAMA_HASH:
138 ptr->hash_continuum= (memcached_hash_t)(data);
139 run_distribution(ptr);
140 break;
141 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS:
142 ptr->flags.use_cache_lookups= data ? true : false;
143 memcached_quit(ptr);
144 break;
145 case MEMCACHED_BEHAVIOR_VERIFY_KEY:
146 if (ptr->flags.binary_protocol)
147 break;
148 ptr->flags.verify_key= data ? true : false;
149 break;
150 case MEMCACHED_BEHAVIOR_SORT_HOSTS:
151 {
152 ptr->flags.use_sort_hosts= data ? true : false;
153 run_distribution(ptr);
154
155 break;
156 }
157 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT:
158 ptr->poll_timeout= (int32_t)data;
159 break;
160 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT:
161 ptr->connect_timeout= (int32_t)data;
162 break;
163 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT:
164 ptr->retry_timeout= (int32_t)data;
165 break;
166 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE:
167 ptr->send_size= (int32_t)data;
168 memcached_quit(ptr);
169 break;
170 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE:
171 ptr->recv_size= (int32_t)data;
172 memcached_quit(ptr);
173 break;
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;
178 break;
179 case MEMCACHED_BEHAVIOR_NOREPLY:
180 ptr->flags.no_reply= data ? true : false;
181 break;
182 case MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS:
183 ptr->flags.auto_eject_hosts= data ? true : false;
184 break;
185 case MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ:
186 srandom((uint32_t) time(NULL));
187 ptr->flags.randomize_replica_read= data ? true : false;
188 break;
189 default:
190 /* Shouldn't get here */
191 WATCHPOINT_ASSERT(flag);
192 break;
193 }
194
195 return MEMCACHED_SUCCESS;
196 }
197
198 uint64_t memcached_behavior_get(memcached_st *ptr,
199 memcached_behavior_t flag)
200 {
201 switch (flag)
202 {
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)
235 {
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:
243 default:
244 return (uint64_t)-1;
245 }
246 /* NOTREACHED */
247 case MEMCACHED_BEHAVIOR_HASH:
248 return ptr->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:
266 {
267 int sock_size;
268 socklen_t sock_length= sizeof(int);
269
270 /* REFACTOR */
271 /* We just try the first host, and if it is down we return zero */
272 if ((memcached_connect(&ptr->hosts[0])) != MEMCACHED_SUCCESS)
273 return 0;
274
275 if (getsockopt(ptr->hosts[0].fd, SOL_SOCKET,
276 SO_SNDBUF, &sock_size, &sock_length))
277 return 0; /* Zero means error */
278
279 return (uint64_t) sock_size;
280 }
281 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE:
282 {
283 int sock_size;
284 socklen_t sock_length= sizeof(int);
285
286 /* REFACTOR */
287 /* We just try the first host, and if it is down we return zero */
288 if ((memcached_connect(&ptr->hosts[0])) != MEMCACHED_SUCCESS)
289 return 0;
290
291 if (getsockopt(ptr->hosts[0].fd, SOL_SOCKET,
292 SO_RCVBUF, &sock_size, &sock_length))
293 return 0; /* Zero means error */
294
295 return (uint64_t) sock_size;
296 }
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;
307 default:
308 WATCHPOINT_ASSERT(flag);
309 break;
310 }
311
312 WATCHPOINT_ASSERT(0); /* Programming mistake if it gets this far */
313 return 0;
314 }