4 #include <sys/socket.h>
5 #include <netinet/tcp.h>
8 This function is used to modify the behavior of running client.
10 We quit all connections so we can reset the sockets.
13 static void set_behavior_flag(memcached_st
*ptr
, memcached_flags temp_flag
, uint64_t data
)
16 ptr
->flags
|= temp_flag
;
18 ptr
->flags
&= ~temp_flag
;
21 memcached_return
memcached_behavior_set(memcached_st
*ptr
,
22 memcached_behavior flag
,
27 case MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
:
28 ptr
->number_of_replicas
= (uint32_t)data
;
30 case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
:
31 ptr
->io_msg_watermark
= (uint32_t) data
;
33 case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK
:
34 ptr
->io_bytes_watermark
= (uint32_t)data
;
36 case MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH
:
37 ptr
->io_key_prefetch
= (uint32_t)data
;
39 case MEMCACHED_BEHAVIOR_SND_TIMEOUT
:
40 ptr
->snd_timeout
= (int32_t)data
;
42 case MEMCACHED_BEHAVIOR_RCV_TIMEOUT
:
43 ptr
->rcv_timeout
= (int32_t)data
;
45 case MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
:
46 ptr
->server_failure_limit
= (uint32_t)data
;
48 case MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
:
50 set_behavior_flag(ptr
, MEM_VERIFY_KEY
, 0);
51 set_behavior_flag(ptr
, MEM_BINARY_PROTOCOL
, data
);
53 case MEMCACHED_BEHAVIOR_SUPPORT_CAS
:
54 set_behavior_flag(ptr
, MEM_SUPPORT_CAS
, data
);
56 case MEMCACHED_BEHAVIOR_NO_BLOCK
:
57 set_behavior_flag(ptr
, MEM_NO_BLOCK
, data
);
60 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
:
61 set_behavior_flag(ptr
, MEM_BUFFER_REQUESTS
, data
);
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
);
69 set_behavior_flag(ptr
,MEM_NOREPLY
,data
);
71 case MEMCACHED_BEHAVIOR_TCP_NODELAY
:
72 set_behavior_flag(ptr
, MEM_TCP_NODELAY
, data
);
75 case MEMCACHED_BEHAVIOR_DISTRIBUTION
:
77 ptr
->distribution
= (memcached_server_distribution
)(data
);
78 if (ptr
->distribution
== MEMCACHED_DISTRIBUTION_RANDOM
)
80 srandom((uint32_t) time(NULL
));
82 run_distribution(ptr
);
85 case MEMCACHED_BEHAVIOR_KETAMA
:
89 ptr
->hash
= MEMCACHED_HASH_MD5
;
90 ptr
->distribution
= MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
;
97 run_distribution(ptr
);
100 case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
:
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
);
108 case MEMCACHED_BEHAVIOR_HASH
:
109 #ifndef HAVE_HSIEH_HASH
110 if ((memcached_hash
)(data
) == MEMCACHED_HASH_HSIEH
)
111 return MEMCACHED_FAILURE
;
113 ptr
->hash
= (memcached_hash
)(data
);
115 case MEMCACHED_BEHAVIOR_KETAMA_HASH
:
116 ptr
->hash_continuum
= (memcached_hash
)(data
);
117 run_distribution(ptr
);
119 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
:
120 set_behavior_flag(ptr
, MEM_USE_CACHE_LOOKUPS
, data
);
123 case MEMCACHED_BEHAVIOR_VERIFY_KEY
:
124 if (ptr
->flags
& MEM_BINARY_PROTOCOL
)
126 set_behavior_flag(ptr
, MEM_VERIFY_KEY
, data
);
128 case MEMCACHED_BEHAVIOR_SORT_HOSTS
:
130 set_behavior_flag(ptr
, MEM_USE_SORT_HOSTS
, data
);
131 run_distribution(ptr
);
135 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT
:
136 ptr
->poll_timeout
= (int32_t)data
;
138 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
:
139 ptr
->connect_timeout
= (int32_t)data
;
141 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
:
142 ptr
->retry_timeout
= (int32_t)data
;
144 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
:
145 ptr
->send_size
= (int32_t)data
;
148 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
:
149 ptr
->recv_size
= (int32_t)data
;
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
);
157 case MEMCACHED_BEHAVIOR_NOREPLY
:
158 set_behavior_flag(ptr
, MEM_NOREPLY
, data
);
160 case MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
:
161 set_behavior_flag(ptr
, MEM_AUTO_EJECT_HOSTS
, data
);
164 /* Shouldn't get here */
165 WATCHPOINT_ASSERT(flag
);
169 return MEMCACHED_SUCCESS
;
172 uint64_t memcached_behavior_get(memcached_st
*ptr
,
173 memcached_behavior flag
)
175 memcached_flags temp_flag
= MEM_NO_BLOCK
;
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
;
190 case MEMCACHED_BEHAVIOR_SUPPORT_CAS
:
191 temp_flag
= MEM_SUPPORT_CAS
;
193 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
:
194 temp_flag
= MEM_USE_CACHE_LOOKUPS
;
196 case MEMCACHED_BEHAVIOR_NO_BLOCK
:
197 temp_flag
= MEM_NO_BLOCK
;
199 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
:
200 temp_flag
= MEM_BUFFER_REQUESTS
;
202 case MEMCACHED_BEHAVIOR_USE_UDP
:
203 temp_flag
= MEM_USE_UDP
;
205 case MEMCACHED_BEHAVIOR_TCP_NODELAY
:
206 temp_flag
= MEM_TCP_NODELAY
;
208 case MEMCACHED_BEHAVIOR_VERIFY_KEY
:
209 temp_flag
= MEM_VERIFY_KEY
;
211 case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
:
212 temp_flag
= MEM_KETAMA_WEIGHTED
;
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
:
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
;
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
:
240 socklen_t sock_length
= sizeof(int);
243 /* We just try the first host, and if it is down we return zero */
244 if ((memcached_connect(&ptr
->hosts
[0])) != MEMCACHED_SUCCESS
)
247 if (getsockopt(ptr
->hosts
[0].fd
, SOL_SOCKET
,
248 SO_SNDBUF
, &sock_size
, &sock_length
))
249 return 0; /* Zero means error */
251 return (uint64_t) sock_size
;
253 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
:
256 socklen_t sock_length
= sizeof(int);
259 /* We just try the first host, and if it is down we return zero */
260 if ((memcached_connect(&ptr
->hosts
[0])) != MEMCACHED_SUCCESS
)
263 if (getsockopt(ptr
->hosts
[0].fd
, SOL_SOCKET
,
264 SO_RCVBUF
, &sock_size
, &sock_length
))
265 return 0; /* Zero means error */
267 return (uint64_t) sock_size
;
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
;
274 case MEMCACHED_BEHAVIOR_NOREPLY
:
275 temp_flag
= MEM_NOREPLY
;
277 case MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
:
278 temp_flag
= MEM_AUTO_EJECT_HOSTS
;
281 WATCHPOINT_ASSERT(flag
);
285 WATCHPOINT_ASSERT(temp_flag
); /* Programming mistake if it gets this far */
286 if (ptr
->flags
& temp_flag
)