Fix IO call to use bool.
[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 /*
25 This function is used to modify the behavior of running client.
26
27 We quit all connections so we can reset the sockets.
28 */
29
30 memcached_return_t memcached_behavior_set(memcached_st *ptr,
31 const memcached_behavior_t flag,
32 uint64_t data)
33 {
34 switch (flag)
35 {
36 case MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS:
37 ptr->number_of_replicas= (uint32_t)data;
38 break;
39 case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK:
40 ptr->io_msg_watermark= (uint32_t) data;
41 break;
42 case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK:
43 ptr->io_bytes_watermark= (uint32_t)data;
44 break;
45 case MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH:
46 ptr->io_key_prefetch = (uint32_t)data;
47 break;
48 case MEMCACHED_BEHAVIOR_SND_TIMEOUT:
49 ptr->snd_timeout= (int32_t)data;
50 break;
51 case MEMCACHED_BEHAVIOR_RCV_TIMEOUT:
52 ptr->rcv_timeout= (int32_t)data;
53 break;
54 case MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT:
55 ptr->server_failure_limit= (uint32_t)data;
56 break;
57 case MEMCACHED_BEHAVIOR_BINARY_PROTOCOL:
58 if (data)
59 {
60 ptr->flags.verify_key= false;
61 }
62 ptr->flags.binary_protocol= set_flag(data);
63 break;
64 case MEMCACHED_BEHAVIOR_SUPPORT_CAS:
65 ptr->flags.support_cas= set_flag(data);
66 break;
67 case MEMCACHED_BEHAVIOR_NO_BLOCK:
68 ptr->flags.no_block= set_flag(data);
69 memcached_quit(ptr);
70 break;
71 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS:
72 ptr->flags.buffer_requests= set_flag(data);
73 memcached_quit(ptr);
74 break;
75 case MEMCACHED_BEHAVIOR_USE_UDP:
76 if (memcached_server_count(ptr))
77 {
78 return MEMCACHED_FAILURE;
79 }
80 ptr->flags.use_udp= set_flag(data);
81 if (data)
82 {
83 ptr->flags.no_reply= set_flag(data);
84 }
85 break;
86 case MEMCACHED_BEHAVIOR_TCP_NODELAY:
87 ptr->flags.tcp_nodelay= set_flag(data);
88 memcached_quit(ptr);
89 break;
90 case MEMCACHED_BEHAVIOR_DISTRIBUTION:
91 return memcached_behavior_set_distribution(ptr, (memcached_server_distribution_t)data);
92 case MEMCACHED_BEHAVIOR_KETAMA:
93 {
94 if (data)
95 {
96 (void)memcached_behavior_set_key_hash(ptr, MEMCACHED_HASH_MD5);
97 (void)memcached_behavior_set_distribution_hash(ptr, MEMCACHED_HASH_MD5);
98 (void)memcached_behavior_set_distribution(ptr, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA);
99 }
100 else
101 {
102 (void)memcached_behavior_set_key_hash(ptr, MEMCACHED_HASH_DEFAULT);
103 (void)memcached_behavior_set_distribution_hash(ptr, MEMCACHED_HASH_DEFAULT);
104 (void)memcached_behavior_set_distribution(ptr, MEMCACHED_DISTRIBUTION_MODULA);
105 }
106
107 break;
108 }
109 case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED:
110 {
111 (void)memcached_behavior_set_key_hash(ptr, MEMCACHED_HASH_MD5);
112 (void)memcached_behavior_set_distribution_hash(ptr, MEMCACHED_HASH_MD5);
113 ptr->flags.ketama_weighted= set_flag(data);
114 /**
115 @note We try to keep the same distribution going. This should be deprecated and rewritten.
116 */
117 return memcached_behavior_set_distribution(ptr, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA);
118 }
119 case MEMCACHED_BEHAVIOR_HASH:
120 return memcached_behavior_set_key_hash(ptr, (memcached_hash_t)(data));
121 case MEMCACHED_BEHAVIOR_KETAMA_HASH:
122 return memcached_behavior_set_distribution_hash(ptr, (memcached_hash_t)(data));
123 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS:
124 ptr->flags.use_cache_lookups= set_flag(data);
125 memcached_quit(ptr);
126 break;
127 case MEMCACHED_BEHAVIOR_VERIFY_KEY:
128 if (ptr->flags.binary_protocol)
129 return MEMCACHED_FAILURE;
130 ptr->flags.verify_key= set_flag(data);
131 break;
132 case MEMCACHED_BEHAVIOR_SORT_HOSTS:
133 {
134 ptr->flags.use_sort_hosts= set_flag(data);
135 run_distribution(ptr);
136
137 break;
138 }
139 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT:
140 ptr->poll_timeout= (int32_t)data;
141 break;
142 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT:
143 ptr->connect_timeout= (int32_t)data;
144 break;
145 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT:
146 ptr->retry_timeout= (int32_t)data;
147 break;
148 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE:
149 ptr->send_size= (int32_t)data;
150 memcached_quit(ptr);
151 break;
152 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE:
153 ptr->recv_size= (int32_t)data;
154 memcached_quit(ptr);
155 break;
156 case MEMCACHED_BEHAVIOR_USER_DATA:
157 return MEMCACHED_FAILURE;
158 case MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY:
159 ptr->flags.hash_with_prefix_key= set_flag(data);
160 break;
161 case MEMCACHED_BEHAVIOR_NOREPLY:
162 ptr->flags.no_reply= set_flag(data);
163 break;
164 case MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS:
165 ptr->flags.auto_eject_hosts= set_flag(data);
166 break;
167 case MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ:
168 srandom((uint32_t) time(NULL));
169 ptr->flags.randomize_replica_read= set_flag(data);
170 break;
171 case MEMCACHED_BEHAVIOR_CORK:
172 {
173 memcached_server_instance_st *instance;
174 bool action= set_flag(data);
175
176 if (action == false)
177 {
178 ptr->flags.cork= set_flag(false);
179 return MEMCACHED_SUCCESS;
180 }
181
182 instance= memcached_server_instance_fetch(ptr, 0);
183 if (! instance)
184 return MEMCACHED_NO_SERVERS;
185
186
187 /* We just try the first host, and if it is down we return zero */
188 memcached_return_t rc;
189 rc= memcached_connect(instance);
190 if (rc != MEMCACHED_SUCCESS)
191 {
192 return rc;
193 }
194
195 /* Now we test! */
196 memcached_ternary_t enabled;
197 enabled= test_cork(instance, true);
198
199 switch (enabled)
200 {
201 case MEM_FALSE:
202 return ptr->cached_errno ? MEMCACHED_ERRNO : MEMCACHED_FAILURE ;
203 case MEM_TRUE:
204 {
205 enabled= test_cork(instance, false);
206
207 if (enabled == false) // Possible bug in OS?
208 {
209 memcached_quit_server(instance, false); // We should reset everything on this error.
210 return MEMCACHED_ERRNO; // Errno will be true because we will have already set it.
211 }
212 ptr->flags.cork= true;
213 ptr->flags.tcp_nodelay= true;
214 memcached_quit(ptr); // We go on and reset the connections.
215 }
216 break;
217 case MEM_NOT:
218 default:
219 return MEMCACHED_NOT_SUPPORTED;
220 }
221 }
222 break;
223 case MEMCACHED_BEHAVIOR_MAX:
224 default:
225 /* Shouldn't get here */
226 WATCHPOINT_ASSERT(0);
227 return MEMCACHED_FAILURE;
228 }
229
230 return MEMCACHED_SUCCESS;
231 }
232
233 uint64_t memcached_behavior_get(memcached_st *ptr,
234 const memcached_behavior_t flag)
235 {
236 switch (flag)
237 {
238 case MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS:
239 return ptr->number_of_replicas;
240 case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK:
241 return ptr->io_msg_watermark;
242 case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK:
243 return ptr->io_bytes_watermark;
244 case MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH:
245 return ptr->io_key_prefetch;
246 case MEMCACHED_BEHAVIOR_BINARY_PROTOCOL:
247 return ptr->flags.binary_protocol;
248 case MEMCACHED_BEHAVIOR_SUPPORT_CAS:
249 return ptr->flags.support_cas;
250 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS:
251 return ptr->flags.use_cache_lookups;
252 case MEMCACHED_BEHAVIOR_NO_BLOCK:
253 return ptr->flags.no_block;
254 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS:
255 return ptr->flags.buffer_requests;
256 case MEMCACHED_BEHAVIOR_USE_UDP:
257 return ptr->flags.use_udp;
258 case MEMCACHED_BEHAVIOR_TCP_NODELAY:
259 return ptr->flags.tcp_nodelay;
260 case MEMCACHED_BEHAVIOR_VERIFY_KEY:
261 return ptr->flags.verify_key;
262 case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED:
263 return ptr->flags.ketama_weighted;
264 case MEMCACHED_BEHAVIOR_DISTRIBUTION:
265 return ptr->distribution;
266 case MEMCACHED_BEHAVIOR_KETAMA:
267 return (ptr->distribution == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA) ? (uint64_t) 1 : 0;
268 case MEMCACHED_BEHAVIOR_HASH:
269 return hashkit_get_function(&ptr->hashkit);
270 case MEMCACHED_BEHAVIOR_KETAMA_HASH:
271 return hashkit_get_function(&ptr->distribution_hashkit);
272 case MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT:
273 return ptr->server_failure_limit;
274 case MEMCACHED_BEHAVIOR_SORT_HOSTS:
275 return ptr->flags.use_sort_hosts;
276 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT:
277 return (uint64_t)ptr->poll_timeout;
278 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT:
279 return (uint64_t)ptr->connect_timeout;
280 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT:
281 return (uint64_t)ptr->retry_timeout;
282 case MEMCACHED_BEHAVIOR_SND_TIMEOUT:
283 return (uint64_t)ptr->snd_timeout;
284 case MEMCACHED_BEHAVIOR_RCV_TIMEOUT:
285 return (uint64_t)ptr->rcv_timeout;
286 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE:
287 {
288 int sock_size= 0;
289 socklen_t sock_length= sizeof(int);
290 memcached_server_instance_st *instance;
291
292 if (ptr->send_size != -1) // If value is -1 then we are using the default
293 return (uint64_t) ptr->send_size;
294
295 instance= memcached_server_instance_fetch(ptr, 0);
296
297 if (instance) // If we have an instance we test, otherwise we just set and pray
298 {
299 /* REFACTOR */
300 /* We just try the first host, and if it is down we return zero */
301 if ((memcached_connect(instance)) != MEMCACHED_SUCCESS)
302 return 0;
303
304 if (getsockopt(instance->fd, SOL_SOCKET,
305 SO_SNDBUF, &sock_size, &sock_length))
306 return 0; /* Zero means error */
307 }
308
309 return (uint64_t) sock_size;
310 }
311 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE:
312 {
313 int sock_size= 0;
314 socklen_t sock_length= sizeof(int);
315 memcached_server_instance_st *instance;
316
317 if (ptr->recv_size != -1) // If value is -1 then we are using the default
318 return (uint64_t) ptr->recv_size;
319
320 instance= memcached_server_instance_fetch(ptr, 0);
321
322 /**
323 @note REFACTOR
324 */
325 if (instance)
326 {
327 /* We just try the first host, and if it is down we return zero */
328 if ((memcached_connect(instance)) != MEMCACHED_SUCCESS)
329 return 0;
330
331 if (getsockopt(instance->fd, SOL_SOCKET,
332 SO_RCVBUF, &sock_size, &sock_length))
333 return 0; /* Zero means error */
334
335 }
336
337 return (uint64_t) sock_size;
338 }
339 case MEMCACHED_BEHAVIOR_USER_DATA:
340 return MEMCACHED_FAILURE;
341 case MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY:
342 return ptr->flags.hash_with_prefix_key;
343 case MEMCACHED_BEHAVIOR_NOREPLY:
344 return ptr->flags.no_reply;
345 case MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS:
346 return ptr->flags.auto_eject_hosts;
347 case MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ:
348 return ptr->flags.randomize_replica_read;
349 case MEMCACHED_BEHAVIOR_CORK:
350 return ptr->flags.cork;
351 case MEMCACHED_BEHAVIOR_MAX:
352 default:
353 WATCHPOINT_ASSERT(0); /* Programming mistake if it gets this far */
354 return 0;
355 }
356
357 /* NOTREACHED */
358 }
359
360
361 memcached_return_t memcached_behavior_set_distribution(memcached_st *ptr, memcached_server_distribution_t type)
362 {
363 if (type < MEMCACHED_DISTRIBUTION_CONSISTENT_MAX)
364 {
365 ptr->distribution= type;
366 run_distribution(ptr);
367 }
368 else
369 {
370 return MEMCACHED_FAILURE;
371 }
372
373 return MEMCACHED_SUCCESS;
374 }
375
376
377 memcached_server_distribution_t memcached_behavior_get_distribution(memcached_st *ptr)
378 {
379 return ptr->distribution;
380 }
381
382 memcached_return_t memcached_behavior_set_key_hash(memcached_st *ptr, memcached_hash_t type)
383 {
384 hashkit_return_t rc;
385 rc= hashkit_set_function(&ptr->hashkit, (hashkit_hash_algorithm_t)type);
386
387 return rc == HASHKIT_SUCCESS ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
388 }
389
390 memcached_hash_t memcached_behavior_get_key_hash(memcached_st *ptr)
391 {
392 return (memcached_hash_t)hashkit_get_function(&ptr->hashkit);
393 }
394
395 memcached_return_t memcached_behavior_set_distribution_hash(memcached_st *ptr, memcached_hash_t type)
396 {
397 hashkit_return_t rc;
398 rc= hashkit_set_function(&ptr->distribution_hashkit, (hashkit_hash_algorithm_t)type);
399
400 return rc == HASHKIT_SUCCESS ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
401 }
402
403 memcached_hash_t memcached_behavior_get_distribution_hash(memcached_st *ptr)
404 {
405 return (memcached_hash_t)hashkit_get_function(&ptr->distribution_hashkit);
406 }