Merge trunk.
[m6w6/libmemcached] / libmemcached / hosts.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2010 Brian Aker All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38 #include <libmemcached/common.h>
39
40 #include <cmath>
41 #include <sys/time.h>
42
43 /* Protoypes (static) */
44 static memcached_return_t server_add(memcached_st *ptr, const char *hostname,
45 in_port_t port,
46 uint32_t weight,
47 memcached_connection_t type);
48
49 static memcached_return_t update_continuum(memcached_st *ptr);
50
51 static int compare_servers(const void *p1, const void *p2)
52 {
53 int return_value;
54 memcached_server_instance_st a= (memcached_server_instance_st)p1;
55 memcached_server_instance_st b= (memcached_server_instance_st)p2;
56
57 return_value= strcmp(a->hostname, b->hostname);
58
59 if (return_value == 0)
60 {
61 return_value= (int) (a->port - b->port);
62 }
63
64 return return_value;
65 }
66
67 static void sort_hosts(memcached_st *ptr)
68 {
69 if (memcached_server_count(ptr))
70 {
71 memcached_server_write_instance_st instance;
72
73 qsort(memcached_server_list(ptr), memcached_server_count(ptr), sizeof(memcached_server_st), compare_servers);
74 instance= memcached_server_instance_fetch(ptr, 0);
75 instance->number_of_hosts= memcached_server_count(ptr);
76 }
77 }
78
79
80 memcached_return_t run_distribution(memcached_st *ptr)
81 {
82 if (ptr->flags.use_sort_hosts)
83 sort_hosts(ptr);
84
85 switch (ptr->distribution)
86 {
87 case MEMCACHED_DISTRIBUTION_CONSISTENT:
88 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA:
89 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY:
90 case MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED:
91 return update_continuum(ptr);
92 case MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET:
93 case MEMCACHED_DISTRIBUTION_MODULA:
94 break;
95 case MEMCACHED_DISTRIBUTION_RANDOM:
96 srandom((uint32_t) time(NULL));
97 break;
98 case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX:
99 default:
100 WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
101 }
102
103 return MEMCACHED_SUCCESS;
104 }
105
106 static uint32_t ketama_server_hash(const char *key, size_t key_length, uint32_t alignment)
107 {
108 unsigned char results[16];
109
110 libhashkit_md5_signature((unsigned char*)key, key_length, results);
111
112 return ((uint32_t) (results[3 + alignment * 4] & 0xFF) << 24)
113 | ((uint32_t) (results[2 + alignment * 4] & 0xFF) << 16)
114 | ((uint32_t) (results[1 + alignment * 4] & 0xFF) << 8)
115 | (results[0 + alignment * 4] & 0xFF);
116 }
117
118 static int continuum_item_cmp(const void *t1, const void *t2)
119 {
120 memcached_continuum_item_st *ct1= (memcached_continuum_item_st *)t1;
121 memcached_continuum_item_st *ct2= (memcached_continuum_item_st *)t2;
122
123 /* Why 153? Hmmm... */
124 WATCHPOINT_ASSERT(ct1->value != 153);
125 if (ct1->value == ct2->value)
126 return 0;
127 else if (ct1->value > ct2->value)
128 return 1;
129 else
130 return -1;
131 }
132
133 static memcached_return_t update_continuum(memcached_st *ptr)
134 {
135 uint32_t continuum_index= 0;
136 memcached_server_st *list;
137 uint32_t pointer_counter= 0;
138 uint32_t pointer_per_server= MEMCACHED_POINTS_PER_SERVER;
139 uint32_t pointer_per_hash= 1;
140 uint32_t live_servers= 0;
141 struct timeval now;
142
143 if (gettimeofday(&now, NULL))
144 {
145 return memcached_set_errno(*ptr, errno, MEMCACHED_AT);
146 }
147
148 list= memcached_server_list(ptr);
149
150 /* count live servers (those without a retry delay set) */
151 bool is_auto_ejecting= _is_auto_eject_host(ptr);
152 if (is_auto_ejecting)
153 {
154 live_servers= 0;
155 ptr->ketama.next_distribution_rebuild= 0;
156 for (uint32_t host_index= 0; host_index < memcached_server_count(ptr); ++host_index)
157 {
158 if (list[host_index].next_retry <= now.tv_sec)
159 {
160 live_servers++;
161 }
162 else
163 {
164 if (ptr->ketama.next_distribution_rebuild == 0 || list[host_index].next_retry < ptr->ketama.next_distribution_rebuild)
165 {
166 ptr->ketama.next_distribution_rebuild= list[host_index].next_retry;
167 }
168 }
169 }
170 }
171 else
172 {
173 live_servers= memcached_server_count(ptr);
174 }
175
176 uint64_t is_ketama_weighted= memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
177 uint32_t points_per_server= (uint32_t) (is_ketama_weighted ? MEMCACHED_POINTS_PER_SERVER_KETAMA : MEMCACHED_POINTS_PER_SERVER);
178
179 if (not live_servers)
180 {
181 return MEMCACHED_SUCCESS;
182 }
183
184 if (live_servers > ptr->ketama.continuum_count)
185 {
186 memcached_continuum_item_st *new_ptr;
187
188 new_ptr= static_cast<memcached_continuum_item_st*>(libmemcached_realloc(ptr, ptr->ketama.continuum,
189 sizeof(memcached_continuum_item_st) * (live_servers + MEMCACHED_CONTINUUM_ADDITION) * points_per_server));
190
191 if (new_ptr == 0)
192 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
193
194 ptr->ketama.continuum= new_ptr;
195 ptr->ketama.continuum_count= live_servers + MEMCACHED_CONTINUUM_ADDITION;
196 }
197
198 uint64_t total_weight= 0;
199 if (is_ketama_weighted)
200 {
201 for (uint32_t host_index = 0; host_index < memcached_server_count(ptr); ++host_index)
202 {
203 if (! is_auto_ejecting || list[host_index].next_retry <= now.tv_sec)
204 {
205 total_weight += list[host_index].weight;
206 }
207 }
208 }
209
210 for (uint32_t host_index= 0; host_index < memcached_server_count(ptr); ++host_index)
211 {
212 if (is_auto_ejecting && list[host_index].next_retry > now.tv_sec)
213 continue;
214
215 if (is_ketama_weighted)
216 {
217 float pct = (float)list[host_index].weight / (float)total_weight;
218 pointer_per_server= (uint32_t) ((floorf((float) (pct * MEMCACHED_POINTS_PER_SERVER_KETAMA / 4 * (float)live_servers + 0.0000000001))) * 4);
219 pointer_per_hash= 4;
220 #ifdef DEBUG
221 printf("ketama_weighted:%s|%d|%llu|%u\n",
222 list[host_index].hostname,
223 list[host_index].port,
224 (unsigned long long)list[host_index].weight,
225 pointer_per_server);
226 #endif
227 }
228
229
230 if (ptr->distribution == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY)
231 {
232 for (uint32_t pointer_index= 0;
233 pointer_index < pointer_per_server / pointer_per_hash;
234 pointer_index++)
235 {
236 char sort_host[MEMCACHED_MAX_HOST_SORT_LENGTH]= "";
237 int sort_host_length;
238
239 // Spymemcached ketema key format is: hostname/ip:port-index
240 // If hostname is not available then: /ip:port-index
241 sort_host_length= snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH,
242 "/%s:%u-%u",
243 list[host_index].hostname,
244 (uint32_t)list[host_index].port,
245 pointer_index);
246
247 if (sort_host_length >= MEMCACHED_MAX_HOST_SORT_LENGTH || sort_host_length < 0)
248 {
249 return MEMCACHED_FAILURE;
250 }
251 #ifdef DEBUG
252 printf("update_continuum: key is %s\n", sort_host);
253 #endif
254
255 WATCHPOINT_ASSERT(sort_host_length);
256
257 if (is_ketama_weighted)
258 {
259 for (uint32_t x= 0; x < pointer_per_hash; x++)
260 {
261 uint32_t value= ketama_server_hash(sort_host, (size_t)sort_host_length, x);
262 ptr->ketama.continuum[continuum_index].index= host_index;
263 ptr->ketama.continuum[continuum_index++].value= value;
264 }
265 }
266 else
267 {
268 uint32_t value= hashkit_digest(&ptr->hashkit, sort_host, (size_t)sort_host_length);
269 ptr->ketama.continuum[continuum_index].index= host_index;
270 ptr->ketama.continuum[continuum_index++].value= value;
271 }
272 }
273 }
274 else
275 {
276 for (uint32_t pointer_index= 1;
277 pointer_index <= pointer_per_server / pointer_per_hash;
278 pointer_index++)
279 {
280 char sort_host[MEMCACHED_MAX_HOST_SORT_LENGTH]= "";
281 int sort_host_length;
282
283 if (list[host_index].port == MEMCACHED_DEFAULT_PORT)
284 {
285 sort_host_length= snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH,
286 "%s-%u",
287 list[host_index].hostname,
288 pointer_index - 1);
289 }
290 else
291 {
292 sort_host_length= snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH,
293 "%s:%u-%u",
294 list[host_index].hostname,
295 (uint32_t)list[host_index].port,
296 pointer_index - 1);
297 }
298
299 if (sort_host_length >= MEMCACHED_MAX_HOST_SORT_LENGTH || sort_host_length < 0)
300 {
301 return MEMCACHED_FAILURE;
302 }
303
304 WATCHPOINT_ASSERT(sort_host_length);
305
306 if (is_ketama_weighted)
307 {
308 for (uint32_t x = 0; x < pointer_per_hash; x++)
309 {
310 uint32_t value= ketama_server_hash(sort_host, (size_t)sort_host_length, x);
311 ptr->ketama.continuum[continuum_index].index= host_index;
312 ptr->ketama.continuum[continuum_index++].value= value;
313 }
314 }
315 else
316 {
317 uint32_t value= hashkit_digest(&ptr->hashkit, sort_host, (size_t)sort_host_length);
318 ptr->ketama.continuum[continuum_index].index= host_index;
319 ptr->ketama.continuum[continuum_index++].value= value;
320 }
321 }
322 }
323
324 pointer_counter+= pointer_per_server;
325 }
326
327 WATCHPOINT_ASSERT(ptr);
328 WATCHPOINT_ASSERT(ptr->continuum);
329 WATCHPOINT_ASSERT(memcached_server_count(ptr) * MEMCACHED_POINTS_PER_SERVER <= MEMCACHED_CONTINUUM_SIZE);
330 ptr->ketama.continuum_points_counter= pointer_counter;
331 qsort(ptr->ketama.continuum, ptr->ketama.continuum_points_counter, sizeof(memcached_continuum_item_st), continuum_item_cmp);
332
333 #ifdef DEBUG
334 for (uint32_t pointer_index= 0; memcached_server_count(ptr) && pointer_index < ((live_servers * MEMCACHED_POINTS_PER_SERVER) - 1); pointer_index++)
335 {
336 WATCHPOINT_ASSERT(ptr->continuum[pointer_index].value <= ptr->continuum[pointer_index + 1].value);
337 }
338 #endif
339
340 return MEMCACHED_SUCCESS;
341 }
342
343
344 memcached_return_t memcached_server_push(memcached_st *ptr, const memcached_server_list_st list)
345 {
346 if (not list)
347 return MEMCACHED_SUCCESS;
348
349 uint32_t count= memcached_server_list_count(list);
350
351 memcached_server_st *new_host_list;
352 new_host_list= static_cast<memcached_server_st*>(libmemcached_realloc(ptr, memcached_server_list(ptr),
353 sizeof(memcached_server_st) * (count + memcached_server_count(ptr))));
354
355 if (not new_host_list)
356 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
357
358 memcached_server_list_set(ptr, new_host_list);
359
360 for (uint32_t x= 0; x < count; x++)
361 {
362 memcached_server_write_instance_st instance;
363
364 if ((ptr->flags.use_udp && list[x].type != MEMCACHED_CONNECTION_UDP)
365 || ((list[x].type == MEMCACHED_CONNECTION_UDP)
366 && ! (ptr->flags.use_udp)) )
367 {
368 return MEMCACHED_INVALID_HOST_PROTOCOL;
369 }
370
371 WATCHPOINT_ASSERT(list[x].hostname[0] != 0);
372
373 instance= memcached_server_instance_fetch(ptr, memcached_server_count(ptr));
374 WATCHPOINT_ASSERT(instance);
375
376 /* TODO check return type */
377 instance= memcached_server_create_with(ptr, instance, list[x].hostname,
378 list[x].port, list[x].weight, list[x].type);
379 if (not instance)
380 {
381 return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
382 }
383
384 if (list[x].weight > 1)
385 {
386 ptr->ketama.weighted= true;
387 }
388
389 ptr->number_of_hosts++;
390 }
391
392 // Provides backwards compatibility with server list.
393 {
394 memcached_server_write_instance_st instance;
395 instance= memcached_server_instance_fetch(ptr, 0);
396 instance->number_of_hosts= memcached_server_count(ptr);
397 }
398
399 return run_distribution(ptr);
400 }
401
402 memcached_return_t memcached_server_add_unix_socket(memcached_st *ptr,
403 const char *filename)
404 {
405 return memcached_server_add_unix_socket_with_weight(ptr, filename, 0);
406 }
407
408 memcached_return_t memcached_server_add_unix_socket_with_weight(memcached_st *ptr,
409 const char *filename,
410 uint32_t weight)
411 {
412 if (! filename)
413 return MEMCACHED_FAILURE;
414
415 return server_add(ptr, filename, 0, weight, MEMCACHED_CONNECTION_UNIX_SOCKET);
416 }
417
418 memcached_return_t memcached_server_add_udp(memcached_st *ptr,
419 const char *hostname,
420 in_port_t port)
421 {
422 return memcached_server_add_udp_with_weight(ptr, hostname, port, 0);
423 }
424
425 memcached_return_t memcached_server_add_udp_with_weight(memcached_st *ptr,
426 const char *hostname,
427 in_port_t port,
428 uint32_t weight)
429 {
430 if (not port)
431 port= MEMCACHED_DEFAULT_PORT;
432
433 if (not hostname)
434 hostname= "localhost";
435
436 return server_add(ptr, hostname, port, weight, MEMCACHED_CONNECTION_UDP);
437 }
438
439 memcached_return_t memcached_server_add(memcached_st *ptr,
440 const char *hostname,
441 in_port_t port)
442 {
443 return memcached_server_add_with_weight(ptr, hostname, port, 0);
444 }
445
446 memcached_return_t memcached_server_add_with_weight(memcached_st *ptr,
447 const char *hostname,
448 in_port_t port,
449 uint32_t weight)
450 {
451 if (! port)
452 port= MEMCACHED_DEFAULT_PORT;
453
454 if (! hostname)
455 hostname= "localhost";
456
457 return server_add(ptr, hostname, port, weight, MEMCACHED_CONNECTION_TCP);
458 }
459
460 static memcached_return_t server_add(memcached_st *ptr, const char *hostname,
461 in_port_t port,
462 uint32_t weight,
463 memcached_connection_t type)
464 {
465 memcached_server_st *new_host_list;
466 memcached_server_write_instance_st instance;
467
468 if ( (ptr->flags.use_udp && type != MEMCACHED_CONNECTION_UDP)
469 || ( (type == MEMCACHED_CONNECTION_UDP) && (! ptr->flags.use_udp) ) )
470 return MEMCACHED_INVALID_HOST_PROTOCOL;
471
472 new_host_list= static_cast<memcached_server_st*>(libmemcached_realloc(ptr, memcached_server_list(ptr),
473 sizeof(memcached_server_st) * (ptr->number_of_hosts + 1)));
474
475 if (new_host_list == NULL)
476 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
477
478 memcached_server_list_set(ptr, new_host_list);
479
480 /* TODO: Check return type */
481 instance= memcached_server_instance_fetch(ptr, memcached_server_count(ptr));
482
483 (void)memcached_server_create_with(ptr, instance, hostname, port, weight, type);
484
485 if (weight > 1)
486 {
487 ptr->ketama.weighted= true;
488 }
489
490 ptr->number_of_hosts++;
491
492 instance= memcached_server_instance_fetch(ptr, 0);
493 memcached_servers_set_count(instance, memcached_server_count(ptr));
494
495 return run_distribution(ptr);
496 }
497
498 memcached_return_t memcached_server_add_parsed(memcached_st *ptr,
499 const char *hostname,
500 size_t hostname_length,
501 in_port_t port,
502 uint32_t weight)
503 {
504 char buffer[NI_MAXHOST];
505
506 memcpy(buffer, hostname, hostname_length);
507 buffer[hostname_length]= 0;
508
509 return server_add(ptr, buffer,
510 port,
511 weight,
512 MEMCACHED_CONNECTION_TCP);
513 }