All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
as_node.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2017 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_error.h>
20 #include <aerospike/as_event.h>
21 #include <aerospike/as_socket.h>
22 #include <aerospike/as_queue.h>
23 #include <aerospike/as_vector.h>
24 #include <netinet/in.h>
25 #include <sys/uio.h>
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 // Concurrency kit needs to be under extern "C" when compiling C++.
32 #include <aerospike/ck/ck_pr.h>
33 
34 /******************************************************************************
35  * MACROS
36  *****************************************************************************/
37 
38 /**
39  * Maximum size (including NULL byte) of a hostname.
40  */
41 #define AS_HOSTNAME_SIZE 256
42 
43 /**
44  * Maximum size of node name
45  */
46 #define AS_NODE_NAME_SIZE 20
47 
48 // Leave this is in for backwards compatibility.
49 #define AS_NODE_NAME_MAX_SIZE AS_NODE_NAME_SIZE
50 
51 #define AS_FEATURES_GEO (1 << 0)
52 #define AS_FEATURES_DOUBLE (1 << 1)
53 #define AS_FEATURES_BATCH_INDEX (1 << 2)
54 #define AS_FEATURES_REPLICAS_ALL (1 << 3)
55 #define AS_FEATURES_PIPELINING (1 << 4)
56 #define AS_FEATURES_PEERS (1 << 5)
57 
58 #define AS_IP_ADDRESS_SIZE 64
59 #define AS_ADDRESS4_MAX 4
60 #define AS_ADDRESS6_MAX 8
61 
62 /******************************************************************************
63  * TYPES
64  *****************************************************************************/
65 
66 /**
67  * Socket address information.
68  */
69 typedef struct as_address_s {
70  /**
71  * Socket IP address.
72  */
73  struct sockaddr_storage addr;
74 
75  /**
76  * Socket IP address string representation including port.
77  */
78  char name[AS_IP_ADDRESS_SIZE];
79 
80 } as_address;
81 
82 /**
83  * @private
84  * Host address alias information.
85  */
86 typedef struct as_alias_s {
87  /**
88  * @private
89  * Hostname or IP address string representation.
90  */
91  char name[AS_HOSTNAME_SIZE];
92 
93  /**
94  * @private
95  * Socket IP port.
96  */
97  in_port_t port;
98 
99 } as_alias;
100 
101 /**
102  * @private
103  * Queue with lock.
104  */
105 typedef struct as_queue_lock_s {
106  /**
107  * @private
108  * Mutex lock.
109  */
110  pthread_mutex_t lock;
111 
112  /**
113  * @private
114  * Queue.
115  */
117 
118 } as_queue_lock;
119 
120 
121 struct as_cluster_s;
122 
123 /**
124  * Server node representation.
125  */
126 typedef struct as_node_s {
127  /**
128  * @private
129  * Reference count of node.
130  */
131  uint32_t ref_count;
132 
133  /**
134  * @private
135  * Server's generation count for partition management.
136  */
138 
139  /**
140  * @private
141  * TLS certificate name (needed for TLS only, NULL otherwise).
142  */
143  char* tls_name;
144 
145  /**
146  * The name of the node.
147  */
148  char name[AS_NODE_NAME_SIZE];
149 
150  /**
151  * @private
152  * Primary address index into addresses array.
153  */
154  uint32_t address_index;
155 
156  /**
157  * @private
158  * Number of IPv4 addresses.
159  */
160  uint32_t address4_size;
161 
162  /**
163  * @private
164  * Number of IPv6 addresses.
165  */
166  uint32_t address6_size;
167 
168  /**
169  * @private
170  * Array of IP addresses. Not thread-safe.
171  */
173 
174  /**
175  * @private
176  * Array of hostnames aliases. Not thread-safe.
177  */
178  as_vector /* <as_alias> */ aliases;
179 
180  struct as_cluster_s* cluster;
181 
182  /**
183  * @private
184  * Pools of current, cached sockets.
185  */
187 
188  /**
189  * @private
190  * Array of connection pools used in async commands. There is one pool per node/event loop.
191  * Only used by event loop threads. Not thread-safe.
192  */
194 
195  /**
196  * @private
197  * Pool of connections used in pipelined async commands. Also not thread-safe.
198  */
200 
201  /**
202  * @private
203  * Socket used exclusively for cluster tend thread info requests.
204  */
206 
207  /**
208  * @private
209  * Features supported by server. Stored in bitmap.
210  */
211  uint32_t features;
212 
213  /**
214  * @private
215  * Connection queue iterator. Not atomic by design.
216  */
217  uint32_t conn_iter;
218 
219  /**
220  * @private
221  * Server's generation count for peers.
222  */
224 
225  /**
226  * @private
227  * Number of peers returned by server node.
228  */
229  uint32_t peers_count;
230 
231  /**
232  * @private
233  * Number of other nodes that consider this node a member of the cluster.
234  */
235  uint32_t friends;
236 
237  /**
238  * @private
239  * Number of consecutive info request failures.
240  */
241  uint32_t failures;
242 
243  /**
244  * @private
245  * Shared memory node array index.
246  */
247  uint32_t index;
248 
249  /**
250  * @private
251  * Is node currently active.
252  */
253  uint8_t active;
254 
255  /**
256  * @private
257  * Did partition change in current cluster tend.
258  */
260 
261 } as_node;
262 
263 /**
264  * @private
265  * Node discovery information.
266  */
267 typedef struct as_node_info_s {
268  /**
269  * @private
270  * Node name.
271  */
272  char name[AS_NODE_NAME_SIZE];
273 
274  /**
275  * @private
276  * Features supported by server. Stored in bitmap.
277  */
278  uint32_t features;
279 
280  /**
281  * @private
282  * Validated socket.
283  */
285 
286 } as_node_info;
287 
288 /******************************************************************************
289  * FUNCTIONS
290  ******************************************************************************/
291 
292 /**
293  * @private
294  * Create new cluster node.
295  */
296 as_node*
298  struct as_cluster_s* cluster, const char* hostname, const char* tls_name,
299  in_port_t port, bool is_alias, struct sockaddr* addr, as_node_info* node_info
300  );
301 
302 /**
303  * @private
304  * Close all connections in pool and free resources.
305  */
306 void
307 as_node_destroy(as_node* node);
308 
309 /**
310  * @private
311  * Set node to inactive.
312  */
313 static inline void
315 {
316  // Make volatile write so changes are reflected in other threads.
317  ck_pr_store_8(&node->active, false);
318 }
319 
320 /**
321  * @private
322  * Reserve existing cluster node.
323  */
324 static inline void
326 {
327  //ck_pr_fence_acquire();
328  ck_pr_inc_32(&node->ref_count);
329 }
330 
331 /**
332  * @private
333  * Release existing cluster node.
334  */
335 static inline void
337 {
338  //ck_pr_fence_release();
339 
340  bool destroy;
341  ck_pr_dec_32_zero(&node->ref_count, &destroy);
342 
343  if (destroy) {
344  as_node_destroy(node);
345  }
346 }
347 
348 /**
349  * @private
350  * Add socket address to node addresses.
351  */
352 void
353 as_node_add_address(as_node* node, struct sockaddr* addr);
354 
355 /**
356  * @private
357  * Add hostname to node aliases.
358  */
359 void
360 as_node_add_alias(as_node* node, const char* hostname, in_port_t port);
361 
362 /**
363  * Get primary socket address.
364  */
365 static inline as_address*
367 {
368  return &node->addresses[node->address_index];
369 }
370 
371 /**
372  * Get socket address as a string.
373  */
374 static inline const char*
376 {
377  return node->addresses[node->address_index].name;
378 }
379 
380 /**
381  * @private
382  * Get a connection to the given node from pool and validate. Return 0 on success.
383  */
384 as_status
385 as_node_get_connection(as_error* err, as_node* node, uint64_t deadline_ms, as_socket* sock);
386 
387 /**
388  * @private
389  * Close a node's connection and do not put back into pool.
390  */
391 static inline void
393  as_queue_lock* queue = sock->queue;
394  as_socket_close(sock);
395  pthread_mutex_lock(&queue->lock);
396  queue->queue.total--;
397  pthread_mutex_unlock(&queue->lock);
398 }
399 
400 /**
401  * @private
402  * Put connection back into pool.
403  */
404 static inline void
406 {
407  // Save queue.
408  as_queue_lock* queue = sock->queue;
409 
410  // Update last_used for TLS connections.
411  if (sock->ctx) {
412  sock->last_used = cf_get_seconds();
413  }
414 
415  // Push into queue.
416  pthread_mutex_lock(&queue->lock);
417  bool status = as_queue_push(&queue->queue, sock);
418  pthread_mutex_unlock(&queue->lock);
419 
420  if (! status) {
421  as_socket_close(sock);
422  pthread_mutex_lock(&queue->lock);
423  queue->queue.total--;
424  pthread_mutex_unlock(&queue->lock);
425  }
426 }
427 
428 /**
429  * @private
430  * Are hosts equal.
431  */
432 static inline bool
434 {
435  return strcmp(h1->name, h2->name) == 0 && h1->port == h2->port;
436 }
437 
438 #ifdef __cplusplus
439 } // end extern "C"
440 #endif
static void as_node_deactivate(as_node *node)
Definition: as_node.h:314
void as_node_add_alias(as_node *node, const char *hostname, in_port_t port)
uint8_t active
Definition: as_node.h:253
char name[AS_IP_ADDRESS_SIZE]
Definition: as_node.h:78
#define AS_HOSTNAME_SIZE
Definition: as_node.h:41
void as_node_destroy(as_node *node)
as_status
Definition: as_status.h:30
struct as_cluster_s * cluster
Definition: as_node.h:180
as_tls_context * ctx
Definition: as_socket.h:86
uint32_t address_index
Definition: as_node.h:154
as_queue * async_conn_qs
Definition: as_node.h:193
uint32_t address4_size
Definition: as_node.h:160
as_vector aliases
Definition: as_node.h:178
static bool as_host_equals(as_host *h1, as_host *h2)
Definition: as_node.h:433
uint32_t peers_generation
Definition: as_node.h:223
#define AS_NODE_NAME_SIZE
Definition: as_node.h:46
as_socket socket
Definition: as_node.h:284
char * tls_name
Definition: as_node.h:143
uint32_t index
Definition: as_node.h:247
uint32_t ref_count
Definition: as_node.h:131
void as_node_add_address(as_node *node, struct sockaddr *addr)
char * name
Definition: as_host.h:37
struct as_queue_lock_s * queue
Definition: as_socket.h:83
uint32_t features
Definition: as_node.h:278
bool partition_changed
Definition: as_node.h:259
uint32_t features
Definition: as_node.h:211
uint64_t last_used
Definition: as_socket.h:84
in_port_t port
Definition: as_node.h:97
as_queue_lock * conn_qs
Definition: as_node.h:186
uint32_t failures
Definition: as_node.h:241
as_socket info_socket
Definition: as_node.h:205
bool as_queue_push(as_queue *queue, const void *ptr)
uint32_t partition_generation
Definition: as_node.h:137
uint32_t total
Definition: as_queue.h:64
static void as_node_put_connection(as_socket *sock)
Definition: as_node.h:405
as_status as_node_get_connection(as_error *err, as_node *node, uint64_t deadline_ms, as_socket *sock)
as_node * as_node_create(struct as_cluster_s *cluster, const char *hostname, const char *tls_name, in_port_t port, bool is_alias, struct sockaddr *addr, as_node_info *node_info)
as_queue queue
Definition: as_node.h:116
uint16_t port
Definition: as_host.h:47
static void as_node_release(as_node *node)
Definition: as_node.h:336
uint32_t peers_count
Definition: as_node.h:229
static const char * as_node_get_address_string(as_node *node)
Definition: as_node.h:375
uint32_t address6_size
Definition: as_node.h:166
void as_socket_close(as_socket *sock)
pthread_mutex_t lock
Definition: as_node.h:110
uint32_t friends
Definition: as_node.h:235
uint32_t conn_iter
Definition: as_node.h:217
as_address * addresses
Definition: as_node.h:172
as_queue * pipe_conn_qs
Definition: as_node.h:199
static void as_node_close_connection(as_socket *sock)
Definition: as_node.h:392
static void as_node_reserve(as_node *node)
Definition: as_node.h:325
static as_address * as_node_get_address(as_node *node)
Definition: as_node.h:366
#define AS_IP_ADDRESS_SIZE
Definition: as_node.h:58