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