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