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>
42 static const memcached_st global_copy
= {
44 .is_purging
= false, // .is_purging
45 .is_processing_input
= false, // is_processing_input
46 .is_time_for_rebuild
= false,
49 .auto_eject_hosts
= false,
50 .binary_protocol
= false,
51 .buffer_requests
= false,
52 .hash_with_prefix_key
= false,
55 .randomize_replica_read
= false,
58 .use_sort_hosts
= false,
61 .tcp_keepalive
= false,
66 static inline bool _memcached_init(memcached_st
*self
)
68 self
->state
.is_purging
= false;
69 self
->state
.is_processing_input
= false;
70 self
->state
.is_time_for_rebuild
= false;
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;
86 self
->virtual_bucket
= NULL
;
88 self
->distribution
= MEMCACHED_DISTRIBUTION_MODULA
;
90 if (not hashkit_create(&self
->hashkit
))
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;
99 self
->number_of_hosts
= 0;
101 self
->last_disconnected_server
= NULL
;
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
108 /* TODO, Document why we picked these defaults */
109 self
->io_msg_watermark
= 500;
110 self
->io_bytes_watermark
= 65 * 1024;
112 self
->tcp_keepidle
= 0;
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;
122 self
->user_data
= NULL
;
123 self
->number_of_replicas
= 0;
125 self
->allocators
= memcached_allocators_return_default();
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;
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
;
145 static void _free(memcached_st
*ptr
, bool release_st
)
147 /* If we have anything open, lets close it now */
149 memcached_server_list_free(memcached_server_list(ptr
));
150 memcached_result_free(&ptr
->result
);
152 memcached_virtual_bucket_free(ptr
);
154 memcached_server_free(ptr
->last_disconnected_server
);
157 ptr
->on_cleanup(ptr
);
159 libmemcached_free(ptr
, ptr
->ketama
.continuum
);
161 memcached_array_free(ptr
->prefix_key
);
162 ptr
->prefix_key
= NULL
;
164 memcached_error_free(ptr
);
166 if (ptr
->sasl
.callbacks
)
168 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
169 memcached_destroy_sasl_auth_data(ptr
);
175 memcached_array_free(ptr
->configure
.filename
);
176 ptr
->configure
.filename
= NULL
;
179 if (memcached_is_allocated(ptr
) && release_st
)
181 libmemcached_free(ptr
, ptr
);
185 memcached_st
*memcached_create(memcached_st
*ptr
)
189 ptr
= (memcached_st
*)malloc(sizeof(memcached_st
));
193 return NULL
; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
196 ptr
->options
.is_allocated
= true;
200 ptr
->options
.is_allocated
= false;
204 memcached_set_purging(ptr
, false);
205 memcached_set_processing_input(ptr
, false);
208 if (! _memcached_init(ptr
))
214 if (! memcached_result_create(ptr
, &ptr
->result
))
220 WATCHPOINT_ASSERT_INITIALIZED(&ptr
->result
);
225 memcached_st
*memcached(const char *string
, size_t length
)
227 memcached_st
*self
= memcached_create(NULL
);
237 memcached_return_t rc
= memcached_parse_configuration(self
, string
, length
);
239 if (memcached_success(rc
) and memcached_parse_filename(self
))
241 rc
= memcached_parse_configure_file(self
, memcached_parse_filename(self
), memcached_parse_filename_length(self
));
244 if (memcached_failed(rc
))
246 memcached_free(self
);
254 memcached_return_t
memcached_reset(memcached_st
*ptr
)
256 WATCHPOINT_ASSERT(ptr
);
258 return MEMCACHED_INVALID_ARGUMENTS
;
260 bool stored_is_allocated
= memcached_is_allocated(ptr
);
261 uint64_t query_id
= ptr
->query_id
;
263 memcached_create(ptr
);
264 memcached_set_allocated(ptr
, stored_is_allocated
);
265 ptr
->query_id
= query_id
;
267 if (ptr
->configure
.filename
)
269 return memcached_parse_configure_file(ptr
, memcached_param_array(ptr
->configure
.filename
));
272 return MEMCACHED_SUCCESS
;
275 void memcached_servers_reset(memcached_st
*self
)
280 memcached_server_list_free(memcached_server_list(self
));
282 memcached_server_list_set(self
, NULL
);
283 self
->number_of_hosts
= 0;
284 memcached_server_free(self
->last_disconnected_server
);
285 self
->last_disconnected_server
= NULL
;
286 self
->server_failure_limit
= 0;
289 void memcached_reset_last_disconnected_server(memcached_st
*self
)
294 memcached_server_free(self
->last_disconnected_server
);
295 self
->last_disconnected_server
= NULL
;
298 void memcached_free(memcached_st
*ptr
)
307 clone is the destination, while source is the structure to clone.
308 If source is NULL the call is the same as if a memcached_create() was
311 memcached_st
*memcached_clone(memcached_st
*clone
, const memcached_st
*source
)
313 memcached_return_t rc
= MEMCACHED_SUCCESS
;
316 return memcached_create(clone
);
318 if (clone
&& memcached_is_allocated(clone
))
323 memcached_st
*new_clone
= memcached_create(clone
);
328 new_clone
->flags
= source
->flags
;
329 new_clone
->send_size
= source
->send_size
;
330 new_clone
->recv_size
= source
->recv_size
;
331 new_clone
->poll_timeout
= source
->poll_timeout
;
332 new_clone
->connect_timeout
= source
->connect_timeout
;
333 new_clone
->retry_timeout
= source
->retry_timeout
;
334 new_clone
->distribution
= source
->distribution
;
336 if (not hashkit_clone(&new_clone
->hashkit
, &source
->hashkit
))
338 memcached_free(new_clone
);
342 new_clone
->user_data
= source
->user_data
;
344 new_clone
->snd_timeout
= source
->snd_timeout
;
345 new_clone
->rcv_timeout
= source
->rcv_timeout
;
347 new_clone
->on_clone
= source
->on_clone
;
348 new_clone
->on_cleanup
= source
->on_cleanup
;
350 new_clone
->allocators
= source
->allocators
;
352 new_clone
->get_key_failure
= source
->get_key_failure
;
353 new_clone
->delete_trigger
= source
->delete_trigger
;
354 new_clone
->server_failure_limit
= source
->server_failure_limit
;
355 new_clone
->io_msg_watermark
= source
->io_msg_watermark
;
356 new_clone
->io_bytes_watermark
= source
->io_bytes_watermark
;
357 new_clone
->io_key_prefetch
= source
->io_key_prefetch
;
358 new_clone
->number_of_replicas
= source
->number_of_replicas
;
359 new_clone
->tcp_keepidle
= source
->tcp_keepidle
;
361 if (memcached_server_count(source
))
362 rc
= memcached_push(new_clone
, source
);
364 if (rc
!= MEMCACHED_SUCCESS
)
366 memcached_free(new_clone
);
372 new_clone
->prefix_key
= memcached_array_clone(new_clone
, source
->prefix_key
);
373 new_clone
->configure
.filename
= memcached_array_clone(new_clone
, source
->prefix_key
);
375 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
376 if (source
->sasl
.callbacks
)
378 if (memcached_clone_sasl(new_clone
, source
) != MEMCACHED_SUCCESS
)
380 memcached_free(new_clone
);
386 rc
= run_distribution(new_clone
);
388 if (rc
!= MEMCACHED_SUCCESS
)
390 memcached_free(new_clone
);
395 if (source
->on_clone
)
396 source
->on_clone(new_clone
, source
);
401 void *memcached_get_user_data(const memcached_st
*ptr
)
403 return ptr
->user_data
;
406 void *memcached_set_user_data(memcached_st
*ptr
, void *data
)
408 void *ret
= ptr
->user_data
;
409 ptr
->user_data
= data
;
414 memcached_return_t
memcached_push(memcached_st
*destination
, const memcached_st
*source
)
416 return memcached_server_push(destination
, source
->servers
);
419 memcached_server_write_instance_st
memcached_server_instance_fetch(memcached_st
*ptr
, uint32_t server_key
)
421 return &ptr
->servers
[server_key
];
424 memcached_server_instance_st
memcached_server_instance_by_position(const memcached_st
*ptr
, uint32_t server_key
)
426 return &ptr
->servers
[server_key
];
429 uint64_t memcached_query_id(const memcached_st
*self
)
434 return self
->query_id
;