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