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