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