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-2016 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_queue.h>
22 #include <aerospike/as_vector.h>
23 #include <citrusleaf/cf_queue.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 /******************************************************************************
52  * TYPES
53  *****************************************************************************/
54 
55 /**
56  * Socket address information.
57  */
58 typedef struct as_address_s {
59  /**
60  * Socket IP address.
61  */
62  struct sockaddr_in addr;
63 
64  /**
65  * Socket IP address string representation (xxx.xxx.xxx.xxx).
66  */
67  char name[INET_ADDRSTRLEN];
68 } as_address;
69 
70 struct as_cluster_s;
71 
72 /**
73  * Server node representation.
74  */
75 typedef struct as_node_s {
76  /**
77  * @private
78  * Reference count of node.
79  */
80  uint32_t ref_count;
81 
82  /**
83  * @private
84  * Server's generation count for partition management.
85  */
87 
88  /**
89  * The name of the node.
90  */
91  char name[AS_NODE_NAME_SIZE];
92 
93  /**
94  * @private
95  * Primary host address index into addresses array.
96  */
97  uint32_t address_index;
98 
99  /**
100  * @private
101  * Vector of sockaddr_in which the host is currently known by.
102  * Only used by tend thread. Not thread-safe.
103  */
104  as_vector /* <as_address> */ addresses;
105 
106  /**
107  * @private
108  * Vector of aliases which the host is currently known by.
109  * Only used by tend thread. Not thread-safe.
110  */
111  as_vector /* <as_host> */ aliases;
112 
113  struct as_cluster_s* cluster;
114 
115  /**
116  * @private
117  * Pool of current, cached FDs.
118  */
119  cf_queue* conn_q;
120 
121  /**
122  * @private
123  * Array of connection pools used in async commands. There is one pool per node/event loop.
124  * Only used by event loop threads. Not thread-safe.
125  */
127 
128  /**
129  * @private
130  * Pool of connections used in pipelined async commands. Also not thread-safe.
131  */
133 
134  /**
135  * @private
136  * Socket used exclusively for cluster tend thread info requests.
137  */
138  int info_fd;
139 
140  /**
141  * @private
142  * Number of other nodes that consider this node a member of the cluster.
143  */
144  uint32_t conn_count;
145 
146  /**
147  * @private
148  * Number of other nodes that consider this node a member of the cluster.
149  */
150  uint32_t friends;
151 
152  /**
153  * @private
154  * Number of consecutive info request failures.
155  */
156  uint32_t failures;
157 
158  /**
159  * @private
160  * Shared memory node array index.
161  */
162  uint32_t index;
163 
164  /**
165  * @private
166  * Is node currently active.
167  */
168  uint8_t active;
169 
170  /**
171  * @private
172  * Does node support batch-index protocol?
173  */
175 
176  /**
177  * @private
178  * Does node support replicas-all info protocol?
179  */
181 
182  /**
183  * @private
184  * Does node support floating point type?
185  */
186  uint8_t has_double;
187 
188  /**
189  * @private
190  * Does node support geospatial queries?
191  */
192  uint8_t has_geo;
193 
194  /**
195  * @private
196  * Does node support pipelining?
197  */
198  uint8_t has_pipelining;
199 
200 } as_node;
201 
202 /**
203  * @private
204  * Node discovery information.
205  */
206 typedef struct as_node_info_s {
207  /**
208  * @private
209  * Node name.
210  */
211  char name[AS_NODE_NAME_SIZE];
212 
213  /**
214  * @private
215  * Validated socket.
216  */
217  int fd;
218 
219  /**
220  * @private
221  * Does node support batch-index protocol?
222  */
224 
225  /**
226  * @private
227  * Does node support replicas-all info protocol?
228  */
230 
231  /**
232  * @private
233  * Does node support floating point type?
234  */
235  uint8_t has_double;
236 
237  /**
238  * @private
239  * Does node support geospatial queries?
240  */
241  uint8_t has_geo;
242 
243  /**
244  * @private
245  * Does node support pipelining?
246  */
247  uint8_t has_pipelining;
248 
249 } as_node_info;
250 
251 /**
252  * @private
253  * Friend host address information.
254  */
255 typedef struct as_host_s {
256  /**
257  * @private
258  * Hostname or IP address string representation (xxx.xxx.xxx.xxx).
259  */
260  char name[AS_HOSTNAME_SIZE];
261 
262  /**
263  * @private
264  * Socket IP port.
265  */
266  in_port_t port;
267 
268 } as_host;
269 
270 /******************************************************************************
271  * FUNCTIONS
272  ******************************************************************************/
273 
274 /**
275  * @private
276  * Create new cluster node.
277  */
278 as_node*
279 as_node_create(struct as_cluster_s* cluster, as_host* host, struct sockaddr_in* addr, as_node_info* node_info);
280 
281 /**
282  * @private
283  * Close all connections in pool and free resources.
284  */
285 void
286 as_node_destroy(as_node* node);
287 
288 /**
289  * @private
290  * Set node to inactive.
291  */
292 static inline void
294 {
295  // Make volatile write so changes are reflected in other threads.
296  ck_pr_store_8(&node->active, false);
297 }
298 
299 /**
300  * @private
301  * Reserve existing cluster node.
302  */
303 static inline void
305 {
306  //ck_pr_fence_acquire();
307  ck_pr_inc_32(&node->ref_count);
308 }
309 
310 /**
311  * @private
312  * Release existing cluster node.
313  */
314 static inline void
316 {
317  //ck_pr_fence_release();
318 
319  bool destroy;
320  ck_pr_dec_32_zero(&node->ref_count, &destroy);
321 
322  if (destroy) {
323  as_node_destroy(node);
324  }
325 }
326 
327 /**
328  * @private
329  * Add socket address to node addresses.
330  */
331 void
332 as_node_add_address(as_node* node, as_host* host, struct sockaddr_in* addr);
333 
334 /**
335  * @private
336  * Get socket address and name.
337  */
338 static inline struct sockaddr_in*
340 {
341  as_address* address = (as_address *)as_vector_get(&node->addresses, node->address_index);
342  return &address->addr;
343 }
344 
345 /**
346  * Get socket address and name.
347  */
348 static inline as_address*
350 {
351  return (as_address *)as_vector_get(&node->addresses, node->address_index);
352 }
353 
354 /**
355  * @private
356  * Get a connection to the given node from pool and validate. Return 0 on success.
357  */
358 as_status
359 as_node_get_connection(as_error* err, as_node* node, uint64_t deadline_ms, int* fd);
360 
361 /**
362  * @private
363  * Close a node's connection and do not put back into pool.
364  */
365 static inline void
367  close(fd);
368  ck_pr_dec_32(&node->conn_count);
369 }
370 
371 /**
372  * @private
373  * Put connection back into pool.
374  */
375 static inline void
377 {
378  if (cf_queue_push(node->conn_q, &fd) != CF_QUEUE_OK) {
379  as_node_close_connection(node, fd);
380  }
381 }
382 
383 /**
384  * @private
385  * Are hosts equal.
386  */
387 static inline bool
389 {
390  return strcmp(h1->name, h2->name) == 0 && h1->port == h2->port;
391 }
392 
393 #ifdef __cplusplus
394 } // end extern "C"
395 #endif
static void as_node_deactivate(as_node *node)
Definition: as_node.h:293
struct sockaddr_in addr
Definition: as_node.h:62
uint8_t active
Definition: as_node.h:168
#define AS_HOSTNAME_SIZE
Definition: as_node.h:41
static void as_node_put_connection(as_node *node, int fd)
Definition: as_node.h:376
as_vector addresses
Definition: as_node.h:104
void as_node_destroy(as_node *node)
as_status
Definition: as_status.h:30
static void as_node_close_connection(as_node *node, int fd)
Definition: as_node.h:366
struct as_cluster_s * cluster
Definition: as_node.h:113
uint8_t has_replicas_all
Definition: as_node.h:229
uint8_t has_geo
Definition: as_node.h:241
void as_node_add_address(as_node *node, as_host *host, struct sockaddr_in *addr)
in_port_t port
Definition: as_node.h:266
uint8_t has_batch_index
Definition: as_node.h:223
uint32_t address_index
Definition: as_node.h:97
as_status as_node_get_connection(as_error *err, as_node *node, uint64_t deadline_ms, int *fd)
static void * as_vector_get(as_vector *vector, uint32_t index)
Definition: as_vector.h:113
as_queue * async_conn_qs
Definition: as_node.h:126
as_vector aliases
Definition: as_node.h:111
static bool as_host_equals(as_host *h1, as_host *h2)
Definition: as_node.h:388
#define AS_NODE_NAME_SIZE
Definition: as_node.h:46
uint32_t conn_count
Definition: as_node.h:144
uint32_t index
Definition: as_node.h:162
uint32_t ref_count
Definition: as_node.h:80
static struct sockaddr_in * as_node_get_address(as_node *node)
Definition: as_node.h:339
as_node * as_node_create(struct as_cluster_s *cluster, as_host *host, struct sockaddr_in *addr, as_node_info *node_info)
uint32_t failures
Definition: as_node.h:156
cf_queue * conn_q
Definition: as_node.h:119
uint32_t partition_generation
Definition: as_node.h:86
uint8_t has_batch_index
Definition: as_node.h:174
uint8_t has_replicas_all
Definition: as_node.h:180
char name[AS_HOSTNAME_SIZE]
Definition: as_node.h:260
uint8_t has_geo
Definition: as_node.h:192
static void as_node_release(as_node *node)
Definition: as_node.h:315
static as_address * as_node_get_address_full(as_node *node)
Definition: as_node.h:349
uint32_t friends
Definition: as_node.h:150
uint8_t has_double
Definition: as_node.h:235
uint8_t has_pipelining
Definition: as_node.h:198
int info_fd
Definition: as_node.h:138
as_queue * pipe_conn_qs
Definition: as_node.h:132
uint8_t has_pipelining
Definition: as_node.h:247
static void as_node_reserve(as_node *node)
Definition: as_node.h:304
uint8_t has_double
Definition: as_node.h:186