From 5bd8cf958aaf8ff0b03ba638bd8a17945b58ef39 Mon Sep 17 00:00:00 2001 From: Ian Barwick Date: Wed, 10 Jan 2018 10:58:01 +0900 Subject: [PATCH] repmgr standby switchover: add "%p" event notification parameter This will contain the node ID of the former primary. --- HISTORY | 5 ++++- dbutils.c | 17 ++++++++++++-- dbutils.h | 4 +++- doc/event-notifications.sgml | 13 ++++++++++- doc/repmgr-standby-switchover.sgml | 7 +++++- repmgr-action-standby.c | 36 +++++++++++++++++------------- 6 files changed, 60 insertions(+), 22 deletions(-) diff --git a/HISTORY b/HISTORY index 964e65cb..c0fad065 100644 --- a/HISTORY +++ b/HISTORY @@ -2,7 +2,10 @@ repmgr: enable documentation to be build as single HTML file; GitHub #353 (fanf2) repmgr: add missing -W option to getopt_long() invocation; GitHub #350 (Ian) repmgr: recognize "--terse" option for "repmgr cluster event"; GitHub #360 (Ian) - docs: various fixes (Ian, Daymel, Martín, ams) + repmgr: add "--wait-start" option for "repmgr standby register"; GitHub #356 (Ian) + repmgr: add "%p" event notification parameter for "repmgr standby switchover" + containing the node ID of the demoted primary (Ian) + docs: various fixes and updates (Ian, Daymel, Martín, ams) 4.0.1 2017-12-13 repmgr: ensure "repmgr node check --action=" returns appropriate return diff --git a/dbutils.c b/dbutils.c index b631e031..ef35bc3e 100644 --- a/dbutils.c +++ b/dbutils.c @@ -2962,7 +2962,6 @@ create_event_record(PGconn *conn, t_configuration_options *options, int node_id, } - /* * create_event_notification() * @@ -3063,7 +3062,7 @@ _create_event(PGconn *conn, t_configuration_options *options, int node_id, char if (PQresultStatus(res) != PGRES_TUPLES_OK) { - /* we don't treat this as an error */ + /* we don't treat this as a fatal error */ log_warning(_("unable to create event record:\n %s"), PQerrorMessage(conn)); @@ -3216,6 +3215,20 @@ _create_event(PGconn *conn, t_configuration_options *options, int node_id, char dst_ptr += strlen(dst_ptr); } break; + case 'p': + /* %p: former primary id ("repmgr standby switchover") */ + src_ptr++; + if (event_info->former_primary_id != UNKNOWN_NODE_ID) + { + PQExpBufferData node_id; + initPQExpBuffer(&node_id); + appendPQExpBuffer(&node_id, + "%i", event_info->former_primary_id); + strlcpy(dst_ptr, node_id.data, end_ptr - dst_ptr); + dst_ptr += strlen(dst_ptr); + termPQExpBuffer(&node_id); + } + break; default: /* otherwise treat the % as not special */ if (dst_ptr < end_ptr) diff --git a/dbutils.h b/dbutils.h index 214cb0a8..45e2bb3c 100644 --- a/dbutils.h +++ b/dbutils.h @@ -174,11 +174,13 @@ typedef struct s_event_info { char *node_name; char *conninfo_str; + int former_primary_id; } t_event_info; #define T_EVENT_INFO_INITIALIZER { \ NULL, \ - NULL \ + NULL, \ + UNKNOWN_NODE_ID \ } diff --git a/doc/event-notifications.sgml b/doc/event-notifications.sgml index 7b8dfa4c..faf11f67 100644 --- a/doc/event-notifications.sgml +++ b/doc/event-notifications.sgml @@ -63,7 +63,7 @@ - success (1 or 0) + success (1) or failure (0) @@ -84,6 +84,17 @@ + + + + + + + node ID of the demoted standby ( only) + + + + The values provided for %t and %d diff --git a/doc/repmgr-standby-switchover.sgml b/doc/repmgr-standby-switchover.sgml index 47c60204..dada6a6b 100644 --- a/doc/repmgr-standby-switchover.sgml +++ b/doc/repmgr-standby-switchover.sgml @@ -109,9 +109,14 @@ Event notifications standby_switchover and standby_promote - event notification will be generated for the new primary, + event notifications will be generated for the new primary, and a node_rejoin event notification for the former primary (new standby). + + If using an event notification script, standby_switchover + will populate the placeholder parameter %p with the node ID of + the former standby. + diff --git a/repmgr-action-standby.c b/repmgr-action-standby.c index 65e8735c..a1710e2f 100644 --- a/repmgr-action-standby.c +++ b/repmgr-action-standby.c @@ -2016,6 +2016,8 @@ do_standby_switchover(void) NodeInfoList sibling_nodes = T_NODE_INFO_LIST_INITIALIZER; int unreachable_sibling_node_count = 0; + t_event_info event_info = T_EVENT_INFO_INITIALIZER; + /* * SANITY CHECKS * @@ -2114,6 +2116,8 @@ do_standby_switchover(void) log_verbose(LOG_DEBUG, "remote node name is \"%s\"", remote_node_record.node_name); + /* this will fill the %p event notification parameter */ + event_info.former_primary_id = remote_node_record.node_id; /* * If --force-rewind specified, check pg_rewind can be used, and @@ -2790,8 +2794,7 @@ do_standby_switchover(void) log_debug("executing:\n %s", remote_command_str.data); initPQExpBuffer(&command_output); - command_success = remote_command( - remote_host, + command_success = remote_command(remote_host, runtime_options.remote_user, remote_command_str.data, &command_output); @@ -2807,12 +2810,13 @@ do_standby_switchover(void) if (strlen(command_output.data) > 2) log_detail("%s", command_output.data); - create_event_record(local_conn, - &config_file_options, - config_file_options.node_id, - "standby_switchover", - false, - command_output.data); + create_event_notification_extended(local_conn, + &config_file_options, + config_file_options.node_id, + "standby_switchover", + false, + command_output.data, + &event_info); } else { @@ -2825,12 +2829,13 @@ do_standby_switchover(void) config_file_options.node_id, remote_node_record.node_id); - create_event_record(local_conn, - &config_file_options, - config_file_options.node_id, - "standby_switchover", - true, - event_details.data); + create_event_notification_extended(local_conn, + &config_file_options, + config_file_options.node_id, + "standby_switchover", + true, + event_details.data, + &event_info); termPQExpBuffer(&event_details); } @@ -3730,8 +3735,7 @@ initialise_direct_clone(t_node_info *node_record) { log_error("%s", event_details.data); - create_event_notification( - primary_conn, + create_event_notification(primary_conn, &config_file_options, config_file_options.node_id, "standby_clone",