File: hb.c

package info (click to toggle)
rocks 2.4-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 296 kB
  • ctags: 434
  • sloc: ansic: 5,668; makefile: 111
file content (426 lines) | stat: -rw-r--r-- 8,418 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/* 
 *  rocks/hb.c
 *
 *  The heartbeat connection failure detection mechanism.
 *
 *  Copyright (C) 2001 Victor Zandy
 *  See COPYING for distribution terms.
 */

#include <sys/time.h>
#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <assert.h>

#include "rs.h"
#include "log.h"

struct hb {
	int period;		  /* Beat period (multiple of alarm period) */
	int count;		  /* Beats that have occurred within period */
	int limit;		  /* Missed beats limit */
	int missed;		  /* Current number of missed beats */
	int s;   		  /* Connection to peer hb socket (plain) */
	struct sockaddr_in addr;  /* Address of peer's hb socket */
	rs_t rs;		  /* Pointer to owning rock */
	int lshm;		  /* Last count seen in shm */
	int ushm;		  /* Beats in which shm has not changed */
};

/* These are NULL unless there is a HB for the descriptor. */
static hb_t hbs[RS_MAXFD];

hb_t
rs_new_heartbeat(rs_t rs)
{
	hb_t hb;
	hb = (hb_t) malloc(sizeof(struct hb));
	if (!hb)
		return NULL;
	hb->limit = rs_opt_max_alarm_misses;
	hb->period = 1;
	hb->count = 0;
	hb->missed = 0;
	hb->s = -1;
	hb->rs = rs;
	hb->lshm = 0;
	hb->ushm = 0;
	return hb;
}

int
rs_hb_save(hb_t hb, int fd)
{
	if (0 > rs_xwrite(fd, hb, sizeof(*hb)))
		return -1;
	return 0;
}

hb_t
rs_hb_restore(rs_t rs, int fd)
{
	struct hb h;
	hb_t hb;

	if (0 > rs_xread(fd, &h, sizeof(h), 0))
		return NULL;
	hb = rs_new_heartbeat(NULL);
	if (!hb)
		return NULL;
	*hb = h;
	rs->hb = hb;
	hb->rs = rs;
	hbs[hb->s] = hb;
	return hb;
}

void
rs_free_heartbeat(hb_t hb)
{
	rs_mode_native();
	close(hb->s);
	rs_mode_pop();
	free(hb);
}

static hb_t
hb_lookup(int s)
{
	if (s >= RS_MAXFD || s < 0)
		return NULL;
	return hbs[s];
}

/* ROCK is established data connection descriptor.
   HB is allocated, initialized HB structure.
   Establish HB connection and update HB accordingly.
   ROLE is used to decide ties. */
int
rs_hb_establish(int rock, hb_t hb, rs_role role)
{
	int rv, len, sd;
	struct sockaddr_in laddr;

	rs_mode_native();

	/* Bind a new listening socket with IP address of ROCK */
	len = sizeof(laddr);
	if (0 > getsockname(rock, (struct sockaddr *)&laddr, &len))
		goto out_noclose;
	sd = socket(AF_INET, SOCK_DGRAM, 0);
	if (0 > sd)
		goto out_noclose;
	len = sizeof(laddr);
	laddr.sin_port = htons(0);
	rv = bind(sd, (struct sockaddr *) &laddr, sizeof(laddr));
	if (0 > rv)
		goto out;

	/* Exchange socket addresses with peer - network order */
	len = sizeof(laddr);
	if (0 > getsockname(sd, (struct sockaddr *)&laddr, &len))
		goto out;
	if (0 >= rs_xwrite(rock, &laddr, sizeof(laddr)))
		goto out;
	if (0 >= rs_xread(rock, &hb->addr, sizeof(hb->addr), 0))
		goto out;
	hb->s = sd;
	hbs[sd] = hb;
	rs_mode_pop();
	rs_log("return from hb est l=%s", rs_ipstr(&laddr));
	rs_log("return from hb est p=%s", rs_ipstr(&hb->addr));
	return 0;
out:
	close(sd);
out_noclose:
	rs_mode_pop();
	rs_log("Cannot establish heartbeat connection for <%p>.", hb->rs);
	return -1;
}

