aacdad64a3d2262f92a4e137a6c5250e32715c46
[awesomized/libmemcached] / libmemcached / instance.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 static inline void _server_init(org::libmemcached::Instance* self, memcached_st *root,
41 const memcached_string_t& hostname,
42 in_port_t port,
43 uint32_t weight, memcached_connection_t type)
44 {
45 self->options.is_shutting_down= false;
46 self->options.is_dead= false;
47 self->cursor_active_= 0;
48 self->port_= port;
49 self->fd= INVALID_SOCKET;
50 self->io_bytes_sent= 0;
51 self->request_id= 0;
52 self->server_failure_counter= 0;
53 self->server_failure_counter_query_id= 0;
54 self->weight= weight ? weight : 1; // 1 is the default weight value
55 self->io_wait_count.read= 0;
56 self->io_wait_count.write= 0;
57 self->io_wait_count.timeouts= 0;
58 self->io_wait_count._bytes_read= 0;
59 self->major_version= UINT8_MAX;
60 self->micro_version= UINT8_MAX;
61 self->minor_version= UINT8_MAX;
62 self->type= type;
63 self->error_messages= NULL;
64 self->read_ptr= self->read_buffer;
65 self->read_buffer_length= 0;
66 self->read_data_length= 0;
67 self->write_buffer_offset= 0;
68 self->address_info= NULL;
69 self->address_info_next= NULL;
70
71 self->state= MEMCACHED_SERVER_STATE_NEW;
72 self->next_retry= 0;
73
74 self->root= root;
75 if (root)
76 {
77 self->version= ++root->server_info.version;
78 }
79 else
80 {
81 self->version= UINT_MAX;
82 }
83 self->limit_maxbytes= 0;
84 memcpy(self->hostname, hostname.c_str, hostname.size);
85 self->hostname[hostname.size]= 0;
86 }
87
88 static org::libmemcached::Instance* _server_create(org::libmemcached::Instance* self, const memcached_st *memc)
89 {
90 if (self == NULL)
91 {
92 self= libmemcached_xmalloc(memc, org::libmemcached::Instance);
93
94 if (self == NULL)
95 {
96 return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
97 }
98
99 self->options.is_allocated= true;
100 }
101 else
102 {
103 self->options.is_allocated= false;
104 }
105
106 self->options.is_initialized= true;
107
108 return self;
109 }
110
111 org::libmemcached::Instance* __instance_create_with(memcached_st *memc,
112 org::libmemcached::Instance* self,
113 const memcached_string_t& hostname,
114 const in_port_t port,
115 uint32_t weight,
116 const memcached_connection_t type)
117 {
118 if (memcached_is_valid_servername(hostname) == false)
119 {
120 memcached_set_error(*memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid hostname provided"));
121 return NULL;
122 }
123
124 self= _server_create(self, memc);
125
126 if (self == NULL)
127 {
128 return NULL;
129 }
130
131 _server_init(self, const_cast<memcached_st *>(memc), hostname, port, weight, type);
132
133 if (memc and memcached_is_udp(memc))
134 {
135 self->write_buffer_offset= UDP_DATAGRAM_HEADER_LENGTH;
136 memcached_io_init_udp_header(self, 0);
137 }
138
139 if (memc)
140 {
141 memcached_connect_try(self);
142 }
143
144 return self;
145 }
146
147 void __instance_free(org::libmemcached::Instance* self)
148 {
149 memcached_quit_server(self, false);
150
151 if (self->address_info)
152 {
153 freeaddrinfo(self->address_info);
154 self->address_info= NULL;
155 self->address_info_next= NULL;
156 }
157
158 memcached_error_free(*self);
159
160 if (memcached_is_allocated(self))
161 {
162 libmemcached_free(self->root, self);
163 }
164 else
165 {
166 self->options.is_initialized= false;
167 }
168 }
169
170 void memcached_instance_free(org::libmemcached::Instance* self)
171 {
172 if (self == NULL)
173 {
174 return;
175 }
176
177 __instance_free(self);
178 }
179
180 /*
181 If we do not have a valid object to clone from, we toss an error.
182 */
183 memcached_server_st *memcached_instance_2_server(org::libmemcached::Instance* source)
184 {
185 /* We just do a normal create if source is missing */
186 if (source == NULL)
187 {
188 return NULL;
189 }
190
191 memcached_string_t hostname= { memcached_string_make_from_cstr(source->hostname) };
192 return __server_create_with(source->root, NULL,
193 hostname,
194 source->port(), source->weight,
195 source->type);
196
197 }
198
199 memcached_return_t memcached_server_cursor(const memcached_st *ptr,
200 const memcached_server_fn *callback,
201 void *context,
202 uint32_t number_of_callbacks)
203 {
204 memcached_return_t rc;
205 if (memcached_failed(rc= initialize_const_query(ptr)))
206 {
207 return rc;
208 }
209
210 size_t errors= 0;
211 for (uint32_t x= 0; x < memcached_instance_list_count(ptr); x++)
212 {
213 org::libmemcached::Instance* instance= memcached_instance_by_position(ptr, x);
214
215 for (uint32_t y= 0; y < number_of_callbacks; y++)
216 {
217 memcached_return_t ret= (*callback[y])(ptr, instance, context);
218
219 if (memcached_failed(ret))
220 {
221 errors++;
222 continue;
223 }
224 }
225 }
226
227 return errors ? MEMCACHED_SOME_ERRORS : MEMCACHED_SUCCESS;
228 }
229
230 memcached_return_t memcached_server_execute(memcached_st *ptr,
231 memcached_server_execute_fn callback,
232 void *context)
233 {
234 if (callback == NULL)
235 {
236 return MEMCACHED_INVALID_ARGUMENTS;
237 }
238
239 bool some_errors= false;;
240 for (uint32_t x= 0; x < memcached_instance_list_count(ptr); x++)
241 {
242 org::libmemcached::Instance* instance= memcached_instance_fetch(ptr, x);
243
244 memcached_return_t rc= (*callback)(ptr, instance, context);
245 if (rc == MEMCACHED_INVALID_ARGUMENTS)
246 {
247 return rc;
248 }
249 else if (memcached_fatal(rc))
250 {
251 some_errors= true;
252 }
253 }
254
255 (void)some_errors;
256 return MEMCACHED_SUCCESS;
257 }
258
259 memcached_server_instance_st memcached_server_by_key(memcached_st *ptr,
260 const char *key,
261 size_t key_length,
262 memcached_return_t *error)
263 {
264 memcached_return_t unused;
265 if (error == NULL)
266 {
267 error= &unused;
268 }
269
270
271 memcached_return_t rc;
272 if (memcached_failed(rc= initialize_const_query(ptr)))
273 {
274 *error= rc;
275 return NULL;
276 }
277
278 if (memcached_failed((memcached_key_test(*ptr, (const char **)&key, &key_length, 1))))
279 {
280 *error= memcached_last_error(ptr);
281 return NULL;
282 }
283
284 uint32_t server_key= memcached_generate_hash(ptr, key, key_length);
285 return memcached_instance_by_position(ptr, server_key);
286 }
287
288 /*
289 If we do not have a valid object to clone from, we toss an error.
290 */
291 static org::libmemcached::Instance* memcached_instance_clone(org::libmemcached::Instance* source)
292 {
293 /* We just do a normal create if source is missing */
294 if (source == NULL)
295 {
296 return NULL;
297 }
298
299 memcached_string_t hostname= { memcached_string_make_from_cstr(source->hostname) };
300 return __instance_create_with(source->root,
301 NULL,
302 hostname,
303 source->port(), source->weight,
304 source->type);
305 }
306
307 void set_last_disconnected_host(org::libmemcached::Instance* self)
308 {
309 assert(self->root);
310 if (self->root == NULL)
311 {
312 return;
313 }
314
315 if (memcached_server_get_last_disconnect(self->root) and
316 memcached_server_get_last_disconnect(self->root)->version == self->version)
317 {
318 return;
319 }
320
321 // const_cast
322 memcached_st *root= (memcached_st *)self->root;
323
324 memcached_instance_free((org::libmemcached::Instance*)(root->last_disconnected_server));
325 root->last_disconnected_server= memcached_instance_clone(self);
326 ((org::libmemcached::Instance*)memcached_server_get_last_disconnect(root))->version= self->version;
327 }
328
329 memcached_server_instance_st memcached_server_get_last_disconnect(const memcached_st *self)
330 {
331 WATCHPOINT_ASSERT(self);
332 if (self == NULL)
333 {
334 return 0;
335 }
336
337 return (memcached_server_instance_st)self->last_disconnected_server;
338 }
339
340 const char *memcached_instance_name(const memcached_server_instance_st self)
341 {
342 WATCHPOINT_ASSERT(self);
343 if (self == NULL)
344 return NULL;
345
346 return self->hostname;
347 }
348
349 in_port_t memcached_instance_port(const memcached_server_instance_st self)
350 {
351 WATCHPOINT_ASSERT(self);
352 if (self == NULL)
353 {
354 return 0;
355 }
356
357 return self->port();
358 }
359
360 void memcached_instance_next_retry(memcached_server_instance_st self, const time_t absolute_time)
361 {
362 WATCHPOINT_ASSERT(self);
363 if (self)
364 {
365 ((org::libmemcached::Instance*)self)->next_retry= absolute_time;
366 }
367 }
368
369 uint32_t memcached_instance_response_count(const org::libmemcached::Instance* self)
370 {
371 WATCHPOINT_ASSERT(self);
372 if (self == NULL)
373 {
374 return 0;
375 }
376
377 return self->cursor_active_;
378 }