From bde8c7e29cd3b655a6171c5277d1eef48df42ff1 Mon Sep 17 00:00:00 2001 From: Ian Barwick Date: Wed, 6 Mar 2019 14:54:05 +0900 Subject: [PATCH] repmgrd: handle reconnect to restarted server when using "connection" checks --- configfile.c | 2 +- repmgrd-physical.c | 8 ++++---- repmgrd.c | 26 ++++++++++++++++---------- repmgrd.h | 2 +- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/configfile.c b/configfile.c index 1b6dd1ea..677e1920 100644 --- a/configfile.c +++ b/configfile.c @@ -632,7 +632,7 @@ _parse_config(t_configuration_options *options, ItemList *error_list, ItemList * else { item_list_append(error_list, - _("value for \"connection_check_type\" must be \"ping\" or \"connect\"\n")); + _("value for \"connection_check_type\" must be \"ping\" or \"connection\"\n")); } } diff --git a/repmgrd-physical.c b/repmgrd-physical.c index 0280217b..2ffdaa92 100644 --- a/repmgrd-physical.c +++ b/repmgrd-physical.c @@ -831,7 +831,7 @@ monitor_streaming_standby(void) while (true) { log_verbose(LOG_DEBUG, "checking %s", upstream_node_info.conninfo); - if (check_upstream_connection(upstream_conn, upstream_node_info.conninfo) == true) + if (check_upstream_connection(&upstream_conn, upstream_node_info.conninfo) == true) { set_upstream_last_seen(local_conn); } @@ -1030,7 +1030,7 @@ monitor_streaming_standby(void) upstream_node_info.node_id, degraded_monitoring_elapsed); - if (check_upstream_connection(upstream_conn, upstream_node_info.conninfo) == true) + if (check_upstream_connection(&upstream_conn, upstream_node_info.conninfo) == true) { if (config_file_options.connection_check_type == CHECK_PING) upstream_conn = establish_db_connection(upstream_node_info.conninfo, false); @@ -1605,7 +1605,7 @@ monitor_streaming_witness(void) while (true) { - if (check_upstream_connection(upstream_conn, upstream_node_info.conninfo) == false) + if (check_upstream_connection(&upstream_conn, upstream_node_info.conninfo) == false) { if (upstream_node_info.node_status == NODE_STATUS_UP) { @@ -1694,7 +1694,7 @@ monitor_streaming_witness(void) upstream_node_info.node_id, degraded_monitoring_elapsed); - if (check_upstream_connection(primary_conn, upstream_node_info.conninfo) == true) + if (check_upstream_connection(&primary_conn, upstream_node_info.conninfo) == true) { if (config_file_options.connection_check_type == CHECK_PING) primary_conn = establish_db_connection(upstream_node_info.conninfo, false); diff --git a/repmgrd.c b/repmgrd.c index d62d3fa7..faa47fef 100644 --- a/repmgrd.c +++ b/repmgrd.c @@ -819,7 +819,7 @@ show_help(void) bool -check_upstream_connection(PGconn *conn, const char *conninfo) +check_upstream_connection(PGconn **conn, const char *conninfo) { /* Check the connection status twice in case it changes after reset */ bool twice = false; @@ -829,30 +829,33 @@ check_upstream_connection(PGconn *conn, const char *conninfo) for (;;) { - if (PQstatus(conn) != CONNECTION_OK) + if (PQstatus(*conn) != CONNECTION_OK) { + log_debug("connection not OK"); if (twice) return false; - PQreset(conn); /* reconnect */ + /* reconnect */ + PQfinish(*conn); + *conn = PQconnectdb(conninfo); twice = true; } else { - if (!cancel_query(conn, config_file_options.async_query_timeout)) + if (!cancel_query(*conn, config_file_options.async_query_timeout)) goto failed; - if (wait_connection_availability(conn, config_file_options.async_query_timeout) != 1) + if (wait_connection_availability(*conn, config_file_options.async_query_timeout) != 1) goto failed; /* execute a simple query to verify connection availability */ - if (PQsendQuery(conn, "SELECT 1") == 0) + if (PQsendQuery(*conn, "SELECT 1") == 0) { log_warning(_("unable to send query to upstream")); - log_detail("%s", PQerrorMessage(conn)); + log_detail("%s", PQerrorMessage(*conn)); goto failed; } - if (wait_connection_availability(conn, config_file_options.async_query_timeout) != 1) + if (wait_connection_availability(*conn, config_file_options.async_query_timeout) != 1) goto failed; break; @@ -861,7 +864,10 @@ check_upstream_connection(PGconn *conn, const char *conninfo) /* retry once */ if (twice) return false; - PQreset(conn); /* reconnect */ + + /* reconnect */ + PQfinish(*conn); + *conn = PQconnectdb(conninfo); twice = true; } } @@ -926,7 +932,7 @@ try_reconnect(PGconn **conn, t_node_info *node_info) if (ping_result != PGRES_TUPLES_OK) { - log_info("original connnection no longer available, using new connection"); + log_info("original connection no longer available, using new connection"); close_connection(conn); *conn = our_conn; } diff --git a/repmgrd.h b/repmgrd.h index 56c1df70..20fb0118 100644 --- a/repmgrd.h +++ b/repmgrd.h @@ -23,7 +23,7 @@ extern PGconn *local_conn; extern bool startup_event_logged; extern char pid_file[MAXPGPATH]; -bool check_upstream_connection(PGconn *conn, const char *conninfo); +bool check_upstream_connection(PGconn **conn, const char *conninfo); void try_reconnect(PGconn **conn, t_node_info *node_info); int calculate_elapsed(instr_time start_time);