hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
cl_shm.h
Go to the documentation of this file.
1 #include "citrusleaf.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <pthread.h>
6 #include <sys/shm.h>
7 
8 #define NUM_NODES 128
9 #define NUM_NAMESPACES 10
10 
11 /*Shared memory return values*/
12 #define CL_SHM_ERROR -1
13 #define CL_SHM_OK 0
14 
15 //The shared memory is divided into nodes, each node has a socket address structure and
16 //the data associated with that structure.
17 #define SZ_NODE_IP 32
18 #define SZ_NAMESPACE 32
19 #define SZ_PARTITION_ID 4
20 #define MAX_NEIGHBORS (NUM_NODES-1)
21 #define NUM_PARTITIONS 4096
22 #define MAX_ADDRESSES_PER_NODE 4
23 
24 #define SZ_FIELD_NEIGHBORS (MAX_NEIGHBORS * SZ_NODE_IP + 1)
25 /*
26  * Example:
27  * node BB958DE9B776038
28  * partition-generation 29218
29  * services 192.168.3.102:3000;192.168.3.103:3000
30  */
31 
32 // For 3 field names with line separators, and partition-generation value:
33 #define SZ_OVERHEAD ((3 * 32) + 20)
34 // Size of each base-64 encoded bitmap:
35 #define SZ_BITMAP ((NUM_PARTITIONS + 7) / 8)
36 #define SZ_ENCODED_BITMAP (((SZ_BITMAP + 2) / 3) * 4)
37 // With namespace name and per-namespace separators:
38 #define SZ_NS_ENCODED_BITMAP (64 + SZ_ENCODED_BITMAP + 1)
39 // Finally:
40 #define SZ_REPLICAS_TEXT (SZ_OVERHEAD + (NUM_NAMESPACES * 2 * SZ_NS_ENCODED_BITMAP))
41 /*
42  * Example:
43  * partition-generation 292219
44  * replicas-master foo:Ab2T60...;bar:ry4Jfs...; ...
45  * replicas-prole foo:8xd4K2...;bar:4hTe5q...; ...
46  */
47 
48 #define SHMMAX_SYS_FILE "/proc/sys/kernel/shmmax"
49 #define DEFAULT_NUM_NODES_FOR_SHM 64
50 #define DEFAULT_SHM_KEY 229857887
51 
52 /* The shm structure has some metadata (updater_id, node_count, global lock)
53  * and then start the actual node information. Each node's information is further
54  * represented by a structure cl_shm_ninfo which has a socket address, node level lock
55  * and the fields */
56 typedef struct {
57  struct sockaddr_in address_array[MAX_ADDRESSES_PER_NODE];
58  pthread_mutex_t ninfo_lock;
61  char node_name[NODE_NAME_SIZE];
62  char services[SZ_FIELD_NEIGHBORS];
63  char replicas[SZ_REPLICAS_TEXT];
64 } cl_shm_ninfo;
65 
66 typedef struct {
67  size_t updater_id;
70  pthread_mutex_t shm_lock;
71 
72  /* Change this approach to calculating address
73  * of all the structures in the shared memory upfront*/
74  cl_shm_ninfo node_info[];
75 } cl_shm;
76 
77 /* This is a global structure which has shared memory information like size,
78  * its nodes size, and id, the update thread period and the condition on which it will end itself*/
79 typedef struct {
80  int id;
81  size_t shm_sz;
82  size_t node_sz;
83  /*Condition on which the updater thread will exit*/
86 } cl_shm_info;
87 
88 /*Switch to move between shared memory and back*/
89 extern bool g_shared_memory;
90 
91 /*Shared memory functions*/
92 int citrusleaf_use_shm(int num_nodes, key_t key);
94 
96 
97 cl_shm_ninfo* cl_shm_find_node_from_name(const char* node_name);
98 cl_shm_ninfo* cl_shm_find_node_from_address(struct sockaddr_in* sa_in);
99 
100 int cl_shm_node_lock(cl_shm_ninfo* shared_node);
101 void cl_shm_node_unlock(cl_shm_ninfo* shared_node);