Loading...
Searching...
No Matches
as_node.h
Go to the documentation of this file.
1/*
2 * Copyright 2008-2024 Aerospike, Inc.
3 *
4 * Portions may be licensed to Aerospike, Inc. under one or more contributor
5 * license agreements.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8 * use this file except in compliance with the License. You may obtain a copy of
9 * the License at http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 */
17#pragma once
18
19#include <aerospike/as_atomic.h>
20#include <aerospike/as_config.h>
22#include <aerospike/as_error.h>
23#include <aerospike/as_event.h>
25#include <aerospike/as_socket.h>
27#include <aerospike/as_queue.h>
28#include <aerospike/as_vector.h>
29
30#if !defined(_MSC_VER)
31#include <netinet/in.h>
32#include <sys/uio.h>
33#endif
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
39/******************************************************************************
40 * MACROS
41 *****************************************************************************/
42
43/**
44 * Maximum size (including NULL byte) of a hostname.
45 */
46#define AS_HOSTNAME_SIZE 256
47
48/**
49 * Maximum size of node name
50 */
51#define AS_NODE_NAME_SIZE 20
52
53// Leave this is in for backwards compatibility.
54#define AS_NODE_NAME_MAX_SIZE AS_NODE_NAME_SIZE
55
56#define AS_FEATURES_PARTITION_SCAN (1 << 0)
57#define AS_FEATURES_QUERY_SHOW (1 << 1)
58#define AS_FEATURES_BATCH_ANY (1 << 2)
59#define AS_FEATURES_PARTITION_QUERY (1 << 3)
60
61#define AS_ADDRESS4_MAX 4
62#define AS_ADDRESS6_MAX 8
63
64/******************************************************************************
65 * TYPES
66 *****************************************************************************/
67
68/**
69 * Socket address information.
70 */
71typedef struct as_address_s {
72 /**
73 * Socket IP address.
74 */
75 struct sockaddr_storage addr;
76
77 /**
78 * Socket IP address string representation including port.
79 */
81
83
84/**
85 * @private
86 * Host address alias information.
87 */
88typedef struct as_alias_s {
89 /**
90 * Hostname or IP address string representation.
91 */
92 char name[AS_HOSTNAME_SIZE];
93
94 /**
95 * Socket IP port.
96 */
97 uint16_t port;
98
99} as_alias;
100
101/**
102 * @private
103 * Rack.
104 */
105typedef struct as_rack_s {
106 /**
107 * Namespace
108 */
110
111 /**
112 * Rack ID
113 */
115
116} as_rack;
117
118/**
119 * @private
120 * Racks.
121 */
122typedef struct as_racks_s {
123 /**
124 * Reference count of racks array.
125 */
126 uint32_t ref_count;
127
128 /**
129 * Rack ID when all namespaces use same rack.
130 */
132
133 /**
134 * Length of racks array.
135 */
136 uint32_t size;
137
138 /**
139 * Pad to 8 byte boundary.
140 */
141 uint32_t pad;
142
143 /**
144 * Racks array.
145 */
146 as_rack racks[];
147
148} as_racks;
149
150/**
151 * @private
152 * Session info.
153 */
154typedef struct as_session_s {
155 /**
156 * Reference count of session.
157 */
158 uint32_t ref_count;
159
160 /**
161 * Session token length.
162 */
163 uint32_t token_length;
164
165 /**
166 * Session expiration for this node.
167 */
168 uint64_t expiration;
169
170 /**
171 * Session token for this node.
172 */
173 uint8_t token[];
174
175} as_session;
176
177/**
178 * @private
179 * Async connection pool.
180 */
181typedef struct as_async_conn_pool_s {
182 /**
183 * Async connection queue.
184 */
186
187 /**
188 * Min connections allowed for this pool.
189 */
190 uint32_t min_size;
191
192 /**
193 * Max connections allowed for this pool.
194 */
195 uint32_t limit;
196
197 /**
198 * Total async connections opened.
199 */
200 uint32_t opened;
201
202 /**
203 * Total async connections closed.
204 */
205 uint32_t closed;
206
208
209/**
210 * Node metrics latency bucket struct
211 */
212typedef struct as_node_metrics_s {
215
216struct as_cluster_s;
217
218/**
219 * Server node representation.
220 */
221typedef struct as_node_s {
222 /**
223 * Reference count of node.
224 */
225 uint32_t ref_count;
226
227 /**
228 * Reference count of node in partition maps.
229 */
231
232 /**
233 * Server's generation count for partition management.
234 */
236
237 /**
238 * Features supported by server. Stored in bitmap.
239 */
240 uint32_t features;
241
242 /**
243 * TLS certificate name (needed for TLS only, NULL otherwise).
244 */
245 char* tls_name;
246
247 /**
248 * The name of the node.
249 */
251
252 /**
253 * Primary address index into addresses array.
254 */
256
257 /**
258 * Number of IPv4 addresses.
259 */
261
262 /**
263 * Number of IPv6 addresses.
264 */
266
267 /**
268 * Array of IP addresses. Not thread-safe.
269 */
271
272 /**
273 * Array of hostnames aliases. Not thread-safe.
274 */
275 as_vector /* <as_alias> */ aliases;
276
277 /**
278 * Cluster from which this node resides.
279 */
280 struct as_cluster_s* cluster;
281
282 /**
283 * Pools of current, cached sockets.
284 */
286
287 /**
288 * Array of connection pools used in async commands. There is one pool per node/event loop.
289 * Only used by event loop threads. Not thread-safe.
290 */
292
293 /**
294 * Pool of connections used in pipelined async commands. Also not thread-safe.
295 */
297
298 /**
299 * Authentication session.
300 */
302
303 /**
304 * Racks data.
305 */
307
308 /**
309 * Node metrics
310 */
312
313 /**
314 * Socket used exclusively for cluster tend thread info requests.
315 */
317
318 /**
319 * Transaction error count since node was initialized. If the error is retryable, multiple errors per
320 * transaction may occur.
321 */
322 uint64_t error_count;
323
324 /**
325 * Transaction timeout count since node was initialized. If the timeout is retryable (ie socketTimeout),
326 * multiple timeouts per transaction may occur.
327 */
329
330 /**
331 * Connection queue iterator. Not atomic by design.
332 */
333 uint32_t conn_iter;
334
335 /**
336 * Total sync connections opened.
337 */
339
340 /**
341 * Total sync connections closed.
342 */
344
345 /**
346 * Error count for this node's error_rate_window.
347 */
348 uint32_t error_rate;
349
350 /**
351 * Server's generation count for peers.
352 */
354
355 /**
356 * Number of peers returned by server node.
357 */
358 uint32_t peers_count;
359
360 /**
361 * Server's generation count for partition rebalancing.
362 */
364
365 /**
366 * Number of other nodes that consider this node a member of the cluster.
367 */
368 uint32_t friends;
369
370 /**
371 * Number of consecutive info request failures.
372 */
373 uint32_t failures;
374
375 /**
376 * Shared memory node array index.
377 */
378 uint32_t index;
379
380 /**
381 * Should user login to avoid session expiration.
382 */
384
385 /**
386 * Is node currently active.
387 */
388 uint8_t active;
389
390 /**
391 * Did partition change in current cluster tend.
392 */
394
395 /**
396 * Did rebalance generation change in current cluster tend.
397 */
399
400} as_node;
401
402/**
403 * @private
404 * Node discovery information.
405 */
406typedef struct as_node_info_s {
407 /**
408 * Node name.
409 */
411
412 /**
413 * Features supported by server. Stored in bitmap.
414 */
415 uint32_t features;
416
417 /**
418 * Host.
419 */
421
422 /**
423 * Validated socket.
424 */
426
427 /**
428 * Socket address.
429 */
430 struct sockaddr_storage addr;
431
432 /**
433 * Authentication session.
434 */
436
438
439/******************************************************************************
440 * FUNCTIONS
441 ******************************************************************************/
442
443/**
444 * @private
445 * Create new cluster node.
446 */
447as_node*
448as_node_create(struct as_cluster_s* cluster, as_node_info* node_info);
449
450/**
451 * @private
452 * Close all connections in pool and free resources.
453 */
454AS_EXTERN void
456
457/**
458 * @private
459 * Destroy node metrics.
460 */
461void
463
464/**
465 * @private
466 * Create configured minimum number of connections.
467 */
468void
470
471/**
472 * @private
473 * Check if node is active from a transaction thread.
474 */
475static inline bool
477{
478 return (bool)as_load_uint8_acq(&node->active);
479}
480
481/**
482 * @private
483 * Set node to inactive.
484 */
485static inline void
487{
488 // Make volatile write so changes are reflected in other threads.
489 as_store_uint8_rls(&node->active, false);
490}
491
492/**
493 * @private
494 * Read volatile node.
495 */
496static inline as_node*
498{
499 return (as_node*)as_load_ptr((void* const*)node);
500}
501
502/**
503 * @private
504 * Reserve existing cluster node.
505 */
506static inline void
508{
510}
511
512/**
513 * @private
514 * Set volatile node.
515 */
516static inline void
518{
519 as_store_ptr_rls((void**)trg, src);
520}
521
522/**
523 * @private
524 * Release existing cluster node.
525 */
526static inline void
528{
529 if (as_aaf_uint32_rls(&node->ref_count, -1) == 0) {
530 as_fence_acq();
531 as_node_destroy(node);
532 }
533}
534
535/**
536 * @private
537 * Release node on next cluster tend iteration.
538 */
539void
541
542/**
543 * @private
544 * Add socket address to node addresses.
545 */
546void
547as_node_add_address(as_node* node, struct sockaddr* addr);
548
549/**
550 * @private
551 * Add hostname to node aliases.
552 */
553void
554as_node_add_alias(as_node* node, const char* hostname, uint16_t port);
555
556/**
557 * Get primary socket address.
558 */
559static inline as_address*
561{
562 return &node->addresses[node->address_index];
563}
564
565/**
566 * Get socket address as a string.
567 */
568static inline const char*
570{
571 return node->addresses[node->address_index].name;
572}
573
574/**
575 * @private
576 * Attempt to authenticate given current cluster's user and password.
577 */
579as_node_authenticate_connection(struct as_cluster_s* cluster, uint64_t deadline_ms);
580
581/**
582 * @private
583 * Get a connection to the given node from pool and validate. Return 0 on success.
584 */
586as_node_get_connection(as_error* err, as_node* node, uint32_t socket_timeout, uint64_t deadline_ms, as_socket* sock);
587
588/**
589 * @private
590 * Close a node's connection and update node/pool statistics.
591 */
592static inline void
599
600/**
601 * @private
602 * Close a node's connection and update node statistics.
603 */
604static inline void
610
611/**
612 * @private
613 * Put connection back into pool.
614 */
615static inline void
617{
618 // Save pool.
619 as_conn_pool* pool = sock->pool;
620
621 // Update last used timestamp.
622 sock->last_used = cf_getns();
623
624 // Put into pool.
625 if (! as_conn_pool_push_head(pool, sock)) {
626 as_node_close_connection(node, sock, pool);
627 }
628}
629
630/**
631 * @private
632 * Balance sync connections.
633 */
634void
636
637/**
638 * @private
639 * Are hosts equal.
640 */
641static inline bool
643{
644 return strcmp(h1->name, h2->name) == 0 && h1->port == h2->port;
645}
646
647/**
648 * @private
649 * Destroy node_info contents.
650 */
651static inline void
653{
654 as_socket_close(&node_info->socket);
655 cf_free(node_info->session);
656}
657
658/**
659 * @private
660 * Tell tend thread to perform another node login.
661 */
662void
664
665/**
666 * @private
667 * Does node contain rack.
668 */
669bool
670as_node_has_rack(as_node* node, const char* ns, int rack_id);
671
672/**
673 * @private
674 * Record latency of type latency_type for node
675 */
676void
677as_node_add_latency(as_node* node, as_latency_type latency_type, uint64_t elapsed);
678
679struct as_metrics_policy_s;
680
681/**
682 * @private
683 * Enable metrics at the node level
684 */
685void
686as_node_enable_metrics(as_node* node, const struct as_metrics_policy_s* policy);
687
688/**
689 * Return transaction error count. The value is cumulative and not reset per metrics interval.
690 */
691static inline uint64_t
693{
694 return as_load_uint64(&node->error_count);
695}
696
697/**
698 * Increment transaction error count. If the error is retryable, multiple errors per
699 * transaction may occur.
700 */
701static inline void
706
707/**
708 * Return transaction timeout count. The value is cumulative and not reset per metrics interval.
709 */
710static inline uint64_t
715
716/**
717 * Increment transaction timeout count. If the timeout is retryable (ie socketTimeout),
718 * multiple timeouts per transaction may occur.
719 */
720static inline void
725
726/**
727 * @private
728 * Volatile read session pointer.
729 */
730static inline as_session*
732{
733 return (as_session*)as_load_ptr((void* const*)session);
734}
735
736/**
737 * @private
738 * Release existing session.
739 */
740static inline void
742{
743 if (as_aaf_uint32_rls(&session->ref_count, -1) == 0) {
744 cf_free(session);
745 }
746}
747
748#ifdef __cplusplus
749} // end extern "C"
750#endif
#define AS_IP_ADDRESS_SIZE
Definition as_address.h:32
#define as_fence_acq()
#define as_load_ptr(_target)
#define as_incr_uint64(_target)
#define as_incr_uint32(_target)
#define as_store_ptr_rls(_target, _value)
#define as_store_uint8_rls(_target, _value)
#define as_load_uint8_acq(_target)
#define as_load_uint64(_target)
#define as_aaf_uint32_rls(_target, _value)
static bool as_conn_pool_push_head(as_conn_pool *pool, as_socket *sock)
static void as_conn_pool_decr(as_conn_pool *pool)
uint8_t as_latency_type
Definition as_latency.h:29
AS_EXTERN void as_node_destroy(as_node *node)
static void as_node_deactivate(as_node *node)
Definition as_node.h:486
static void as_node_info_destroy(as_node_info *node_info)
Definition as_node.h:652
static uint64_t as_node_get_error_count(as_node *node)
Definition as_node.h:692
void as_node_balance_connections(as_node *node)
static as_session * as_session_load(as_session **session)
Definition as_node.h:731
void as_node_create_min_connections(as_node *node)
#define AS_HOSTNAME_SIZE
Definition as_node.h:46
bool as_node_has_rack(as_node *node, const char *ns, int rack_id)
static void as_session_release(as_session *session)
Definition as_node.h:741
static as_address * as_node_get_address(as_node *node)
Definition as_node.h:560
static void as_node_close_connection(as_node *node, as_socket *sock, as_conn_pool *pool)
Definition as_node.h:593
as_node * as_node_create(struct as_cluster_s *cluster, as_node_info *node_info)
static bool as_host_equals(as_host *h1, as_host *h2)
Definition as_node.h:642
static void as_node_store(as_node **trg, as_node *src)
Definition as_node.h:517
void as_node_destroy_metrics(as_node *node)
as_status as_node_get_connection(as_error *err, as_node *node, uint32_t socket_timeout, uint64_t deadline_ms, as_socket *sock)
static bool as_node_is_active(const as_node *node)
Definition as_node.h:476
as_status as_node_authenticate_connection(struct as_cluster_s *cluster, uint64_t deadline_ms)
static const char * as_node_get_address_string(as_node *node)
Definition as_node.h:569
void as_node_add_address(as_node *node, struct sockaddr *addr)
static void as_node_close_socket(as_node *node, as_socket *sock)
Definition as_node.h:605
static as_node * as_node_load(as_node **node)
Definition as_node.h:497
#define AS_NODE_NAME_SIZE
Definition as_node.h:51
void as_node_release_delayed(as_node *node)
void as_node_enable_metrics(as_node *node, const struct as_metrics_policy_s *policy)
void as_node_add_latency(as_node *node, as_latency_type latency_type, uint64_t elapsed)
static void as_node_add_error(as_node *node)
Definition as_node.h:702
static void as_node_put_connection(as_node *node, as_socket *sock)
Definition as_node.h:616
static void as_node_reserve(as_node *node)
Definition as_node.h:507
void as_node_add_alias(as_node *node, const char *hostname, uint16_t port)
static void as_node_add_timeout(as_node *node)
Definition as_node.h:721
void as_node_signal_login(as_node *node)
static uint64_t as_node_get_timeout_count(as_node *node)
Definition as_node.h:711
static void as_node_release(as_node *node)
Definition as_node.h:527
#define AS_MAX_NAMESPACE_SIZE
void as_socket_close(as_socket *sock)
as_status
Definition as_status.h:30
#define AS_EXTERN
Definition as_std.h:25
char name[AS_IP_ADDRESS_SIZE]
Definition as_node.h:80
uint16_t port
Definition as_node.h:97
uint32_t min_size
Definition as_node.h:190
uint32_t opened
Definition as_node.h:200
uint32_t closed
Definition as_node.h:205
char * name
Definition as_host.h:37
uint16_t port
Definition as_host.h:47
as_host host
Definition as_node.h:420
as_session * session
Definition as_node.h:435
as_socket socket
Definition as_node.h:425
uint32_t features
Definition as_node.h:415
as_latency_buckets * latency
Definition as_node.h:213
as_session * session
Definition as_node.h:301
uint64_t error_count
Definition as_node.h:322
uint32_t peers_count
Definition as_node.h:358
bool rebalance_changed
Definition as_node.h:398
as_conn_pool * sync_conn_pools
Definition as_node.h:285
uint32_t ref_count
Definition as_node.h:225
as_node_metrics * metrics
Definition as_node.h:311
uint32_t address4_size
Definition as_node.h:260
uint64_t timeout_count
Definition as_node.h:328
struct as_cluster_s * cluster
Definition as_node.h:280
uint32_t partition_generation
Definition as_node.h:235
uint32_t failures
Definition as_node.h:373
uint32_t peers_generation
Definition as_node.h:353
uint32_t address_index
Definition as_node.h:255
as_address * addresses
Definition as_node.h:270
uint32_t friends
Definition as_node.h:368
as_async_conn_pool * pipe_conn_pools
Definition as_node.h:296
uint32_t sync_conns_opened
Definition as_node.h:338
as_vector aliases
Definition as_node.h:275
uint32_t index
Definition as_node.h:378
uint32_t conn_iter
Definition as_node.h:333
uint32_t sync_conns_closed
Definition as_node.h:343
as_async_conn_pool * async_conn_pools
Definition as_node.h:291
char * tls_name
Definition as_node.h:245
uint8_t active
Definition as_node.h:388
uint8_t perform_login
Definition as_node.h:383
uint32_t address6_size
Definition as_node.h:265
uint32_t features
Definition as_node.h:240
uint32_t error_rate
Definition as_node.h:348
as_racks * racks
Definition as_node.h:306
uint32_t partition_ref_count
Definition as_node.h:230
uint32_t rebalance_generation
Definition as_node.h:363
bool partition_changed
Definition as_node.h:393
as_socket info_socket
Definition as_node.h:316
int rack_id
Definition as_node.h:114
uint32_t ref_count
Definition as_node.h:126
uint32_t pad
Definition as_node.h:141
uint32_t size
Definition as_node.h:136
int rack_id
Definition as_node.h:131
uint32_t ref_count
Definition as_node.h:158
uint32_t token_length
Definition as_node.h:163
uint64_t expiration
Definition as_node.h:168
struct as_conn_pool_s * pool
Definition as_socket.h:87
uint64_t last_used
Definition as_socket.h:88