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