pgpool2.git / summary / log / commit / refs
commit 9fa61fc8dd183d2e381b4fdf2185e823d5f16822
Author: Tatsuo Ishii <ishii@postgresql.org>
Date: Thu Jul 09 04:13:00 2015 +0000
Save pgpool_status to disk each time when it is changed.
Currently pgpool-II does not save pgpool_status file when it's
changed. Instead it saves when pgpool-II explicitly is going
shutdown. This is fine in most cases and it saves slight CPU cycles.
But what if pgpool-II killed abnormally? Or the system forces
pgpool-II to go down by OOM killer? In these cases next pgpool-II
restarting could load obsolete status file.
This fix is a guard against such that situation by saving the
pgpool_status file each time when it's changed.
src/include/pool.h | 4 ++--
src/main/pgpool_main.c | 22 ++++++++++++++++++----
src/protocol/pool_connection_pool.c | 2 ++
3 files changed, 22 insertions(+), 6 deletions(-)
diff --git a/src/include/pool.h b/src/include/pool.h
index 1f8710d6e..5f6440ee1 100644
--- a/src/include/pool.h
+++ b/src/include/pool.h
@@ -6,7 +6,7 @@
* pgpool: a language independent connection pool server for PostgreSQL
* written by Tatsuo Ishii
*
- * Copyright (c) 2003-2014 PgPool Global Development Group
+ * Copyright (c) 2003-2015 PgPool Global Development Group
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby
@@ -716,6 +716,6 @@ extern bool pcp_mark_recovery_in_progress(void);
extern int pool_send_to_frontend(char* data, int len, bool flush);
extern int pool_frontend_exists(void);
extern pid_t pool_waitpid(int *status);
-
+extern int write_status_file(void);
#endif /* POOL_H */
diff --git a/src/main/pgpool_main.c b/src/main/pgpool_main.c
index fb6463fa1..402389dcc 100644
--- a/src/main/pgpool_main.c
+++ b/src/main/pgpool_main.c
@@ -97,7 +97,6 @@ static int process_backend_health_check_failure(int health_check_node_id, int re
static bool do_health_check(bool use_template_db, volatile int *health_check_node_id);
static void FileUnlink(int code, Datum path);
-static int write_status_file();
static pid_t pcp_fork_a_child(int unix_fd, int inet_fd, char *pcp_conf_file);
static pid_t fork_a_child(int *fds, int id);
static pid_t worker_fork_a_child(void);
@@ -400,6 +399,9 @@ int PgpoolMain(bool discard_status, bool clear_memcache_oidmaps)
}
/* We can now handle ereport(ERROR) */
PG_exception_stack = &local_sigjmp_buf;
+
+ /* Create or write status file */
+ (void)write_status_file();
/* This is the main loop */
for (;;)
@@ -1463,6 +1465,7 @@ static void failover(void)
BACKEND_INFO(node_id).backend_port)));
BACKEND_INFO(node_id).backend_status = CON_CONNECT_WAIT; /* unset down status */
+ (void)write_status_file();
/* wait for failback command lock or to be lock holder */
if (pool_config->use_watchdog && !wd_am_I_lock_holder())
@@ -1517,6 +1520,8 @@ static void failover(void)
BACKEND_INFO(node_id_set[i]).backend_port)));
BACKEND_INFO(node_id_set[i]).backend_status = CON_DOWN; /* set down status */
+ (void)write_status_file();
+
/* save down node */
nodes[node_id_set[i]] = 1;
cnt++;
@@ -1715,6 +1720,8 @@ static void failover(void)
bkinfo->backend_hostname,
bkinfo->backend_port)));
bkinfo->backend_status = CON_DOWN; /* set down status */
+ (void)write_status_file();
+
follow_cnt++;
}
}
@@ -2830,6 +2837,7 @@ static int read_status_file(bool discard_status)
if (backend_rec.status[i] == CON_DOWN)
{
BACKEND_INFO(i).backend_status = CON_DOWN;
+ (void)write_status_file();
ereport(LOG,
(errmsg("read_status_file: %d th backend is set to down status", i)));
}
@@ -2837,6 +2845,7 @@ static int read_status_file(bool discard_status)
BACKEND_INFO(i).backend_status == CON_UP)
{
BACKEND_INFO(i).backend_status = CON_CONNECT_WAIT;
+ (void)write_status_file();
someone_wakeup = true;
}
else
@@ -2919,15 +2928,16 @@ static int read_status_file(bool discard_status)
{
BACKEND_INFO(i).backend_status = CON_CONNECT_WAIT;
}
+ (void)write_status_file();
}
return 0;
}
/*
-* Write the pid file
+* Write the status file
*/
-static int write_status_file()
+int write_status_file()
{
FILE *fd;
char fnamebuf[POOLMAXPATHLEN];
@@ -2990,6 +3000,10 @@ static void reload_config(void)
(errmsg("reload config files.")));
MemoryContext oldContext = MemoryContextSwitchTo(TopMemoryContext);
pool_get_config(conf_file, RELOAD_CONFIG);
+
+ /* Realoading config file could change backend status */
+ (void)write_status_file();
+
MemoryContextSwitchTo(oldContext);
if (pool_config->enable_pool_hba)
load_hba(hba_file);
@@ -3024,7 +3038,7 @@ static void system_will_go_down(int code, Datum arg)
}
POOL_SETMASK(&AuthBlockSig);
/* Write status file */
- write_status_file();
+ (void)write_status_file();
/*
* Terminate all childrens. But we may already have killed
* all the childrens if we come to this function because of shutdown
diff --git a/src/protocol/pool_connection_pool.c b/src/protocol/pool_connection_pool.c
index 508686c45..adde02f29 100644
--- a/src/protocol/pool_connection_pool.c
+++ b/src/protocol/pool_connection_pool.c
@@ -867,6 +867,8 @@ static POOL_CONNECTION_POOL *new_connection(POOL_CONNECTION_POOL *p)
active_backend_count++;
}
+ (void)write_status_file();
+
MemoryContextSwitchTo(oldContext);
if (active_backend_count > 0)
[parent: 363a296c15a1]