First pass through turning instance into ++
[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 update_continuum(memcached_st *ptr);
45
46 static int compare_servers(const void *p1, const void *p2)
47 {
48 memcached_server_instance_st a= (memcached_server_instance_st)p1;
49 memcached_server_instance_st b= (memcached_server_instance_st)p2;
50
51 int return_value= strcmp(a->hostname, b->hostname);
52
53 if (return_value == 0)
54 {
55 return_value= int(a->port() - b->port());
56 }
57
58 return return_value;
59 }
60
61 static void sort_hosts(memcached_st *ptr)
62 {
63 if (memcached_server_count(ptr))
64 {
65
66 qsort(memcached_instance_list(ptr), memcached_server_count(ptr), sizeof(org::libmemcached::Instance), compare_servers);
67 org::libmemcached::Instance* instance= memcached_instance_fetch(ptr, 0);
68 instance->number_of_hosts= memcached_server_count(ptr);
69 }
70 }
71
72
73 memcached_return_t run_distribution(memcached_st *ptr)
74 {
75 if (ptr->flags.use_sort_hosts)
76 {
77 sort_hosts(ptr);
78 }
79
80 switch (ptr->distribution)
81 {
82 case MEMCACHED_DISTRIBUTION_CONSISTENT:
83 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA:
84 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY:
85 case MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED:
86 return update_continuum(ptr);
87
88 case MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET:
89 case MEMCACHED_DISTRIBUTION_MODULA:
90 break;
91
92 case MEMCACHED_DISTRIBUTION_RANDOM:
93 srandom((uint32_t) time(NULL));
94 break;
95
96 case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX:
97 default:
98 assert_msg(0, "Invalid distribution type passed to run_distribution()");
99 }
100
101 return MEMCACHED_SUCCESS;
102 }
103
104 static uint32_t ketama_server_hash(const char *key, size_t key_length, uint32_t alignment)
105 {
106 unsigned char results[16];
107
108 libhashkit_md5_signature((unsigned char*)key, key_length, results);
109
110 return ((uint32_t) (results[3 + alignment * 4] & 0xFF) << 24)
111 | ((uint32_t) (results[2 + alignment * 4] & 0xFF) << 16)
112 | ((uint32_t) (results[1 + alignment * 4] & 0xFF) << 8)
113 | (results[0 + alignment * 4] & 0xFF);
114 }
115
116 static int continuum_item_cmp(const void *t1, const void *t2)
117 {
118 memcached_continuum_item_st *ct1= (memcached_continuum_item_st *)t1;
119 memcached_continuum_item_st *ct2= (memcached_continuum_item_st *)t2;
120
121 /* Why 153? Hmmm... */
122 WATCHPOINT_ASSERT(ct1->value != 153);
123 if (ct1->value == ct2->value)
124 return 0;
125 else if (ct1->value > ct2->value)
126 return 1;
127 else
128 return -1;
129 }
130
131 static memcached_return_t update_continuum(memcached_st *ptr)
132 {
133 uint32_t continuum_index= 0;
134 uint32_t pointer_counter= 0;
135 uint32_t pointer_per_server= MEMCACHED_POINTS_PER_SERVER;
136 uint32_t pointer_per_hash= 1;
137 uint32_t live_servers= 0;
138 struct timeval now;
139
140 if (gettimeofday(&now, NULL))
141 {
142 return memcached_set_errno(*ptr, errno, MEMCACHED_AT);
143 }
144
145 org::libmemcached::Instance* list= memcached_instance_list(ptr);
146
147 /* count live servers (those without a retry delay set) */
148 bool is_auto_ejecting= _is_auto_eject_host(ptr);
149 if (is_auto_ejecting)
150 {
151 live_servers= 0;
152 ptr->ketama.next_distribution_rebuild= 0;
153 for (uint32_t host_index= 0; host_index < memcached_server_count(ptr); ++host_index)
154 {
155 if (list[host_index].next_retry <= now.tv_sec)
156 {
157 live_servers++;
158 }
159 else
160 {
161 if (ptr->ketama.next_distribution_rebuild == 0 or list[host_index].next_retry < ptr->ketama.next_distribution_rebuild)
162 {
163 ptr->ketama.next_distribution_rebuild= list[host_index].next_retry;
164 }
165 }
166 }
167 }
168 else
169 {
170 live_servers= memcached_server_count(ptr);
171 }
172
173 uint64_t is_ketama_weighted= memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
174 uint32_t points_per_server= (uint32_t) (is_ketama_weighted ? MEMCACHED_POINTS_PER_SERVER_KETAMA : MEMCACHED_POINTS_PER_SERVER);
175
176 if (live_servers == 0)
177 {
178 return MEMCACHED_SUCCESS;
179 }
180
181 if (live_servers > ptr->ketama.continuum_count)
182 {
183 memcached_continuum_item_st *new_ptr;
184
185 new_ptr= libmemcached_xrealloc(ptr, ptr->ketama.continuum, (live_servers + MEMCACHED_CONTINUUM_ADDITION) * points_per_server, memcached_continuum_item_st);
186
187 if (new_ptr == 0)
188 {
189 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
190 }
191
192 ptr->ketama.continuum= new_ptr;
193 ptr->ketama.continuum_count= live_servers + MEMCACHED_CONTINUUM_ADDITION;
194 }
195
196 uint64_t total_weight= 0;
197 if (is_ketama_weighted)
198 {
199 for (uint32_t host_index = 0; host_index < memcached_server_count(ptr); ++host_index)
200 {
201 if (is_auto_ejecting == false or list[host_index].next_retry <= now.tv_sec)
202 {
203 total_weight += list[host_index].weight;
204 }
205 }
206 }
207
208 for (uint32_t host_index= 0; host_index < memcached_server_count(ptr); ++host_index)
209 {
210 if (is_auto_ejecting and list[host_index].next_retry > now.tv_sec)
211 {
212 continue;
213 }
214
215 if (is_ketama_weighted)
216 {
217 float pct= (float)list[host_index].weight / (float)total_weight;
218 pointer_per_server= (uint32_t) ((::floor((float) (pct * MEMCACHED_POINTS_PER_SERVER_KETAMA / 4 * (float)live_servers + 0.0000000001))) * 4);
219 pointer_per_hash= 4;
220 if (DEBUG)
221 {
222 printf("ketama_weighted:%s|%d|%llu|%u\n",
223 list[host_index].hostname,
224 list[host_index].port(),
225 (unsigned long long)list[host_index].weight,
226 pointer_per_server);
227 }
228 }
229
230
231 if (ptr->distribution == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY)
232 {
233 for (uint32_t pointer_index= 0;
234 pointer_index < pointer_per_server / pointer_per_hash;
235 pointer_index++)
236 {
237 char sort_host[1 +MEMCACHED_NI_MAXHOST +1 +MEMCACHED_NI_MAXSERV +1 + MEMCACHED_NI_MAXSERV ]= "";
238 int sort_host_length;
239
240 // Spymemcached ketema key format is: hostname/ip:port-index
241 // If hostname is not available then: /ip:port-index
242 sort_host_length= snprintf(sort_host, sizeof(sort_host),
243 "/%s:%u-%u",
244 list[host_index].hostname,
245 (uint32_t)list[host_index].port(),
246 pointer_index);
247
248 if (size_t(sort_host_length) >= sizeof(sort_host) or sort_host_length < 0)
249 {
250 return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
251 memcached_literal_param("snprintf(sizeof(sort_host))"));
252 }
253
254 if (DEBUG)
255 {
256 fprintf(stdout, "update_continuum: key is %s\n", sort_host);
257 }
258
259 if (is_ketama_weighted)
260 {
261 for (uint32_t x= 0; x < pointer_per_hash; x++)
262 {
263 uint32_t value= ketama_server_hash(sort_host, (size_t)sort_host_length, x);
264 ptr->ketama.continuum[continuum_index].index= host_index;
265 ptr->ketama.continuum[continuum_index++].value= value;
266 }
267 }
268 else
269 {
270 uint32_t value= hashkit_digest(&ptr->hashkit, sort_host, (size_t)sort_host_length);
271 ptr->ketama.continuum[continuum_index].index= host_index;
272 ptr->ketama.continuum[continuum_index++].value= value;
273 }
274 }
275 }
276 else
277 {
278 for (uint32_t pointer_index= 1;
279 pointer_index <= pointer_per_server / pointer_per_hash;
280 pointer_index++)
281 {
282 char sort_host[MEMCACHED_NI_MAXHOST +1 +MEMCACHED_NI_MAXSERV +1 +MEMCACHED_NI_MAXSERV]= "";
283 int sort_host_length;
284
285 if (list[host_index].port() == MEMCACHED_DEFAULT_PORT)
286 {
287 sort_host_length= snprintf(sort_host, sizeof(sort_host),
288 "%s-%u",
289 list[host_index].hostname,
290 pointer_index - 1);
291 }
292 else
293 {
294 sort_host_length= snprintf(sort_host, sizeof(sort_host),
295 "%s:%u-%u",
296 list[host_index].hostname,
297 (uint32_t)list[host_index].port(),
298 pointer_index - 1);
299 }
300
301 if (size_t(sort_host_length) >= sizeof(sort_host) or sort_host_length < 0)
302 {
303 return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
304 memcached_literal_param("snprintf(sizeof(sort_host)))"));
305 }
306
307 if (is_ketama_weighted)
308 {
309 for (uint32_t x = 0; x < pointer_per_hash; x++)
310 {
311 uint32_t value= ketama_server_hash(sort_host, (size_t)sort_host_length, x);
312 ptr->ketama.continuum[continuum_index].index= host_index;
313 ptr->ketama.continuum[continuum_index++].value= value;
314 }
315 }
316 else
317 {
318 uint32_t value= hashkit_digest(&ptr->hashkit, sort_host, (size_t)sort_host_length);
319 ptr->ketama.continuum[continuum_index].index= host_index;
320 ptr->ketama.continuum[continuum_index++].value= value;
321 }
322 }
323 }
324
325 pointer_counter+= pointer_per_server;
326 }
327
328 WATCHPOINT_ASSERT(ptr);
329 WATCHPOINT_ASSERT(ptr->ketama.continuum);
330 WATCHPOINT_ASSERT(memcached_server_count(ptr) * MEMCACHED_POINTS_PER_SERVER <= MEMCACHED_CONTINUUM_SIZE);
331 ptr->ketama.continuum_points_counter= pointer_counter;
332 qsort(ptr->ketama.continuum, ptr->ketama.continuum_points_counter, sizeof(memcached_continuum_item_st), continuum_item_cmp);
333
334 if (DEBUG)
335 {
336 for (uint32_t pointer_index= 0; memcached_server_count(ptr) && pointer_index < ((live_servers * MEMCACHED_POINTS_PER_SERVER) - 1); pointer_index++)
337 {
338 WATCHPOINT_ASSERT(ptr->ketama.continuum[pointer_index].value <= ptr->ketama.continuum[pointer_index + 1].value);
339 }
340 }
341
342 return MEMCACHED_SUCCESS;
343 }
344
345 static memcached_return_t server_add(memcached_st *ptr,
346 const memcached_string_t& hostname,
347 in_port_t port,
348 uint32_t weight,
349 memcached_connection_t type)
350 {
351 assert_msg(ptr, "Programmer mistake, somehow server_add() was passed a NULL memcached_st");
352
353 org::libmemcached::Instance* new_host_list= libmemcached_xrealloc(ptr, memcached_instance_list(ptr), (ptr->number_of_hosts + 1), org::libmemcached::Instance);
354
355 if (new_host_list == NULL)
356 {
357 return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
358 }
359
360 memcached_instance_set(ptr, new_host_list);
361
362 /* TODO: Check return type */
363 org::libmemcached::Instance* instance= memcached_instance_fetch(ptr, memcached_server_count(ptr));
364
365 if (__instance_create_with(ptr, instance, hostname, port, weight, type) == NULL)
366 {
367 return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
368 }
369
370 if (weight > 1)
371 {
372 ptr->ketama.weighted= true;
373 }
374
375 ptr->number_of_hosts++;
376
377 // @note we place the count in the bottom of the server list
378 instance= memcached_instance_fetch(ptr, 0);
379 memcached_instance_set_count(instance, memcached_server_count(ptr));
380
381 return run_distribution(ptr);
382 }
383
384
385 memcached_return_t memcached_server_push(memcached_st *ptr, const memcached_server_list_st list)
386 {
387 if (list == NULL)
388 {
389 return MEMCACHED_SUCCESS;
390 }
391
392 uint32_t count= memcached_server_list_count(list);
393
394 org::libmemcached::Instance* new_host_list= libmemcached_xrealloc(ptr, memcached_instance_list(ptr), (count + memcached_server_count(ptr)), org::libmemcached::Instance);
395
396 if (new_host_list == NULL)
397 {
398 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
399 }
400
401 memcached_instance_set(ptr, new_host_list);
402
403 for (uint32_t x= 0; x < count; x++)
404 {
405 WATCHPOINT_ASSERT(list[x].hostname[0] != 0);
406
407 // We have extended the array, and now we will find it, and use it.
408 org::libmemcached::Instance* instance= memcached_instance_fetch(ptr, memcached_server_count(ptr));
409 WATCHPOINT_ASSERT(instance);
410
411 memcached_string_t hostname= { memcached_string_make_from_cstr(list[x].hostname) };
412 if (__instance_create_with(ptr, instance,
413 hostname,
414 list[x].port, list[x].weight, list[x].type) == NULL)
415 {
416 return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
417 }
418
419 if (list[x].weight > 1)
420 {
421 ptr->ketama.weighted= true;
422 }
423
424 ptr->number_of_hosts++;
425 }
426
427 // Provides backwards compatibility with server list.
428 {
429 org::libmemcached::Instance* instance= memcached_instance_fetch(ptr, 0);
430 instance->number_of_hosts= memcached_server_count(ptr);
431 }
432
433 return run_distribution(ptr);
434 }
435
436 memcached_return_t memcached_instance_push(memcached_st *ptr, const struct org::libmemcached::Instance* list, uint32_t number_of_hosts)
437 {
438 if (list == NULL)
439 {
440 return MEMCACHED_SUCCESS;
441 }
442
443 org::libmemcached::Instance* new_host_list= libmemcached_xrealloc(ptr, memcached_instance_list(ptr), (number_of_hosts +memcached_server_count(ptr)), org::libmemcached::Instance);
444
445 if (new_host_list == NULL)
446 {
447 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
448 }
449
450 memcached_instance_set(ptr, new_host_list);
451
452 for (uint32_t x= 0; x < number_of_hosts; x++)
453 {
454
455 WATCHPOINT_ASSERT(list[x].hostname[0] != 0);
456
457 // We have extended the array, and now we will find it, and use it.
458 org::libmemcached::Instance* instance= memcached_instance_fetch(ptr, memcached_server_count(ptr));
459 WATCHPOINT_ASSERT(instance);
460
461 memcached_string_t hostname= { memcached_string_make_from_cstr(list[x].hostname) };
462 if (__instance_create_with(ptr, instance,
463 hostname,
464 list[x].port(), list[x].weight, list[x].type) == NULL)
465 {
466 return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
467 }
468
469 if (list[x].weight > 1)
470 {
471 ptr->ketama.weighted= true;
472 }
473
474 ptr->number_of_hosts++;
475 }
476
477 // Provides backwards compatibility with server list.
478 {
479 org::libmemcached::Instance* instance= memcached_instance_fetch(ptr, 0);
480 instance->number_of_hosts= memcached_server_count(ptr);
481 }
482
483 return run_distribution(ptr);
484 }
485
486 memcached_return_t memcached_server_add_unix_socket(memcached_st *ptr,
487 const char *filename)
488 {
489 return memcached_server_add_unix_socket_with_weight(ptr, filename, 0);
490 }
491
492 memcached_return_t memcached_server_add_unix_socket_with_weight(memcached_st *ptr,
493 const char *filename,
494 uint32_t weight)
495 {
496 if (ptr == NULL)
497 {
498 return MEMCACHED_FAILURE;
499 }
500
501 memcached_string_t _filename= { memcached_string_make_from_cstr(filename) };
502 if (memcached_is_valid_servername(_filename) == false)
503 {
504 memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid filename for socket provided"));
505 }
506
507 return server_add(ptr, _filename, 0, weight, MEMCACHED_CONNECTION_UNIX_SOCKET);
508 }
509
510 memcached_return_t memcached_server_add_udp(memcached_st *ptr,
511 const char *hostname,
512 in_port_t port)
513 {
514 return memcached_server_add_udp_with_weight(ptr, hostname, port, 0);
515 }
516
517 memcached_return_t memcached_server_add_udp_with_weight(memcached_st *ptr,
518 const char *,
519 in_port_t,
520 uint32_t)
521 {
522 if (ptr == NULL)
523 {
524 return MEMCACHED_INVALID_ARGUMENTS;
525 }
526
527 return memcached_set_error(*ptr, MEMCACHED_DEPRECATED, MEMCACHED_AT);
528 }
529
530 memcached_return_t memcached_server_add(memcached_st *ptr,
531 const char *hostname,
532 in_port_t port)
533 {
534 return memcached_server_add_with_weight(ptr, hostname, port, 0);
535 }
536
537 memcached_return_t memcached_server_add_with_weight(memcached_st *ptr,
538 const char *hostname,
539 in_port_t port,
540 uint32_t weight)
541 {
542 if (ptr == NULL)
543 {
544 return MEMCACHED_INVALID_ARGUMENTS;
545 }
546
547 if (port == 0)
548 {
549 port= MEMCACHED_DEFAULT_PORT;
550 }
551
552 size_t hostname_length= hostname ? strlen(hostname) : 0;
553 if (hostname_length == 0)
554 {
555 hostname= "localhost";
556 hostname_length= memcached_literal_param_size("localhost");
557 }
558
559 memcached_string_t _hostname= { hostname, hostname_length };
560
561 if (memcached_is_valid_servername(_hostname) == false)
562 {
563 return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid hostname provided"));
564 }
565
566 return server_add(ptr, _hostname, port, weight, _hostname.c_str[0] == '/' ? MEMCACHED_CONNECTION_UNIX_SOCKET : MEMCACHED_CONNECTION_TCP);
567 }
568
569 memcached_return_t memcached_server_add_parsed(memcached_st *ptr,
570 const char *hostname,
571 size_t hostname_length,
572 in_port_t port,
573 uint32_t weight)
574 {
575 char buffer[NI_MAXHOST];
576
577 memcpy(buffer, hostname, hostname_length);
578 buffer[hostname_length]= 0;
579
580 memcached_string_t _hostname= { buffer, hostname_length };
581
582 return server_add(ptr, _hostname,
583 port,
584 weight,
585 MEMCACHED_CONNECTION_TCP);
586 }