Merged from build.
[m6w6/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 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 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-%d",
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:%d-%d",
267 list[host_index].hostname,
268 list[host_index].port, pointer_index - 1);
269 }
270
271 WATCHPOINT_ASSERT(sort_host_length);
272
273 if (is_ketama_weighted)
274 {
275 unsigned int i;
276 for (i = 0; i < pointer_per_hash; i++)
277 {
278 value= ketama_server_hash(sort_host, (uint32_t) sort_host_length, (int) i);
279 ptr->continuum[continuum_index].index= host_index;
280 ptr->continuum[continuum_index++].value= value;
281 }
282 }
283 else
284 {
285 value= memcached_generate_hash_value(sort_host, sort_host_length, ptr->distribution_hash);
286 ptr->continuum[continuum_index].index= host_index;
287 ptr->continuum[continuum_index++].value= value;
288 }
289 }
290 }
291
292 pointer_counter+= pointer_per_server;
293 }
294
295 WATCHPOINT_ASSERT(ptr);
296 WATCHPOINT_ASSERT(ptr->continuum);
297 WATCHPOINT_ASSERT(ptr->number_of_hosts * MEMCACHED_POINTS_PER_SERVER <= MEMCACHED_CONTINUUM_SIZE);
298 ptr->continuum_points_counter= pointer_counter;
299 qsort(ptr->continuum, ptr->continuum_points_counter, sizeof(memcached_continuum_item_st), continuum_item_cmp);
300
301 #ifdef DEBUG
302 for (pointer_index= 0; ptr->number_of_hosts && pointer_index < ((live_servers * MEMCACHED_POINTS_PER_SERVER) - 1); pointer_index++)
303 {
304 WATCHPOINT_ASSERT(ptr->continuum[pointer_index].value <= ptr->continuum[pointer_index + 1].value);
305 }
306 #endif
307
308 return MEMCACHED_SUCCESS;
309 }
310
311
312 memcached_return_t memcached_server_push(memcached_st *ptr, memcached_server_st *list)
313 {
314 unsigned int x;
315 uint16_t count;
316 memcached_server_st *new_host_list;
317
318 if (!list)
319 return MEMCACHED_SUCCESS;
320
321 count= list[0].count;
322 new_host_list= ptr->call_realloc(ptr, ptr->hosts,
323 sizeof(memcached_server_st) * (count + ptr->number_of_hosts));
324
325 if (!new_host_list)
326 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
327
328 ptr->hosts= new_host_list;
329
330 for (x= 0; x < count; x++)
331 {
332 if ((ptr->flags.use_udp && list[x].type != MEMCACHED_CONNECTION_UDP)
333 || ((list[x].type == MEMCACHED_CONNECTION_UDP)
334 && ! (ptr->flags.use_udp)) )
335 return MEMCACHED_INVALID_HOST_PROTOCOL;
336
337 WATCHPOINT_ASSERT(list[x].hostname[0] != 0);
338 memcached_server_create(ptr, &ptr->hosts[ptr->number_of_hosts]);
339 /* TODO check return type */
340 (void)memcached_server_create_with(ptr, &ptr->hosts[ptr->number_of_hosts], list[x].hostname,
341 list[x].port, list[x].weight, list[x].type);
342 ptr->number_of_hosts++;
343 }
344 ptr->hosts[0].count= (uint16_t) ptr->number_of_hosts;
345
346 return run_distribution(ptr);
347 }
348
349 memcached_return_t memcached_server_add_unix_socket(memcached_st *ptr,
350 const char *filename)
351 {
352 return memcached_server_add_unix_socket_with_weight(ptr, filename, 0);
353 }
354
355 memcached_return_t memcached_server_add_unix_socket_with_weight(memcached_st *ptr,
356 const char *filename,
357 uint32_t weight)
358 {
359 if (!filename)
360 return MEMCACHED_FAILURE;
361
362 return server_add(ptr, filename, 0, weight, MEMCACHED_CONNECTION_UNIX_SOCKET);
363 }
364
365 memcached_return_t memcached_server_add_udp(memcached_st *ptr,
366 const char *hostname,
367 in_port_t port)
368 {
369 return memcached_server_add_udp_with_weight(ptr, hostname, port, 0);
370 }
371
372 memcached_return_t memcached_server_add_udp_with_weight(memcached_st *ptr,
373 const char *hostname,
374 in_port_t port,
375 uint32_t weight)
376 {
377 if (!port)
378 port= MEMCACHED_DEFAULT_PORT;
379
380 if (!hostname)
381 hostname= "localhost";
382
383 return server_add(ptr, hostname, port, weight, MEMCACHED_CONNECTION_UDP);
384 }
385
386 memcached_return_t memcached_server_add(memcached_st *ptr,
387 const char *hostname,
388 in_port_t port)
389 {
390 return memcached_server_add_with_weight(ptr, hostname, port, 0);
391 }
392
393 memcached_return_t memcached_server_add_with_weight(memcached_st *ptr,
394 const char *hostname,
395 in_port_t port,
396 uint32_t weight)
397 {
398 if (!port)
399 port= MEMCACHED_DEFAULT_PORT;
400
401 if (!hostname)
402 hostname= "localhost";
403
404 return server_add(ptr, hostname, port, weight, MEMCACHED_CONNECTION_TCP);
405 }
406
407 static memcached_return_t server_add(memcached_st *ptr, const char *hostname,
408 in_port_t port,
409 uint32_t weight,
410 memcached_connection_t type)
411 {
412 memcached_server_st *new_host_list;
413
414 if ( (ptr->flags.use_udp && type != MEMCACHED_CONNECTION_UDP)
415 || ( (type == MEMCACHED_CONNECTION_UDP) && (! ptr->flags.use_udp) ) )
416 return MEMCACHED_INVALID_HOST_PROTOCOL;
417
418 new_host_list= ptr->call_realloc(ptr, ptr->hosts,
419 sizeof(memcached_server_st) * (ptr->number_of_hosts+1));
420
421 if (new_host_list == NULL)
422 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
423
424 ptr->hosts= new_host_list;
425
426 /* TODO: Check return type */
427 (void)memcached_server_create_with(ptr, &ptr->hosts[ptr->number_of_hosts], hostname, port, weight, type);
428 ptr->number_of_hosts++;
429 ptr->hosts[0].count= (uint16_t) ptr->number_of_hosts;
430
431 return run_distribution(ptr);
432 }
433
434 memcached_return_t memcached_server_remove(memcached_server_st *st_ptr)
435 {
436 uint32_t x, host_index;
437 memcached_st *ptr= st_ptr->root;
438 memcached_server_st *list= ptr->hosts;
439
440 for (x= 0, host_index= 0; x < ptr->number_of_hosts; x++)
441 {
442 if (strncmp(list[x].hostname, st_ptr->hostname, MEMCACHED_MAX_HOST_LENGTH) != 0 || list[x].port != st_ptr->port)
443 {
444 if (host_index != x)
445 memcpy(list+host_index, list+x, sizeof(memcached_server_st));
446 host_index++;
447 }
448 }
449 ptr->number_of_hosts= host_index;
450
451 if (st_ptr->address_info)
452 {
453 freeaddrinfo(st_ptr->address_info);
454 st_ptr->address_info= NULL;
455 }
456 run_distribution(ptr);
457
458 return MEMCACHED_SUCCESS;
459 }
460
461 memcached_server_st *memcached_server_list_append(memcached_server_st *ptr,
462 const char *hostname, in_port_t port,
463 memcached_return_t *error)
464 {
465 return memcached_server_list_append_with_weight(ptr, hostname, port, 0, error);
466 }
467
468 memcached_server_st *memcached_server_list_append_with_weight(memcached_server_st *ptr,
469 const char *hostname, in_port_t port,
470 uint32_t weight,
471 memcached_return_t *error)
472 {
473 unsigned int count;
474 memcached_server_st *new_host_list;
475
476 if (hostname == NULL || error == NULL)
477 return NULL;
478
479 if (!port)
480 port= MEMCACHED_DEFAULT_PORT;
481
482 /* Increment count for hosts */
483 count= 1;
484 if (ptr != NULL)
485 {
486 count+= ptr[0].count;
487 }
488
489 new_host_list= (memcached_server_st *)realloc(ptr, sizeof(memcached_server_st) * count);
490 if (!new_host_list)
491 {
492 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
493 return NULL;
494 }
495
496 /* TODO: Check return type */
497 memcached_server_create_with(NULL, &new_host_list[count-1], hostname, port, weight, MEMCACHED_CONNECTION_TCP);
498
499 /* Backwards compatibility hack */
500 new_host_list[0].count= (uint16_t) count;
501
502 *error= MEMCACHED_SUCCESS;
503 return new_host_list;
504 }
505
506 unsigned int memcached_server_list_count(memcached_server_st *ptr)
507 {
508 if (ptr == NULL)
509 return 0;
510
511 return ptr[0].count;
512 }
513
514 void memcached_server_list_free(memcached_server_st *ptr)
515 {
516 server_list_free(NULL, ptr);
517 }