Update for last disconnect to stick around even if ejected.
[awesomized/libmemcached] / libmemcached / memcached.c
1 /* LibMemcached
2 * Copyright (C) 2006-2010 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary:
9 *
10 */
11
12 #include "common.h"
13
14 static const memcached_st global_copy= {
15 .state= {
16 .is_purging= false,
17 .is_processing_input= false,
18 .is_time_for_rebuild= false,
19 },
20 .flags= {
21 .auto_eject_hosts= false,
22 .binary_protocol= false,
23 .buffer_requests= false,
24 .cork= false,
25 .hash_with_prefix_key= false,
26 .ketama_weighted= false,
27 .no_block= false,
28 .no_reply= false,
29 .randomize_replica_read= false,
30 .reuse_memory= false,
31 .support_cas= false,
32 .tcp_nodelay= false,
33 .use_cache_lookups= false,
34 .use_sort_hosts= false,
35 .use_udp= false,
36 .verify_key= false,
37 .tcp_keepalive= false
38 }
39 };
40
41 static inline bool _memcached_init(memcached_st *self)
42 {
43 self->state= global_copy.state;
44 self->flags= global_copy.flags;
45
46 self->distribution= MEMCACHED_DISTRIBUTION_MODULA;
47
48 hashkit_st *hash_ptr;
49 hash_ptr= hashkit_create(&self->hashkit);
50 if (! hash_ptr)
51 return false;
52
53 self->continuum_points_counter= 0;
54
55 self->number_of_hosts= 0;
56 self->servers= NULL;
57 self->last_disconnected_server= NULL;
58
59 self->snd_timeout= 0;
60 self->rcv_timeout= 0;
61 self->server_failure_limit= 0;
62
63 /* TODO, Document why we picked these defaults */
64 self->io_msg_watermark= 500;
65 self->io_bytes_watermark= 65 * 1024;
66
67 self->tcp_keepidle= 0;
68
69 self->io_key_prefetch= 0;
70 self->cached_errno= 0;
71 self->poll_timeout= MEMCACHED_DEFAULT_TIMEOUT;
72 self->connect_timeout= MEMCACHED_DEFAULT_TIMEOUT;
73 self->retry_timeout= 0;
74 self->continuum_count= 0;
75
76 self->send_size= -1;
77 self->recv_size= -1;
78
79 self->user_data= NULL;
80 self->next_distribution_rebuild= 0;
81 self->prefix_key_length= 0;
82 self->number_of_replicas= 0;
83 hash_ptr= hashkit_create(&self->distribution_hashkit);
84 if (! hash_ptr)
85 return false;
86 self->continuum= NULL;
87
88 self->allocators= memcached_allocators_return_default();
89
90 self->on_clone= NULL;
91 self->on_cleanup= NULL;
92 self->get_key_failure= NULL;
93 self->delete_trigger= NULL;
94 self->callbacks= NULL;
95 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
96 self->sasl.callbacks= NULL;
97 self->sasl.is_allocated= false;
98 #endif
99
100 return true;
101 }
102
103 memcached_st *memcached_create(memcached_st *ptr)
104 {
105 if (ptr == NULL)
106 {
107 ptr= (memcached_st *)malloc(sizeof(memcached_st));
108
109 if (! ptr)
110 {
111 return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
112 }
113
114 ptr->options.is_allocated= true;
115 }
116 else
117 {
118 ptr->options.is_allocated= false;
119 }
120
121 #if 0
122 memcached_set_purging(ptr, false);
123 memcached_set_processing_input(ptr, false);
124 #endif
125
126 if (! _memcached_init(ptr))
127 {
128 memcached_free(ptr);
129 return NULL;
130 }
131
132 if (! memcached_result_create(ptr, &ptr->result))
133 {
134 memcached_free(ptr);
135 return NULL;
136 }
137
138 WATCHPOINT_ASSERT_INITIALIZED(&ptr->result);
139
140 return ptr;
141 }
142
143 void memcached_servers_reset(memcached_st *ptr)
144 {
145 memcached_server_list_free(memcached_server_list(ptr));
146
147 memcached_server_list_set(ptr, NULL);
148 ptr->number_of_hosts= 0;
149 if (ptr->last_disconnected_server)
150 {
151 memcached_server_free(ptr->last_disconnected_server);
152 }
153 ptr->last_disconnected_server= NULL;
154 ptr->server_failure_limit= 0;
155 }
156
157 void memcached_free(memcached_st *ptr)
158 {
159 /* If we have anything open, lets close it now */
160 memcached_quit(ptr);
161 memcached_server_list_free(memcached_server_list(ptr));
162 memcached_result_free(&ptr->result);
163
164 if (ptr->last_disconnected_server)
165 memcached_server_free(ptr->last_disconnected_server);
166
167 if (ptr->on_cleanup)
168 ptr->on_cleanup(ptr);
169
170 if (ptr->continuum)
171 libmemcached_free(ptr, ptr->continuum);
172
173 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
174 if (ptr->sasl.callbacks != NULL)
175 {
176 memcached_destroy_sasl_auth_data(ptr);
177 }
178 #endif
179
180 if (memcached_is_allocated(ptr))
181 {
182 libmemcached_free(ptr, ptr);
183 }
184 }
185
186 /*
187 clone is the destination, while source is the structure to clone.
188 If source is NULL the call is the same as if a memcached_create() was
189 called.
190 */
191 memcached_st *memcached_clone(memcached_st *clone, const memcached_st *source)
192 {
193 memcached_return_t rc= MEMCACHED_SUCCESS;
194 memcached_st *new_clone;
195
196 if (source == NULL)
197 return memcached_create(clone);
198
199 if (clone && memcached_is_allocated(clone))
200 {
201 return NULL;
202 }
203
204 new_clone= memcached_create(clone);
205
206 if (new_clone == NULL)
207 return NULL;
208
209 new_clone->flags= source->flags;
210 new_clone->send_size= source->send_size;
211 new_clone->recv_size= source->recv_size;
212 new_clone->poll_timeout= source->poll_timeout;
213 new_clone->connect_timeout= source->connect_timeout;
214 new_clone->retry_timeout= source->retry_timeout;
215 new_clone->distribution= source->distribution;
216
217 hashkit_st *hash_ptr;
218
219 hash_ptr= hashkit_clone(&new_clone->hashkit, &source->hashkit);
220 if (! hash_ptr)
221 {
222 memcached_free(new_clone);
223 return NULL;
224 }
225
226 hash_ptr= hashkit_clone(&new_clone->distribution_hashkit, &source->distribution_hashkit);
227 if (! hash_ptr)
228 {
229 memcached_free(new_clone);
230 return NULL;
231 }
232
233 new_clone->user_data= source->user_data;
234
235 new_clone->snd_timeout= source->snd_timeout;
236 new_clone->rcv_timeout= source->rcv_timeout;
237
238 new_clone->on_clone= source->on_clone;
239 new_clone->on_cleanup= source->on_cleanup;
240
241 new_clone->allocators= source->allocators;
242
243 new_clone->get_key_failure= source->get_key_failure;
244 new_clone->delete_trigger= source->delete_trigger;
245 new_clone->server_failure_limit= source->server_failure_limit;
246 new_clone->io_msg_watermark= source->io_msg_watermark;
247 new_clone->io_bytes_watermark= source->io_bytes_watermark;
248 new_clone->io_key_prefetch= source->io_key_prefetch;
249 new_clone->number_of_replicas= source->number_of_replicas;
250 new_clone->tcp_keepidle= source->tcp_keepidle;
251
252 if (memcached_server_count(source))
253 rc= memcached_push(new_clone, source);
254
255 if (rc != MEMCACHED_SUCCESS)
256 {
257 memcached_free(new_clone);
258
259 return NULL;
260 }
261
262
263 if (source->prefix_key_length)
264 {
265 strcpy(new_clone->prefix_key, source->prefix_key);
266 new_clone->prefix_key_length= source->prefix_key_length;
267 }
268
269 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
270 if (source->sasl.callbacks)
271 {
272 if (memcached_clone_sasl(new_clone, source) != MEMCACHED_SUCCESS)
273 {
274 memcached_free(new_clone);
275 return NULL;
276 }
277 }
278 #endif
279
280 rc= run_distribution(new_clone);
281
282 if (rc != MEMCACHED_SUCCESS)
283 {
284 memcached_free(new_clone);
285
286 return NULL;
287 }
288
289 if (source->on_clone)
290 source->on_clone(new_clone, source);
291
292 return new_clone;
293 }
294
295 void *memcached_get_user_data(const memcached_st *ptr)
296 {
297 return ptr->user_data;
298 }
299
300 void *memcached_set_user_data(memcached_st *ptr, void *data)
301 {
302 void *ret= ptr->user_data;
303 ptr->user_data= data;
304
305 return ret;
306 }
307
308 memcached_return_t memcached_push(memcached_st *destination, const memcached_st *source)
309 {
310 return memcached_server_push(destination, source->servers);
311 }
312
313 memcached_server_write_instance_st memcached_server_instance_fetch(memcached_st *ptr, uint32_t server_key)
314 {
315 return &ptr->servers[server_key];
316 }
317
318 memcached_server_instance_st memcached_server_instance_by_position(const memcached_st *ptr, uint32_t server_key)
319 {
320 return &ptr->servers[server_key];
321 }