2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
16 #include "libmemcached/common.h"
17 #include "libmemcached/assert.hpp"
22 /* Protoypes (static) */
23 static memcached_return_t
update_continuum(Memcached
*ptr
);
25 static int compare_servers(const void *p1
, const void *p2
) {
26 const memcached_instance_st
*a
= (const memcached_instance_st
*) p1
;
27 const memcached_instance_st
*b
= (const memcached_instance_st
*) p2
;
29 int return_value
= strcmp(a
->_hostname
, b
->_hostname
);
31 if (return_value
== 0) {
32 return_value
= int(a
->port() - b
->port());
38 static void sort_hosts(Memcached
*ptr
) {
39 if (memcached_server_count(ptr
)) {
40 qsort(memcached_instance_list(ptr
), memcached_server_count(ptr
), sizeof(memcached_instance_st
),
45 memcached_return_t
run_distribution(Memcached
*ptr
) {
46 if (ptr
->flags
.use_sort_hosts
) {
50 switch (ptr
->distribution
) {
51 case MEMCACHED_DISTRIBUTION_CONSISTENT
:
52 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
:
53 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
:
54 case MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED
:
55 return update_continuum(ptr
);
57 case MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET
:
58 case MEMCACHED_DISTRIBUTION_MODULA
:
61 case MEMCACHED_DISTRIBUTION_RANDOM
:
62 srandom((uint32_t) time(NULL
));
65 case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX
:
67 assert_msg(0, "Invalid distribution type passed to run_distribution()");
70 return MEMCACHED_SUCCESS
;
73 static uint32_t ketama_server_hash(const char *key
, size_t key_length
, uint32_t alignment
) {
74 unsigned char results
[16];
76 libhashkit_md5_signature((unsigned char *) key
, key_length
, results
);
78 return ((uint32_t)(results
[3 + alignment
* 4] & 0xFF) << 24)
79 | ((uint32_t)(results
[2 + alignment
* 4] & 0xFF) << 16)
80 | ((uint32_t)(results
[1 + alignment
* 4] & 0xFF) << 8) | (results
[0 + alignment
* 4] & 0xFF);
83 static int continuum_item_cmp(const void *t1
, const void *t2
) {
84 memcached_continuum_item_st
*ct1
= (memcached_continuum_item_st
*) t1
;
85 memcached_continuum_item_st
*ct2
= (memcached_continuum_item_st
*) t2
;
87 /* Why 153? Hmmm... */
88 WATCHPOINT_ASSERT(ct1
->value
!= 153);
89 if (ct1
->value
== ct2
->value
) {
90 if (ct1
->index
== ct2
->index
) {
92 } else if (ct1
->index
> ct2
->index
) {
97 } else if (ct1
->value
> ct2
->value
) {
104 static memcached_return_t
update_continuum(Memcached
*ptr
) {
105 uint32_t continuum_index
= 0;
106 uint32_t pointer_counter
= 0;
107 uint32_t pointer_per_server
= MEMCACHED_POINTS_PER_SERVER
;
108 uint32_t pointer_per_hash
= 1;
109 uint32_t live_servers
= 0;
112 if (gettimeofday(&now
, NULL
)) {
113 return memcached_set_errno(*ptr
, errno
, MEMCACHED_AT
);
116 memcached_instance_st
*list
= memcached_instance_list(ptr
);
118 /* count live servers (those without a retry delay set) */
119 bool is_auto_ejecting
= _is_auto_eject_host(ptr
);
120 if (is_auto_ejecting
) {
122 ptr
->ketama
.next_distribution_rebuild
= 0;
123 for (uint32_t host_index
= 0; host_index
< memcached_server_count(ptr
); ++host_index
) {
124 if (list
[host_index
].next_retry
<= now
.tv_sec
) {
127 if (ptr
->ketama
.next_distribution_rebuild
== 0
128 or list
[host_index
].next_retry
< ptr
->ketama
.next_distribution_rebuild
)
130 ptr
->ketama
.next_distribution_rebuild
= list
[host_index
].next_retry
;
135 live_servers
= memcached_server_count(ptr
);
138 if (live_servers
== 0) {
139 return MEMCACHED_SUCCESS
;
142 uint32_t points_per_server
=
143 (uint32_t)(memcached_is_weighted_ketama(ptr
) ? MEMCACHED_POINTS_PER_SERVER_KETAMA
144 : MEMCACHED_POINTS_PER_SERVER
);
145 uint32_t continuum_limit
= live_servers
* points_per_server
;
146 uint32_t continuum_extra
= MEMCACHED_CONTINUUM_ADDITION
* points_per_server
;
148 if (continuum_limit
> ptr
->ketama
.continuum_count
) {
149 memcached_continuum_item_st
*new_ptr
;
151 new_ptr
= libmemcached_xrealloc(ptr
, ptr
->ketama
.continuum
, continuum_limit
+ continuum_extra
,
152 memcached_continuum_item_st
);
155 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
158 ptr
->ketama
.continuum
= new_ptr
;
159 ptr
->ketama
.continuum_count
= continuum_limit
+ continuum_extra
;
161 assert_msg(ptr
->ketama
.continuum
, "Programmer Error, empty ketama continuum");
163 uint64_t total_weight
= 0;
164 if (memcached_is_weighted_ketama(ptr
)) {
165 for (uint32_t host_index
= 0; host_index
< memcached_server_count(ptr
); ++host_index
) {
166 if (is_auto_ejecting
== false or list
[host_index
].next_retry
<= now
.tv_sec
) {
167 total_weight
+= list
[host_index
].weight
;
172 for (uint32_t host_index
= 0; host_index
< memcached_server_count(ptr
); ++host_index
) {
173 if (is_auto_ejecting
and list
[host_index
].next_retry
> now
.tv_sec
) {
177 if (memcached_is_weighted_ketama(ptr
)) {
178 float pct
= (float) list
[host_index
].weight
/ (float) total_weight
;
179 pointer_per_server
= (uint32_t)(
180 (::floor((float) (pct
* MEMCACHED_POINTS_PER_SERVER_KETAMA
/ 4 * (float) live_servers
183 pointer_per_hash
= 4;
185 printf("ketama_weighted:%s|%d|%llu|%u\n", list
[host_index
]._hostname
,
186 list
[host_index
].port(), (unsigned long long) list
[host_index
].weight
,
191 if (ptr
->distribution
== MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) {
192 for (uint32_t pointer_index
= 0; pointer_index
< pointer_per_server
/ pointer_per_hash
;
195 char sort_host
[1 + MEMCACHED_NI_MAXHOST
+ 1 + MEMCACHED_NI_MAXSERV
+ 1
196 + MEMCACHED_NI_MAXSERV
] = "";
197 int sort_host_length
;
199 // Spymemcached ketema key format is: hostname/ip:port-index
200 // If hostname is not available then: /ip:port-index
202 snprintf(sort_host
, sizeof(sort_host
), "/%s:%u-%u", list
[host_index
]._hostname
,
203 (uint32_t) list
[host_index
].port(), pointer_index
);
205 if (size_t(sort_host_length
) >= sizeof(sort_host
) or sort_host_length
< 0) {
206 return memcached_set_error(*ptr
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
,
207 memcached_literal_param("snprintf(sizeof(sort_host))"));
211 fprintf(stdout
, "update_continuum: key is %s\n", sort_host
);
214 if (memcached_is_weighted_ketama(ptr
)) {
215 for (uint32_t x
= 0; x
< pointer_per_hash
; x
++) {
216 uint32_t value
= ketama_server_hash(sort_host
, (size_t) sort_host_length
, x
);
217 ptr
->ketama
.continuum
[continuum_index
].index
= host_index
;
218 ptr
->ketama
.continuum
[continuum_index
++].value
= value
;
221 uint32_t value
= hashkit_digest(&ptr
->hashkit
, sort_host
, (size_t) sort_host_length
);
222 ptr
->ketama
.continuum
[continuum_index
].index
= host_index
;
223 ptr
->ketama
.continuum
[continuum_index
++].value
= value
;
227 for (uint32_t pointer_index
= 1; pointer_index
<= pointer_per_server
/ pointer_per_hash
;
230 char sort_host
[MEMCACHED_NI_MAXHOST
+ 1 + MEMCACHED_NI_MAXSERV
+ 1 + MEMCACHED_NI_MAXSERV
] =
232 int sort_host_length
;
234 if (list
[host_index
].port() == MEMCACHED_DEFAULT_PORT
) {
235 sort_host_length
= snprintf(sort_host
, sizeof(sort_host
), "%s-%u",
236 list
[host_index
]._hostname
, pointer_index
- 1);
239 snprintf(sort_host
, sizeof(sort_host
), "%s:%u-%u", list
[host_index
]._hostname
,
240 (uint32_t) list
[host_index
].port(), pointer_index
- 1);
243 if (size_t(sort_host_length
) >= sizeof(sort_host
) or sort_host_length
< 0) {
244 return memcached_set_error(*ptr
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
,
245 memcached_literal_param("snprintf(sizeof(sort_host)))"));
248 if (memcached_is_weighted_ketama(ptr
)) {
249 for (uint32_t x
= 0; x
< pointer_per_hash
; x
++) {
250 uint32_t value
= ketama_server_hash(sort_host
, (size_t) sort_host_length
, x
);
251 ptr
->ketama
.continuum
[continuum_index
].index
= host_index
;
252 ptr
->ketama
.continuum
[continuum_index
++].value
= value
;
255 uint32_t value
= hashkit_digest(&ptr
->hashkit
, sort_host
, (size_t) sort_host_length
);
256 ptr
->ketama
.continuum
[continuum_index
].index
= host_index
;
257 ptr
->ketama
.continuum
[continuum_index
++].value
= value
;
262 pointer_counter
+= pointer_per_server
;
265 assert_msg(ptr
, "Programmer Error, no valid ptr");
266 assert_msg(ptr
->ketama
.continuum
, "Programmer Error, empty ketama continuum");
267 assert_msg(memcached_server_count(ptr
) * MEMCACHED_POINTS_PER_SERVER
<= MEMCACHED_CONTINUUM_SIZE
,
268 "invalid size information being given to qsort()");
269 ptr
->ketama
.continuum_points_counter
= pointer_counter
;
270 qsort(ptr
->ketama
.continuum
, ptr
->ketama
.continuum_points_counter
,
271 sizeof(memcached_continuum_item_st
), continuum_item_cmp
);
274 for (uint32_t pointer_index
= 0; memcached_server_count(ptr
)
275 && pointer_index
< ((live_servers
* MEMCACHED_POINTS_PER_SERVER
) - 1);
278 WATCHPOINT_ASSERT(ptr
->ketama
.continuum
[pointer_index
].value
279 <= ptr
->ketama
.continuum
[pointer_index
+ 1].value
);
283 return MEMCACHED_SUCCESS
;
286 static memcached_return_t
server_add(Memcached
*memc
, const memcached_string_t
&hostname
,
287 in_port_t port
, uint32_t weight
, memcached_connection_t type
) {
288 assert_msg(memc
, "Programmer mistake, somehow server_add() was passed a NULL memcached_st");
290 if (memc
->number_of_hosts
) {
291 assert(memcached_instance_list(memc
));
294 if (memcached_instance_list(memc
)) {
295 assert(memc
->number_of_hosts
);
298 uint32_t host_list_size
= memc
->number_of_hosts
+ 1;
299 memcached_instance_st
*new_host_list
= libmemcached_xrealloc(
300 memc
, memcached_instance_list(memc
), host_list_size
, memcached_instance_st
);
302 if (new_host_list
== NULL
) {
303 return memcached_set_error(*memc
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
);
306 memcached_instance_set(memc
, new_host_list
, host_list_size
);
307 assert(memc
->number_of_hosts
== host_list_size
);
309 /* TODO: Check return type */
310 memcached_instance_st
*instance
=
311 memcached_instance_fetch(memc
, memcached_server_count(memc
) - 1);
313 if (__instance_create_with(memc
, instance
, hostname
, port
, weight
, type
) == NULL
) {
314 return memcached_set_error(*memc
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
);
318 if (memcached_is_consistent_distribution(memc
)) {
319 memcached_set_weighted_ketama(memc
, true);
323 return run_distribution(memc
);
326 memcached_return_t
memcached_server_push(memcached_st
*shell
, const memcached_server_list_st list
) {
328 return MEMCACHED_SUCCESS
;
331 Memcached
*ptr
= memcached2Memcached(shell
);
333 uint32_t original_host_size
= memcached_server_count(ptr
);
334 uint32_t count
= memcached_server_list_count(list
);
335 uint32_t host_list_size
= count
+ original_host_size
;
337 memcached_instance_st
*new_host_list
= libmemcached_xrealloc(
338 ptr
, memcached_instance_list(ptr
), host_list_size
, memcached_instance_st
);
340 if (new_host_list
== NULL
) {
341 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
344 memcached_instance_set(ptr
, new_host_list
, host_list_size
);
346 ptr
->state
.is_parsing
= true;
347 for (uint32_t x
= 0; x
< count
; ++x
, ++original_host_size
) {
348 WATCHPOINT_ASSERT(list
[x
].hostname
[0] != 0);
350 // We have extended the array, and now we will find it, and use it.
351 memcached_instance_st
*instance
= memcached_instance_fetch(ptr
, original_host_size
);
352 WATCHPOINT_ASSERT(instance
);
354 memcached_string_t hostname
= {memcached_string_make_from_cstr(list
[x
].hostname
)};
355 if (__instance_create_with(ptr
, instance
, hostname
, list
[x
].port
, list
[x
].weight
,
359 ptr
->state
.is_parsing
= false;
360 return memcached_set_error(*ptr
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
);
363 if (list
[x
].weight
> 1) {
364 memcached_set_weighted_ketama(ptr
, true);
367 ptr
->state
.is_parsing
= false;
369 return run_distribution(ptr
);
372 return MEMCACHED_INVALID_ARGUMENTS
;
375 memcached_return_t
memcached_instance_push(memcached_st
*ptr
,
376 const struct memcached_instance_st
*list
,
377 uint32_t number_of_hosts
) {
379 return MEMCACHED_SUCCESS
;
382 uint32_t original_host_size
= memcached_server_count(ptr
);
383 uint32_t host_list_size
= number_of_hosts
+ original_host_size
;
384 memcached_instance_st
*new_host_list
= libmemcached_xrealloc(
385 ptr
, memcached_instance_list(ptr
), host_list_size
, memcached_instance_st
);
387 if (new_host_list
== NULL
) {
388 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
391 memcached_instance_set(ptr
, new_host_list
, host_list_size
);
393 // We don't bother with lookups for this operation
394 ptr
->state
.is_parsing
= true;
396 // We use original_host_size since size will now point to the first new
397 // instance allocated.
398 for (uint32_t x
= 0; x
< number_of_hosts
; ++x
, ++original_host_size
) {
399 WATCHPOINT_ASSERT(list
[x
]._hostname
[0] != 0);
401 // We have extended the array, and now we will find it, and use it.
402 memcached_instance_st
*instance
= memcached_instance_fetch(ptr
, original_host_size
);
403 WATCHPOINT_ASSERT(instance
);
405 memcached_string_t hostname
= {memcached_string_make_from_cstr(list
[x
]._hostname
)};
406 if (__instance_create_with(ptr
, instance
, hostname
, list
[x
].port(), list
[x
].weight
,
410 ptr
->state
.is_parsing
= false;
411 return memcached_set_error(*ptr
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
);
414 if (list
[x
].weight
> 1) {
415 memcached_set_weighted_ketama(ptr
, true);
418 ptr
->state
.is_parsing
= false;
420 return run_distribution(ptr
);
423 memcached_return_t
memcached_server_add_unix_socket(memcached_st
*ptr
, const char *filename
) {
424 return memcached_server_add_unix_socket_with_weight(ptr
, filename
, 0);
427 memcached_return_t
memcached_server_add_unix_socket_with_weight(memcached_st
*shell
,
428 const char *filename
,
430 Memcached
*ptr
= memcached2Memcached(shell
);
432 memcached_string_t _filename
= {memcached_string_make_from_cstr(filename
)};
433 if (memcached_is_valid_filename(_filename
) == false) {
434 return memcached_set_error(*ptr
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
,
435 memcached_literal_param("Invalid filename for socket provided"));
438 return server_add(ptr
, _filename
, 0, weight
, MEMCACHED_CONNECTION_UNIX_SOCKET
);
441 return MEMCACHED_FAILURE
;
444 memcached_return_t
memcached_server_add_udp(memcached_st
*ptr
, const char *hostname
,
446 return memcached_server_add_udp_with_weight(ptr
, hostname
, port
, 0);
449 memcached_return_t
memcached_server_add_udp_with_weight(memcached_st
*shell
, const char *,
450 in_port_t
, uint32_t) {
451 Memcached
*self
= memcached2Memcached(shell
);
453 return memcached_set_error(*self
, MEMCACHED_DEPRECATED
, MEMCACHED_AT
);
456 return MEMCACHED_INVALID_ARGUMENTS
;
459 memcached_return_t
memcached_server_add(memcached_st
*shell
, const char *hostname
, in_port_t port
) {
460 return memcached_server_add_with_weight(shell
, hostname
, port
, 0);
463 memcached_return_t
memcached_server_add_with_weight(memcached_st
*shell
, const char *hostname
,
464 in_port_t port
, uint32_t weight
) {
465 Memcached
*ptr
= memcached2Memcached(shell
);
467 return MEMCACHED_INVALID_ARGUMENTS
;
471 port
= MEMCACHED_DEFAULT_PORT
;
474 size_t hostname_length
= hostname
? strlen(hostname
) : 0;
475 if (hostname_length
== 0) {
476 hostname
= "localhost";
477 hostname_length
= memcached_literal_param_size("localhost");
480 memcached_string_t _hostname
= {hostname
, hostname_length
};
482 if (memcached_is_valid_servername(_hostname
) == false) {
483 return memcached_set_error(*ptr
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
,
484 memcached_literal_param("Invalid hostname provided"));
487 return server_add(ptr
, _hostname
, port
, weight
,
488 _hostname
.c_str
[0] == '/' ? MEMCACHED_CONNECTION_UNIX_SOCKET
489 : MEMCACHED_CONNECTION_TCP
);
492 memcached_return_t
memcached_server_add_parsed(memcached_st
*ptr
, const char *hostname
,
493 size_t hostname_length
, in_port_t port
,
495 char buffer
[MEMCACHED_NI_MAXHOST
] = {0};
497 memcpy(buffer
, hostname
, hostname_length
);
498 buffer
[hostname_length
] = 0;
500 memcached_string_t _hostname
= {buffer
, hostname_length
};
502 return server_add(ptr
, _hostname
, port
, weight
, MEMCACHED_CONNECTION_TCP
);