1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2010 Brian Aker All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
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.
38 #include <libmemcached/common.h>
43 /* Protoypes (static) */
44 static memcached_return_t
update_continuum(memcached_st
*ptr
);
46 static int compare_servers(const void *p1
, const void *p2
)
49 memcached_server_instance_st a
= (memcached_server_instance_st
)p1
;
50 memcached_server_instance_st b
= (memcached_server_instance_st
)p2
;
52 return_value
= strcmp(a
->hostname
, b
->hostname
);
54 if (return_value
== 0)
56 return_value
= (int) (a
->port
- b
->port
);
62 static void sort_hosts(memcached_st
*ptr
)
64 if (memcached_server_count(ptr
))
66 memcached_server_write_instance_st instance
;
68 qsort(memcached_server_list(ptr
), memcached_server_count(ptr
), sizeof(memcached_server_st
), compare_servers
);
69 instance
= memcached_server_instance_fetch(ptr
, 0);
70 instance
->number_of_hosts
= memcached_server_count(ptr
);
75 memcached_return_t
run_distribution(memcached_st
*ptr
)
77 if (ptr
->flags
.use_sort_hosts
)
82 switch (ptr
->distribution
)
84 case MEMCACHED_DISTRIBUTION_CONSISTENT
:
85 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
:
86 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
:
87 case MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED
:
88 return update_continuum(ptr
);
90 case MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET
:
91 case MEMCACHED_DISTRIBUTION_MODULA
:
94 case MEMCACHED_DISTRIBUTION_RANDOM
:
95 srandom((uint32_t) time(NULL
));
98 case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX
:
100 assert_msg(0, "Invalid distribution type passed to run_distribution()");
103 return MEMCACHED_SUCCESS
;
106 static uint32_t ketama_server_hash(const char *key
, size_t key_length
, uint32_t alignment
)
108 unsigned char results
[16];
110 libhashkit_md5_signature((unsigned char*)key
, key_length
, results
);
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);
118 static int continuum_item_cmp(const void *t1
, const void *t2
)
120 memcached_continuum_item_st
*ct1
= (memcached_continuum_item_st
*)t1
;
121 memcached_continuum_item_st
*ct2
= (memcached_continuum_item_st
*)t2
;
123 /* Why 153? Hmmm... */
124 WATCHPOINT_ASSERT(ct1
->value
!= 153);
125 if (ct1
->value
== ct2
->value
)
127 else if (ct1
->value
> ct2
->value
)
133 static memcached_return_t
update_continuum(memcached_st
*ptr
)
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;
143 if (gettimeofday(&now
, NULL
))
145 return memcached_set_errno(*ptr
, errno
, MEMCACHED_AT
);
148 list
= memcached_server_list(ptr
);
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
)
155 ptr
->ketama
.next_distribution_rebuild
= 0;
156 for (uint32_t host_index
= 0; host_index
< memcached_server_count(ptr
); ++host_index
)
158 if (list
[host_index
].next_retry
<= now
.tv_sec
)
164 if (ptr
->ketama
.next_distribution_rebuild
== 0 or list
[host_index
].next_retry
< ptr
->ketama
.next_distribution_rebuild
)
166 ptr
->ketama
.next_distribution_rebuild
= list
[host_index
].next_retry
;
173 live_servers
= memcached_server_count(ptr
);
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
);
179 if (not live_servers
)
181 return MEMCACHED_SUCCESS
;
184 if (live_servers
> ptr
->ketama
.continuum_count
)
186 memcached_continuum_item_st
*new_ptr
;
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
));
193 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
196 ptr
->ketama
.continuum
= new_ptr
;
197 ptr
->ketama
.continuum_count
= live_servers
+ MEMCACHED_CONTINUUM_ADDITION
;
200 uint64_t total_weight
= 0;
201 if (is_ketama_weighted
)
203 for (uint32_t host_index
= 0; host_index
< memcached_server_count(ptr
); ++host_index
)
205 if (is_auto_ejecting
== false or list
[host_index
].next_retry
<= now
.tv_sec
)
207 total_weight
+= list
[host_index
].weight
;
212 for (uint32_t host_index
= 0; host_index
< memcached_server_count(ptr
); ++host_index
)
214 if (is_auto_ejecting
and list
[host_index
].next_retry
> now
.tv_sec
)
219 if (is_ketama_weighted
)
221 float pct
= (float)list
[host_index
].weight
/ (float)total_weight
;
222 pointer_per_server
= (uint32_t) ((floorf((float) (pct
* MEMCACHED_POINTS_PER_SERVER_KETAMA
/ 4 * (float)live_servers
+ 0.0000000001))) * 4);
226 printf("ketama_weighted:%s|%d|%llu|%u\n",
227 list
[host_index
].hostname
,
228 list
[host_index
].port
,
229 (unsigned long long)list
[host_index
].weight
,
235 if (ptr
->distribution
== MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
)
237 for (uint32_t pointer_index
= 0;
238 pointer_index
< pointer_per_server
/ pointer_per_hash
;
241 char sort_host
[MEMCACHED_MAX_HOST_SORT_LENGTH
]= "";
242 int sort_host_length
;
244 // Spymemcached ketema key format is: hostname/ip:port-index
245 // If hostname is not available then: /ip:port-index
246 sort_host_length
= snprintf(sort_host
, MEMCACHED_MAX_HOST_SORT_LENGTH
,
248 list
[host_index
].hostname
,
249 (uint32_t)list
[host_index
].port
,
252 if (sort_host_length
>= MEMCACHED_MAX_HOST_SORT_LENGTH
|| sort_host_length
< 0)
254 return memcached_set_error(*ptr
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
,
255 memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
260 fprintf(stdout
, "update_continuum: key is %s\n", sort_host
);
263 if (is_ketama_weighted
)
265 for (uint32_t x
= 0; x
< pointer_per_hash
; x
++)
267 uint32_t value
= ketama_server_hash(sort_host
, (size_t)sort_host_length
, x
);
268 ptr
->ketama
.continuum
[continuum_index
].index
= host_index
;
269 ptr
->ketama
.continuum
[continuum_index
++].value
= value
;
274 uint32_t value
= hashkit_digest(&ptr
->hashkit
, sort_host
, (size_t)sort_host_length
);
275 ptr
->ketama
.continuum
[continuum_index
].index
= host_index
;
276 ptr
->ketama
.continuum
[continuum_index
++].value
= value
;
282 for (uint32_t pointer_index
= 1;
283 pointer_index
<= pointer_per_server
/ pointer_per_hash
;
286 char sort_host
[MEMCACHED_MAX_HOST_SORT_LENGTH
]= "";
287 int sort_host_length
;
289 if (list
[host_index
].port
== MEMCACHED_DEFAULT_PORT
)
291 sort_host_length
= snprintf(sort_host
, MEMCACHED_MAX_HOST_SORT_LENGTH
,
293 list
[host_index
].hostname
,
298 sort_host_length
= snprintf(sort_host
, MEMCACHED_MAX_HOST_SORT_LENGTH
,
300 list
[host_index
].hostname
,
301 (uint32_t)list
[host_index
].port
,
305 if (sort_host_length
>= MEMCACHED_MAX_HOST_SORT_LENGTH
|| sort_host_length
< 0)
307 return memcached_set_error(*ptr
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
,
308 memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
311 if (is_ketama_weighted
)
313 for (uint32_t x
= 0; x
< pointer_per_hash
; x
++)
315 uint32_t value
= ketama_server_hash(sort_host
, (size_t)sort_host_length
, x
);
316 ptr
->ketama
.continuum
[continuum_index
].index
= host_index
;
317 ptr
->ketama
.continuum
[continuum_index
++].value
= value
;
322 uint32_t value
= hashkit_digest(&ptr
->hashkit
, sort_host
, (size_t)sort_host_length
);
323 ptr
->ketama
.continuum
[continuum_index
].index
= host_index
;
324 ptr
->ketama
.continuum
[continuum_index
++].value
= value
;
329 pointer_counter
+= pointer_per_server
;
332 WATCHPOINT_ASSERT(ptr
);
333 WATCHPOINT_ASSERT(ptr
->ketama
.continuum
);
334 WATCHPOINT_ASSERT(memcached_server_count(ptr
) * MEMCACHED_POINTS_PER_SERVER
<= MEMCACHED_CONTINUUM_SIZE
);
335 ptr
->ketama
.continuum_points_counter
= pointer_counter
;
336 qsort(ptr
->ketama
.continuum
, ptr
->ketama
.continuum_points_counter
, sizeof(memcached_continuum_item_st
), continuum_item_cmp
);
340 for (uint32_t pointer_index
= 0; memcached_server_count(ptr
) && pointer_index
< ((live_servers
* MEMCACHED_POINTS_PER_SERVER
) - 1); pointer_index
++)
342 WATCHPOINT_ASSERT(ptr
->ketama
.continuum
[pointer_index
].value
<= ptr
->ketama
.continuum
[pointer_index
+ 1].value
);
346 return MEMCACHED_SUCCESS
;
349 static memcached_return_t
server_add(memcached_st
*ptr
,
350 const memcached_string_t
& hostname
,
353 memcached_connection_t type
)
355 assert_msg(ptr
, "Programmer mistake, somehow server_add() was passed a NULL memcached_st");
356 if ( (ptr
->flags
.use_udp
and type
!= MEMCACHED_CONNECTION_UDP
)
357 or ( (type
== MEMCACHED_CONNECTION_UDP
) and (not ptr
->flags
.use_udp
) ) )
359 return memcached_set_error(*ptr
, MEMCACHED_INVALID_HOST_PROTOCOL
, MEMCACHED_AT
);
362 memcached_server_st
*new_host_list
= static_cast<memcached_server_st
*>(libmemcached_realloc(ptr
, memcached_server_list(ptr
),
363 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+ 1)));
365 if (not new_host_list
)
367 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
370 memcached_server_list_set(ptr
, new_host_list
);
372 /* TODO: Check return type */
373 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, memcached_server_count(ptr
));
375 if (not __server_create_with(ptr
, instance
, hostname
, port
, weight
, type
))
377 return memcached_set_error(*ptr
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
);
382 ptr
->ketama
.weighted
= true;
385 ptr
->number_of_hosts
++;
387 // @note we place the count in the bottom of the server list
388 instance
= memcached_server_instance_fetch(ptr
, 0);
389 memcached_servers_set_count(instance
, memcached_server_count(ptr
));
391 return run_distribution(ptr
);
395 memcached_return_t
memcached_server_push(memcached_st
*ptr
, const memcached_server_list_st list
)
399 return MEMCACHED_SUCCESS
;
402 uint32_t count
= memcached_server_list_count(list
);
404 memcached_server_st
*new_host_list
;
405 new_host_list
= static_cast<memcached_server_st
*>(libmemcached_realloc(ptr
, memcached_server_list(ptr
),
406 sizeof(memcached_server_st
) * (count
+ memcached_server_count(ptr
))));
408 if (not new_host_list
)
409 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
411 memcached_server_list_set(ptr
, new_host_list
);
413 for (uint32_t x
= 0; x
< count
; x
++)
415 memcached_server_write_instance_st instance
;
417 if ((ptr
->flags
.use_udp
&& list
[x
].type
!= MEMCACHED_CONNECTION_UDP
)
418 or ((list
[x
].type
== MEMCACHED_CONNECTION_UDP
) and not (ptr
->flags
.use_udp
)) )
420 return MEMCACHED_INVALID_HOST_PROTOCOL
;
423 WATCHPOINT_ASSERT(list
[x
].hostname
[0] != 0);
425 // We have extended the array, and now we will find it, and use it.
426 instance
= memcached_server_instance_fetch(ptr
, memcached_server_count(ptr
));
427 WATCHPOINT_ASSERT(instance
);
429 memcached_string_t hostname
= { memcached_string_make_from_cstr(list
[x
].hostname
) };
430 if (__server_create_with(ptr
, instance
,
432 list
[x
].port
, list
[x
].weight
, list
[x
].type
) == NULL
)
434 return memcached_set_error(*ptr
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
);
437 if (list
[x
].weight
> 1)
439 ptr
->ketama
.weighted
= true;
442 ptr
->number_of_hosts
++;
445 // Provides backwards compatibility with server list.
447 memcached_server_write_instance_st instance
;
448 instance
= memcached_server_instance_fetch(ptr
, 0);
449 instance
->number_of_hosts
= memcached_server_count(ptr
);
452 return run_distribution(ptr
);
455 memcached_return_t
memcached_server_add_unix_socket(memcached_st
*ptr
,
456 const char *filename
)
458 return memcached_server_add_unix_socket_with_weight(ptr
, filename
, 0);
461 memcached_return_t
memcached_server_add_unix_socket_with_weight(memcached_st
*ptr
,
462 const char *filename
,
467 return MEMCACHED_FAILURE
;
470 memcached_string_t _filename
= { memcached_string_make_from_cstr(filename
) };
471 if (memcached_is_valid_servername(_filename
) == false)
473 memcached_set_error(*ptr
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
, memcached_literal_param("Invalid filename for socket provided"));
476 return server_add(ptr
, _filename
, 0, weight
, MEMCACHED_CONNECTION_UNIX_SOCKET
);
479 memcached_return_t
memcached_server_add_udp(memcached_st
*ptr
,
480 const char *hostname
,
483 return memcached_server_add_udp_with_weight(ptr
, hostname
, port
, 0);
486 memcached_return_t
memcached_server_add_udp_with_weight(memcached_st
*ptr
,
487 const char *hostname
,
493 return MEMCACHED_INVALID_ARGUMENTS
;
498 port
= MEMCACHED_DEFAULT_PORT
;
503 hostname
= "localhost";
506 memcached_string_t _hostname
= { memcached_string_make_from_cstr(hostname
) };
507 if (memcached_is_valid_servername(_hostname
) == false)
509 memcached_set_error(*ptr
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
, memcached_literal_param("Invalid hostname provided"));
512 return server_add(ptr
, _hostname
, port
, weight
, MEMCACHED_CONNECTION_UDP
);
515 memcached_return_t
memcached_server_add(memcached_st
*ptr
,
516 const char *hostname
,
519 return memcached_server_add_with_weight(ptr
, hostname
, port
, 0);
522 memcached_return_t
memcached_server_add_with_weight(memcached_st
*ptr
,
523 const char *hostname
,
529 return MEMCACHED_INVALID_ARGUMENTS
;
534 port
= MEMCACHED_DEFAULT_PORT
;
537 size_t hostname_length
= hostname
? strlen(hostname
) : 0;
538 if (hostname_length
== 0)
540 hostname
= "localhost";
541 hostname_length
= sizeof("localhost") -1;
544 memcached_string_t _hostname
= { hostname
, hostname_length
};
546 if (memcached_is_valid_servername(_hostname
) == false)
548 return memcached_set_error(*ptr
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
, memcached_literal_param("Invalid hostname provided"));
551 return server_add(ptr
, _hostname
, port
, weight
, _hostname
.c_str
[0] == '/' ? MEMCACHED_CONNECTION_UNIX_SOCKET
: MEMCACHED_CONNECTION_TCP
);
554 memcached_return_t
memcached_server_add_parsed(memcached_st
*ptr
,
555 const char *hostname
,
556 size_t hostname_length
,
560 char buffer
[NI_MAXHOST
];
562 memcpy(buffer
, hostname
, hostname_length
);
563 buffer
[hostname_length
]= 0;
565 memcached_string_t _hostname
= { buffer
, hostname_length
};
567 return server_add(ptr
, _hostname
,
570 MEMCACHED_CONNECTION_TCP
);