repmgr standby switchover: add "%p" event notification parameter

This will contain the node ID of the former primary.
This commit is contained in:
Ian Barwick
2018-01-10 10:58:01 +09:00
parent 5a45997db5
commit 5bd8cf958a
6 changed files with 60 additions and 22 deletions

View File

@@ -2,7 +2,10 @@
repmgr: enable documentation to be build as single HTML file; GitHub #353 (fanf2) 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: add missing -W option to getopt_long() invocation; GitHub #350 (Ian)
repmgr: recognize "--terse" option for "repmgr cluster event"; GitHub #360 (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 4.0.1 2017-12-13
repmgr: ensure "repmgr node check --action=" returns appropriate return repmgr: ensure "repmgr node check --action=" returns appropriate return

View File

@@ -2962,7 +2962,6 @@ create_event_record(PGconn *conn, t_configuration_options *options, int node_id,
} }
/* /*
* create_event_notification() * 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) 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"), log_warning(_("unable to create event record:\n %s"),
PQerrorMessage(conn)); PQerrorMessage(conn));
@@ -3216,6 +3215,20 @@ _create_event(PGconn *conn, t_configuration_options *options, int node_id, char
dst_ptr += strlen(dst_ptr); dst_ptr += strlen(dst_ptr);
} }
break; 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: default:
/* otherwise treat the % as not special */ /* otherwise treat the % as not special */
if (dst_ptr < end_ptr) if (dst_ptr < end_ptr)

View File

@@ -174,11 +174,13 @@ typedef struct s_event_info
{ {
char *node_name; char *node_name;
char *conninfo_str; char *conninfo_str;
int former_primary_id;
} t_event_info; } t_event_info;
#define T_EVENT_INFO_INITIALIZER { \ #define T_EVENT_INFO_INITIALIZER { \
NULL, \ NULL, \
NULL \ NULL, \
UNKNOWN_NODE_ID \
} }

View File

@@ -63,7 +63,7 @@
<term><option>%s</option></term> <term><option>%s</option></term>
<listitem> <listitem>
<para> <para>
success (1 or 0) success (1) or failure (0)
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
@@ -84,6 +84,17 @@
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term><option>%p</option></term>
<listitem>
<para>
node ID of the demoted standby (<xref linkend="repmgr-standby-switchover"> only)
</para>
</listitem>
</varlistentry>
</variablelist> </variablelist>
<para> <para>
The values provided for <literal>%t</literal> and <literal>%d</literal> The values provided for <literal>%t</literal> and <literal>%d</literal>

View File

@@ -109,9 +109,14 @@
<title>Event notifications</title> <title>Event notifications</title>
<para> <para>
<literal>standby_switchover</literal> and <literal>standby_promote</literal> <literal>standby_switchover</literal> and <literal>standby_promote</literal>
<link linkend="event-notifications">event notification</link> will be generated for the new primary, <link linkend="event-notifications">event notifications</link> will be generated for the new primary,
and a <literal>node_rejoin</literal> event notification for the former primary (new standby). and a <literal>node_rejoin</literal> event notification for the former primary (new standby).
</para> </para>
<para>
If using an event notification script, <literal>standby_switchover</literal>
will populate the placeholder parameter <literal>%p</literal> with the node ID of
the former standby.
</para>
</refsect1> </refsect1>

View File

@@ -2016,6 +2016,8 @@ do_standby_switchover(void)
NodeInfoList sibling_nodes = T_NODE_INFO_LIST_INITIALIZER; NodeInfoList sibling_nodes = T_NODE_INFO_LIST_INITIALIZER;
int unreachable_sibling_node_count = 0; int unreachable_sibling_node_count = 0;
t_event_info event_info = T_EVENT_INFO_INITIALIZER;
/* /*
* SANITY CHECKS * SANITY CHECKS
* *
@@ -2114,6 +2116,8 @@ do_standby_switchover(void)
log_verbose(LOG_DEBUG, "remote node name is \"%s\"", remote_node_record.node_name); 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 * 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); log_debug("executing:\n %s", remote_command_str.data);
initPQExpBuffer(&command_output); initPQExpBuffer(&command_output);
command_success = remote_command( command_success = remote_command(remote_host,
remote_host,
runtime_options.remote_user, runtime_options.remote_user,
remote_command_str.data, remote_command_str.data,
&command_output); &command_output);
@@ -2807,12 +2810,13 @@ do_standby_switchover(void)
if (strlen(command_output.data) > 2) if (strlen(command_output.data) > 2)
log_detail("%s", command_output.data); log_detail("%s", command_output.data);
create_event_record(local_conn, create_event_notification_extended(local_conn,
&config_file_options, &config_file_options,
config_file_options.node_id, config_file_options.node_id,
"standby_switchover", "standby_switchover",
false, false,
command_output.data); command_output.data,
&event_info);
} }
else else
{ {
@@ -2825,12 +2829,13 @@ do_standby_switchover(void)
config_file_options.node_id, config_file_options.node_id,
remote_node_record.node_id); remote_node_record.node_id);
create_event_record(local_conn, create_event_notification_extended(local_conn,
&config_file_options, &config_file_options,
config_file_options.node_id, config_file_options.node_id,
"standby_switchover", "standby_switchover",
true, true,
event_details.data); event_details.data,
&event_info);
termPQExpBuffer(&event_details); termPQExpBuffer(&event_details);
} }
@@ -3730,8 +3735,7 @@ initialise_direct_clone(t_node_info *node_record)
{ {
log_error("%s", event_details.data); log_error("%s", event_details.data);
create_event_notification( create_event_notification(primary_conn,
primary_conn,
&config_file_options, &config_file_options,
config_file_options.node_id, config_file_options.node_id,
"standby_clone", "standby_clone",