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