File: wd_list.c

package info (click to toggle)
pgpool2 3.3.4-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 9,196 kB
  • sloc: ansic: 59,850; sh: 12,893; yacc: 10,787; lex: 4,637; sql: 743; makefile: 481; java: 469; php: 125; ruby: 98; asm: 5
file content (447 lines) | stat: -rw-r--r-- 8,493 bytes parent folder | download | duplicates (2)
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
 * $Header$
 *
 * Handles watchdog connection, and protocol communication with pgpool-II
 *
 * pgpool: a language independent connection pool server for PostgreSQL
 * written by Tatsuo Ishii
 *
 * Copyright (c) 2003-2012	PgPool Global Development Group
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby
 * granted, provided that the above copyright notice appear in all
 * copies and that both that copyright notice and this permission
 * notice appear in supporting documentation, and that the name of the
 * author not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior
 * permission. The author makes no representations about the
 * suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>

#include "pool.h"
#include "pool_config.h"
#include "watchdog.h"
#include "wd_ext.h"

int wd_add_wd_list(WdDesc * other_wd);
int wd_set_wd_info(WdInfo * info);
WdInfo * wd_is_exist_master(void);
WdInfo * wd_get_lock_holder(void);
WdInfo * wd_get_interlocking(void);
bool wd_are_interlocking_all(void);
void wd_set_lock_holder(WdInfo *info, bool value);
void wd_set_interlocking(WdInfo *info, bool value);
void wd_clear_interlocking_info(void);
int wd_am_I_oldest(void);
int wd_set_myself(struct timeval * tv, int status);
WdInfo * wd_is_alive_master(void);
bool wd_is_contactable_master(void);
bool wd_are_contactable_all(void);

static void wd_reset_wd_info(WdInfo *info);

/* add or modify watchdog information list */
int
wd_set_wd_list(char * hostname, int pgpool_port, int wd_port, char * delegate_ip, struct timeval * tv, int status)
{
	int i = 0;
	WdInfo * p = NULL;

	if ((WD_List == NULL) || (hostname == NULL))
	{
		pool_error("wd_set_wd_list: memory allocate error");
		return -1;
	}

	if (strcmp(pool_config->delegate_IP, delegate_ip))
	{
		pool_error("wd_set_wd_list: delegate IP mismatch error");
		return -1;
	}

	for ( i = 0 ; i < MAX_WATCHDOG_NUM ; i ++)
	{
		p = (WD_List+i);

		if( p->status != WD_END)
		{
			/* found; modify the pgpool. */
			if ((!strncmp(p->hostname, hostname, sizeof(p->hostname))) &&
				(p->pgpool_port == pgpool_port)	&&
				(p->wd_port == wd_port))
			{
				if (p->status == WD_DOWN || p->status == WD_INIT)
				{
					wd_reset_wd_info(p);
				}

				p->status = status;

				if (tv != NULL)
				{
					memcpy(&(p->tv), tv, sizeof(struct timeval));
				}

				return i;
			}
		}

		/* not found; add as a new pgpool */
		else
		{
			wd_reset_wd_info(p);

			p->status = status;
			p->pgpool_port = pgpool_port;
			p->wd_port = wd_port;

			strlcpy(p->hostname, hostname, sizeof(p->hostname));
			strlcpy(p->delegate_ip, delegate_ip, sizeof(p->delegate_ip));

			if (tv != NULL)
			{
				memcpy(&(p->tv), tv, sizeof(struct timeval));
			}

			return i;
		}
	}

	pool_error("wd_set_wd_list: Can not add new watchdog information cause the WD_List is full.");
	return -1;
}

/** reset watchdog info*/
static void
wd_reset_wd_info(WdInfo * info)
{
	WD_TIME_INIT(info->hb_send_time);
	WD_TIME_INIT(info->hb_last_recv_time);

	info->in_interlocking = false;
	info->is_lock_holder = false;
}

/* add watchdog information to list using config description */
int
wd_add_wd_list(WdDesc * other_wd)
{
	WdInfo * p = NULL;
	int i = 0;

	if (other_wd == NULL)
	{
		pool_error("wd_add_wd_list: memory allocate error");
		return -1;
	}

	for ( i = 0 ; i < other_wd->num_wd ; i ++)
	{
		p = &(other_wd->wd_info[i]);
		strlcpy(p->delegate_ip, pool_config->delegate_IP, sizeof(p->delegate_ip));
		wd_set_wd_info(p);
	}

	return i;
}

/* set watchdog information to list */
int
wd_set_wd_info(WdInfo * info)
{
	int rtn;
	rtn = wd_set_wd_list(info->hostname, info->pgpool_port, info->wd_port,
	                     info->delegate_ip, &(info->tv), info->status);
	return rtn;
}

/* return master if exist, NULL if not found */
WdInfo *
wd_is_exist_master(void)
{
	WdInfo * p = WD_List;

	while (p->status != WD_END)
	{
		/* find master pgpool in the other servers */
		if (p->status == WD_MASTER)
		{
			/* master found */
			return p;
		}
		p++;
	}
	/* not found */
	return NULL;
}

