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