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