int
rs_hb_cancel(hb_t hb)
{
	if (hb->s >= 0) {
		hbs[hb->s] = NULL;
		close(hb->s);
		rs_log("<%d:%p> canceled hb", hb->rs->sd, hb->rs);
	}
	hb->s = -1;
	hb->count = 0;
	hb->missed = 0;
	return 0;
}

void
rs_hb_init_shm(rs_t rs)
{
	rs->shm->hb_owner = getpid();
	rs->shm->hb_count = 0;
	rs->hb->lshm = 0;
	rs->hb->ushm = 0;
}

static pid_t
hb_owner(hb_t hb)
{
	if (!rs_rock_is_shared(hb->rs))
		return getpid();
	else
		return hb->rs->shm->hb_owner;
}

static int
hb_ping_owner(hb_t hb)
{
	if (hb->lshm == hb->rs->shm->hb_count) {
		hb->ushm++;
		if (hb->ushm > 2)
			return 0;
		else
			return 1;
	}
	hb->lshm = hb->rs->shm->hb_count;
	hb->ushm = 0;
	return 1;
}

static int
hb_takeover(hb_t hb)
{
	rs_shm_lock(hb->rs->shm);
	hb->rs->shm->hb_owner = getpid();
	hb->rs->shm->hb_count++; /* notifies others that hb is okay */
	rs_log("new owner of HB for <%d:%p>", hb->rs->sd, hb->rs);
	rs_shm_unlock(hb->rs->shm);
	return 0;
}

/* Return 0 if sent hb okay.  Return -1 if the connection went away. */
static int
send_hb(hb_t hb)
{
	int rv, e;
retry:
	rv = sendto(hb->s, "x", 1, 0,
		    (struct sockaddr *)&hb->addr, sizeof(hb->addr));
	if (rv == 1)
		return 0;
	assert(rv != 0);
	e = errno;
	switch(e) {
	case EINTR:
		goto retry;
		break;
	case EAGAIN:
	case ENOSPC:
	case EIO:
	case EFAULT:
		/* These shouldn't happen */
		assert(0);
		break;
	case EBADF:
	case EINVAL:  /* Linux can do this if you remove the nic */
	default:
 	        /* The connection went away */
		break;
	}
	return -1;
}


static void
chkrecv()
{
	fd_set fdr, fds, fdrx;
	int max, maxh, n, sd, rv;
	struct timeval tv;
	rs_t rs;
	hb_t hb;
	char x;
	pid_t mypid;

	mypid = getpid();
retry_select:
	FD_ZERO(&fdrx);
	FD_ZERO(&fds);
	max = rs_fdset(&fds);
	maxh = 0;
	for (sd = 0; sd < max; sd++)
		if (FD_ISSET(sd, &fds)) {
			int x;
			rs_t rs = rs_lookup(sd);
			assert(rs);
			if (mypid != hb_owner(rs->hb))
				continue;
			if (RS_SUSPENDED == rs->state)
				x = rs->sd; /* reconnect notification */
			else
				x = rs->hb->s; /* hb */
			FD_SET(x, &fdrx);
			if (x > maxh)
				maxh = x;
		}
	maxh++;
	memcpy(&fdr, &fdrx, sizeof(fdrx));
	tv.tv_sec = 0;
	tv.tv_usec = 0;
	n = select(maxh, &fdr, NULL, NULL, &tv);
	if (0 > n) {
		if (errno == EBADF)
			rs_recover_bad_rocks(maxh, &fds);
		goto retry_select;
	}

	for (sd = 0; n > 0 && sd < maxh; sd++) {
		if (!FD_ISSET(sd, &fdr))
			continue;

		/* reconnection? */
		if ((rs = rs_lookup(sd))) {
			assert(rs->state == RS_SUSPENDED);
			rs_rec_complete(rs, RS_NOBLOCK);
			continue;
		}

		/* otherwise a heartbeat */
		hb = hb_lookup(sd);
		assert(hb);
		assert(hb->s == sd);
		rv = recv(hb->s, &x, 1, 0);
		if (0 > rv) {
			rs_log("rock <%d:%p> hb recv error (%s)",
			       hb->rs->sd, hb->rs,
			       strerror(errno));
			rs_reconnect(hb->rs, RS_NOBLOCK);
			continue;
		} else {
			hb->missed = 0;
			n--;
		}
	}
}

