Add function create_event_record()

For logging an event to the event table without generating an external
event notification.

Rename existing create_event_record*() functions to create_event_notification*()
as this describes their function better.
This commit is contained in:
Ian Barwick
2017-07-05 09:52:22 +09:00
parent d3ec15cd25
commit 617dee6bd6
6 changed files with 62 additions and 42 deletions

View File

@@ -28,7 +28,7 @@ static void _populate_node_record(PGresult *res, t_node_info *node_info, int row
static void _populate_node_records(PGresult *res, NodeInfoList *node_list);
static bool _create_update_node_record(PGconn *conn, char *action, t_node_info *node_info);
static bool _create_event_record(PGconn *conn, t_configuration_options *options, int node_id, char *event, bool successful, char *details, t_event_info *event_info);
static bool _create_event(PGconn *conn, t_configuration_options *options, int node_id, char *event, bool successful, char *details, t_event_info *event_info, bool send_notification);
/* ================= */
/* utility functions */
@@ -1783,46 +1783,65 @@ clear_node_info_list(NodeInfoList *nodes)
/* event record functions */
/* ====================== */
/*
* create_event_record()
*
* Create a record in the "events" table, but don't execute the
* "event_notification_command".
*/
bool
create_event_record(PGconn *conn, t_configuration_options *options, int node_id, char *event, bool successful, char *details)
{
/* create dummy t_event_info */
t_event_info event_info = T_EVENT_INFO_INITIALIZER;
return _create_event(conn, options, node_id, event, successful, details, &event_info, false);
}
/*
* create_event_notification()
*
* If `conn` is not NULL, insert a record into the events table.
*
* If configuration parameter `event_notification_command` is set, also
* If configuration parameter "event_notification_command" is set, also
* attempt to execute that command.
*
* Returns true if all operations succeeded, false if one or more failed.
*
* Note this function may be called with `conn` set to NULL in cases where
* Note this function may be called with "conn" set to NULL in cases where
* the primary node is not available and it's therefore not possible to write
* an event record. In this case, if `event_notification_command` is set, a
* user-defined notification to be generated; if not, this function will have
* no effect.
*/
bool
create_event_record(PGconn *conn, t_configuration_options *options, int node_id, char *event, bool successful, char *details)
create_event_notification(PGconn *conn, t_configuration_options *options, int node_id, char *event, bool successful, char *details)
{
/* create dummy t_event_info */
t_event_info event_info = T_EVENT_INFO_INITIALIZER;
return _create_event_record(conn, options, node_id, event, successful, details, &event_info);
return _create_event(conn, options, node_id, event, successful, details, &event_info, true);
}
/*
* create_event_record_extended()
* create_event_notification_extended()
*
* The caller may need to pass additional parameters to the event notification
* command (currently only the conninfo string of another node)
*/
bool
create_event_record_extended(PGconn *conn, t_configuration_options *options, int node_id, char *event, bool successful, char *details, t_event_info *event_info)
create_event_notification_extended(PGconn *conn, t_configuration_options *options, int node_id, char *event, bool successful, char *details, t_event_info *event_info)
{
return _create_event_record(conn, options, node_id, event, successful, details, event_info);
return _create_event(conn, options, node_id, event, successful, details, event_info, true);
}
static bool
_create_event_record(PGconn *conn, t_configuration_options *options, int node_id, char *event, bool successful, char *details, t_event_info *event_info)
_create_event(PGconn *conn, t_configuration_options *options, int node_id, char *event, bool successful, char *details, t_event_info *event_info, bool send_notification)
{
PQExpBufferData query;
PGresult *res;
@@ -1865,7 +1884,7 @@ _create_event_record(PGconn *conn, t_configuration_options *options, int node_id
" VALUES ($1, $2, $3, $4) "
" RETURNING event_timestamp ");
log_verbose(LOG_DEBUG, "create_event_record():\n %s", query.data);
log_verbose(LOG_DEBUG, "_create_event():\n %s", query.data);
res = PQexecParams(conn,
query.data,
@@ -1910,10 +1929,10 @@ _create_event_record(PGconn *conn, t_configuration_options *options, int node_id
strftime(event_timestamp, MAXLEN, "%Y-%m-%d %H:%M:%S%z", &ts);
}
log_verbose(LOG_DEBUG, "create_event_record(): Event timestamp is \"%s\"\n", event_timestamp);
log_verbose(LOG_DEBUG, "_create_event(): Event timestamp is \"%s\"", event_timestamp);
/* an event notification command was provided - parse and execute it */
if (strlen(options->event_notification_command))
if (send_notification == true && strlen(options->event_notification_command))
{
char parsed_command[MAXPGPATH];
const char *src_ptr;
@@ -1974,7 +1993,7 @@ _create_event_record(PGconn *conn, t_configuration_options *options, int node_id
src_ptr++;
if (event_info->node_name != NULL)
{
log_debug("node_name: %s\n", event_info->node_name);
log_verbose(LOG_DEBUG, "node_name: %s\n", event_info->node_name);
strlcpy(dst_ptr, event_info->node_name, end_ptr - dst_ptr);
dst_ptr += strlen(dst_ptr);
}
@@ -2033,7 +2052,7 @@ _create_event_record(PGconn *conn, t_configuration_options *options, int node_id
*dst_ptr = '\0';
log_debug("create_event_record(): executing\n%s", parsed_command);
log_debug("_create_event(): executing\n%s", parsed_command);
r = system(parsed_command);
if (r != 0)

View File

@@ -221,14 +221,15 @@ bool update_node_record(PGconn *conn, char *repmgr_action, t_node_info *node_in
bool delete_node_record(PGconn *conn, int node);
bool update_node_record_set_primary(PGconn *conn, int this_node_id);
bool update_node_record_set_upstream(PGconn *conn, int this_node_id, int new_upstream_node_id);
bool update_node_record_status(PGconn *conn, int this_node_id, char *type, int upstream_node_id, bool active);
bool update_node_record_set_upstream(PGconn *conn, int this_node_id, int new_upstream_node_id);
bool update_node_record_status(PGconn *conn, int this_node_id, char *type, int upstream_node_id, bool active);
void clear_node_info_list(NodeInfoList *nodes);
void clear_node_info_list(NodeInfoList *nodes);
/* event record functions */
bool create_event_record(PGconn *conn, t_configuration_options *options, int node_id, char *event, bool successful, char *details);
bool create_event_record_extended(PGconn *conn, t_configuration_options *options, int node_id, char *event, bool successful, char *details, t_event_info *event_info);
/* event functions */
bool create_event_record(PGconn *conn, t_configuration_options *options, int node_id, char *event, bool successful, char *details);
bool create_event_notification(PGconn *conn, t_configuration_options *options, int node_id, char *event, bool successful, char *details);
bool create_event_notification_extended(PGconn *conn, t_configuration_options *options, int node_id, char *event, bool successful, char *details, t_event_info *event_info);
/* replication slot functions */
bool create_replication_slot(PGconn *conn, char *slot_name, int server_version_num, PQExpBufferData *error_msg);

View File

@@ -206,7 +206,7 @@ do_primary_register(void)
}
/* Log the event */
create_event_record(conn,
create_event_notification(conn,
&config_file_options,
config_file_options.node_id,
"primary_register",
@@ -494,7 +494,7 @@ do_primary_unregister(void)
config_file_options.node_id);
}
create_event_record(primary_conn,
create_event_notification(primary_conn,
&config_file_options,
config_file_options.node_id,
"primary_unregister",

View File

@@ -452,7 +452,7 @@ do_standby_clone(void)
_("; --force: %s"),
runtime_options.force ? "Y" : "N");
create_event_record(primary_conn,
create_event_notification(primary_conn,
&config_file_options,
config_file_options.node_id,
"standby_clone",
@@ -819,7 +819,7 @@ do_standby_register(void)
{
/* XXX add event description */
create_event_record(primary_conn,
create_event_notification(primary_conn,
&config_file_options,
config_file_options.node_id,
"standby_register",
@@ -834,7 +834,7 @@ do_standby_register(void)
}
/* Log the event */
create_event_record(primary_conn,
create_event_notification(primary_conn,
&config_file_options,
config_file_options.node_id,
"standby_register",
@@ -1013,7 +1013,7 @@ do_standby_unregister(void)
}
/* Log the event */
create_event_record(primary_conn,
create_event_notification(primary_conn,
&config_file_options,
target_node_id,
"standby_unregister",
@@ -1193,7 +1193,7 @@ do_standby_promote(void)
log_error("%s", details.data);
create_event_record(NULL,
create_event_notification(NULL,
&config_file_options,
config_file_options.node_id,
"standby_promote",
@@ -1213,7 +1213,7 @@ do_standby_promote(void)
log_detail("%s", details.data);
/* Log the event */
create_event_record(conn,
create_event_notification(conn,
&config_file_options,
config_file_options.node_id,
"standby_promote",
@@ -1357,7 +1357,7 @@ do_standby_follow(void)
{
log_error("%s", event_details.data);
create_event_record(primary_conn,
create_event_notification(primary_conn,
&config_file_options,
config_file_options.node_id,
"standby_follow",
@@ -1543,7 +1543,7 @@ do_standby_follow(void)
log_notice(_("STANDBY FOLLOW successful"));
create_event_record(primary_conn,
create_event_notification(primary_conn,
&config_file_options,
config_file_options.node_id,
"standby_follow",
@@ -2066,7 +2066,7 @@ initialise_direct_clone(void)
{
log_error("%s", event_details.data);
create_event_record(primary_conn,
create_event_notification(primary_conn,
&config_file_options,
config_file_options.node_id,
"standby_clone",

View File

@@ -1413,7 +1413,7 @@ create_repmgr_extension(PGconn *conn)
log_notice(_("\"repmgr\" extension successfully installed"));
create_event_record(conn,
create_event_notification(conn,
&config_file_options,
config_file_options.node_id,
"cluster_created",

View File

@@ -489,7 +489,7 @@ monitor_streaming_primary(void)
local_node_info.node_name,
local_node_info.node_id);
create_event_record(local_conn,
create_event_notification(local_conn,
&config_file_options,
config_file_options.node_id,
"repmgrd_start",
@@ -533,7 +533,7 @@ monitor_streaming_primary(void)
PQfinish(local_conn);
/* */
create_event_record(NULL,
create_event_notification(NULL,
&config_file_options,
config_file_options.node_id,
"repmgrd_local_disconnect",
@@ -560,7 +560,7 @@ monitor_streaming_primary(void)
(int)local_node_unreachable_elapsed);
log_notice("%s", event_details.data);
create_event_record(local_conn,
create_event_notification(local_conn,
&config_file_options,
config_file_options.node_id,
"repmgrd_local_reconnect",
@@ -674,7 +674,7 @@ monitor_streaming_standby(void)
upstream_node_info.node_name,
upstream_node_info.node_id);
create_event_record(upstream_conn,
create_event_notification(upstream_conn,
&config_file_options,
config_file_options.node_id,
"repmgrd_start",
@@ -1076,7 +1076,7 @@ do_upstream_standby_failover(void)
* table but we should be able to generate an external notification
* if required.
*/
create_event_record(primary_conn,
create_event_notification(primary_conn,
&config_file_options,
local_node_info.node_id,
"repmgrd_failover_follow",
@@ -1097,7 +1097,7 @@ do_upstream_standby_failover(void)
log_error("%s", event_details.data);
create_event_record(NULL,
create_event_notification(NULL,
&config_file_options,
local_node_info.node_id,
"repmgrd_failover_follow",
@@ -1116,7 +1116,7 @@ do_upstream_standby_failover(void)
log_notice("%s", event_details.data);
create_event_record(primary_conn,
create_event_notification(primary_conn,
&config_file_options,
local_node_info.node_id,
"repmgrd_failover_follow",
@@ -1205,7 +1205,7 @@ promote_self(void)
failed_primary.node_name,
failed_primary.node_id);
create_event_record(upstream_conn,
create_event_notification(upstream_conn,
&config_file_options,
local_node_info.node_id,
"repmgrd_failover_abort",
@@ -1244,7 +1244,7 @@ promote_self(void)
failed_primary.node_id);
/* local_conn is now the primary connection */
create_event_record(local_conn,
create_event_notification(local_conn,
&config_file_options,
local_node_info.node_id,
"repmgrd_failover_promote",
@@ -1472,7 +1472,7 @@ follow_new_primary(int new_primary_id)
log_notice("%s\n", event_details.data);
create_event_record(upstream_conn,
create_event_notification(upstream_conn,
&config_file_options,
local_node_info.node_id,
"repmgrd_failover_follow",