Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Restore backend status by using pgpool_status file.

  • Loading branch information...
commit 90e33772e368e7b0c2b6d48b74170734e31fd920 1 parent d7ea587
t-ishii authored
Showing with 95 additions and 3 deletions.
  1. +83 −1 main.c
  2. +4 −1 pool.h
  3. +8 −1 pool_type.h
View
84 main.c
@@ -1,6 +1,6 @@
/* -*-pgsql-c-*- */
/*
- * $Header: /cvsroot/pgpool/pgpool-II/main.c,v 1.55 2009/11/14 11:36:35 t-ishii Exp $
+ * $Header: /cvsroot/pgpool/pgpool-II/main.c,v 1.56 2009/12/06 12:55:08 t-ishii Exp $
*
* pgpool: a language independent connection pool server for PostgreSQL
* written by Tatsuo Ishii
@@ -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)
{
@@ -774,6 +783,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
*/
pid_t pcp_fork_a_child(int unix_fd, int inet_fd, char *pcp_conf_file)
@@ -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);
}
View
5 pool.h
@@ -1,7 +1,7 @@
/* -*-pgsql-c-*- */
/*
*
- * $Header: /cvsroot/pgpool/pgpool-II/pool.h,v 1.47 2009/12/06 08:46:34 t-ishii Exp $
+ * $Header: /cvsroot/pgpool/pgpool-II/pool.h,v 1.48 2009/12/06 12:55:08 t-ishii Exp $
*
* pgpool: a language independent connection pool server for PostgreSQL
* written by Tatsuo Ishii
@@ -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,
View
9 pool_type.h
@@ -1,7 +1,7 @@
/* -*-pgsql-c-*- */
/*
*
- * $Header: /cvsroot/pgpool/pgpool-II/pool_type.h,v 1.10 2009/07/30 14:44:21 t-ishii Exp $
+ * $Header: /cvsroot/pgpool/pgpool-II/pool_type.h,v 1.11 2009/12/06 12:55:08 t-ishii Exp $
*
* pgpool: a language independent connection pool server for PostgreSQL
* written by Tatsuo Ishii
@@ -190,6 +190,13 @@ typedef struct {
} 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
* that uses sockaddr_storage.
Please sign in to comment.
Something went wrong with that request. Please try again.