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