Merge in build lp
[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
40 #include <libmemcached/options.hpp>
41 #include <libmemcached/virtual_bucket.h>
42
43 #if 0
44 static const memcached_st global_copy= {
45 .state= {
46 .is_purging= false, // .is_purging
47 .is_processing_input= false, // is_processing_input
48 .is_time_for_rebuild= false,
49 },
50 .flags= {
51 .auto_eject_hosts= false,
52 .binary_protocol= false,
53 .buffer_requests= false,
54 .hash_with_namespace= false,
55 .no_block= false,
56 .no_reply= false,
57 .randomize_replica_read= false,
58 .support_cas= false,
59 .tcp_nodelay= false,
60 .use_sort_hosts= false,
61 .use_udp= false,
62 .verify_key= false,
63 .tcp_keepalive= false,
64 },
65 };
66 #endif
67
68 static inline bool _memcached_init(memcached_st *self)
69 {
70 self->state.is_purging= false;
71 self->state.is_processing_input= false;
72 self->state.is_time_for_rebuild= false;
73
74 self->flags.auto_eject_hosts= false;
75 self->flags.binary_protocol= false;
76 self->flags.buffer_requests= false;
77 self->flags.hash_with_namespace= false;
78 self->flags.no_block= false;
79 self->flags.no_reply= false;
80 self->flags.randomize_replica_read= false;
81 self->flags.support_cas= false;
82 self->flags.tcp_nodelay= false;
83 self->flags.use_sort_hosts= false;
84 self->flags.use_udp= false;
85 self->flags.verify_key= false;
86 self->flags.tcp_keepalive= false;
87
88 self->virtual_bucket= NULL;
89
90 self->distribution= MEMCACHED_DISTRIBUTION_MODULA;
91
92 if (not hashkit_create(&self->hashkit))
93 return false;
94
95 self->ketama.continuum= NULL;
96 self->ketama.continuum_count= 0;
97 self->ketama.continuum_points_counter= 0;
98 self->ketama.next_distribution_rebuild= 0;
99 self->ketama.weighted= false;
100
101 self->number_of_hosts= 0;
102 self->servers= NULL;
103 self->last_disconnected_server= NULL;
104
105 self->snd_timeout= 0;
106 self->rcv_timeout= 0;
107 self->server_failure_limit= 0;
108 self->query_id= 1; // 0 is considered invalid
109
110 /* TODO, Document why we picked these defaults */
111 self->io_msg_watermark= 500;
112 self->io_bytes_watermark= 65 * 1024;
113
114 self->tcp_keepidle= 0;
115
116 self->io_key_prefetch= 0;
117 self->poll_timeout= MEMCACHED_DEFAULT_TIMEOUT;
118 self->connect_timeout= MEMCACHED_DEFAULT_CONNECT_TIMEOUT;
119 self->retry_timeout= 0;
120
121 self->send_size= -1;
122 self->recv_size= -1;
123
124 self->user_data= NULL;
125 self->number_of_replicas= 0;
126
127 self->allocators= memcached_allocators_return_default();
128
129 self->on_clone= NULL;
130 self->on_cleanup= NULL;
131 self->get_key_failure= NULL;
132 self->delete_trigger= NULL;
133 self->callbacks= NULL;
134 self->sasl.callbacks= NULL;
135 self->sasl.is_allocated= false;
136
137 self->error_messages= NULL;
138 self->_namespace= NULL;
139 self->configure.initial_pool_size= 1;
140 self->configure.max_pool_size= 1;
141 self->configure.version= -1;
142 self->configure.filename= NULL;
143
144 return true;
145 }
146
147 static void _free(memcached_st *ptr, bool release_st)
148 {
149 /* If we have anything open, lets close it now */
150 send_quit(ptr);
151 memcached_server_list_free(memcached_server_list(ptr));
152 memcached_result_free(&ptr->result);
153
154 memcached_virtual_bucket_free(ptr);
155
156 memcached_server_free(ptr->last_disconnected_server);
157
158 if (ptr->on_cleanup)
159 ptr->on_cleanup(ptr);
160
161 libmemcached_free(ptr, ptr->ketama.continuum);
162
163 memcached_array_free(ptr->_namespace);
164 ptr->_namespace= NULL;
165
166 memcached_error_free(*ptr);
167
168 if (ptr->sasl.callbacks)
169 {
170 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
171 memcached_destroy_sasl_auth_data(ptr);
172 #endif
173 }
174
175 if (release_st)
176 {
177 memcached_array_free(ptr->configure.filename);
178 ptr->configure.filename= NULL;
179 }
180
181 if (memcached_is_allocated(ptr) && release_st)
182 {
183 libmemcached_free(ptr, ptr);
184 }
185 }
186
187 memcached_st *memcached_create(memcached_st *ptr)
188 {
189 if (ptr == NULL)
190 {
191 ptr= (memcached_st *)malloc(sizeof(memcached_st));
192
193 if (not ptr)
194 {
195 return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
196 }
197
198 ptr->options.is_allocated= true;
199 }
200 else
201 {
202 ptr->options.is_allocated= false;
203 }
204
205 #if 0
206 memcached_set_purging(ptr, false);
207 memcached_set_processing_input(ptr, false);
208 #endif
209
210 if (! _memcached_init(ptr))
211 {
212 memcached_free(ptr);
213 return NULL;
214 }
215
216 if (! memcached_result_create(ptr, &ptr->result))
217 {
218 memcached_free(ptr);
219 return NULL;
220 }
221
222 WATCHPOINT_ASSERT_INITIALIZED(&ptr->result);
223
224 return ptr;
225 }
226
227 memcached_st *memcached(const char *string, size_t length)
228 {
229 memcached_st *self= memcached_create(NULL);
230 if (not self)
231 {
232 errno= ENOMEM;
233 return NULL;
234 }
235
236 if (not length)
237 return self;
238
239 memcached_return_t rc= memcached_parse_configuration(self, string, length);
240
241 if (memcached_success(rc) and memcached_parse_filename(self))
242 {
243 rc= memcached_parse_configure_file(*self, memcached_parse_filename(self), memcached_parse_filename_length(self));
244 }
245
246 if (memcached_failed(rc))
247 {
248 memcached_free(self);
249 errno= EINVAL;
250 return NULL;
251 }
252
253 return self;
254 }
255
256 memcached_return_t memcached_reset(memcached_st *ptr)
257 {
258 WATCHPOINT_ASSERT(ptr);
259 if (not ptr)
260 return MEMCACHED_INVALID_ARGUMENTS;
261
262 bool stored_is_allocated= memcached_is_allocated(ptr);
263 uint64_t query_id= ptr->query_id;
264 _free(ptr, false);
265 memcached_create(ptr);
266 memcached_set_allocated(ptr, stored_is_allocated);
267 ptr->query_id= query_id;
268
269 if (ptr->configure.filename)
270 {
271 return memcached_parse_configure_file(*ptr, memcached_param_array(ptr->configure.filename));
272 }
273
274 return MEMCACHED_SUCCESS;
275 }
276
277 void memcached_servers_reset(memcached_st *self)
278 {
279 if (not self)
280 return;
281
282 memcached_server_list_free(memcached_server_list(self));
283
284 memcached_server_list_set(self, NULL);
285 self->number_of_hosts= 0;
286 memcached_server_free(self->last_disconnected_server);
287 self->last_disconnected_server= NULL;
288 self->server_failure_limit= 0;
289 }
290
291 void memcached_reset_last_disconnected_server(memcached_st *self)
292 {
293 if (not self)
294 return;
295
296 memcached_server_free(self->last_disconnected_server);
297 self->last_disconnected_server= NULL;
298 }
299
300 void memcached_free(memcached_st *ptr)
301 {
302 if (not ptr)
303 return;
304
305 _free(ptr, true);
306 }
307
308 /*
309 clone is the destination, while source is the structure to clone.
310 If source is NULL the call is the same as if a memcached_create() was
311 called.
312 */
313 memcached_st *memcached_clone(memcached_st *clone, const memcached_st *source)
314 {
315 memcached_return_t rc= MEMCACHED_SUCCESS;
316
317 if (not source)
318 return memcached_create(clone);
319
320 if (clone && memcached_is_allocated(clone))
321 {
322 return NULL;
323 }
324
325 memcached_st *new_clone= memcached_create(clone);
326
327 if (not new_clone)
328 return NULL;
329
330 new_clone->flags= source->flags;
331 new_clone->send_size= source->send_size;
332 new_clone->recv_size= source->recv_size;
333 new_clone->poll_timeout= source->poll_timeout;
334 new_clone->connect_timeout= source->connect_timeout;
335 new_clone->retry_timeout= source->retry_timeout;
336 new_clone->distribution= source->distribution;
337
338 if (not hashkit_clone(&new_clone->hashkit, &source->hashkit))
339 {
340 memcached_free(new_clone);
341 return NULL;
342 }
343
344 new_clone->user_data= source->user_data;
345
346 new_clone->snd_timeout= source->snd_timeout;
347 new_clone->rcv_timeout= source->rcv_timeout;
348
349 new_clone->on_clone= source->on_clone;
350 new_clone->on_cleanup= source->on_cleanup;
351
352 new_clone->allocators= source->allocators;
353
354 new_clone->get_key_failure= source->get_key_failure;
355 new_clone->delete_trigger= source->delete_trigger;
356 new_clone->server_failure_limit= source->server_failure_limit;
357 new_clone->io_msg_watermark= source->io_msg_watermark;
358 new_clone->io_bytes_watermark= source->io_bytes_watermark;
359 new_clone->io_key_prefetch= source->io_key_prefetch;
360 new_clone->number_of_replicas= source->number_of_replicas;
361 new_clone->tcp_keepidle= source->tcp_keepidle;
362
363 if (memcached_server_count(source))
364 rc= memcached_push(new_clone, source);
365
366 if (rc != MEMCACHED_SUCCESS)
367 {
368 memcached_free(new_clone);
369
370 return NULL;
371 }
372
373
374 new_clone->_namespace= memcached_array_clone(new_clone, source->_namespace);
375 new_clone->configure.filename= memcached_array_clone(new_clone, source->_namespace);
376
377 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
378 if (source->sasl.callbacks)
379 {
380 if (memcached_clone_sasl(new_clone, source) != MEMCACHED_SUCCESS)
381 {
382 memcached_free(new_clone);
383 return NULL;
384 }
385 }
386 #endif
387
388 rc= run_distribution(new_clone);
389
390 if (rc != MEMCACHED_SUCCESS)
391 {
392 memcached_free(new_clone);
393
394 return NULL;
395 }
396
397 if (source->on_clone)
398 source->on_clone(new_clone, source);
399
400 return new_clone;
401 }
402
403 void *memcached_get_user_data(const memcached_st *ptr)
404 {
405 return ptr->user_data;
406 }
407
408 void *memcached_set_user_data(memcached_st *ptr, void *data)
409 {
410 void *ret= ptr->user_data;
411 ptr->user_data= data;
412
413 return ret;
414 }
415
416 memcached_return_t memcached_push(memcached_st *destination, const memcached_st *source)
417 {
418 return memcached_server_push(destination, source->servers);
419 }
420
421 memcached_server_write_instance_st memcached_server_instance_fetch(memcached_st *ptr, uint32_t server_key)
422 {
423 return &ptr->servers[server_key];
424 }
425
426 memcached_server_instance_st memcached_server_instance_by_position(const memcached_st *ptr, uint32_t server_key)
427 {
428 return &ptr->servers[server_key];
429 }
430
431 uint64_t memcached_query_id(const memcached_st *self)
432 {
433 if (not self)
434 return 0;
435
436 return self->query_id;
437 }