All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
as_cluster.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_config.h>
24 #include <aerospike/as_node.h>
25 #include <aerospike/as_partition.h>
26 #include <aerospike/as_policy.h>
27 #include <aerospike/as_thread_pool.h>
28 #include <citrusleaf/cf_atomic.h>
29 #include "ck_pr.h"
30 
31 /******************************************************************************
32  * TYPES
33  *****************************************************************************/
34 
35 /**
36  * Seed host.
37  */
38 typedef struct as_seed_s {
39  /**
40  * Host name.
41  */
42  char* name;
43 
44  /**
45  * Host port.
46  */
47  in_port_t port;
48 } as_seed;
49 
50 /**
51  * @private
52  * Reference counted array of server node pointers.
53  */
54 typedef struct as_nodes_s {
55  /**
56  * @private
57  * Reference count of node array.
58  */
59  uint32_t ref_count;
60 
61  /**
62  * @private
63  * Length of node array.
64  */
65  uint32_t size;
66 
67  /**
68  * @private
69  * Server node array.
70  */
71  as_node* array[];
72 } as_nodes;
73 
74 /**
75  * @private
76  * Reference counted release function definition.
77  */
78 typedef void (*as_release_fn) (void* value);
79 
80 /**
81  * @private
82  * Reference counted data to be garbage collected.
83  */
84 typedef struct as_gc_item_s {
85  /**
86  * @private
87  * Reference counted data to be garbage collected.
88  */
89  void* data;
90 
91  /**
92  * @private
93  * Release function.
94  */
96 } as_gc_item;
97 
98 /**
99  * Cluster of server nodes.
100  */
101 typedef struct as_cluster_s {
102  /**
103  * @private
104  * Active nodes in cluster.
105  */
107 
108  /**
109  * @private
110  * Hints for best node for a partition.
111  */
113 
114  /**
115  * @private
116  * Nodes to be garbage collected.
117  */
118  as_vector* /* <as_gc_item> */ gc;
119 
120  /**
121  * @private
122  * Shared memory implementation of cluster.
123  */
124  struct as_shm_info_s* shm_info;
125 
126  /**
127  * @private
128  * User name in UTF-8 encoded bytes.
129  */
130  char* user;
131 
132  /**
133  * @private
134  * Password in hashed format in bytes.
135  */
136  char* password;
137 
138  /**
139  * @private
140  * Initial seed nodes specified by user.
141  */
143 
144  /**
145  * @private
146  * Length of seeds array.
147  */
148  uint32_t seeds_size;
149 
150  /**
151  * @private
152  * Length of ip_map array.
153  */
154  uint32_t ip_map_size;
155 
156  /**
157  * @private
158  * A IP translation table is used in cases where different clients use different server
159  * IP addresses. This may be necessary when using clients from both inside and outside
160  * a local area network. Default is no translation.
161  *
162  * The key is the IP address returned from friend info requests to other servers. The
163  * value is the real IP address used to connect to the server.
164  */
166 
167  /**
168  * @private
169  * Pool of threads used to query server nodes in parallel for batch, scan and query.
170  */
171  as_thread_pool thread_pool;
172 
173  /**
174  * @private
175  * Lock for the tend thread to wait on with the tend interval as timeout.
176  * Normally locked, resulting in waiting a full interval between
177  * tend iterations. Upon cluster shutdown, unlocked by the main
178  * thread, allowing a fast termination of the tend thread.
179  */
180  pthread_mutex_t tend_lock;
181 
182  /**
183  * @private
184  * Tend thread identifier to be used with tend_lock.
185  */
186  pthread_cond_t tend_cond;
187 
188  /**
189  * @private
190  * Milliseconds between cluster tends.
191  */
192  uint32_t tend_interval;
193 
194  /**
195  * @private
196  * Size of node's synchronous connection pool.
197  */
198  uint32_t conn_queue_size;
199 
200  /**
201  * @private
202  * Initial connection timeout in milliseconds.
203  */
204  uint32_t conn_timeout_ms;
205 
206  /**
207  * @private
208  * Maximum socket idle in seconds.
209  */
210  uint32_t max_socket_idle;
211 
212  /**
213  * @private
214  * Random node index.
215  */
216  uint32_t node_index;
217 
218  /**
219  * @private
220  * Total number of data partitions used by cluster.
221  */
222  cl_partition_id n_partitions;
223 
224  /**
225  * @private
226  * Should continue to tend cluster.
227  */
228  volatile bool valid;
229 
230  /**
231  * @private
232  * Cluster tend thread.
233  */
234  pthread_t tend_thread;
235 } as_cluster;
236 
237 /******************************************************************************
238  * FUNCTIONS
239  ******************************************************************************/
240 
241 /**
242  * Create and initialize cluster.
243  */
244 as_status
245 as_cluster_create(as_config* config, as_error* err, as_cluster** cluster);
246 
247 /**
248  * Close all connections and release memory associated with cluster.
249  */
250 void
252 
253 /**
254  * Is cluster connected to any server nodes.
255  */
256 bool
258 
259 /**
260  * Get all node names in cluster.
261  */
262 void
263 as_cluster_get_node_names(as_cluster* cluster, int* n_nodes, char** node_names);
264 
265 /**
266  * Reserve reference counted access to cluster nodes.
267  */
268 static inline as_nodes*
270 {
271  as_nodes* nodes = (as_nodes *)ck_pr_load_ptr(&cluster->nodes);
272  //ck_pr_fence_acquire();
273  ck_pr_inc_32(&nodes->ref_count);
274  return nodes;
275 }
276 
277 /**
278  * Release reference counted access to cluster nodes.
279  */
280 static inline void
282 {
283  //ck_pr_fence_release();
284 
285  bool destroy;
286  ck_pr_dec_32_zero(&nodes->ref_count, &destroy);
287 
288  if (destroy) {
289  cf_free(nodes);
290  }
291 }
292 
293 /**
294  * @private
295  * Change user and password that is used to authenticate with cluster servers.
296  */
297 void
298 as_cluster_change_password(as_cluster* cluster, const char* user, const char* password);
299 
300 /**
301  * @private
302  * Get random node in the cluster.
303  * as_nodes_release() must be called when done with node.
304  */
305 as_node*
307 
308 /**
309  * @private
310  * Get node given node name.
311  * as_nodes_release() must be called when done with node.
312  */
313 as_node*
314 as_node_get_by_name(as_cluster* cluster, const char* name);
315 
316 /**
317  * @private
318  * Reserve reference counted access to partition tables.
319  * as_partition_tables_release() must be called when done with tables.
320  */
321 static inline as_partition_tables*
323 {
324  as_partition_tables* tables = (as_partition_tables *)ck_pr_load_ptr(&cluster->partition_tables);
325  ck_pr_inc_32(&tables->ref_count);
326  return tables;
327 }
328 
329 /**
330  * @private
331  * Release reference counted access to partition tables.
332  */
333 static inline void
335 {
336  bool destroy;
337  ck_pr_dec_32_zero(&tables->ref_count, &destroy);
338 
339  if (destroy) {
340  cf_free(tables);
341  }
342 }
343 
344 /**
345  * @private
346  * Get partition table given namespace.
347  */
348 static inline as_partition_table*
350 {
351  // Partition tables array size does not currently change after first cluster tend.
352  // Also, there is a one second delayed garbage collection coupled with as_partition_tables_get()
353  // being very fast. Reference counting the tables array is not currently necessary, but do it
354  // anyway in case the server starts supporting dynamic namespaces.
356  as_partition_table* table = as_partition_tables_get(tables, ns);
358  return table;
359 }
360 
361 /**
362  * @private
363  * Get mapped node given digest key and partition table. If there is no mapped node, a random
364  * node is used instead.
365  * as_nodes_release() must be called when done with node.
366  */
367 as_node*
368 as_partition_table_get_node(as_cluster* cluster, as_partition_table* table, const uint8_t* digest, bool write, as_policy_replica replica);
369 
370 /**
371  * @private
372  * Get shared memory mapped node given digest key. If there is no mapped node, a random node is used instead.
373  * as_nodes_release() must be called when done with node.
374  */
375 as_node*
376 as_shm_node_get(as_cluster* cluster, const char* ns, const uint8_t* digest, bool write, as_policy_replica replica);
377 
378 /**
379  * @private
380  * Get mapped node given digest key. If there is no mapped node, a random node is used instead.
381  * as_nodes_release() must be called when done with node.
382  */
383 static inline as_node*
384 as_node_get(as_cluster* cluster, const char* ns, const uint8_t* digest, bool write, as_policy_replica replica)
385 {
386  if (cluster->shm_info) {
387  return as_shm_node_get(cluster, ns, digest, write, replica);
388  }
389  else {
391  return as_partition_table_get_node(cluster, table, digest, write, replica);
392  }
393 }
394 
395 #ifdef __cplusplus
396 } // end extern "C"
397 #endif
as_thread_pool thread_pool
Definition: as_cluster.h:171
pthread_mutex_t tend_lock
Definition: as_cluster.h:180
uint32_t ref_count
Definition: as_cluster.h:59
as_namespace ns
Definition: as_scan.h:345
as_policy_replica
Definition: as_policy.h:257
as_nodes * nodes
Definition: as_cluster.h:106
as_seed * seeds
Definition: as_cluster.h:142
as_status
Definition: as_status.h:30
static void as_partition_tables_release(as_partition_tables *tables)
Definition: as_cluster.h:334
void(* as_release_fn)(void *value)
Definition: as_cluster.h:78
void as_cluster_change_password(as_cluster *cluster, const char *user, const char *password)
as_status as_cluster_create(as_config *config, as_error *err, as_cluster **cluster)
void * data
Definition: as_cluster.h:89
static as_partition_tables * as_partition_tables_reserve(as_cluster *cluster)
Definition: as_cluster.h:322
as_partition_tables * partition_tables
Definition: as_cluster.h:112
static as_node * as_node_get(as_cluster *cluster, const char *ns, const uint8_t *digest, bool write, as_policy_replica replica)
Definition: as_cluster.h:384
in_port_t port
Definition: as_cluster.h:47
uint32_t conn_queue_size
Definition: as_cluster.h:198
uint32_t conn_timeout_ms
Definition: as_cluster.h:204
char * password
Definition: as_cluster.h:136
as_node * as_shm_node_get(as_cluster *cluster, const char *ns, const uint8_t *digest, bool write, as_policy_replica replica)
as_release_fn release_fn
Definition: as_cluster.h:95
pthread_t tend_thread
Definition: as_cluster.h:234
uint32_t max_socket_idle
Definition: as_cluster.h:210
void as_cluster_get_node_names(as_cluster *cluster, int *n_nodes, char **node_names)
char * user
Definition: as_cluster.h:130
uint32_t node_index
Definition: as_cluster.h:216
pthread_cond_t tend_cond
Definition: as_cluster.h:186
uint32_t tend_interval
Definition: as_cluster.h:192
cl_partition_id n_partitions
Definition: as_cluster.h:222
static as_partition_table * as_cluster_get_partition_table(as_cluster *cluster, const char *ns)
Definition: as_cluster.h:349
uint32_t size
Definition: as_cluster.h:65
struct as_shm_info_s * shm_info
Definition: as_cluster.h:124
volatile bool valid
Definition: as_cluster.h:228
as_node * as_partition_table_get_node(as_cluster *cluster, as_partition_table *table, const uint8_t *digest, bool write, as_policy_replica replica)
void as_cluster_destroy(as_cluster *cluster)
as_addr_map * ip_map
Definition: as_cluster.h:165
as_node * as_node_get_by_name(as_cluster *cluster, const char *name)
as_vector * gc
Definition: as_cluster.h:118
as_partition_table * as_partition_tables_get(as_partition_tables *tables, const char *ns)
uint32_t ip_map_size
Definition: as_cluster.h:154
static as_nodes * as_nodes_reserve(as_cluster *cluster)
Definition: as_cluster.h:269
char * name
Definition: as_cluster.h:42
bool as_cluster_is_connected(as_cluster *cluster)
uint32_t seeds_size
Definition: as_cluster.h:148
static void as_nodes_release(as_nodes *nodes)
Definition: as_cluster.h:281
as_node * as_node_get_random(as_cluster *cluster)