All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
as_shm_cluster.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2017 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_partition.h>
21 #include <citrusleaf/cf_queue.h>
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 // Concurrency kit needs to be under extern "C" when compiling C++.
28 #include <aerospike/ck/ck_spinlock.h>
29 #include <aerospike/ck/ck_swlock.h>
30 
31 /******************************************************************************
32  * TYPES
33  *****************************************************************************/
34 
35 /**
36  * @private
37  * Shared memory representation of node. 48 bytes.
38  */
39 typedef struct as_node_shm_s {
40  /**
41  * @private
42  * Node name.
43  */
44  char name[AS_NODE_NAME_SIZE];
45 
46  /**
47  * @private
48  * Lightweight node read/write lock.
49  */
50  ck_swlock_t lock;
51 
52  /**
53  * @private
54  * Socket address.
55  */
56  struct sockaddr_storage addr;
57 
58  /**
59  * @private
60  * TLS certificate name (needed for TLS only).
61  */
62  char tls_name[AS_HOSTNAME_SIZE];
63 
64  /**
65  * @private
66  * Features supported by server. Stored in bitmap.
67  */
68  uint32_t features;
69 
70  /**
71  * @private
72  * Is node currently active.
73  */
74  uint8_t active;
75 
76  /**
77  * @private
78  * Pad to 8 byte boundary.
79  */
80  char pad[3];
81 } as_node_shm;
82 
83 /**
84  * @private
85  * Shared memory representation of map of namespace data partitions to nodes. 8 bytes.
86  */
87 typedef struct as_partition_shm_s {
88  /**
89  * @private
90  * Master node index offset.
91  */
92  uint32_t master;
93 
94  /**
95  * @private
96  * Prole node index offset.
97  */
98  uint32_t prole;
100 
101 /**
102  * @private
103  * Shared memory representation of map of namespace to data partitions. 32 bytes + partitions size.
104  */
105 typedef struct as_partition_table_shm_s {
106  /**
107  * @private
108  * Namespace name.
109  */
111 
112  /**
113  * @private
114  * Array of partitions for a given namespace.
115  */
116  as_partition_shm partitions[];
118 
119 /**
120  * @private
121  * Shared memory cluster map. The map contains fixed arrays of nodes and partition tables.
122  * Each partition table contains a fixed array of partitions. The shared memory segment will be
123  * sized on startup and never change afterwards. If the max nodes or max namespaces are reached,
124  * the tender client will ignore additional nodes/namespaces and log an error message that the
125  * corresponding array is full.
126  */
127 typedef struct as_cluster_shm_s {
128  /**
129  * @private
130  * Last time cluster was tended in milliseconds since epoch.
131  */
132  uint64_t timestamp;
133 
134  /**
135  * @private
136  * Cluster tend owner process id.
137  */
138  uint32_t owner_pid;
139 
140  /**
141  * @private
142  * Current size of nodes array.
143  */
144  uint32_t nodes_size;
145 
146  /**
147  * @private
148  * Maximum size of nodes array.
149  */
150  uint32_t nodes_capacity;
151 
152  /**
153  * @private
154  * Nodes generation count. Incremented whenever a node is added or removed from cluster.
155  */
156  uint32_t nodes_gen;
157 
158  /**
159  * @private
160  * Total number of data partitions used by cluster.
161  */
162  uint32_t n_partitions;
163 
164  /**
165  * @private
166  * Current size of partition tables array.
167  */
169 
170  /**
171  * @private
172  * Maximum size of partition tables array.
173  */
175 
176  /**
177  * @private
178  * Cluster offset to partition tables at the end of this structure.
179  */
181 
182  /**
183  * @private
184  * Bytes required to hold one partition_table.
185  */
187 
188  /**
189  * @private
190  * Spin lock for taking over from a dead cluster tender.
191  */
192  ck_spinlock_t take_over_lock;
193 
194  /**
195  * @private
196  * Shared memory master mutex lock. Used to determine cluster tend owner.
197  */
198  uint8_t lock;
199 
200  /**
201  * @private
202  * Has shared memory been fully initialized and populated.
203  */
204  uint8_t ready;
205 
206  /**
207  * @private
208  * Pad to 8 byte boundary.
209  */
210  char pad[6];
211 
212  /*
213  * @private
214  * Dynamically allocated node array.
215  */
216  as_node_shm nodes[];
217 
218  // This is where the dynamically allocated partition tables are located.
220 
221 /**
222  * @private
223  * Local data related to shared memory implementation.
224  */
225 typedef struct as_shm_info_s {
226  /**
227  * @private
228  * Pointer to cluster shared memory.
229  */
231 
232  /**
233  * @private
234  * Array of pointers to local nodes.
235  * Array index offsets are synchronized with shared memory node offsets.
236  */
238 
239  /**
240  * @private
241  * Shared memory identifier.
242  */
243  int shm_id;
244 
245  /**
246  * @private
247  * Take over shared memory cluster tending if the cluster hasn't been tended by this
248  * millisecond threshold.
249  */
251 
252  /**
253  * @private
254  * Is this process responsible for performing cluster tending.
255  */
256  volatile bool is_tend_master;
257 } as_shm_info;
258 
259 /******************************************************************************
260  * FUNCTIONS
261  ******************************************************************************/
262 
263 /**
264  * @private
265  * Create shared memory implementation of cluster.
266  */
267 as_status
268 as_shm_create(struct as_cluster_s* cluster, as_error* err, as_config* config);
269 
270 /**
271  * @private
272  * Destroy shared memory components.
273  */
274 void
275 as_shm_destroy(struct as_cluster_s* cluster);
276 
277 /**
278  * @private
279  * Add nodes to shared memory.
280  */
281 void
282 as_shm_add_nodes(struct as_cluster_s* cluster, as_vector* /* <as_node*> */ nodes_to_add);
283 
284 /**
285  * @private
286  * Remove nodes from shared memory.
287  */
288 void
289 as_shm_remove_nodes(struct as_cluster_s* cluster, as_vector* /* <as_node*> */ nodes_to_remove);
290 
291 /**
292  * @private
293  * Update shared memory partition tables for given namespace.
294  */
295 void
296 as_shm_update_partitions(as_shm_info* shm_info, const char* ns, char* bitmap_b64, int64_t len, as_node* node, bool master);
297 
298 /**
299  * @private
300  * Get shared memory mapped node given digest key. If there is no mapped node, a random node is
301  * used instead. as_nodes_release() must be called when done with node.
302  */
303 as_node*
304 as_shm_node_get(struct as_cluster_s* cluster, const char* ns, const uint8_t* digest, as_policy_replica replica, bool master);
305 
306 /**
307  * @private
308  * Get shared memory partition tables array.
309  */
310 static inline as_partition_table_shm*
312 {
313  return (as_partition_table_shm*) ((char*)cluster_shm + cluster_shm->partition_tables_offset);
314 }
315 
316 /**
317  * @private
318  * Get partition table identified by index.
319  */
320 static inline as_partition_table_shm*
322 {
323  return (as_partition_table_shm*) ((char*)tables + (cluster_shm->partition_table_byte_size * index));
324 }
325 
326 /**
327  * @private
328  * Get next partition table in array.
329  */
330 static inline as_partition_table_shm*
332 {
333  return (as_partition_table_shm*) ((char*)table + cluster_shm->partition_table_byte_size);
334 }
335 
336 #ifdef __cplusplus
337 } // end extern "C"
338 #endif