Consolidate version checking code

This commit is contained in:
Ian Barwick
2015-03-09 14:19:13 +09:00
parent abf92883a8
commit 29110a6e11

107
repmgr.c
View File

@@ -74,8 +74,10 @@ static void check_parameters_for_action(const int action);
static bool create_schema(PGconn *conn); static bool create_schema(PGconn *conn);
static void write_primary_conninfo(char *line); static void write_primary_conninfo(char *line);
static bool write_recovery_file_line(FILE *recovery_file, char *recovery_file_path, char *line); static bool write_recovery_file_line(FILE *recovery_file, char *recovery_file_path, char *line);
static void check_master_standby_version_match(PGconn *conn, PGconn *master_conn);
static int check_server_version(PGconn *conn, char *server_type, bool exit_on_error, char *server_version_string); static int check_server_version(PGconn *conn, char *server_type, bool exit_on_error, char *server_version_string);
static bool check_upstream_config(PGconn *conn, int server_version_num, bool exit_on_error); static bool check_upstream_config(PGconn *conn, int server_version_num, bool exit_on_error);
static char *make_pg_path(char *file); static char *make_pg_path(char *file);
static bool log_event(PGconn *standby_conn, bool success, char *details); static bool log_event(PGconn *standby_conn, bool success, char *details);
@@ -802,20 +804,12 @@ do_standby_register(void)
PGconn *master_conn; PGconn *master_conn;
int ret; int ret;
char master_version[MAXVERSIONSTR];
int master_version_num = 0;
char standby_version[MAXVERSIONSTR];
int standby_version_num = 0;
bool record_created; bool record_created;
log_info(_("connecting to standby database\n")); log_info(_("connecting to standby database\n"));
conn = establish_db_connection(options.conninfo, true); conn = establish_db_connection(options.conninfo, true);
/* Verify that standby is a supported server version */
standby_version_num = check_server_version(conn, "standby", true, standby_version);
/* Check we are a standby */ /* Check we are a standby */
ret = is_standby(conn); ret = is_standby(conn);
if (ret == 0 || ret == -1) if (ret == 0 || ret == -1)
@@ -846,25 +840,11 @@ do_standby_register(void)
exit(ERR_BAD_CONFIG); exit(ERR_BAD_CONFIG);
} }
/* Verify that master is a supported server version */ /*
log_info(_("connected to master, checking its state\n")); * Verify that standby and master are supported and compatible server
master_version_num = check_server_version(conn, "master", false, master_version); * versions
if(master_version_num < 0) */
{ check_master_standby_version_match(conn, master_conn);
PQfinish(conn);
PQfinish(master_conn);
exit(ERR_BAD_CONFIG);
}
/* master and standby version should match */
if ((master_version_num / 100) != (standby_version_num / 100))
{
PQfinish(conn);
PQfinish(master_conn);
log_err(_("PostgreSQL versions on master (%s) and standby (%s) must match.\n"),
master_version, standby_version);
exit(ERR_BAD_CONFIG);
}
/* Now register the standby */ /* Now register the standby */
log_info(_("registering the standby\n")); log_info(_("registering the standby\n"));
@@ -1726,12 +1706,6 @@ do_standby_follow(void)
retval; retval;
char data_dir[MAXLEN]; char data_dir[MAXLEN];
char master_version[MAXVERSIONSTR];
int master_version_num = 0;
char standby_version[MAXVERSIONSTR];
int standby_version_num = 0;
bool success; bool success;
@@ -1740,9 +1714,6 @@ do_standby_follow(void)
conn = establish_db_connection(options.conninfo, true); conn = establish_db_connection(options.conninfo, true);
log_info(_("connected to standby, checking its state\n")); log_info(_("connected to standby, checking its state\n"));
/* Verify that standby is a supported server version */
standby_version_num = check_server_version(conn, "standby", true, standby_version);
/* Check we are in a standby node */ /* Check we are in a standby node */
retval = is_standby(conn); retval = is_standby(conn);
if (retval == 0 || retval == -1) if (retval == 0 || retval == -1)
@@ -1791,25 +1762,11 @@ do_standby_follow(void)
exit(ERR_BAD_CONFIG); exit(ERR_BAD_CONFIG);
} }
/* Verify that master is a supported server version */ /*
log_info(_("connected to master, checking its state\n")); * Verify that standby and master are supported and compatible server
master_version_num = check_server_version(conn, "master", false, master_version); * versions
if(master_version_num < 0) */
{ check_master_standby_version_match(conn, master_conn);
PQfinish(conn);
PQfinish(master_conn);
exit(ERR_BAD_CONFIG);
}
/* master and standby version should match */
if ((master_version_num / 100) != (standby_version_num / 100))
{
PQfinish(conn);
PQfinish(master_conn);
log_err(_("PostgreSQL versions on master (%s) and standby (%s) must match.\n"),
master_version, standby_version);
exit(ERR_BAD_CONFIG);
}
/* /*
* set the host and masterport variables with the master ones before * set the host and masterport variables with the master ones before
@@ -2986,6 +2943,46 @@ check_server_version(PGconn *conn, char *server_type, bool exit_on_error, char *
} }
/*
* check_master_standby_version_match()
*
* Check server versions of supplied connections are compatible for
* replication purposes.
*
* Exits on error.
*/
static void
check_master_standby_version_match(PGconn *conn, PGconn *master_conn)
{
char standby_version[MAXVERSIONSTR];
int standby_version_num = 0;
char master_version[MAXVERSIONSTR];
int master_version_num = 0;
standby_version_num = check_server_version(conn, "standby", true, standby_version);
/* Verify that master is a supported server version */
master_version_num = check_server_version(conn, "master", false, master_version);
if(master_version_num < 0)
{
PQfinish(conn);
PQfinish(master_conn);
exit(ERR_BAD_CONFIG);
}
/* master and standby version should match */
if ((master_version_num / 100) != (standby_version_num / 100))
{
PQfinish(conn);
PQfinish(master_conn);
log_err(_("PostgreSQL versions on master (%s) and standby (%s) must match.\n"),
master_version, standby_version);
exit(ERR_BAD_CONFIG);
}
}
/* /*
* check_upstream_config() * check_upstream_config()
* *