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