Blame view

cluster.h 7.35 KB
redmine authored
1
/*
redmine authored
2
    clsync - file tree sync utility based on inotify
redmine authored
3
    
redmine authored
4
    Copyright (C) 2013  Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
redmine authored
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

redmine authored
20 21
#ifdef CLUSTER_SUPPORT

redmine authored
22 23 24
// Macros for reading messages

#define CLUSTER_RESTDATALEN(clustercmd_p, data_type) \
redmine authored
25
	((clustercmd_p)->h.data_len - sizeof(data_type) + sizeof(char *))
redmine authored
26 27 28 29

#define CLUSTER_LOOP_EXPECTCMD(clustercmd_p, clustercmd_id, ret) {\
		/* Exit if error */ \
		if(ret == -1) { \
redmine authored
30
			error("CLUSTER_LOOP_EXPECTCMD()"); \
redmine authored
31 32 33 34
			return errno; \
		}\
\
		/* Is that the command we are expecting? Skipping if not. */\
redmine authored
35
		if(clustercmd_p->h.cmd_id != clustercmd_id)\
redmine authored
36 37 38 39 40
			continue;\
}

// Macros for writing messages

redmine authored
41
//	calculated required memory for clustercmd packet
redmine authored
42
#define CLUSTER_REQMEM(data_type, restdata_len) \
redmine authored
43
	(sizeof(clustercmdhdr_t) + sizeof(data_type) + (restdata_len) + 2)
redmine authored
44

redmine authored
45
//	calculated required memory for clustercmd packet with padding
redmine authored
46 47
#define CLUSTER_REQMEM_PADDED(data_type, restdata_len) \
	CLUSTER_PAD(CLUSTER_REQMEM(data_type, restdata_len))
redmine authored
48 49

//	allocated memory for clustercmd packet with padding
redmine authored
50
#define CLUSTER_ALLOC(data_type, restdata_len, alloc_funct)\
redmine authored
51
	(clustercmd_t *)memset((alloc_funct)(CLUSTER_REQMEM_PADDED(data_type, restdata_len)), 0, CLUSTER_REQMEM_PADDED(data_type, restdata_len))
redmine authored
52

redmine authored
53
//	allocated memory for clustercmd packet with padding with alloca()
redmine authored
54 55
#define CLUSTER_ALLOCA(data_type, restdata_len)\
	CLUSTER_ALLOC(data_type, restdata_len, alloca)
redmine authored
56

redmine authored
57
//	allocated memory for clustercmd packet with padding with xmalloc()
redmine authored
58 59
#define CLUSTER_MALLOC(data_type, restdata_len)\
	CLUSTER_ALLOC(data_type, restdata_len, xmalloc)
redmine authored
60

redmine authored
61
// Common macros
redmine authored
62

redmine authored
63
#define CLUSTER_PAD(size) ((((size) + 3) >> 2) << 2)
redmine authored
64 65 66

#define CLUSTERCMD_SIZE(clustercmd_p)        (sizeof(clustercmdhdr_t) +             (*(clustercmd_p)).h.data_len)
#define CLUSTERCMD_SIZE_PADDED(clustercmd_p) (sizeof(clustercmdhdr_t) + CLUSTER_PAD((*(clustercmd_p)).h.data_len))
redmine authored
67

redmine authored
68 69
// Types

redmine authored
70
enum adler32_calc {
redmine authored
71
	ADLER32_CALC_NONE	= 0x00,
redmine authored
72
	ADLER32_CALC_HEADER	= 0x01,
redmine authored
73 74
	ADLER32_CALC_DATA	= 0x02,
	ADLER32_CALC_ALL	= 0x03,
redmine authored
75
};
redmine authored
76
typedef enum adler32_calc adler32_calc_t;
redmine authored
77

redmine authored
78 79 80 81 82 83
enum cluster_read_flags {
	CLREAD_NONE		= 0x00,
	CLREAD_ALL		= 0xff
};
typedef enum cluster_read_flags cluster_read_flags_t;

