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