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
: return update_continuum(ptr
);
56 case MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET
:
57 case MEMCACHED_DISTRIBUTION_MODULA
: break;
59 case MEMCACHED_DISTRIBUTION_RANDOM
: srandom((uint32_t) time(NULL
)); break;
61 case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX
:
62 default: assert_msg(0, "Invalid distribution type passed to run_distribution()");
65 return MEMCACHED_SUCCESS
;
68 static uint32_t ketama_server_hash(const char *key
, size_t key_length
, uint32_t alignment
) {
69 unsigned char results
[16];
71 libhashkit_md5_signature((unsigned char *) key
, key_length
, results
);
73 return ((uint32_t)(results
[3 + alignment
* 4] & 0xFF) << 24)
74 | ((uint32_t)(results
[2 + alignment
* 4] & 0xFF) << 16)
75 | ((uint32_t)(results
[1 + alignment
* 4] & 0xFF) << 8) | (results
[0 + alignment
* 4] & 0xFF);
78 static int continuum_item_cmp(const void *t1
, const void *t2
) {
79 memcached_continuum_item_st
*ct1
= (memcached_continuum_item_st
*) t1
;
80 memcached_continuum_item_st
*ct2
= (memcached_continuum_item_st
*) t2
;
82 /* Why 153? Hmmm... */
83 WATCHPOINT_ASSERT(ct1
->value
!= 153);
84 if (ct1
->value
== ct2
->value
) {
85 if (ct1
->index
== ct2
->index
) {
87 } else if (ct1
->index
> ct2
->index
) {
92 } else if (ct1
->value
> ct2
->value
) {
99 static memcached_return_t
update_continuum(Memcached
*ptr
) {
100 uint32_t continuum_index
= 0;
101 uint32_t pointer_counter
= 0;
102 uint32_t pointer_per_server
= MEMCACHED_POINTS_PER_SERVER
;
103 uint32_t pointer_per_hash
= 1;
104 uint32_t live_servers
= 0;
107 if (gettimeofday(&now
, NULL
)) {
108 return memcached_set_errno(*ptr
, errno
, MEMCACHED_AT
);
111 memcached_instance_st
*list
= memcached_instance_list(ptr
);
113 /* count live servers (those without a retry delay set) */
114 bool is_auto_ejecting
= _is_auto_eject_host(ptr
);
115 if (is_auto_ejecting
) {
117 ptr
->ketama
.next_distribution_rebuild
= 0;
118 for (uint32_t host_index
= 0; host_index
< memcached_server_count(ptr
); ++host_index
) {
119 if (list
[host_index
].next_retry
<= now
.tv_sec
) {
122 if (ptr
->ketama
.next_distribution_rebuild
== 0
123 or list
[host_index
].next_retry
< ptr
->ketama
.next_distribution_rebuild
)
125 ptr
->ketama
.next_distribution_rebuild
= list
[host_index
].next_retry
;
130 live_servers
= memcached_server_count(ptr
);
133 if (live_servers
== 0) {
134 return MEMCACHED_SUCCESS
;
137 uint32_t points_per_server
=
138 (uint32_t)(memcached_is_weighted_ketama(ptr
) ? MEMCACHED_POINTS_PER_SERVER_KETAMA
139 : MEMCACHED_POINTS_PER_SERVER
);
140 uint32_t continuum_limit
= live_servers
* points_per_server
;
141 uint32_t continuum_extra
= MEMCACHED_CONTINUUM_ADDITION
* points_per_server
;
143 if (continuum_limit
> ptr
->ketama
.continuum_count
) {
144 memcached_continuum_item_st
*new_ptr
;
146 new_ptr
= libmemcached_xrealloc(ptr
, ptr
->ketama
.continuum
, continuum_limit
+ continuum_extra
,
147 memcached_continuum_item_st
);
150 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
153 ptr
->ketama
.continuum
= new_ptr
;
154 ptr
->ketama
.continuum_count
= continuum_limit
+ continuum_extra
;
156 assert_msg(ptr
->ketama
.continuum
, "Programmer Error, empty ketama continuum");
158 uint64_t total_weight
= 0;
159 if (memcached_is_weighted_ketama(ptr
)) {
160 for (uint32_t host_index
= 0; host_index
< memcached_server_count(ptr
); ++host_index
) {
161 if (is_auto_ejecting
== false or list
[host_index
].next_retry
<= now
.tv_sec
) {
162 total_weight
+= list
[host_index
].weight
;
167 for (uint32_t host_index
= 0; host_index
< memcached_server_count(ptr
); ++host_index
) {
168 if (is_auto_ejecting
and list
[host_index
].next_retry
> now
.tv_sec
) {
172 if (memcached_is_weighted_ketama(ptr
)) {
173 float pct
= (float) list
[host_index
].weight
/ (float) total_weight
;
174 pointer_per_server
= (uint32_t)(
175 (::floor((float) (pct
* MEMCACHED_POINTS_PER_SERVER_KETAMA
/ 4 * (float) live_servers
178 pointer_per_hash
= 4;
180 printf("ketama_weighted:%s|%d|%llu|%u\n", list
[host_index
]._hostname
,
181 list
[host_index
].port(), (unsigned long long) list
[host_index
].weight
,
186 if (ptr
->distribution
== MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) {
187 for (uint32_t pointer_index
= 0; pointer_index
< pointer_per_server
/ pointer_per_hash
;
190 char sort_host
[1 + MEMCACHED_NI_MAXHOST
+ 1 + MEMCACHED_NI_MAXSERV
+ 1
191 + MEMCACHED_NI_MAXSERV
] = "";
192 int sort_host_length
;
194 // Spymemcached ketema key format is: hostname/ip:port-index
195 // If hostname is not available then: /ip:port-index
197 snprintf(sort_host
, sizeof(sort_host
), "/%s:%u-%u", list
[host_index
]._hostname
,
198 (uint32_t) list
[host_index
].port(), pointer_index
);
200 if (size_t(sort_host_length
) >= sizeof(sort_host
) or sort_host_length
< 0) {
201 return memcached_set_error(*ptr
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
,
202 memcached_literal_param("snprintf(sizeof(sort_host))"));
206 fprintf(stdout
, "update_continuum: key is %s\n", sort_host
);
209 if (memcached_is_weighted_ketama(ptr
)) {
210 for (uint32_t x
= 0; x
< pointer_per_hash
; x
++) {
211 uint32_t value
= ketama_server_hash(sort_host
, (size_t) sort_host_length
, x
);
212 ptr
->ketama
.continuum
[continuum_index
].index
= host_index
;
213 ptr
->ketama
.continuum
[continuum_index
++].value
= value
;
216 uint32_t value
= hashkit_digest(&ptr
->hashkit
, sort_host
, (size_t) sort_host_length
);
217 ptr
->ketama
.continuum
[continuum_index
].index
= host_index
;
218 ptr
->ketama
.continuum
[continuum_index
++].value
= value
;
222 for (uint32_t pointer_index
= 1; pointer_index
<= pointer_per_server
/ pointer_per_hash
;
225 char sort_host
[MEMCACHED_NI_MAXHOST
+ 1 + MEMCACHED_NI_MAXSERV
+ 1 + MEMCACHED_NI_MAXSERV
] =
227 int sort_host_length
;
229 if (list
[host_index
].port() == MEMCACHED_DEFAULT_PORT
) {
230 sort_host_length
= snprintf(sort_host
, sizeof(sort_host
), "%s-%u",
231 list
[host_index
]._hostname
, pointer_index
- 1);
234 snprintf(sort_host
, sizeof(sort_host
), "%s:%u-%u", list
[host_index
]._hostname
,
235 (uint32_t) list
[host_index
].port(), pointer_index
- 1);
238 if (size_t(sort_host_length
) >= sizeof(sort_host
) or sort_host_length
< 0) {
239 return memcached_set_error(*ptr
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
,
240 memcached_literal_param("snprintf(sizeof(sort_host)))"));
243 if (memcached_is_weighted_ketama(ptr
)) {
244 for (uint32_t x
= 0; x
< pointer_per_hash
; x
++) {
245 uint32_t value
= ketama_server_hash(sort_host
, (size_t) sort_host_length
, x
);
246 ptr
->ketama
.continuum
[continuum_index
].index
= host_index
;
247 ptr
->ketama
.continuum
[continuum_index
++].value
= value
;
250 uint32_t value
= hashkit_digest(&ptr
->hashkit
, sort_host
, (size_t) sort_host_length
);
251 ptr
->ketama
.continuum
[continuum_index
].index
= host_index
;
252 ptr
->ketama
.continuum
[continuum_index
++].value
= value
;
257 pointer_counter
+= pointer_per_server
;
260 assert_msg(ptr
, "Programmer Error, no valid ptr");
261 assert_msg(ptr
->ketama
.continuum
, "Programmer Error, empty ketama continuum");
262 assert_msg(memcached_server_count(ptr
) * MEMCACHED_POINTS_PER_SERVER
<= MEMCACHED_CONTINUUM_SIZE
,
263 "invalid size information being given to qsort()");
264 ptr
->ketama
.continuum_points_counter
= pointer_counter
;
265 qsort(ptr
->ketama
.continuum
, ptr
->ketama
.continuum_points_counter
,
266 sizeof(memcached_continuum_item_st
), continuum_item_cmp
);
269 for (uint32_t pointer_index
= 0; memcached_server_count(ptr
)
270 && pointer_index
< ((live_servers
* MEMCACHED_POINTS_PER_SERVER
) - 1);
273 WATCHPOINT_ASSERT(ptr
->ketama
.continuum
[pointer_index
].value
274 <= ptr
->ketama
.continuum
[pointer_index
+ 1].value
);
278 return MEMCACHED_SUCCESS
;
281 static memcached_return_t
server_add(Memcached
*memc
, const memcached_string_t
&hostname
,
282 in_port_t port
, uint32_t weight
, memcached_connection_t type
) {
283 assert_msg(memc
, "Programmer mistake, somehow server_add() was passed a NULL memcached_st");
285 if (memc
->number_of_hosts
) {
286 assert(memcached_instance_list(memc
));
289 if (memcached_instance_list(memc
)) {
290 assert(memc
->number_of_hosts
);
293 uint32_t host_list_size
= memc
->number_of_hosts
+ 1;
294 memcached_instance_st
*new_host_list
= libmemcached_xrealloc(
295 memc
, memcached_instance_list(memc
), host_list_size
, memcached_instance_st
);
297 if (new_host_list
== NULL
) {
298 return memcached_set_error(*memc
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
);
301 memcached_instance_set(memc
, new_host_list
, host_list_size
);
302 assert(memc
->number_of_hosts
== host_list_size
);
304 /* TODO: Check return type */
305 memcached_instance_st
*instance
=
306 memcached_instance_fetch(memc
, memcached_server_count(memc
) - 1);
308 if (__instance_create_with(memc
, instance
, hostname
, port
, weight
, type
) == NULL
) {
309 return memcached_set_error(*memc
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
);
313 if (memcached_is_consistent_distribution(memc
)) {
314 memcached_set_weighted_ketama(memc
, true);
318 return run_distribution(memc
);
321 memcached_return_t
memcached_server_push(memcached_st
*shell
, const memcached_server_list_st list
) {
323 return MEMCACHED_SUCCESS
;
326 Memcached
*ptr
= memcached2Memcached(shell
);
328 uint32_t original_host_size
= memcached_server_count(ptr
);
329 uint32_t count
= memcached_server_list_count(list
);
330 uint32_t host_list_size
= count
+ original_host_size
;
332 memcached_instance_st
*new_host_list
= libmemcached_xrealloc(
333 ptr
, memcached_instance_list(ptr
), host_list_size
, memcached_instance_st
);
335 if (new_host_list
== NULL
) {
336 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
339 memcached_instance_set(ptr
, new_host_list
, host_list_size
);
341 ptr
->state
.is_parsing
= true;
342 for (uint32_t x
= 0; x
< count
; ++x
, ++original_host_size
) {
343 WATCHPOINT_ASSERT(list
[x
].hostname
[0] != 0);
345 // We have extended the array, and now we will find it, and use it.
346 memcached_instance_st
*instance
= memcached_instance_fetch(ptr
, original_host_size
);
347 WATCHPOINT_ASSERT(instance
);
349 memcached_string_t hostname
= {memcached_string_make_from_cstr(list
[x
].hostname
)};
350 if (__instance_create_with(ptr
, instance
, hostname
, list
[x
].port
, list
[x
].weight
,
354 ptr
->state
.is_parsing
= false;
355 return memcached_set_error(*ptr
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
);
358 if (list
[x
].weight
> 1) {
359 memcached_set_weighted_ketama(ptr
, true);
362 ptr
->state
.is_parsing
= false;
364 return run_distribution(ptr
);
367 return MEMCACHED_INVALID_ARGUMENTS
;
370 memcached_return_t
memcached_instance_push(memcached_st
*ptr
,
371 const struct memcached_instance_st
*list
,
372 uint32_t number_of_hosts
) {
374 return MEMCACHED_SUCCESS
;
377 uint32_t original_host_size
= memcached_server_count(ptr
);
378 uint32_t host_list_size
= number_of_hosts
+ original_host_size
;
379 memcached_instance_st
*new_host_list
= libmemcached_xrealloc(
380 ptr
, memcached_instance_list(ptr
), host_list_size
, memcached_instance_st
);
382 if (new_host_list
== NULL
) {
383 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
386 memcached_instance_set(ptr
, new_host_list
, host_list_size
);
388 // We don't bother with lookups for this operation
389 ptr
->state
.is_parsing
= true;
391 // We use original_host_size since size will now point to the first new
392 // instance allocated.
393 for (uint32_t x
= 0; x
< number_of_hosts
; ++x
, ++original_host_size
) {
394 WATCHPOINT_ASSERT(list
[x
]._hostname
[0] != 0);
396 // We have extended the array, and now we will find it, and use it.
397 memcached_instance_st
*instance
= memcached_instance_fetch(ptr
, original_host_size
);
398 WATCHPOINT_ASSERT(instance
);
400 memcached_string_t hostname
= {memcached_string_make_from_cstr(list
[x
]._hostname
)};
401 if (__instance_create_with(ptr
, instance
, hostname
, list
[x
].port(), list
[x
].weight
,
405 ptr
->state
.is_parsing
= false;
406 return memcached_set_error(*ptr
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
);
409 if (list
[x
].weight
> 1) {
410 memcached_set_weighted_ketama(ptr
, true);
413 ptr
->state
.is_parsing
= false;
415 return run_distribution(ptr
);
418 memcached_return_t
memcached_server_add_unix_socket(memcached_st
*ptr
, const char *filename
) {
419 return memcached_server_add_unix_socket_with_weight(ptr
, filename
, 0);
422 memcached_return_t
memcached_server_add_unix_socket_with_weight(memcached_st
*shell
,
423 const char *filename
,
425 Memcached
*ptr
= memcached2Memcached(shell
);
427 memcached_string_t _filename
= {memcached_string_make_from_cstr(filename
)};
428 if (memcached_is_valid_filename(_filename
) == false) {
429 return memcached_set_error(*ptr
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
,
430 memcached_literal_param("Invalid filename for socket provided"));
433 return server_add(ptr
, _filename
, 0, weight
, MEMCACHED_CONNECTION_UNIX_SOCKET
);
436 return MEMCACHED_FAILURE
;
439 memcached_return_t
memcached_server_add_udp(memcached_st
*ptr
, const char *hostname
,
441 return memcached_server_add_udp_with_weight(ptr
, hostname
, port
, 0);
444 memcached_return_t
memcached_server_add_udp_with_weight(memcached_st
*shell
, const char *,
445 in_port_t
, uint32_t) {
446 Memcached
*self
= memcached2Memcached(shell
);
448 return memcached_set_error(*self
, MEMCACHED_DEPRECATED
, MEMCACHED_AT
);
451 return MEMCACHED_INVALID_ARGUMENTS
;
454 memcached_return_t
memcached_server_add(memcached_st
*shell
, const char *hostname
, in_port_t port
) {
455 return memcached_server_add_with_weight(shell
, hostname
, port
, 0);
458 memcached_return_t
memcached_server_add_with_weight(memcached_st
*shell
, const char *hostname
,
459 in_port_t port
, uint32_t weight
) {
460 Memcached
*ptr
= memcached2Memcached(shell
);
462 return MEMCACHED_INVALID_ARGUMENTS
;
466 port
= MEMCACHED_DEFAULT_PORT
;
469 size_t hostname_length
= hostname
? strlen(hostname
) : 0;
470 if (hostname_length
== 0) {
471 hostname
= "localhost";
472 hostname_length
= memcached_literal_param_size("localhost");
475 memcached_string_t _hostname
= {hostname
, hostname_length
};
477 if (memcached_is_valid_servername(_hostname
) == false) {
478 return memcached_set_error(*ptr
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
,
479 memcached_literal_param("Invalid hostname provided"));
482 return server_add(ptr
, _hostname
, port
, weight
,
483 _hostname
.c_str
[0] == '/' ? MEMCACHED_CONNECTION_UNIX_SOCKET
484 : MEMCACHED_CONNECTION_TCP
);
487 memcached_return_t
memcached_server_add_parsed(memcached_st
*ptr
, const char *hostname
,
488 size_t hostname_length
, in_port_t port
,
490 char buffer
[MEMCACHED_NI_MAXHOST
] = {0};
492 memcpy(buffer
, hostname
, hostname_length
);
493 buffer
[hostname_length
] = 0;
495 memcached_string_t _hostname
= {buffer
, hostname_length
};
497 return server_add(ptr
, _hostname
, port
, weight
, MEMCACHED_CONNECTION_TCP
);