pgpool2.git / summary / log / commit / refs

commit    78240da96f4a975907cebada8be236387d6e272b
Author:   Tatsuo Ishii <ishii at sraoss.co.jp>
Date:     Sun Dec 06 12:55:08 2009 +0000

    Restore backend status by using pgpool_status file.


main.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ pool.h | 3 +++ pool_type.h | 7 ++++++ 3 files changed, 92 insertions(+) diff --git a/main.c b/main.c index ab047ee35..da75fc9bb 100644 --- a/main.c +++ b/main.c @@ -84,6 +84,8 @@ static void daemonize(void); static int read_pid_file(void); static void write_pid_file(void); +static int read_status_file(void); +static int write_statusd_file(void); static pid_t pcp_fork_a_child(int unix_fd, int inet_fd, char *pcp_conf_file); static pid_t fork_a_child(int unix_fd, int inet_fd, int id); static int create_unix_domain_socket(struct sockaddr_un un_addr_tmp); @@ -163,6 +165,8 @@ static int pipe_fds[2]; /* for delivering signals */ int my_proc_id; +static BackendStatusRecord backend_rec; /* Backend status record */ + int myargc; char **myargv; @@ -356,6 +360,11 @@ int main(int argc, char **argv) exit(1); } + /* + * Restore previous backend status if possible + */ + read_status_file(); + /* clear cache */ if (clear_cache && pool_config->enable_query_cache && SYSDB_STATUS == CON_UP) { @@ -773,6 +782,77 @@ static void write_pid_file(void) } } +/* +* Read the status file +*/ +static int read_status_file(void) +{ + FILE *fd; + char fnamebuf[POOLMAXPATHLEN]; + int i; + + snprintf(fnamebuf, sizeof(fnamebuf), "%s/%s", pool_config->logdir, STATUS_FILE_NAME); + fd = fopen(fnamebuf, "r"); + if (!fd) + { + pool_log("Backend status file %s does not exist", fnamebuf); + return -1; + } + if (fread(&backend_rec, 1, sizeof(backend_rec), fd) <= 0) + { + pool_error("Could not read backend status file as %s. reason: %s", + fnamebuf, strerror(errno)); + fclose(fd); + return -1; + } + fclose(fd); + + for (i=0;i< pool_config->backend_desc->num_backends;i++) + { + if (backend_rec.status[i] == CON_DOWN) + BACKEND_INFO(i).backend_status = CON_DOWN; + else + BACKEND_INFO(i).backend_status = CON_CONNECT_WAIT; + } + + return 0; +} + +/* +* Write the pid file +*/ +static int write_status_file(void) +{ + FILE *fd; + char fnamebuf[POOLMAXPATHLEN]; + int i; + + snprintf(fnamebuf, sizeof(fnamebuf), "%s/%s", pool_config->logdir, STATUS_FILE_NAME); + fd = fopen(fnamebuf, "w"); + if (!fd) + { + pool_error("Could not open status file %s", fnamebuf); + return -1; + } + + memset(&backend_rec, 0, sizeof(backend_rec)); + + for (i=0;i< pool_config->backend_desc->num_backends;i++) + { + backend_rec.status[i] = BACKEND_INFO(i).backend_status; + } + + if (fwrite(&backend_rec, 1, sizeof(backend_rec), fd) <= 0) + { + pool_error("Could not write backend status file as %s. reason: %s", + fnamebuf, strerror(errno)); + fclose(fd); + return -1; + } + fclose(fd); + return 0; +} + /* * fork a child for PCP */ @@ -993,6 +1073,8 @@ static void myexit(int code) myunlink(pcp_un_addr.sun_path); myunlink(pool_config->pid_file_name); + write_status_file(); + pool_shmem_exit(code); exit(code); } diff --git a/pool.h b/pool.h index bddbe6909..8e93b7640 100644 --- a/pool.h +++ b/pool.h @@ -58,6 +58,9 @@ /* pid file name */ #define DEFAULT_PID_FILE_NAME "/var/run/pgpool/pgpool.pid" +/* status file name */ +#define STATUS_FILE_NAME "pgpool_status" + typedef enum { POOL_CONTINUE = 0, POOL_IDLE, diff --git a/pool_type.h b/pool_type.h index b3092a24b..512f89b47 100644 --- a/pool_type.h +++ b/pool_type.h @@ -189,6 +189,13 @@ typedef struct { BACKEND_STATUS system_db_status; } SystemDBInfo; +/* + * Backend status record file + */ +typedef struct { + BACKEND_STATUS status[MAX_NUM_BACKENDS]; +} BackendStatusRecord; + /* * It seems that sockaddr_storage is now commonly used in place of sockaddr. * So, define it if it is not define yet, and create new SockAddr structure [parent: f9ec1067dbf9]