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