From 3b96b2afcede1ca3408404ae0d6d54f79d06d118 Mon Sep 17 00:00:00 2001 From: Ian Barwick Date: Tue, 23 Apr 2019 16:50:17 +0900 Subject: [PATCH] "primary register": ensure --force works if another primary is registered but not running --- doc/appendix-release-notes.sgml | 7 +++-- repmgr-action-primary.c | 52 ++++++++++++++++----------------- 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/doc/appendix-release-notes.sgml b/doc/appendix-release-notes.sgml index 0a378f5a..8798f3e4 100644 --- a/doc/appendix-release-notes.sgml +++ b/doc/appendix-release-notes.sgml @@ -48,9 +48,10 @@ - &repmgr;: when executing repmgr standby clone - in mode, ensure provision of the option - does not result in an existing data directory being modified in any way. + &repmgr;: when executing repmgr primary register + with the option, if another primary record exists but the associated node is + unreachable (or running as a standby), set that node's record to inactive to enable the current node + to be registered as a primary. diff --git a/repmgr-action-primary.c b/repmgr-action-primary.c index 8176b03f..63db2979 100644 --- a/repmgr-action-primary.c +++ b/repmgr-action-primary.c @@ -96,28 +96,6 @@ do_primary_register(void) initialize_voting_term(conn); - /* Ensure there isn't another registered node which is primary */ - primary_conn = get_primary_connection(conn, ¤t_primary_id, NULL); - - if (primary_conn != NULL) - { - if (current_primary_id != config_file_options.node_id) - { - /* - * it's impossible to add a second primary to a streaming - * replication cluster - */ - log_error(_("there is already an active registered primary (node ID: %i) in this cluster"), current_primary_id); - PQfinish(primary_conn); - PQfinish(conn); - exit(ERR_BAD_CONFIG); - } - - /* we've probably connected to ourselves */ - PQfinish(primary_conn); - } - - begin_transaction(conn); /* @@ -128,12 +106,32 @@ do_primary_register(void) current_primary_id = get_primary_node_id(conn); if (current_primary_id != NODE_NOT_FOUND && current_primary_id != config_file_options.node_id) { - log_error(_("another node with id %i is already registered as primary"), current_primary_id); - log_detail(_("a streaming replication cluster can have only one primary node")); + log_debug("XXX %i", current_primary_id); + primary_conn = establish_primary_db_connection(conn, false); - rollback_transaction(conn); - PQfinish(conn); - exit(ERR_BAD_CONFIG); + if (PQstatus(primary_conn) == CONNECTION_OK) + { + if (get_recovery_type(primary_conn) == RECTYPE_PRIMARY) + { + log_error(_("there is already an active registered primary (node ID: %i) in this cluster"), + current_primary_id); + log_detail(_("a streaming replication cluster can have only one primary node")); + + log_hint(_("ensure this node is shut down before registering a new primary")); + PQfinish(primary_conn); + rollback_transaction(conn); + PQfinish(conn); + exit(ERR_BAD_CONFIG); + } + + log_warning(_("node %is is registered as primary but running as a standby"), + current_primary_id); + PQfinish(primary_conn); + } + + log_notice(_("setting node %i's node record to inactive"), + current_primary_id); + update_node_record_set_active(conn, current_primary_id, false); } /*