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