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