/* set or unset in_interlocking flag */
void
wd_set_interlocking(WdInfo *info, bool value)
{
	WdInfo * p = WD_List;

	while (p->status != WD_END)
	{
		if ((!strncmp(p->hostname, info->hostname, sizeof(p->hostname))) &&
			(p->pgpool_port == info->pgpool_port) &&
			(p->wd_port == info->wd_port))
		{
			p->in_interlocking = value;

			return;
		}
		p++;
	}
}

/* set or unset lock holder flag */
void
wd_set_lock_holder(WdInfo *info, bool value)
{
	WdInfo * p = WD_List;

	while (p->status != WD_END)
	{
		if ((!strncmp(p->hostname, info->hostname, sizeof(p->hostname))) &&
			(p->pgpool_port == info->pgpool_port) &&
			(p->wd_port == info->wd_port))
		{
			p->is_lock_holder = value;

			return;
		}
		p++;
	}
}

/* return the lock holder if exist, NULL if not found */
WdInfo *
wd_get_lock_holder(void)
{
	WdInfo * p = WD_List;

	while (p->status != WD_END)
	{
		/* find failover lock holder */
		if ((p->status == WD_NORMAL || p->status == WD_MASTER) &&
			p->is_lock_holder)
		{
			/* found */
			return p;
		}
		p++;
	}

	/* not found */
	return NULL;
}

/* return the pgpool in interlocking found in first, NULL if not found */
WdInfo *
wd_get_interlocking(void)
{
	WdInfo * p = WD_List;

	/* for updating contactable flag */
	wd_update_info();

	while (p->status != WD_END)
	{
		/* skip if not contactable */
		if (!p->is_contactable)
		{
			p++;
			continue;
		}

		/* find pgpool in interlocking */
		if ((p->status == WD_NORMAL || p->status == WD_MASTER) &&
		    p->in_interlocking)
		{
			/* found */
			return p;
		}
		p++;
	}

	/* not found */
	return NULL;
}

/* if all pgpool are in interlocking return true, otherwise false */
bool
wd_are_interlocking_all(void)
{
	WdInfo * p = WD_List;
	bool rtn = true;

	/* for updating contactable flag */
	wd_update_info();

	while (p->status != WD_END)
	{
		/* skip if not contactable */
		if (!p->is_contactable)
		{
			p++;
			continue;
		}

		/* find pgpool not in interlocking */
		if ((p->status == WD_NORMAL || p->status == WD_MASTER) &&
		    !p->in_interlocking)
		{
			rtn = false;
		}
		p++;
	}

	return rtn;
}

/* clear flags for interlocking */
void
wd_clear_interlocking_info(void)
{
	WdInfo * p = WD_List;

	while (p->status != WD_END)
	{
		wd_set_lock_holder(p, false);
		wd_set_interlocking(p, false);
		p++;
	}
}

int
wd_am_I_oldest(void)
{
	WdInfo * p = WD_List;

	p++;
	while (p->status != WD_END)
	{
		if ((p->status == WD_NORMAL) ||
			(p->status == WD_MASTER))
		{
			if (WD_TIME_BEFORE(p->tv, WD_MYSELF->tv))
			{
				return WD_NG;
			}
		}
		p++;
	}
	return WD_OK;
}

int
wd_set_myself(struct timeval * tv, int status)
{
	if (WD_MYSELF == NULL)
	{
		return WD_NG;
	}

	if (tv != NULL)
	{
		memcpy(&(WD_MYSELF->tv),tv,sizeof(struct timeval));
	}

	WD_MYSELF->status = status;

	return WD_OK;
}

/*
 * if master exists and it is alive actually return the master,
 * otherwise return NULL
 */
WdInfo *
wd_is_alive_master(void)
{
	WdInfo * master = NULL;

	if (WD_MYSELF->status == WD_MASTER)
		return WD_MYSELF;

	master = wd_is_exist_master();
	if (master != NULL)
	{
		if ((!strcmp(pool_config->wd_lifecheck_method, MODE_HEARTBEAT)) ||
		    (!strcmp(pool_config->wd_lifecheck_method, MODE_QUERY)
			     && wd_ping_pgpool(master) == WD_OK))
		{
			return master;
		}
	}

	pool_debug("wd_is_alive_master: alive master not found");

	return NULL;
}

/*
 * if master exists and it is contactable return true,
 * otherwise return false
 */
bool
wd_is_contactable_master(void)
{
	WdInfo * master = NULL;

	if (WD_MYSELF->status == WD_MASTER)
		return true;

	/* for updating contactable flag */
	wd_update_info();

	master = wd_is_exist_master();
	if (master != NULL)
	{
		return master->is_contactable;
	}

	return false;
}

bool
wd_are_contactable_all(void)
{
	WdInfo * p = WD_List;

	/* for updating contactable flag */
	wd_update_info();

	while (p->status != WD_END)
	{
		if ((p->status == WD_NORMAL || p->status == WD_MASTER) &&
		    !p->is_contactable)
		{
			return false;
		}
		p++;
	}

	return true;
}

/*
 * get watchdog information of specified index
 */
WdInfo *
wd_get_watchdog_info(int wd_index)
{
	if (wd_index < 0 || wd_index > pool_config->other_wd->num_wd)
		return NULL;

	return &WD_List[wd_index];
}