Merging changes from trunk
[m6w6/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 ptr->last_disconnected_server = NULL;
55
56 return MEMCACHED_SUCCESS;
57 }
58
59 void server_list_free(memcached_st *ptr, memcached_server_st *servers)
60 {
61 unsigned int x;
62
63 if (servers == NULL)
64 return;
65
66 for (x= 0; x < servers->count; x++)
67 if (servers[x].address_info)
68 {
69 freeaddrinfo(servers[x].address_info);
70 servers[x].address_info= NULL;
71 }
72
73 if (ptr)
74 ptr->call_free(ptr, servers);
75 else
76 free(servers);
77 }
78
79 static uint32_t ketama_server_hash(const char *key, unsigned int key_length, int alignment)
80 {
81 unsigned char results[16];
82
83 md5_signature((unsigned char*)key, key_length, results);
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 memcached_return 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 ptr->cached_errno = errno;
125 return MEMCACHED_ERRNO;
126 }
127
128 list = ptr->hosts;
129
130 /* count live servers (those without a retry delay set) */
131 is_auto_ejecting= memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS);
132 if (is_auto_ejecting)
133 {
134 live_servers= 0;
135 ptr->next_distribution_rebuild= 0;
136 for (host_index= 0; host_index < ptr->number_of_hosts; ++host_index)
137 {
138 if (list[host_index].next_retry <= now.tv_sec)
139 live_servers++;
140 else
141 {
142 if (ptr->next_distribution_rebuild == 0 || list[host_index].next_retry < ptr->next_distribution_rebuild)
143 ptr->next_distribution_rebuild= list[host_index].next_retry;
144 }
145 }
146 }
147 else
148 live_servers= ptr->number_of_hosts;
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->continuum_count)
157 {
158 memcached_continuum_item_st *new_ptr;
159
160 new_ptr= ptr->call_realloc(ptr, ptr->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->continuum= new_ptr;
167 ptr->continuum_count= live_servers + MEMCACHED_CONTINUUM_ADDITION;
168 }
169
170 if (is_ketama_weighted)
171 {
172 for (host_index = 0; host_index < ptr->number_of_hosts; ++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 < ptr->number_of_hosts; ++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 for (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 size_t 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= (size_t) snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH,
211 "/%s:%d-%d",
212 list[host_index].hostname,
213 list[host_index].port,
214 pointer_index);
215 #ifdef DEBUG
216 printf("update_continuum: key is %s\n", sort_host);
217 #endif
218
219 WATCHPOINT_ASSERT(sort_host_length);
220
221 if (is_ketama_weighted)
222 {
223 unsigned int i;
224 for (i = 0; i < pointer_per_hash; i++)
225 {
226 value= ketama_server_hash(sort_host, (uint32_t) sort_host_length, (int) i);
227 ptr->continuum[continuum_index].index= host_index;
228 ptr->continuum[continuum_index++].value= value;
229 }
230 }
231 else
232 {
233 value= memcached_generate_hash_value(sort_host, sort_host_length, ptr->hash_continuum);
234 ptr->continuum[continuum_index].index= host_index;
235 ptr->continuum[continuum_index++].value= value;
236 }
237 }
238 pointer_counter+= pointer_per_server;
239 }
240
241 WATCHPOINT_ASSERT(ptr);
242 WATCHPOINT_ASSERT(ptr->continuum);
243 WATCHPOINT_ASSERT(ptr->number_of_hosts * MEMCACHED_POINTS_PER_SERVER <= MEMCACHED_CONTINUUM_SIZE);
244 ptr->continuum_points_counter= pointer_counter;
245 qsort(ptr->continuum, ptr->continuum_points_counter, sizeof(memcached_continuum_item_st), continuum_item_cmp);
246
247 #ifdef DEBUG
248 for (pointer_index= 0; ptr->number_of_hosts && pointer_index < ((live_servers * MEMCACHED_POINTS_PER_SERVER) - 1); pointer_index++)
249 {
250 WATCHPOINT_ASSERT(ptr->continuum[pointer_index].value <= ptr->continuum[pointer_index + 1].value);
251 }
252 #endif
253
254 return MEMCACHED_SUCCESS;
255 }
256
257
258 memcached_return memcached_server_push(memcached_st *ptr, memcached_server_st *list)
259 {
260 unsigned int x;
261 uint16_t count;
262 memcached_server_st *new_host_list;
263
264 if (!list)
265 return MEMCACHED_SUCCESS;
266
267 count= list[0].count;
268 new_host_list= ptr->call_realloc(ptr, ptr->hosts,
269 sizeof(memcached_server_st) * (count + ptr->number_of_hosts));
270
271 if (!new_host_list)
272 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
273
274 ptr->hosts= new_host_list;
275
276 for (x= 0; x < count; x++)
277 {
278 if ((ptr->flags & MEM_USE_UDP && list[x].type != MEMCACHED_CONNECTION_UDP)
279 || ((list[x].type == MEMCACHED_CONNECTION_UDP)
280 && ! (ptr->flags & MEM_USE_UDP)) )
281 return MEMCACHED_INVALID_HOST_PROTOCOL;
282
283 WATCHPOINT_ASSERT(list[x].hostname[0] != 0);
284 memcached_server_create(ptr, &ptr->hosts[ptr->number_of_hosts]);
285 /* TODO check return type */
286 (void)memcached_server_create_with(ptr, &ptr->hosts[ptr->number_of_hosts], list[x].hostname,
287 list[x].port, list[x].weight, list[x].type);
288 ptr->number_of_hosts++;
289 }
290 ptr->hosts[0].count= (uint16_t) ptr->number_of_hosts;
291
292 return run_distribution(ptr);
293 }
294
295 memcached_return memcached_server_add_unix_socket(memcached_st *ptr,
296 const char *filename)
297 {
298 return memcached_server_add_unix_socket_with_weight(ptr, filename, 0);
299 }
300
301 memcached_return memcached_server_add_unix_socket_with_weight(memcached_st *ptr,
302 const char *filename,
303 uint32_t weight)
304 {
305 if (!filename)
306 return MEMCACHED_FAILURE;
307
308 return server_add(ptr, filename, 0, weight, MEMCACHED_CONNECTION_UNIX_SOCKET);
309 }
310
311 memcached_return memcached_server_add_udp(memcached_st *ptr,
312 const char *hostname,
313 unsigned int port)
314 {
315 return memcached_server_add_udp_with_weight(ptr, hostname, port, 0);
316 }
317
318 memcached_return memcached_server_add_udp_with_weight(memcached_st *ptr,
319 const char *hostname,
320 unsigned int port,
321 uint32_t weight)
322 {
323 if (!port)
324 port= MEMCACHED_DEFAULT_PORT;
325
326 if (!hostname)
327 hostname= "localhost";
328
329 return server_add(ptr, hostname, port, weight, MEMCACHED_CONNECTION_UDP);
330 }
331
332 memcached_return memcached_server_add(memcached_st *ptr,
333 const char *hostname,
334 unsigned int port)
335 {
336 return memcached_server_add_with_weight(ptr, hostname, port, 0);
337 }
338
339 memcached_return memcached_server_add_with_weight(memcached_st *ptr,
340 const char *hostname,
341 unsigned int port,
342 uint32_t weight)
343 {
344 if (!port)
345 port= MEMCACHED_DEFAULT_PORT;
346
347 if (!hostname)
348 hostname= "localhost";
349
350 return server_add(ptr, hostname, port, weight, MEMCACHED_CONNECTION_TCP);
351 }
352
353 static memcached_return server_add(memcached_st *ptr, const char *hostname,
354 unsigned int port,
355 uint32_t weight,
356 memcached_connection type)
357 {
358 memcached_server_st *new_host_list;
359
360 if ( (ptr->flags & MEM_USE_UDP && type != MEMCACHED_CONNECTION_UDP)
361 || ( (type == MEMCACHED_CONNECTION_UDP) && !(ptr->flags & MEM_USE_UDP) ) )
362 return MEMCACHED_INVALID_HOST_PROTOCOL;
363
364 new_host_list= ptr->call_realloc(ptr, ptr->hosts,
365 sizeof(memcached_server_st) * (ptr->number_of_hosts+1));
366
367 if (new_host_list == NULL)
368 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
369
370 ptr->hosts= new_host_list;
371
372 /* TODO: Check return type */
373 (void)memcached_server_create_with(ptr, &ptr->hosts[ptr->number_of_hosts], hostname, port, weight, type);
374 ptr->number_of_hosts++;
375 ptr->hosts[0].count= (uint16_t) ptr->number_of_hosts;
376
377 return run_distribution(ptr);
378 }
379
380 memcached_return memcached_server_remove(memcached_server_st *st_ptr)
381 {
382 uint32_t x, host_index;
383 memcached_st *ptr= st_ptr->root;
384 memcached_server_st *list= ptr->hosts;
385
386 for (x= 0, host_index= 0; x < ptr->number_of_hosts; x++)
387 {
388 if (strncmp(list[x].hostname, st_ptr->hostname, MEMCACHED_MAX_HOST_LENGTH) != 0 || list[x].port != st_ptr->port)
389 {
390 if (host_index != x)
391 memcpy(list+host_index, list+x, sizeof(memcached_server_st));
392 host_index++;
393 }
394 }
395 ptr->number_of_hosts= host_index;
396
397 if (st_ptr->address_info)
398 {
399 freeaddrinfo(st_ptr->address_info);
400 st_ptr->address_info= NULL;
401 }
402 run_distribution(ptr);
403
404 return MEMCACHED_SUCCESS;
405 }
406
407 memcached_server_st *memcached_server_list_append(memcached_server_st *ptr,
408 const char *hostname, unsigned int port,
409 memcached_return *error)
410 {
411 return memcached_server_list_append_with_weight(ptr, hostname, port, 0, error);
412 }
413
414 memcached_server_st *memcached_server_list_append_with_weight(memcached_server_st *ptr,
415 const char *hostname, unsigned int port,
416 uint32_t weight,
417 memcached_return *error)
418 {
419 unsigned int count;
420 memcached_server_st *new_host_list;
421
422 if (hostname == NULL || error == NULL)
423 return NULL;
424
425 if (!port)
426 port= MEMCACHED_DEFAULT_PORT;
427
428 /* Increment count for hosts */
429 count= 1;
430 if (ptr != NULL)
431 {
432 count+= ptr[0].count;
433 }
434
435 new_host_list= (memcached_server_st *)realloc(ptr, sizeof(memcached_server_st) * count);
436 if (!new_host_list)
437 {
438 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
439 return NULL;
440 }
441
442 /* TODO: Check return type */
443 memcached_server_create_with(NULL, &new_host_list[count-1], hostname, port, weight, MEMCACHED_CONNECTION_TCP);
444
445 /* Backwards compatibility hack */
446 new_host_list[0].count= (uint16_t) count;
447
448 *error= MEMCACHED_SUCCESS;
449 return new_host_list;
450 }
451
452 unsigned int memcached_server_list_count(memcached_server_st *ptr)
453 {
454 if (ptr == NULL)
455 return 0;
456
457 return ptr[0].count;
458 }
459
460 void memcached_server_list_free(memcached_server_st *ptr)
461 {
462 server_list_free(NULL, ptr);
463 }