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