From a55d7a4bd3d0c811ae063998a153c1979583b08f Mon Sep 17 00:00:00 2001 From: Jaime Casanova Date: Tue, 12 Jun 2012 23:23:49 -0500 Subject: [PATCH] getMasterConnection() cannot avoid checking the same node that asks to find the master. This was a micro optimization based on the fact that all commands that needed to detect the master were executed from the standby but now that we have CLUSTER level commands that is not true anymore --- dbutils.c | 6 +++--- dbutils.h | 2 +- repmgr.c | 12 +++++------- repmgrd.c | 4 ++-- 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/dbutils.c b/dbutils.c index c3a3be22..d5ed556c 100644 --- a/dbutils.c +++ b/dbutils.c @@ -202,7 +202,7 @@ get_cluster_size(PGconn *conn) * connection string is placed there. */ PGconn * -getMasterConnection(PGconn *standby_conn, int id, char *cluster, +getMasterConnection(PGconn *standby_conn, char *cluster, int *master_id, char *master_conninfo_out) { PGconn *master_conn = NULL; @@ -242,8 +242,8 @@ getMasterConnection(PGconn *standby_conn, int id, char *cluster, cluster); sqlquery_snprintf(sqlquery, "SELECT * FROM %s.repl_nodes " - " WHERE cluster = '%s' and id <> %d", - schema_quoted, cluster, id); + " WHERE cluster = '%s'", + schema_quoted, cluster); res1 = PQexec(standby_conn, sqlquery); if (PQresultStatus(res1) != PGRES_TUPLES_OK) diff --git a/dbutils.h b/dbutils.h index c09a8658..c60129c3 100644 --- a/dbutils.h +++ b/dbutils.h @@ -31,7 +31,7 @@ char *pg_version(PGconn *conn, char* major_version); bool guc_setted(PGconn *conn, const char *parameter, const char *op, const char *value); const char *get_cluster_size(PGconn *conn); -PGconn *getMasterConnection(PGconn *standby_conn, int id, char *cluster, +PGconn *getMasterConnection(PGconn *standby_conn, char *cluster, int *master_id, char *master_conninfo_out); #endif diff --git a/repmgr.c b/repmgr.c index d23fe80c..8b73838f 100644 --- a/repmgr.c +++ b/repmgr.c @@ -413,7 +413,7 @@ do_cluster_cleanup(void) /* check if there is a master in this cluster */ log_info(_("%s connecting to master database\n"), progname); - master_conn = getMasterConnection(conn, options.node, options.cluster_name, + master_conn = getMasterConnection(conn, options.cluster_name, &master_id, NULL); if (!master_conn) { @@ -613,8 +613,7 @@ do_master_register(void) int id; /* Ensure there isn't any other master already registered */ - master_conn = getMasterConnection(conn, options.node, - options.cluster_name, &id,NULL); + master_conn = getMasterConnection(conn, options.cluster_name, &id,NULL); if (master_conn != NULL) { PQfinish(master_conn); @@ -733,7 +732,7 @@ do_standby_register(void) /* check if there is a master in this cluster */ log_info(_("%s connecting to master database\n"), progname); - master_conn = getMasterConnection(conn, options.node, options.cluster_name, + master_conn = getMasterConnection(conn, options.cluster_name, &master_id, NULL); if (!master_conn) { @@ -1320,7 +1319,7 @@ do_standby_promote(void) } /* we also need to check if there isn't any master already */ - old_master_conn = getMasterConnection(conn, options.node, options.cluster_name, + old_master_conn = getMasterConnection(conn, options.cluster_name, &old_master_id, NULL); if (old_master_conn != NULL) { @@ -1424,8 +1423,7 @@ do_standby_follow(void) /* we also need to check if there is any master in the cluster */ log_info(_("%s connecting to master database\n"), progname); - master_conn = getMasterConnection(conn, options.node, - options.cluster_name, &master_id,(char *) &master_conninfo); + master_conn = getMasterConnection(conn, options.cluster_name, &master_id,(char *) &master_conninfo); if (master_conn == NULL) { PQfinish(conn); diff --git a/repmgrd.c b/repmgrd.c index 87214a74..cc0eba42 100644 --- a/repmgrd.c +++ b/repmgrd.c @@ -185,7 +185,7 @@ main(int argc, char **argv) /* I need the id of the primary as well as a connection to it */ log_info(_("%s Connecting to primary for cluster '%s'\n"), progname, local_options.cluster_name); - primaryConn = getMasterConnection(myLocalConn, local_options.node, + primaryConn = getMasterConnection(myLocalConn, local_options.cluster_name, &primary_options.node,NULL); if (primaryConn == NULL) @@ -269,7 +269,7 @@ MonitorExecute(void) log_err(_("We couldn't reconnect to master. Now checking if another node has been promoted.\n")); for (connection_retries = 0; connection_retries < 6; connection_retries++) { - primaryConn = getMasterConnection(myLocalConn, local_options.node, + primaryConn = getMasterConnection(myLocalConn, local_options.cluster_name, &primary_options.node,NULL); if (PQstatus(primaryConn) == CONNECTION_OK) {