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