Skip to content

Commit

Permalink
Ensure 'master register --force' can't create more than one active pr…
Browse files Browse the repository at this point in the history
…imary node record
  • Loading branch information
ibarwick committed Nov 26, 2015
1 parent 9ed71d6 commit 67df082
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
26 changes: 20 additions & 6 deletions repmgr.c
Expand Up @@ -793,6 +793,8 @@ do_master_register(void)
bool schema_exists = false;
int ret;

int primary_node_id = UNKNOWN_NODE_ID;

bool record_created;

conn = establish_db_connection(options.conninfo, true);
Expand Down Expand Up @@ -849,19 +851,29 @@ do_master_register(void)

PQfinish(master_conn);

/* XXX we should check if a node with a different ID is registered as
master, otherwise it would be possible to insert a duplicate record
with --force, which would result in an unwelcome "multi-master" situation
begin_transaction(conn);

/*
* Check if a node with a different ID is registered as primary. This shouldn't
* happen but could do if an existing master was shut down without being
* unregistered.
*/

primary_node_id = get_master_node_id(conn, options.cluster_name);
if (primary_node_id != NODE_NOT_FOUND && primary_node_id != options.node)
{
log_err(_("another node with id %i is already registered as master\n"), primary_node_id);
rollback_transaction(conn);
PQfinish(conn);
exit(ERR_BAD_CONFIG);
}

/* Delete any existing record for this node if --force set */
if (runtime_options.force)
{
PGresult *res;
bool node_record_deleted;

begin_transaction(conn);

res = get_node_record(conn, options.cluster_name, options.node);
if (PQntuples(res))
{
Expand All @@ -878,7 +890,6 @@ do_master_register(void)
}
}

commit_transaction(conn);
}


Expand All @@ -896,10 +907,13 @@ do_master_register(void)

if (record_created == false)
{
rollback_transaction(conn);
PQfinish(conn);
exit(ERR_DB_QUERY);
}

commit_transaction(conn);

/* Log the event */
create_event_record(conn,
&options,
Expand Down
2 changes: 1 addition & 1 deletion repmgr.h
Expand Up @@ -48,7 +48,7 @@
#define AUTOMATIC_FAILOVER 1
#define NODE_NOT_FOUND -1
#define NO_UPSTREAM_NODE -1

#define UNKNOWN_NODE_ID -1



Expand Down

0 comments on commit 67df082

Please sign in to comment.