From bfd3aed33bdf9933fad7ad26c8f9b02ba1704f25 Mon Sep 17 00:00:00 2001 From: Dan Farina Date: Sun, 6 Feb 2011 18:32:49 -0800 Subject: [PATCH] Fix a myriad of problems introduced by merging Signed-off-by: Dan Farina --- config.c | 4 +-- config.h | 15 ++++++--- dbutils.c | 2 +- repmgr.c | 97 ++++++++++++++++++++++++++++++------------------------- repmgr.h | 9 +++--- repmgrd.c | 8 +++-- strutil.h | 3 ++ 7 files changed, 79 insertions(+), 59 deletions(-) diff --git a/config.c b/config.c index 5fdd411e..868ec72a 100644 --- a/config.c +++ b/config.c @@ -17,14 +17,12 @@ * */ +#include "config.h" #include "repmgr.h" - #include "strutil.h" - void parse_config(const char *config_file, repmgr_config *config) - char *conninfo) { char *s, buff[MAXLINELENGTH]; char name[MAXLEN]; diff --git a/config.h b/config.h index b66bc6e4..9241d1d0 100644 --- a/config.h +++ b/config.h @@ -16,15 +16,22 @@ * along with this program. If not, see . * */ +#ifndef _CONFIG_H_ +#define _CONFIG_H_ + +#include "strutil.h" + typedef struct { - char cluster_name[MAXLEN]; - int node; - char conninfo[MAXLEN]; - char rsync_options[QUERY_STR_LEN]; + char cluster_name[MAXLEN]; + int node; + char conninfo[MAXLEN]; + char rsync_options[QUERY_STR_LEN]; } repmgr_config; void parse_config(const char *config_file, repmgr_config *config); void parse_line(char *buff, char *name, char *value); char *trim(char *s); + +#endif diff --git a/dbutils.c b/dbutils.c index 2ac009b3..b2c90ea5 100644 --- a/dbutils.c +++ b/dbutils.c @@ -199,7 +199,7 @@ getMasterConnection(PGconn *standby_conn, int id, char *cluster, /* find all nodes belonging to this cluster */ sqlquery_snprintf(sqlquery, "SELECT * FROM repmgr_%s.repl_nodes " " WHERE cluster = '%s' and id <> %d", - cluster, cluster, id); + cluster, cluster, id); res1 = PQexec(standby_conn, sqlquery); if (PQresultStatus(res1) != PGRES_TUPLES_OK) diff --git a/repmgr.c b/repmgr.c index d9376701..eebf06f6 100644 --- a/repmgr.c +++ b/repmgr.c @@ -341,7 +341,8 @@ do_master_register(void) } /* Check if there is a schema for this cluster */ - sqlquery_sprintf(sqlquery, "SELECT 1 FROM pg_namespace WHERE nspname = 'repmgr_%s'", config.cluster_name); + sqlquery_snprintf(sqlquery, "SELECT 1 FROM pg_namespace " + "WHERE nspname = 'repmgr_%s'", config.cluster_name); res = PQexec(conn, sqlquery); if (PQresultStatus(res) != PGRES_TUPLES_OK) { @@ -372,7 +373,7 @@ do_master_register(void) if (!PQexec(conn, sqlquery)) { fprintf(stderr, "Cannot create the schema repmgr_%s: %s\n", - config.cluster_name, PQerrorMessage(conn)); + config.cluster_name, PQerrorMessage(conn)); PQfinish(conn); return; } @@ -381,11 +382,11 @@ do_master_register(void) sqlquery_snprintf(sqlquery, "CREATE TABLE repmgr_%s.repl_nodes ( " " id integer primary key, " " cluster text not null, " - " conninfo text not null)", config.cluster_name); + " conninfo text not null)", config.cluster_name); if (!PQexec(conn, sqlquery)) { fprintf(stderr, - config.cluster_name, PQerrorMessage(conn)); + config.cluster_name, PQerrorMessage(conn)); PQfinish(conn); return; } @@ -397,33 +398,34 @@ do_master_register(void) " last_wal_primary_location TEXT NOT NULL, " " last_wal_standby_location TEXT NOT NULL, " " replication_lag BIGINT NOT NULL, " - " apply_lag BIGINT NOT NULL) ", config.cluster_name); - myClusterName); - if (!PQexec(conn, sqlquery)) - { - fprintf(stderr, - config.cluster_name, PQerrorMessage(conn)); - PQfinish(conn); - return; - } + " apply_lag BIGINT NOT NULL) ", + config.cluster_name); + } - /* and the view */ - sqlquery_snprintf(sqlquery, "CREATE VIEW repmgr_%s.repl_status AS " - " WITH monitor_info AS (SELECT *, ROW_NUMBER() OVER (PARTITION BY primary_node, standby_node " - " ORDER BY last_monitor_time desc) " - " FROM repmgr_%s.repl_monitor) " - " SELECT primary_node, standby_node, last_monitor_time, last_wal_primary_location, " - " last_wal_standby_location, pg_size_pretty(replication_lag) replication_lag, " - " pg_size_pretty(apply_lag) apply_lag, age(now(), last_monitor_time) AS time_lag " - " FROM monitor_info a " - " WHERE row_number = 1", config.cluster_name, config.cluster_name); - if (!PQexec(conn, sqlquery)) - { - fprintf(stderr, - config.cluster_name, PQerrorMessage(conn)); - PQfinish(conn); - return; - } + if (!PQexec(conn, sqlquery)) + { + fprintf(stderr, + config.cluster_name, PQerrorMessage(conn)); + PQfinish(conn); + return; + } + + /* and the view */ + sqlquery_snprintf(sqlquery, "CREATE VIEW repmgr_%s.repl_status AS " + " WITH monitor_info AS (SELECT *, ROW_NUMBER() OVER (PARTITION BY primary_node, standby_node " + " ORDER BY last_monitor_time desc) " + " FROM repmgr_%s.repl_monitor) " + " SELECT primary_node, standby_node, last_monitor_time, last_wal_primary_location, " + " last_wal_standby_location, pg_size_pretty(replication_lag) replication_lag, " + " pg_size_pretty(apply_lag) apply_lag, age(now(), last_monitor_time) AS time_lag " + " FROM monitor_info a " + " WHERE row_number = 1", config.cluster_name, config.cluster_name); + if (!PQexec(conn, sqlquery)) + { + fprintf(stderr, + config.cluster_name, PQerrorMessage(conn)); + PQfinish(conn); + return; } else { @@ -431,8 +433,9 @@ do_master_register(void) int id; /* Ensure there isn't any other master already registered */ - master_conn = getMasterConnection(conn, config.node, config.cluster_name, &id); - NULL); + master_conn = getMasterConnection(conn, config.node, + config.cluster_name, &id, NULL); + if (master_conn != NULL) { PQfinish(master_conn); @@ -446,7 +449,7 @@ do_master_register(void) { sqlquery_snprintf(sqlquery, "DELETE FROM repmgr_%s.repl_nodes " " WHERE id = %d", - config.cluster_name, config.node); + config.cluster_name, config.node); if (!PQexec(conn, sqlquery)) { @@ -459,7 +462,8 @@ do_master_register(void) sqlquery_snprintf(sqlquery, "INSERT INTO repmgr_%s.repl_nodes " "VALUES (%d, '%s', '%s')", - config.cluster_name, config.node, config.cluster_name, config.conninfo); + config.cluster_name, config.node, config.cluster_name, + config.conninfo); if (!PQexec(conn, sqlquery)) { @@ -508,9 +512,9 @@ do_standby_register(void) } /* Check if there is a schema for this cluster */ - sqlquery_snprintf(sqlquery, "SELECT 1 FROM pg_namespace WHERE nspname = 'repmgr_%s'", config.cluster_name); - "SELECT 1 FROM pg_namespace WHERE nspname = 'repmgr_%s'", - myClusterName); + sqlquery_snprintf(sqlquery, "SELECT 1 FROM pg_namespace " + "WHERE nspname = 'repmgr_%s'", config.cluster_name); + res = PQexec(conn, sqlquery); if (PQresultStatus(res) != PGRES_TUPLES_OK) { @@ -531,8 +535,9 @@ do_standby_register(void) PQclear(res); /* check if there is a master in this cluster */ - master_conn = getMasterConnection(conn, config.node, config.cluster_name, &master_id); + master_conn = getMasterConnection(conn, config.node, config.cluster_name, &master_id, NULL); + if (!master_conn) return; @@ -1056,7 +1061,8 @@ do_standby_promote(void) } /* we also need to check if there isn't any master already */ - old_master_conn = getMasterConnection(conn, config.node, config.cluster_name, &old_master_id); + old_master_conn = getMasterConnection(conn, config.node, config.cluster_name, + &old_master_id, NULL); if (old_master_conn != NULL) { @@ -1152,7 +1158,8 @@ do_standby_follow(void) } /* we also need to check if there is any master in the cluster */ - master_conn = getMasterConnection(conn, config.node, config.cluster_name, &master_id); + master_conn = getMasterConnection(conn, config.node, config.cluster_name, + &master_id, (char *) &master_conninfo); if (master_conn == NULL) { @@ -1366,11 +1373,13 @@ copy_remote_files(char *host, char *remote_user, char *remote_path, char host_string[MAXLEN]; int r; - if (strnlen(config.rsync_options, QUERY_STR_LEN) == 0) - sprintf(options, "--archive --checksum --compress --progress --rsh=ssh"); + if (strnlen(config.rsync_options, MAXLEN) == 0) + maxlen_snprintf( + options, "%s", + "--archive --checksum --compress --progress --rsh=ssh"); else - strncpy(options, config.rsync_options, QUERY_STR_LEN); - + maxlen_snprintf(options, "%s", config.rsync_options); + if (force) strcat(options, " --delete"); diff --git a/repmgr.h b/repmgr.h index accf6d0e..770063b1 100644 --- a/repmgr.h +++ b/repmgr.h @@ -17,14 +17,14 @@ * along with this program. If not, see . * */ - -#ifndef _REPMGR_H_ -#define _REPMGR_H_ - #include "postgres_fe.h" #include "getopt_long.h" #include "libpq-fe.h" + +#ifndef _REPMGR_H_ +#define _REPMGR_H_ + #include "dbutils.h" @@ -32,7 +32,6 @@ #define STANDBY_MODE 1 #define CONFIG_FILE "repmgr.conf" -#define QUERY_STR_LEN 8192 #include "config.h" diff --git a/repmgrd.c b/repmgrd.c index bfde9fea..f31c452f 100644 --- a/repmgrd.c +++ b/repmgrd.c @@ -178,7 +178,9 @@ main(int argc, char **argv) else { /* I need the id of the primary as well as a connection to it */ - primaryConn = getMasterConnection(myLocalConn, config.node, config.cluster_name, &primaryId); + primaryConn = getMasterConnection(myLocalConn, config.node, + config.cluster_name, &primaryId, + NULL); if (primaryConn == NULL) exit(1); @@ -249,7 +251,9 @@ MonitorExecute(void) for (connection_retries = 0; connection_retries < 6; connection_retries++) { - primaryConn = getMasterConnection(myLocalConn, config.node, config.cluster_name, &primaryId); + primaryConn = getMasterConnection(myLocalConn, config.node, + config.cluster_name, &primaryId, + NULL); if (PQstatus(primaryConn) == CONNECTION_OK) { diff --git a/strutil.h b/strutil.h index 68c6b5c2..41d9626e 100644 --- a/strutil.h +++ b/strutil.h @@ -8,6 +8,9 @@ #ifndef _STRUTIL_H_ #define _STRUTIL_H_ +#include + + #define QUERY_STR_LEN 8192 #define MAXLEN 1024 #define MAXLINELENGTH 4096