All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cl_cluster.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2008-2013 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 <citrusleaf/cf_atomic.h>
25 #include <citrusleaf/cf_queue.h>
26 #include <citrusleaf/cf_vector.h>
27 
28 #include <citrusleaf/cl_types.h>
29 
30 /******************************************************************************
31  * CONSTANTS
32  ******************************************************************************/
33 
34 #define MAX_INTERVALS_ABSENT 1
35 
36 #define NUM_BATCH_THREADS 6
37 #define NUM_SCAN_THREADS 5
38 #define NUM_QUERY_THREADS 5
39 
40 
41 /******************************************************************************
42  * TYPES
43  ******************************************************************************/
44 
45 typedef struct cl_cluster_s cl_cluster;
46 typedef struct cl_cluster_node_s cl_cluster_node;
47 typedef struct cl_partition_s cl_partition;
48 typedef struct cl_partition_table_s cl_partition_table;
49 
50 
53  uint32_t intervals_absent; // how many tend periods this node has been out of partitions map
54  cf_vector sockaddr_in_v; // a vector of sockaddr_in which the host is currently known by
55  uint32_t partition_generation; // the server's generation count for all its partition management
56  cf_queue* conn_q; // pool of current, cached FDs
57  cf_queue* conn_q_asyncfd; // FDs for async command execution
58  cf_queue* asyncwork_q;
59  int info_fd; // socket for internal info transactions on this node
60 };
61 
62 //Structure to hold information about compression.
64  int compression_threshold; // Minimum size of packet, to be compressed. 0 = no compression.
65  uint64_t actual_sz; // Accumulative count. Actual size of data, compressed till now.
66  uint64_t compressed_sz; // Accumulative count. Size of data after compression.
67 };
68 
69 struct cl_cluster_s {
70  // Cluster management.
71  pthread_t tender_thread;
72  cf_atomic32 tender_running;
73  uint32_t tend_speed;
74 
75  bool follow; // possible to create a no-follow cluster
76 
77  volatile bool found_all; // have, at some time, found all cluster members
78 
79  // List of host-strings added by the user.
80  cf_vector host_str_v; // vector is pointer-type
81  cf_vector host_port_v; // vector is integer-type
82 
83  cf_vector host_addr_map_v; // mapping from host string to its alternate
84 
85  // list actual node objects that represent the cluster.
86  uint32_t last_node;
87  cf_vector node_v; // vector is pointer-type, host objects are ref-counted
88 
89  // Information about where all the partitions are.
91  cl_partition_table* partition_table_head;
92 
94 
95  int info_timeout; // timeout in ms for info requests
96 
97  pthread_mutex_t LOCK;
98 
99  // Batch transaction management.
100  pthread_mutex_t batch_init_lock;
101  cf_atomic32 batch_initialized;
102  cf_queue* batch_q;
104 
105  // Scan transaction management.
106  cf_atomic32 scan_initialized;
107  cf_queue* scan_q;
109 
110  // Query transaction management.
111  cf_atomic32 query_initialized;
112  cf_queue* query_q;
114 };
115 
117  // Mutex to cover master/prole transitions for this partition.
118  pthread_mutex_t lock;
119 
120  // Which node, if any, is the master.
121  cl_cluster_node* master;
122 
123  // Which node, if any, is the prole.
124  // TODO - not ideal for replication factor > 2.
125  cl_cluster_node* prole;
126 };
127 
129  cl_partition_table* next;
130  char ns[33];
131  cl_partition partitions[];
132 };
133 
134 
135 /******************************************************************************
136  * FUNCTIONS
137  ******************************************************************************/
138 
139 // Cluster calls
140 extern cl_cluster_node * cl_cluster_node_get_random(cl_cluster *asc);
141 extern cl_cluster_node * cl_cluster_node_get(cl_cluster *asc, const char *ns, const cf_digest *d, bool write);
142 extern void cl_cluster_node_release(cl_cluster_node *cn, const char *tag);
143 extern void cl_cluster_node_reserve(cl_cluster_node *cn, const char *tag);
144 extern void cl_cluster_node_put(cl_cluster_node *cn);
145 extern int cl_cluster_node_fd_get(cl_cluster_node *cn, bool asyncfd);
146 extern void cl_cluster_node_fd_put(cl_cluster_node *cn, int fd, bool asyncfd);
147 extern cl_cluster_node *cl_cluster_node_get_byname(cl_cluster *asc, const char *name);
148 
149 extern int citrusleaf_info_parse_single(char *values, char **value);
150 
151 extern cl_cluster * citrusleaf_cluster_create(void);
152 extern void citrusleaf_cluster_destroy(cl_cluster *asc);
153 
154 extern cl_cluster * citrusleaf_cluster_get_or_create(char *host, short port, int timeout_ms);
155 extern void citrusleaf_cluster_release_or_destroy(cl_cluster **asc);
156 extern void citrusleaf_cluster_change_info_timeout(struct cl_cluster_s *asc, int msecs);
157 extern void citrusleaf_cluster_change_tend_speed(struct cl_cluster_s *asc, int secs);
158 
159 // the timeout is how long to wait before the cluster is "settled"
160 // 0 - a sensible default
161 // N - some number of MS
162 // -1 - don't wait this time
163 
164 extern cl_rv citrusleaf_cluster_add_host(cl_cluster *asc, char const *host, short port, int timeout_ms);
165 
166 extern void citrusleaf_cluster_add_addr_map(cl_cluster *asc, char *orig, char *alt);
167 
168 extern bool citrusleaf_cluster_settled(cl_cluster *asc);
169 
170 extern int citrusleaf_cluster_get_nodecount(cl_cluster *asc);
171 
172 // must free node_names when done
173 extern void cl_cluster_get_node_names(cl_cluster *asc, int *n_nodes, char **node_names);
174 
175 
176 // in the PHP system, URLs are lingua franca. We expect that
177 // a single cluster will be created with one name - the URL - and
178 // will be used over and over.
179 //
180 // URLs are of the form;
181 // citrusleaf://host:port (or similar)
182 extern cl_cluster * citrusleaf_cluster_get(char const *url);
183 
184 
185 // By default, the C client will "follow" the cluster, that is,
186 // track all the nodes in the cluster and continually update the cluster
187 // members. If, for testing, you wish to disable this feature, set this
188 // flag to false. This must be done before any 'addhost' calls, because
189 // even at the first one, the following of the cluster might start.
190 //
191 // Currently, setting this flags after hosts has an undefined effect.
192 
193 extern void citrusleaf_cluster_follow(cl_cluster *asc, bool flag);
194 
pthread_t batch_threads[NUM_BATCH_THREADS]
Definition: cl_cluster.h:103
cf_atomic32 scan_initialized
Definition: cl_cluster.h:106
void cl_cluster_get_node_names(cl_cluster *asc, int *n_nodes, char **node_names)
cl_partition_table * next
Definition: cl_cluster.h:129
uint32_t last_node
Definition: cl_cluster.h:86
pthread_mutex_t batch_init_lock
Definition: cl_cluster.h:100
cf_atomic32 tender_running
Definition: cl_cluster.h:72
uint32_t partition_generation
Definition: cl_cluster.h:55
cl_rv
Definition: cl_types.h:64
cf_vector sockaddr_in_v
Definition: cl_cluster.h:54
cf_queue * conn_q_asyncfd
Definition: cl_cluster.h:57
#define NODE_NAME_SIZE
Definition: cl_types.h:52
cf_vector host_port_v
Definition: cl_cluster.h:81
cf_vector host_addr_map_v
Definition: cl_cluster.h:83
cl_cluster * citrusleaf_cluster_create(void)
cf_atomic32 query_initialized
Definition: cl_cluster.h:111
cf_vector host_str_v
Definition: cl_cluster.h:80
cl_cluster_node * prole
Definition: cl_cluster.h:125
cf_queue * asyncwork_q
Definition: cl_cluster.h:58
pthread_mutex_t lock
Definition: cl_cluster.h:118
pthread_t tender_thread
Definition: cl_cluster.h:71
void cl_cluster_node_fd_put(cl_cluster_node *cn, int fd, bool asyncfd)
void citrusleaf_cluster_add_addr_map(cl_cluster *asc, char *orig, char *alt)
void citrusleaf_cluster_release_or_destroy(cl_cluster **asc)
cl_cluster_node * cl_cluster_node_get(cl_cluster *asc, const char *ns, const cf_digest *d, bool write)
cf_atomic32 batch_initialized
Definition: cl_cluster.h:101
int citrusleaf_cluster_get_nodecount(cl_cluster *asc)
cl_partition partitions[]
Definition: cl_cluster.h:131
char name[NODE_NAME_SIZE]
Definition: cl_cluster.h:52
int info_timeout
Definition: cl_cluster.h:95
cl_cluster_node * cl_cluster_node_get_random(cl_cluster *asc)
pthread_mutex_t LOCK
Definition: cl_cluster.h:97
bool citrusleaf_cluster_settled(cl_cluster *asc)
uint16_t cl_partition_id
#define NUM_BATCH_THREADS
Definition: cl_cluster.h:36
void citrusleaf_cluster_destroy(cl_cluster *asc)
void citrusleaf_cluster_change_info_timeout(struct cl_cluster_s *asc, int msecs)
cl_partition_id n_partitions
Definition: cl_cluster.h:90
cl_cluster * citrusleaf_cluster_get_or_create(char *host, short port, int timeout_ms)
int citrusleaf_info_parse_single(char *values, char **value)
cl_cluster * citrusleaf_cluster_get(char const *url)
cf_queue * scan_q
Definition: cl_cluster.h:107
uint32_t intervals_absent
Definition: cl_cluster.h:53
void cl_cluster_node_release(cl_cluster_node *cn, const char *tag)
cl_partition_table * partition_table_head
Definition: cl_cluster.h:91
struct cl_cluster_compression_stat_s compression_stat
Definition: cl_cluster.h:93
int cl_cluster_node_fd_get(cl_cluster_node *cn, bool asyncfd)
cl_rv citrusleaf_cluster_add_host(cl_cluster *asc, char const *host, short port, int timeout_ms)
#define NUM_QUERY_THREADS
Definition: cl_cluster.h:38
pthread_t scan_threads[NUM_SCAN_THREADS]
Definition: cl_cluster.h:108
pthread_t query_threads[NUM_QUERY_THREADS]
Definition: cl_cluster.h:113
void cl_cluster_node_put(cl_cluster_node *cn)
#define NUM_SCAN_THREADS
Definition: cl_cluster.h:37
cf_queue * query_q
Definition: cl_cluster.h:112
uint32_t tend_speed
Definition: cl_cluster.h:73
cl_cluster_node * master
Definition: cl_cluster.h:121
cl_cluster_node * cl_cluster_node_get_byname(cl_cluster *asc, const char *name)
void cl_cluster_node_reserve(cl_cluster_node *cn, const char *tag)
void citrusleaf_cluster_follow(cl_cluster *asc, bool flag)
cf_queue * batch_q
Definition: cl_cluster.h:102
cf_queue * conn_q
Definition: cl_cluster.h:56
volatile bool found_all
Definition: cl_cluster.h:77
void citrusleaf_cluster_change_tend_speed(struct cl_cluster_s *asc, int secs)
cf_vector node_v
Definition: cl_cluster.h:87