Merge from build trunk.
[m6w6/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 set_hostinfo(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 for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
245 {
246 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x);
247
248 unsigned int iferror= (*callback)(ptr, instance, context);
249
250 if (iferror)
251 {
252 continue;
253 }
254 }
255
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 (not error)
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(rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol)))
279 {
280 *error= rc;
281 return NULL;
282 }
283
284 if (memcached_failed((memcached_key_test(*ptr, (const char **)&key, &key_length, 1))))
285 {
286 *error= MEMCACHED_BAD_KEY_PROVIDED;
287 return NULL;
288 }
289
290 uint32_t server_key= memcached_generate_hash(ptr, key, key_length);
291 return memcached_server_instance_by_position(ptr, server_key);
292
293 }
294
295 void memcached_server_error_reset(memcached_server_st *self)
296 {
297 WATCHPOINT_ASSERT(self);
298 if (not self)
299 {
300 return;
301 }
302
303 memcached_error_free(*self);
304 }
305
306 memcached_server_instance_st memcached_server_get_last_disconnect(const memcached_st *self)
307 {
308 WATCHPOINT_ASSERT(self);
309 if (not self)
310 return 0;
311
312 return self->last_disconnected_server;
313 }
314
315 uint32_t memcached_servers_set_count(memcached_server_st *servers, uint32_t count)
316 {
317 WATCHPOINT_ASSERT(servers);
318 if (not servers)
319 return 0;
320
321 return servers->number_of_hosts= count;
322 }
323
324 uint32_t memcached_server_count(const memcached_st *self)
325 {
326 WATCHPOINT_ASSERT(self);
327 if (not self)
328 return 0;
329
330 return self->number_of_hosts;
331 }
332
333 const char *memcached_server_name(const memcached_server_instance_st self)
334 {
335 WATCHPOINT_ASSERT(self);
336 if (not self)
337 return NULL;
338
339 return self->hostname;
340 }
341
342 in_port_t memcached_server_port(const memcached_server_instance_st self)
343 {
344 WATCHPOINT_ASSERT(self);
345 if (not self)
346 return 0;
347
348 return self->port;
349 }
350
351 uint32_t memcached_server_response_count(const memcached_server_instance_st self)
352 {
353 WATCHPOINT_ASSERT(self);
354 if (not self)
355 return 0;
356
357 return self->cursor_active;
358 }
359
360 const char *memcached_server_type(const memcached_server_instance_st ptr)
361 {
362 if (ptr)
363 {
364 switch (ptr->type)
365 {
366 case MEMCACHED_CONNECTION_TCP:
367 return "TCP";
368
369 case MEMCACHED_CONNECTION_UDP:
370 return "UDP";
371
372 case MEMCACHED_CONNECTION_UNIX_SOCKET:
373 return "SOCKET";
374 }
375 }
376
377 return "UNKNOWN";
378 }