Merge in fixes for UDP
[awesomized/libmemcached] / libmemcached / hosts.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-2010 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 #include <libmemcached/common.h>
39
40 #include <cmath>
41 #include <sys/time.h>
42
43 /* Protoypes (static) */
44 static memcached_return_t update_continuum(memcached_st *ptr);
45
46 static int compare_servers(const void *p1, const void *p2)
47 {
48 memcached_server_instance_st a= (memcached_server_instance_st)p1;
49 memcached_server_instance_st b= (memcached_server_instance_st)p2;
50
51 int return_value= strcmp(a->hostname, b->hostname);
52
53 if (return_value == 0)
54 {
55 return_value= (int) (a->port - b->port);
56 }
57
58 return return_value;
59 }
60
61 static void sort_hosts(memcached_st *ptr)
62 {
63 if (memcached_server_count(ptr))
64 {
65 memcached_server_write_instance_st instance;
66
67 qsort(memcached_server_list(ptr), memcached_server_count(ptr), sizeof(memcached_server_st), compare_servers);
68 instance= memcached_server_instance_fetch(ptr, 0);
69 instance->number_of_hosts= memcached_server_count(ptr);
70 }
71 }
72
73
74 memcached_return_t run_distribution(memcached_st *ptr)
75 {
76 if (ptr->flags.use_sort_hosts)
77 {
78 sort_hosts(ptr);
79 }
80
81 switch (ptr->distribution)
82 {
83 case MEMCACHED_DISTRIBUTION_CONSISTENT:
84 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA:
85 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY:
86 case MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED:
87 return update_continuum(ptr);
88
89 case MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET:
90 case MEMCACHED_DISTRIBUTION_MODULA:
91 break;
92
93 case MEMCACHED_DISTRIBUTION_RANDOM:
94 srandom((uint32_t) time(NULL));
95 break;
96
97 case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX:
98 default:
99 assert_msg(0, "Invalid distribution type passed to run_distribution()");
100 }
101
102 return MEMCACHED_SUCCESS;
103 }
104
105 static uint32_t ketama_server_hash(const char *key, size_t key_length, uint32_t alignment)
106 {
107 unsigned char results[16];
108
109 libhashkit_md5_signature((unsigned char*)key, key_length, results);
110
111 return ((uint32_t) (results[3 + alignment * 4] & 0xFF) << 24)
112 | ((uint32_t) (results[2 + alignment * 4] & 0xFF) << 16)
113 | ((uint32_t) (results[1 + alignment * 4] & 0xFF) << 8)
114 | (results[0 + alignment * 4] & 0xFF);
115 }
116
117 static int continuum_item_cmp(const void *t1, const void *t2)
118 {
119 memcached_continuum_item_st *ct1= (memcached_continuum_item_st *)t1;
120 memcached_continuum_item_st *ct2= (memcached_continuum_item_st *)t2;
121
122 /* Why 153? Hmmm... */
123 WATCHPOINT_ASSERT(ct1->value != 153);
124 if (ct1->value == ct2->value)
125 return 0;
126 else if (ct1->value > ct2->value)
127 return 1;
128 else
129 return -1;
130 }
131
132 static memcached_return_t update_continuum(memcached_st *ptr)
133 {
134 uint32_t continuum_index= 0;
135 memcached_server_st *list;
136 uint32_t pointer_counter= 0;
137 uint32_t pointer_per_server= MEMCACHED_POINTS_PER_SERVER;
138 uint32_t pointer_per_hash= 1;
139 uint32_t live_servers= 0;
140 struct timeval now;
141
142 if (gettimeofday(&now, NULL))
143 {
144 return memcached_set_errno(*ptr, errno, MEMCACHED_AT);
145 }
146
147 list= memcached_server_list(ptr);
148
149 /* count live servers (those without a retry delay set) */
150 bool is_auto_ejecting= _is_auto_eject_host(ptr);
151 if (is_auto_ejecting)
152 {
153 live_servers= 0;
154 ptr->ketama.next_distribution_rebuild= 0;
155 for (uint32_t host_index= 0; host_index < memcached_server_count(ptr); ++host_index)
156 {
157 if (list[host_index].next_retry <= now.tv_sec)
158 {
159 live_servers++;
160 }
161 else
162 {
163 if (ptr->ketama.next_distribution_rebuild == 0 or list[host_index].next_retry < ptr->ketama.next_distribution_rebuild)
164 {
165 ptr->ketama.next_distribution_rebuild= list[host_index].next_retry;
166 }
167 }
168 }
169 }
170 else
171 {
172 live_servers= memcached_server_count(ptr);
173 }
174
175 uint64_t is_ketama_weighted= memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
176 uint32_t points_per_server= (uint32_t) (is_ketama_weighted ? MEMCACHED_POINTS_PER_SERVER_KETAMA : MEMCACHED_POINTS_PER_SERVER);
177
178 if (not live_servers)
179 {
180 return MEMCACHED_SUCCESS;
181 }
182
183 if (live_servers > ptr->ketama.continuum_count)
184 {
185 memcached_continuum_item_st *new_ptr;
186
187 new_ptr= static_cast<memcached_continuum_item_st*>(libmemcached_realloc(ptr, ptr->ketama.continuum,
188 sizeof(memcached_continuum_item_st) * (live_servers + MEMCACHED_CONTINUUM_ADDITION) * points_per_server));
189
190 if (new_ptr == 0)
191 {
192 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
193 }
194
195 ptr->ketama.continuum= new_ptr;
196 ptr->ketama.continuum_count= live_servers + MEMCACHED_CONTINUUM_ADDITION;
197 }
198
199 uint64_t total_weight= 0;
200 if (is_ketama_weighted)
201 {
202 for (uint32_t host_index = 0; host_index < memcached_server_count(ptr); ++host_index)
203 {
204 if (is_auto_ejecting == false or list[host_index].next_retry <= now.tv_sec)
205 {
206 total_weight += list[host_index].weight;
207 }
208 }
209 }
210
211 for (uint32_t host_index= 0; host_index < memcached_server_count(ptr); ++host_index)
212 {
213 if (is_auto_ejecting and list[host_index].next_retry > now.tv_sec)
214 {
215 continue;
216 }
217
218 if (is_ketama_weighted)
219 {
220 float pct= (float)list[host_index].weight / (float)total_weight;
221 pointer_per_server= (uint32_t) ((floor((float) (pct * MEMCACHED_POINTS_PER_SERVER_KETAMA / 4 * (float)live_servers + 0.0000000001))) * 4);
222 pointer_per_hash= 4;
223 if (DEBUG)
224 {
225 printf("ketama_weighted:%s|%d|%llu|%u\n",
226 list[host_index].hostname,
227 list[host_index].port,
228 (unsigned long long)list[host_index].weight,
229 pointer_per_server);
230 }
231 }
232
233
234 if (ptr->distribution == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY)
235 {
236 for (uint32_t pointer_index= 0;
237 pointer_index < pointer_per_server / pointer_per_hash;
238 pointer_index++)
239 {
240 char sort_host[MEMCACHED_MAX_HOST_SORT_LENGTH]= "";
241 int sort_host_length;
242
243 // Spymemcached ketema key format is: hostname/ip:port-index
244 // If hostname is not available then: /ip:port-index
245 sort_host_length= snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH,
246 "/%s:%u-%u",
247 list[host_index].hostname,
248 (uint32_t)list[host_index].port,
249 pointer_index);
250
251 if (sort_host_length >= MEMCACHED_MAX_HOST_SORT_LENGTH || sort_host_length < 0)
252 {
253 return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
254 memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
255 }
256
257 if (DEBUG)
258 {
259 fprintf(stdout, "update_continuum: key is %s\n", sort_host);
260 }
261
262 if (is_ketama_weighted)
263 {
264 for (uint32_t x= 0; x < pointer_per_hash; x++)
265 {
266 uint32_t value= ketama_server_hash(sort_host, (size_t)sort_host_length, x);
267 ptr->ketama.continuum[continuum_index].index= host_index;
268 ptr->ketama.continuum[continuum_index++].value= value;
269 }
270 }
271 else
272 {
273 uint32_t value= hashkit_digest(&ptr->hashkit, sort_host, (size_t)sort_host_length);
274 ptr->ketama.continuum[continuum_index].index= host_index;
275 ptr->ketama.continuum[continuum_index++].value= value;
276 }
277 }
278 }
279 else
280 {
281 for (uint32_t pointer_index= 1;
282 pointer_index <= pointer_per_server / pointer_per_hash;
283 pointer_index++)
284 {
285 char sort_host[MEMCACHED_MAX_HOST_SORT_LENGTH]= "";
286 int sort_host_length;
287
288 if (list[host_index].port == MEMCACHED_DEFAULT_PORT)
289 {
290 sort_host_length= snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH,
291 "%s-%u",
292 list[host_index].hostname,
293 pointer_index - 1);
294 }
295 else
296 {
297 sort_host_length= snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH,
298 "%s:%u-%u",
299 list[host_index].hostname,
300 (uint32_t)list[host_index].port,
301 pointer_index - 1);
302 }
303
304 if (sort_host_length >= MEMCACHED_MAX_HOST_SORT_LENGTH || sort_host_length < 0)
305 {
306 return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
307 memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
308 }
309
310 if (is_ketama_weighted)
311 {
312 for (uint32_t x = 0; x < pointer_per_hash; x++)
313 {
314 uint32_t value= ketama_server_hash(sort_host, (size_t)sort_host_length, x);
315 ptr->ketama.continuum[continuum_index].index= host_index;
316 ptr->ketama.continuum[continuum_index++].value= value;
317 }
318 }
319 else
320 {
321 uint32_t value= hashkit_digest(&ptr->hashkit, sort_host, (size_t)sort_host_length);
322 ptr->ketama.continuum[continuum_index].index= host_index;
323 ptr->ketama.continuum[continuum_index++].value= value;
324 }
325 }
326 }
327
328 pointer_counter+= pointer_per_server;
329 }
330
331 WATCHPOINT_ASSERT(ptr);
332 WATCHPOINT_ASSERT(ptr->ketama.continuum);
333 WATCHPOINT_ASSERT(memcached_server_count(ptr) * MEMCACHED_POINTS_PER_SERVER <= MEMCACHED_CONTINUUM_SIZE);
334 ptr->ketama.continuum_points_counter= pointer_counter;
335 qsort(ptr->ketama.continuum, ptr->ketama.continuum_points_counter, sizeof(memcached_continuum_item_st), continuum_item_cmp);
336
337 if (DEBUG)
338 {
339 for (uint32_t pointer_index= 0; memcached_server_count(ptr) && pointer_index < ((live_servers * MEMCACHED_POINTS_PER_SERVER) - 1); pointer_index++)
340 {
341 WATCHPOINT_ASSERT(ptr->ketama.continuum[pointer_index].value <= ptr->ketama.continuum[pointer_index + 1].value);
342 }
343 }
344
345 return MEMCACHED_SUCCESS;
346 }
347
348 static memcached_return_t server_add(memcached_st *ptr,
349 const memcached_string_t& hostname,
350 in_port_t port,
351 uint32_t weight,
352 memcached_connection_t type)
353 {
354 assert_msg(ptr, "Programmer mistake, somehow server_add() was passed a NULL memcached_st");
355
356 memcached_server_st *new_host_list= static_cast<memcached_server_st*>(libmemcached_realloc(ptr, memcached_server_list(ptr),
357 sizeof(memcached_server_st) * (ptr->number_of_hosts + 1)));
358
359 if (new_host_list == NULL)
360 {
361 return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
362 }
363
364 memcached_server_list_set(ptr, new_host_list);
365
366 /* TODO: Check return type */
367 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, memcached_server_count(ptr));
368
369 if (__server_create_with(ptr, instance, hostname, port, weight, type) == NULL)
370 {
371 return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
372 }
373
374 if (weight > 1)
375 {
376 ptr->ketama.weighted= true;
377 }
378
379 ptr->number_of_hosts++;
380
381 // @note we place the count in the bottom of the server list
382 instance= memcached_server_instance_fetch(ptr, 0);
383 memcached_servers_set_count(instance, memcached_server_count(ptr));
384
385 return run_distribution(ptr);
386 }
387
388
389 memcached_return_t memcached_server_push(memcached_st *ptr, const memcached_server_list_st list)
390 {
391 if (list == NULL)
392 {
393 return MEMCACHED_SUCCESS;
394 }
395
396 uint32_t count= memcached_server_list_count(list);
397
398 memcached_server_st *new_host_list;
399 new_host_list= static_cast<memcached_server_st*>(libmemcached_realloc(ptr, memcached_server_list(ptr),
400 sizeof(memcached_server_st) * (count + memcached_server_count(ptr))));
401
402 if (new_host_list == NULL)
403 {
404 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
405 }
406
407 memcached_server_list_set(ptr, new_host_list);
408
409 for (uint32_t x= 0; x < count; x++)
410 {
411 memcached_server_write_instance_st instance;
412
413 WATCHPOINT_ASSERT(list[x].hostname[0] != 0);
414
415 // We have extended the array, and now we will find it, and use it.
416 instance= memcached_server_instance_fetch(ptr, memcached_server_count(ptr));
417 WATCHPOINT_ASSERT(instance);
418
419 memcached_string_t hostname= { memcached_string_make_from_cstr(list[x].hostname) };
420 if (__server_create_with(ptr, instance,
421 hostname,
422 list[x].port, list[x].weight, list[x].type) == NULL)
423 {
424 return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
425 }
426
427 if (list[x].weight > 1)
428 {
429 ptr->ketama.weighted= true;
430 }
431
432 ptr->number_of_hosts++;
433 }
434
435 // Provides backwards compatibility with server list.
436 {
437 memcached_server_write_instance_st instance;
438 instance= memcached_server_instance_fetch(ptr, 0);
439 instance->number_of_hosts= memcached_server_count(ptr);
440 }
441
442 return run_distribution(ptr);
443 }
444
445 memcached_return_t memcached_server_add_unix_socket(memcached_st *ptr,
446 const char *filename)
447 {
448 return memcached_server_add_unix_socket_with_weight(ptr, filename, 0);
449 }
450
451 memcached_return_t memcached_server_add_unix_socket_with_weight(memcached_st *ptr,
452 const char *filename,
453 uint32_t weight)
454 {
455 if (ptr == NULL)
456 {
457 return MEMCACHED_FAILURE;
458 }
459
460 memcached_string_t _filename= { memcached_string_make_from_cstr(filename) };
461 if (memcached_is_valid_servername(_filename) == false)
462 {
463 memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid filename for socket provided"));
464 }
465
466 return server_add(ptr, _filename, 0, weight, MEMCACHED_CONNECTION_UNIX_SOCKET);
467 }
468
469 memcached_return_t memcached_server_add_udp(memcached_st *ptr,
470 const char *hostname,
471 in_port_t port)
472 {
473 return memcached_server_add_udp_with_weight(ptr, hostname, port, 0);
474 }
475
476 memcached_return_t memcached_server_add_udp_with_weight(memcached_st *ptr,
477 const char *,
478 in_port_t,
479 uint32_t)
480 {
481 if (ptr == NULL)
482 {
483 return MEMCACHED_INVALID_ARGUMENTS;
484 }
485
486 return memcached_set_error(*ptr, MEMCACHED_DEPRECATED, MEMCACHED_AT);
487 }
488
489 memcached_return_t memcached_server_add(memcached_st *ptr,
490 const char *hostname,
491 in_port_t port)
492 {
493 return memcached_server_add_with_weight(ptr, hostname, port, 0);
494 }
495
496 memcached_return_t memcached_server_add_with_weight(memcached_st *ptr,
497 const char *hostname,
498 in_port_t port,
499 uint32_t weight)
500 {
501 if (ptr == NULL)
502 {
503 return MEMCACHED_INVALID_ARGUMENTS;
504 }
505
506 if (port == 0)
507 {
508 port= MEMCACHED_DEFAULT_PORT;
509 }
510
511 size_t hostname_length= hostname ? strlen(hostname) : 0;
512 if (hostname_length == 0)
513 {
514 hostname= "localhost";
515 hostname_length= memcached_literal_param_size("localhost");
516 }
517
518 memcached_string_t _hostname= { hostname, hostname_length };
519
520 if (memcached_is_valid_servername(_hostname) == false)
521 {
522 return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid hostname provided"));
523 }
524
525 return server_add(ptr, _hostname, port, weight, _hostname.c_str[0] == '/' ? MEMCACHED_CONNECTION_UNIX_SOCKET : MEMCACHED_CONNECTION_TCP);
526 }
527
528 memcached_return_t memcached_server_add_parsed(memcached_st *ptr,
529 const char *hostname,
530 size_t hostname_length,
531 in_port_t port,
532 uint32_t weight)
533 {
534 char buffer[NI_MAXHOST];
535
536 memcpy(buffer, hostname, hostname_length);
537 buffer[hostname_length]= 0;
538
539 memcached_string_t _hostname= { buffer, hostname_length };
540
541 return server_add(ptr, _hostname,
542 port,
543 weight,
544 MEMCACHED_CONNECTION_TCP);
545 }