mirror of
https://github.com/EnterpriseDB/repmgr.git
synced 2026-03-26 08:36:30 +00:00
Fix a myriad of problems introduced by merging
Signed-off-by: Dan Farina <drfarina@acm.org>
This commit is contained in:
4
config.c
4
config.c
@@ -17,14 +17,12 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
#include "repmgr.h"
|
#include "repmgr.h"
|
||||||
|
|
||||||
#include "strutil.h"
|
#include "strutil.h"
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
parse_config(const char *config_file, repmgr_config *config)
|
parse_config(const char *config_file, repmgr_config *config)
|
||||||
char *conninfo)
|
|
||||||
{
|
{
|
||||||
char *s, buff[MAXLINELENGTH];
|
char *s, buff[MAXLINELENGTH];
|
||||||
char name[MAXLEN];
|
char name[MAXLEN];
|
||||||
|
|||||||
15
config.h
15
config.h
@@ -16,15 +16,22 @@
|
|||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
#ifndef _CONFIG_H_
|
||||||
|
#define _CONFIG_H_
|
||||||
|
|
||||||
|
#include "strutil.h"
|
||||||
|
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
char cluster_name[MAXLEN];
|
char cluster_name[MAXLEN];
|
||||||
int node;
|
int node;
|
||||||
char conninfo[MAXLEN];
|
char conninfo[MAXLEN];
|
||||||
char rsync_options[QUERY_STR_LEN];
|
char rsync_options[QUERY_STR_LEN];
|
||||||
} repmgr_config;
|
} repmgr_config;
|
||||||
|
|
||||||
void parse_config(const char *config_file, repmgr_config *config);
|
void parse_config(const char *config_file, repmgr_config *config);
|
||||||
void parse_line(char *buff, char *name, char *value);
|
void parse_line(char *buff, char *name, char *value);
|
||||||
char *trim(char *s);
|
char *trim(char *s);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|||||||
@@ -199,7 +199,7 @@ getMasterConnection(PGconn *standby_conn, int id, char *cluster,
|
|||||||
/* find all nodes belonging to this cluster */
|
/* find all nodes belonging to this cluster */
|
||||||
sqlquery_snprintf(sqlquery, "SELECT * FROM repmgr_%s.repl_nodes "
|
sqlquery_snprintf(sqlquery, "SELECT * FROM repmgr_%s.repl_nodes "
|
||||||
" WHERE cluster = '%s' and id <> %d",
|
" WHERE cluster = '%s' and id <> %d",
|
||||||
cluster, cluster, id);
|
cluster, cluster, id);
|
||||||
|
|
||||||
res1 = PQexec(standby_conn, sqlquery);
|
res1 = PQexec(standby_conn, sqlquery);
|
||||||
if (PQresultStatus(res1) != PGRES_TUPLES_OK)
|
if (PQresultStatus(res1) != PGRES_TUPLES_OK)
|
||||||
|
|||||||
97
repmgr.c
97
repmgr.c
@@ -341,7 +341,8 @@ do_master_register(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Check if there is a schema for this cluster */
|
/* 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);
|
res = PQexec(conn, sqlquery);
|
||||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||||
{
|
{
|
||||||
@@ -372,7 +373,7 @@ do_master_register(void)
|
|||||||
if (!PQexec(conn, sqlquery))
|
if (!PQexec(conn, sqlquery))
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Cannot create the schema repmgr_%s: %s\n",
|
fprintf(stderr, "Cannot create the schema repmgr_%s: %s\n",
|
||||||
config.cluster_name, PQerrorMessage(conn));
|
config.cluster_name, PQerrorMessage(conn));
|
||||||
PQfinish(conn);
|
PQfinish(conn);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -381,11 +382,11 @@ do_master_register(void)
|
|||||||
sqlquery_snprintf(sqlquery, "CREATE TABLE repmgr_%s.repl_nodes ( "
|
sqlquery_snprintf(sqlquery, "CREATE TABLE repmgr_%s.repl_nodes ( "
|
||||||
" id integer primary key, "
|
" id integer primary key, "
|
||||||
" cluster text not null, "
|
" cluster text not null, "
|
||||||
" conninfo text not null)", config.cluster_name);
|
" conninfo text not null)", config.cluster_name);
|
||||||
if (!PQexec(conn, sqlquery))
|
if (!PQexec(conn, sqlquery))
|
||||||
{
|
{
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
config.cluster_name, PQerrorMessage(conn));
|
config.cluster_name, PQerrorMessage(conn));
|
||||||
PQfinish(conn);
|
PQfinish(conn);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -397,33 +398,34 @@ do_master_register(void)
|
|||||||
" last_wal_primary_location TEXT NOT NULL, "
|
" last_wal_primary_location TEXT NOT NULL, "
|
||||||
" last_wal_standby_location TEXT NOT NULL, "
|
" last_wal_standby_location TEXT NOT NULL, "
|
||||||
" replication_lag BIGINT NOT NULL, "
|
" replication_lag BIGINT NOT NULL, "
|
||||||
" apply_lag BIGINT NOT NULL) ", config.cluster_name);
|
" apply_lag BIGINT NOT NULL) ",
|
||||||
myClusterName);
|
config.cluster_name);
|
||||||
if (!PQexec(conn, sqlquery))
|
}
|
||||||
{
|
|
||||||
fprintf(stderr,
|
|
||||||
config.cluster_name, PQerrorMessage(conn));
|
|
||||||
PQfinish(conn);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* and the view */
|
if (!PQexec(conn, sqlquery))
|
||||||
sqlquery_snprintf(sqlquery, "CREATE VIEW repmgr_%s.repl_status AS "
|
{
|
||||||
" WITH monitor_info AS (SELECT *, ROW_NUMBER() OVER (PARTITION BY primary_node, standby_node "
|
fprintf(stderr,
|
||||||
" ORDER BY last_monitor_time desc) "
|
config.cluster_name, PQerrorMessage(conn));
|
||||||
" FROM repmgr_%s.repl_monitor) "
|
PQfinish(conn);
|
||||||
" SELECT primary_node, standby_node, last_monitor_time, last_wal_primary_location, "
|
return;
|
||||||
" 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 "
|
/* and the view */
|
||||||
" WHERE row_number = 1", config.cluster_name, config.cluster_name);
|
sqlquery_snprintf(sqlquery, "CREATE VIEW repmgr_%s.repl_status AS "
|
||||||
if (!PQexec(conn, sqlquery))
|
" WITH monitor_info AS (SELECT *, ROW_NUMBER() OVER (PARTITION BY primary_node, standby_node "
|
||||||
{
|
" ORDER BY last_monitor_time desc) "
|
||||||
fprintf(stderr,
|
" FROM repmgr_%s.repl_monitor) "
|
||||||
config.cluster_name, PQerrorMessage(conn));
|
" SELECT primary_node, standby_node, last_monitor_time, last_wal_primary_location, "
|
||||||
PQfinish(conn);
|
" last_wal_standby_location, pg_size_pretty(replication_lag) replication_lag, "
|
||||||
return;
|
" 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
|
else
|
||||||
{
|
{
|
||||||
@@ -431,8 +433,9 @@ do_master_register(void)
|
|||||||
int id;
|
int id;
|
||||||
|
|
||||||
/* Ensure there isn't any other master already registered */
|
/* Ensure there isn't any other master already registered */
|
||||||
master_conn = getMasterConnection(conn, config.node, config.cluster_name, &id);
|
master_conn = getMasterConnection(conn, config.node,
|
||||||
NULL);
|
config.cluster_name, &id, NULL);
|
||||||
|
|
||||||
if (master_conn != NULL)
|
if (master_conn != NULL)
|
||||||
{
|
{
|
||||||
PQfinish(master_conn);
|
PQfinish(master_conn);
|
||||||
@@ -446,7 +449,7 @@ do_master_register(void)
|
|||||||
{
|
{
|
||||||
sqlquery_snprintf(sqlquery, "DELETE FROM repmgr_%s.repl_nodes "
|
sqlquery_snprintf(sqlquery, "DELETE FROM repmgr_%s.repl_nodes "
|
||||||
" WHERE id = %d",
|
" WHERE id = %d",
|
||||||
config.cluster_name, config.node);
|
config.cluster_name, config.node);
|
||||||
|
|
||||||
if (!PQexec(conn, sqlquery))
|
if (!PQexec(conn, sqlquery))
|
||||||
{
|
{
|
||||||
@@ -459,7 +462,8 @@ do_master_register(void)
|
|||||||
|
|
||||||
sqlquery_snprintf(sqlquery, "INSERT INTO repmgr_%s.repl_nodes "
|
sqlquery_snprintf(sqlquery, "INSERT INTO repmgr_%s.repl_nodes "
|
||||||
"VALUES (%d, '%s', '%s')",
|
"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))
|
if (!PQexec(conn, sqlquery))
|
||||||
{
|
{
|
||||||
@@ -508,9 +512,9 @@ do_standby_register(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Check if there is a schema for this cluster */
|
/* Check if there is a schema for this cluster */
|
||||||
sqlquery_snprintf(sqlquery, "SELECT 1 FROM pg_namespace WHERE nspname = 'repmgr_%s'", config.cluster_name);
|
sqlquery_snprintf(sqlquery, "SELECT 1 FROM pg_namespace "
|
||||||
"SELECT 1 FROM pg_namespace WHERE nspname = 'repmgr_%s'",
|
"WHERE nspname = 'repmgr_%s'", config.cluster_name);
|
||||||
myClusterName);
|
|
||||||
res = PQexec(conn, sqlquery);
|
res = PQexec(conn, sqlquery);
|
||||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||||
{
|
{
|
||||||
@@ -531,8 +535,9 @@ do_standby_register(void)
|
|||||||
PQclear(res);
|
PQclear(res);
|
||||||
|
|
||||||
/* check if there is a master in this cluster */
|
/* 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);
|
&master_id, NULL);
|
||||||
|
|
||||||
if (!master_conn)
|
if (!master_conn)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@@ -1056,7 +1061,8 @@ do_standby_promote(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* we also need to check if there isn't any master already */
|
/* 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)
|
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 */
|
/* 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)
|
if (master_conn == NULL)
|
||||||
{
|
{
|
||||||
@@ -1366,11 +1373,13 @@ copy_remote_files(char *host, char *remote_user, char *remote_path,
|
|||||||
char host_string[MAXLEN];
|
char host_string[MAXLEN];
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
if (strnlen(config.rsync_options, QUERY_STR_LEN) == 0)
|
if (strnlen(config.rsync_options, MAXLEN) == 0)
|
||||||
sprintf(options, "--archive --checksum --compress --progress --rsh=ssh");
|
maxlen_snprintf(
|
||||||
|
options, "%s",
|
||||||
|
"--archive --checksum --compress --progress --rsh=ssh");
|
||||||
else
|
else
|
||||||
strncpy(options, config.rsync_options, QUERY_STR_LEN);
|
maxlen_snprintf(options, "%s", config.rsync_options);
|
||||||
|
|
||||||
if (force)
|
if (force)
|
||||||
strcat(options, " --delete");
|
strcat(options, " --delete");
|
||||||
|
|
||||||
|
|||||||
9
repmgr.h
9
repmgr.h
@@ -17,14 +17,14 @@
|
|||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef _REPMGR_H_
|
|
||||||
#define _REPMGR_H_
|
|
||||||
|
|
||||||
#include "postgres_fe.h"
|
#include "postgres_fe.h"
|
||||||
#include "getopt_long.h"
|
#include "getopt_long.h"
|
||||||
#include "libpq-fe.h"
|
#include "libpq-fe.h"
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef _REPMGR_H_
|
||||||
|
#define _REPMGR_H_
|
||||||
|
|
||||||
#include "dbutils.h"
|
#include "dbutils.h"
|
||||||
|
|
||||||
|
|
||||||
@@ -32,7 +32,6 @@
|
|||||||
#define STANDBY_MODE 1
|
#define STANDBY_MODE 1
|
||||||
|
|
||||||
#define CONFIG_FILE "repmgr.conf"
|
#define CONFIG_FILE "repmgr.conf"
|
||||||
#define QUERY_STR_LEN 8192
|
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
|
|||||||
@@ -178,7 +178,9 @@ main(int argc, char **argv)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* I need the id of the primary as well as a connection to it */
|
/* 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)
|
if (primaryConn == NULL)
|
||||||
exit(1);
|
exit(1);
|
||||||
@@ -249,7 +251,9 @@ MonitorExecute(void)
|
|||||||
for (connection_retries = 0; connection_retries < 6;
|
for (connection_retries = 0; connection_retries < 6;
|
||||||
connection_retries++)
|
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)
|
if (PQstatus(primaryConn) == CONNECTION_OK)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user