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 } as_node;
160 
161 /**
162  * @private
163  * Node discovery information.
164  */
165 typedef struct as_node_info_s {
166  /**
167  * @private
168  * Node name.
169  */
170  char name[AS_NODE_NAME_SIZE];
171 
172  /**
173  * @private
174  * Does node support batch-index protocol?
175  */
177 
178  /**
179  * @private
180  * Does node support replicas-all info protocol?
181  */
183 } as_node_info;
184 
185 /**
186  * @private
187  * Friend host address information.
188  */
189 typedef struct as_friend_s {
190  /**
191  * @private
192  * Socket IP address string representation (xxx.xxx.xxx.xxx).
193  */
194  char name[INET_ADDRSTRLEN];
195 
196  /**
197  * @private
198  * Socket IP address.
199  */
200  in_addr_t addr;
201 
202  /**
203  * @private
204  * Socket IP port.
205  */
206  in_port_t port;
207 } as_friend;
208 
209 /******************************************************************************
210  * FUNCTIONS
211  ******************************************************************************/
212 
213 /**
214  * @private
215  * Create new cluster node.
216  */
217 as_node*
218 as_node_create(struct as_cluster_s* cluster, struct sockaddr_in* addr, as_node_info* node_info);
219 
220 /**
221  * @private
222  * Close all connections in pool and free resources.
223  */
224 void
225 as_node_destroy(as_node* node);
226 
227 /**
228  * @private
229  * Set node to inactive.
230  */
231 static inline void
233 {
234  // Make volatile write so changes are reflected in other threads.
235  ck_pr_store_8(&node->active, false);
236 }
237 
238 /**
239  * @private
240  * Reserve existing cluster node.
241  */
242 static inline void
244 {
245  //ck_pr_fence_acquire();
246  ck_pr_inc_32(&node->ref_count);
247 }
248 
249 /**
250  * @private
251  * Release existing cluster node.
252  */
253 static inline void
255 {
256  //ck_pr_fence_release();
257 
258  bool destroy;
259  ck_pr_dec_32_zero(&node->ref_count, &destroy);
260 
261  if (destroy) {
262  as_node_destroy(node);
263  }
264 }
265 
266 /**
267  * @private
268  * Add socket address to node addresses.
269  */
270 void
271 as_node_add_address(as_node* node, struct sockaddr_in* addr);
272 
273 /**
274  * @private
275  * Get socket address and name.
276  */
277 static inline struct sockaddr_in*
279 {
280  as_address* address = (as_address *)as_vector_get(&node->addresses, node->address_index);
281  return &address->addr;
282 }
283 
284 /**
285  * Get socket address and name.
286  */
287 static inline as_address*
289 {
290  return (as_address *)as_vector_get(&node->addresses, node->address_index);
291 }
292 
293 /**
294  * @private
295  * Get a connection to the given node from pool and validate. Return 0 on success.
296  */
297 as_status
298 as_node_get_connection(as_error* err, as_node* node, int* fd);
299 
300 /**
301  * @private
302  * Put connection back into pool if pool size < limit. Otherwise, close connection.
303  */
304 static inline void
305 as_node_put_connection(as_node* node, int fd, uint32_t limit)
306 {
307  if (! cf_queue_push_limit(node->conn_q, &fd, limit)) {
308  close(fd);
309  }
310 }
311 
312 #ifdef __cplusplus
313 } // end extern "C"
314 #endif
in_addr_t addr
Definition: as_node.h:200
static void as_node_deactivate(as_node *node)
Definition: as_node.h:232
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:206
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:182
uint8_t has_batch_index
Definition: as_node.h:176
uint32_t address_index
Definition: as_node.h:89
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:278
as_status as_node_get_connection(as_error *err, as_node *node, int *fd)
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:254
static as_address * as_node_get_address_full(as_node *node)
Definition: as_node.h:288
uint32_t friends
Definition: as_node.h:128
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:305
static void as_node_reserve(as_node *node)
Definition: as_node.h:243