Fixed up a few more places where we set the error in the memcached root.
[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 #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 if (ptr->last_disconnected_server)
155 memcached_server_free(ptr->last_disconnected_server);
156
157 if (ptr->on_cleanup)
158 ptr->on_cleanup(ptr);
159
160 if (ptr->ketama.continuum)
161 libmemcached_free(ptr, ptr->ketama.continuum);
162
163 memcached_array_free(ptr->prefix_key);
164 ptr->prefix_key= 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 (! 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 if ((not length and string) or (length and not string))
230 {
231 errno= EINVAL;
232 return NULL;
233 }
234
235 memcached_st *self= memcached_create(NULL);
236 if (not self)
237 {
238 errno= ENOMEM;
239 return NULL;
240 }
241
242 if (not length)
243 return self;
244
245 memcached_return_t rc;
246 rc= memcached_parse_configuration(self, string, length);
247
248 if (rc == MEMCACHED_SUCCESS && memcached_parse_filename(self))
249 {
250 rc= memcached_parse_configure_file(self, memcached_parse_filename(self), memcached_parse_filename_length(self));
251 }
252
253 if (rc != MEMCACHED_SUCCESS)
254 {
255 memcached_free(self);
256 errno= EINVAL;
257 return NULL;
258 }
259
260 errno= 0;
261
262 return self;
263 }
264
265 memcached_return_t memcached_reset(memcached_st *ptr)
266 {
267 WATCHPOINT_ASSERT(ptr);
268 if (not ptr)
269 return MEMCACHED_INVALID_ARGUMENTS;
270
271 bool stored_is_allocated= memcached_is_allocated(ptr);
272 uint64_t query_id= ptr->query_id;
273 _free(ptr, false);
274 memcached_create(ptr);
275 memcached_set_allocated(ptr, stored_is_allocated);
276 ptr->query_id= query_id;
277
278 if (ptr->configure.filename)
279 {
280 return memcached_parse_configure_file(ptr, memcached_param_array(ptr->configure.filename));
281 }
282
283 return MEMCACHED_SUCCESS;
284 }
285
286 void memcached_servers_reset(memcached_st *ptr)
287 {
288 if (! ptr)
289 return;
290
291 memcached_server_list_free(memcached_server_list(ptr));
292
293 memcached_server_list_set(ptr, NULL);
294 ptr->number_of_hosts= 0;
295 if (ptr->last_disconnected_server)
296 {
297 memcached_server_free(ptr->last_disconnected_server);
298 }
299 ptr->last_disconnected_server= NULL;
300 ptr->server_failure_limit= 0;
301 }
302
303 void memcached_reset_last_disconnected_server(memcached_st *ptr)
304 {
305 if (! ptr)
306 return;
307
308 if (ptr->last_disconnected_server)
309 {
310 memcached_server_free(ptr->last_disconnected_server);
311 ptr->last_disconnected_server= NULL;
312 }
313 }
314
315 void memcached_free(memcached_st *ptr)
316 {
317 if (! ptr)
318 return;
319
320 _free(ptr, true);
321 }
322
323 /*
324 clone is the destination, while source is the structure to clone.
325 If source is NULL the call is the same as if a memcached_create() was
326 called.
327 */
328 memcached_st *memcached_clone(memcached_st *clone, const memcached_st *source)
329 {
330 memcached_return_t rc= MEMCACHED_SUCCESS;
331 memcached_st *new_clone;
332
333 if (source == NULL)
334 return memcached_create(clone);
335
336 if (clone && memcached_is_allocated(clone))
337 {
338 return NULL;
339 }
340
341 new_clone= memcached_create(clone);
342
343 if (new_clone == NULL)
344 return NULL;
345
346 new_clone->flags= source->flags;
347 new_clone->send_size= source->send_size;
348 new_clone->recv_size= source->recv_size;
349 new_clone->poll_timeout= source->poll_timeout;
350 new_clone->connect_timeout= source->connect_timeout;
351 new_clone->retry_timeout= source->retry_timeout;
352 new_clone->distribution= source->distribution;
353
354 if (not hashkit_clone(&new_clone->hashkit, &source->hashkit))
355 {
356 memcached_free(new_clone);
357 return NULL;
358 }
359
360 new_clone->user_data= source->user_data;
361
362 new_clone->snd_timeout= source->snd_timeout;
363 new_clone->rcv_timeout= source->rcv_timeout;
364
365 new_clone->on_clone= source->on_clone;
366 new_clone->on_cleanup= source->on_cleanup;
367
368 new_clone->allocators= source->allocators;
369
370 new_clone->get_key_failure= source->get_key_failure;
371 new_clone->delete_trigger= source->delete_trigger;
372 new_clone->server_failure_limit= source->server_failure_limit;
373 new_clone->io_msg_watermark= source->io_msg_watermark;
374 new_clone->io_bytes_watermark= source->io_bytes_watermark;
375 new_clone->io_key_prefetch= source->io_key_prefetch;
376 new_clone->number_of_replicas= source->number_of_replicas;
377 new_clone->tcp_keepidle= source->tcp_keepidle;
378
379 if (memcached_server_count(source))
380 rc= memcached_push(new_clone, source);
381
382 if (rc != MEMCACHED_SUCCESS)
383 {
384 memcached_free(new_clone);
385
386 return NULL;
387 }
388
389
390 new_clone->prefix_key= memcached_array_clone(new_clone, source->prefix_key);
391
392 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
393 if (source->sasl.callbacks)
394 {
395 if (memcached_clone_sasl(new_clone, source) != MEMCACHED_SUCCESS)
396 {
397 memcached_free(new_clone);
398 return NULL;
399 }
400 }
401 #endif
402
403 rc= run_distribution(new_clone);
404
405 if (rc != MEMCACHED_SUCCESS)
406 {
407 memcached_free(new_clone);
408
409 return NULL;
410 }
411
412 if (source->on_clone)
413 source->on_clone(new_clone, source);
414
415 return new_clone;
416 }
417
418 void *memcached_get_user_data(const memcached_st *ptr)
419 {
420 return ptr->user_data;
421 }
422
423 void *memcached_set_user_data(memcached_st *ptr, void *data)
424 {
425 void *ret= ptr->user_data;
426 ptr->user_data= data;
427
428 return ret;
429 }
430
431 memcached_return_t memcached_push(memcached_st *destination, const memcached_st *source)
432 {
433 return memcached_server_push(destination, source->servers);
434 }
435
436 memcached_server_write_instance_st memcached_server_instance_fetch(memcached_st *ptr, uint32_t server_key)
437 {
438 return &ptr->servers[server_key];
439 }
440
441 memcached_server_instance_st memcached_server_instance_by_position(const memcached_st *ptr, uint32_t server_key)
442 {
443 return &ptr->servers[server_key];
444 }
445
446 uint64_t memcached_query_id(const memcached_st *self)
447 {
448 if (not self)
449 return 0;
450
451 return self->query_id;
452 }