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