static void
handle_sigalrm(int sig)
{
	fd_set fdset;
	rs_t rs;
	int max;
	int sd;
	pid_t mypid;

	rs_mode_native();

	if (getpid() != rs_pid) {
		/* FIXME: assume we've restored a checkpoint;
		   restart the log. hopefully we will not need
		   any other updating.  once ckpt does files,
		   this can go. */
		rs_init_log();
		rs_log("process id changed from [%d]", rs_pid);
		rs_pid = getpid();
	}

	chkrecv();
	mypid = getpid();
	FD_ZERO(&fdset);
	max = rs_fdset(&fdset);
	for (sd = 0; sd < max; sd++) {
		if (!FD_ISSET(sd, &fdset))
			continue;
		rs = rs_lookup(sd);
		assert(rs);

		/* Update shared heartbeats; even for suspended rocks */
		if (rs_rock_is_shared(rs)) {
			if (mypid != hb_owner(rs->hb)) {
				if (!hb_ping_owner(rs->hb))
					hb_takeover(rs->hb);
				continue; /* non-owner does not touch hb */
			} else if (rs_shm_has_one_owner(rs))
				/* garbage collect */
				rs_shm_detach(rs);
			else
				rs->shm->hb_count++;
		}
		if (RS_EDP == rs->state || RS_SUSPENDED == rs->state)
			continue;

		/* Count beats toward this heartbeat's period */
		rs->hb->count++;
		if (rs->hb->count < rs->hb->period)
			continue;
		
		/* Period expired */
		rs->hb->count = 0;
		rs->hb->missed++;
		
		/* Send hb */
		if (0 > send_hb(rs->hb)) {
			rs_log("rock <%d:%p> hb send error (%s)", rs->sd, rs,
			       strerror(errno));
			rs_reconnect(rs, RS_NOBLOCK);
			continue;
		}
		
		/* Check missed hb */
		if (rs->hb->missed >= rs->hb->limit) {
			rs_log("reconnect for too many missed hbs");
			rs_reconnect(rs, RS_NOBLOCK);
		} else if (rs->hb->missed > 1)
			rs_log("<%d:%p> missed heartbeat (%d more)",
			       rs->sd, rs, rs->hb->limit - rs->hb->missed);
	}
	rs_mode_pop();
}

void 
rs_stop_heartbeat(sigset_t *oset)
{
	sigset_t set;
	sigemptyset(&set);
	sigaddset(&set, SIGALRM);
	sigprocmask(SIG_BLOCK, &set, oset);
}

void
rs_resume_heartbeat(sigset_t *oset)
{
	sigprocmask(SIG_SETMASK, oset, NULL);
}

int
rs_init_heartbeat()
{
	struct sigaction sa;
	struct itimerval it;
	int ret = 0;

	memset(&sa, 0, sizeof(sa));
	sigfillset(&sa.sa_mask);
	sigdelset(&sa.sa_mask, SIGTERM);
	sigdelset(&sa.sa_mask, SIGINT);
	sa.sa_flags = SA_RESTART;
	sa.sa_handler = handle_sigalrm;
	rs_rs_sigaction(SIGALRM, &sa);

	it.it_value.tv_sec = it.it_interval.tv_sec = rs_opt_alarm_period;
	it.it_value.tv_usec = it.it_interval.tv_usec = 0;
	rs_mode_native();
	ret = setitimer(ITIMER_REAL, &it, NULL);
	rs_mode_pop();
	return ret;
}

int
rs_setitimer(int which, const struct itimerval *value, struct itimerval *ov)
{
	return 0;
}