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