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