Merge from Trond
[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= 1;
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 if (list[host_index].port == MEMCACHED_DEFAULT_PORT)
209 {
210 sort_host_length= (size_t) snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH,
211 "%s-%d",
212 list[host_index].hostname,
213 pointer_index - 1);
214
215 }
216 else
217 {
218 sort_host_length= (size_t) snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH,
219 "%s:%d-%d",
220 list[host_index].hostname,
221 list[host_index].port, pointer_index - 1);
222 }
223 WATCHPOINT_ASSERT(sort_host_length);
224
225 if (is_ketama_weighted)
226 {
227 unsigned int i;
228 for (i = 0; i < pointer_per_hash; i++)
229 {
230 value= ketama_server_hash(sort_host, (uint32_t) sort_host_length, (int) i);
231 ptr->continuum[continuum_index].index= host_index;
232 ptr->continuum[continuum_index++].value= value;
233 }
234 }
235 else
236 {
237 value= memcached_generate_hash_value(sort_host, sort_host_length, ptr->hash_continuum);
238 ptr->continuum[continuum_index].index= host_index;
239 ptr->continuum[continuum_index++].value= value;
240 }
241 }
242 pointer_counter+= pointer_per_server;
243 }
244
245 WATCHPOINT_ASSERT(ptr);
246 WATCHPOINT_ASSERT(ptr->continuum);
247 WATCHPOINT_ASSERT(ptr->number_of_hosts * MEMCACHED_POINTS_PER_SERVER <= MEMCACHED_CONTINUUM_SIZE);
248 ptr->continuum_points_counter= pointer_counter;
249 qsort(ptr->continuum, ptr->continuum_points_counter, sizeof(memcached_continuum_item_st), continuum_item_cmp);
250
251 #ifdef DEBUG
252 for (pointer_index= 0; ptr->number_of_hosts && pointer_index < ((live_servers * MEMCACHED_POINTS_PER_SERVER) - 1); pointer_index++)
253 {
254 WATCHPOINT_ASSERT(ptr->continuum[pointer_index].value <= ptr->continuum[pointer_index + 1].value);
255 }
256 #endif
257
258 return MEMCACHED_SUCCESS;
259 }
260
261
262 memcached_return memcached_server_push(memcached_st *ptr, memcached_server_st *list)
263 {
264 unsigned int x;
265 uint16_t count;
266 memcached_server_st *new_host_list;
267
268 if (!list)
269 return MEMCACHED_SUCCESS;
270
271 count= list[0].count;
272 new_host_list= ptr->call_realloc(ptr, ptr->hosts,
273 sizeof(memcached_server_st) * (count + ptr->number_of_hosts));
274
275 if (!new_host_list)
276 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
277
278 ptr->hosts= new_host_list;
279
280 for (x= 0; x < count; x++)
281 {
282 if ((ptr->flags & MEM_USE_UDP && list[x].type != MEMCACHED_CONNECTION_UDP)
283 || ((list[x].type == MEMCACHED_CONNECTION_UDP)
284 && ! (ptr->flags & MEM_USE_UDP)) )
285 return MEMCACHED_INVALID_HOST_PROTOCOL;
286
287 WATCHPOINT_ASSERT(list[x].hostname[0] != 0);
288 memcached_server_create(ptr, &ptr->hosts[ptr->number_of_hosts]);
289 /* TODO check return type */
290 (void)memcached_server_create_with(ptr, &ptr->hosts[ptr->number_of_hosts], list[x].hostname,
291 list[x].port, list[x].weight, list[x].type);
292 ptr->number_of_hosts++;
293 }
294 ptr->hosts[0].count= (uint16_t) ptr->number_of_hosts;
295
296 return run_distribution(ptr);
297 }
298
299 memcached_return memcached_server_add_unix_socket(memcached_st *ptr,
300 const char *filename)
301 {
302 return memcached_server_add_unix_socket_with_weight(ptr, filename, 0);
303 }
304
305 memcached_return memcached_server_add_unix_socket_with_weight(memcached_st *ptr,
306 const char *filename,
307 uint32_t weight)
308 {
309 if (!filename)
310 return MEMCACHED_FAILURE;
311
312 return server_add(ptr, filename, 0, weight, MEMCACHED_CONNECTION_UNIX_SOCKET);
313 }
314
315 memcached_return memcached_server_add_udp(memcached_st *ptr,
316 const char *hostname,
317 unsigned int port)
318 {
319 return memcached_server_add_udp_with_weight(ptr, hostname, port, 0);
320 }
321
322 memcached_return memcached_server_add_udp_with_weight(memcached_st *ptr,
323 const char *hostname,
324 unsigned int port,
325 uint32_t weight)
326 {
327 if (!port)
328 port= MEMCACHED_DEFAULT_PORT;
329
330 if (!hostname)
331 hostname= "localhost";
332
333 return server_add(ptr, hostname, port, weight, MEMCACHED_CONNECTION_UDP);
334 }
335
336 memcached_return memcached_server_add(memcached_st *ptr,
337 const char *hostname,
338 unsigned int port)
339 {
340 return memcached_server_add_with_weight(ptr, hostname, port, 0);
341 }
342
343 memcached_return memcached_server_add_with_weight(memcached_st *ptr,
344 const char *hostname,
345 unsigned int port,
346 uint32_t weight)
347 {
348 if (!port)
349 port= MEMCACHED_DEFAULT_PORT;
350
351 if (!hostname)
352 hostname= "localhost";
353
354 return server_add(ptr, hostname, port, weight, MEMCACHED_CONNECTION_TCP);
355 }
356
357 static memcached_return server_add(memcached_st *ptr, const char *hostname,
358 unsigned int port,
359 uint32_t weight,
360 memcached_connection type)
361 {
362 memcached_server_st *new_host_list;
363
364 if ( (ptr->flags & MEM_USE_UDP && type != MEMCACHED_CONNECTION_UDP)
365 || ( (type == MEMCACHED_CONNECTION_UDP) && !(ptr->flags & MEM_USE_UDP) ) )
366 return MEMCACHED_INVALID_HOST_PROTOCOL;
367
368 new_host_list= ptr->call_realloc(ptr, ptr->hosts,
369 sizeof(memcached_server_st) * (ptr->number_of_hosts+1));
370
371 if (new_host_list == NULL)
372 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
373
374 ptr->hosts= new_host_list;
375
376 /* TODO: Check return type */
377 (void)memcached_server_create_with(ptr, &ptr->hosts[ptr->number_of_hosts], hostname, port, weight, type);
378 ptr->number_of_hosts++;
379 ptr->hosts[0].count= (uint16_t) ptr->number_of_hosts;
380
381 return run_distribution(ptr);
382 }
383
384 memcached_return memcached_server_remove(memcached_server_st *st_ptr)
385 {
386 uint32_t x, host_index;
387 memcached_st *ptr= st_ptr->root;
388 memcached_server_st *list= ptr->hosts;
389
390 for (x= 0, host_index= 0; x < ptr->number_of_hosts; x++)
391 {
392 if (strncmp(list[x].hostname, st_ptr->hostname, MEMCACHED_MAX_HOST_LENGTH) != 0 || list[x].port != st_ptr->port)
393 {
394 if (host_index != x)
395 memcpy(list+host_index, list+x, sizeof(memcached_server_st));
396 host_index++;
397 }
398 }
399 ptr->number_of_hosts= host_index;
400
401 if (st_ptr->address_info)
402 {
403 freeaddrinfo(st_ptr->address_info);
404 st_ptr->address_info= NULL;
405 }
406 run_distribution(ptr);
407
408 return MEMCACHED_SUCCESS;
409 }
410
411 memcached_server_st *memcached_server_list_append(memcached_server_st *ptr,
412 const char *hostname, unsigned int port,
413 memcached_return *error)
414 {
415 return memcached_server_list_append_with_weight(ptr, hostname, port, 0, error);
416 }
417
418 memcached_server_st *memcached_server_list_append_with_weight(memcached_server_st *ptr,
419 const char *hostname, unsigned int port,
420 uint32_t weight,
421 memcached_return *error)
422 {
423 unsigned int count;
424 memcached_server_st *new_host_list;
425
426 if (hostname == NULL || error == NULL)
427 return NULL;
428
429 if (!port)
430 port= MEMCACHED_DEFAULT_PORT;
431
432 /* Increment count for hosts */
433 count= 1;
434 if (ptr != NULL)
435 {
436 count+= ptr[0].count;
437 }
438
439 new_host_list= (memcached_server_st *)realloc(ptr, sizeof(memcached_server_st) * count);
440 if (!new_host_list)
441 {
442 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
443 return NULL;
444 }
445
446 /* TODO: Check return type */
447 memcached_server_create_with(NULL, &new_host_list[count-1], hostname, port, weight, MEMCACHED_CONNECTION_TCP);
448
449 /* Backwards compatibility hack */
450 new_host_list[0].count= (uint16_t) count;
451
452 *error= MEMCACHED_SUCCESS;
453 return new_host_list;
454 }
455
456 unsigned int memcached_server_list_count(memcached_server_st *ptr)
457 {
458 if (ptr == NULL)
459 return 0;
460
461 return ptr[0].count;
462 }
463
464 void memcached_server_list_free(memcached_server_st *ptr)
465 {
466 server_list_free(NULL, ptr);
467 }