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