f56c89f17c5e2c1bc58b59f4732829e7da095421
[awesomized/libmemcached] / libmemcached / memcached_hosts.c
1 #include "common.h"
2 #include <math.h>
3
4 /* Protoypes (static) */
5 static memcached_return server_add(memcached_st *ptr, const char *hostname,
6 unsigned int port,
7 uint32_t weight,
8 memcached_connection type);
9 memcached_return 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_st *a= (memcached_server_st *)p1;
15 memcached_server_st *b= (memcached_server_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 (ptr->number_of_hosts)
30 {
31 qsort(ptr->hosts, ptr->number_of_hosts, sizeof(memcached_server_st), compare_servers);
32 ptr->hosts[0].count= (uint16_t) ptr->number_of_hosts;
33 }
34 }
35
36
37 memcached_return run_distribution(memcached_st *ptr)
38 {
39 switch (ptr->distribution)
40 {
41 case MEMCACHED_DISTRIBUTION_CONSISTENT:
42 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA:
43 return update_continuum(ptr);
44 case MEMCACHED_DISTRIBUTION_MODULA:
45 if (ptr->flags & MEM_USE_SORT_HOSTS)
46 sort_hosts(ptr);
47 break;
48 case MEMCACHED_DISTRIBUTION_RANDOM:
49 break;
50 default:
51 WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
52 }
53
54 return MEMCACHED_SUCCESS;
55 }
56
57 void server_list_free(memcached_st *ptr, memcached_server_st *servers)
58 {
59 unsigned int x;
60
61 if (servers == NULL)
62 return;
63
64 for (x= 0; x < servers->count; x++)
65 if (servers[x].address_info)
66 {
67 freeaddrinfo(servers[x].address_info);
68 servers[x].address_info= NULL;
69 }
70
71 if (ptr)
72 ptr->call_free(ptr, servers);
73 else
74 free(servers);
75 }
76
77 static uint32_t ketama_server_hash(const char *key, unsigned int key_length, int alignment)
78 {
79 unsigned char results[16];
80
81 md5_signature((unsigned char*)key, key_length, results);
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 memcached_return 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 ptr->cached_errno = errno;
123 return MEMCACHED_ERRNO;
124 }
125
126 list = ptr->hosts;
127
128 /* count live servers (those without a retry delay set) */
129 is_auto_ejecting= memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS);
130 if (is_auto_ejecting)
131 {
132 live_servers= 0;
133 ptr->next_distribution_rebuild= 0;
134 for (host_index= 0; host_index < ptr->number_of_hosts; ++host_index)
135 {
136 if (list[host_index].next_retry <= now.tv_sec)
137 live_servers++;
138 else
139 {
140 if (ptr->next_distribution_rebuild == 0 || list[host_index].next_retry < ptr->next_distribution_rebuild)
141 ptr->next_distribution_rebuild= list[host_index].next_retry;
142 }
143 }
144 }
145 else
146 live_servers= ptr->number_of_hosts;
147
148 is_ketama_weighted= memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
149 points_per_server= (uint32_t) (is_ketama_weighted ? MEMCACHED_POINTS_PER_SERVER_KETAMA : MEMCACHED_POINTS_PER_SERVER);
150
151 if (live_servers == 0)
152 return MEMCACHED_SUCCESS;
153
154 if (live_servers > ptr->continuum_count)
155 {
156 memcached_continuum_item_st *new_ptr;
157
158 new_ptr= ptr->call_realloc(ptr, ptr->continuum,
159 sizeof(memcached_continuum_item_st) * (live_servers + MEMCACHED_CONTINUUM_ADDITION) * points_per_server);
160
161 if (new_ptr == 0)
162 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
163
164 ptr->continuum= new_ptr;
165 ptr->continuum_count= live_servers + MEMCACHED_CONTINUUM_ADDITION;
166 }
167
168 if (is_ketama_weighted)
169 {
170 for (host_index = 0; host_index < ptr->number_of_hosts; ++host_index)
171 {
172 if (list[host_index].weight == 0)
173 {
174 list[host_index].weight = 1;
175 }
176 if (!is_auto_ejecting || list[host_index].next_retry <= now.tv_sec)
177 total_weight += list[host_index].weight;
178 }
179 }
180
181 for (host_index = 0; host_index < ptr->number_of_hosts; ++host_index)
182 {
183 if (is_auto_ejecting && list[host_index].next_retry > now.tv_sec)
184 continue;
185
186 if (is_ketama_weighted)
187 {
188 float pct = (float)list[host_index].weight / (float)total_weight;
189 pointer_per_server= (uint32_t) ((floorf((float) (pct * MEMCACHED_POINTS_PER_SERVER_KETAMA / 4 * (float)live_servers + 0.0000000001))) * 4);
190 pointer_per_hash= 4;
191 #ifdef DEBUG
192 printf("ketama_weighted:%s|%d|%llu|%u\n",
193 list[host_index].hostname,
194 list[host_index].port,
195 (unsigned long long)list[host_index].weight,
196 pointer_per_server);
197 #endif
198 }
199 for (pointer_index= 1;
200 pointer_index <= pointer_per_server / pointer_per_hash;
201 ++pointer_index)
202 {
203 char sort_host[MEMCACHED_MAX_HOST_SORT_LENGTH]= "";
204 size_t sort_host_length;
205
206 if (list[host_index].port == MEMCACHED_DEFAULT_PORT)
207 {
208 sort_host_length= (size_t) snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH,
209 "%s-%d",
210 list[host_index].hostname,
211 pointer_index - 1);
212
213 }
214 else
215 {
216 sort_host_length= (size_t) snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH,
217 "%s:%d-%d",
218 list[host_index].hostname,
219 list[host_index].port, pointer_index - 1);
220 }
221 WATCHPOINT_ASSERT(sort_host_length);
222
223 if (is_ketama_weighted)
224 {
225 unsigned int i;
226 for (i = 0; i < pointer_per_hash; i++)
227 {
228 value= ketama_server_hash(sort_host, (uint32_t) sort_host_length, (int) i);
229 ptr->continuum[continuum_index].index= host_index;
230 ptr->continuum[continuum_index++].value= value;
231 }
232 }
233 else
234 {
235 value= memcached_generate_hash_value(sort_host, sort_host_length, ptr->hash_continuum);
236 ptr->continuum[continuum_index].index= host_index;
237 ptr->continuum[continuum_index++].value= value;
238 }
239 }
240 pointer_counter+= pointer_per_server;
241 }
242
243 WATCHPOINT_ASSERT(ptr);
244 WATCHPOINT_ASSERT(ptr->continuum);
245 WATCHPOINT_ASSERT(ptr->number_of_hosts * MEMCACHED_POINTS_PER_SERVER <= MEMCACHED_CONTINUUM_SIZE);
246 ptr->continuum_points_counter= pointer_counter;
247 qsort(ptr->continuum, ptr->continuum_points_counter, sizeof(memcached_continuum_item_st), continuum_item_cmp);
248
249 #ifdef DEBUG
250 for (pointer_index= 0; ptr->number_of_hosts && pointer_index < ((live_servers * MEMCACHED_POINTS_PER_SERVER) - 1); pointer_index++)
251 {
252 WATCHPOINT_ASSERT(ptr->continuum[pointer_index].value <= ptr->continuum[pointer_index + 1].value);
253 }
254 #endif
255
256 return MEMCACHED_SUCCESS;
257 }
258
259
260 memcached_return memcached_server_push(memcached_st *ptr, memcached_server_st *list)
261 {
262 unsigned int x;
263 uint16_t count;
264 memcached_server_st *new_host_list;
265
266 if (!list)
267 return MEMCACHED_SUCCESS;
268
269 count= list[0].count;
270 new_host_list= ptr->call_realloc(ptr, ptr->hosts,
271 sizeof(memcached_server_st) * (count + ptr->number_of_hosts));
272
273 if (!new_host_list)
274 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
275
276 ptr->hosts= new_host_list;
277
278 for (x= 0; x < count; x++)
279 {
280 if ((ptr->flags & MEM_USE_UDP && list[x].type != MEMCACHED_CONNECTION_UDP)
281 || ((list[x].type == MEMCACHED_CONNECTION_UDP)
282 && ! (ptr->flags & MEM_USE_UDP)) )
283 return MEMCACHED_INVALID_HOST_PROTOCOL;
284
285 WATCHPOINT_ASSERT(list[x].hostname[0] != 0);
286 memcached_server_create(ptr, &ptr->hosts[ptr->number_of_hosts]);
287 /* TODO check return type */
288 (void)memcached_server_create_with(ptr, &ptr->hosts[ptr->number_of_hosts], list[x].hostname,
289 list[x].port, list[x].weight, list[x].type);
290 ptr->number_of_hosts++;
291 }
292 ptr->hosts[0].count= (uint16_t) ptr->number_of_hosts;
293
294 return run_distribution(ptr);
295 }
296
297 memcached_return memcached_server_add_unix_socket(memcached_st *ptr,
298 const char *filename)
299 {
300 return memcached_server_add_unix_socket_with_weight(ptr, filename, 0);
301 }
302
303 memcached_return memcached_server_add_unix_socket_with_weight(memcached_st *ptr,
304 const char *filename,
305 uint32_t weight)
306 {
307 if (!filename)
308 return MEMCACHED_FAILURE;
309
310 return server_add(ptr, filename, 0, weight, MEMCACHED_CONNECTION_UNIX_SOCKET);
311 }
312
313 memcached_return memcached_server_add_udp(memcached_st *ptr,
314 const char *hostname,
315 unsigned int port)
316 {
317 return memcached_server_add_udp_with_weight(ptr, hostname, port, 0);
318 }
319
320 memcached_return memcached_server_add_udp_with_weight(memcached_st *ptr,
321 const char *hostname,
322 unsigned int port,
323 uint32_t weight)
324 {
325 if (!port)
326 port= MEMCACHED_DEFAULT_PORT;
327
328 if (!hostname)
329 hostname= "localhost";
330
331 return server_add(ptr, hostname, port, weight, MEMCACHED_CONNECTION_UDP);
332 }
333
334 memcached_return memcached_server_add(memcached_st *ptr,
335 const char *hostname,
336 unsigned int port)
337 {
338 return memcached_server_add_with_weight(ptr, hostname, port, 0);
339 }
340
341 memcached_return memcached_server_add_with_weight(memcached_st *ptr,
342 const char *hostname,
343 unsigned int port,
344 uint32_t weight)
345 {
346 if (!port)
347 port= MEMCACHED_DEFAULT_PORT;
348
349 if (!hostname)
350 hostname= "localhost";
351
352 return server_add(ptr, hostname, port, weight, MEMCACHED_CONNECTION_TCP);
353 }
354
355 static memcached_return server_add(memcached_st *ptr, const char *hostname,
356 unsigned int port,
357 uint32_t weight,
358 memcached_connection type)
359 {
360 memcached_server_st *new_host_list;
361
362 if ( (ptr->flags & MEM_USE_UDP && type != MEMCACHED_CONNECTION_UDP)
363 || ( (type == MEMCACHED_CONNECTION_UDP) && !(ptr->flags & MEM_USE_UDP) ) )
364 return MEMCACHED_INVALID_HOST_PROTOCOL;
365
366 new_host_list= ptr->call_realloc(ptr, ptr->hosts,
367 sizeof(memcached_server_st) * (ptr->number_of_hosts+1));
368
369 if (new_host_list == NULL)
370 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
371
372 ptr->hosts= new_host_list;
373
374 /* TODO: Check return type */
375 (void)memcached_server_create_with(ptr, &ptr->hosts[ptr->number_of_hosts], hostname, port, weight, type);
376 ptr->number_of_hosts++;
377 ptr->hosts[0].count= (uint16_t) ptr->number_of_hosts;
378
379 return run_distribution(ptr);
380 }
381
382 memcached_return memcached_server_remove(memcached_server_st *st_ptr)
383 {
384 uint32_t x, host_index;
385 memcached_st *ptr= st_ptr->root;
386 memcached_server_st *list= ptr->hosts;
387
388 for (x= 0, host_index= 0; x < ptr->number_of_hosts; x++)
389 {
390 if (strncmp(list[x].hostname, st_ptr->hostname, MEMCACHED_MAX_HOST_LENGTH) != 0 || list[x].port != st_ptr->port)
391 {
392 if (host_index != x)
393 memcpy(list+host_index, list+x, sizeof(memcached_server_st));
394 host_index++;
395 }
396 }
397 ptr->number_of_hosts= host_index;
398
399 if (st_ptr->address_info)
400 {
401 freeaddrinfo(st_ptr->address_info);
402 st_ptr->address_info= NULL;
403 }
404 run_distribution(ptr);
405
406 return MEMCACHED_SUCCESS;
407 }
408
409 memcached_server_st *memcached_server_list_append(memcached_server_st *ptr,
410 const char *hostname, unsigned int port,
411 memcached_return *error)
412 {
413 return memcached_server_list_append_with_weight(ptr, hostname, port, 0, error);
414 }
415
416 memcached_server_st *memcached_server_list_append_with_weight(memcached_server_st *ptr,
417 const char *hostname, unsigned int port,
418 uint32_t weight,
419 memcached_return *error)
420 {
421 unsigned int count;
422 memcached_server_st *new_host_list;
423
424 if (hostname == NULL || error == NULL)
425 return NULL;
426
427 if (!port)
428 port= MEMCACHED_DEFAULT_PORT;
429
430 /* Increment count for hosts */
431 count= 1;
432 if (ptr != NULL)
433 {
434 count+= ptr[0].count;
435 }
436
437 new_host_list= (memcached_server_st *)realloc(ptr, sizeof(memcached_server_st) * count);
438 if (!new_host_list)
439 {
440 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
441 return NULL;
442 }
443
444 /* TODO: Check return type */
445 memcached_server_create_with(NULL, &new_host_list[count-1], hostname, port, weight, MEMCACHED_CONNECTION_TCP);
446
447 /* Backwards compatibility hack */
448 new_host_list[0].count= (uint16_t) count;
449
450 *error= MEMCACHED_SUCCESS;
451 return new_host_list;
452 }
453
454 unsigned int memcached_server_list_count(memcached_server_st *ptr)
455 {
456 if (ptr == NULL)
457 return 0;
458
459 return ptr[0].count;
460 }
461
462 void memcached_server_list_free(memcached_server_st *ptr)
463 {
464 server_list_free(NULL, ptr);
465 }