src/libmemcached: apply clang-format
[awesomized/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
19 #include <cmath>
20 #include <sys/time.h>
21
22 /* Protoypes (static) */
23 static memcached_return_t update_continuum(Memcached *ptr);
24
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;
28
29 int return_value = strcmp(a->_hostname, b->_hostname);
30
31 if (return_value == 0) {
32 return_value = int(a->port() - b->port());
33 }
34
35 return return_value;
36 }
37
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),
41 compare_servers);
42 }
43 }
44
45 memcached_return_t run_distribution(Memcached *ptr) {
46 if (ptr->flags.use_sort_hosts) {
47 sort_hosts(ptr);
48 }
49
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);
55
56 case MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET:
57 case MEMCACHED_DISTRIBUTION_MODULA: break;
58
59 case MEMCACHED_DISTRIBUTION_RANDOM: srandom((uint32_t) time(NULL)); break;
60
61 case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX:
62 default: assert_msg(0, "Invalid distribution type passed to run_distribution()");
63 }
64
65 return MEMCACHED_SUCCESS;
66 }
67
68 static uint32_t ketama_server_hash(const char *key, size_t key_length, uint32_t alignment) {
69 unsigned char results[16];
70
71 libhashkit_md5_signature((unsigned char *) key, key_length, results);
72
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);
76 }
77
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;
81
82 /* Why 153? Hmmm... */
83 WATCHPOINT_ASSERT(ct1->value != 153);
84 if (ct1->value == ct2->value) {
85 if (ct1->index == ct2->index) {
86 return 0;
87 } else if (ct1->index > ct2->index) {
88 return 1;
89 } else {
90 return -1;
91 }
92 } else if (ct1->value > ct2->value) {
93 return 1;
94 } else {
95 return -1;
96 }
97 }
98
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;
105 struct timeval now;
106
107 if (gettimeofday(&now, NULL)) {
108 return memcached_set_errno(*ptr, errno, MEMCACHED_AT);
109 }
110
111 memcached_instance_st *list = memcached_instance_list(ptr);
112
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) {
116 live_servers = 0;
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) {
120 live_servers++;
121 } else {
122 if (ptr->ketama.next_distribution_rebuild == 0
123 or list[host_index].next_retry < ptr->ketama.next_distribution_rebuild)
124 {
125 ptr->ketama.next_distribution_rebuild = list[host_index].next_retry;
126 }
127 }
128 }
129 } else {
130 live_servers = memcached_server_count(ptr);
131 }
132
133 if (live_servers == 0) {
134 return MEMCACHED_SUCCESS;
135 }
136
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;
142
143 if (continuum_limit > ptr->ketama.continuum_count) {
144 memcached_continuum_item_st *new_ptr;
145
146 new_ptr = libmemcached_xrealloc(ptr, ptr->ketama.continuum, continuum_limit + continuum_extra,
147 memcached_continuum_item_st);
148
149 if (new_ptr == 0) {
150 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
151 }
152
153 ptr->ketama.continuum = new_ptr;
154 ptr->ketama.continuum_count = continuum_limit + continuum_extra;
155 }
156 assert_msg(ptr->ketama.continuum, "Programmer Error, empty ketama continuum");
157
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;
163 }
164 }
165 }
166
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) {
169 continue;
170 }
171
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
176 + 0.0000000001F)))
177 * 4);
178 pointer_per_hash = 4;
179 if (0 && DEBUG) {
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,
182 pointer_per_server);
183 }
184 }
185
186 if (ptr->distribution == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY) {
187 for (uint32_t pointer_index = 0; pointer_index < pointer_per_server / pointer_per_hash;
188 pointer_index++)
189 {
190 char sort_host[1 + MEMCACHED_NI_MAXHOST + 1 + MEMCACHED_NI_MAXSERV + 1
191 + MEMCACHED_NI_MAXSERV] = "";
192 int sort_host_length;
193
194 // Spymemcached ketema key format is: hostname/ip:port-index
195 // If hostname is not available then: /ip:port-index
196 sort_host_length =
197 snprintf(sort_host, sizeof(sort_host), "/%s:%u-%u", list[host_index]._hostname,
198 (uint32_t) list[host_index].port(), pointer_index);
199
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))"));
203 }
204
205 if (0 && DEBUG) {
206 fprintf(stdout, "update_continuum: key is %s\n", sort_host);
207 }
208
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;
214 }
215 } else {
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;
219 }
220 }
221 } else {
222 for (uint32_t pointer_index = 1; pointer_index <= pointer_per_server / pointer_per_hash;
223 pointer_index++)
224 {
225 char sort_host[MEMCACHED_NI_MAXHOST + 1 + MEMCACHED_NI_MAXSERV + 1 + MEMCACHED_NI_MAXSERV] =
226 "";
227 int sort_host_length;
228
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);
232 } else {
233 sort_host_length =
234 snprintf(sort_host, sizeof(sort_host), "%s:%u-%u", list[host_index]._hostname,
235 (uint32_t) list[host_index].port(), pointer_index - 1);
236 }
237
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)))"));
241 }
242
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;
248 }
249 } else {
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;
253 }
254 }
255 }
256
257 pointer_counter += pointer_per_server;
258 }
259
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);
267
268 if (DEBUG) {
269 for (uint32_t pointer_index = 0; memcached_server_count(ptr)
270 && pointer_index < ((live_servers * MEMCACHED_POINTS_PER_SERVER) - 1);
271 pointer_index++)
272 {
273 WATCHPOINT_ASSERT(ptr->ketama.continuum[pointer_index].value
274 <= ptr->ketama.continuum[pointer_index + 1].value);
275 }
276 }
277
278 return MEMCACHED_SUCCESS;
279 }
280
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");
284
285 if (memc->number_of_hosts) {
286 assert(memcached_instance_list(memc));
287 }
288
289 if (memcached_instance_list(memc)) {
290 assert(memc->number_of_hosts);
291 }
292
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);
296
297 if (new_host_list == NULL) {
298 return memcached_set_error(*memc, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
299 }
300
301 memcached_instance_set(memc, new_host_list, host_list_size);
302 assert(memc->number_of_hosts == host_list_size);
303
304 /* TODO: Check return type */
305 memcached_instance_st *instance =
306 memcached_instance_fetch(memc, memcached_server_count(memc) - 1);
307
308 if (__instance_create_with(memc, instance, hostname, port, weight, type) == NULL) {
309 return memcached_set_error(*memc, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
310 }
311
312 if (weight > 1) {
313 if (memcached_is_consistent_distribution(memc)) {
314 memcached_set_weighted_ketama(memc, true);
315 }
316 }
317
318 return run_distribution(memc);
319 }
320
321 memcached_return_t memcached_server_push(memcached_st *shell, const memcached_server_list_st list) {
322 if (list == NULL) {
323 return MEMCACHED_SUCCESS;
324 }
325
326 Memcached *ptr = memcached2Memcached(shell);
327 if (ptr) {
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;
331
332 memcached_instance_st *new_host_list = libmemcached_xrealloc(
333 ptr, memcached_instance_list(ptr), host_list_size, memcached_instance_st);
334
335 if (new_host_list == NULL) {
336 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
337 }
338
339 memcached_instance_set(ptr, new_host_list, host_list_size);
340
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);
344
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);
348
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,
351 list[x].type)
352 == NULL)
353 {
354 ptr->state.is_parsing = false;
355 return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
356 }
357
358 if (list[x].weight > 1) {
359 memcached_set_weighted_ketama(ptr, true);
360 }
361 }
362 ptr->state.is_parsing = false;
363
364 return run_distribution(ptr);
365 }
366
367 return MEMCACHED_INVALID_ARGUMENTS;
368 }
369
370 memcached_return_t memcached_instance_push(memcached_st *ptr,
371 const struct memcached_instance_st *list,
372 uint32_t number_of_hosts) {
373 if (list == NULL) {
374 return MEMCACHED_SUCCESS;
375 }
376
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);
381
382 if (new_host_list == NULL) {
383 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
384 }
385
386 memcached_instance_set(ptr, new_host_list, host_list_size);
387
388 // We don't bother with lookups for this operation
389 ptr->state.is_parsing = true;
390
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);
395
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);
399
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,
402 list[x].type)
403 == NULL)
404 {
405 ptr->state.is_parsing = false;
406 return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
407 }
408
409 if (list[x].weight > 1) {
410 memcached_set_weighted_ketama(ptr, true);
411 }
412 }
413 ptr->state.is_parsing = false;
414
415 return run_distribution(ptr);
416 }
417
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);
420 }
421
422 memcached_return_t memcached_server_add_unix_socket_with_weight(memcached_st *shell,
423 const char *filename,
424 uint32_t weight) {
425 Memcached *ptr = memcached2Memcached(shell);
426 if (ptr) {
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"));
431 }
432
433 return server_add(ptr, _filename, 0, weight, MEMCACHED_CONNECTION_UNIX_SOCKET);
434 }
435
436 return MEMCACHED_FAILURE;
437 }
438
439 memcached_return_t memcached_server_add_udp(memcached_st *ptr, const char *hostname,
440 in_port_t port) {
441 return memcached_server_add_udp_with_weight(ptr, hostname, port, 0);
442 }
443
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);
447 if (self) {
448 return memcached_set_error(*self, MEMCACHED_DEPRECATED, MEMCACHED_AT);
449 }
450
451 return MEMCACHED_INVALID_ARGUMENTS;
452 }
453
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);
456 }
457
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);
461 if (ptr == NULL) {
462 return MEMCACHED_INVALID_ARGUMENTS;
463 }
464
465 if (port == 0) {
466 port = MEMCACHED_DEFAULT_PORT;
467 }
468
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");
473 }
474
475 memcached_string_t _hostname = {hostname, hostname_length};
476
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"));
480 }
481
482 return server_add(ptr, _hostname, port, weight,
483 _hostname.c_str[0] == '/' ? MEMCACHED_CONNECTION_UNIX_SOCKET
484 : MEMCACHED_CONNECTION_TCP);
485 }
486
487 memcached_return_t memcached_server_add_parsed(memcached_st *ptr, const char *hostname,
488 size_t hostname_length, in_port_t port,
489 uint32_t weight) {
490 char buffer[MEMCACHED_NI_MAXHOST] = {0};
491
492 memcpy(buffer, hostname, hostname_length);
493 buffer[hostname_length] = 0;
494
495 memcached_string_t _hostname = {buffer, hostname_length};
496
497 return server_add(ptr, _hostname, port, weight, MEMCACHED_CONNECTION_TCP);
498 }