redmine authored
84 85 86
enum nodestatus {
	NODESTATUS_DOESNTEXIST = 0,
	NODESTATUS_OFFLINE,
redmine authored
87
	NODESTATUS_SEEMSONLINE,
redmine authored
88 89
	NODESTATUS_ONLINE,
	NODESTATUS_BANNED
redmine authored
90 91 92 93 94 95 96 97
};
typedef enum nodestatus nodestatus_t;

enum nodeid {
	NODEID_NOID		= MAXNODES
};
typedef enum nodeid nodeid_t;

redmine authored
98 99 100 101 102 103
struct packets_stats {
	uint64_t	 tot;
	uint64_t	 rej;
};
typedef struct packets_stats packets_stats_t;

redmine authored
104
struct nodeinfo {
redmine authored
105 106 107 108 109
	uint8_t      	 id;
	uint8_t      	 num;
	nodestatus_t 	 status;
	uint32_t    	 updatets;
	GHashTable  	*modtime_ht;
redmine authored
110
	GHashTable	*serial2queuedpacket_ht;
redmine authored
111 112 113
	packets_stats_t	 packets_in;
	packets_stats_t	 packets_out;
	uint32_t	 last_serial;
redmine authored
114
	char		 *node_name;
redmine authored
115 116 117 118
};
typedef struct nodeinfo nodeinfo_t;

enum clustercmd_id {
redmine authored
119 120 121 122 123 124 125
	CLUSTERCMDID_PING	= 0,
	CLUSTERCMDID_ACK	= 1,
	CLUSTERCMDID_REG	= 2,
	CLUSTERCMDID_HELLO	= 3,
	CLUSTERCMDID_WELCOME	= 4,
	CLUSTERCMDID_DIE	= 5,
	CLUSTERCMDID_HT_EXCH	= 6,
redmine authored
126
	COUNT_CLUSTERCMDID
redmine authored
127 128 129
};
typedef enum clustercmd_id clustercmd_id_t;

redmine authored
130 131
struct clustercmd_hello {
	char      node_name[0];
redmine authored
132
};
redmine authored
133
typedef struct clustercmd_hello clustercmd_hello_t;
redmine authored
134

redmine authored
135 136 137 138 139 140
#define    welcome_to_node_name_len(cmd_p) ((cmd_p)->h.data_len-(((clustercmd_welcome_t *)&(cmd_p)->data)->from_node_name_len)-sizeof(clustercmd_welcome_t))
#define    welcome_to_node_name(cmddata_p) (&cmddata_p->from_node_name[cmddata_p->from_node_name_len])
struct clustercmd_welcome {
	size_t    from_node_name_len;
	char      from_node_name[0];
//                to_node_name  ==  my_node_name+my_node_name_len
redmine authored
141
};
redmine authored
142
typedef struct clustercmd_welcome clustercmd_welcome_t;
redmine authored
143

redmine authored
144
struct clustercmd_reg {
redmine authored
145
	char      node_name[0];
redmine authored
146
};
redmine authored
147
typedef struct clustercmd_reg clustercmd_reg_t;
redmine authored
148 149 150

struct clustercmd_ack {
	uint32_t serial;
redmine authored
151
};
redmine authored
152
typedef struct clustercmd_ack clustercmd_ack_t;
redmine authored
153

redmine authored
154 155
enum reject_reason {
	REJ_UNKNOWN		= 0,
redmine authored
156
	REJ_ADLER32MISMATCH,
redmine authored
157 158 159 160
};
typedef enum reject_reason reject_reason_t;

struct clustercmd_rej {
redmine authored
161
	uint32_t serial;
redmine authored
162 163 164 165
	uint8_t	 reason;
};
typedef struct clustercmd_rej clustercmd_rej_t;

redmine authored
166 167 168
struct clustercmd_ht_exch {
	time_t	 ctime;
	size_t	 path_length;
redmine authored
169
	char	 path[0];
redmine authored
170 171 172
};
typedef struct clustercmd_ht_exch clustercmd_ht_exch_t;

redmine authored
173
struct clustercmdadler32 {
redmine authored
174 175
	uint32_t hdr;					// 32
	uint32_t dat;					// 64
redmine authored
176
};
redmine authored
177
typedef struct clustercmdadler32 clustercmdadler32_t;
redmine authored
178

