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