repmgr: various fixes for "master unregister"

This commit is contained in:
Ian Barwick
2017-06-11 23:27:37 +09:00
parent fef184ce9a
commit aa53514f9f
5 changed files with 83 additions and 19 deletions

View File

@@ -1277,6 +1277,32 @@ get_master_node_record(PGconn *conn, t_node_info *node_info)
}
/*
* Get the local node record; if this fails, exit. Many operations
* depend on this being available, so we'll centralize the check
* and failure messages here.
*/
bool
get_local_node_record(PGconn *conn, int node_id, t_node_info *node_info)
{
bool record_found;
record_found = get_node_record(conn, node_id, node_info);
if (record_found == false)
{
log_error(_("unable to retrieve record for local node"));
log_detail(_("local node id is %i"), node_id);
log_hint(_("check this node was correctly registered"));
PQfinish(conn);
exit(ERR_BAD_CONFIG);
}
return record_found;
}
bool
create_node_record(PGconn *conn, char *repmgr_action, t_node_info *node_info)
{

View File

@@ -188,6 +188,7 @@ const char * get_node_type_string(t_server_type type);
int get_node_record(PGconn *conn, int node_id, t_node_info *node_info);
int get_node_record_by_name(PGconn *conn, const char *node_name, t_node_info *node_info);
bool get_local_node_record(PGconn *conn, int node_id, t_node_info *node_info);
bool get_master_node_record(PGconn *conn, t_node_info *node_info);
bool create_node_record(PGconn *conn, char *repmgr_action, t_node_info *node_info);

View File

@@ -218,7 +218,6 @@ do_master_unregister(void)
PGconn *master_conn = NULL;
PGconn *local_conn = NULL;
t_node_info local_node_info = T_NODE_INFO_INITIALIZER;
bool record_found;
t_node_info *target_node_info_ptr;
PGconn *target_node_conn = NULL;
@@ -226,24 +225,35 @@ do_master_unregister(void)
/* We must be able to connect to the local node */
local_conn = establish_db_connection(config_file_options.conninfo, true);
/* From which we obtain a connection to the master node */
master_conn = establish_master_db_connection(local_conn, true);
/* Get local node record */
get_local_node_record(local_conn, config_file_options.node_id, &local_node_info);
/* Obtain a connection to the master node */
master_conn = establish_master_db_connection(local_conn, false);
if (PQstatus(master_conn) != CONNECTION_OK)
{
t_node_info master_node_info;
log_error(_("unable to connect to master server"));
if (get_master_node_record(local_conn, &master_node_info))
{
log_detail(_("current master registered as node %s (id: %i, conninfo: \"%s\")"),
master_node_info.node_name,
master_node_info.node_id,
master_node_info.conninfo);
}
log_hint(_("you may need to promote this standby or ask it to look for a new master to follow"));
PQfinish(local_conn);
exit(ERR_DB_CONN);
}
/* Local connection no longer required */
PQfinish(local_conn);
/* Get local node record */
record_found = get_node_record(master_conn, config_file_options.node_id, &local_node_info);
// XXX add function get_local_node_record() which aborts as below
if (record_found == FALSE)
{
log_error(_("unable to retrieve record for local node"));
log_detail(_("local node id is %i"), config_file_options.node_id);
log_hint(_("check this node was correctly registered"));
exit(ERR_BAD_CONFIG);
}
/* Target node is local node? */
if (target_node_info.node_id == UNKNOWN_NODE_ID
@@ -308,7 +318,7 @@ do_master_unregister(void)
t_node_info master_node_info = T_NODE_INFO_INITIALIZER;
bool master_record_found;
master_record_found = get_master_node_record(local_conn, &master_node_info);
master_record_found = get_master_node_record(master_conn, &master_node_info);
if (master_record_found == false)
{
@@ -356,7 +366,7 @@ do_master_unregister(void)
}
}
// check if any records point to this record, detail: each, hint: follow or unregister
// check if any records point to this record, detail: each, hint: follow or unregister standby(s)
if (runtime_options.dry_run == true)
{
@@ -367,6 +377,7 @@ do_master_unregister(void)
}
else
{
PQExpBufferData event_details;
bool delete_success = delete_node_record(master_conn,
target_node_info_ptr->node_id);
@@ -379,6 +390,28 @@ do_master_unregister(void)
exit(ERR_DB_QUERY);
}
initPQExpBuffer(&event_details);
appendPQExpBuffer(&event_details,
_("node %s (id: %i) unregistered"),
target_node_info_ptr->node_name,
target_node_info_ptr->node_id);
if (target_node_info_ptr->node_id != config_file_options.node_id)
{
appendPQExpBuffer(&event_details,
_(" from node %s (id: %i)"),
config_file_options.node_name,
config_file_options.node_id);
}
create_event_record(master_conn,
&config_file_options,
config_file_options.node_id,
"master_unregister",
true,
event_details.data);
termPQExpBuffer(&event_details);
log_info(_("node %s (id: %i) was successfully unregistered"),
target_node_info_ptr->node_name,
target_node_info_ptr->node_id);

View File

@@ -407,7 +407,7 @@ do_standby_clone(void)
/* Add details about relevant runtime options used */
appendPQExpBuffer(&event_details,
_("Cloned from host '%s', port %s"),
_("cloned from host '%s', port %s"),
runtime_options.host,
runtime_options.port);
@@ -1177,12 +1177,15 @@ do_standby_promote(void)
void
do_standby_follow(void)
{
puts("not implemented");
return;
}
void
do_standby_switchover(void)
{
puts("not implemented");
return;
}

View File

@@ -768,6 +768,8 @@ main(int argc, char **argv)
{
PGconn *conn;
int record_found;
log_verbose(LOG_DEBUG, "connecting to local node to retrieve record for node specified with --node-id or --node-name");
conn = establish_db_connection(config_file_options.conninfo, true);
if (runtime_options.node_id != UNKNOWN_NODE_ID)
@@ -781,7 +783,6 @@ main(int argc, char **argv)
PQfinish(conn);
exit(ERR_BAD_CONFIG);
}
printf("xXX %s\n", target_node_info.node_name);
}
else if (runtime_options.node_name[0] != '\0')
{