Work inspired by matt knox
[m6w6/libmemcached] / libmemcached / memcached_behavior.c
1 #include "common.h"
2 #include <time.h>
3 #include <sys/types.h>
4 #include <sys/socket.h>
5 #include <netinet/tcp.h>
6
7 /*
8 This function is used to modify the behavior of running client.
9
10 We quit all connections so we can reset the sockets.
11 */
12
13 static void set_behavior_flag(memcached_st *ptr, memcached_flags temp_flag, uint64_t data)
14 {
15 if (data)
16 ptr->flags|= temp_flag;
17 else
18 ptr->flags&= ~temp_flag;
19 }
20
21 memcached_return memcached_behavior_set(memcached_st *ptr,
22 memcached_behavior flag,
23 uint64_t data)
24 {
25 switch (flag)
26 {
27 case MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS:
28 ptr->number_of_replicas= (uint32_t)data;
29 break;
30 case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK:
31 ptr->io_msg_watermark= (int32_t)data;
32 break;
33 case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK:
34 ptr->io_bytes_watermark= (int32_t)data;
35 break;
36 case MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH:
37 ptr->io_key_prefetch = (int32_t)data;
38 break;
39 case MEMCACHED_BEHAVIOR_SND_TIMEOUT:
40 ptr->snd_timeout= (int32_t)data;
41 break;
42 case MEMCACHED_BEHAVIOR_RCV_TIMEOUT:
43 ptr->rcv_timeout= (int32_t)data;
44 break;
45 case MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT:
46 ptr->server_failure_limit= (uint32_t)data;
47 break;
48 case MEMCACHED_BEHAVIOR_BINARY_PROTOCOL:
49 if (data)
50 set_behavior_flag(ptr, MEM_VERIFY_KEY, 0);
51 set_behavior_flag(ptr, MEM_BINARY_PROTOCOL, data);
52 break;
53 case MEMCACHED_BEHAVIOR_SUPPORT_CAS:
54 set_behavior_flag(ptr, MEM_SUPPORT_CAS, data);
55 break;
56 case MEMCACHED_BEHAVIOR_NO_BLOCK:
57 set_behavior_flag(ptr, MEM_NO_BLOCK, data);
58 memcached_quit(ptr);
59 break;
60 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS:
61 set_behavior_flag(ptr, MEM_BUFFER_REQUESTS, data);
62 memcached_quit(ptr);
63 break;
64 case MEMCACHED_BEHAVIOR_USE_UDP:
65 if (ptr->number_of_hosts)
66 return MEMCACHED_FAILURE;
67 set_behavior_flag(ptr, MEM_USE_UDP, data);
68 if (data)
69 set_behavior_flag(ptr,MEM_NOREPLY,data);
70 break;
71 case MEMCACHED_BEHAVIOR_TCP_NODELAY:
72 set_behavior_flag(ptr, MEM_TCP_NODELAY, data);
73 memcached_quit(ptr);
74 break;
75 case MEMCACHED_BEHAVIOR_DISTRIBUTION:
76 {
77 ptr->distribution= (memcached_server_distribution)(data);
78 if (ptr->distribution == MEMCACHED_DISTRIBUTION_RANDOM)
79 {
80 srandom(time(NULL));
81 }
82 run_distribution(ptr);
83 break;
84 }
85 case MEMCACHED_BEHAVIOR_KETAMA:
86 {
87 if (data)
88 {
89 ptr->hash= MEMCACHED_HASH_MD5;
90 ptr->distribution= MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA;
91 }
92 else
93 {
94 ptr->hash= 0;
95 ptr->distribution= 0;
96 }
97 run_distribution(ptr);
98 break;
99 }
100 case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED:
101 {
102 ptr->hash= MEMCACHED_HASH_MD5;
103 ptr->distribution= MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA;
104 set_behavior_flag(ptr, MEM_KETAMA_WEIGHTED, data);
105 run_distribution(ptr);
106 break;
107 }
108 case MEMCACHED_BEHAVIOR_HASH:
109 #ifndef HAVE_HSIEH_HASH
110 if ((memcached_hash)(data) == MEMCACHED_HASH_HSIEH)
111 return MEMCACHED_FAILURE;
112 #endif
113 ptr->hash= (memcached_hash)(data);
114 break;
115 case MEMCACHED_BEHAVIOR_KETAMA_HASH:
116 ptr->hash_continuum= (memcached_hash)(data);
117 run_distribution(ptr);
118 break;
119 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS:
120 set_behavior_flag(ptr, MEM_USE_CACHE_LOOKUPS, data);
121 memcached_quit(ptr);
122 break;
123 case MEMCACHED_BEHAVIOR_VERIFY_KEY:
124 if (ptr->flags & MEM_BINARY_PROTOCOL)
125 break;
126 set_behavior_flag(ptr, MEM_VERIFY_KEY, data);
127 break;
128 case MEMCACHED_BEHAVIOR_SORT_HOSTS:
129 {
130 set_behavior_flag(ptr, MEM_USE_SORT_HOSTS, data);
131 run_distribution(ptr);
132
133 break;
134 }
135 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT:
136 ptr->poll_timeout= (int32_t)data;
137 break;
138 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT:
139 ptr->connect_timeout= (int32_t)data;
140 break;
141 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT:
142 ptr->retry_timeout= (int32_t)data;
143 break;
144 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE:
145 ptr->send_size= (int32_t)data;
146 memcached_quit(ptr);
147 break;
148 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE:
149 ptr->recv_size= (int32_t)data;
150 memcached_quit(ptr);
151 break;
152 case MEMCACHED_BEHAVIOR_USER_DATA:
153 return MEMCACHED_FAILURE;
154 case MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY:
155 set_behavior_flag(ptr, MEM_HASH_WITH_PREFIX_KEY, data);
156 break;
157 case MEMCACHED_BEHAVIOR_NOREPLY:
158 set_behavior_flag(ptr, MEM_NOREPLY, data);
159 break;
160 case MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS:
161 set_behavior_flag(ptr, MEM_AUTO_EJECT_HOSTS, data);
162 break;
163 default:
164 /* Shouldn't get here */
165 WATCHPOINT_ASSERT(flag);
166 break;
167 }
168
169 return MEMCACHED_SUCCESS;
170 }
171
172 uint64_t memcached_behavior_get(memcached_st *ptr,
173 memcached_behavior flag)
174 {
175 memcached_flags temp_flag= 0;
176
177 switch (flag)
178 {
179 case MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS:
180 return ptr->number_of_replicas;
181 case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK:
182 return ptr->io_msg_watermark;
183 case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK:
184 return ptr->io_bytes_watermark;
185 case MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH:
186 return ptr->io_key_prefetch;
187 case MEMCACHED_BEHAVIOR_BINARY_PROTOCOL:
188 temp_flag= MEM_BINARY_PROTOCOL;
189 break;
190 case MEMCACHED_BEHAVIOR_SUPPORT_CAS:
191 temp_flag= MEM_SUPPORT_CAS;
192 break;
193 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS:
194 temp_flag= MEM_USE_CACHE_LOOKUPS;
195 break;
196 case MEMCACHED_BEHAVIOR_NO_BLOCK:
197 temp_flag= MEM_NO_BLOCK;
198 break;
199 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS:
200 temp_flag= MEM_BUFFER_REQUESTS;
201 break;
202 case MEMCACHED_BEHAVIOR_USE_UDP:
203 temp_flag= MEM_USE_UDP;
204 break;
205 case MEMCACHED_BEHAVIOR_TCP_NODELAY:
206 temp_flag= MEM_TCP_NODELAY;
207 break;
208 case MEMCACHED_BEHAVIOR_VERIFY_KEY:
209 temp_flag= MEM_VERIFY_KEY;
210 break;
211 case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED:
212 temp_flag= MEM_KETAMA_WEIGHTED;
213 break;
214 case MEMCACHED_BEHAVIOR_DISTRIBUTION:
215 return ptr->distribution;
216 case MEMCACHED_BEHAVIOR_KETAMA:
217 return (ptr->distribution == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA) ? 1 : 0;
218 case MEMCACHED_BEHAVIOR_HASH:
219 return ptr->hash;
220 case MEMCACHED_BEHAVIOR_KETAMA_HASH:
221 return ptr->hash_continuum;
222 case MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT:
223 return ptr->server_failure_limit;
224 case MEMCACHED_BEHAVIOR_SORT_HOSTS:
225 temp_flag= MEM_USE_SORT_HOSTS;
226 break;
227 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT:
228 {
229 return (uint64_t)ptr->poll_timeout;
230 }
231 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT:
232 {
233 return (uint64_t)ptr->connect_timeout;
234 }
235 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT:
236 {
237 return (uint64_t)ptr->retry_timeout;
238 }
239 case MEMCACHED_BEHAVIOR_SND_TIMEOUT:
240 return (uint64_t)ptr->snd_timeout;
241 case MEMCACHED_BEHAVIOR_RCV_TIMEOUT:
242 return (uint64_t)ptr->rcv_timeout;
243 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE:
244 {
245 int sock_size;
246 socklen_t sock_length= sizeof(int);
247
248 /* REFACTOR */
249 /* We just try the first host, and if it is down we return zero */
250 if ((memcached_connect(&ptr->hosts[0])) != MEMCACHED_SUCCESS)
251 return 0;
252
253 if (getsockopt(ptr->hosts[0].fd, SOL_SOCKET,
254 SO_SNDBUF, &sock_size, &sock_length))
255 return 0; /* Zero means error */
256
257 return sock_size;
258 }
259 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE:
260 {
261 int sock_size;
262 socklen_t sock_length= sizeof(int);
263
264 /* REFACTOR */
265 /* We just try the first host, and if it is down we return zero */
266 if ((memcached_connect(&ptr->hosts[0])) != MEMCACHED_SUCCESS)
267 return 0;
268
269 if (getsockopt(ptr->hosts[0].fd, SOL_SOCKET,
270 SO_RCVBUF, &sock_size, &sock_length))
271 return 0; /* Zero means error */
272
273 return sock_size;
274 }
275 case MEMCACHED_BEHAVIOR_USER_DATA:
276 return MEMCACHED_FAILURE;
277 case MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY:
278 temp_flag= MEM_HASH_WITH_PREFIX_KEY;
279 break;
280 case MEMCACHED_BEHAVIOR_NOREPLY:
281 temp_flag= MEM_NOREPLY;
282 break;
283 case MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS:
284 temp_flag= MEM_AUTO_EJECT_HOSTS;
285 break;
286 default:
287 WATCHPOINT_ASSERT(flag);
288 break;
289 }
290
291 WATCHPOINT_ASSERT(temp_flag); /* Programming mistake if it gets this far */
292 if (ptr->flags & temp_flag)
293 return 1;
294 else
295 return 0;
296 }