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