redmine authored
179 180 181 182 183
struct clustercmdhdr {					// bits
	uint8_t			dst_node_id;		// 8
	uint8_t			src_node_id;		// 16
	uint8_t			flags;			// 24	(for future compatibility)
	uint8_t			cmd_id;			// 32
redmine authored
184 185 186 187
	clustercmdadler32_t	adler32;		// 96
	uint32_t		data_len;		// 128
	uint32_t		ts;			// 160
	uint32_t		serial;			// 192
redmine authored
188 189 190
};
typedef struct clustercmdhdr clustercmdhdr_t;

redmine authored
191 192
typedef char clustercmd_die_t;

redmine authored
193 194
struct clustercmd {
	clustercmdhdr_t h;
redmine authored
195
	union data {
redmine authored
196 197
		char 			p[0];
		clustercmd_welcome_t	welcome;
redmine authored
198 199 200
		clustercmd_reg_t	reg;
		clustercmd_ack_t	ack;
		clustercmd_rej_t	rej;
redmine authored
201
		clustercmd_hello_t	hello;
redmine authored
202
		clustercmd_ht_exch_t	ht_exch;
redmine authored
203
		clustercmd_die_t	die;
redmine authored
204
	} data;
redmine authored
205 206 207
};
typedef struct clustercmd clustercmd_t;

redmine authored
208
struct clustercmdqueuedpackethdri {
redmine authored
209
	char	dummy; // anti-warning
redmine authored
210 211 212 213
};
typedef struct clustercmdqueuedpackethdri clustercmdqueuedpackethdri_t;

struct clustercmdqueuedpackethdro {
redmine authored
214 215 216
	char 		ack_from[MAXNODES];
	uint8_t 	ack_count;
};
redmine authored
217 218 219 220
typedef struct clustercmdqueuedpackethdro clustercmdqueuedpackethdro_t;

struct clustercmdqueuedpackethdr {
	unsigned int	window_id;
redmine authored
221
	union w {
redmine authored
222 223
		clustercmdqueuedpackethdri_t i;
		clustercmdqueuedpackethdro_t o;
redmine authored
224
	} w;
redmine authored
225 226
};
typedef struct clustercmdqueuedpackethdr clustercmdqueuedpackethdr_t;
redmine authored
227

redmine authored
228 229 230
struct clustercmdqueuedpacket {
	clustercmdqueuedpackethdr_t	h;
	clustercmd_t 			cmd;
redmine authored
231
};
redmine authored
232
typedef struct clustercmdqueuedpacket clustercmdqueuedpacket_t;
redmine authored
233

redmine authored
234
struct window_occupied_sides {
redmine authored
235 236 237
	size_t	left;
	size_t	right;
};
redmine authored
238
typedef struct window_occupied_sides window_occupied_sides_t;
redmine authored
239 240

struct window {
redmine authored
241
	unsigned int		  size;			// Allocated cells
redmine authored
242 243
	unsigned int		  packets_len;		// Count of packets (are waiting for ACK-s)
	unsigned int		 *packets_id;		// Array of cells' id-s with packets
redmine authored
244 245 246
	window_occupied_sides_t	 *occupied_sides;	// Array of structures with coordinates in buffer of occupied space by cell ida (aka window_id)
	size_t			  buf_size;		// Allocated space of the buffer
	char 			 *buf;			// Pointer to the buffer
redmine authored
247 248 249 250 251
};
typedef struct window window_t;

typedef int (*cluster_recvproc_funct_t)(clustercmd_t *clustercmd_p);

redmine authored
252
// Externs
redmine authored
253

redmine authored
254
extern int cluster_init(struct ctx *ctx_p, struct indexes *indexes_p);
redmine authored
255 256 257 258 259
extern int cluster_deinit();

extern int cluster_lock(const char *fpath);
extern int cluster_lock_byindexes();
extern int cluster_unlock_all();
redmine authored
260
extern int cluster_capture(const char *fpath);
redmine authored
261

redmine authored
262
extern int cluster_modtime_update(const char *dirpath, short int dirlevel, mode_t st_mode);
redmine authored
263
extern int cluster_initialsync();
redmine authored
264

redmine authored
265 266
#endif