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