From a8232337d83f519ea693f29840c59a35d3e47920 Mon Sep 17 00:00:00 2001 From: Ian Barwick Date: Wed, 14 Feb 2018 11:28:38 +0900 Subject: [PATCH] Catch various corner cases when restarting a PostgreSQL instance --- repmgr-action-standby.c | 86 ++++++++++++++++++++++++++++++++++------- repmgr-client-global.h | 1 + repmgr-client.c | 59 +++++++++++++--------------- 3 files changed, 101 insertions(+), 45 deletions(-) diff --git a/repmgr-action-standby.c b/repmgr-action-standby.c index 6da5e1d2..a0a349f6 100644 --- a/repmgr-action-standby.c +++ b/repmgr-action-standby.c @@ -2078,28 +2078,88 @@ do_standby_follow_internal(PGconn *primary_conn, t_node_info *primary_node_recor if (server_up == true) { - action = "restart"; - get_server_action(ACTION_RESTART, server_command, config_file_options.data_directory); + /* no "service_restart_command" defined - stop and start using pg_ctl*/ + if (config_file_options.service_restart_command[0] == '\0') + { + action = "stopp"; /* sic */ + get_server_action(ACTION_STOP_WAIT, server_command, config_file_options.data_directory); + + /* if translation needed, generate messages in the preceding if/else */ + log_notice(_("%sing server using \"%s\""), + action, + server_command); + + success = local_command(server_command, &output_buf); + + if (success == false) + { + log_error(_("unable to %s server"), action); + + *error_code = ERR_NO_RESTART; + return false; + } + + action = "start"; + get_server_action(ACTION_START, server_command, config_file_options.data_directory); + + /* if translation needed, generate messages in the preceding if/else */ + log_notice(_("%sing server using \"%s\""), + action, + server_command); + + success = local_command(server_command, &output_buf); + + if (success == false) + { + log_error(_("unable to %s server"), action); + + *error_code = ERR_NO_RESTART; + return false; + } + + } + else + { + action = "restart"; + get_server_action(ACTION_RESTART, server_command, config_file_options.data_directory); + + /* if translation needed, generate messages in the preceding if/else */ + log_notice(_("%sing server using \"%s\""), + action, + server_command); + + success = local_command(server_command, &output_buf); + + if (success == false) + { + log_error(_("unable to %s server"), action); + + *error_code = ERR_NO_RESTART; + return false; + } + + } } else { + action = "start"; get_server_action(ACTION_START, server_command, config_file_options.data_directory); - } - /* if translation needed, generate messages in the preceding if/else */ - log_notice(_("%sing server using \"%s\""), - action, - server_command); + /* if translation needed, generate messages in the preceding if/else */ + log_notice(_("%sing server using \"%s\""), + action, + server_command); - success = local_command(server_command, &output_buf); + success = local_command(server_command, &output_buf); - if (success == false) - { - log_error(_("unable to %s server"), action); + if (success == false) + { + log_error(_("unable to %s server"), action); - *error_code = ERR_NO_RESTART; - return false; + *error_code = ERR_NO_RESTART; + return false; + } } } diff --git a/repmgr-client-global.h b/repmgr-client-global.h index c5810f64..fe7f8036 100644 --- a/repmgr-client-global.h +++ b/repmgr-client-global.h @@ -181,6 +181,7 @@ typedef enum ACTION_NONE, ACTION_START, ACTION_STOP, + ACTION_STOP_WAIT, ACTION_RESTART, ACTION_RELOAD, ACTION_PROMOTE diff --git a/repmgr-client.c b/repmgr-client.c index b00566b1..595f1436 100644 --- a/repmgr-client.c +++ b/repmgr-client.c @@ -2137,7 +2137,8 @@ local_command(const char *command, PQExpBufferData *outputbuf) retval = pclose(fp); - success = (WEXITSTATUS(retval) == 0) ? true : false; + /* */ + success = (WEXITSTATUS(retval) == 0 || WEXITSTATUS(retval) == 141) ? true : false; log_verbose(LOG_DEBUG, "result of command was %i (%i)", WEXITSTATUS(retval), retval); @@ -2459,18 +2460,15 @@ get_server_action(t_server_action action, char *script, char *data_dir) { initPQExpBuffer(&command); - appendPQExpBuffer( - &command, + appendPQExpBuffer(&command, "%s %s -w -D ", make_pg_path("pg_ctl"), config_file_options.pg_ctl_options); - appendShellString( - &command, + appendShellString(&command, data_dir); - appendPQExpBuffer( - &command, + appendPQExpBuffer(&command, " start"); strncpy(script, command.data, MAXLEN); @@ -2482,6 +2480,7 @@ get_server_action(t_server_action action, char *script, char *data_dir) } case ACTION_STOP: + case ACTION_STOP_WAIT: { if (config_file_options.service_stop_command[0] != '\0') { @@ -2491,19 +2490,23 @@ get_server_action(t_server_action action, char *script, char *data_dir) else { initPQExpBuffer(&command); - appendPQExpBuffer( - &command, + appendPQExpBuffer(&command, "%s %s -D ", make_pg_path("pg_ctl"), config_file_options.pg_ctl_options); - appendShellString( - &command, + appendShellString(&command, data_dir); - appendPQExpBuffer( - &command, - " -m fast -W stop"); + if (action == ACTION_STOP_WAIT) + appendPQExpBuffer(&command, + " -w"); + else + appendPQExpBuffer(&command, + " -W"); + + appendPQExpBuffer(&command, + " -m fast stop"); strncpy(script, command.data, MAXLEN); @@ -2522,18 +2525,15 @@ get_server_action(t_server_action action, char *script, char *data_dir) else { initPQExpBuffer(&command); - appendPQExpBuffer( - &command, + appendPQExpBuffer(&command, "%s %s -w -D ", make_pg_path("pg_ctl"), config_file_options.pg_ctl_options); - appendShellString( - &command, + appendShellString(&command, data_dir); - appendPQExpBuffer( - &command, + appendPQExpBuffer(&command, " restart"); strncpy(script, command.data, MAXLEN); @@ -2553,18 +2553,15 @@ get_server_action(t_server_action action, char *script, char *data_dir) else { initPQExpBuffer(&command); - appendPQExpBuffer( - &command, + appendPQExpBuffer(&command, "%s %s -w -D ", make_pg_path("pg_ctl"), config_file_options.pg_ctl_options); - appendShellString( - &command, + appendShellString(&command, data_dir); - appendPQExpBuffer( - &command, + appendPQExpBuffer(&command, " reload"); strncpy(script, command.data, MAXLEN); @@ -2585,18 +2582,15 @@ get_server_action(t_server_action action, char *script, char *data_dir) else { initPQExpBuffer(&command); - appendPQExpBuffer( - &command, + appendPQExpBuffer(&command, "%s %s -w -D ", make_pg_path("pg_ctl"), config_file_options.pg_ctl_options); - appendShellString( - &command, + appendShellString(&command, data_dir); - appendPQExpBuffer( - &command, + appendPQExpBuffer(&command, " promote"); strncpy(script, command.data, MAXLEN); @@ -2630,6 +2624,7 @@ data_dir_required_for_action(t_server_action action) return true; case ACTION_STOP: + case ACTION_STOP_WAIT: if (config_file_options.service_stop_command[0] != '\0') { return false;