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