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-2015 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_in addr;
57 
58  /**
59  * @private
60  * Is node currently active.
61  */
62  uint8_t active;
63 
64  /**
65  * @private
66  * Does node support batch-index protocol?
67  */
68  uint8_t has_batch_index;
69 
70  /**
71  * @private
72  * Does node support replicas-all info protocol?
73  */
75 
76  /**
77  * @private
78  * Does node support floating point type?
79  */
80  uint8_t has_double;
81 
82  /**
83  * @private
84  * Pad to 8 byte boundary.
85  */
86  char pad[4];
87 } as_node_shm;
88 
89 /**
90  * @private
91  * Shared memory representation of map of namespace data partitions to nodes. 8 bytes.
92  */
93 typedef struct as_partition_shm_s {
94  /**
95  * @private
96  * Master node index offset.
97  */
98  uint32_t master;
99 
100  /**
101  * @private
102  * Prole node index offset.
103  */
104  uint32_t prole;
106 
107 /**
108  * @private
109  * Shared memory representation of map of namespace to data partitions. 32 bytes + partitions size.
110  */
111 typedef struct as_partition_table_shm_s {
112  /**
113  * @private
114  * Namespace name.
115  */
117 
118  /**
119  * @private
120  * Array of partitions for a given namespace.
121  */
122  as_partition_shm partitions[];
124 
125 /**
126  * @private
127  * Shared memory cluster map. The map contains fixed arrays of nodes and partition tables.
128  * Each partition table contains a fixed array of partitions. The shared memory segment will be
129  * sized on startup and never change afterwards. If the max nodes or max namespaces are reached,
130  * the tender client will ignore additional nodes/namespaces and log an error message that the
131  * corresponding array is full.
132  */
133 typedef struct as_cluster_shm_s {
134  /**
135  * @private
136  * Last time cluster was tended in milliseconds since epoch.
137  */
138  uint64_t timestamp;
139 
140  /**
141  * @private
142  * Cluster tend owner process id.
143  */
144  uint32_t owner_pid;
145 
146  /**
147  * @private
148  * Current size of nodes array.
149  */
150  uint32_t nodes_size;
151 
152  /**
153  * @private
154  * Maximum size of nodes array.
155  */
156  uint32_t nodes_capacity;
157 
158  /**
159  * @private
160  * Nodes generation count. Incremented whenever a node is added or removed from cluster.
161  */
162  uint32_t nodes_gen;
163 
164  /**
165  * @private
166  * Total number of data partitions used by cluster.
167  */
168  uint32_t n_partitions;
169 
170  /**
171  * @private
172  * Current size of partition tables array.
173  */
175 
176  /**
177  * @private
178  * Maximum size of partition tables array.
179  */
181 
182  /**
183  * @private
184  * Cluster offset to partition tables at the end of this structure.
185  */
187 
188  /**
189  * @private
190  * Bytes required to hold one partition_table.
191  */
193 
194  /**
195  * @private
196  * Spin lock for taking over from a dead cluster tender.
197  */
198  ck_spinlock_t take_over_lock;
199 
200  /**
201  * @private
202  * Shared memory master mutex lock. Used to determine cluster tend owner.
203  */
204  uint8_t lock;
205 
206  /**
207  * @private
208  * Has shared memory been fully initialized and populated.
209  */
210  uint8_t ready;
211 
212  /**
213  * @private
214  * Pad to 8 byte boundary.
215  */
216  char pad[6];
217 
218  /*
219  * @private
220  * Dynamically allocated node array.
221  */
222  as_node_shm nodes[];
223 
224  // This is where the dynamically allocated partition tables are located.
226 
227 /**
228  * @private
229  * Local data related to shared memory implementation.
230  */
231 typedef struct as_shm_info_s {
232  /**
233  * @private
234  * Pointer to cluster shared memory.
235  */
237 
238  /**
239  * @private
240  * Array of pointers to local nodes.
241  * Array index offsets are synchronized with shared memory node offsets.
242  */
244 
245  /**
246  * @private
247  * Shared memory identifier.
248  */
249  int shm_id;
250 
251  /**
252  * @private
253  * Take over shared memory cluster tending if the cluster hasn't been tended by this
254  * millisecond threshold.
255  */
257 
258  /**
259  * @private
260  * Is this process responsible for performing cluster tending.
261  */
262  volatile bool is_tend_master;
263 } as_shm_info;
264 
265 /******************************************************************************
266  * FUNCTIONS
267  ******************************************************************************/
268 
269 /**
270  * @private
271  * Create shared memory implementation of cluster.
272  */
273 as_status
274 as_shm_create(struct as_cluster_s* cluster, as_error* err, as_config* config);
275 
276 /**
277  * @private
278  * Destroy shared memory components.
279  */
280 void
281 as_shm_destroy(struct as_cluster_s* cluster);
282 
283 /**
284  * @private
285  * Add nodes to shared memory.
286  */
287 void
288 as_shm_add_nodes(struct as_cluster_s* cluster, as_vector* /* <as_node*> */ nodes_to_add);
289 
290 /**
291  * @private
292  * Remove nodes from shared memory.
293  */
294 void
295 as_shm_remove_nodes(struct as_cluster_s* cluster, as_vector* /* <as_node*> */ nodes_to_remove);
296 
297 /**
298  * @private
299  * Update shared memory partition tables for given namespace.
300  */
301 void
302 as_shm_update_partitions(as_shm_info* shm_info, const char* ns, char* bitmap_b64, int64_t len, as_node* node, bool master);
303 
304 /**
305  * @private
306  * Get shared memory mapped node given digest key. If there is no mapped node, a random node is
307  * used instead. as_nodes_release() must be called when done with node.
308  */
309 as_node*
310 as_shm_node_get(struct as_cluster_s* cluster, const char* ns, const uint8_t* digest, bool write, as_policy_replica replica);
311 
312 /**
313  * @private
314  * Get shared memory partition tables array.
315  */
316 static inline as_partition_table_shm*
318 {
319  return (as_partition_table_shm*) ((char*)cluster_shm + cluster_shm->partition_tables_offset);
320 }
321 
322 /**
323  * @private
324  * Get partition table identified by index.
325  */
326 static inline as_partition_table_shm*
328 {
329  return (as_partition_table_shm*) ((char*)tables + (cluster_shm->partition_table_byte_size * index));
330 }
331 
332 /**
333  * @private
334  * Get next partition table in array.
335  */
336 static inline as_partition_table_shm*
338 {
339  return (as_partition_table_shm*) ((char*)table + cluster_shm->partition_table_byte_size);
340 }
341 
342 #ifdef __cplusplus
343 } // end extern "C"
344 #endif