From 4930c95ef7dff37bf40a3929fce18502742d8b35 Mon Sep 17 00:00:00 2001 From: Ian Barwick Date: Wed, 9 Aug 2017 19:31:42 +0900 Subject: [PATCH] Consolidate final output of "standby follow" / "node rejoin" --- repmgr-action-node.c | 33 +++++++++++++++- repmgr-action-standby.c | 88 ++++++++++++++++++++++------------------- repmgr-action-standby.h | 3 +- 3 files changed, 81 insertions(+), 43 deletions(-) diff --git a/repmgr-action-node.c b/repmgr-action-node.c index 1fab24cc..49fa6a3e 100644 --- a/repmgr-action-node.c +++ b/repmgr-action-node.c @@ -927,10 +927,13 @@ do_node_rejoin(void) PQExpBufferData command; PQExpBufferData command_output; + PQExpBufferData follow_output; struct stat statbuf; char filebuf[MAXPGPATH] = ""; t_node_info primary_node_record = T_NODE_INFO_INITIALIZER; + bool success; + /* check node is not actually running */ status = PQping(config_file_options.conninfo); @@ -1069,8 +1072,36 @@ do_node_rejoin(void) } } - do_standby_follow_internal(upstream_conn, &primary_node_record); + initPQExpBuffer(&follow_output); + success = do_standby_follow_internal( + upstream_conn, + &primary_node_record, + &follow_output); + + create_event_notification( + upstream_conn, + &config_file_options, + config_file_options.node_id, + "node_rejoin", + success, + follow_output.data); + + PQfinish(upstream_conn); + + if (success == false) + { + log_notice(_("NODE REJOIN failed")); + log_detail("%s", follow_output.data); + + termPQExpBuffer(&follow_output); + exit(ERR_DB_QUERY); + } + + log_notice(_("NODE REJOIN successful")); + log_detail("%s", follow_output.data); + + termPQExpBuffer(&follow_output); } /* diff --git a/repmgr-action-standby.c b/repmgr-action-standby.c index 15736aac..de3e27a9 100644 --- a/repmgr-action-standby.c +++ b/repmgr-action-standby.c @@ -1226,6 +1226,9 @@ do_standby_follow(void) int timer; + PQExpBufferData follow_output; + bool success; + log_verbose(LOG_DEBUG, "do_standby_follow()"); @@ -1268,7 +1271,38 @@ do_standby_follow(void) get_node_record(primary_conn, primary_id, &primary_node_record); - do_standby_follow_internal(primary_conn, &primary_node_record); + initPQExpBuffer(&follow_output); + + success = do_standby_follow_internal( + primary_conn, + &primary_node_record, + &follow_output); + + create_event_notification( + primary_conn, + &config_file_options, + config_file_options.node_id, + "standby_follow", + success, + follow_output.data); + + PQfinish(primary_conn); + + if (success == false) + { + log_notice(_("STANDBY FOLLOW failed")); + log_detail("%s", follow_output.data); + + termPQExpBuffer(&follow_output); + exit(ERR_DB_QUERY); + } + + log_notice(_("STANDBY FOLLOW successful")); + log_detail("%s", follow_output.data); + + termPQExpBuffer(&follow_output); + + return; } @@ -1276,8 +1310,8 @@ do_standby_follow(void) * Perform the actuall "follow" operation; this is executed by * "node rejoin" too. */ -void -do_standby_follow_internal(PGconn *primary_conn, t_node_info *primary_node_record) +bool +do_standby_follow_internal(PGconn *primary_conn, t_node_info *primary_node_record, PQExpBufferData *output) { t_node_info local_node_record = T_NODE_INFO_INITIALIZER; int original_upstream_node_id = UNKNOWN_NODE_ID; @@ -1285,7 +1319,6 @@ do_standby_follow_internal(PGconn *primary_conn, t_node_info *primary_node_recor int r; RecordStatus record_status; - PQExpBufferData event_details; char *errmsg = NULL; @@ -1315,28 +1348,16 @@ do_standby_follow_internal(PGconn *primary_conn, t_node_info *primary_node_recor { int primary_server_version_num = get_server_version(primary_conn, NULL); - initPQExpBuffer(&event_details); if (create_replication_slot(primary_conn, local_node_record.slot_name, primary_server_version_num, - &event_details) == false) + output) == false) { - log_error("%s", event_details.data); + log_error("%s", output->data); - create_event_notification( - primary_conn, - &config_file_options, - config_file_options.node_id, - "standby_follow", - false, - event_details.data); - - PQfinish(primary_conn); - exit(ERR_DB_QUERY); + return false; } - - termPQExpBuffer(&event_details); } @@ -1477,34 +1498,19 @@ do_standby_follow_internal(PGconn *primary_conn, t_node_info *primary_node_recor primary_node_record->node_id, true) == false) { - log_error(_("unable to update upstream node")); - PQfinish(primary_conn); - - exit(ERR_BAD_CONFIG); + appendPQExpBuffer(output, + _("unable to update upstream node")); + return false; } - // XXX return to caller - log_notice(_("STANDBY FOLLOW successful")); - initPQExpBuffer(&event_details); - appendPQExpBuffer(&event_details, + appendPQExpBuffer(output, _("node %i is now attached to node %i"), - config_file_options.node_id, primary_node_record->node_id); + config_file_options.node_id, + primary_node_record->node_id); - create_event_notification(primary_conn, - &config_file_options, - config_file_options.node_id, - "standby_follow", - true, - event_details.data); - log_detail("%s", event_details.data); - - termPQExpBuffer(&event_details); - - PQfinish(primary_conn); - - return; + return true; } diff --git a/repmgr-action-standby.h b/repmgr-action-standby.h index 4d06ecad..5344738f 100644 --- a/repmgr-action-standby.h +++ b/repmgr-action-standby.h @@ -11,9 +11,10 @@ extern void do_standby_register(void); extern void do_standby_unregister(void); extern void do_standby_promote(void); extern void do_standby_follow(void); -extern void do_standby_follow_internal(PGconn *primary_conn, t_node_info *primary_node_record); extern void do_standby_switchover(void); +extern bool do_standby_follow_internal(PGconn *primary_conn, t_node_info *primary_node_record, PQExpBufferData *output); + #endif /* _REPMGR_ACTION_STANDBY_H_ */