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