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