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