2bcd189c15ab3fce95eba0970a039743b848af3b
[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= 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 && ptr->call_free)
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 index;
106 uint32_t host_index;
107 uint32_t continuum_index= 0;
108 uint32_t value;
109 memcached_server_st *list;
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 uint32_t is_ketama_weighted= 0;
115 uint32_t points_per_server= 0;
116
117 is_ketama_weighted= memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
118 points_per_server= is_ketama_weighted ? MEMCACHED_POINTS_PER_SERVER_KETAMA : MEMCACHED_POINTS_PER_SERVER;
119
120 if (ptr->number_of_hosts == 0)
121 return MEMCACHED_SUCCESS;
122
123 if (ptr->number_of_hosts > ptr->continuum_count)
124 {
125 memcached_continuum_item_st *new_ptr;
126
127 if (ptr->call_realloc)
128 new_ptr= (memcached_continuum_item_st *)ptr->call_realloc(ptr, ptr->continuum,
129 sizeof(memcached_continuum_item_st) * (ptr->number_of_hosts + MEMCACHED_CONTINUUM_ADDITION) * points_per_server);
130 else
131 new_ptr= (memcached_continuum_item_st *)realloc(ptr->continuum,
132 sizeof(memcached_continuum_item_st) * (ptr->number_of_hosts + MEMCACHED_CONTINUUM_ADDITION) * points_per_server);
133
134 if (new_ptr == 0)
135 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
136
137 ptr->continuum= new_ptr;
138 ptr->continuum_count= ptr->number_of_hosts + MEMCACHED_CONTINUUM_ADDITION;
139 }
140
141 list = ptr->hosts;
142
143 if (is_ketama_weighted)
144 {
145 for (host_index = 0; host_index < ptr->number_of_hosts; ++host_index)
146 {
147 if (list[host_index].weight == 0)
148 {
149 list[host_index].weight = 1;
150 }
151 total_weight += list[host_index].weight;
152 }
153 }
154
155 for (host_index = 0; host_index < ptr->number_of_hosts; ++host_index)
156 {
157 if (is_ketama_weighted)
158 {
159 float pct = (float)list[host_index].weight / (float)total_weight;
160 pointer_per_server= floorf(pct * MEMCACHED_POINTS_PER_SERVER_KETAMA / 4 * (float)(ptr->number_of_hosts) + 0.0000000001) * 4;
161 pointer_per_hash= 4;
162 #ifdef HAVE_DEBUG
163 printf("ketama_weighted:%s|%d|%llu|%u\n",
164 list[host_index].hostname,
165 list[host_index].port,
166 (unsigned long long)list[host_index].weight,
167 pointer_per_server);
168 #endif
169 }
170 for (index= 1; index <= pointer_per_server / pointer_per_hash; ++index)
171 {
172 char sort_host[MEMCACHED_MAX_HOST_SORT_LENGTH]= "";
173 size_t sort_host_length;
174
175 if (list[host_index].port == MEMCACHED_DEFAULT_PORT)
176 {
177 sort_host_length= snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH, "%s-%d",
178 list[host_index].hostname, index - 1);
179
180 }
181 else
182 {
183 sort_host_length= snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH, "%s:%d-%d",
184 list[host_index].hostname, list[host_index].port, index - 1);
185 }
186 WATCHPOINT_ASSERT(sort_host_length);
187
188 if (is_ketama_weighted)
189 {
190 int i;
191 for (i = 0; i < pointer_per_hash; i++)
192 {
193 value= ketama_server_hash(sort_host, sort_host_length, i);
194 ptr->continuum[continuum_index].index= host_index;
195 ptr->continuum[continuum_index++].value= value;
196 }
197 }
198 else
199 {
200 value= memcached_generate_hash_value(sort_host, sort_host_length, ptr->hash_continuum);
201 ptr->continuum[continuum_index].index= host_index;
202 ptr->continuum[continuum_index++].value= value;
203 }
204 }
205 pointer_counter+= pointer_per_server;
206 }
207
208 WATCHPOINT_ASSERT(ptr);
209 WATCHPOINT_ASSERT(ptr->continuum);
210 WATCHPOINT_ASSERT(ptr->number_of_hosts * MEMCACHED_POINTS_PER_SERVER <= MEMCACHED_CONTINUUM_SIZE);
211 ptr->continuum_points_counter= pointer_counter;
212 qsort(ptr->continuum, ptr->continuum_points_counter, sizeof(memcached_continuum_item_st), continuum_item_cmp);
213
214 #ifdef HAVE_DEBUG
215 for (index= 0; ptr->number_of_hosts && index < ((ptr->number_of_hosts * MEMCACHED_POINTS_PER_SERVER) - 1); index++)
216 {
217 WATCHPOINT_ASSERT(ptr->continuum[index].value <= ptr->continuum[index + 1].value);
218 }
219 #endif
220
221 return MEMCACHED_SUCCESS;
222 }
223
224
225 memcached_return memcached_server_push(memcached_st *ptr, memcached_server_st *list)
226 {
227 unsigned int x;
228 uint16_t count;
229 memcached_server_st *new_host_list;
230
231 if (!list)
232 return MEMCACHED_SUCCESS;
233
234 count= list[0].count;
235 if (ptr->call_realloc)
236 new_host_list=
237 (memcached_server_st *)ptr->call_realloc(ptr, ptr->hosts,
238 sizeof(memcached_server_st) * (count + ptr->number_of_hosts));
239 else
240 new_host_list=
241 (memcached_server_st *)realloc(ptr->hosts,
242 sizeof(memcached_server_st) * (count + ptr->number_of_hosts));
243
244 if (!new_host_list)
245 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
246
247 ptr->hosts= new_host_list;
248
249 for (x= 0; x < count; x++)
250 {
251 if ((ptr->flags & MEM_USE_UDP && list[x].type != MEMCACHED_CONNECTION_UDP)
252 || ((list[x].type == MEMCACHED_CONNECTION_UDP)
253 && ! (ptr->flags & MEM_USE_UDP)) )
254 return MEMCACHED_INVALID_HOST_PROTOCOL;
255
256 WATCHPOINT_ASSERT(list[x].hostname[0] != 0);
257 memcached_server_create(ptr, &ptr->hosts[ptr->number_of_hosts]);
258 /* TODO check return type */
259 (void)memcached_server_create_with(ptr, &ptr->hosts[ptr->number_of_hosts], list[x].hostname,
260 list[x].port, list[x].weight, list[x].type);
261 ptr->number_of_hosts++;
262 }
263 ptr->hosts[0].count= ptr->number_of_hosts;
264
265 return run_distribution(ptr);
266 }
267
268 memcached_return memcached_server_add_unix_socket(memcached_st *ptr,
269 const char *filename)
270 {
271 return memcached_server_add_unix_socket_with_weight(ptr, filename, 0);
272 }
273
274 memcached_return memcached_server_add_unix_socket_with_weight(memcached_st *ptr,
275 const char *filename,
276 uint32_t weight)
277 {
278 if (!filename)
279 return MEMCACHED_FAILURE;
280
281 return server_add(ptr, filename, 0, weight, MEMCACHED_CONNECTION_UNIX_SOCKET);
282 }
283
284 memcached_return memcached_server_add_udp(memcached_st *ptr,
285 const char *hostname,
286 unsigned int port)
287 {
288 return memcached_server_add_udp_with_weight(ptr, hostname, port, 0);
289 }
290
291 memcached_return memcached_server_add_udp_with_weight(memcached_st *ptr,
292 const char *hostname,
293 unsigned int port,
294 uint32_t weight)
295 {
296 if (!port)
297 port= MEMCACHED_DEFAULT_PORT;
298
299 if (!hostname)
300 hostname= "localhost";
301
302 return server_add(ptr, hostname, port, weight, MEMCACHED_CONNECTION_UDP);
303 }
304
305 memcached_return memcached_server_add(memcached_st *ptr,
306 const char *hostname,
307 unsigned int port)
308 {
309 return memcached_server_add_with_weight(ptr, hostname, port, 0);
310 }
311
312 memcached_return memcached_server_add_with_weight(memcached_st *ptr,
313 const char *hostname,
314 unsigned int port,
315 uint32_t weight)
316 {
317 if (!port)
318 port= MEMCACHED_DEFAULT_PORT;
319
320 if (!hostname)
321 hostname= "localhost";
322
323 return server_add(ptr, hostname, port, weight, MEMCACHED_CONNECTION_TCP);
324 }
325
326 static memcached_return server_add(memcached_st *ptr, const char *hostname,
327 unsigned int port,
328 uint32_t weight,
329 memcached_connection type)
330 {
331 memcached_server_st *new_host_list;
332
333 if ( (ptr->flags & MEM_USE_UDP && type != MEMCACHED_CONNECTION_UDP)
334 || ( (type == MEMCACHED_CONNECTION_UDP) && !(ptr->flags & MEM_USE_UDP) ) )
335 return MEMCACHED_INVALID_HOST_PROTOCOL;
336
337 if (ptr->call_realloc)
338 new_host_list= (memcached_server_st *)ptr->call_realloc(ptr, ptr->hosts,
339 sizeof(memcached_server_st) * (ptr->number_of_hosts+1));
340 else
341 new_host_list= (memcached_server_st *)realloc(ptr->hosts,
342 sizeof(memcached_server_st) * (ptr->number_of_hosts+1));
343 if (new_host_list == NULL)
344 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
345
346 ptr->hosts= new_host_list;
347
348 /* TODO: Check return type */
349 (void)memcached_server_create_with(ptr, &ptr->hosts[ptr->number_of_hosts], hostname, port, weight, type);
350 ptr->number_of_hosts++;
351 ptr->hosts[0].count= ptr->number_of_hosts;
352
353 return run_distribution(ptr);
354 }
355
356 memcached_return memcached_server_remove(memcached_server_st *st_ptr)
357 {
358 uint32_t x, index;
359 memcached_st *ptr= st_ptr->root;
360 memcached_server_st *list= ptr->hosts;
361
362 for (x= 0, index= 0; x < ptr->number_of_hosts; x++)
363 {
364 if (strncmp(list[x].hostname, st_ptr->hostname, MEMCACHED_MAX_HOST_LENGTH) != 0 || list[x].port != st_ptr->port)
365 {
366 if (index != x)
367 memcpy(list+index, list+x, sizeof(memcached_server_st));
368 index++;
369 }
370 }
371 ptr->number_of_hosts= index;
372
373 if (st_ptr->address_info)
374 {
375 freeaddrinfo(st_ptr->address_info);
376 st_ptr->address_info= NULL;
377 }
378 run_distribution(ptr);
379
380 return MEMCACHED_SUCCESS;
381 }
382
383 memcached_server_st *memcached_server_list_append(memcached_server_st *ptr,
384 const char *hostname, unsigned int port,
385 memcached_return *error)
386 {
387 return memcached_server_list_append_with_weight(ptr, hostname, port, 0, error);
388 }
389
390 memcached_server_st *memcached_server_list_append_with_weight(memcached_server_st *ptr,
391 const char *hostname, unsigned int port,
392 uint32_t weight,
393 memcached_return *error)
394 {
395 unsigned int count;
396 memcached_server_st *new_host_list;
397
398 if (hostname == NULL || error == NULL)
399 return NULL;
400
401 if (!port)
402 port= MEMCACHED_DEFAULT_PORT;
403
404 /* Increment count for hosts */
405 count= 1;
406 if (ptr != NULL)
407 {
408 count+= ptr[0].count;
409 }
410
411 new_host_list= (memcached_server_st *)realloc(ptr, sizeof(memcached_server_st) * count);
412 if (!new_host_list)
413 {
414 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
415 return NULL;
416 }
417
418 /* TODO: Check return type */
419 memcached_server_create_with(NULL, &new_host_list[count-1], hostname, port, weight, MEMCACHED_CONNECTION_TCP);
420
421 /* Backwards compatibility hack */
422 new_host_list[0].count= count;
423
424 *error= MEMCACHED_SUCCESS;
425 return new_host_list;
426 }
427
428 unsigned int memcached_server_list_count(memcached_server_st *ptr)
429 {
430 if (ptr == NULL)
431 return 0;
432
433 return ptr[0].count;
434 }
435
436 void memcached_server_list_free(memcached_server_st *ptr)
437 {
438 server_list_free(NULL, ptr);
439 }