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