Code cleanup in memcached_io_close
[m6w6/libmemcached] / libmemcached / 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 static bool set_flag(uint64_t data)
19 {
20 // Wordy :)
21 return data ? true : false;
22 }
23
24 static memcached_return_t set_hash(memcached_hash_t *store, memcached_hash_t type)
25 {
26 #ifndef HAVE_HSIEH_HASH
27 if (type == MEMCACHED_HASH_HSIEH)
28 return MEMCACHED_FAILURE;
29 #endif
30 if (type < MEMCACHED_HASH_MAX)
31 {
32 *store= type;
33 }
34 else
35 {
36 return MEMCACHED_FAILURE;
37 }
38
39 return MEMCACHED_SUCCESS;
40 }
41 /*
42 This function is used to modify the behavior of running client.
43
44 We quit all connections so we can reset the sockets.
45 */
46
47 memcached_return_t memcached_behavior_set(memcached_st *ptr,
48 const memcached_behavior_t flag,
49 uint64_t data)
50 {
51 switch (flag)
52 {
53 case MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS:
54 ptr->number_of_replicas= (uint32_t)data;
55 break;
56 case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK:
57 ptr->io_msg_watermark= (uint32_t) data;
58 break;
59 case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK:
60 ptr->io_bytes_watermark= (uint32_t)data;
61 break;
62 case MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH:
63 ptr->io_key_prefetch = (uint32_t)data;
64 break;
65 case MEMCACHED_BEHAVIOR_SND_TIMEOUT:
66 ptr->snd_timeout= (int32_t)data;
67 break;
68 case MEMCACHED_BEHAVIOR_RCV_TIMEOUT:
69 ptr->rcv_timeout= (int32_t)data;
70 break;
71 case MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT:
72 ptr->server_failure_limit= (uint32_t)data;
73 break;
74 case MEMCACHED_BEHAVIOR_BINARY_PROTOCOL:
75 if (data)
76 {
77 ptr->flags.verify_key= false;
78 }
79 ptr->flags.binary_protocol= set_flag(data);
80 break;
81 case MEMCACHED_BEHAVIOR_SUPPORT_CAS:
82 ptr->flags.support_cas= set_flag(data);
83 break;
84 case MEMCACHED_BEHAVIOR_NO_BLOCK:
85 ptr->flags.no_block= set_flag(data);
86 memcached_quit(ptr);
87 break;
88 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS:
89 ptr->flags.buffer_requests= set_flag(data);
90 memcached_quit(ptr);
91 break;
92 case MEMCACHED_BEHAVIOR_USE_UDP:
93 if (ptr->number_of_hosts)
94 {
95 return MEMCACHED_FAILURE;
96 }
97 ptr->flags.use_udp= set_flag(data);
98 if (data)
99 {
100 ptr->flags.no_reply= set_flag(data);
101 }
102 break;
103 case MEMCACHED_BEHAVIOR_TCP_NODELAY:
104 ptr->flags.tcp_nodelay= set_flag(data);
105 memcached_quit(ptr);
106 break;
107 case MEMCACHED_BEHAVIOR_DISTRIBUTION:
108 return memcached_behavior_set_distribution(ptr, (memcached_server_distribution_t)data);
109 case MEMCACHED_BEHAVIOR_KETAMA:
110 {
111 if (data)
112 {
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);
116 }
117 else
118 {
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);
122 }
123
124 break;
125 }
126 case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED:
127 {
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);
131 /**
132 @note We try to keep the same distribution going. This should be deprecated and rewritten.
133 */
134 return memcached_behavior_set_distribution(ptr, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA);
135 }
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);
142 memcached_quit(ptr);
143 break;
144 case MEMCACHED_BEHAVIOR_VERIFY_KEY:
145 if (ptr->flags.binary_protocol)
146 return MEMCACHED_FAILURE;
147 ptr->flags.verify_key= set_flag(data);
148 break;
149 case MEMCACHED_BEHAVIOR_SORT_HOSTS:
150 {
151 ptr->flags.use_sort_hosts= set_flag(data);
152 run_distribution(ptr);
153
154 break;
155 }
156 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT:
157 ptr->poll_timeout= (int32_t)data;
158 break;
159 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT:
160 ptr->connect_timeout= (int32_t)data;
161 break;
162 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT:
163 ptr->retry_timeout= (int32_t)data;
164 break;
165 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE:
166 ptr->send_size= (int32_t)data;
167 memcached_quit(ptr);
168 break;
169 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE:
170 ptr->recv_size= (int32_t)data;
171 memcached_quit(ptr);
172 break;
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);
177 break;
178 case MEMCACHED_BEHAVIOR_NOREPLY:
179 ptr->flags.no_reply= set_flag(data);
180 break;
181 case MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS:
182 ptr->flags.auto_eject_hosts= set_flag(data);
183 break;
184 case MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ:
185 srandom((uint32_t) time(NULL));
186 ptr->flags.randomize_replica_read= set_flag(data);
187 break;
188 case MEMCACHED_BEHAVIOR_MAX:
189 default:
190 /* Shouldn't get here */
191 WATCHPOINT_ASSERT(0);
192 return MEMCACHED_FAILURE;
193 }
194
195 return MEMCACHED_SUCCESS;
196 }
197
198 uint64_t memcached_behavior_get(memcached_st *ptr,
199 const 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_HASH:
234 return ptr->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:
252 {
253 int sock_size;
254 socklen_t sock_length= sizeof(int);
255
256 /* REFACTOR */
257 /* We just try the first host, and if it is down we return zero */
258 if ((memcached_connect(&ptr->hosts[0])) != MEMCACHED_SUCCESS)
259 return 0;
260
261 if (getsockopt(ptr->hosts[0].fd, SOL_SOCKET,
262 SO_SNDBUF, &sock_size, &sock_length))
263 return 0; /* Zero means error */
264
265 return (uint64_t) sock_size;
266 }
267 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE:
268 {
269 int sock_size;
270 socklen_t sock_length= sizeof(int);
271
272 /**
273 @note REFACTOR
274 */
275 /* We just try the first host, and if it is down we return zero */
276 if ((memcached_connect(&ptr->hosts[0])) != MEMCACHED_SUCCESS)
277 return 0;
278
279 if (getsockopt(ptr->hosts[0].fd, SOL_SOCKET,
280 SO_RCVBUF, &sock_size, &sock_length))
281 return 0; /* Zero means error */
282
283 return (uint64_t) sock_size;
284 }
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:
296 default:
297 WATCHPOINT_ASSERT(0); /* Programming mistake if it gets this far */
298 return 0;
299 }
300
301 /* NOTREACHED */
302 }
303
304
305 memcached_return_t memcached_behavior_set_distribution(memcached_st *ptr, memcached_server_distribution_t type)
306 {
307 if (type < MEMCACHED_DISTRIBUTION_CONSISTENT_MAX)
308 {
309 ptr->distribution= type;
310 run_distribution(ptr);
311 }
312 else
313 {
314 return MEMCACHED_FAILURE;
315 }
316
317 return MEMCACHED_SUCCESS;
318 }
319
320
321 memcached_server_distribution_t memcached_behavior_get_distribution(memcached_st *ptr)
322 {
323 return ptr->distribution;
324 }
325
326 memcached_return_t memcached_behavior_set_key_hash(memcached_st *ptr, memcached_hash_t type)
327 {
328 return set_hash(&ptr->hash, type);
329 }
330
331 memcached_hash_t memcached_behavior_get_key_hash(memcached_st *ptr)
332 {
333 return ptr->hash;
334 }
335
336 memcached_return_t memcached_behavior_set_distribution_hash(memcached_st *ptr, memcached_hash_t type)
337 {
338 return set_hash(&ptr->distribution_hash, type);
339 }
340
341 memcached_hash_t memcached_behavior_get_distribution_hash(memcached_st *ptr)
342 {
343 return ptr->distribution_hash;
344 }