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-2014 by Aerospike.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  *****************************************************************************/
22 #pragma once
23 
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_MAX_SIZE 20
37 
38 /******************************************************************************
39  * TYPES
40  *****************************************************************************/
41 
42 /**
43  * Socket address information.
44  */
45 typedef struct as_address_s {
46  /**
47  * Socket IP address.
48  */
49  struct sockaddr_in addr;
50 
51  /**
52  * Socket IP address string representation (xxx.xxx.xxx.xxx).
53  */
54  char name[INET_ADDRSTRLEN];
55 } as_address;
56 
57 struct as_cluster_s;
58 
59 /**
60  * Server node representation.
61  */
62 typedef struct as_node_s {
63  /**
64  * @private
65  * Reference count of node.
66  */
67  uint32_t ref_count;
68 
69  /**
70  * @private
71  * Server's generation count for partition management.
72  */
74 
75  /**
76  * The name of the node.
77  */
79 
80  /**
81  * @private
82  * Primary host address index into addresses array.
83  */
84  uint32_t address_index;
85 
86  /**
87  * @private
88  * Vector of sockaddr_in which the host is currently known by.
89  * Only used by tend thread. Not thread-safe.
90  */
91  as_vector /* <as_address> */ addresses;
92 
93  struct as_cluster_s* cluster;
94 
95  /**
96  * @private
97  * Pool of current, cached FDs.
98  */
99  cf_queue* conn_q;
100 
101  /**
102  * @private
103  * FDs for async command execution. Not currently used.
104  */
105  // cf_queue* conn_q_asyncfd;
106 
107  /**
108  * @private
109  * Asynchronous work queue. Not currently used.
110  */
111  // cf_queue* asyncwork_q;
112 
113  /**
114  * @private
115  * Number of other nodes that consider this node a member of the cluster.
116  */
117  uint32_t friends;
118 
119  /**
120  * @private
121  * Number of consecutive info request failures.
122  */
123  uint32_t failures;
124 
125  /**
126  * @private
127  * Is node currently active.
128  */
129  uint8_t active;
130 } as_node;
131 
132 /**
133  * @private
134  * Friend host address information.
135  */
136 typedef struct as_friend_s {
137  /**
138  * @private
139  * Socket IP address string representation (xxx.xxx.xxx.xxx).
140  */
141  char name[INET_ADDRSTRLEN];
142 
143  /**
144  * @private
145  * Socket IP address.
146  */
147  in_addr_t addr;
148 
149  /**
150  * @private
151  * Socket IP port.
152  */
153  in_port_t port;
154 } as_friend;
155 
156 /******************************************************************************
157  * FUNCTIONS
158  ******************************************************************************/
159 
160 /**
161  * @private
162  * Create new cluster node.
163  */
164 as_node*
165 as_node_create(struct as_cluster_s* cluster, const char* name, struct sockaddr_in* addr);
166 
167 /**
168  * @private
169  * Close all connections in pool and free resources.
170  */
171 void
172 as_node_destroy(as_node* node);
173 
174 /**
175  * @private
176  * Set node to inactive.
177  */
178 static inline void
180 {
181  // Make volatile write so changes are reflected in other threads.
182  ck_pr_store_8(&node->active, false);
183 }
184 
185 /**
186  * @private
187  * Reserve existing cluster node.
188  */
189 static inline void
191 {
192  //ck_pr_fence_acquire();
193  ck_pr_inc_32(&node->ref_count);
194 }
195 
196 /**
197  * @private
198  * Release existing cluster node.
199  */
200 static inline void
202 {
203  //ck_pr_fence_release();
204 
205  bool destroy;
206  ck_pr_dec_32_zero(&node->ref_count, &destroy);
207 
208  if (destroy) {
209  as_node_destroy(node);
210  }
211 }
212 
213 /**
214  * @private
215  * Add socket address to node addresses.
216  */
217 void
218 as_node_add_address(as_node* node, struct sockaddr_in* addr);
219 
220 /**
221  * @private
222  * Get socket address and name.
223  */
224 static inline struct sockaddr_in*
226 {
227  as_address* address = as_vector_get(&node->addresses, node->address_index);
228  return &address->addr;
229 }
230 
231 /**
232  * Get socket address and name.
233  */
234 static inline as_address*
236 {
237  return as_vector_get(&node->addresses, node->address_index);
238 }
239 
240 /**
241  * @private
242  * Get a connection to the given node from pool. Return fd on success and -1 on error.
243  */
244 int
245 as_node_fd_get(as_node* node);
246 
247 /**
248  * @private
249  * Put connection back into pool.
250  */
251 void
252 as_node_fd_put(as_node* node, int fd);
in_addr_t addr
Definition: as_node.h:147
int as_node_fd_get(as_node *node)
static void as_node_deactivate(as_node *node)
Definition: as_node.h:179
struct sockaddr_in addr
Definition: as_node.h:49
uint8_t active
Definition: as_node.h:129
in_port_t port
Definition: as_node.h:153
as_vector addresses
Definition: as_node.h:91
void as_node_destroy(as_node *node)
struct as_cluster_s * cluster
Definition: as_node.h:93
uint32_t address_index
Definition: as_node.h:84
static void * as_vector_get(as_vector *vector, uint32_t index)
Definition: as_vector.h:116
#define AS_NODE_NAME_MAX_SIZE
Definition: as_node.h:36
uint32_t ref_count
Definition: as_node.h:67
static struct sockaddr_in * as_node_get_address(as_node *node)
Definition: as_node.h:225
uint32_t failures
Definition: as_node.h:123
cf_queue * conn_q
Definition: as_node.h:99
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:73
void as_node_add_address(as_node *node, struct sockaddr_in *addr)
static void as_node_release(as_node *node)
Definition: as_node.h:201
static as_address * as_node_get_address_full(as_node *node)
Definition: as_node.h:235
void as_node_fd_put(as_node *node, int fd)
uint32_t friends
Definition: as_node.h:117
static void as_node_reserve(as_node *node)
Definition: as_node.h:190