Updating for 1.0.2 release
[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= -1;
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 (not self)
92 {
93 self= (memcached_server_st *)libmemcached_malloc(memc, sizeof(memcached_server_st));
94
95 if (not self)
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
135 if (memc and memcached_is_udp(memc))
136 {
137 self->write_buffer_offset= UDP_DATAGRAM_HEADER_LENGTH;
138 memcached_io_init_udp_header(self, 0);
139 }
140
141 return self;
142 }
143
144 void __server_free(memcached_server_st *self)
145 {
146 memcached_quit_server(self, false);
147
148 if (self->address_info)
149 {
150 freeaddrinfo(self->address_info);
151 self->address_info= NULL;
152 self->address_info_next= NULL;
153 }
154
155 memcached_error_free(*self);
156
157 if (memcached_is_allocated(self))
158 {
159 libmemcached_free(self->root, self);
160 }
161 else
162 {
163 self->options.is_initialized= false;
164 }
165 }
166
167 void memcached_server_free(memcached_server_st *self)
168 {
169 if (self == NULL)
170 {
171 return;
172 }
173
174 if (memcached_server_list_count(self))
175 {
176 memcached_server_list_free(self);
177 return;
178 }
179
180 __server_free(self);
181 }
182
183 /*
184 If we do not have a valid object to clone from, we toss an error.
185 */
186 memcached_server_st *memcached_server_clone(memcached_server_st *destination,
187 memcached_server_st *source)
188 {
189 /* We just do a normal create if source is missing */
190 if (source == NULL)
191 {
192 return NULL;
193 }
194
195 memcached_string_t hostname= { memcached_string_make_from_cstr(source->hostname) };
196 destination= __server_create_with(source->root, destination,
197 hostname,
198 source->port, source->weight,
199 source->type);
200 if (destination)
201 {
202 if (source->error_messages)
203 {
204 destination->error_messages= memcached_error_copy(*source);
205 }
206 }
207
208 return destination;
209
210 }
211
212 memcached_return_t memcached_server_cursor(const memcached_st *ptr,
213 const memcached_server_fn *callback,
214 void *context,
215 uint32_t number_of_callbacks)
216 {
217 memcached_return_t rc;
218 if (memcached_failed(rc= initialize_const_query(ptr)))
219 {
220 return rc;
221 }
222
223 size_t errors= 0;
224 for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
225 {
226 memcached_server_instance_st instance=
227 memcached_server_instance_by_position(ptr, x);
228
229 for (uint32_t y= 0; y < number_of_callbacks; y++)
230 {
231 memcached_return_t ret= (*callback[y])(ptr, instance, context);
232
233 if (memcached_failed(ret))
234 {
235 errors++;
236 continue;
237 }
238 }
239 }
240
241 return errors ? MEMCACHED_SOME_ERRORS : MEMCACHED_SUCCESS;
242 }
243
244 memcached_return_t memcached_server_execute(memcached_st *ptr,
245 memcached_server_execute_fn callback,
246 void *context)
247 {
248 for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
249 {
250 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x);
251
252 unsigned int iferror= (*callback)(ptr, instance, context);
253
254 if (iferror)
255 {
256 continue;
257 }
258 }
259
260 return MEMCACHED_SUCCESS;
261 }
262
263 memcached_server_instance_st memcached_server_by_key(memcached_st *ptr,
264 const char *key,
265 size_t key_length,
266 memcached_return_t *error)
267 {
268 memcached_return_t unused;
269 if (not error)
270 {
271 error= &unused;
272 }
273
274
275 memcached_return_t rc;
276 if (memcached_failed(rc= initialize_const_query(ptr)))
277 {
278 *error= rc;
279 return NULL;
280 }
281
282 if (memcached_failed(rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol)))
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_BAD_KEY_PROVIDED;
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 (not self)
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 }