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