Fix for issue where stats may not be initialized.
[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 (memcached_server_count(ptr))
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_CORK:
189 {
190 memcached_server_instance_st *instance;
191 bool action= set_flag(data);
192
193 if (action == false)
194 {
195 ptr->flags.cork= set_flag(false);
196 return MEMCACHED_SUCCESS;
197 }
198
199 instance= memcached_server_instance_fetch(ptr, 0);
200 if (! instance)
201 return MEMCACHED_NO_SERVERS;
202
203
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)
208 {
209 return rc;
210 }
211
212 /* Now we test! */
213 memcached_ternary_t enabled;
214 enabled= test_cork(instance, true);
215
216 switch (enabled)
217 {
218 case MEM_FALSE:
219 return ptr->cached_errno ? MEMCACHED_ERRNO : MEMCACHED_FAILURE ;
220 case MEM_TRUE:
221 {
222 enabled= test_cork(instance, false);
223
224 if (enabled == false) // Possible bug in OS?
225 {
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.
228 }
229 ptr->flags.cork= true;
230 ptr->flags.tcp_nodelay= true;
231 memcached_quit(ptr); // We go on and reset the connections.
232 }
233 break;
234 case MEM_NOT:
235 default:
236 return MEMCACHED_NOT_SUPPORTED;
237 }
238 }
239 break;
240 case MEMCACHED_BEHAVIOR_MAX:
241 default:
242 /* Shouldn't get here */
243 WATCHPOINT_ASSERT(0);
244 return MEMCACHED_FAILURE;
245 }
246
247 return MEMCACHED_SUCCESS;
248 }
249
250 uint64_t memcached_behavior_get(memcached_st *ptr,
251 const memcached_behavior_t flag)
252 {
253 switch (flag)
254 {
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:
286 return ptr->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:
304 {
305 int sock_size= 0;
306 socklen_t sock_length= sizeof(int);
307 memcached_server_instance_st *instance;
308
309 if (ptr->send_size != -1) // If value is -1 then we are using the default
310 return (uint64_t) ptr->send_size;
311
312 instance= memcached_server_instance_fetch(ptr, 0);
313
314 if (instance) // If we have an instance we test, otherwise we just set and pray
315 {
316 /* REFACTOR */
317 /* We just try the first host, and if it is down we return zero */
318 if ((memcached_connect(instance)) != MEMCACHED_SUCCESS)
319 return 0;
320
321 if (getsockopt(instance->fd, SOL_SOCKET,
322 SO_SNDBUF, &sock_size, &sock_length))
323 return 0; /* Zero means error */
324 }
325
326 return (uint64_t) sock_size;
327 }
328 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE:
329 {
330 int sock_size= 0;
331 socklen_t sock_length= sizeof(int);
332 memcached_server_instance_st *instance;
333
334 if (ptr->recv_size != -1) // If value is -1 then we are using the default
335 return (uint64_t) ptr->recv_size;
336
337 instance= memcached_server_instance_fetch(ptr, 0);
338
339 /**
340 @note REFACTOR
341 */
342 if (instance)
343 {
344 /* We just try the first host, and if it is down we return zero */
345 if ((memcached_connect(instance)) != MEMCACHED_SUCCESS)
346 return 0;
347
348 if (getsockopt(instance->fd, SOL_SOCKET,
349 SO_RCVBUF, &sock_size, &sock_length))
350 return 0; /* Zero means error */
351
352 }
353
354 return (uint64_t) sock_size;
355 }
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:
369 default:
370 WATCHPOINT_ASSERT(0); /* Programming mistake if it gets this far */
371 return 0;
372 }
373
374 /* NOTREACHED */
375 }
376
377
378 memcached_return_t memcached_behavior_set_distribution(memcached_st *ptr, memcached_server_distribution_t type)
379 {
380 if (type < MEMCACHED_DISTRIBUTION_CONSISTENT_MAX)
381 {
382 ptr->distribution= type;
383 run_distribution(ptr);
384 }
385 else
386 {
387 return MEMCACHED_FAILURE;
388 }
389
390 return MEMCACHED_SUCCESS;
391 }
392
393
394 memcached_server_distribution_t memcached_behavior_get_distribution(memcached_st *ptr)
395 {
396 return ptr->distribution;
397 }
398
399 memcached_return_t memcached_behavior_set_key_hash(memcached_st *ptr, memcached_hash_t type)
400 {
401 return set_hash(&ptr->hash, type);
402 }
403
404 memcached_hash_t memcached_behavior_get_key_hash(memcached_st *ptr)
405 {
406 return ptr->hash;
407 }
408
409 memcached_return_t memcached_behavior_set_distribution_hash(memcached_st *ptr, memcached_hash_t type)
410 {
411 return set_hash(&ptr->distribution_hash, type);
412 }
413
414 memcached_hash_t memcached_behavior_get_distribution_hash(memcached_st *ptr)
415 {
416 return ptr->distribution_hash;
417 }