p9y
[m6w6/libmemcached] / src / libmemcached / hosts.cc
1 /*
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 +--------------------------------------------------------------------+
14 */
15
16 #include "libmemcached/common.h"
17 #include "libmemcached/assert.hpp"
18 #include "p9y/gettimeofday.hpp"
19 #include "p9y/random.hpp"
20
21 #include <cmath>
22
23 /* Protoypes (static) */
24 static memcached_return_t update_continuum(Memcached *ptr);
25
26 static int compare_servers(const void *p1, const void *p2) {
27 const memcached_instance_st *a = (const memcached_instance_st *) p1;
28 const memcached_instance_st *b = (const memcached_instance_st *) p2;
29
30 int return_value = strcmp(a->_hostname, b->_hostname);
31
32 if (return_value == 0) {
33 return_value = int(a->port() - b->port());
34 }
35
36 return return_value;
37 }
38
39 static void sort_hosts(Memcached *ptr) {
40 if (memcached_server_count(ptr)) {
41 qsort(memcached_instance_list(ptr), memcached_server_count(ptr), sizeof(memcached_instance_st),
42 compare_servers);
43 }
44 }
45
46 memcached_return_t run_distribution(Memcached *ptr) {
47 if (ptr->flags.use_sort_hosts) {
48 sort_hosts(ptr);
49 }
50
51 switch (ptr->distribution) {
52 case MEMCACHED_DISTRIBUTION_CONSISTENT:
53 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA:
54 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY:
55 case MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED:
56 return update_continuum(ptr);
57
58 case MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET:
59 case MEMCACHED_DISTRIBUTION_MODULA:
60 break;
61
62 case MEMCACHED_DISTRIBUTION_RANDOM:
63 srandom((uint32_t) time(NULL));
64 break;
65
66 case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX:
67 default:
68 assert_msg(0, "Invalid distribution type passed to run_distribution()");
69 }
70
71 return MEMCACHED_SUCCESS;
72 }
73
74 static uint32_t ketama_server_hash(const char *key, size_t key_length, uint32_t alignment) {
75 unsigned char results[16];
76
77 libhashkit_md5_signature((unsigned char *) key, key_length, results);
78
79 return ((uint32_t)(results[3 + alignment * 4] & 0xFF) << 24)
80 | ((uint32_t)(results[2 + alignment * 4] & 0xFF) << 16)
81 | ((uint32_t)(results[1 + alignment * 4] & 0xFF) << 8) | (results[0 + alignment * 4] & 0xFF);
82 }
83
84 static int continuum_item_cmp(const void *t1, const void *t2) {
85 memcached_continuum_item_st *ct1 = (memcached_continuum_item_st *) t1;
86 memcached_continuum_item_st *ct2 = (memcached_continuum_item_st *) t2;
87
88 /* Why 153? Hmmm... */
89 WATCHPOINT_ASSERT(ct1->value != 153);
90 if (ct1->value == ct2->value) {
91 if (ct1->index == ct2->index) {
92 return 0;
93 } else if (ct1->index > ct2->index) {
94 return 1;
95 } else {
96 return -1;
97 }
98 } else if (ct1->value > ct2->value) {
99 return 1;
100 } else {
101 return -1;
102 }
103 }
104
105 static memcached_return_t update_continuum(Memcached *ptr) {
106 uint32_t continuum_index = 0;
107 uint32_t pointer_counter = 0;
108 uint32_t pointer_per_server = MEMCACHED_POINTS_PER_SERVER;
109 uint32_t pointer_per_hash = 1;
110 uint32_t live_servers = 0;
111 struct timeval now;
112
113 if (gettimeofday(&now, NULL)) {
114 return memcached_set_errno(*ptr, errno, MEMCACHED_AT);
115 }
116
117 memcached_instance_st *list = memcached_instance_list(ptr);
118
119 /* count live servers (those without a retry delay set) */
120 bool is_auto_ejecting = _is_auto_eject_host(ptr);
121 if (is_auto_ejecting) {
122 live_servers = 0;
123 ptr->ketama.next_distribution_rebuild = 0;
124 for (uint32_t host_index = 0; host_index < memcached_server_count(ptr); ++host_index) {
125 if (list[host_index].next_retry <= now.tv_sec) {
126 live_servers++;
127 } else {
128 if (ptr->ketama.next_distribution_rebuild == 0
129 or list[host_index].next_retry < ptr->ketama.next_distribution_rebuild)
130 {
131 ptr->ketama.next_distribution_rebuild = list[host_index].next_retry;
132 }
133 }
134 }
135 } else {
136 live_servers = memcached_server_count(ptr);
137 }
138
139 if (live_servers == 0) {
140 return MEMCACHED_SUCCESS;
141 }
142
143 uint32_t points_per_server =
144 (uint32_t)(memcached_is_weighted_ketama(ptr) ? MEMCACHED_POINTS_PER_SERVER_KETAMA
145 : MEMCACHED_POINTS_PER_SERVER);
146 uint32_t continuum_limit = live_servers * points_per_server;
147 uint32_t continuum_extra = MEMCACHED_CONTINUUM_ADDITION * points_per_server;
148
149 if (continuum_limit > ptr->ketama.continuum_count) {
150 memcached_continuum_item_st *new_ptr;
151
152 new_ptr = libmemcached_xrealloc(ptr, ptr->ketama.continuum, continuum_limit + continuum_extra,
153 memcached_continuum_item_st);
154
155 if (new_ptr == 0) {
156 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
157 }
158
159 ptr->ketama.continuum = new_ptr;
160 ptr->ketama.continuum_count = continuum_limit + continuum_extra;
161 }
162 assert_msg(ptr->ketama.continuum, "Programmer Error, empty ketama continuum");
163
164 uint64_t total_weight = 0;
165 if (memcached_is_weighted_ketama(ptr)) {
166 for (uint32_t host_index = 0; host_index < memcached_server_count(ptr); ++host_index) {
167 if (is_auto_ejecting == false or list[host_index].next_retry <= now.tv_sec) {
168 total_weight += list[host_index].weight;
169 }
170 }
171 }
172
173 for (uint32_t host_index = 0; host_index < memcached_server_count(ptr); ++host_index) {
174 if (is_auto_ejecting and list[host_index].next_retry > now.tv_sec) {
175 continue;
176 }
177
178 if (memcached_is_weighted_ketama(ptr)) {
179 float pct = (float) list[host_index].weight / (float) total_weight;
180 pointer_per_server = (uint32_t)(
181 (::floor((float) (pct * MEMCACHED_POINTS_PER_SERVER_KETAMA / 4 * (float) live_servers
182 + 0.0000000001F)))
183 * 4);
184 pointer_per_hash = 4;
185 if (0 && DEBUG) {
186 printf("ketama_weighted:%s|%d|%llu|%u\n", list[host_index]._hostname,
187 list[host_index].port(), (unsigned long long) list[host_index].weight,
188 pointer_per_server);
189 }
190 }
191
192 if (ptr->distribution == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY) {
193 for (uint32_t pointer_index = 0; pointer_index < pointer_per_server / pointer_per_hash;
194 pointer_index++)
195 {
196 char sort_host[1 + MEMCACHED_NI_MAXHOST + 1 + MEMCACHED_NI_MAXSERV + 1
197 + MEMCACHED_NI_MAXSERV] = "";
198 int sort_host_length;
199
200 // Spymemcached ketema key format is: hostname/ip:port-index
201 // If hostname is not available then: /ip:port-index
202 sort_host_length =
203 snprintf(sort_host, sizeof(sort_host), "/%s:%u-%u", list[host_index]._hostname,
204 (uint32_t) list[host_index].port(), pointer_index);
205
206 if (size_t(sort_host_length) >= sizeof(sort_host) or sort_host_length < 0) {
207 return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
208 memcached_literal_param("snprintf(sizeof(sort_host))"));
209 }
210
211 if (0 && DEBUG) {
212 fprintf(stdout, "update_continuum: key is %s\n", sort_host);
213 }
214
215 if (memcached_is_weighted_ketama(ptr)) {
216 for (uint32_t x = 0; x < pointer_per_hash; x++) {
217 uint32_t value = ketama_server_hash(sort_host, (size_t) sort_host_length, x);
218 ptr->ketama.continuum[continuum_index].index = host_index;
219 ptr->ketama.continuum[continuum_index++].value = value;
220 }
221 } else {
222 uint32_t value = hashkit_digest(&ptr->hashkit, sort_host, (size_t) sort_host_length);
223 ptr->ketama.continuum[continuum_index].index = host_index;
224 ptr->ketama.continuum[continuum_index++].value = value;
225 }
226 }
227 } else {
228 for (uint32_t pointer_index = 1; pointer_index <= pointer_per_server / pointer_per_hash;
229 pointer_index++)
230 {
231 char sort_host[MEMCACHED_NI_MAXHOST + 1 + MEMCACHED_NI_MAXSERV + 1 + MEMCACHED_NI_MAXSERV] =
232 "";
233 int sort_host_length;
234
235 if (list[host_index].port() == MEMCACHED_DEFAULT_PORT) {
236 sort_host_length = snprintf(sort_host, sizeof(sort_host), "%s-%u",
237 list[host_index]._hostname, pointer_index - 1);
238 } else {
239 sort_host_length =
240 snprintf(sort_host, sizeof(sort_host), "%s:%u-%u", list[host_index]._hostname,
241 (uint32_t) list[host_index].port(), pointer_index - 1);
242 }
243
244 if (size_t(sort_host_length) >= sizeof(sort_host) or sort_host_length < 0) {
245 return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
246 memcached_literal_param("snprintf(sizeof(sort_host)))"));
247 }
248
249 if (memcached_is_weighted_ketama(ptr)) {
250 for (uint32_t x = 0; x < pointer_per_hash; x++) {
251 uint32_t value = ketama_server_hash(sort_host, (size_t) sort_host_length, x);
252 ptr->ketama.continuum[continuum_index].index = host_index;
253 ptr->ketama.continuum[continuum_index++].value = value;
254 }
255 } else {
256 uint32_t value = hashkit_digest(&ptr->hashkit, sort_host, (size_t) sort_host_length);
257 ptr->ketama.continuum[continuum_index].index = host_index;
258 ptr->ketama.continuum[continuum_index++].value = value;
259 }
260 }
261 }
262
263 pointer_counter += pointer_per_server;
264 }
265
266 assert_msg(ptr, "Programmer Error, no valid ptr");
267 assert_msg(ptr->ketama.continuum, "Programmer Error, empty ketama continuum");
268 assert_msg(memcached_server_count(ptr) * MEMCACHED_POINTS_PER_SERVER <= MEMCACHED_CONTINUUM_SIZE,
269 "invalid size information being given to qsort()");
270 ptr->ketama.continuum_points_counter = pointer_counter;
271 qsort(ptr->ketama.continuum, ptr->ketama.continuum_points_counter,
272 sizeof(memcached_continuum_item_st), continuum_item_cmp);
273
274 if (DEBUG) {
275 for (uint32_t pointer_index = 0; memcached_server_count(ptr)
276 && pointer_index < ((live_servers * MEMCACHED_POINTS_PER_SERVER) - 1);
277 pointer_index++)
278 {
279 WATCHPOINT_ASSERT(ptr->ketama.continuum[pointer_index].value
280 <= ptr->ketama.continuum[pointer_index + 1].value);
281 }
282 }
283
284 return MEMCACHED_SUCCESS;
285 }
286
287 static memcached_return_t server_add(Memcached *memc, const memcached_string_t &hostname,
288 in_port_t port, uint32_t weight, memcached_connection_t type) {
289 assert_msg(memc, "Programmer mistake, somehow server_add() was passed a NULL memcached_st");
290
291 if (memc->number_of_hosts) {
292 assert(memcached_instance_list(memc));
293 }
294
295 if (memcached_instance_list(memc)) {
296 assert(memc->number_of_hosts);
297 }
298
299 uint32_t host_list_size = memc->number_of_hosts + 1;
300 memcached_instance_st *new_host_list = libmemcached_xrealloc(
301 memc, memcached_instance_list(memc), host_list_size, memcached_instance_st);
302
303 if (new_host_list == NULL) {
304 return memcached_set_error(*memc, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
305 }
306
307 memcached_instance_set(memc, new_host_list, host_list_size);
308 assert(memc->number_of_hosts == host_list_size);
309
310 /* TODO: Check return type */
311 memcached_instance_st *instance =
312 memcached_instance_fetch(memc, memcached_server_count(memc) - 1);
313
314 if (__instance_create_with(memc, instance, hostname, port, weight, type) == NULL) {
315 return memcached_set_error(*memc, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
316 }
317
318 if (weight > 1) {
319 if (memcached_is_consistent_distribution(memc)) {
320 memcached_set_weighted_ketama(memc, true);
321 }
322 }
323
324 return run_distribution(memc);
325 }
326
327 memcached_return_t memcached_server_push(memcached_st *shell, const memcached_server_list_st list) {
328 if (list == NULL) {
329 return MEMCACHED_SUCCESS;
330 }
331
332 Memcached *ptr = memcached2Memcached(shell);
333 if (ptr) {
334 uint32_t original_host_size = memcached_server_count(ptr);
335 uint32_t count = memcached_server_list_count(list);
336 uint32_t host_list_size = count + original_host_size;
337
338 memcached_instance_st *new_host_list = libmemcached_xrealloc(
339 ptr, memcached_instance_list(ptr), host_list_size, memcached_instance_st);
340
341 if (new_host_list == NULL) {
342 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
343 }
344
345 memcached_instance_set(ptr, new_host_list, host_list_size);
346
347 ptr->state.is_parsing = true;
348 for (uint32_t x = 0; x < count; ++x, ++original_host_size) {
349 WATCHPOINT_ASSERT(list[x].hostname[0]);
350
351 // We have extended the array, and now we will find it, and use it.
352 memcached_instance_st *instance = memcached_instance_fetch(ptr, original_host_size);
353 WATCHPOINT_ASSERT(instance);
354
355 memcached_string_t hostname = {memcached_string_make_from_cstr(list[x].hostname)};
356 if (__instance_create_with(ptr, instance, hostname, list[x].port, list[x].weight,
357 list[x].type)
358 == NULL)
359 {
360 ptr->state.is_parsing = false;
361 return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
362 }
363
364 if (list[x].weight > 1) {
365 memcached_set_weighted_ketama(ptr, true);
366 }
367 }
368 ptr->state.is_parsing = false;
369
370 return run_distribution(ptr);
371 }
372
373 return MEMCACHED_INVALID_ARGUMENTS;
374 }
375
376 memcached_return_t memcached_instance_push(memcached_st *ptr,
377 const struct memcached_instance_st *list,
378 uint32_t number_of_hosts) {
379 if (list == NULL) {
380 return MEMCACHED_SUCCESS;
381 }
382
383 uint32_t original_host_size = memcached_server_count(ptr);
384 uint32_t host_list_size = number_of_hosts + original_host_size;
385 memcached_instance_st *new_host_list = libmemcached_xrealloc(
386 ptr, memcached_instance_list(ptr), host_list_size, memcached_instance_st);
387
388 if (new_host_list == NULL) {
389 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
390 }
391
392 memcached_instance_set(ptr, new_host_list, host_list_size);
393
394 // We don't bother with lookups for this operation
395 ptr->state.is_parsing = true;
396
397 // We use original_host_size since size will now point to the first new
398 // instance allocated.
399 for (uint32_t x = 0; x < number_of_hosts; ++x, ++original_host_size) {
400 WATCHPOINT_ASSERT(list[x]._hostname[0]);
401
402 // We have extended the array, and now we will find it, and use it.
403 memcached_instance_st *instance = memcached_instance_fetch(ptr, original_host_size);
404 WATCHPOINT_ASSERT(instance);
405
406 memcached_string_t hostname = {memcached_string_make_from_cstr(list[x]._hostname)};
407 if (__instance_create_with(ptr, instance, hostname, list[x].port(), list[x].weight,
408 list[x].type)
409 == NULL)
410 {
411 ptr->state.is_parsing = false;
412 return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
413 }
414
415 if (list[x].weight > 1) {
416 memcached_set_weighted_ketama(ptr, true);
417 }
418 }
419 ptr->state.is_parsing = false;
420
421 return run_distribution(ptr);
422 }
423
424 memcached_return_t memcached_server_add_unix_socket(memcached_st *ptr, const char *filename) {
425 return memcached_server_add_unix_socket_with_weight(ptr, filename, 0);
426 }
427
428 memcached_return_t memcached_server_add_unix_socket_with_weight(memcached_st *shell,
429 const char *filename,
430 uint32_t weight) {
431 Memcached *ptr = memcached2Memcached(shell);
432 if (ptr) {
433 memcached_string_t _filename = {memcached_string_make_from_cstr(filename)};
434 if (memcached_is_valid_filename(_filename) == false) {
435 return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT,
436 memcached_literal_param("Invalid filename for socket provided"));
437 }
438
439 return server_add(ptr, _filename, 0, weight, MEMCACHED_CONNECTION_UNIX_SOCKET);
440 }
441
442 return MEMCACHED_FAILURE;
443 }
444
445 memcached_return_t memcached_server_add_udp(memcached_st *ptr, const char *hostname,
446 in_port_t port) {
447 return memcached_server_add_udp_with_weight(ptr, hostname, port, 0);
448 }
449
450 memcached_return_t memcached_server_add_udp_with_weight(memcached_st *shell, const char *,
451 in_port_t, uint32_t) {
452 Memcached *self = memcached2Memcached(shell);
453 if (self) {
454 return memcached_set_error(*self, MEMCACHED_DEPRECATED, MEMCACHED_AT);
455 }
456
457 return MEMCACHED_INVALID_ARGUMENTS;
458 }
459
460 memcached_return_t memcached_server_add(memcached_st *shell, const char *hostname, in_port_t port) {
461 return memcached_server_add_with_weight(shell, hostname, port, 0);
462 }
463
464 memcached_return_t memcached_server_add_with_weight(memcached_st *shell, const char *hostname,
465 in_port_t port, uint32_t weight) {
466 Memcached *ptr = memcached2Memcached(shell);
467 if (ptr == NULL) {
468 return MEMCACHED_INVALID_ARGUMENTS;
469 }
470
471 if (port == 0) {
472 port = MEMCACHED_DEFAULT_PORT;
473 }
474
475 size_t hostname_length = hostname ? strlen(hostname) : 0;
476 if (hostname_length == 0) {
477 hostname = "localhost";
478 hostname_length = memcached_literal_param_size("localhost");
479 }
480
481 memcached_string_t _hostname = {hostname, hostname_length};
482
483 if (memcached_is_valid_servername(_hostname) == false) {
484 return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT,
485 memcached_literal_param("Invalid hostname provided"));
486 }
487
488 return server_add(ptr, _hostname, port, weight,
489 _hostname.c_str[0] == '/' ? MEMCACHED_CONNECTION_UNIX_SOCKET
490 : MEMCACHED_CONNECTION_TCP);
491 }
492
493 memcached_return_t memcached_server_add_parsed(memcached_st *ptr, const char *hostname,
494 size_t hostname_length, in_port_t port,
495 uint32_t weight) {
496 char buffer[MEMCACHED_NI_MAXHOST] = {0};
497
498 memcpy(buffer, hostname, hostname_length);
499 buffer[hostname_length] = 0;
500
501 memcached_string_t _hostname = {buffer, hostname_length};
502
503 return server_add(ptr, _hostname, port, weight, MEMCACHED_CONNECTION_TCP);
504 }