1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
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.
38 #include <libmemcached/common.h>
39 #include <libmemcached/virtual_bucket.h>
41 static const memcached_st global_copy
= {
44 .is_processing_input
= false,
45 .is_time_for_rebuild
= false,
48 .auto_eject_hosts
= false,
49 .binary_protocol
= false,
50 .buffer_requests
= false,
51 .hash_with_prefix_key
= false,
54 .randomize_replica_read
= false,
57 .use_sort_hosts
= false,
60 .tcp_keepalive
= false,
64 static inline bool _memcached_init(memcached_st
*self
)
66 self
->state
= global_copy
.state
;
67 self
->flags
= global_copy
.flags
;
68 self
->virtual_bucket
= NULL
;
70 self
->distribution
= MEMCACHED_DISTRIBUTION_MODULA
;
73 hash_ptr
= hashkit_create(&self
->hashkit
);
77 self
->ketama
.continuum
= NULL
;
78 self
->ketama
.continuum_count
= 0;
79 self
->ketama
.continuum_points_counter
= 0;
80 self
->ketama
.next_distribution_rebuild
= 0;
81 self
->ketama
.weighted
= false;
83 self
->number_of_hosts
= 0;
85 self
->last_disconnected_server
= NULL
;
89 self
->server_failure_limit
= 0;
92 /* TODO, Document why we picked these defaults */
93 self
->io_msg_watermark
= 500;
94 self
->io_bytes_watermark
= 65 * 1024;
96 self
->tcp_keepidle
= 0;
98 self
->io_key_prefetch
= 0;
99 self
->poll_timeout
= MEMCACHED_DEFAULT_TIMEOUT
;
100 self
->connect_timeout
= MEMCACHED_DEFAULT_CONNECT_TIMEOUT
;
101 self
->retry_timeout
= 0;
106 self
->user_data
= NULL
;
107 self
->number_of_replicas
= 0;
108 hash_ptr
= hashkit_create(&self
->distribution_hashkit
);
112 self
->allocators
= memcached_allocators_return_default();
114 self
->on_clone
= NULL
;
115 self
->on_cleanup
= NULL
;
116 self
->get_key_failure
= NULL
;
117 self
->delete_trigger
= NULL
;
118 self
->callbacks
= NULL
;
119 self
->sasl
.callbacks
= NULL
;
120 self
->sasl
.is_allocated
= false;
122 self
->error_messages
= NULL
;
123 self
->prefix_key
= NULL
;
124 self
->configure
.initial_pool_size
= 1;
125 self
->configure
.max_pool_size
= 1;
126 self
->configure
.version
= -1;
127 self
->configure
.filename
= NULL
;
132 static void _free(memcached_st
*ptr
, bool release_st
)
134 /* If we have anything open, lets close it now */
136 memcached_server_list_free(memcached_server_list(ptr
));
137 memcached_result_free(&ptr
->result
);
139 memcached_virtual_bucket_free(ptr
);
141 if (ptr
->last_disconnected_server
)
142 memcached_server_free(ptr
->last_disconnected_server
);
145 ptr
->on_cleanup(ptr
);
147 if (ptr
->ketama
.continuum
)
148 libmemcached_free(ptr
, ptr
->ketama
.continuum
);
150 memcached_array_free(ptr
->prefix_key
);
151 ptr
->prefix_key
= NULL
;
153 memcached_error_free(ptr
);
155 if (ptr
->sasl
.callbacks
)
157 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
158 memcached_destroy_sasl_auth_data(ptr
);
164 memcached_array_free(ptr
->configure
.filename
);
165 ptr
->configure
.filename
= NULL
;
168 if (memcached_is_allocated(ptr
) && release_st
)
170 libmemcached_free(ptr
, ptr
);
174 memcached_st
*memcached_create(memcached_st
*ptr
)
178 ptr
= (memcached_st
*)malloc(sizeof(memcached_st
));
182 return NULL
; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
185 ptr
->options
.is_allocated
= true;
189 ptr
->options
.is_allocated
= false;
193 memcached_set_purging(ptr
, false);
194 memcached_set_processing_input(ptr
, false);
197 if (! _memcached_init(ptr
))
203 if (! memcached_result_create(ptr
, &ptr
->result
))
209 WATCHPOINT_ASSERT_INITIALIZED(&ptr
->result
);
214 memcached_st
*memcached(const char *string
, size_t length
)
216 if (! length
|| ! string
)
222 memcached_st
*self
= memcached_create(NULL
);
229 memcached_return_t rc
;
230 rc
= memcached_parse_configuration(self
, string
, length
);
232 if (rc
== MEMCACHED_SUCCESS
&& memcached_parse_filename(self
))
234 rc
= memcached_parse_configure_file(self
, memcached_parse_filename(self
), memcached_parse_filename_length(self
));
237 if (rc
!= MEMCACHED_SUCCESS
)
239 memcached_free(self
);
249 memcached_return_t
memcached_reset(memcached_st
*ptr
)
251 WATCHPOINT_ASSERT(ptr
);
253 return MEMCACHED_INVALID_ARGUMENTS
;
255 bool stored_is_allocated
= memcached_is_allocated(ptr
);
256 uint64_t query_id
= ptr
->query_id
;
258 memcached_create(ptr
);
259 memcached_set_allocated(ptr
, stored_is_allocated
);
260 ptr
->query_id
= query_id
;
262 if (ptr
->configure
.filename
)
264 return memcached_parse_configure_file(ptr
, memcached_param_array(ptr
->configure
.filename
));
267 return MEMCACHED_SUCCESS
;
270 void memcached_servers_reset(memcached_st
*ptr
)
275 memcached_server_list_free(memcached_server_list(ptr
));
277 memcached_server_list_set(ptr
, NULL
);
278 ptr
->number_of_hosts
= 0;
279 if (ptr
->last_disconnected_server
)
281 memcached_server_free(ptr
->last_disconnected_server
);
283 ptr
->last_disconnected_server
= NULL
;
284 ptr
->server_failure_limit
= 0;
287 void memcached_reset_last_disconnected_server(memcached_st
*ptr
)
292 if (ptr
->last_disconnected_server
)
294 memcached_server_free(ptr
->last_disconnected_server
);
295 ptr
->last_disconnected_server
= NULL
;
299 void memcached_free(memcached_st
*ptr
)
308 clone is the destination, while source is the structure to clone.
309 If source is NULL the call is the same as if a memcached_create() was
312 memcached_st
*memcached_clone(memcached_st
*clone
, const memcached_st
*source
)
314 memcached_return_t rc
= MEMCACHED_SUCCESS
;
315 memcached_st
*new_clone
;
318 return memcached_create(clone
);
320 if (clone
&& memcached_is_allocated(clone
))
325 new_clone
= memcached_create(clone
);
327 if (new_clone
== NULL
)
330 new_clone
->flags
= source
->flags
;
331 new_clone
->send_size
= source
->send_size
;
332 new_clone
->recv_size
= source
->recv_size
;
333 new_clone
->poll_timeout
= source
->poll_timeout
;
334 new_clone
->connect_timeout
= source
->connect_timeout
;
335 new_clone
->retry_timeout
= source
->retry_timeout
;
336 new_clone
->distribution
= source
->distribution
;
338 hashkit_st
*hash_ptr
;
340 hash_ptr
= hashkit_clone(&new_clone
->hashkit
, &source
->hashkit
);
343 memcached_free(new_clone
);
347 hash_ptr
= hashkit_clone(&new_clone
->distribution_hashkit
, &source
->distribution_hashkit
);
350 memcached_free(new_clone
);
354 new_clone
->user_data
= source
->user_data
;
356 new_clone
->snd_timeout
= source
->snd_timeout
;
357 new_clone
->rcv_timeout
= source
->rcv_timeout
;
359 new_clone
->on_clone
= source
->on_clone
;
360 new_clone
->on_cleanup
= source
->on_cleanup
;
362 new_clone
->allocators
= source
->allocators
;
364 new_clone
->get_key_failure
= source
->get_key_failure
;
365 new_clone
->delete_trigger
= source
->delete_trigger
;
366 new_clone
->server_failure_limit
= source
->server_failure_limit
;
367 new_clone
->io_msg_watermark
= source
->io_msg_watermark
;
368 new_clone
->io_bytes_watermark
= source
->io_bytes_watermark
;
369 new_clone
->io_key_prefetch
= source
->io_key_prefetch
;
370 new_clone
->number_of_replicas
= source
->number_of_replicas
;
371 new_clone
->tcp_keepidle
= source
->tcp_keepidle
;
373 if (memcached_server_count(source
))
374 rc
= memcached_push(new_clone
, source
);
376 if (rc
!= MEMCACHED_SUCCESS
)
378 memcached_free(new_clone
);
384 new_clone
->prefix_key
= memcached_array_clone(new_clone
, source
->prefix_key
);
386 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
387 if (source
->sasl
.callbacks
)
389 if (memcached_clone_sasl(new_clone
, source
) != MEMCACHED_SUCCESS
)
391 memcached_free(new_clone
);
397 rc
= run_distribution(new_clone
);
399 if (rc
!= MEMCACHED_SUCCESS
)
401 memcached_free(new_clone
);
406 if (source
->on_clone
)
407 source
->on_clone(new_clone
, source
);
412 void *memcached_get_user_data(const memcached_st
*ptr
)
414 return ptr
->user_data
;
417 void *memcached_set_user_data(memcached_st
*ptr
, void *data
)
419 void *ret
= ptr
->user_data
;
420 ptr
->user_data
= data
;
425 memcached_return_t
memcached_push(memcached_st
*destination
, const memcached_st
*source
)
427 return memcached_server_push(destination
, source
->servers
);
430 memcached_server_write_instance_st
memcached_server_instance_fetch(memcached_st
*ptr
, uint32_t server_key
)
432 return &ptr
->servers
[server_key
];
435 memcached_server_instance_st
memcached_server_instance_by_position(const memcached_st
*ptr
, uint32_t server_key
)
437 return &ptr
->servers
[server_key
];