Merge from trunk.
[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= (uint32_t) data;
32 break;
33 case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK:
34 ptr->io_bytes_watermark= (uint32_t)data;
35 break;
36 case MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH:
37 ptr->io_key_prefetch = (uint32_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((uint32_t) 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= MEM_NO_BLOCK;
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) ? (uint64_t) 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 return (uint64_t)ptr->poll_timeout;
229 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT:
230 return (uint64_t)ptr->connect_timeout;
231 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT:
232 return (uint64_t)ptr->retry_timeout;
233 case MEMCACHED_BEHAVIOR_SND_TIMEOUT:
234 return (uint64_t)ptr->snd_timeout;
235 case MEMCACHED_BEHAVIOR_RCV_TIMEOUT:
236 return (uint64_t)ptr->rcv_timeout;
237 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE:
238 {
239 int sock_size;
240 socklen_t sock_length= sizeof(int);
241
242 /* REFACTOR */
243 /* We just try the first host, and if it is down we return zero */
244 if ((memcached_connect(&ptr->hosts[0])) != MEMCACHED_SUCCESS)
245 return 0;
246
247 if (getsockopt(ptr->hosts[0].fd, SOL_SOCKET,
248 SO_SNDBUF, &sock_size, &sock_length))
249 return 0; /* Zero means error */
250
251 return (uint64_t) sock_size;
252 }
253 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE:
254 {
255 int sock_size;
256 socklen_t sock_length= sizeof(int);
257
258 /* REFACTOR */
259 /* We just try the first host, and if it is down we return zero */
260 if ((memcached_connect(&ptr->hosts[0])) != MEMCACHED_SUCCESS)
261 return 0;
262
263 if (getsockopt(ptr->hosts[0].fd, SOL_SOCKET,
264 SO_RCVBUF, &sock_size, &sock_length))
265 return 0; /* Zero means error */
266
267 return (uint64_t) sock_size;
268 }
269 case MEMCACHED_BEHAVIOR_USER_DATA:
270 return MEMCACHED_FAILURE;
271 case MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY:
272 temp_flag= MEM_HASH_WITH_PREFIX_KEY;
273 break;
274 case MEMCACHED_BEHAVIOR_NOREPLY:
275 temp_flag= MEM_NOREPLY;
276 break;
277 case MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS:
278 temp_flag= MEM_AUTO_EJECT_HOSTS;
279 break;
280 default:
281 WATCHPOINT_ASSERT(flag);
282 break;
283 }
284
285 WATCHPOINT_ASSERT(temp_flag); /* Programming mistake if it gets this far */
286 if (ptr->flags & temp_flag)
287 return 1;
288 else
289 return 0;
290 }