diff options
Diffstat (limited to 'src/main')
| -rw-r--r-- | src/main/health_check.c | 111 | ||||
| -rw-r--r-- | src/main/main.c | 11 | ||||
| -rw-r--r-- | src/main/pgpool_logger.c | 47 | ||||
| -rw-r--r-- | src/main/pgpool_main.c | 697 | ||||
| -rw-r--r-- | src/main/pool_globals.c | 41 | ||||
| -rw-r--r-- | src/main/pool_internal_comms.c | 5 |
6 files changed, 479 insertions, 433 deletions
diff --git a/src/main/health_check.c b/src/main/health_check.c index 9111d3bf9..0c72c0a72 100644 --- a/src/main/health_check.c +++ b/src/main/health_check.c @@ -68,9 +68,10 @@ #include "auth/md5.h" #include "auth/pool_hba.h" -volatile POOL_HEALTH_CHECK_STATISTICS *health_check_stats; /* health check stats area in shared memory */ +volatile POOL_HEALTH_CHECK_STATISTICS *health_check_stats; /* health check stats + * area in shared memory */ -static POOL_CONNECTION_POOL_SLOT * slot; +static POOL_CONNECTION_POOL_SLOT *slot; static volatile sig_atomic_t reload_config_request = 0; static volatile sig_atomic_t restart_request = 0; volatile POOL_HEALTH_CHECK_STATISTICS *stats; @@ -108,27 +109,30 @@ static bool check_backend_down_request(int node, bool done_requests); * health check child main loop */ void -do_health_check_child(int *node_id) +do_health_check_child(void *params) { sigjmp_buf local_sigjmp_buf; MemoryContext HealthCheckMemoryContext; char psbuffer[NI_MAXHOST]; - static struct timeval start_time; - static struct timeval end_time; - long diff_t; + static struct timeval start_time; + static struct timeval end_time; + long diff_t; + int node_id; + + POOL_HEALTH_CHECK_STATISTICS mystat; - POOL_HEALTH_CHECK_STATISTICS mystat; - stats = &health_check_stats[*node_id]; + node_id = *((int *) params); + stats = &health_check_stats[node_id]; /* Set application name */ - set_application_name_with_suffix(PT_HEALTH_CHECK, *node_id); + set_application_name_with_suffix(PT_HEALTH_CHECK, node_id); ereport(DEBUG1, - (errmsg("I am health check process pid:%d DB node id:%d", getpid(), *node_id))); + (errmsg("I am health check process pid:%d DB node id:%d", getpid(), node_id))); /* Identify myself via ps */ init_ps_display("", "", "", ""); - snprintf(psbuffer, sizeof(psbuffer), "health check process(%d)", *node_id); + snprintf(psbuffer, sizeof(psbuffer), "health check process(%d)", node_id); set_ps_display(psbuffer, false); /* set up signal handlers */ @@ -180,17 +184,18 @@ do_health_check_child(int *node_id) { MemoryContextSwitchTo(HealthCheckMemoryContext); MemoryContextResetAndDeleteChildren(HealthCheckMemoryContext); + /* - * Since HealthCheckMemoryContext is used for "slot", we need to clear it - * so that new slot is allocated later on. + * Since HealthCheckMemoryContext is used for "slot", we need to clear + * it so that new slot is allocated later on. */ slot = NULL; - bool skipped = false; + bool skipped = false; CHECK_REQUEST; - if (pool_config->health_check_params[*node_id].health_check_period <= 0) + if (pool_config->health_check_params[node_id].health_check_period <= 0) { stats->min_health_check_duration = 0; sleep(30); @@ -200,27 +205,27 @@ do_health_check_child(int *node_id) * If health checking is enabled and the node is not in down status, * do health check. */ - else if (pool_config->health_check_params[*node_id].health_check_period > 0) + else if (pool_config->health_check_params[node_id].health_check_period > 0) { bool result; - BackendInfo *bkinfo = pool_get_node_info(*node_id); + BackendInfo *bkinfo = pool_get_node_info(node_id); stats->total_count++; gettimeofday(&start_time, NULL); stats->last_health_check = time(NULL); - result = establish_persistent_connection(*node_id); + result = establish_persistent_connection(node_id); if (result && slot == NULL) { stats->last_failed_health_check = time(NULL); - if (POOL_DISALLOW_TO_FAILOVER(BACKEND_INFO(*node_id).flag)) + if (POOL_DISALLOW_TO_FAILOVER(BACKEND_INFO(node_id).flag)) { ereport(LOG, (errmsg("health check failed on node %d but failover is disallowed for the node", - *node_id))); + node_id))); } else { @@ -229,19 +234,19 @@ do_health_check_child(int *node_id) stats->fail_count++; ereport(LOG, (errmsg("health check failed on node %d (timeout:%d)", - *node_id, health_check_timer_expired))); + node_id, health_check_timer_expired))); if (bkinfo->backend_status == CON_DOWN && bkinfo->quarantine == true) { ereport(LOG, (errmsg("health check failed on quarantine node %d (timeout:%d)", - *node_id, health_check_timer_expired), + node_id, health_check_timer_expired), errdetail("ignoring.."))); } else { /* trigger failover */ partial = health_check_timer_expired ? false : true; - degenerate_backend_set(node_id, 1, partial ? REQ_DETAIL_SWITCHOVER : 0); + degenerate_backend_set(&node_id, 1, partial ? REQ_DETAIL_SWITCHOVER : 0); } } } @@ -250,10 +255,11 @@ do_health_check_child(int *node_id) stats->success_count++; stats->last_successful_health_check = time(NULL); - /* The node has become reachable again. Reset - * the quarantine state + /* + * The node has become reachable again. Reset the quarantine + * state */ - send_failback_request(*node_id, false, REQ_DETAIL_UPDATE | REQ_DETAIL_WATCHDOG); + send_failback_request(node_id, false, REQ_DETAIL_UPDATE | REQ_DETAIL_WATCHDOG); } else if (result && slot) { @@ -270,12 +276,12 @@ do_health_check_child(int *node_id) } /* Discard persistent connections */ - discard_persistent_connection(*node_id); + discard_persistent_connection(node_id); /* - Update health check duration only if health check was not skipped - since the duration could be very small (probably 0) if health - check is skipped. + * Update health check duration only if health check was not + * skipped since the duration could be very small (probably 0) if + * health check is skipped. */ if (!skipped) @@ -296,9 +302,9 @@ do_health_check_child(int *node_id) stats->min_health_check_duration = diff_t; } - memcpy(&mystat, (void *)stats, sizeof(mystat)); + memcpy(&mystat, (void *) stats, sizeof(mystat)); - sleep(pool_config->health_check_params[*node_id].health_check_period); + sleep(pool_config->health_check_params[node_id].health_check_period); } } exit(0); @@ -313,18 +319,19 @@ establish_persistent_connection(int node) { BackendInfo *bkinfo; int retry_cnt; - static time_t auto_failback_interval = 0; /* resume time of auto_failback */ + static time_t auto_failback_interval = 0; /* resume time of + * auto_failback */ bool check_failback = false; time_t now; - char *dbname; + char *dbname; bkinfo = pool_get_node_info(node); /* - * If the node is already in down status or unused, do nothing. - * except when the node state is down because of quarantine operation - * since we want to detect when the node comes back to life again to - * remove it from the quarantine state + * If the node is already in down status or unused, do nothing. except + * when the node state is down because of quarantine operation since we + * want to detect when the node comes back to life again to remove it from + * the quarantine state */ if (bkinfo->backend_status == CON_UNUSED || (bkinfo->backend_status == CON_DOWN && bkinfo->quarantine == false)) @@ -335,9 +342,9 @@ establish_persistent_connection(int node) if (pool_config->auto_failback && auto_failback_interval < now && STREAM && !strcmp(bkinfo->replication_state, "streaming") && !Req_info->switching) { - ereport(DEBUG1, - (errmsg("health check DB node: %d (status:%d) for auto_failback", node, bkinfo->backend_status))); - check_failback = true; + ereport(DEBUG1, + (errmsg("health check DB node: %d (status:%d) for auto_failback", node, bkinfo->backend_status))); + check_failback = true; } else return false; @@ -432,7 +439,7 @@ establish_persistent_connection(int node) if (retry_cnt != pool_config->health_check_params[node].health_check_max_retries) { - int ret_cnt; + int ret_cnt; retry_cnt++; ret_cnt = pool_config->health_check_params[node].health_check_max_retries - retry_cnt; @@ -446,13 +453,13 @@ establish_persistent_connection(int node) if (check_failback && !Req_info->switching && slot) { - ereport(LOG, + ereport(LOG, (errmsg("request auto failback, node id:%d", node))); - /* get current time to use auto_failback_interval */ - now = time(NULL); - auto_failback_interval = now + pool_config->auto_failback_interval; + /* get current time to use auto_failback_interval */ + now = time(NULL); + auto_failback_interval = now + pool_config->auto_failback_interval; - send_failback_request(node, true, REQ_DETAIL_CONFIRMED); + send_failback_request(node, true, REQ_DETAIL_CONFIRMED); } } @@ -553,9 +560,9 @@ static RETSIGTYPE health_check_timer_handler(int sig) size_t health_check_stats_shared_memory_size(void) { - size_t size; + size_t size; - size = MAXALIGN(sizeof(POOL_HEALTH_CHECK_STATISTICS) * MAX_NUM_BACKENDS); + size = MAXALIGN(sizeof(POOL_HEALTH_CHECK_STATISTICS) * MAX_NUM_BACKENDS); elog(DEBUG1, "health_check_stats_shared_memory_size: requested size: %lu", size); return size; } @@ -566,12 +573,12 @@ health_check_stats_shared_memory_size(void) void health_check_stats_init(POOL_HEALTH_CHECK_STATISTICS *addr) { - int i; + int i; health_check_stats = addr; memset((void *) health_check_stats, 0, health_check_stats_shared_memory_size()); - for (i = 0 ;i < MAX_NUM_BACKENDS; i++) + for (i = 0; i < MAX_NUM_BACKENDS; i++) { health_check_stats[i].min_health_check_duration = INT_MAX; } @@ -613,7 +620,7 @@ check_backend_down_request(int node, bool done_requests) if (backend_down_request_file[0] == '\0') { snprintf(backend_down_request_file, sizeof(backend_down_request_file), - "%s/%s", pool_config->logdir, BACKEND_DOWN_REQUEST_FILE); + "%s/%s", pool_config->work_dir, BACKEND_DOWN_REQUEST_FILE); } fd = fopen(backend_down_request_file, "r"); diff --git a/src/main/main.c b/src/main/main.c index 5d586829f..52a2b81ea 100644 --- a/src/main/main.c +++ b/src/main/main.c @@ -355,7 +355,7 @@ main(int argc, char **argv) } else strlcpy(pool_passwd, pool_config->pool_passwd, - sizeof(pool_passwd)); + sizeof(pool_passwd)); pool_init_pool_passwd(pool_passwd, POOL_PASSWD_R); } @@ -534,7 +534,7 @@ stop_me(void) for (;;) { - int cnt = 5; /* sending signal retry interval */ + int cnt = 5; /* sending signal retry interval */ if (kill(pid, stop_sig) == -1) { @@ -550,8 +550,11 @@ stop_me(void) fprintf(stderr, "."); sleep(1); cnt--; - /* If pgpool did not stop within 5 seconds, break the loop and try - * to send the signal again */ + + /* + * If pgpool did not stop within 5 seconds, break the loop and try + * to send the signal again + */ if (cnt <= 0) break; } diff --git a/src/main/pgpool_logger.c b/src/main/pgpool_logger.c index 206086732..d57a0d000 100644 --- a/src/main/pgpool_logger.c +++ b/src/main/pgpool_logger.c @@ -50,7 +50,8 @@ #include "main/pgpool_logger.h" #define DEVNULL "/dev/null" -typedef int64 pg_time_t; +typedef time_t pg_time_t; + /* * We read() into a temp buffer twice as big as a chunk, so that any fragment * left after processing can be moved down to the front and we'll still have @@ -68,7 +69,7 @@ typedef int64 pg_time_t; */ -bool redirection_done = false; +bool redirection_done = false; /* * Private state @@ -112,7 +113,7 @@ static volatile sig_atomic_t got_SIGHUP = false; static volatile sig_atomic_t rotation_requested = false; -static void SysLoggerMain(int argc, char *argv[]) pg_attribute_noreturn(); +pg_noreturn static void SysLoggerMain(int argc, char *argv[]); static void process_pipe_input(char *logbuffer, int *bytes_in_logbuffer); static void flush_pipe_input(char *logbuffer, int *bytes_in_logbuffer); static FILE *logfile_open(const char *filename, const char *mode, @@ -262,8 +263,8 @@ SysLoggerMain(int argc, char *argv[]) /* * Check if the log directory or filename pattern changed in - * pgpool.conf. If so, force rotation to make sure we're - * writing the logfiles in the right place. + * pgpool.conf. If so, force rotation to make sure we're writing + * the logfiles in the right place. */ if (strcmp(pool_config->log_directory, currentLogDir) != 0) { @@ -360,9 +361,9 @@ SysLoggerMain(int argc, char *argv[]) * next_rotation_time. * * Also note that we need to beware of overflow in calculation of the - * timeout: with large settings of pool_config->log_rotation_age, next_rotation_time - * could be more than INT_MAX msec in the future. In that case we'll - * wait no more than INT_MAX msec, and try again. + * timeout: with large settings of pool_config->log_rotation_age, + * next_rotation_time could be more than INT_MAX msec in the future. + * In that case we'll wait no more than INT_MAX msec, and try again. */ timeout.tv_sec = 0; /* Reset usec everytime before calling sellect */ @@ -384,10 +385,10 @@ SysLoggerMain(int argc, char *argv[]) /* * Sleep until there's something to do */ - + FD_ZERO(&rfds); FD_SET(syslogPipe[0], &rfds); - rc = select(syslogPipe[0] + 1, &rfds, NULL, NULL, timeout.tv_sec?&timeout:NULL); + rc = select(syslogPipe[0] + 1, &rfds, NULL, NULL, timeout.tv_sec ? &timeout : NULL); if (rc == 1) { int bytesRead; @@ -487,8 +488,8 @@ SysLogger_Start(void) /* * The initial logfile is created right in the postmaster, to verify that - * the pool_config->log_directory is writable. We save the reference time so that the - * syslogger child process can recompute this file name. + * the pool_config->log_directory is writable. We save the reference time + * so that the syslogger child process can recompute this file name. * * It might look a bit strange to re-do this during a syslogger restart, * but we must do so since the postmaster closed syslogFile after the @@ -883,8 +884,8 @@ logfile_open(const char *filename, const char *mode, bool allow_errors) mode_t oumask; /* - * Note we do not let pool_config->log_file_mode disable IWUSR, since we certainly want - * to be able to write the files ourselves. + * Note we do not let pool_config->log_file_mode disable IWUSR, since we + * certainly want to be able to write the files ourselves. */ oumask = umask((mode_t) ((~(pool_config->log_file_mode | S_IWUSR)) & (S_IRWXU | S_IRWXG | S_IRWXO))); fh = fopen(filename, mode); @@ -940,9 +941,9 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for) /* * Decide whether to overwrite or append. We can overwrite if (a) - * pool_config->log_truncate_on_rotation is set, (b) the rotation was triggered by - * elapsed time and not something else, and (c) the computed file name is - * different from what we were previously logging into. + * pool_config->log_truncate_on_rotation is set, (b) the rotation was + * triggered by elapsed time and not something else, and (c) the computed + * file name is different from what we were previously logging into. * * Note: last_file_name should never be NULL here, but if it is, append. */ @@ -960,8 +961,8 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for) /* * ENFILE/EMFILE are not too surprising on a busy system; just * keep using the old file till we manage to get a new one. - * Otherwise, assume something's wrong with pool_config->log_directory and stop - * trying to create files. + * Otherwise, assume something's wrong with + * pool_config->log_directory and stop trying to create files. */ if (errno != ENFILE && errno != EMFILE) { @@ -1010,8 +1011,8 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for) /* * ENFILE/EMFILE are not too surprising on a busy system; just * keep using the old file till we manage to get a new one. - * Otherwise, assume something's wrong with pool_config->log_directory and stop - * trying to create files. + * Otherwise, assume something's wrong with + * pool_config->log_directory and stop trying to create files. */ if (errno != ENFILE && errno != EMFILE) { @@ -1079,7 +1080,7 @@ logfile_getname(pg_time_t timestamp, const char *suffix) /* treat pool_config->log_filename as a strftime pattern */ strftime(filename + len, MAXPGPATH - len, pool_config->log_filename, - localtime(×tamp)); + localtime(×tamp)); if (suffix != NULL) { @@ -1099,7 +1100,7 @@ static void set_next_rotation_time(void) { pg_time_t now; - struct tm *tm; + struct tm *tm; int rotinterval; /* nothing to do if time-based rotation is disabled */ diff --git a/src/main/pgpool_main.c b/src/main/pgpool_main.c index e7ffdd8bf..4d88c5815 100644 --- a/src/main/pgpool_main.c +++ b/src/main/pgpool_main.c @@ -5,7 +5,7 @@ * pgpool: a language independent connection pool server for PostgreSQL * written by Tatsuo Ishii * - * Copyright (c) 2003-2024 PgPool Global Development Group + * Copyright (c) 2003-2025 PgPool Global Development Group * * Permission to use, copy, modify, and distribute this software and * its documentation for any purpose and without fee is hereby @@ -75,16 +75,16 @@ typedef enum * required */ SIG_WATCHDOG_QUORUM_CHANGED, /* notify main about cluster quorum change * of watchdog cluster */ - SIG_INFORM_QUARANTINE_NODES, /* notify main about send degenerate requests - * for all quarantine nodes */ + SIG_INFORM_QUARANTINE_NODES, /* notify main about send degenerate + * requests for all quarantine nodes */ MAX_INTERRUPTS /* Must be last! */ -} User1SignalReason; +} User1SignalReason; typedef struct User1SignalSlot { sig_atomic_t signalFlags[MAX_INTERRUPTS]; -} User1SignalSlot; +} User1SignalSlot; #ifdef NOT_USED /* @@ -128,12 +128,16 @@ typedef struct User1SignalSlot typedef struct { bool all_backend_down; /* true if all backends are down */ - bool search_primary; /* true if we need to seach primary node */ - bool need_to_restart_children; /* true if we need to restart child process */ - bool need_to_restart_pcp; /* true if we need to restart pc process */ + bool search_primary; /* true if we need to seach primary node */ + bool need_to_restart_children; /* true if we need to restart + * child process */ + bool need_to_restart_pcp; /* true if we need to restart pc + * process */ bool partial_restart; /* true if partial restart is needed */ - bool sync_required; /* true if watchdog synchronization is necessary */ + bool sync_required; /* true if watchdog synchronization is + * necessary */ + /* followings are copy of Req_info */ POOL_REQUEST_KIND reqkind; int node_id_set[MAX_NUM_BACKENDS]; int node_count; @@ -142,7 +146,7 @@ typedef struct /* * An array to hold down nodes information. Each array member corresponds * to node id. If nodes[i] is 1, the node i is down. - */ + */ int nodes[MAX_NUM_BACKENDS]; } FAILOVER_CONTEXT; @@ -151,7 +155,7 @@ static void signal_user1_to_parent_with_reason(User1SignalReason reason); static void FileUnlink(int code, Datum path); static pid_t pcp_fork_a_child(int *fds, char *pcp_conf_file); static pid_t fork_a_child(int *fds, int id); -static pid_t worker_fork_a_child(ProcessType type, void (*func) (), void *params); +static pid_t worker_fork_a_child(ProcessType type, void (*func) (void *), void *params); static int create_unix_domain_socket(struct sockaddr_un un_addr_tmp, const char *group, const int permissions); static int *create_unix_domain_sockets_by_list(struct sockaddr_un *un_addrs, char *group, int permissions, int n_sockets); static int *create_inet_domain_sockets(const char *hostname, const int port); @@ -173,8 +177,8 @@ static RETSIGTYPE reload_config_handler(int sig); static RETSIGTYPE wakeup_handler(int sig); static void initialize_shared_mem_objects(bool clear_memcache_oidmaps); -static int trigger_failover_command(int node, const char *command_line, - int old_main_node, int new_main_node, int old_primary); +static int trigger_failover_command(int node, const char *command_line, + int old_main_node, int new_main_node, int old_primary); static int find_primary_node(void); static int find_primary_node_repeatedly(void); static void terminate_all_childrens(int sig); @@ -182,18 +186,18 @@ static void system_will_go_down(int code, Datum arg); static char *process_name_from_pid(pid_t pid); static void sync_backend_from_watchdog(void); static void update_backend_quarantine_status(void); -static int get_server_version(POOL_CONNECTION_POOL_SLOT * *slots, int node_id); +static int get_server_version(POOL_CONNECTION_POOL_SLOT **slots, int node_id); static void get_info_from_conninfo(char *conninfo, char *host, int hostlen, char *port, int portlen); /* * Subroutines of failover() */ -static int handle_failback_request(FAILOVER_CONTEXT *failover_context, int node_id); -static int handle_failover_request(FAILOVER_CONTEXT *failover_context, int node_id); +static int handle_failback_request(FAILOVER_CONTEXT *failover_context, int node_id); +static int handle_failover_request(FAILOVER_CONTEXT *failover_context, int node_id); static void kill_failover_children(FAILOVER_CONTEXT *failover_context, int node_id); static void exec_failover_command(FAILOVER_CONTEXT *failover_context, int new_main_node_id, int promote_node_id); -static int determine_new_primary_node(FAILOVER_CONTEXT *failover_context, int node_id); -static int exec_follow_primary_command(FAILOVER_CONTEXT *failover_context, int node_id, int new_primary_node_id); +static int determine_new_primary_node(FAILOVER_CONTEXT *failover_context, int node_id); +static int exec_follow_primary_command(FAILOVER_CONTEXT *failover_context, int node_id, int new_primary_node_id); static void save_node_info(FAILOVER_CONTEXT *failover_context, int new_primary_node_id, int new_main_node_id); static void exec_child_restart(FAILOVER_CONTEXT *failover_context, int node_id); static void exec_notice_pcp_child(FAILOVER_CONTEXT *failover_context); @@ -201,14 +205,14 @@ static void exec_notice_pcp_child(FAILOVER_CONTEXT *failover_context); static void check_requests(void); static void print_signal_member(sigset_t *sig); static void service_child_processes(void); -static int select_victim_processes(int *process_info_idxs, int count); +static int select_victim_processes(int *process_info_idxs, int count); static struct sockaddr_un *un_addrs; /* unix domain socket path */ static struct sockaddr_un *pcp_un_addrs; /* unix domain socket path for PCP */ ProcessInfo *process_info = NULL; /* Per child info table on shmem */ -volatile User1SignalSlot *user1SignalSlot = NULL; /* User 1 signal slot on - * shmem */ -int current_child_process_count; +volatile User1SignalSlot *user1SignalSlot = NULL; /* User 1 signal slot on + * shmem */ +int current_child_process_count; /* * To track health check process ids @@ -227,18 +231,19 @@ BACKEND_STATUS private_backend_status[MAX_NUM_BACKENDS]; */ ConnectionInfo *con_info; -static int *fds = NULL; /* listening file descriptors (UNIX socket, +static int *fds = NULL; /* listening file descriptors (UNIX socket, * inet domain sockets) */ -static int *pcp_fds = NULL; /* listening file descriptors for pcp (UNIX socket, - * inet domain sockets) */ +static int *pcp_fds = NULL; /* listening file descriptors for pcp (UNIX + * socket, inet domain sockets) */ extern char *pcp_conf_file; /* path for pcp.conf */ extern char *conf_file; extern char *hba_file; -static volatile sig_atomic_t exiting = 0; /* non 0 if I'm exiting */ -static volatile sig_atomic_t switching = 0; /* non 0 if I'm failing over or degenerating */ +static volatile sig_atomic_t exiting = 0; /* non 0 if I'm exiting */ +static volatile sig_atomic_t switching = 0; /* non 0 if I'm failing over or + * degenerating */ POOL_REQUEST_INFO *Req_info; /* request info area in shared memory */ volatile sig_atomic_t *InRecovery; /* non 0 if recovery is started */ @@ -269,7 +274,7 @@ int my_main_node_id; /* Main node id buffer */ * Dummy variable to suppress compiler warnings by discarding return values * from write(2) in signal handlers */ -static int dummy_status; +static int dummy_status; /* * Snapshot Isolation manage area @@ -286,10 +291,10 @@ PgpoolMain(bool discard_status, bool clear_memcache_oidmaps) int num_inet_fds = 0; int num_unix_fds = 0; int num_pcp_fds = 0; - int *unix_fds; - int *inet_fds; - int *pcp_unix_fds; - int *pcp_inet_fds; + int *unix_fds; + int *inet_fds; + int *pcp_unix_fds; + int *pcp_inet_fds; int i; char unix_domain_socket_path[UNIXSOCK_PATH_BUFLEN + 1024]; @@ -301,6 +306,12 @@ PgpoolMain(bool discard_status, bool clear_memcache_oidmaps) */ volatile bool first = true; + /* + * Query cache lock file path. This should be declared as "static" because + * the path is passed to be registered using on_proc_exit(). + */ + static char query_cache_lock_path[MAXPGPATH]; + processState = INITIALIZING; /* @@ -318,15 +329,15 @@ PgpoolMain(bool discard_status, bool clear_memcache_oidmaps) if (strlen(unix_domain_socket_path) >= UNIXSOCK_PATH_BUFLEN) { ereport(WARNING, - (errmsg("Unix-domain socket path \"%s\" is too long (maximum %d bytes)", - unix_domain_socket_path, - (int) (UNIXSOCK_PATH_BUFLEN - 1)))); + (errmsg("Unix-domain socket path \"%s\" is too long (maximum %d bytes)", + unix_domain_socket_path, + (int) (UNIXSOCK_PATH_BUFLEN - 1)))); continue; } un_addrs = realloc(un_addrs, sizeof(struct sockaddr_un) * (num_unix_fds + 1)); if (un_addrs == NULL) ereport(FATAL, - (errmsg("failed to allocate memory in startup process"))); + (errmsg("failed to allocate memory in startup process"))); snprintf(un_addrs[i].sun_path, sizeof(un_addrs[i].sun_path), "%s", unix_domain_socket_path); num_unix_fds++; @@ -335,7 +346,7 @@ PgpoolMain(bool discard_status, bool clear_memcache_oidmaps) if (num_unix_fds == 0) { ereport(FATAL, - (errmsg("could not create any Unix-domain sockets"))); + (errmsg("could not create any Unix-domain sockets"))); } /* set unix domain socket path for pgpool PCP communication */ @@ -343,21 +354,21 @@ PgpoolMain(bool discard_status, bool clear_memcache_oidmaps) { memset(unix_domain_socket_path, 0, sizeof(unix_domain_socket_path)); snprintf(unix_domain_socket_path, sizeof(unix_domain_socket_path), "%s/.s.PGSQL.%d", - pool_config->pcp_socket_dir[i], - pool_config->pcp_port); + pool_config->pcp_socket_dir[i], + pool_config->pcp_port); if (strlen(unix_domain_socket_path) >= UNIXSOCK_PATH_BUFLEN) { ereport(WARNING, - (errmsg("PCP Unix-domain socket path \"%s\" is too long (maximum %d bytes)", - unix_domain_socket_path, - (int) (UNIXSOCK_PATH_BUFLEN - 1)))); + (errmsg("PCP Unix-domain socket path \"%s\" is too long (maximum %d bytes)", + unix_domain_socket_path, + (int) (UNIXSOCK_PATH_BUFLEN - 1)))); continue; } pcp_un_addrs = realloc(pcp_un_addrs, sizeof(struct sockaddr_un) * (num_pcp_fds + 1)); if (pcp_un_addrs == NULL) ereport(FATAL, - (errmsg("failed to allocate memory in startup process"))); + (errmsg("failed to allocate memory in startup process"))); snprintf(pcp_un_addrs[i].sun_path, sizeof(pcp_un_addrs[i].sun_path), "%s", unix_domain_socket_path); num_pcp_fds++; @@ -366,7 +377,7 @@ PgpoolMain(bool discard_status, bool clear_memcache_oidmaps) if (num_pcp_fds == 0) { ereport(FATAL, - (errmsg("could not create any PCP Unix-domain sockets"))); + (errmsg("could not create any PCP Unix-domain sockets"))); } /* set up signal handlers */ @@ -374,10 +385,11 @@ PgpoolMain(bool discard_status, bool clear_memcache_oidmaps) /* start the log collector if enabled */ pgpool_logger_pid = SysLogger_Start(); - /* - * If using syslogger, close the read side of the pipe. We don't bother - * tracking this in fd.c, either. - */ + + /* + * If using syslogger, close the read side of the pipe. We don't bother + * tracking this in fd.c, either. + */ if (syslogPipe[0] >= 0) close(syslogPipe[0]); syslogPipe[0] = -1; @@ -405,18 +417,18 @@ PgpoolMain(bool discard_status, bool clear_memcache_oidmaps) wakeup_request = 0; /* - * Watchdog process fires SIGUSR2 once in stable state - * In addition, when watchdog fails to start with FATAL, the process - * exits and SIGCHLD is fired, so we can also expect SIGCHLD from - * watchdog process. Finally, we also need to look for the SIGUSR1 - * signal for the failover requests from other watchdog nodes. In - * case a request arrives at the same time when the watchdog has just - * been initialized. + * Watchdog process fires SIGUSR2 once in stable state In addition, + * when watchdog fails to start with FATAL, the process exits and + * SIGCHLD is fired, so we can also expect SIGCHLD from watchdog + * process. Finally, we also need to look for the SIGUSR1 signal for + * the failover requests from other watchdog nodes. In case a request + * arrives at the same time when the watchdog has just been + * initialized. * - * So we need to wait until watchdog is in stable state so only - * wait for SIGUSR1, SIGCHLD, and signals those are necessary to make - * sure we respond to user requests of shutdown if it arrives while we - * are in waiting state. + * So we need to wait until watchdog is in stable state so only wait + * for SIGUSR1, SIGCHLD, and signals those are necessary to make sure + * we respond to user requests of shutdown if it arrives while we are + * in waiting state. * * Note that SIGUSR1 does not need to be in the wait signal list, * although it's signal handler is already installed, but even if the @@ -450,7 +462,7 @@ PgpoolMain(bool discard_status, bool clear_memcache_oidmaps) ereport(LOG, (errmsg("watchdog process is initialized"), - errdetail("watchdog messaging data version: %s",WD_MESSAGE_DATA_VERSION))); + errdetail("watchdog messaging data version: %s", WD_MESSAGE_DATA_VERSION))); /* * initialize the lifecheck process @@ -459,7 +471,8 @@ PgpoolMain(bool discard_status, bool clear_memcache_oidmaps) if (sigusr1_request) { - do { + do + { sigusr1_request = 0; sigusr1_interrupt_processor(); } while (sigusr1_request == 1); @@ -470,7 +483,7 @@ PgpoolMain(bool discard_status, bool clear_memcache_oidmaps) fds = malloc(sizeof(int) * (num_unix_fds + 1)); if (fds == NULL) ereport(FATAL, - (errmsg("failed to allocate memory in startup process"))); + (errmsg("failed to allocate memory in startup process"))); unix_fds = create_unix_domain_sockets_by_list(un_addrs, pool_config->unix_socket_group, @@ -497,7 +510,7 @@ PgpoolMain(bool discard_status, bool clear_memcache_oidmaps) fds = realloc(fds, sizeof(int) * (num_inet_fds + num_unix_fds + 1)); if (fds == NULL) ereport(FATAL, - (errmsg("failed to expand memory for fds"))); + (errmsg("failed to expand memory for fds"))); memcpy(&fds[num_unix_fds], inet_fds, sizeof(int) * num_inet_fds); fds[num_unix_fds + num_inet_fds] = -1; @@ -505,23 +518,23 @@ PgpoolMain(bool discard_status, bool clear_memcache_oidmaps) } /* For query cache concurrency control */ - if (pool_config->memory_cache_enabled) + if (pool_config->memory_cache_enabled || pool_config->enable_shared_relcache) { - char path[1024]; - int lfd; + int lfd; - snprintf(path, sizeof(path), "%s/QUERY_CACHE_LOCK_FILE", pool_config->logdir); - lfd = open(path, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR); + snprintf(query_cache_lock_path, sizeof(query_cache_lock_path), + "%s/%s", pool_config->work_dir, QUERY_CACHE_LOCK_FILE); + lfd = open(query_cache_lock_path, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR); if (lfd == -1) { ereport(FATAL, - (errmsg("Failed to open lock file for query cache \"%s\"", path), + (errmsg("Failed to open lock file for query cache \"%s\"", query_cache_lock_path), errdetail("%m"))); } close(lfd); /* Register file unlink at exit */ - on_proc_exit(FileUnlink, (Datum) path); + on_proc_exit(FileUnlink, (Datum) query_cache_lock_path); } /* @@ -542,7 +555,7 @@ PgpoolMain(bool discard_status, bool clear_memcache_oidmaps) else current_child_process_count = pool_config->num_init_children; ereport(DEBUG1, - (errmsg("Spawning %d child processes",current_child_process_count))); + (errmsg("Spawning %d child processes", current_child_process_count))); for (i = 0; i < current_child_process_count; i++) { process_info[i].start_time = time(NULL); @@ -601,7 +614,7 @@ PgpoolMain(bool discard_status, bool clear_memcache_oidmaps) pcp_fds = realloc(pcp_fds, sizeof(int) * (num_inet_fds + num_pcp_fds + 1)); if (pcp_fds == NULL) ereport(FATAL, - (errmsg("failed to expand memory for pcp_fds"))); + (errmsg("failed to expand memory for pcp_fds"))); memcpy(&pcp_fds[num_pcp_fds], pcp_inet_fds, sizeof(int) * num_inet_fds); pcp_fds[num_inet_fds + num_pcp_fds] = -1; @@ -645,6 +658,7 @@ PgpoolMain(bool discard_status, bool clear_memcache_oidmaps) #ifdef NOT_USED CHECK_REQUEST; #endif + /* * check for child signals to ensure child startup before reporting * successful start. @@ -871,7 +885,7 @@ fork_a_child(int *fds, int id) * fork worker child process */ static pid_t -worker_fork_a_child(ProcessType type, void (*func) (), void *params) +worker_fork_a_child(ProcessType type, void (*func) (void *), void *params) { pid_t pid; @@ -1108,11 +1122,11 @@ create_unix_domain_socket(struct sockaddr_un un_addr_tmp, const char *group, con if (*group != '\0') { - char *endptr; - gid_t gid; + char *endptr; + gid_t gid; unsigned long val; - /* check group*/ + /* check group */ val = strtoul(group, &endptr, 10); if (*endptr == '\0') { @@ -1121,8 +1135,9 @@ create_unix_domain_socket(struct sockaddr_un un_addr_tmp, const char *group, con else { struct group *gr; + gr = getgrnam(group); - if(!gr) + if (!gr) { ereport(FATAL, (errmsg("unix_socket_group \"%s\" does not exist", group))); @@ -1167,6 +1182,7 @@ terminate_all_childrens(int sig) int i; int killed_count = 0; int terminated_count = 0; + /* * This is supposed to be called from main process */ @@ -1228,7 +1244,7 @@ terminate_all_childrens(int sig) } } - for (i = 0 ; i < MAX_NUM_BACKENDS; i++) + for (i = 0; i < MAX_NUM_BACKENDS; i++) { if (health_check_pids[i] != 0) { @@ -1335,7 +1351,7 @@ static RETSIGTYPE exit_handler(int sig) */ if (follow_pid > 0) { - ereport(LOG, + ereport(LOG, (errmsg("terminating all child processes of follow child"))); kill(follow_pid, sig); switch (sig) @@ -1373,8 +1389,8 @@ get_next_main_node(void) { /* * Do not use VALID_BACKEND macro in raw mode. VALID_BACKEND return - * true only if the argument is main node id. In other words, - * standby nodes are false. So need to check backend status with + * true only if the argument is main node id. In other words, standby + * nodes are false. So need to check backend status with * VALID_BACKEND_RAW. */ if (RAW_MODE) @@ -1412,7 +1428,7 @@ static RETSIGTYPE sigusr1_handler(int sig) if (write(pipe_fds[1], "\0", 1) < 0) ereport(WARNING, (errmsg("SIGUSR1 handler: write to pipe failed"), - errdetail("%m"))); + errdetail("%m"))); #endif POOL_SETMASK(&UnBlockSig); @@ -1478,11 +1494,11 @@ sigusr1_interrupt_processor(void) (errmsg("we have joined the watchdog cluster as STANDBY node"), errdetail("syncing the backend states from the LEADER watchdog node"))); sync_backend_from_watchdog(); + /* * we also want to release the follow_primary lock if it was held - * by the remote node. - * because the change of watchdog coordinator would lead to forever stuck - * in the the locked state + * by the remote node. because the change of watchdog coordinator + * would lead to forever stuck in the the locked state */ pool_release_follow_primary_lock(true); } @@ -1627,28 +1643,29 @@ failover(void) wd_failover_start(); /* - * if not in replication mode/native replication mode, we treat this a - * restart request. otherwise we need to check if we have already - * failovered. + * If not in streaming replication mode/native replication mode, we + * treat this as a restart request. Otherwise we need to check if we + * have already performed the failover. */ ereport(DEBUG1, (errmsg("failover handler"), errdetail("starting to select new main node"))); - /* If this is promoting specified node, new_main_node - * should be replaced by the requested node. The requested - * node should be REAL_PRIMARY_NODE_ID. + /* + * If this is promoting specified node, new_main_node should be + * replaced by the requested node. The requested node should be + * REAL_PRIMARY_NODE_ID. */ if (failover_context.request_details & REQ_DETAIL_PROMOTE) { - promote_node = failover_context.node_id_set[0]; + promote_node = failover_context.node_id_set[0]; /* requested node */ for (i = 0; i < failover_context.node_count; i++) { failover_context.node_id_set[i] = REAL_PRIMARY_NODE_ID; } } - node_id = failover_context.node_id_set[0]; + node_id = failover_context.node_id_set[0]; /* set target node id */ /* failback request? */ if (failover_context.reqkind == NODE_UP_REQUEST) @@ -1675,7 +1692,8 @@ failover(void) else /* NODE_DOWN_REQUEST && * NODE_QUARANTINE_REQUEST */ { - + + /* process single failover request */ if (handle_failover_request(&failover_context, node_id) < 0) continue; } @@ -1709,14 +1727,15 @@ failover(void) exec_failover_command(&failover_context, new_main_node, promote_node); /* - * Determine new primary node id. Possibly call find_primary_node_repeatedly(). + * Determine new primary node id. Possibly call + * find_primary_node_repeatedly(). */ new_primary = determine_new_primary_node(&failover_context, node_id); - + /* - * If follow_primary_command is provided and in streaming - * replication mode, we start degenerating all backends as they are - * not replicated anymore. + * If follow_primary_command is provided and in streaming replication + * mode, we start degenerating all backends as they are not replicated + * anymore. */ i = exec_follow_primary_command(&failover_context, node_id, new_primary); @@ -1725,8 +1744,8 @@ failover(void) new_main_node = i; /* - * Now new primary node and new main node are established. - * Save them into shared memory. Also update status changed time. + * Now new primary node and new main node are established. Save them + * into shared memory. Also update status changed time. */ save_node_info(&failover_context, new_primary, new_main_node); @@ -1735,8 +1754,7 @@ failover(void) } /* - * We are almost done. - * Unlock flags. + * We are almost done. Unlock flags. */ pool_semaphore_lock(REQUEST_INFO_SEM); switching = 0; @@ -1768,7 +1786,7 @@ static RETSIGTYPE reap_handler(int sig) if (pipe_fds[1] && write(pipe_fds[1], "\0", 1) < 0) ereport(WARNING, (errmsg("reap_handler: write to pipe failed"), - errdetail("%m"))); + errdetail("%m"))); #endif POOL_SETMASK(&UnBlockSig); @@ -2092,7 +2110,7 @@ pool_get_process_list(int *array_size) int cnt = 0; int i; - for (i=0;i < pool_config->num_init_children;i++) + for (i = 0; i < pool_config->num_init_children; i++) { if (process_info[i].pid != 0) cnt++; @@ -2307,8 +2325,8 @@ trigger_failover_command(int node, const char *command_line, int old_main_node, int new_main_node, int old_primary) { int r = 0; - StringInfoData exec_cmd_data; - StringInfo exec_cmd = &exec_cmd_data; + StringInfoData exec_cmd_data; + StringInfo exec_cmd = &exec_cmd_data; BackendInfo *info; BackendInfo *newmain; BackendInfo *oldprimary; @@ -2445,7 +2463,7 @@ trigger_failover_command(int node, const char *command_line, static POOL_NODE_STATUS pool_node_status[MAX_NUM_BACKENDS]; POOL_NODE_STATUS * -verify_backend_node_status(POOL_CONNECTION_POOL_SLOT * *slots) +verify_backend_node_status(POOL_CONNECTION_POOL_SLOT **slots) { POOL_SELECT_RESULT *res; int num_primaries = 0; @@ -2661,13 +2679,14 @@ verify_backend_node_status(POOL_CONNECTION_POOL_SLOT * *slots) /* verify host and port */ if (((*backend_info->backend_hostname == '/' && *host == '\0') || - /* - * It is possible that backend_hostname is Unix - * domain socket but wal_receiver connects via - * TCP/IP localhost. - */ + + /* + * It is possible that backend_hostname is Unix domain + * socket but wal_receiver connects via TCP/IP + * localhost. + */ (*backend_info->backend_hostname == '/' && !strcmp("localhost", host)) || - !strcmp(backend_info->backend_hostname, host)) && + !strcmp(backend_info->backend_hostname, host)) && backend_info->backend_port == atoi(port)) { /* the standby connects to the primary */ @@ -2909,8 +2928,8 @@ find_primary_node_repeatedly(void) { ereport(LOG, (errmsg("failed to find primary node"), - errdetail("find_primary_node_repeatedly: expired after %d seconds", - pool_config->search_primary_node_timeout))); + errdetail("find_primary_node_repeatedly: expired after %d seconds", + pool_config->search_primary_node_timeout))); break; } } @@ -2955,13 +2974,12 @@ fork_follow_child(int old_main_node, int new_primary, int old_primary) #endif SetProcessGlobalVariables(PT_FOLLOWCHILD); + /* - * when the watchdog is enabled, we would come here - * only on the coordinator node. - * so before acquiring the local lock, Lock all the - * standby nodes so that they should stop false primary - * detection until we are finished with the follow primary - * command. + * when the watchdog is enabled, we would come here only on the + * coordinator node. so before acquiring the local lock, Lock all the + * standby nodes so that they should stop false primary detection + * until we are finished with the follow primary command. */ wd_lock_standby(WD_FOLLOW_PRIMARY_LOCK); pool_acquire_follow_primary_lock(true, false); @@ -2983,7 +3001,7 @@ fork_follow_child(int old_main_node, int new_primary, int old_primary) } Req_info->follow_primary_ongoing = false; pool_release_follow_primary_lock(false); - /* inform standby watchdog nodes to release the lock as well*/ + /* inform standby watchdog nodes to release the lock as well */ wd_unlock_standby(WD_FOLLOW_PRIMARY_LOCK); exit(0); } @@ -3000,26 +3018,26 @@ fork_follow_child(int old_main_node, int new_primary, int old_primary) static void initialize_shared_mem_objects(bool clear_memcache_oidmaps) { - BackendDesc* backend_desc; - Size size; - int i; + BackendDesc *backend_desc; + Size size; + int i; /* * Calculate the size of required shared memory and try to allocate * everything in single memory segment */ - size = 256;/* let us have some extra space */ + size = 256; /* let us have some extra space */ size += MAXALIGN(sizeof(BackendDesc)); elog(DEBUG1, "BackendDesc: %zu bytes requested for shared memory", MAXALIGN(sizeof(BackendDesc))); size += MAXALIGN(pool_coninfo_size()); size += MAXALIGN(pool_config->num_init_children * (sizeof(ProcessInfo))); elog(DEBUG1, "ProcessInfo: num_init_children (%d) * sizeof(ProcessInfo) (%zu) = %zu bytes requested for shared memory", - pool_config->num_init_children, sizeof(ProcessInfo), pool_config->num_init_children* sizeof(ProcessInfo)); + pool_config->num_init_children, sizeof(ProcessInfo), pool_config->num_init_children * sizeof(ProcessInfo)); size += MAXALIGN(sizeof(User1SignalSlot)); elog(DEBUG1, "UserSignalSlot: %zu bytes requested for shared memory", MAXALIGN(sizeof(User1SignalSlot))); size += MAXALIGN(sizeof(POOL_REQUEST_INFO)); elog(DEBUG1, "POOL_REQUEST_INFO: %zu bytes requested for shared memory", MAXALIGN(sizeof(POOL_REQUEST_INFO))); - size += MAXALIGN(sizeof(int)); /* for InRecovery */ + size += MAXALIGN(sizeof(int)); /* for InRecovery */ size += MAXALIGN(stat_shared_memory_size()); elog(DEBUG1, "stat_shared_memory_size: %zu bytes requested for shared memory", MAXALIGN(stat_shared_memory_size())); size += MAXALIGN(health_check_stats_shared_memory_size()); @@ -3051,25 +3069,25 @@ initialize_shared_mem_objects(bool clear_memcache_oidmaps) /* Move the backend descriptors to shared memory */ backend_desc = pool_shared_memory_segment_get_chunk(sizeof(BackendDesc)); - memcpy(backend_desc, pool_config->backend_desc,sizeof(BackendDesc)); + memcpy(backend_desc, pool_config->backend_desc, sizeof(BackendDesc)); pfree(pool_config->backend_desc); pool_config->backend_desc = backend_desc; - /* get the shared memory from main segment*/ - con_info = (ConnectionInfo *)pool_shared_memory_segment_get_chunk(pool_coninfo_size()); + /* get the shared memory from main segment */ + con_info = (ConnectionInfo *) pool_shared_memory_segment_get_chunk(pool_coninfo_size()); - process_info = (ProcessInfo *)pool_shared_memory_segment_get_chunk(pool_config->num_init_children * (sizeof(ProcessInfo))); + process_info = (ProcessInfo *) pool_shared_memory_segment_get_chunk(pool_config->num_init_children * (sizeof(ProcessInfo))); for (i = 0; i < pool_config->num_init_children; i++) { process_info[i].connection_info = pool_coninfo(i, 0, 0); process_info[i].pid = 0; } - user1SignalSlot = (User1SignalSlot *)pool_shared_memory_segment_get_chunk(sizeof(User1SignalSlot)); + user1SignalSlot = (User1SignalSlot *) pool_shared_memory_segment_get_chunk(sizeof(User1SignalSlot)); - Req_info = (POOL_REQUEST_INFO *)pool_shared_memory_segment_get_chunk(sizeof(POOL_REQUEST_INFO)); + Req_info = (POOL_REQUEST_INFO *) pool_shared_memory_segment_get_chunk(sizeof(POOL_REQUEST_INFO)); - InRecovery = (int *)pool_shared_memory_segment_get_chunk(sizeof(int)); + InRecovery = (int *) pool_shared_memory_segment_get_chunk(sizeof(int)); /* Initialize statistics area */ stat_set_stat_area(pool_shared_memory_segment_get_chunk(stat_shared_memory_size())); @@ -3079,13 +3097,13 @@ initialize_shared_mem_objects(bool clear_memcache_oidmaps) health_check_stats_init(pool_shared_memory_segment_get_chunk(health_check_stats_shared_memory_size())); /* Initialize Snapshot Isolation manage area */ - si_manage_info = (SI_ManageInfo*)pool_shared_memory_segment_get_chunk(sizeof(SI_ManageInfo)); + si_manage_info = (SI_ManageInfo *) pool_shared_memory_segment_get_chunk(sizeof(SI_ManageInfo)); si_manage_info->snapshot_waiting_children = - (pid_t*)pool_shared_memory_segment_get_chunk(pool_config->num_init_children * sizeof(pid_t)); + (pid_t *) pool_shared_memory_segment_get_chunk(pool_config->num_init_children * sizeof(pid_t)); si_manage_info->commit_waiting_children = - (pid_t*)pool_shared_memory_segment_get_chunk(pool_config->num_init_children * sizeof(pid_t)); + (pid_t *) pool_shared_memory_segment_get_chunk(pool_config->num_init_children * sizeof(pid_t)); /* * Initialize backend status area. From now on, VALID_BACKEND macro can be @@ -3164,6 +3182,7 @@ initialize_shared_mem_objects(bool clear_memcache_oidmaps) } } + /* * Read the status file */ @@ -3185,7 +3204,7 @@ read_status_file(bool discard_status) pool_set_backend_status_changed_time(i); } - snprintf(fnamebuf, sizeof(fnamebuf), "%s/%s", pool_config->logdir, STATUS_FILE_NAME); + snprintf(fnamebuf, sizeof(fnamebuf), "%s/%s", pool_config->work_dir, STATUS_FILE_NAME); fd = fopen(fnamebuf, "r"); if (!fd) { @@ -3372,7 +3391,7 @@ write_status_file(void) return 0; } - snprintf(fnamebuf, sizeof(fnamebuf), "%s/%s", pool_config->logdir, STATUS_FILE_NAME); + snprintf(fnamebuf, sizeof(fnamebuf), "%s/%s", pool_config->work_dir, STATUS_FILE_NAME); fd = fopen(fnamebuf, "w"); if (!fd) { @@ -3503,7 +3522,7 @@ system_will_go_down(int code, Datum arg) */ if (follow_pid > 0) { - ereport(LOG, + ereport(LOG, (errmsg("terminating all child processes of follow child"))); kill(follow_pid, SIGTERM); kill(-follow_pid, SIGTERM); @@ -3662,8 +3681,8 @@ sync_backend_from_watchdog(void) /* * Update primary node id info on the shared memory area if it's different - * from the one on leader watchdog node. This should be done only in streaming - * or logical replication mode. + * from the one on leader watchdog node. This should be done only in + * streaming or logical replication mode. */ if (SL_MODE && Req_info->primary_node_id != backendStatus->primary_node_id) { @@ -3672,6 +3691,7 @@ sync_backend_from_watchdog(void) ereport(LOG, (errmsg("primary node:%d on leader watchdog node \"%s\" is different from local primary node:%d", backendStatus->primary_node_id, backendStatus->nodeName, Req_info->primary_node_id))); + /* * leader node returns primary_node_id = -1 when the primary node is * in quarantine state on the leader. So we will not update our @@ -3687,9 +3707,9 @@ sync_backend_from_watchdog(void) backendStatus->primary_node_id == -1 && BACKEND_INFO(Req_info->primary_node_id).backend_status != CON_DOWN) { ereport(LOG, - (errmsg("primary node:%d on leader watchdog node \"%s\" seems to be quarantined", - Req_info->primary_node_id, backendStatus->nodeName), - errdetail("keeping the current primary"))); + (errmsg("primary node:%d on leader watchdog node \"%s\" seems to be quarantined", + Req_info->primary_node_id, backendStatus->nodeName), + errdetail("keeping the current primary"))); } else { @@ -3871,7 +3891,7 @@ sync_backend_from_watchdog(void) * version number is in the static memory area. */ static int -get_server_version(POOL_CONNECTION_POOL_SLOT * *slots, int node_id) +get_server_version(POOL_CONNECTION_POOL_SLOT **slots, int node_id) { static int server_versions[MAX_NUM_BACKENDS]; @@ -3955,7 +3975,7 @@ bool pool_acquire_follow_primary_lock(bool block, bool remote_request) { pool_sigset_t oldmask; - volatile int follow_primary_count; + volatile int follow_primary_count; for (;;) { @@ -3974,7 +3994,8 @@ pool_acquire_follow_primary_lock(bool block, bool remote_request) { if (Req_info->follow_primary_lock_held_remotely) { - /* The lock was already held by remote node and we only + /* + * The lock was already held by remote node and we only * support one remote lock */ ereport(LOG, @@ -4047,25 +4068,26 @@ pool_release_follow_primary_lock(bool remote_request) else if (Req_info->follow_primary_count) { /* - * we have received the release lock request from remote - * but the lock is not held by remote node. - * Just ignore the request + * we have received the release lock request from remote but the + * lock is not held by remote node. Just ignore the request */ ereport(DEBUG1, (errmsg("pool_release_follow_primary_lock is not relasing the lock since it was not held by remote node"))); } + /* - * Silently ignore, if we received the release request from remote while no lock was held. - * Also clear the pending lock request, As we only support single remote lock + * Silently ignore, if we received the release request from remote + * while no lock was held. Also clear the pending lock request, As we + * only support single remote lock */ Req_info->follow_primary_lock_pending = false; } - else /*local request */ + else /* local request */ { /* - * if we have a pending lock request from watchdog - * do not remove the actual lock, Just clear the pending flag + * if we have a pending lock request from watchdog do not remove the + * actual lock, Just clear the pending flag */ if (Req_info->follow_primary_lock_pending) { @@ -4081,10 +4103,10 @@ pool_release_follow_primary_lock(bool remote_request) if (Req_info->follow_primary_lock_held_remotely) { /* - * Ideally this should not happen. - * yet if for some reason our local node is trying to release a lock - * that is held by remote node. Just produce a LOG message and release - * the lock + * Ideally this should not happen. yet if for some reason our + * local node is trying to release a lock that is held by + * remote node. Just produce a LOG message and release the + * lock */ ereport(LOG, (errmsg("pool_release_follow_primary_lock is relasing the remote lock by local request"))); @@ -4146,7 +4168,7 @@ handle_failback_request(FAILOVER_CONTEXT *failover_context, int node_id) { if (node_id < 0 || node_id >= MAX_NUM_BACKENDS || (failover_context->reqkind == NODE_UP_REQUEST && !(RAW_MODE && - BACKEND_INFO(node_id).backend_status == CON_DOWN) && VALID_BACKEND(node_id)) || + BACKEND_INFO(node_id).backend_status == CON_DOWN) && VALID_BACKEND(node_id)) || (failover_context->reqkind == NODE_DOWN_REQUEST && !VALID_BACKEND(node_id))) { if (node_id < 0 || node_id >= MAX_NUM_BACKENDS) @@ -4176,26 +4198,24 @@ handle_failback_request(FAILOVER_CONTEXT *failover_context, int node_id) BACKEND_INFO(node_id).quarantine = false; /* - * do not search for primary node when handling the quarantine - * nodes + * do not search for primary node when handling the quarantine nodes */ failover_context->search_primary = false; /* - * recalculate the main node id after setting the backend - * status of quarantined node, this will bring us to the old - * main_node_id that was before the quarantine state + * recalculate the main node id after setting the backend status of + * quarantined node, this will bring us to the old main_node_id that + * was before the quarantine state */ Req_info->main_node_id = get_next_main_node(); if (Req_info->primary_node_id == -1 && BACKEND_INFO(node_id).role == ROLE_PRIMARY) { /* - * if the failback request is for the quarantined node and - * that node had a primary role before it was quarantined, - * restore the primary node status for that node. this is - * important for the failover script to get the proper - * value of old primary + * if the failback request is for the quarantined node and that + * node had a primary role before it was quarantined, restore the + * primary node status for that node. this is important for the + * failover script to get the proper value of old primary */ ereport(LOG, (errmsg("failover: failing back the quarantine node that was primary before it was quarantined"), @@ -4203,8 +4223,8 @@ handle_failback_request(FAILOVER_CONTEXT *failover_context, int node_id) Req_info->primary_node_id = node_id; /* - * since we changed the primary node so restart of all - * children is required + * since we changed the primary node so restart of all children is + * required */ failover_context->need_to_restart_children = true; failover_context->partial_restart = false; @@ -4227,8 +4247,8 @@ handle_failback_request(FAILOVER_CONTEXT *failover_context, int node_id) else { /* - * The request is a proper failback request and not because of - * the update status of quarantined node + * The request is a proper failback request and not because of the + * update status of quarantined node */ (void) write_status_file(); @@ -4248,14 +4268,14 @@ handle_failback_request(FAILOVER_CONTEXT *failover_context, int node_id) static int handle_failover_request(FAILOVER_CONTEXT *failover_context, int node_id) { - int cnt = 0; /* number of down node ids */ - int i; + int cnt = 0; /* number of down node ids */ + int i; for (i = 0; i < failover_context->node_count; i++) { if (failover_context->node_id_set[i] != -1 && (BACKEND_INFO(failover_context->node_id_set[i]).quarantine == true || - ((RAW_MODE && VALID_BACKEND_RAW(failover_context->node_id_set[i])) || - VALID_BACKEND(failover_context->node_id_set[i])))) + ((RAW_MODE && VALID_BACKEND_RAW(failover_context->node_id_set[i])) || + VALID_BACKEND(failover_context->node_id_set[i])))) { ereport(LOG, (errmsg("=== Starting %s. shutdown host %s(%d) ===", @@ -4263,7 +4283,7 @@ handle_failover_request(FAILOVER_CONTEXT *failover_context, int node_id) BACKEND_INFO(failover_context->node_id_set[i]).backend_hostname, BACKEND_INFO(failover_context->node_id_set[i]).backend_port))); - BACKEND_INFO(failover_context->node_id_set[i]).backend_status = CON_DOWN; /* set down status */ + BACKEND_INFO(failover_context->node_id_set[i]).backend_status = CON_DOWN; /* set down status */ pool_set_backend_status_changed_time(failover_context->node_id_set[i]); if (failover_context->reqkind == NODE_QUARANTINE_REQUEST) { @@ -4272,12 +4292,11 @@ handle_failover_request(FAILOVER_CONTEXT *failover_context, int node_id) else { /* - * if the degeneration request is for the quarantined - * node and that node had a primary role before it was - * quarantined, Restore the primary node status for - * that node before degenerating it. This is important - * for the failover script to get the proper value of - * old primary + * if the degeneration request is for the quarantined node and + * that node had a primary role before it was quarantined, + * Restore the primary node status for that node before + * degenerating it. This is important for the failover script + * to get the proper value of old primary */ if (Req_info->primary_node_id == -1 && BACKEND_INFO(failover_context->node_id_set[i]).quarantine == true && @@ -4313,41 +4332,43 @@ handle_failover_request(FAILOVER_CONTEXT *failover_context, int node_id) static void kill_failover_children(FAILOVER_CONTEXT *failover_context, int node_id) { - int i, j, k; + int i, + j, + k; + /* * On 2011/5/2 Tatsuo Ishii says: if mode is streaming replication and - * request is NODE_UP_REQUEST (failback case) we don't need to restart - * all children. Existing session will not use newly attached node, - * but load balanced node is not changed until this session ends, so - * it's harmless anyway. + * request is NODE_UP_REQUEST (failback case) we don't need to restart all + * children. Existing session will not use newly attached node, but load + * balanced node is not changed until this session ends, so it's harmless + * anyway. */ /* - * On 2015/9/21 Tatsuo Ishii says: this judgment is not sufficient if - * all backends were down. Child process has local status in which all + * On 2015/9/21 Tatsuo Ishii says: this judgment is not sufficient if all + * backends were down. Child process has local status in which all * backends are down. In this case even if new connection arrives from - * frontend, the child will not accept it because the local status - * shows all backends are down. For this purpose we refer to - * "all_backend_down" variable, which was set before updating backend - * status. + * frontend, the child will not accept it because the local status shows + * all backends are down. For this purpose we refer to "all_backend_down" + * variable, which was set before updating backend status. * * See bug 248 for more details. */ /* - * We also need to think about a case when the former primary node did - * not exist. In the case we need to restart all children as - * well. For example when previous primary node id is 0 and then it - * went down, restarted, re-attached without promotion. Then existing - * child process loses connection slot to node 0 and keeps on using it - * when node 0 comes back. This could result in segfault later on in - * the child process because there's no connection to node id 0. + * We also need to think about a case when the former primary node did not + * exist. In the case we need to restart all children as well. For + * example when previous primary node id is 0 and then it went down, + * restarted, re-attached without promotion. Then existing child process + * loses connection slot to node 0 and keeps on using it when node 0 comes + * back. This could result in segfault later on in the child process + * because there's no connection to node id 0. * - * Actually we need to think about when ALWAYS_PRIMARY flag is set - * *but* DISALLOW_TO_FAILOVER flag is not set case. In the case after - * primary failover Req_info->primary_node_id is set, but connection - * to the primary node does not exist. So we should do full restart if - * requested node id is the former primary node. + * Actually we need to think about when ALWAYS_PRIMARY flag is set *but* + * DISALLOW_TO_FAILOVER flag is not set case. In the case after primary + * failover Req_info->primary_node_id is set, but connection to the + * primary node does not exist. So we should do full restart if requested + * node id is the former primary node. * * See bug 672 for more details. */ @@ -4453,7 +4474,7 @@ kill_failover_children(FAILOVER_CONTEXT *failover_context, int node_id) static void exec_failover_command(FAILOVER_CONTEXT *failover_context, int new_main_node_id, int promote_node_id) { - int i; + int i; if (failover_context->reqkind == NODE_DOWN_REQUEST) { @@ -4461,9 +4482,10 @@ exec_failover_command(FAILOVER_CONTEXT *failover_context, int new_main_node_id, { if (failover_context->nodes[i]) { - /* If this is promoting specified node, new_main_node - * should be replaced by the requested node. The requested - * node should be REAL_PRIMARY_NODE_ID. + /* + * If this is promoting specified node, new_main_node should + * be replaced by the requested node. The requested node + * should be REAL_PRIMARY_NODE_ID. */ if (failover_context->request_details & REQ_DETAIL_PROMOTE) { @@ -4487,7 +4509,7 @@ exec_failover_command(FAILOVER_CONTEXT *failover_context, int new_main_node_id, static int determine_new_primary_node(FAILOVER_CONTEXT *failover_context, int node_id) { - int new_primary; + int new_primary; if (failover_context->reqkind == PROMOTE_NODE_REQUEST && VALID_BACKEND(node_id)) { @@ -4496,15 +4518,14 @@ determine_new_primary_node(FAILOVER_CONTEXT *failover_context, int node_id) else if (failover_context->reqkind == NODE_QUARANTINE_REQUEST) { /* - * If the quarantine node was the primary node, set the new primary - * to -1 (invalid). + * If the quarantine node was the primary node, set the new primary to + * -1 (invalid). */ if (Req_info->primary_node_id == node_id) { /* - * set the role of the node, This will help us restore the - * primary node id when the node will come out from quarantine - * state + * set the role of the node, This will help us restore the primary + * node id when the node will come out from quarantine state */ BACKEND_INFO(node_id).role = ROLE_PRIMARY; new_primary = -1; @@ -4516,12 +4537,11 @@ determine_new_primary_node(FAILOVER_CONTEXT *failover_context, int node_id) } /* - * If the down node was a standby node in streaming replication mode, - * we can avoid calling find_primary_node_repeatedly() and recognize - * the former primary as the new primary node, which will reduce the - * time to process standby down. - * This does not apply to the case when no primary node existed - * (Req_info->primary_node_id < 0). In this case + * If the down node was a standby node in streaming replication mode, we + * can avoid calling find_primary_node_repeatedly() and recognize the + * former primary as the new primary node, which will reduce the time to + * process standby down. This does not apply to the case when no primary + * node existed (Req_info->primary_node_id < 0). In this case * find_primary_node_repeatedly() should be called. */ else if (SL_MODE && @@ -4560,9 +4580,9 @@ determine_new_primary_node(FAILOVER_CONTEXT *failover_context, int node_id) static int exec_follow_primary_command(FAILOVER_CONTEXT *failover_context, int node_id, int new_primary_node_id) { - int follow_cnt = 0; - int new_main_node_id = -1; - int i; + int follow_cnt = 0; + int new_main_node_id = -1; + int i; if (!STREAM) return -1; @@ -4571,10 +4591,9 @@ exec_follow_primary_command(FAILOVER_CONTEXT *failover_context, int node_id, int failover_context->reqkind == PROMOTE_NODE_REQUEST) { /* - * follow primary command is executed in following cases: - * - failover against the current primary - * - no primary exists and new primary is created by failover - * - promote node request + * follow primary command is executed in following cases: - failover + * against the current primary - no primary exists and new primary is + * created by failover - promote node request */ if (((failover_context->reqkind == NODE_DOWN_REQUEST) && Req_info->primary_node_id >= 0 && @@ -4668,21 +4687,22 @@ save_node_info(FAILOVER_CONTEXT *failover_context, int new_primary_node_id, int static void exec_child_restart(FAILOVER_CONTEXT *failover_context, int node_id) { - int i, j, k; + int i, + j, + k; if (failover_context->need_to_restart_children) { for (i = 0; i < pool_config->num_init_children; i++) { /* - * Try to kill pgpool child because previous kill signal may - * not be received by pgpool child. This could happen if - * multiple PostgreSQL are going down (or even starting - * pgpool, without starting PostgreSQL can trigger this). - * Child calls degenerate_backend() and it tries to acquire - * semaphore to write a failover request. In this case the - * signal mask is set as well, thus signals are never - * received. + * Try to kill pgpool child because previous kill signal may not + * be received by pgpool child. This could happen if multiple + * PostgreSQL are going down (or even starting pgpool, without + * starting PostgreSQL can trigger this). Child calls + * degenerate_backend() and it tries to acquire semaphore to write + * a failover request. In this case the signal mask is set as + * well, thus signals are never received. */ bool restart = false; @@ -4734,8 +4754,8 @@ exec_child_restart(FAILOVER_CONTEXT *failover_context, int node_id) else { /* - * Set restart request to each child. Children will exit(1) - * whenever they are convenient. + * Set restart request to each child. Children will exit(1) whenever + * they are convenient. */ for (i = 0; i < pool_config->num_init_children; i++) { @@ -4783,8 +4803,7 @@ exec_child_restart(FAILOVER_CONTEXT *failover_context, int node_id) { #ifdef NOT_USED /* - * Temporary black magic. Without this regression 055 does not - * finish + * Temporary black magic. Without this regression 055 does not finish */ fprintf(stderr, "=== %s done. shutdown host %s(%d) ===", (failover_context->reqkind == NODE_DOWN_REQUEST) ? "Failover" : "Quarantine", @@ -4848,6 +4867,7 @@ exec_notice_pcp_child(FAILOVER_CONTEXT *failover_context) (errmsg("fork a new PCP child pid %d in failover()", pcp_pid))); } } + /* * ------------------------------------------------------------------------- * Subroutines for failover() end @@ -4865,8 +4885,8 @@ static int * create_unix_domain_sockets_by_list(struct sockaddr_un *un_addrs, char *group, int permissions, int n_sockets) { - int i; - int *sockets = NULL; + int i; + int *sockets = NULL; if (un_addrs == NULL) return NULL; @@ -4874,7 +4894,7 @@ create_unix_domain_sockets_by_list(struct sockaddr_un *un_addrs, sockets = malloc(sizeof(int) * n_sockets); if (sockets == NULL) ereport(FATAL, - (errmsg("failed to allocate memory in startup process"))); + (errmsg("failed to allocate memory in startup process"))); for (i = 0; i < n_sockets; i++) { @@ -4896,8 +4916,8 @@ create_unix_domain_sockets_by_list(struct sockaddr_un *un_addrs, static int * create_inet_domain_sockets_by_list(char **listen_addresses, int n_listen_addresses, int port, int *n_sockets) { - int *sockets = NULL; - int i; + int *sockets = NULL; + int i; *n_sockets = 0; @@ -4908,7 +4928,8 @@ create_inet_domain_sockets_by_list(char **listen_addresses, int n_listen_address { int *inet_fds, *walk; - int n = 0; /* number of fds returned from create_inet_domain_sockets(). */ + int n = 0; /* number of fds returned from + * create_inet_domain_sockets(). */ ereport(LOG, (errmsg("listen address[%d]: %s", i, listen_addresses[i]))); @@ -4946,7 +4967,8 @@ create_inet_domain_sockets_by_list(char **listen_addresses, int n_listen_address * Check and execute pending requests set by signal interrupts. */ static -void check_requests(void) +void +check_requests(void) { sigset_t sig; @@ -4964,7 +4986,8 @@ void check_requests(void) */ if (sigusr1_request) { - do { + do + { sigusr1_request = 0; sigusr1_interrupt_processor(); } while (sigusr1_request == 1); @@ -4975,8 +4998,8 @@ void check_requests(void) print_signal_member(&sig); /* - * Unblock signals so that SIGQUIT/SIGTERRM/SIGINT can be accepted. - * They are all shutdown requests. + * Unblock signals so that SIGQUIT/SIGTERRM/SIGINT can be accepted. They + * are all shutdown requests. */ POOL_SETMASK(&UnBlockSig); @@ -5004,7 +5027,8 @@ void check_requests(void) } static -void print_signal_member(sigset_t *sig) +void +print_signal_member(sigset_t *sig) { if (sigismember(sig, SIGQUIT)) ereport(LOG, @@ -5023,55 +5047,56 @@ void print_signal_member(sigset_t *sig) static void service_child_processes(void) { - int connected_children = Req_info->conn_counter; - int idle_children = current_child_process_count - connected_children; - static int high_load_counter = 0; + int connected_children = Req_info->conn_counter; + int idle_children = current_child_process_count - connected_children; + static int high_load_counter = 0; + ereport(DEBUG2, - (errmsg("current_children_count = %d idle_children = %d connected_children = %d high_load_counter = %d", - current_child_process_count, idle_children, connected_children, high_load_counter))); + (errmsg("current_children_count = %d idle_children = %d connected_children = %d high_load_counter = %d", + current_child_process_count, idle_children, connected_children, high_load_counter))); if (idle_children > pool_config->max_spare_children) { - int ki; - int victim_count; - int kill_process_info_idxs[MAX_ONE_SHOT_KILLS]; - int kill_count = idle_children - pool_config->max_spare_children; - int cycle_skip_count_before_scale_down; - int cycle_skip_between_scale_down; - int one_shot_kill_count; + int ki; + int victim_count; + int kill_process_info_idxs[MAX_ONE_SHOT_KILLS]; + int kill_count = idle_children - pool_config->max_spare_children; + int cycle_skip_count_before_scale_down; + int cycle_skip_between_scale_down; + int one_shot_kill_count; switch (pool_config->process_management_strategy) { - case PM_STRATEGY_AGGRESSIVE: - cycle_skip_count_before_scale_down = 25; /* roughly 50 seconds */ - cycle_skip_between_scale_down = 2; - one_shot_kill_count = MAX_ONE_SHOT_KILLS; - break; + case PM_STRATEGY_AGGRESSIVE: + cycle_skip_count_before_scale_down = 25; /* roughly 50 seconds */ + cycle_skip_between_scale_down = 2; + one_shot_kill_count = MAX_ONE_SHOT_KILLS; + break; - case PM_STRATEGY_LAZY: - cycle_skip_count_before_scale_down = 150; /* roughly 300 seconds */ - cycle_skip_between_scale_down = 10; - one_shot_kill_count = 3; - break; + case PM_STRATEGY_LAZY: + cycle_skip_count_before_scale_down = 150; /* roughly 300 seconds */ + cycle_skip_between_scale_down = 10; + one_shot_kill_count = 3; + break; - case PM_STRATEGY_GENTLE: - cycle_skip_count_before_scale_down = 60; /* roughly 120 seconds */ - cycle_skip_between_scale_down = 5; - one_shot_kill_count = 3; - break; + case PM_STRATEGY_GENTLE: + cycle_skip_count_before_scale_down = 60; /* roughly 120 seconds */ + cycle_skip_between_scale_down = 5; + one_shot_kill_count = 3; + break; - default: - /* should never come here, but if we do use gentle counts*/ - cycle_skip_count_before_scale_down = 60; /* roughly 120 seconds */ - cycle_skip_between_scale_down = 5; - one_shot_kill_count = 3; - break; + default: + /* should never come here, but if we do use gentle counts */ + cycle_skip_count_before_scale_down = 60; /* roughly 120 seconds */ + cycle_skip_between_scale_down = 5; + one_shot_kill_count = 3; + break; } /* Do not scale down too quickly */ if (++high_load_counter < cycle_skip_count_before_scale_down || high_load_counter % cycle_skip_between_scale_down) return; - memset(kill_process_info_idxs, -1 ,sizeof(kill_process_info_idxs)); + memset(kill_process_info_idxs, -1, sizeof(kill_process_info_idxs)); if (kill_count > one_shot_kill_count) kill_count = one_shot_kill_count; @@ -5080,15 +5105,16 @@ service_child_processes(void) for (ki = 0; ki < victim_count; ki++) { - int index = kill_process_info_idxs[ki]; - if (index >=0) + int index = kill_process_info_idxs[ki]; + + if (index >= 0) { if (process_info[index].pid && process_info[index].status == WAIT_FOR_CONNECT) { ereport(DEBUG1, - (errmsg("asking child process with pid:%d to kill itself to satisfy max_spare_children", - process_info[index].pid), - errdetail("child process has %d pooled connections",process_info[index].pooled_connections))); + (errmsg("asking child process with pid:%d to kill itself to satisfy max_spare_children", + process_info[index].pid), + errdetail("child process has %d pooled connections", process_info[index].pooled_connections))); process_info[index].exit_if_idle = true; kill(process_info[index].pid, SIGUSR2); } @@ -5099,19 +5125,20 @@ service_child_processes(void) { /* Reset the high load counter */ high_load_counter = 0; - /*See if we need to spawn new children */ + /* See if we need to spawn new children */ if (idle_children < pool_config->min_spare_children) { - int i; - int spawned = 0; - int new_spawn_no = pool_config->min_spare_children - idle_children; + int i; + int spawned = 0; + int new_spawn_no = pool_config->min_spare_children - idle_children; + /* Add 25% of max_spare_children */ new_spawn_no += pool_config->max_spare_children / 4; if (new_spawn_no + current_child_process_count > pool_config->num_init_children) { ereport(DEBUG5, - (errmsg("we have hit the ceiling, spawning %d child(ren)", - pool_config->num_init_children - current_child_process_count))); + (errmsg("we have hit the ceiling, spawning %d child(ren)", + pool_config->num_init_children - current_child_process_count))); new_spawn_no = pool_config->num_init_children - current_child_process_count; } if (new_spawn_no <= 0) @@ -5149,47 +5176,51 @@ service_child_processes(void) static int select_victim_processes(int *process_info_idxs, int count) { - int i, ki; - bool found_enough = false; - int selected_count = 0; + int i, + ki; + bool found_enough = false; + int selected_count = 0; - if (count <= 0) - return 0; + if (count <= 0) + return 0; - for (i = 0; i < pool_config->num_init_children; i++) + for (i = 0; i < pool_config->num_init_children; i++) + { + /* Only the child process in waiting for connect can be terminated */ + if (process_info[i].pid && process_info[i].status == WAIT_FOR_CONNECT) { - /* Only the child process in waiting for connect can be terminated */ - if (process_info[i].pid && process_info[i].status == WAIT_FOR_CONNECT) + if (selected_count < count) { - if (selected_count < count) - { - process_info_idxs[selected_count++] = i; - } - else + process_info_idxs[selected_count++] = i; + } + else + { + found_enough = true; + + /* + * we don't bother selecting the child having least pooled + * connection with aggressive strategy + */ + if (pool_config->process_management_strategy != PM_STRATEGY_AGGRESSIVE) { - found_enough = true; - /* we don't bother selecting the child having least pooled connection with - * aggressive strategy - */ - if (pool_config->process_management_strategy != PM_STRATEGY_AGGRESSIVE) + for (ki = 0; ki < count; ki++) { - for (ki = 0; ki < count; ki++) + int old_index = process_info_idxs[ki]; + + if (old_index < 0 || process_info[old_index].pooled_connections > process_info[i].pooled_connections) { - int old_index = process_info_idxs[ki]; - if (old_index < 0 || process_info[old_index].pooled_connections > process_info[i].pooled_connections) - { - process_info_idxs[ki] = i; - found_enough = false; - break; - } - if (process_info[old_index].pooled_connections) - found_enough = false; + process_info_idxs[ki] = i; + found_enough = false; + break; } + if (process_info[old_index].pooled_connections) + found_enough = false; } } } - if (found_enough) - break; } + if (found_enough) + break; + } return selected_count; } diff --git a/src/main/pool_globals.c b/src/main/pool_globals.c index 06f775ad9..84680fc8d 100644 --- a/src/main/pool_globals.c +++ b/src/main/pool_globals.c @@ -20,38 +20,39 @@ * * Global variables. Should be eventually removed. */ -#include <unistd.h> /*For getpid*/ +#include <unistd.h> /* For getpid */ #include "pool.h" #include "utils/elog.h" pid_t mypid; /* pgpool parent process id */ -pid_t myProcPid; /* process pid */ +pid_t myProcPid; /* process pid */ ProcessType processType; ProcessState processState; -bool reset_query_error; /* true if error returned from backend while processing reset queries */ +bool reset_query_error; /* true if error returned from backend while + * processing reset queries */ /* * Application name */ -static char *process_application_name = "main"; +static char *process_application_name = "main"; /* * Fixed application names. ordered by ProcessType. */ -char *application_names[] = {"main", - "child", - "sr_check_worker", - "heart_beat_sender", - "heart_beat_receiver", - "watchdog", - "life_check", - "follow_child", - "watchdog_utility", - "pcp_main", - "pcp_child", - "health_check", - "logger" +char *application_names[] = {"main", + "child", + "sr_check_worker", + "heart_beat_sender", + "heart_beat_receiver", + "watchdog", + "life_check", + "follow_child", + "watchdog_utility", + "pcp_main", + "pcp_child", + "health_check", + "logger" }; char * @@ -90,7 +91,8 @@ set_application_name_with_string(char *string) void set_application_name_with_suffix(ProcessType ptype, int suffix) { - static char appname_buf[POOLCONFIG_MAXNAMELEN +1]; + static char appname_buf[POOLCONFIG_MAXNAMELEN + 1]; + snprintf(appname_buf, POOLCONFIG_MAXNAMELEN, "%s%d", get_application_name_for_process(ptype), suffix); set_application_name_with_string(appname_buf); } @@ -104,7 +106,8 @@ get_application_name(void) return process_application_name; } -void SetProcessGlobalVariables(ProcessType pType) +void +SetProcessGlobalVariables(ProcessType pType) { processType = pType; myProcPid = getpid(); diff --git a/src/main/pool_internal_comms.c b/src/main/pool_internal_comms.c index 9aa653c60..35efd0465 100644 --- a/src/main/pool_internal_comms.c +++ b/src/main/pool_internal_comms.c @@ -42,7 +42,8 @@ * sends the signal to pgpool-II main process to terminate Pgpool-II * process. */ -bool terminate_pgpool(char mode, bool error) +bool +terminate_pgpool(char mode, bool error) { pid_t ppid = getppid(); @@ -71,7 +72,7 @@ bool terminate_pgpool(char mode, bool error) } else { - ereport(error?ERROR:WARNING, + ereport(error ? ERROR : WARNING, (errmsg("error while processing shutdown request"), errdetail("invalid shutdown mode \"%c\"", mode))); return false; |
