mirror of
https://github.com/EnterpriseDB/repmgr.git
synced 2026-03-26 16:46:28 +00:00
Replace is_standby() with get_recovery_type()
We what to know what kind of node it is, not whether it's a standby or not.
This commit is contained in:
25
dbutils.c
25
dbutils.c
@@ -825,14 +825,16 @@ get_server_version(PGconn *conn, char *server_version)
|
|||||||
return atoi(PQgetvalue(res, 0, 0));
|
return atoi(PQgetvalue(res, 0, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
|
||||||
is_standby(PGconn *conn)
|
t_recovery_type
|
||||||
|
get_recovery_type(PGconn *conn)
|
||||||
{
|
{
|
||||||
PGresult *res;
|
PGresult *res;
|
||||||
int result = 0;
|
t_recovery_type recovery_type = RECTYPE_MASTER;
|
||||||
|
|
||||||
char *sqlquery = "SELECT pg_catalog.pg_is_in_recovery()";
|
char *sqlquery = "SELECT pg_catalog.pg_is_in_recovery()";
|
||||||
|
|
||||||
log_verbose(LOG_DEBUG, "is_standby(): %s", sqlquery);
|
log_verbose(LOG_DEBUG, "get_recovery_type(): %s", sqlquery);
|
||||||
|
|
||||||
res = PQexec(conn, sqlquery);
|
res = PQexec(conn, sqlquery);
|
||||||
|
|
||||||
@@ -840,15 +842,15 @@ is_standby(PGconn *conn)
|
|||||||
{
|
{
|
||||||
log_error(_("unable to determine if server is in recovery:\n %s"),
|
log_error(_("unable to determine if server is in recovery:\n %s"),
|
||||||
PQerrorMessage(conn));
|
PQerrorMessage(conn));
|
||||||
result = -1;
|
recovery_type = RECTYPE_UNKNOWN;
|
||||||
}
|
}
|
||||||
else if (PQntuples(res) == 1 && strcmp(PQgetvalue(res, 0, 0), "t") == 0)
|
else if (PQntuples(res) == 1 && strcmp(PQgetvalue(res, 0, 0), "t") == 0)
|
||||||
{
|
{
|
||||||
result = 1;
|
recovery_type = RECTYPE_STANDBY;
|
||||||
}
|
}
|
||||||
|
|
||||||
PQclear(res);
|
PQclear(res);
|
||||||
return result;
|
return recovery_type;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -916,7 +918,7 @@ get_master_connection(PGconn *conn,
|
|||||||
|
|
||||||
for (i = 0; i < PQntuples(res); i++)
|
for (i = 0; i < PQntuples(res); i++)
|
||||||
{
|
{
|
||||||
int is_node_standby;
|
t_recovery_type recovery_type;
|
||||||
|
|
||||||
/* initialize with the values of the current node being processed */
|
/* initialize with the values of the current node being processed */
|
||||||
node_id = atoi(PQgetvalue(res, i, 0));
|
node_id = atoi(PQgetvalue(res, i, 0));
|
||||||
@@ -929,9 +931,9 @@ get_master_connection(PGconn *conn,
|
|||||||
if (PQstatus(remote_conn) != CONNECTION_OK)
|
if (PQstatus(remote_conn) != CONNECTION_OK)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
is_node_standby = is_standby(remote_conn);
|
recovery_type = get_recovery_type(remote_conn);
|
||||||
|
|
||||||
if (is_node_standby == -1)
|
if (recovery_type == RECTYPE_UNKNOWN)
|
||||||
{
|
{
|
||||||
log_error(_("unable to retrieve recovery state from node %i:\n %s"),
|
log_error(_("unable to retrieve recovery state from node %i:\n %s"),
|
||||||
node_id,
|
node_id,
|
||||||
@@ -940,8 +942,7 @@ get_master_connection(PGconn *conn,
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if is_standby() returns 0, queried node is the master */
|
if (recovery_type == RECTYPE_MASTER)
|
||||||
if (is_node_standby == 0)
|
|
||||||
{
|
{
|
||||||
PQclear(res);
|
PQclear(res);
|
||||||
log_debug(_("get_master_connection(): current master node is %i"), node_id);
|
log_debug(_("get_master_connection(): current master node is %i"), node_id);
|
||||||
|
|||||||
@@ -33,6 +33,12 @@ typedef enum {
|
|||||||
REPMGR_UNKNOWN
|
REPMGR_UNKNOWN
|
||||||
} t_extension_status;
|
} t_extension_status;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
RECTYPE_UNKNOWN = 0,
|
||||||
|
RECTYPE_MASTER,
|
||||||
|
RECTYPE_STANDBY
|
||||||
|
} t_recovery_type;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Struct to store node information
|
* Struct to store node information
|
||||||
*/
|
*/
|
||||||
@@ -165,7 +171,7 @@ bool get_pg_setting(PGconn *conn, const char *setting, char *output);
|
|||||||
/* server information functions */
|
/* server information functions */
|
||||||
bool get_cluster_size(PGconn *conn, char *size);
|
bool get_cluster_size(PGconn *conn, char *size);
|
||||||
int get_server_version(PGconn *conn, char *server_version);
|
int get_server_version(PGconn *conn, char *server_version);
|
||||||
int is_standby(PGconn *conn);
|
t_recovery_type get_recovery_type(PGconn *conn);
|
||||||
int get_master_node_id(PGconn *conn);
|
int get_master_node_id(PGconn *conn);
|
||||||
|
|
||||||
/* extension functions */
|
/* extension functions */
|
||||||
|
|||||||
@@ -18,8 +18,7 @@ do_master_register(void)
|
|||||||
PGconn *conn = NULL;
|
PGconn *conn = NULL;
|
||||||
PGconn *master_conn = NULL;
|
PGconn *master_conn = NULL;
|
||||||
int current_master_id = UNKNOWN_NODE_ID;
|
int current_master_id = UNKNOWN_NODE_ID;
|
||||||
int ret;
|
t_recovery_type recovery_type;
|
||||||
|
|
||||||
t_node_info node_info = T_NODE_INFO_INITIALIZER;
|
t_node_info node_info = T_NODE_INFO_INITIALIZER;
|
||||||
int record_found;
|
int record_found;
|
||||||
bool record_created;
|
bool record_created;
|
||||||
@@ -35,14 +34,22 @@ do_master_register(void)
|
|||||||
check_server_version(conn, "master", true, NULL);
|
check_server_version(conn, "master", true, NULL);
|
||||||
|
|
||||||
/* check that node is actually a master */
|
/* check that node is actually a master */
|
||||||
ret = is_standby(conn);
|
recovery_type = get_recovery_type(conn);
|
||||||
if (ret)
|
|
||||||
{
|
|
||||||
log_error(_(ret == 1 ? "server is in standby mode and cannot be registered as a master" :
|
|
||||||
"connection to node lost!"));
|
|
||||||
|
|
||||||
PQfinish(conn);
|
if (recovery_type != RECTYPE_STANDBY)
|
||||||
exit(ERR_BAD_CONFIG);
|
{
|
||||||
|
if (recovery_type == RECTYPE_MASTER)
|
||||||
|
{
|
||||||
|
log_error(_("server is in standby mode and cannot be registered as a master"));
|
||||||
|
PQfinish(conn);
|
||||||
|
exit(ERR_BAD_CONFIG);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
log_error(_("connection to node lost"));
|
||||||
|
PQfinish(conn);
|
||||||
|
exit(ERR_DB_CONN);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log_verbose(LOG_INFO, _("server is not in recovery"));
|
log_verbose(LOG_INFO, _("server is not in recovery"));
|
||||||
@@ -204,3 +211,4 @@ do_master_register(void)
|
|||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -525,7 +525,6 @@ do_standby_register(void)
|
|||||||
{
|
{
|
||||||
PGconn *conn;
|
PGconn *conn;
|
||||||
PGconn *master_conn;
|
PGconn *master_conn;
|
||||||
int ret;
|
|
||||||
|
|
||||||
bool record_created;
|
bool record_created;
|
||||||
t_node_info node_record = T_NODE_INFO_INITIALIZER;
|
t_node_info node_record = T_NODE_INFO_INITIALIZER;
|
||||||
@@ -560,15 +559,24 @@ do_standby_register(void)
|
|||||||
|
|
||||||
if (PQstatus(conn) == CONNECTION_OK)
|
if (PQstatus(conn) == CONNECTION_OK)
|
||||||
{
|
{
|
||||||
ret = is_standby(conn);
|
t_recovery_type recovery_type = get_recovery_type(conn);
|
||||||
|
|
||||||
if (ret == 0 || ret == -1)
|
if (recovery_type != RECTYPE_STANDBY)
|
||||||
{
|
{
|
||||||
log_error(_(ret == 0 ? "this node should be a standby (%s)" :
|
if (recovery_type == RECTYPE_MASTER)
|
||||||
"connection to node (%s) lost"), config_file_options.conninfo);
|
{
|
||||||
|
log_error(_("this node should be a standby (%s)"),
|
||||||
PQfinish(conn);
|
config_file_options.conninfo);
|
||||||
exit(ERR_BAD_CONFIG);
|
PQfinish(conn);
|
||||||
|
exit(ERR_BAD_CONFIG);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
log_error(_("connection to node (%s) lost"),
|
||||||
|
config_file_options.conninfo);
|
||||||
|
PQfinish(conn);
|
||||||
|
exit(ERR_DB_CONN);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -997,9 +1005,8 @@ do_standby_promote(void)
|
|||||||
|
|
||||||
char script[MAXLEN];
|
char script[MAXLEN];
|
||||||
|
|
||||||
|
t_recovery_type recovery_type;
|
||||||
int r,
|
int r;
|
||||||
retval;
|
|
||||||
char data_dir[MAXLEN];
|
char data_dir[MAXLEN];
|
||||||
|
|
||||||
int i,
|
int i,
|
||||||
@@ -1018,18 +1025,22 @@ do_standby_promote(void)
|
|||||||
check_server_version(conn, "standby", true, NULL);
|
check_server_version(conn, "standby", true, NULL);
|
||||||
|
|
||||||
/* Check we are in a standby node */
|
/* Check we are in a standby node */
|
||||||
retval = is_standby(conn);
|
recovery_type = get_recovery_type(conn);
|
||||||
|
|
||||||
switch (retval)
|
if (recovery_type != RECTYPE_STANDBY)
|
||||||
{
|
{
|
||||||
case 0:
|
if (recovery_type == RECTYPE_MASTER)
|
||||||
|
{
|
||||||
log_error(_("STANDBY PROMOTE can only be executed on a standby node"));
|
log_error(_("STANDBY PROMOTE can only be executed on a standby node"));
|
||||||
PQfinish(conn);
|
PQfinish(conn);
|
||||||
exit(ERR_BAD_CONFIG);
|
exit(ERR_BAD_CONFIG);
|
||||||
case -1:
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
log_error(_("connection to node lost"));
|
log_error(_("connection to node lost"));
|
||||||
PQfinish(conn);
|
PQfinish(conn);
|
||||||
exit(ERR_DB_CONN);
|
exit(ERR_DB_CONN);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1094,8 +1105,9 @@ do_standby_promote(void)
|
|||||||
|
|
||||||
for (i = 0; i < promote_check_timeout; i += promote_check_interval)
|
for (i = 0; i < promote_check_timeout; i += promote_check_interval)
|
||||||
{
|
{
|
||||||
retval = is_standby(conn);
|
|
||||||
if (!retval)
|
recovery_type = get_recovery_type(conn);
|
||||||
|
if (recovery_type == RECTYPE_MASTER)
|
||||||
{
|
{
|
||||||
promote_success = true;
|
promote_success = true;
|
||||||
break;
|
break;
|
||||||
@@ -1105,16 +1117,17 @@ do_standby_promote(void)
|
|||||||
|
|
||||||
if (promote_success == false)
|
if (promote_success == false)
|
||||||
{
|
{
|
||||||
switch (retval)
|
if (recovery_type == RECTYPE_STANDBY)
|
||||||
{
|
{
|
||||||
case 1:
|
log_error(_("STANDBY PROMOTE failed, node is still a standby"));
|
||||||
log_error(_("STANDBY PROMOTE failed, node is still a standby"));
|
PQfinish(conn);
|
||||||
PQfinish(conn);
|
exit(ERR_PROMOTION_FAIL);
|
||||||
exit(ERR_PROMOTION_FAIL);
|
}
|
||||||
default:
|
else
|
||||||
log_error(_("connection to node lost"));
|
{
|
||||||
PQfinish(conn);
|
log_error(_("connection to node lost"));
|
||||||
exit(ERR_DB_CONN);
|
PQfinish(conn);
|
||||||
|
exit(ERR_DB_CONN);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1258,7 +1271,7 @@ check_source_server()
|
|||||||
* If the upstream node is a standby, try to connect to the primary too so we
|
* If the upstream node is a standby, try to connect to the primary too so we
|
||||||
* can write an event record
|
* can write an event record
|
||||||
*/
|
*/
|
||||||
if (is_standby(source_conn))
|
if (get_recovery_type(source_conn) == RECTYPE_STANDBY)
|
||||||
{
|
{
|
||||||
primary_conn = get_master_connection(source_conn, NULL, NULL);
|
primary_conn = get_master_connection(source_conn, NULL, NULL);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user