Merge in fix for additional logic for timeouts.
[awesomized/libmemcached] / libmemcached / memcached.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38 #include <libmemcached/common.h>
39 #include <libmemcached/virtual_bucket.h>
40
41 #if 0
42 static const memcached_st global_copy= {
43 .state= {
44 .is_purging= false, // .is_purging
45 .is_processing_input= false, // is_processing_input
46 .is_time_for_rebuild= false,
47 },
48 .flags= {
49 .auto_eject_hosts= false,
50 .binary_protocol= false,
51 .buffer_requests= false,
52 .hash_with_prefix_key= false,
53 .no_block= false,
54 .no_reply= false,
55 .randomize_replica_read= false,
56 .support_cas= false,
57 .tcp_nodelay= false,
58 .use_sort_hosts= false,
59 .use_udp= false,
60 .verify_key= false,
61 .tcp_keepalive= false,
62 },
63 };
64 #endif
65
66 static inline bool _memcached_init(memcached_st *self)
67 {
68 self->state.is_purging= false;
69 self->state.is_processing_input= false;
70 self->state.is_time_for_rebuild= false;
71
72 self->flags.auto_eject_hosts= false;
73 self->flags.binary_protocol= false;
74 self->flags.buffer_requests= false;
75 self->flags.hash_with_prefix_key= false;
76 self->flags.no_block= false;
77 self->flags.no_reply= false;
78 self->flags.randomize_replica_read= false;
79 self->flags.support_cas= false;
80 self->flags.tcp_nodelay= false;
81 self->flags.use_sort_hosts= false;
82 self->flags.use_udp= false;
83 self->flags.verify_key= false;
84 self->flags.tcp_keepalive= false;
85
86 self->virtual_bucket= NULL;
87
88 self->distribution= MEMCACHED_DISTRIBUTION_MODULA;
89
90 if (not hashkit_create(&self->hashkit))
91 return false;
92
93 self->ketama.continuum= NULL;
94 self->ketama.continuum_count= 0;
95 self->ketama.continuum_points_counter= 0;
96 self->ketama.next_distribution_rebuild= 0;
97 self->ketama.weighted= false;
98
99 self->number_of_hosts= 0;
100 self->servers= NULL;
101 self->last_disconnected_server= NULL;
102
103 self->snd_timeout= 0;
104 self->rcv_timeout= 0;
105 self->server_failure_limit= 0;
106 self->query_id= 1; // 0 is considered invalid
107
108 /* TODO, Document why we picked these defaults */
109 self->io_msg_watermark= 500;
110 self->io_bytes_watermark= 65 * 1024;
111
112 self->tcp_keepidle= 0;
113
114 self->io_key_prefetch= 0;
115 self->poll_timeout= MEMCACHED_DEFAULT_TIMEOUT;
116 self->connect_timeout= MEMCACHED_DEFAULT_CONNECT_TIMEOUT;
117 self->retry_timeout= 0;
118
119 self->send_size= -1;
120 self->recv_size= -1;
121
122 self->user_data= NULL;
123 self->number_of_replicas= 0;
124
125 self->allocators= memcached_allocators_return_default();
126
127 self->on_clone= NULL;
128 self->on_cleanup= NULL;
129 self->get_key_failure= NULL;
130 self->delete_trigger= NULL;
131 self->callbacks= NULL;
132 self->sasl.callbacks= NULL;
133 self->sasl.is_allocated= false;
134
135 self->error_messages= NULL;
136 self->prefix_key= NULL;
137 self->configure.initial_pool_size= 1;
138 self->configure.max_pool_size= 1;
139 self->configure.version= -1;
140 self->configure.filename= NULL;
141
142 return true;
143 }
144
145 static void _free(memcached_st *ptr, bool release_st)
146 {
147 /* If we have anything open, lets close it now */
148 send_quit(ptr);
149 memcached_server_list_free(memcached_server_list(ptr));
150 memcached_result_free(&ptr->result);
151
152 memcached_virtual_bucket_free(ptr);
153
154 memcached_server_free(ptr->last_disconnected_server);
155
156 if (ptr->on_cleanup)
157 ptr->on_cleanup(ptr);
158
159 libmemcached_free(ptr, ptr->ketama.continuum);
160
161 memcached_array_free(ptr->prefix_key);
162 ptr->prefix_key= NULL;
163
164 memcached_error_free(ptr);
165
166 if (ptr->sasl.callbacks)
167 {
168 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
169 memcached_destroy_sasl_auth_data(ptr);
170 #endif
171 }
172
173 if (release_st)
174 {
175 memcached_array_free(ptr->configure.filename);
176 ptr->configure.filename= NULL;
177 }
178
179 if (memcached_is_allocated(ptr) && release_st)
180 {
181 libmemcached_free(ptr, ptr);
182 }
183 }
184
185 memcached_st *memcached_create(memcached_st *ptr)
186 {
187 if (ptr == NULL)
188 {
189 ptr= (memcached_st *)malloc(sizeof(memcached_st));
190
191 if (not ptr)
192 {
193 return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
194 }
195
196 ptr->options.is_allocated= true;
197 }
198 else
199 {
200 ptr->options.is_allocated= false;
201 }
202
203 #if 0
204 memcached_set_purging(ptr, false);
205 memcached_set_processing_input(ptr, false);
206 #endif
207
208 if (! _memcached_init(ptr))
209 {
210 memcached_free(ptr);
211 return NULL;
212 }
213
214 if (! memcached_result_create(ptr, &ptr->result))
215 {
216 memcached_free(ptr);
217 return NULL;
218 }
219
220 WATCHPOINT_ASSERT_INITIALIZED(&ptr->result);
221
222 return ptr;
223 }
224
225 memcached_st *memcached(const char *string, size_t length)
226 {
227 memcached_st *self= memcached_create(NULL);
228 if (not self)
229 {
230 errno= ENOMEM;
231 return NULL;
232 }
233
234 if (not length)
235 return self;
236
237 memcached_return_t rc= memcached_parse_configuration(self, string, length);
238
239 if (memcached_success(rc) and memcached_parse_filename(self))
240 {
241 rc= memcached_parse_configure_file(self, memcached_parse_filename(self), memcached_parse_filename_length(self));
242 }
243
244 if (memcached_failed(rc))
245 {
246 memcached_free(self);
247 errno= EINVAL;
248 return NULL;
249 }
250
251 return self;
252 }
253
254 memcached_return_t memcached_reset(memcached_st *ptr)
255 {
256 WATCHPOINT_ASSERT(ptr);
257 if (not ptr)
258 return MEMCACHED_INVALID_ARGUMENTS;
259
260 bool stored_is_allocated= memcached_is_allocated(ptr);
261 uint64_t query_id= ptr->query_id;
262 _free(ptr, false);
263 memcached_create(ptr);
264 memcached_set_allocated(ptr, stored_is_allocated);
265 ptr->query_id= query_id;
266
267 if (ptr->configure.filename)
268 {
269 return memcached_parse_configure_file(ptr, memcached_param_array(ptr->configure.filename));
270 }
271
272 return MEMCACHED_SUCCESS;
273 }
274
275 void memcached_servers_reset(memcached_st *self)
276 {
277 if (not self)
278 return;
279
280 memcached_server_list_free(memcached_server_list(self));
281
282 memcached_server_list_set(self, NULL);
283 self->number_of_hosts= 0;
284 memcached_server_free(self->last_disconnected_server);
285 self->last_disconnected_server= NULL;
286 self->server_failure_limit= 0;
287 }
288
289 void memcached_reset_last_disconnected_server(memcached_st *self)
290 {
291 if (not self)
292 return;
293
294 memcached_server_free(self->last_disconnected_server);
295 self->last_disconnected_server= NULL;
296 }
297
298 void memcached_free(memcached_st *ptr)
299 {
300 if (not ptr)
301 return;
302
303 _free(ptr, true);
304 }
305
306 /*
307 clone is the destination, while source is the structure to clone.
308 If source is NULL the call is the same as if a memcached_create() was
309 called.
310 */
311 memcached_st *memcached_clone(memcached_st *clone, const memcached_st *source)
312 {
313 memcached_return_t rc= MEMCACHED_SUCCESS;
314
315 if (not source)
316 return memcached_create(clone);
317
318 if (clone && memcached_is_allocated(clone))
319 {
320 return NULL;
321 }
322
323 memcached_st *new_clone= memcached_create(clone);
324
325 if (not new_clone)
326 return NULL;
327
328 new_clone->flags= source->flags;
329 new_clone->send_size= source->send_size;
330 new_clone->recv_size= source->recv_size;
331 new_clone->poll_timeout= source->poll_timeout;
332 new_clone->connect_timeout= source->connect_timeout;
333 new_clone->retry_timeout= source->retry_timeout;
334 new_clone->distribution= source->distribution;
335
336 if (not hashkit_clone(&new_clone->hashkit, &source->hashkit))
337 {
338 memcached_free(new_clone);
339 return NULL;
340 }
341
342 new_clone->user_data= source->user_data;
343
344 new_clone->snd_timeout= source->snd_timeout;
345 new_clone->rcv_timeout= source->rcv_timeout;
346
347 new_clone->on_clone= source->on_clone;
348 new_clone->on_cleanup= source->on_cleanup;
349
350 new_clone->allocators= source->allocators;
351
352 new_clone->get_key_failure= source->get_key_failure;
353 new_clone->delete_trigger= source->delete_trigger;
354 new_clone->server_failure_limit= source->server_failure_limit;
355 new_clone->io_msg_watermark= source->io_msg_watermark;
356 new_clone->io_bytes_watermark= source->io_bytes_watermark;
357 new_clone->io_key_prefetch= source->io_key_prefetch;
358 new_clone->number_of_replicas= source->number_of_replicas;
359 new_clone->tcp_keepidle= source->tcp_keepidle;
360
361 if (memcached_server_count(source))
362 rc= memcached_push(new_clone, source);
363
364 if (rc != MEMCACHED_SUCCESS)
365 {
366 memcached_free(new_clone);
367
368 return NULL;
369 }
370
371
372 new_clone->prefix_key= memcached_array_clone(new_clone, source->prefix_key);
373 new_clone->configure.filename= memcached_array_clone(new_clone, source->prefix_key);
374
375 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
376 if (source->sasl.callbacks)
377 {
378 if (memcached_clone_sasl(new_clone, source) != MEMCACHED_SUCCESS)
379 {
380 memcached_free(new_clone);
381 return NULL;
382 }
383 }
384 #endif
385
386 rc= run_distribution(new_clone);
387
388 if (rc != MEMCACHED_SUCCESS)
389 {
390 memcached_free(new_clone);
391
392 return NULL;
393 }
394
395 if (source->on_clone)
396 source->on_clone(new_clone, source);
397
398 return new_clone;
399 }
400
401 void *memcached_get_user_data(const memcached_st *ptr)
402 {
403 return ptr->user_data;
404 }
405
406 void *memcached_set_user_data(memcached_st *ptr, void *data)
407 {
408 void *ret= ptr->user_data;
409 ptr->user_data= data;
410
411 return ret;
412 }
413
414 memcached_return_t memcached_push(memcached_st *destination, const memcached_st *source)
415 {
416 return memcached_server_push(destination, source->servers);
417 }
418
419 memcached_server_write_instance_st memcached_server_instance_fetch(memcached_st *ptr, uint32_t server_key)
420 {
421 return &ptr->servers[server_key];
422 }
423
424 memcached_server_instance_st memcached_server_instance_by_position(const memcached_st *ptr, uint32_t server_key)
425 {
426 return &ptr->servers[server_key];
427 }
428
429 uint64_t memcached_query_id(const memcached_st *self)
430 {
431 if (not self)
432 return 0;
433
434 return self->query_id;
435 }