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