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