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