Merge Monty.
[awesomized/libmemcached] / libmemcached / hosts.c
1 #include "common.h"
2 #include <math.h>
3
4 /* Protoypes (static) */
5 static memcached_return_t server_add(memcached_st *ptr, const char *hostname,
6 in_port_t port,
7 uint32_t weight,
8 memcached_connection_t type);
9 static memcached_return_t update_continuum(memcached_st *ptr);
10
11 static int compare_servers(const void *p1, const void *p2)
12 {
13 int return_value;
14 memcached_server_st *a= (memcached_server_st *)p1;
15 memcached_server_st *b= (memcached_server_st *)p2;
16
17 return_value= strcmp(a->hostname, b->hostname);
18
19 if (return_value == 0)
20 {
21 return_value= (int) (a->port - b->port);
22 }
23
24 return return_value;
25 }
26
27 static void sort_hosts(memcached_st *ptr)
28 {
29 if (ptr->number_of_hosts)
30 {
31 qsort(ptr->hosts, ptr->number_of_hosts, sizeof(memcached_server_st), compare_servers);
32 ptr->hosts[0].count= (uint16_t) ptr->number_of_hosts;
33 }
34 }
35
36
37 memcached_return_t run_distribution(memcached_st *ptr)
38 {
39 if (ptr->flags.use_sort_hosts)
40 sort_hosts(ptr);
41
42 switch (ptr->distribution)
43 {
44 case MEMCACHED_DISTRIBUTION_CONSISTENT:
45 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA:
46 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY:
47 return update_continuum(ptr);
48 case MEMCACHED_DISTRIBUTION_MODULA:
49 break;
50 case MEMCACHED_DISTRIBUTION_RANDOM:
51 srandom((uint32_t) time(NULL));
52 break;
53 case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX:
54 default:
55 WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
56 }
57
58 ptr->last_disconnected_server = NULL;
59
60 return MEMCACHED_SUCCESS;
61 }
62
63 void server_list_free(memcached_st *ptr, memcached_server_st *servers)
64 {
65 unsigned int x;
66
67 if (servers == NULL)
68 return;
69
70 for (x= 0; x < servers->count; x++)
71 if (servers[x].address_info)
72 {
73 freeaddrinfo(servers[x].address_info);
74 servers[x].address_info= NULL;
75 }
76
77 if (ptr)
78 ptr->call_free(ptr, servers);
79 else
80 free(servers);
81 }
82
83 static uint32_t ketama_server_hash(const char *key, unsigned int key_length, int alignment)
84 {
85 unsigned char results[16];
86
87 md5_signature((unsigned char*)key, key_length, results);
88 return ((uint32_t) (results[3 + alignment * 4] & 0xFF) << 24)
89 | ((uint32_t) (results[2 + alignment * 4] & 0xFF) << 16)
90 | ((uint32_t) (results[1 + alignment * 4] & 0xFF) << 8)
91 | (results[0 + alignment * 4] & 0xFF);
92 }
93
94 static int continuum_item_cmp(const void *t1, const void *t2)
95 {
96 memcached_continuum_item_st *ct1= (memcached_continuum_item_st *)t1;
97 memcached_continuum_item_st *ct2= (memcached_continuum_item_st *)t2;
98
99 /* Why 153? Hmmm... */
100 WATCHPOINT_ASSERT(ct1->value != 153);
101 if (ct1->value == ct2->value)
102 return 0;
103 else if (ct1->value > ct2->value)
104 return 1;
105 else
106 return -1;
107 }
108
109 static memcached_return_t update_continuum(memcached_st *ptr)
110 {
111 uint32_t host_index;
112 uint32_t continuum_index= 0;
113 uint32_t value;
114 memcached_server_st *list;
115 uint32_t pointer_index;
116 uint32_t pointer_counter= 0;
117 uint32_t pointer_per_server= MEMCACHED_POINTS_PER_SERVER;
118 uint32_t pointer_per_hash= 1;
119 uint64_t total_weight= 0;
120 uint64_t is_ketama_weighted= 0;
121 uint64_t is_auto_ejecting= 0;
122 uint32_t points_per_server= 0;
123 uint32_t live_servers= 0;
124 struct timeval now;
125
126 if (gettimeofday(&now, NULL) != 0)
127 {
128 ptr->cached_errno = errno;
129 return MEMCACHED_ERRNO;
130 }
131
132 list = ptr->hosts;
133
134 /* count live servers (those without a retry delay set) */
135 is_auto_ejecting= memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS);
136 if (is_auto_ejecting)
137 {
138 live_servers= 0;
139 ptr->next_distribution_rebuild= 0;
140 for (host_index= 0; host_index < ptr->number_of_hosts; ++host_index)
141 {
142 if (list[host_index].next_retry <= now.tv_sec)
143 live_servers++;
144 else
145 {
146 if (ptr->next_distribution_rebuild == 0 || list[host_index].next_retry < ptr->next_distribution_rebuild)
147 ptr->next_distribution_rebuild= list[host_index].next_retry;
148 }
149 }
150 }
151 else
152 live_servers= ptr->number_of_hosts;
153
154 is_ketama_weighted= memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
155 points_per_server= (uint32_t) (is_ketama_weighted ? MEMCACHED_POINTS_PER_SERVER_KETAMA : MEMCACHED_POINTS_PER_SERVER);
156
157 if (live_servers == 0)
158 return MEMCACHED_SUCCESS;
159
160 if (live_servers > ptr->continuum_count)
161 {
162 memcached_continuum_item_st *new_ptr;
163
164 new_ptr= ptr->call_realloc(ptr, ptr->continuum,
165 sizeof(memcached_continuum_item_st) * (live_servers + MEMCACHED_CONTINUUM_ADDITION) * points_per_server);
166
167 if (new_ptr == 0)
168 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
169
170 ptr->continuum= new_ptr;
171 ptr->continuum_count= live_servers + MEMCACHED_CONTINUUM_ADDITION;
172 }
173
174 if (is_ketama_weighted)
175 {
176 for (host_index = 0; host_index < ptr->number_of_hosts; ++host_index)
177 {
178 if (list[host_index].weight == 0)
179 {
180 list[host_index].weight = 1;
181 }
182 if (!is_auto_ejecting || list[host_index].next_retry <= now.tv_sec)
183 total_weight += list[host_index].weight;
184 }
185 }
186
187 for (host_index = 0; host_index < ptr->number_of_hosts; ++host_index)
188 {
189 if (is_auto_ejecting && list[host_index].next_retry > now.tv_sec)
190 continue;
191
192 if (is_ketama_weighted)
193 {
194 float pct = (float)list[host_index].weight / (float)total_weight;
195 pointer_per_server= (uint32_t) ((floorf((float) (pct * MEMCACHED_POINTS_PER_SERVER_KETAMA / 4 * (float)live_servers + 0.0000000001))) * 4);
196 pointer_per_hash= 4;
197 #ifdef DEBUG
198 printf("ketama_weighted:%s|%d|%llu|%u\n",
199 list[host_index].hostname,
200 list[host_index].port,
201 (unsigned long long)list[host_index].weight,
202 pointer_per_server);
203 #endif
204 }
205
206
207 if (ptr->distribution == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY)
208 {
209 for (pointer_index= 0;
210 pointer_index < pointer_per_server / pointer_per_hash;
211 pointer_index++)
212 {
213 char sort_host[MEMCACHED_MAX_HOST_SORT_LENGTH]= "";
214 size_t sort_host_length;
215
216 // Spymemcached ketema key format is: hostname/ip:port-index
217 // If hostname is not available then: /ip:port-index
218 sort_host_length= (size_t) snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH,
219 "/%s:%u-%u",
220 list[host_index].hostname,
221 (uint32_t)list[host_index].port,
222 pointer_index);
223 #ifdef DEBUG
224 printf("update_continuum: key is %s\n", sort_host);
225 #endif
226
227 WATCHPOINT_ASSERT(sort_host_length);
228
229 if (is_ketama_weighted)
230 {
231 unsigned int i;
232 for (i = 0; i < pointer_per_hash; i++)
233 {
234 value= ketama_server_hash(sort_host, (uint32_t) sort_host_length, (int) i);
235 ptr->continuum[continuum_index].index= host_index;
236 ptr->continuum[continuum_index++].value= value;
237 }
238 }
239 else
240 {
241 value= memcached_generate_hash_value(sort_host, sort_host_length, ptr->distribution_hash);
242 ptr->continuum[continuum_index].index= host_index;
243 ptr->continuum[continuum_index++].value= value;
244 }
245 }
246 }
247 else
248 {
249 for (pointer_index= 1;
250 pointer_index <= pointer_per_server / pointer_per_hash;
251 pointer_index++)
252 {
253 char sort_host[MEMCACHED_MAX_HOST_SORT_LENGTH]= "";
254 size_t sort_host_length;
255
256 if (list[host_index].port == MEMCACHED_DEFAULT_PORT)
257 {
258 sort_host_length= (size_t) snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH,
259 "%s-%u",
260 list[host_index].hostname,
261 pointer_index - 1);
262 }
263 else
264 {
265 sort_host_length= (size_t) snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH,
266 "%s:%u-%u",
267 list[host_index].hostname,
268 (uint32_t)list[host_index].port,
269 pointer_index - 1);
270 }
271
272 WATCHPOINT_ASSERT(sort_host_length);
273
274 if (is_ketama_weighted)
275 {
276 unsigned int i;
277 for (i = 0; i < pointer_per_hash; i++)
278 {
279 value= ketama_server_hash(sort_host, (uint32_t) sort_host_length, (int) i);
280 ptr->continuum[continuum_index].index= host_index;
281 ptr->continuum[continuum_index++].value= value;
282 }
283 }
284 else
285 {
286 value= memcached_generate_hash_value(sort_host, sort_host_length, ptr->distribution_hash);
287 ptr->continuum[continuum_index].index= host_index;
288 ptr->continuum[continuum_index++].value= value;
289 }
290 }
291 }
292
293 pointer_counter+= pointer_per_server;
294 }
295
296 WATCHPOINT_ASSERT(ptr);
297 WATCHPOINT_ASSERT(ptr->continuum);
298 WATCHPOINT_ASSERT(ptr->number_of_hosts * MEMCACHED_POINTS_PER_SERVER <= MEMCACHED_CONTINUUM_SIZE);
299 ptr->continuum_points_counter= pointer_counter;
300 qsort(ptr->continuum, ptr->continuum_points_counter, sizeof(memcached_continuum_item_st), continuum_item_cmp);
301
302 #ifdef DEBUG
303 for (pointer_index= 0; ptr->number_of_hosts && pointer_index < ((live_servers * MEMCACHED_POINTS_PER_SERVER) - 1); pointer_index++)
304 {
305 WATCHPOINT_ASSERT(ptr->continuum[pointer_index].value <= ptr->continuum[pointer_index + 1].value);
306 }
307 #endif
308
309 return MEMCACHED_SUCCESS;
310 }
311
312
313 memcached_return_t memcached_server_push(memcached_st *ptr, memcached_server_st *list)
314 {
315 unsigned int x;
316 uint16_t count;
317 memcached_server_st *new_host_list;
318
319 if (!list)
320 return MEMCACHED_SUCCESS;
321
322 count= list[0].count;
323 new_host_list= ptr->call_realloc(ptr, ptr->hosts,
324 sizeof(memcached_server_st) * (count + ptr->number_of_hosts));
325
326 if (!new_host_list)
327 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
328
329 ptr->hosts= new_host_list;
330
331 for (x= 0; x < count; x++)
332 {
333 if ((ptr->flags.use_udp && list[x].type != MEMCACHED_CONNECTION_UDP)
334 || ((list[x].type == MEMCACHED_CONNECTION_UDP)
335 && ! (ptr->flags.use_udp)) )
336 return MEMCACHED_INVALID_HOST_PROTOCOL;
337
338 WATCHPOINT_ASSERT(list[x].hostname[0] != 0);
339 memcached_server_create(ptr, &ptr->hosts[ptr->number_of_hosts]);
340 /* TODO check return type */
341 (void)memcached_server_create_with(ptr, &ptr->hosts[ptr->number_of_hosts], list[x].hostname,
342 list[x].port, list[x].weight, list[x].type);
343 ptr->number_of_hosts++;
344 }
345 ptr->hosts[0].count= (uint16_t) ptr->number_of_hosts;
346
347 return run_distribution(ptr);
348 }
349
350 memcached_return_t memcached_server_add_unix_socket(memcached_st *ptr,
351 const char *filename)
352 {
353 return memcached_server_add_unix_socket_with_weight(ptr, filename, 0);
354 }
355
356 memcached_return_t memcached_server_add_unix_socket_with_weight(memcached_st *ptr,
357 const char *filename,
358 uint32_t weight)
359 {
360 if (!filename)
361 return MEMCACHED_FAILURE;
362
363 return server_add(ptr, filename, 0, weight, MEMCACHED_CONNECTION_UNIX_SOCKET);
364 }
365
366 memcached_return_t memcached_server_add_udp(memcached_st *ptr,
367 const char *hostname,
368 in_port_t port)
369 {
370 return memcached_server_add_udp_with_weight(ptr, hostname, port, 0);
371 }
372
373 memcached_return_t memcached_server_add_udp_with_weight(memcached_st *ptr,
374 const char *hostname,
375 in_port_t port,
376 uint32_t weight)
377 {
378 if (!port)
379 port= MEMCACHED_DEFAULT_PORT;
380
381 if (!hostname)
382 hostname= "localhost";
383
384 return server_add(ptr, hostname, port, weight, MEMCACHED_CONNECTION_UDP);
385 }
386
387 memcached_return_t memcached_server_add(memcached_st *ptr,
388 const char *hostname,
389 in_port_t port)
390 {
391 return memcached_server_add_with_weight(ptr, hostname, port, 0);
392 }
393
394 memcached_return_t memcached_server_add_with_weight(memcached_st *ptr,
395 const char *hostname,
396 in_port_t port,
397 uint32_t weight)
398 {
399 if (!port)
400 port= MEMCACHED_DEFAULT_PORT;
401
402 if (!hostname)
403 hostname= "localhost";
404
405 return server_add(ptr, hostname, port, weight, MEMCACHED_CONNECTION_TCP);
406 }
407
408 static memcached_return_t server_add(memcached_st *ptr, const char *hostname,
409 in_port_t port,
410 uint32_t weight,
411 memcached_connection_t type)
412 {
413 memcached_server_st *new_host_list;
414
415 if ( (ptr->flags.use_udp && type != MEMCACHED_CONNECTION_UDP)
416 || ( (type == MEMCACHED_CONNECTION_UDP) && (! ptr->flags.use_udp) ) )
417 return MEMCACHED_INVALID_HOST_PROTOCOL;
418
419 new_host_list= ptr->call_realloc(ptr, ptr->hosts,
420 sizeof(memcached_server_st) * (ptr->number_of_hosts+1));
421
422 if (new_host_list == NULL)
423 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
424
425 ptr->hosts= new_host_list;
426
427 /* TODO: Check return type */
428 (void)memcached_server_create_with(ptr, &ptr->hosts[ptr->number_of_hosts], hostname, port, weight, type);
429 ptr->number_of_hosts++;
430 ptr->hosts[0].count= (uint16_t) ptr->number_of_hosts;
431
432 return run_distribution(ptr);
433 }
434
435 memcached_return_t memcached_server_remove(memcached_server_st *st_ptr)
436 {
437 uint32_t x, host_index;
438 memcached_st *ptr= st_ptr->root;
439 memcached_server_st *list= ptr->hosts;
440
441 for (x= 0, host_index= 0; x < ptr->number_of_hosts; x++)
442 {
443 if (strncmp(list[x].hostname, st_ptr->hostname, MEMCACHED_MAX_HOST_LENGTH) != 0 || list[x].port != st_ptr->port)
444 {
445 if (host_index != x)
446 memcpy(list+host_index, list+x, sizeof(memcached_server_st));
447 host_index++;
448 }
449 }
450 ptr->number_of_hosts= host_index;
451
452 if (st_ptr->address_info)
453 {
454 freeaddrinfo(st_ptr->address_info);
455 st_ptr->address_info= NULL;
456 }
457 run_distribution(ptr);
458
459 return MEMCACHED_SUCCESS;
460 }
461
462 memcached_server_st *memcached_server_list_append(memcached_server_st *ptr,
463 const char *hostname, in_port_t port,
464 memcached_return_t *error)
465 {
466 return memcached_server_list_append_with_weight(ptr, hostname, port, 0, error);
467 }
468
469 memcached_server_st *memcached_server_list_append_with_weight(memcached_server_st *ptr,
470 const char *hostname, in_port_t port,
471 uint32_t weight,
472 memcached_return_t *error)
473 {
474 unsigned int count;
475 memcached_server_st *new_host_list;
476
477 if (hostname == NULL || error == NULL)
478 return NULL;
479
480 if (!port)
481 port= MEMCACHED_DEFAULT_PORT;
482
483 /* Increment count for hosts */
484 count= 1;
485 if (ptr != NULL)
486 {
487 count+= ptr[0].count;
488 }
489
490 new_host_list= (memcached_server_st *)realloc(ptr, sizeof(memcached_server_st) * count);
491 if (!new_host_list)
492 {
493 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
494 return NULL;
495 }
496
497 /* TODO: Check return type */
498 memcached_server_create_with(NULL, &new_host_list[count-1], hostname, port, weight, MEMCACHED_CONNECTION_TCP);
499
500 /* Backwards compatibility hack */
501 new_host_list[0].count= (uint16_t) count;
502
503 *error= MEMCACHED_SUCCESS;
504 return new_host_list;
505 }
506
507 unsigned int memcached_server_list_count(memcached_server_st *ptr)
508 {
509 if (ptr == NULL)
510 return 0;
511
512 return ptr[0].count;
513 }
514
515 void memcached_server_list_free(memcached_server_st *ptr)
516 {
517 server_list_free(NULL, ptr);
518 }