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