mirror of
https://github.com/EnterpriseDB/repmgr.git
synced 2026-03-28 01:16:29 +00:00
Attack of whitespace pedantry
pgsql conventions (tabs, four-spaces-wide, etc) applied all around. Also tried to fix some very tiny capitalization errors, auto-fill problems, and some inter-block vertical whitespacing issues. Long strings in repmgr.c were left intact, though. They are rather numerous and are less of a problem than tiny bits of function calls and comments wrapping over a line; the latter kind of problem has been mostly fixed. Signed-off-by: Dan Farina <drfarina@acm.org> Signed-off-by: Peter van Hardenberg <pvh@heroku.com>
This commit is contained in:
committed by
Peter van Hardenberg
parent
56c65acd99
commit
af2edf10a0
6
config.c
6
config.c
@@ -8,7 +8,8 @@
|
||||
#include "repmgr.h"
|
||||
|
||||
void
|
||||
parse_config(const char *config_file, char *cluster_name, int *node, char *conninfo)
|
||||
parse_config(const char *config_file, char *cluster_name, int *node,
|
||||
char *conninfo)
|
||||
{
|
||||
char *s, buff[256];
|
||||
FILE *fp = fopen (config_file, "r");
|
||||
@@ -37,7 +38,8 @@ parse_config(const char *config_file, char *cluster_name, int *node, char *conni
|
||||
else if (strcmp(name, "conninfo") == 0)
|
||||
strncpy (conninfo, value, MAXLEN);
|
||||
else
|
||||
printf ("WARNING: %s/%s: Unknown name/value pair!\n", name, value);
|
||||
printf("WARNING: %s/%s: Unknown name/value pair!\n",
|
||||
name, value);
|
||||
}
|
||||
|
||||
/* Close file */
|
||||
|
||||
3
config.h
3
config.h
@@ -4,6 +4,7 @@
|
||||
*
|
||||
*/
|
||||
|
||||
void parse_config(const char *config_file, char *cluster_name, int *node, char *service);
|
||||
void parse_config(const char *config_file, char *cluster_name, int *node,
|
||||
char *service);
|
||||
void parse_line(char *buff, char *name, char *value);
|
||||
char *trim(char *s);
|
||||
|
||||
38
dbutils.c
38
dbutils.c
@@ -12,8 +12,10 @@ PGconn *
|
||||
establishDBConnection(const char *conninfo, const bool exit_on_error)
|
||||
{
|
||||
PGconn *conn;
|
||||
|
||||
/* Make a connection to the database */
|
||||
conn = PQconnectdb(conninfo);
|
||||
|
||||
/* Check to see that the backend connection was successfully made */
|
||||
if ((PQstatus(conn) != CONNECTION_OK))
|
||||
{
|
||||
@@ -38,6 +40,7 @@ is_standby(PGconn *conn)
|
||||
bool result;
|
||||
|
||||
res = PQexec(conn, "SELECT pg_is_in_recovery()");
|
||||
|
||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||
{
|
||||
fprintf(stderr, "Can't query server mode: %s", PQerrorMessage(conn));
|
||||
@@ -69,8 +72,12 @@ pg_version(PGconn *conn)
|
||||
int major_version1;
|
||||
char *major_version2;
|
||||
|
||||
res = PQexec(conn, "WITH pg_version(ver) AS (SELECT split_part(version(), ' ', 2)) "
|
||||
"SELECT split_part(ver, '.', 1), split_part(ver, '.', 2) FROM pg_version");
|
||||
res = PQexec(conn,
|
||||
"WITH pg_version(ver) AS "
|
||||
"(SELECT split_part(version(), ' ', 2)) "
|
||||
"SELECT split_part(ver, '.', 1), split_part(ver, '.', 2) "
|
||||
"FROM pg_version");
|
||||
|
||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||
{
|
||||
fprintf(stderr, "PQexec failed: %s", PQerrorMessage(conn));
|
||||
@@ -78,11 +85,13 @@ pg_version(PGconn *conn)
|
||||
PQfinish(conn);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
major_version1 = atoi(PQgetvalue(res, 0, 0));
|
||||
major_version2 = PQgetvalue(res, 0, 1);
|
||||
PQclear(res);
|
||||
|
||||
major_version = malloc(10);
|
||||
|
||||
if (major_version1 >= 9)
|
||||
{
|
||||
/* form a major version string */
|
||||
@@ -96,7 +105,8 @@ pg_version(PGconn *conn)
|
||||
|
||||
|
||||
bool
|
||||
guc_setted(PGconn *conn, const char *parameter, const char *op, const char *value)
|
||||
guc_setted(PGconn *conn, const char *parameter, const char *op,
|
||||
const char *value)
|
||||
{
|
||||
PGresult *res;
|
||||
char sqlquery[8192];
|
||||
@@ -131,7 +141,8 @@ get_cluster_size(PGconn *conn)
|
||||
const char *size;
|
||||
char sqlquery[8192];
|
||||
|
||||
sprintf(sqlquery, "SELECT pg_size_pretty(SUM(pg_database_size(oid))::bigint) "
|
||||
sprintf(sqlquery,
|
||||
"SELECT pg_size_pretty(SUM(pg_database_size(oid))::bigint) "
|
||||
" FROM pg_database ");
|
||||
|
||||
res = PQexec(conn, sqlquery);
|
||||
@@ -152,7 +163,8 @@ get_cluster_size(PGconn *conn)
|
||||
* to each node (one at a time) and finding if it is a master or a standby
|
||||
*/
|
||||
PGconn *
|
||||
getMasterConnection(PGconn *standby_conn, int id, char *cluster, int *master_id)
|
||||
getMasterConnection(PGconn *standby_conn, int id, char *cluster,
|
||||
int *master_id)
|
||||
{
|
||||
PGconn *master_conn = NULL;
|
||||
PGresult *res1;
|
||||
@@ -169,7 +181,8 @@ getMasterConnection(PGconn *standby_conn, int id, char *cluster, int *master_id)
|
||||
res1 = PQexec(standby_conn, sqlquery);
|
||||
if (PQresultStatus(res1) != PGRES_TUPLES_OK)
|
||||
{
|
||||
fprintf(stderr, "Can't get nodes info: %s\n", PQerrorMessage(standby_conn));
|
||||
fprintf(stderr, "Can't get nodes info: %s\n",
|
||||
PQerrorMessage(standby_conn));
|
||||
PQclear(res1);
|
||||
PQfinish(standby_conn);
|
||||
exit(1);
|
||||
@@ -181,18 +194,21 @@ getMasterConnection(PGconn *standby_conn, int id, char *cluster, int *master_id)
|
||||
*master_id = atoi(PQgetvalue(res1, i, 0));
|
||||
strcpy(master_conninfo, PQgetvalue(res1, i, 2));
|
||||
master_conn = establishDBConnection(master_conninfo, false);
|
||||
|
||||
if (PQstatus(master_conn) != CONNECTION_OK)
|
||||
continue;
|
||||
|
||||
/*
|
||||
* I can't use the is_standby() function here because on error that
|
||||
* function closes the connection i pass and exit, but i still need to close
|
||||
* standby_conn
|
||||
* function closes the connection i pass and exit, but i still need to
|
||||
* close standby_conn
|
||||
*/
|
||||
res2 = PQexec(master_conn, "SELECT pg_is_in_recovery()");
|
||||
|
||||
if (PQresultStatus(res2) != PGRES_TUPLES_OK)
|
||||
{
|
||||
fprintf(stderr, "Can't get recovery state from this node: %s\n", PQerrorMessage(master_conn));
|
||||
fprintf(stderr, "Can't get recovery state from this node: %s\n",
|
||||
PQerrorMessage(master_conn));
|
||||
PQclear(res2);
|
||||
PQfinish(master_conn);
|
||||
continue;
|
||||
@@ -217,7 +233,8 @@ getMasterConnection(PGconn *standby_conn, int id, char *cluster, int *master_id)
|
||||
/* If we finish this loop without finding a master then
|
||||
* we doesn't have the info or the master has failed (or we
|
||||
* reached max_connections or superuser_reserved_connections,
|
||||
* anything else i'm missing?),
|
||||
* anything else I'm missing?).
|
||||
*
|
||||
* Probably we will need to check the error to know if we need
|
||||
* to start failover procedure or just fix some situation on the
|
||||
* standby.
|
||||
@@ -225,4 +242,3 @@ getMasterConnection(PGconn *standby_conn, int id, char *cluster, int *master_id)
|
||||
PQclear(res1);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
PGconn *establishDBConnection(const char *conninfo, const bool exit_on_error);
|
||||
bool is_standby(PGconn *conn);
|
||||
char *pg_version(PGconn *conn);
|
||||
bool guc_setted(PGconn *conn, const char *parameter, const char *op, const char *value);
|
||||
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, int *master_id);
|
||||
PGconn * getMasterConnection(PGconn *standby_conn, int id, char *cluster,
|
||||
int *master_id);
|
||||
|
||||
226
repmgr.c
226
repmgr.c
@@ -34,7 +34,8 @@
|
||||
|
||||
static void help(const char *progname);
|
||||
static bool create_recovery_file(const char *data_dir);
|
||||
static int copy_remote_files(char *host, char *remote_user, char *remote_path, char *local_path, bool is_directory);
|
||||
static int copy_remote_files(char *host, char *remote_user, char *remote_path,
|
||||
char *local_path, bool is_directory);
|
||||
static bool check_parameters_for_action(const int action);
|
||||
|
||||
static void do_master_register(void);
|
||||
@@ -103,7 +104,8 @@ main(int argc, char **argv)
|
||||
}
|
||||
|
||||
|
||||
while ((c = getopt_long(argc, argv, "d:h:p:U:D:f:R:w:F:v", long_options, &optindex)) != -1)
|
||||
while ((c = getopt_long(argc, argv, "d:h:p:U:D:f:R:w:F:v", long_options,
|
||||
&optindex)) != -1)
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
@@ -138,7 +140,8 @@ main(int argc, char **argv)
|
||||
verbose = true;
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
|
||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
|
||||
progname);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
@@ -154,9 +157,11 @@ main(int argc, char **argv)
|
||||
if (optind < argc)
|
||||
{
|
||||
server_mode = argv[optind++];
|
||||
if (strcasecmp(server_mode, "STANDBY") != 0 && strcasecmp(server_mode, "MASTER") != 0)
|
||||
if (strcasecmp(server_mode, "STANDBY") != 0 &&
|
||||
strcasecmp(server_mode, "MASTER") != 0)
|
||||
{
|
||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
|
||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
|
||||
progname);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
@@ -185,7 +190,8 @@ main(int argc, char **argv)
|
||||
action = STANDBY_FOLLOW;
|
||||
else
|
||||
{
|
||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
|
||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
|
||||
progname);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
@@ -211,7 +217,8 @@ main(int argc, char **argv)
|
||||
default:
|
||||
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
|
||||
progname, argv[optind + 1]);
|
||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
|
||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
|
||||
progname);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -267,7 +274,8 @@ main(int argc, char **argv)
|
||||
do_standby_follow();
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
|
||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
|
||||
progname);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -307,7 +315,8 @@ do_master_register(void)
|
||||
if (strcmp(master_version, "") == 0)
|
||||
{
|
||||
PQfinish(conn);
|
||||
fprintf(stderr, _("%s needs master to be PostgreSQL 9.0 or better\n"), progname);
|
||||
fprintf(stderr, _("%s needs master to be PostgreSQL 9.0 or better\n"),
|
||||
progname);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -320,11 +329,13 @@ do_master_register(void)
|
||||
}
|
||||
|
||||
/* Check if there is a schema for this cluster */
|
||||
sprintf(sqlquery, "SELECT 1 FROM pg_namespace WHERE nspname = 'repmgr_%s'", myClusterName);
|
||||
sprintf(sqlquery, "SELECT 1 FROM pg_namespace WHERE nspname = 'repmgr_%s'",
|
||||
myClusterName);
|
||||
res = PQexec(conn, sqlquery);
|
||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||
{
|
||||
fprintf(stderr, "Can't get info about schemas: %s\n", PQerrorMessage(conn));
|
||||
fprintf(stderr, "Can't get info about schemas: %s\n",
|
||||
PQerrorMessage(conn));
|
||||
PQclear(res);
|
||||
PQfinish(conn);
|
||||
return;
|
||||
@@ -362,7 +373,8 @@ do_master_register(void)
|
||||
" conninfo text not null)", myClusterName);
|
||||
if (!PQexec(conn, sqlquery))
|
||||
{
|
||||
fprintf(stderr, "Cannot create the table repmgr_%s.repl_nodes: %s\n",
|
||||
fprintf(stderr,
|
||||
"Cannot create the table repmgr_%s.repl_nodes: %s\n",
|
||||
myClusterName, PQerrorMessage(conn));
|
||||
PQfinish(conn);
|
||||
return;
|
||||
@@ -375,10 +387,12 @@ 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) ", myClusterName);
|
||||
" apply_lag BIGINT NOT NULL) ",
|
||||
myClusterName);
|
||||
if (!PQexec(conn, sqlquery))
|
||||
{
|
||||
fprintf(stderr, "Cannot create the table repmgr_%s.repl_monitor: %s\n",
|
||||
fprintf(stderr,
|
||||
"Cannot create the table repmgr_%s.repl_monitor: %s\n",
|
||||
myClusterName, PQerrorMessage(conn));
|
||||
PQfinish(conn);
|
||||
return;
|
||||
@@ -396,7 +410,8 @@ do_master_register(void)
|
||||
" WHERE row_number = 1", myClusterName, myClusterName);
|
||||
if (!PQexec(conn, sqlquery))
|
||||
{
|
||||
fprintf(stderr, "Cannot create the view repmgr_%s.repl_status: %s\n",
|
||||
fprintf(stderr,
|
||||
"Cannot create the view repmgr_%s.repl_status: %s\n",
|
||||
myClusterName, PQerrorMessage(conn));
|
||||
PQfinish(conn);
|
||||
return;
|
||||
@@ -485,7 +500,8 @@ do_standby_register(void)
|
||||
if (strcmp(standby_version, "") == 0)
|
||||
{
|
||||
PQfinish(conn);
|
||||
fprintf(stderr, _("%s needs standby to be PostgreSQL 9.0 or better\n"), progname);
|
||||
fprintf(stderr, _("%s needs standby to be PostgreSQL 9.0 or better\n"),
|
||||
progname);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -498,11 +514,13 @@ do_standby_register(void)
|
||||
}
|
||||
|
||||
/* Check if there is a schema for this cluster */
|
||||
sprintf(sqlquery, "SELECT 1 FROM pg_namespace WHERE nspname = 'repmgr_%s'", myClusterName);
|
||||
sprintf(sqlquery, "SELECT 1 FROM pg_namespace WHERE nspname = 'repmgr_%s'",
|
||||
myClusterName);
|
||||
res = PQexec(conn, sqlquery);
|
||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||
{
|
||||
fprintf(stderr, "Can't get info about tablespaces: %s\n", PQerrorMessage(conn));
|
||||
fprintf(stderr, "Can't get info about tablespaces: %s\n",
|
||||
PQerrorMessage(conn));
|
||||
PQclear(res);
|
||||
PQfinish(conn);
|
||||
return;
|
||||
@@ -518,7 +536,8 @@ do_standby_register(void)
|
||||
PQclear(res);
|
||||
|
||||
/* check if there is a master in this cluster */
|
||||
master_conn = getMasterConnection(conn, myLocalId, myClusterName, &master_id);
|
||||
master_conn = getMasterConnection(conn, myLocalId, myClusterName,
|
||||
&master_id);
|
||||
if (!master_conn)
|
||||
return;
|
||||
|
||||
@@ -528,7 +547,8 @@ do_standby_register(void)
|
||||
{
|
||||
PQfinish(conn);
|
||||
PQfinish(master_conn);
|
||||
fprintf(stderr, _("%s needs master to be PostgreSQL 9.0 or better\n"), progname);
|
||||
fprintf(stderr, _("%s needs master to be PostgreSQL 9.0 or better\n"),
|
||||
progname);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -687,7 +707,8 @@ do_standby_clone(void)
|
||||
if (strcmp(master_version, "") == 0)
|
||||
{
|
||||
PQfinish(conn);
|
||||
fprintf(stderr, _("%s needs master to be PostgreSQL 9.0 or better\n"), progname);
|
||||
fprintf(stderr, _("%s needs master to be PostgreSQL 9.0 or better\n"),
|
||||
progname);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -748,7 +769,8 @@ do_standby_clone(void)
|
||||
|
||||
if (!create_directory(tblspc_dir))
|
||||
{
|
||||
fprintf(stderr, _("%s: couldn't create directory \"%s\"... "),
|
||||
fprintf(stderr,
|
||||
_("%s: couldn't create directory \"%s\"... "),
|
||||
progname, tblspc_dir);
|
||||
PQclear(res);
|
||||
PQfinish(conn);
|
||||
@@ -775,7 +797,8 @@ do_standby_clone(void)
|
||||
/* Present and not empty */
|
||||
if (!force)
|
||||
{
|
||||
fprintf(stderr,
|
||||
fprintf(
|
||||
stderr,
|
||||
_("%s: directory \"%s\" exists but is not empty\n"),
|
||||
progname, tblspc_dir);
|
||||
PQclear(res);
|
||||
@@ -784,7 +807,8 @@ do_standby_clone(void)
|
||||
}
|
||||
default:
|
||||
/* Trouble accessing directory */
|
||||
fprintf(stderr, _("%s: could not access directory \"%s\": %s\n"),
|
||||
fprintf(stderr,
|
||||
_("%s: could not access directory \"%s\": %s\n"),
|
||||
progname, tblspc_dir, strerror(errno));
|
||||
PQclear(res);
|
||||
PQfinish(conn);
|
||||
@@ -840,18 +864,20 @@ do_standby_clone(void)
|
||||
/*
|
||||
* 1) first move global/pg_control
|
||||
*
|
||||
* 2) then move data_directory ommiting the files we have already moved and pg_xlog
|
||||
* content
|
||||
* 2) then move data_directory ommiting the files we have already moved and
|
||||
* pg_xlog content
|
||||
*
|
||||
* 3) finally We need to backup configuration files (that could be on other directories, debian
|
||||
* like systems likes to do that), so look at config_file, hba_file and ident_file but we
|
||||
* can omit external_pid_file ;)
|
||||
* 3) finally We need to backup configuration files (that could be on other
|
||||
* directories, debian like systems likes to do that), so look at
|
||||
* config_file, hba_file and ident_file but we can omit
|
||||
* external_pid_file ;)
|
||||
*
|
||||
* On error we need to return but before that execute pg_stop_backup()
|
||||
*/
|
||||
|
||||
/* need to create the global sub directory */
|
||||
sprintf(master_control_file, "%s/global/pg_control", master_data_directory);
|
||||
sprintf(master_control_file, "%s/global/pg_control",
|
||||
master_data_directory);
|
||||
sprintf(local_control_file, "%s/global", dest_dir);
|
||||
if (!create_directory(local_control_file))
|
||||
{
|
||||
@@ -860,34 +886,40 @@ do_standby_clone(void)
|
||||
goto stop_backup;
|
||||
}
|
||||
|
||||
r = copy_remote_files(host, remote_user, master_control_file, local_control_file, false);
|
||||
r = copy_remote_files(host, remote_user, master_control_file,
|
||||
local_control_file, false);
|
||||
if (r != 0)
|
||||
goto stop_backup;
|
||||
|
||||
r = copy_remote_files(host, remote_user, master_data_directory, dest_dir, true);
|
||||
r = copy_remote_files(host, remote_user, master_data_directory, dest_dir,
|
||||
true);
|
||||
if (r != 0)
|
||||
goto stop_backup;
|
||||
|
||||
/*
|
||||
* Copy tablespace locations, i'm doing this separately because i couldn't find and appropiate
|
||||
* rsync option but besides we could someday make all these rsync happen concurrently
|
||||
* Copy tablespace locations, i'm doing this separately because i couldn't
|
||||
* find and appropiate rsync option but besides we could someday make all
|
||||
* these rsync happen concurrently
|
||||
*/
|
||||
sprintf(sqlquery, "select spclocation from pg_tablespace where spcname not in ('pg_default', 'pg_global')");
|
||||
res = PQexec(conn, sqlquery);
|
||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||
{
|
||||
fprintf(stderr, "Can't get info about tablespaces: %s\n", PQerrorMessage(conn));
|
||||
fprintf(stderr, "Can't get info about tablespaces: %s\n",
|
||||
PQerrorMessage(conn));
|
||||
PQclear(res);
|
||||
goto stop_backup;
|
||||
}
|
||||
for (i = 0; i < PQntuples(res); i++)
|
||||
{
|
||||
r = copy_remote_files(host, remote_user, PQgetvalue(res, i, 0), PQgetvalue(res, i, 0), true);
|
||||
r = copy_remote_files(host, remote_user, PQgetvalue(res, i, 0),
|
||||
PQgetvalue(res, i, 0), true);
|
||||
if (r != 0)
|
||||
goto stop_backup;
|
||||
}
|
||||
|
||||
r = copy_remote_files(host, remote_user, master_config_file, dest_dir, false);
|
||||
r = copy_remote_files(host, remote_user, master_config_file, dest_dir,
|
||||
false);
|
||||
if (r != 0)
|
||||
goto stop_backup;
|
||||
|
||||
@@ -895,7 +927,8 @@ do_standby_clone(void)
|
||||
if (r != 0)
|
||||
goto stop_backup;
|
||||
|
||||
r = copy_remote_files(host, remote_user, master_ident_file, dest_dir, false);
|
||||
r = copy_remote_files(host, remote_user, master_ident_file, dest_dir,
|
||||
false);
|
||||
if (r != 0)
|
||||
goto stop_backup;
|
||||
|
||||
@@ -929,10 +962,14 @@ stop_backup:
|
||||
return;
|
||||
|
||||
if (verbose)
|
||||
printf(_("%s requires primary to keep WAL files %s until at least %s\n"),
|
||||
printf(
|
||||
_("%s requires primary to keep WAL files %s until at least %s\n"),
|
||||
progname, first_wal_segment, last_wal_segment);
|
||||
|
||||
/* we need to create the pg_xlog sub directory too, i'm reusing a variable here */
|
||||
/*
|
||||
* We need to create the pg_xlog sub directory too, I'm reusing a variable
|
||||
* here.
|
||||
*/
|
||||
sprintf(local_control_file, "%s/pg_xlog", dest_dir);
|
||||
if (!create_directory(local_control_file))
|
||||
{
|
||||
@@ -943,7 +980,10 @@ stop_backup:
|
||||
/* Finally, write the recovery.conf file */
|
||||
create_recovery_file(dest_dir);
|
||||
|
||||
/* We don't start the service because we still may want to move the directory */
|
||||
/*
|
||||
* We don't start the service because we still may want to move the
|
||||
* directory
|
||||
*/
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -989,19 +1029,22 @@ do_standby_promote(void)
|
||||
if (strcmp(standby_version, "") == 0)
|
||||
{
|
||||
PQfinish(conn);
|
||||
fprintf(stderr, _("%s needs standby to be PostgreSQL 9.0 or better\n"), progname);
|
||||
fprintf(stderr, _("%s needs standby to be PostgreSQL 9.0 or better\n"),
|
||||
progname);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Check we are in a standby node */
|
||||
if (!is_standby(conn))
|
||||
{
|
||||
fprintf(stderr, "repmgr: The command should be executed in a standby node\n");
|
||||
fprintf(stderr,
|
||||
"repmgr: The command should be executed in a standby node\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/* we also need to check if there isn't any master already */
|
||||
old_master_conn = getMasterConnection(conn, myLocalId, myClusterName, &old_master_id);
|
||||
old_master_conn = getMasterConnection(conn, myLocalId, myClusterName,
|
||||
&old_master_id);
|
||||
if (old_master_conn != NULL)
|
||||
{
|
||||
PQfinish(old_master_conn);
|
||||
@@ -1018,7 +1061,8 @@ do_standby_promote(void)
|
||||
res = PQexec(conn, sqlquery);
|
||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||
{
|
||||
fprintf(stderr, "Can't get info about data directory: %s\n", PQerrorMessage(conn));
|
||||
fprintf(stderr, "Can't get info about data directory: %s\n",
|
||||
PQerrorMessage(conn));
|
||||
PQclear(res);
|
||||
PQfinish(conn);
|
||||
return;
|
||||
@@ -1041,15 +1085,17 @@ do_standby_promote(void)
|
||||
}
|
||||
|
||||
/* reconnect to check we got promoted */
|
||||
|
||||
/*
|
||||
* XXX i'm removing this because it gives an annoying message saying couldn't connect
|
||||
* but is just the server starting up
|
||||
* conn = establishDBConnection(conninfo, true);
|
||||
* if (is_standby(conn))
|
||||
* fprintf(stderr, "\n%s: STANDBY PROMOTE failed, this is still a standby node.\n", progname);
|
||||
* else
|
||||
* fprintf(stderr, "\n%s: you should REINDEX any hash indexes you have.\n", progname);
|
||||
* PQfinish(conn);
|
||||
* XXX i'm removing this because it gives an annoying message saying
|
||||
* couldn't connect but is just the server starting up
|
||||
*
|
||||
* conn = establishDBConnection(conninfo, true);
|
||||
* if (is_standby(conn))
|
||||
* fprintf(stderr, "\n%s: STANDBY PROMOTE failed, this is still a standby node.\n", progname);
|
||||
* else
|
||||
* fprintf(stderr, "\n%s: you should REINDEX any hash indexes you have.\n", progname);
|
||||
* PQfinish(conn);
|
||||
*/
|
||||
|
||||
return;
|
||||
@@ -1077,9 +1123,7 @@ do_standby_follow(void)
|
||||
const char *master_version = NULL;
|
||||
const char *standby_version = NULL;
|
||||
|
||||
/*
|
||||
* Read the configuration file: repmgr.conf
|
||||
*/
|
||||
/* Read the configuration file: repmgr.conf */
|
||||
parse_config(config_file, myClusterName, &myLocalId, conninfo);
|
||||
if (myLocalId == -1)
|
||||
{
|
||||
@@ -1108,7 +1152,8 @@ do_standby_follow(void)
|
||||
}
|
||||
|
||||
/* we also need to check if there is any master in the cluster */
|
||||
master_conn = getMasterConnection(conn, myLocalId, myClusterName, &master_id);
|
||||
master_conn = getMasterConnection(conn, myLocalId, myClusterName,
|
||||
&master_id);
|
||||
if (master_conn == NULL)
|
||||
{
|
||||
PQfinish(conn);
|
||||
@@ -1120,7 +1165,8 @@ do_standby_follow(void)
|
||||
if (is_standby(master_conn))
|
||||
{
|
||||
PQfinish(conn);
|
||||
fprintf(stderr, "%s: The node to follow should be a master\n", progname);
|
||||
fprintf(stderr, "%s: The node to follow should be a master\n",
|
||||
progname);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1130,7 +1176,8 @@ do_standby_follow(void)
|
||||
{
|
||||
PQfinish(conn);
|
||||
PQfinish(master_conn);
|
||||
fprintf(stderr, _("%s needs master to be PostgreSQL 9.0 or better\n"), progname);
|
||||
fprintf(stderr, _("%s needs master to be PostgreSQL 9.0 or better\n"),
|
||||
progname);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1164,7 +1211,8 @@ do_standby_follow(void)
|
||||
res = PQexec(conn, sqlquery);
|
||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||
{
|
||||
fprintf(stderr, "Can't get info about data directory: %s\n", PQerrorMessage(conn));
|
||||
fprintf(stderr, "Can't get info about data directory: %s\n",
|
||||
PQerrorMessage(conn));
|
||||
PQclear(res);
|
||||
PQfinish(conn);
|
||||
return;
|
||||
@@ -1197,7 +1245,8 @@ help(const char *progname)
|
||||
printf(_("\n%s: Replicator manager \n"), progname);
|
||||
printf(_("Usage:\n"));
|
||||
printf(_(" %s [OPTIONS] master {register}\n"), progname);
|
||||
printf(_(" %s [OPTIONS] standby {register|clone|promote|follow}\n"), progname);
|
||||
printf(_(" %s [OPTIONS] standby {register|clone|promote|follow}\n"),
|
||||
progname);
|
||||
printf(_("\nGeneral options:\n"));
|
||||
printf(_(" --help show this help, then exit\n"));
|
||||
printf(_(" --version output version information, then exit\n"));
|
||||
@@ -1250,7 +1299,8 @@ create_recovery_file(const char *data_dir)
|
||||
return false;
|
||||
}
|
||||
|
||||
sprintf(line, "primary_conninfo = 'host=%s port=%s'\n", host, ((masterport==NULL) ? "5432" : masterport));
|
||||
sprintf(line, "primary_conninfo = 'host=%s port=%s'\n", host,
|
||||
((masterport==NULL) ? "5432" : masterport));
|
||||
if (fputs(line, recovery_file) == EOF)
|
||||
{
|
||||
fprintf(stderr, "recovery file could not be written, it could be necesary to create it manually\n");
|
||||
@@ -1266,7 +1316,8 @@ create_recovery_file(const char *data_dir)
|
||||
|
||||
|
||||
static int
|
||||
copy_remote_files(char *host, char *remote_user, char *remote_path, char *local_path, bool is_directory)
|
||||
copy_remote_files(char *host, char *remote_user, char *remote_path,
|
||||
char *local_path, bool is_directory)
|
||||
{
|
||||
char script[QUERY_STR_LEN];
|
||||
char options[QUERY_STR_LEN];
|
||||
@@ -1288,7 +1339,8 @@ copy_remote_files(char *host, char *remote_user, char *remote_path, char *local_
|
||||
|
||||
if (is_directory)
|
||||
{
|
||||
strcat(options, " --exclude=pg_xlog* --exclude=pg_control --exclude=*.pid");
|
||||
strcat(options,
|
||||
" --exclude=pg_xlog* --exclude=pg_control --exclude=*.pid");
|
||||
sprintf(script, "rsync %s %s:%s/* %s",
|
||||
options, host_string, remote_path, local_path);
|
||||
}
|
||||
@@ -1304,7 +1356,8 @@ copy_remote_files(char *host, char *remote_user, char *remote_path, char *local_
|
||||
r = system(script);
|
||||
|
||||
if (r != 0)
|
||||
fprintf(stderr, _("Can't rsync from remote file or directory (%s:%s)\n"),
|
||||
fprintf(stderr,
|
||||
_("Can't rsync from remote file or directory (%s:%s)\n"),
|
||||
host_string, remote_path);
|
||||
|
||||
return r;
|
||||
@@ -1327,16 +1380,18 @@ check_parameters_for_action(const int action)
|
||||
* all other parameters are at least useless and could be
|
||||
* confusing so reject them
|
||||
*/
|
||||
if ((host != NULL) || (masterport != NULL) || (username != NULL) ||
|
||||
(dbname != NULL))
|
||||
if ((host != NULL) || (masterport != NULL) ||
|
||||
(username != NULL) || (dbname != NULL))
|
||||
{
|
||||
fprintf(stderr, "\nYou can't use connection parameters to the master when issuing a MASTER REGISTER command.");
|
||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
|
||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
|
||||
progname);
|
||||
ok = false;
|
||||
}
|
||||
if (dest_dir != NULL) {
|
||||
fprintf(stderr, "\nYou don't need a destination directory for MASTER REGISTER command");
|
||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
|
||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
|
||||
progname);
|
||||
ok = false;
|
||||
}
|
||||
break;
|
||||
@@ -1346,16 +1401,18 @@ check_parameters_for_action(const int action)
|
||||
* we don't need connection parameters to the master
|
||||
* because we can detect the master in repl_nodes
|
||||
*/
|
||||
if ((host != NULL) || (masterport != NULL) || (username != NULL) ||
|
||||
(dbname != NULL))
|
||||
if ((host != NULL) || (masterport != NULL) ||
|
||||
(username != NULL) || (dbname != NULL))
|
||||
{
|
||||
fprintf(stderr, "\nYou can't use connection parameters to the master when issuing a STANDBY REGISTER command.");
|
||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
|
||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
|
||||
progname);
|
||||
ok = false;
|
||||
}
|
||||
if (dest_dir != NULL) {
|
||||
fprintf(stderr, "\nYou don't need a destination directory for STANDBY REGISTER command");
|
||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
|
||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
|
||||
progname);
|
||||
ok = false;
|
||||
}
|
||||
break;
|
||||
@@ -1366,16 +1423,18 @@ check_parameters_for_action(const int action)
|
||||
* because we will try to detect the master in repl_nodes
|
||||
* if we can't find it then the promote action will be cancelled
|
||||
*/
|
||||
if ((host != NULL) || (masterport != NULL) || (username != NULL) ||
|
||||
(dbname != NULL))
|
||||
if ((host != NULL) || (masterport != NULL) ||
|
||||
(username != NULL) || (dbname != NULL))
|
||||
{
|
||||
fprintf(stderr, "\nYou can't use connection parameters to the master when issuing a STANDBY PROMOTE command.");
|
||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
|
||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
|
||||
progname);
|
||||
ok = false;
|
||||
}
|
||||
if (dest_dir != NULL) {
|
||||
fprintf(stderr, "\nYou don't need a destination directory for STANDBY PROMOTE command");
|
||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
|
||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
|
||||
progname);
|
||||
ok = false;
|
||||
}
|
||||
break;
|
||||
@@ -1386,16 +1445,18 @@ check_parameters_for_action(const int action)
|
||||
* because we will try to detect the master in repl_nodes
|
||||
* if we can't find it then the follow action will be cancelled
|
||||
*/
|
||||
if ((host != NULL) || (masterport != NULL) || (username != NULL) ||
|
||||
(dbname != NULL))
|
||||
if ((host != NULL) || (masterport != NULL) ||
|
||||
(username != NULL) || (dbname != NULL))
|
||||
{
|
||||
fprintf(stderr, "\nYou can't use connection parameters to the master when issuing a STANDBY FOLLOW command.");
|
||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
|
||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
|
||||
progname);
|
||||
ok = false;
|
||||
}
|
||||
if (dest_dir != NULL) {
|
||||
fprintf(stderr, "\nYou don't need a destination directory for STANDBY FOLLOW command");
|
||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
|
||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
|
||||
progname);
|
||||
ok = false;
|
||||
}
|
||||
break;
|
||||
@@ -1407,7 +1468,8 @@ check_parameters_for_action(const int action)
|
||||
*/
|
||||
if (config_file != NULL) {
|
||||
fprintf(stderr, "\nYou need to use connection parameters to the master when issuing a STANDBY CLONE command.");
|
||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
|
||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
|
||||
progname);
|
||||
ok = false;
|
||||
}
|
||||
break;
|
||||
|
||||
29
repmgrd.c
29
repmgrd.c
@@ -111,7 +111,8 @@ main(int argc, char **argv)
|
||||
verbose = true;
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
|
||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
|
||||
progname);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
@@ -142,7 +143,8 @@ main(int argc, char **argv)
|
||||
if (strcmp(standby_version, "") == 0)
|
||||
{
|
||||
PQfinish(myLocalConn);
|
||||
fprintf(stderr, _("%s needs standby to be PostgreSQL 9.0 or better\n"), progname);
|
||||
fprintf(stderr, _("%s needs standby to be PostgreSQL 9.0 or better\n"),
|
||||
progname);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -160,7 +162,8 @@ main(int argc, char **argv)
|
||||
else
|
||||
{
|
||||
/* I need the id of the primary as well as a connection to it */
|
||||
primaryConn = getMasterConnection(myLocalConn, myLocalId, myClusterName, &primaryId);
|
||||
primaryConn = getMasterConnection(myLocalConn, myLocalId,
|
||||
myClusterName, &primaryId);
|
||||
if (primaryConn == NULL)
|
||||
exit(1);
|
||||
}
|
||||
@@ -221,11 +224,14 @@ MonitorExecute(void)
|
||||
}
|
||||
if (PQstatus(primaryConn) != CONNECTION_OK)
|
||||
{
|
||||
fprintf(stderr, "\n%s: We couldn't reconnect to master, checking if ", progname);
|
||||
fprintf(stderr, "\n%s: We couldn't reconnect to master, checking if ",
|
||||
progname);
|
||||
fprintf(stderr, "%s: another node has been promoted.\n", progname);
|
||||
for (connection_retries = 0; connection_retries < 6; connection_retries++)
|
||||
for (connection_retries = 0; connection_retries < 6;
|
||||
connection_retries++)
|
||||
{
|
||||
primaryConn = getMasterConnection(myLocalConn, myLocalId, myClusterName, &primaryId);
|
||||
primaryConn = getMasterConnection(myLocalConn, myLocalId,
|
||||
myClusterName, &primaryId);
|
||||
if (PQstatus(primaryConn) == CONNECTION_OK)
|
||||
{
|
||||
/* Connected, we can continue the process so break the loop */
|
||||
@@ -344,8 +350,10 @@ checkClusterConfiguration(void)
|
||||
}
|
||||
|
||||
/*
|
||||
* If there isn't any results then we have not configured a primary node yet
|
||||
* in repmgr or the connection string is pointing to the wrong database.
|
||||
* If there isn't any results then we have not configured a primary node
|
||||
* yet in repmgr or the connection string is pointing to the wrong
|
||||
* database.
|
||||
*
|
||||
* XXX if we are the primary, should we try to create the tables needed?
|
||||
*/
|
||||
if (PQntuples(res) == 0)
|
||||
@@ -365,9 +373,7 @@ checkNodeConfiguration(char *conninfo)
|
||||
{
|
||||
PGresult *res;
|
||||
|
||||
/*
|
||||
* Check if we have my node information in repl_nodes
|
||||
*/
|
||||
/* Check if we have my node information in repl_nodes */
|
||||
sprintf(sqlquery, "SELECT * FROM repmgr_%s.repl_nodes "
|
||||
" WHERE id = %d AND cluster = '%s' ",
|
||||
myClusterName, myLocalId, myClusterName);
|
||||
@@ -389,6 +395,7 @@ checkNodeConfiguration(char *conninfo)
|
||||
if (PQntuples(res) == 0)
|
||||
{
|
||||
PQclear(res);
|
||||
|
||||
/* Adding the node */
|
||||
sprintf(sqlquery, "INSERT INTO repmgr_%s.repl_nodes "
|
||||
"VALUES (%d, '%s', '%s')",
|
||||
|
||||
Reference in New Issue
Block a user