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