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