rather big refactoring: use a naming scheme

In the past naming of functions, variables and such didn't really have a
naming scheme. Now they should have.

This is backpatched from master (2.1dev) just because it will be easier
to backpatch other fixes.
This commit is contained in:
Christian Kruse
2014-03-06 18:22:16 +01:00
committed by Jaime Casanova
parent 65989840d2
commit 04c101c5f0
8 changed files with 202 additions and 200 deletions

View File

@@ -90,10 +90,10 @@ check_dir(char *dir)
/* /*
* Create directory * Create directory with error log message when failing
*/ */
bool bool
create_directory(char *dir) create_dir(char *dir)
{ {
if (mkdir_p(dir, 0700) == 0) if (mkdir_p(dir, 0700) == 0)
return true; return true;
@@ -105,7 +105,7 @@ create_directory(char *dir)
} }
bool bool
set_directory_permissions(char *dir) set_dir_permissions(char *dir)
{ {
return (chmod(dir, 0700) != 0) ? false : true; return (chmod(dir, 0700) != 0) ? false : true;
} }
@@ -242,7 +242,7 @@ is_pg_dir(char *dir)
bool bool
create_pgdir(char *dir, bool force) create_pg_dir(char *dir, bool force)
{ {
bool pg_dir = false; bool pg_dir = false;
@@ -253,7 +253,7 @@ create_pgdir(char *dir, bool force)
/* dir not there, must create it */ /* dir not there, must create it */
log_info(_("creating directory \"%s\"...\n"), dir); log_info(_("creating directory \"%s\"...\n"), dir);
if (!create_directory(dir)) if (!create_dir(dir))
{ {
log_err(_("couldn't create directory \"%s\"...\n"), log_err(_("couldn't create directory \"%s\"...\n"),
dir); dir);
@@ -265,7 +265,7 @@ create_pgdir(char *dir, bool force)
log_info(_("checking and correcting permissions on existing directory %s ...\n"), log_info(_("checking and correcting permissions on existing directory %s ...\n"),
dir); dir);
if (!set_directory_permissions(dir)) if (!set_dir_permissions(dir))
{ {
log_err(_("could not change permissions of directory \"%s\": %s\n"), log_err(_("could not change permissions of directory \"%s\": %s\n"),
dir, strerror(errno)); dir, strerror(errno));

View File

@@ -22,9 +22,9 @@
int mkdir_p(char *path, mode_t omode); int mkdir_p(char *path, mode_t omode);
int check_dir(char *dir); int check_dir(char *dir);
bool create_directory(char *dir); bool create_dir(char *dir);
bool set_directory_permissions(char *dir); bool set_dir_permissions(char *dir);
bool is_pg_dir(char *dir); bool is_pg_dir(char *dir);
bool create_pgdir(char *dir, bool force); bool create_pg_dir(char *dir, bool force);
#endif #endif

View File

@@ -235,7 +235,7 @@ parse_line(char *buff, char *name, char *value)
} }
bool bool
reload_configuration(char *config_file, t_configuration_options * orig_options) reload_config(char *config_file, t_configuration_options * orig_options)
{ {
PGconn *conn; PGconn *conn;
@@ -295,7 +295,7 @@ reload_configuration(char *config_file, t_configuration_options * orig_options)
} }
/* Test conninfo string */ /* Test conninfo string */
conn = establishDBConnection(new_options.conninfo, false); conn = establish_db_connection(new_options.conninfo, false);
if (!conn || (PQstatus(conn) != CONNECTION_OK)) if (!conn || (PQstatus(conn) != CONNECTION_OK))
{ {
log_warning(_("conninfo string is not valid, will keep current configuration.\n")); log_warning(_("conninfo string is not valid, will keep current configuration.\n"));

View File

@@ -52,6 +52,6 @@ typedef struct
void parse_config(const char *config_file, t_configuration_options * options); void parse_config(const char *config_file, t_configuration_options * options);
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);
bool reload_configuration(char *config_file, t_configuration_options * orig_options); bool reload_config(char *config_file, t_configuration_options * orig_options);
#endif #endif

View File

@@ -26,7 +26,7 @@
#include "log.h" #include "log.h"
PGconn * PGconn *
establishDBConnection(const char *conninfo, const bool exit_on_error) establish_db_connection(const char *conninfo, const bool exit_on_error)
{ {
/* Make a connection to the database */ /* Make a connection to the database */
PGconn *conn = NULL; PGconn *conn = NULL;
@@ -53,8 +53,8 @@ establishDBConnection(const char *conninfo, const bool exit_on_error)
} }
PGconn * PGconn *
establishDBConnectionByParams(const char *keywords[], const char *values[], establish_db_connection_by_params(const char *keywords[], const char *values[],
const bool exit_on_error) const bool exit_on_error)
{ {
/* Make a connection to the database */ /* Make a connection to the database */
PGconn *conn = PQconnectdbParams(keywords, values, true); PGconn *conn = PQconnectdbParams(keywords, values, true);
@@ -144,7 +144,7 @@ is_pgup(PGconn *conn, int timeout)
/* /*
* Send a SELECT 1 just to check if the connection is OK * Send a SELECT 1 just to check if the connection is OK
*/ */
if (!CancelQuery(conn, timeout)) if (!cancel_query(conn, timeout))
goto failed; goto failed;
if (wait_connection_availability(conn, timeout) != 1) if (wait_connection_availability(conn, timeout) != 1)
goto failed; goto failed;
@@ -320,8 +320,8 @@ get_cluster_size(PGconn *conn)
* connection string is placed there. * connection string is placed there.
*/ */
PGconn * PGconn *
getMasterConnection(PGconn *standby_conn, char *schema, char *cluster, get_master_connection(PGconn *standby_conn, char *schema, char *cluster,
int *master_id, char *master_conninfo_out) int *master_id, char *master_conninfo_out)
{ {
PGconn *master_conn = NULL; PGconn *master_conn = NULL;
PGresult *res1; PGresult *res1;
@@ -377,7 +377,7 @@ getMasterConnection(PGconn *standby_conn, char *schema, char *cluster,
strncpy(master_conninfo, PQgetvalue(res1, i, 1), MAXCONNINFO); strncpy(master_conninfo, PQgetvalue(res1, i, 1), MAXCONNINFO);
log_info(_("checking role of cluster node '%s'\n"), log_info(_("checking role of cluster node '%s'\n"),
master_conninfo); master_conninfo);
master_conn = establishDBConnection(master_conninfo, false); master_conn = establish_db_connection(master_conninfo, false);
if (PQstatus(master_conn) != CONNECTION_OK) if (PQstatus(master_conn) != CONNECTION_OK)
continue; continue;
@@ -499,7 +499,7 @@ wait_connection_availability(PGconn *conn, long long timeout)
bool bool
CancelQuery(PGconn *conn, int timeout) cancel_query(PGconn *conn, int timeout)
{ {
char errbuf[ERRBUFF_SIZE]; char errbuf[ERRBUFF_SIZE];
PGcancel *pgcancel; PGcancel *pgcancel;

View File

@@ -22,10 +22,11 @@
#include "strutil.h" #include "strutil.h"
PGconn *establishDBConnection(const char *conninfo, const bool exit_on_error); PGconn *establish_db_connection(const char *conninfo,
PGconn *establishDBConnectionByParams(const char *keywords[], const bool exit_on_error);
const char *values[], PGconn *establish_db_connection_by_params(const char *keywords[],
const bool exit_on_error); const char *values[],
const bool exit_on_error);
int is_standby(PGconn *conn); int is_standby(PGconn *conn);
int is_witness(PGconn *conn, char *schema, char *cluster, int node_id); int is_witness(PGconn *conn, char *schema, char *cluster, int node_id);
bool is_pgup(PGconn *conn, int timeout); bool is_pgup(PGconn *conn, int timeout);
@@ -36,10 +37,10 @@ int guc_set_typed(PGconn *conn, const char *parameter, const char *op,
const char *value, const char *datatype); const char *value, const char *datatype);
const char *get_cluster_size(PGconn *conn); const char *get_cluster_size(PGconn *conn);
PGconn *getMasterConnection(PGconn *standby_conn, char *schema, char *cluster, PGconn *get_master_connection(PGconn *standby_conn, char *schema, char *cluster,
int *master_id, char *master_conninfo_out); int *master_id, char *master_conninfo_out);
int wait_connection_availability(PGconn *conn, long long timeout); int wait_connection_availability(PGconn *conn, long long timeout);
bool CancelQuery(PGconn *conn, int timeout); bool cancel_query(PGconn *conn, int timeout);
#endif #endif

View File

@@ -386,7 +386,7 @@ do_cluster_show(void)
/* We need to connect to check configuration */ /* We need to connect to check configuration */
log_info(_("%s connecting to database\n"), progname); log_info(_("%s connecting to database\n"), progname);
conn = establishDBConnection(options.conninfo, true); conn = establish_db_connection(options.conninfo, true);
sqlquery_snprintf(sqlquery, "SELECT conninfo, witness FROM %s.repl_nodes;", sqlquery_snprintf(sqlquery, "SELECT conninfo, witness FROM %s.repl_nodes;",
repmgr_schema); repmgr_schema);
@@ -405,7 +405,7 @@ do_cluster_show(void)
printf("Role | Connection String \n"); printf("Role | Connection String \n");
for (i = 0; i < PQntuples(res); i++) for (i = 0; i < PQntuples(res); i++)
{ {
conn = establishDBConnection(PQgetvalue(res, i, 0), false); conn = establish_db_connection(PQgetvalue(res, i, 0), false);
if (PQstatus(conn) != CONNECTION_OK) if (PQstatus(conn) != CONNECTION_OK)
strcpy(node_role, " FAILED"); strcpy(node_role, " FAILED");
else if (strcmp(PQgetvalue(res, i, 1), "t") == 0) else if (strcmp(PQgetvalue(res, i, 1), "t") == 0)
@@ -435,12 +435,12 @@ do_cluster_cleanup(void)
/* We need to connect to check configuration */ /* We need to connect to check configuration */
log_info(_("%s connecting to database\n"), progname); log_info(_("%s connecting to database\n"), progname);
conn = establishDBConnection(options.conninfo, true); conn = establish_db_connection(options.conninfo, true);
/* check if there is a master in this cluster */ /* check if there is a master in this cluster */
log_info(_("%s connecting to master database\n"), progname); log_info(_("%s connecting to master database\n"), progname);
master_conn = getMasterConnection(conn, repmgr_schema, options.cluster_name, master_conn = get_master_connection(conn, repmgr_schema, options.cluster_name,
&master_id, NULL); &master_id, NULL);
if (!master_conn) if (!master_conn)
{ {
log_err(_("cluster cleanup: cannot connect to master\n")); log_err(_("cluster cleanup: cannot connect to master\n"));
@@ -498,7 +498,7 @@ do_master_register(void)
char master_version[MAXVERSIONSTR]; char master_version[MAXVERSIONSTR];
int ret; int ret;
conn = establishDBConnection(options.conninfo, true); conn = establish_db_connection(options.conninfo, true);
/* master should be v9 or better */ /* master should be v9 or better */
log_info(_("%s connecting to master database\n"), progname); log_info(_("%s connecting to master database\n"), progname);
@@ -598,8 +598,8 @@ do_master_register(void)
} }
/* Ensure there isn't any other master already registered */ /* Ensure there isn't any other master already registered */
master_conn = getMasterConnection(conn, repmgr_schema, master_conn = get_master_connection(conn, repmgr_schema,
options.cluster_name, &id, NULL); options.cluster_name, &id, NULL);
if (master_conn != NULL) if (master_conn != NULL)
{ {
PQfinish(master_conn); PQfinish(master_conn);
@@ -653,7 +653,7 @@ do_standby_register(void)
/* XXX: A lot of copied code from do_master_register! Refactor */ /* XXX: A lot of copied code from do_master_register! Refactor */
log_info(_("%s connecting to standby database\n"), progname); log_info(_("%s connecting to standby database\n"), progname);
conn = establishDBConnection(options.conninfo, true); conn = establish_db_connection(options.conninfo, true);
/* should be v9 or better */ /* should be v9 or better */
log_info(_("%s connected to standby, checking its state\n"), progname); log_info(_("%s connected to standby, checking its state\n"), progname);
@@ -717,8 +717,8 @@ do_standby_register(void)
/* check if there is a master in this cluster */ /* check if there is a master in this cluster */
log_info(_("%s connecting to master database\n"), progname); log_info(_("%s connecting to master database\n"), progname);
master_conn = getMasterConnection(conn, repmgr_schema, options.cluster_name, master_conn = get_master_connection(conn, repmgr_schema, options.cluster_name,
&master_id, NULL); &master_id, NULL);
if (!master_conn) if (!master_conn)
{ {
log_err(_("A master must be defined before configuring a slave\n")); log_err(_("A master must be defined before configuring a slave\n"));
@@ -855,7 +855,7 @@ do_standby_clone(void)
/* We need to connect to check configuration and start a backup */ /* We need to connect to check configuration and start a backup */
log_info(_("%s connecting to master database\n"), progname); log_info(_("%s connecting to master database\n"), progname);
conn = establishDBConnectionByParams(keywords, values, true); conn = establish_db_connection_by_params(keywords, values, true);
/* primary should be v9 or better */ /* primary should be v9 or better */
log_info(_("%s connected to master, checking its state\n"), progname); log_info(_("%s connected to master, checking its state\n"), progname);
@@ -964,7 +964,7 @@ do_standby_clone(void)
* the directory a bit too early XXX build an array of tablespace to * the directory a bit too early XXX build an array of tablespace to
* create later in the backup * create later in the backup
*/ */
if (!create_pgdir(tblspc_dir, runtime_options.force)) if (!create_pg_dir(tblspc_dir, runtime_options.force))
{ {
PQclear(res); PQclear(res);
PQfinish(conn); PQfinish(conn);
@@ -1105,7 +1105,7 @@ do_standby_clone(void)
PQclear(res); PQclear(res);
/* Check the directory could be used as a PGDATA dir */ /* Check the directory could be used as a PGDATA dir */
if (!create_pgdir(local_data_directory, runtime_options.force)) if (!create_pg_dir(local_data_directory, runtime_options.force))
{ {
log_err(_("%s: couldn't use directory %s ...\nUse --force option to force\n"), log_err(_("%s: couldn't use directory %s ...\nUse --force option to force\n"),
progname, local_data_directory); progname, local_data_directory);
@@ -1134,7 +1134,7 @@ do_standby_clone(void)
maxlen_snprintf(local_control_file, "%s/global", local_data_directory); maxlen_snprintf(local_control_file, "%s/global", local_data_directory);
log_info(_("standby clone: master control file '%s'\n"), log_info(_("standby clone: master control file '%s'\n"),
master_control_file); master_control_file);
if (!create_directory(local_control_file)) if (!create_dir(local_control_file))
{ {
log_err(_("%s: couldn't create directory %s ...\n"), log_err(_("%s: couldn't create directory %s ...\n"),
progname, local_control_file); progname, local_control_file);
@@ -1292,7 +1292,7 @@ stop_backup:
/* /*
* We need to create the pg_xlog sub directory too. * We need to create the pg_xlog sub directory too.
*/ */
if (!create_directory(local_xlog_directory)) if (!create_dir(local_xlog_directory))
{ {
log_err(_("%s: couldn't create directory %s, you will need to do it manually...\n"), log_err(_("%s: couldn't create directory %s, you will need to do it manually...\n"),
progname, local_xlog_directory); progname, local_xlog_directory);
@@ -1349,7 +1349,7 @@ do_standby_promote(void)
/* We need to connect to check configuration */ /* We need to connect to check configuration */
log_info(_("%s connecting to master database\n"), progname); log_info(_("%s connecting to master database\n"), progname);
conn = establishDBConnection(options.conninfo, true); conn = establish_db_connection(options.conninfo, true);
/* we need v9 or better */ /* we need v9 or better */
log_info(_("%s connected to master, checking its state\n"), progname); log_info(_("%s connected to master, checking its state\n"), progname);
@@ -1375,7 +1375,7 @@ 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, repmgr_schema, old_master_conn = get_master_connection(conn, repmgr_schema,
options.cluster_name, &old_master_id, NULL); options.cluster_name, &old_master_id, NULL);
if (old_master_conn != NULL) if (old_master_conn != NULL)
{ {
@@ -1427,7 +1427,7 @@ do_standby_promote(void)
/* reconnect to check we got promoted */ /* reconnect to check we got promoted */
log_info(_("%s connecting to now restarted database\n"), progname); log_info(_("%s connecting to now restarted database\n"), progname);
conn = establishDBConnection(options.conninfo, true); conn = establish_db_connection(options.conninfo, true);
retval = is_standby(conn); retval = is_standby(conn);
if (retval) if (retval)
{ {
@@ -1466,7 +1466,7 @@ do_standby_follow(void)
/* We need to connect to check configuration */ /* We need to connect to check configuration */
log_info(_("%s connecting to standby database\n"), progname); log_info(_("%s connecting to standby database\n"), progname);
conn = establishDBConnection(options.conninfo, true); conn = establish_db_connection(options.conninfo, true);
/* Check we are in a standby node */ /* Check we are in a standby node */
log_info(_("%s connected to standby, checking its state\n"), progname); log_info(_("%s connected to standby, checking its state\n"), progname);
@@ -1501,10 +1501,10 @@ do_standby_follow(void)
{ {
if (!is_pgup(conn, options.master_response_timeout)) if (!is_pgup(conn, options.master_response_timeout))
{ {
conn = establishDBConnection(options.conninfo, true); conn = establish_db_connection(options.conninfo, true);
} }
master_conn = getMasterConnection(conn, repmgr_schema, master_conn = get_master_connection(conn, repmgr_schema,
options.cluster_name, &master_id, (char *) &master_conninfo); options.cluster_name, &master_id, (char *) &master_conninfo);
} }
while (master_conn == NULL && runtime_options.wait_for_master); while (master_conn == NULL && runtime_options.wait_for_master);
@@ -1626,7 +1626,7 @@ do_witness_create(void)
values[1] = runtime_options.masterport; values[1] = runtime_options.masterport;
/* We need to connect to check configuration and copy it */ /* We need to connect to check configuration and copy it */
masterconn = establishDBConnectionByParams(keywords, values, true); masterconn = establish_db_connection_by_params(keywords, values, true);
if (!masterconn) if (!masterconn)
{ {
log_err(_("%s: could not connect to master\n"), progname); log_err(_("%s: could not connect to master\n"), progname);
@@ -1668,7 +1668,7 @@ do_witness_create(void)
} }
/* Check this directory could be used as a PGDATA dir */ /* Check this directory could be used as a PGDATA dir */
if (!create_pgdir(runtime_options.dest_dir, runtime_options.force)) if (!create_pg_dir(runtime_options.dest_dir, runtime_options.force))
{ {
log_err(_("witness create: couldn't create data directory (\"%s\") for witness"), log_err(_("witness create: couldn't create data directory (\"%s\") for witness"),
runtime_options.dest_dir); runtime_options.dest_dir);
@@ -1786,7 +1786,7 @@ do_witness_create(void)
} }
/* establish a connection to the witness, and create the schema */ /* establish a connection to the witness, and create the schema */
witnessconn = establishDBConnection(options.conninfo, true); witnessconn = establish_db_connection(options.conninfo, true);
log_info(_("Starting copy of configuration from master...\n")); log_info(_("Starting copy of configuration from master...\n"));

297
repmgrd.c
View File

@@ -72,30 +72,30 @@ const XLogRecPtr InvalidXLogRecPtr = {0, 0};
* Struct to keep info about the nodes, used in the voting process in * Struct to keep info about the nodes, used in the voting process in
* do_failover() * do_failover()
*/ */
typedef struct nodeInfo typedef struct s_node_info
{ {
int nodeId; int node_id;
char conninfostr[MAXLEN]; char conninfo_str[MAXLEN];
XLogRecPtr xlog_location; XLogRecPtr xlog_location;
bool is_ready; bool is_ready;
bool is_visible; bool is_visible;
bool is_witness; bool is_witness;
} nodeInfo; } t_node_info;
char myClusterName[MAXLEN]; char myClusterName[MAXLEN];
/* Local info */ /* Local info */
t_configuration_options local_options; t_configuration_options local_options;
int myLocalMode = STANDBY_MODE; int my_local_mode = STANDBY_MODE;
PGconn *myLocalConn = NULL; PGconn *my_local_conn = NULL;
/* Primary info */ /* Primary info */
t_configuration_options primary_options; t_configuration_options primary_options;
PGconn *primaryConn = NULL;
char sqlquery[QUERY_STR_LEN]; char sqlquery[QUERY_STR_LEN];
PGconn *primary_conn = NULL;
const char *progname; const char *progname;
@@ -116,17 +116,17 @@ t_configuration_options config = T_CONFIGURATION_OPTIONS_INITIALIZER;
static void help(const char *progname); static void help(const char *progname);
static void usage(void); static void usage(void);
static void checkClusterConfiguration(PGconn *conn); static void check_cluster_configuration(PGconn *conn);
static void checkNodeConfiguration(void); static void check_node_configuration(void);
static void StandbyMonitor(void); static void standby_monitor(void);
static void WitnessMonitor(void); static void witness_monitor(void);
static bool CheckConnection(PGconn *conn, const char *type); static bool check_connection(PGconn *conn, const char *type);
static void update_shared_memory(char *last_wal_standby_applied); static void update_shared_memory(char *last_wal_standby_applied);
static void update_registration(void); static void update_registration(void);
static void do_failover(void); static void do_failover(void);
static unsigned long long int walLocationToBytes(char *wal_location); static unsigned long long int wal_location_to_bytes(char *wal_location);
/* /*
* Flag to mark SIGHUP. Whenever the main loop comes around it * Flag to mark SIGHUP. Whenever the main loop comes around it
@@ -147,19 +147,19 @@ static void do_daemonize(void);
static void check_and_create_pid_file(const char *pid_file); static void check_and_create_pid_file(const char *pid_file);
static void static void
CloseConnections() close_connections()
{ {
if (primaryConn != NULL && PQisBusy(primaryConn) == 1) if (primary_conn != NULL && PQisBusy(primary_conn) == 1)
CancelQuery(primaryConn, local_options.master_response_timeout); cancel_query(primary_conn, local_options.master_response_timeout);
if (myLocalConn != NULL) if (my_local_conn != NULL)
PQfinish(myLocalConn); PQfinish(my_local_conn);
if (primaryConn != NULL && primaryConn != myLocalConn) if (primary_conn != NULL && primary_conn != my_local_conn)
PQfinish(primaryConn); PQfinish(primary_conn);
primaryConn = NULL; primary_conn = NULL;
myLocalConn = NULL; my_local_conn = NULL;
} }
@@ -286,11 +286,11 @@ main(int argc, char **argv)
log_info(_("%s Connecting to database '%s'\n"), progname, log_info(_("%s Connecting to database '%s'\n"), progname,
local_options.conninfo); local_options.conninfo);
myLocalConn = establishDBConnection(local_options.conninfo, true); my_local_conn = establish_db_connection(local_options.conninfo, true);
/* should be v9 or better */ /* should be v9 or better */
log_info(_("%s Connected to database, checking its state\n"), progname); log_info(_("%s Connected to database, checking its state\n"), progname);
ret_ver = pg_version(myLocalConn, standby_version); ret_ver = pg_version(my_local_conn, standby_version);
if (ret_ver == NULL || strcmp(standby_version, "") == 0) if (ret_ver == NULL || strcmp(standby_version, "") == 0)
{ {
if (ret_ver != NULL) if (ret_ver != NULL)
@@ -302,7 +302,7 @@ main(int argc, char **argv)
/* /*
* MAIN LOOP This loops cicles once per failover and at startup * MAIN LOOP This loops cicles once per failover and at startup
* Requisites: - myLocalConn needs to be already setted with an active * Requisites: - my_local_conn needs to be already setted with an active
* connection - no master connection * connection - no master connection
*/ */
do do
@@ -311,19 +311,19 @@ main(int argc, char **argv)
* Set my server mode, establish a connection to primary and start * Set my server mode, establish a connection to primary and start
* monitor * monitor
*/ */
ret = is_witness(myLocalConn, repmgr_schema, ret = is_witness(my_local_conn, repmgr_schema,
local_options.cluster_name, local_options.node); local_options.cluster_name, local_options.node);
if (ret == 1) if (ret == 1)
myLocalMode = WITNESS_MODE; my_local_mode = WITNESS_MODE;
else if (ret == 0) else if (ret == 0)
{ {
ret = is_standby(myLocalConn); ret = is_standby(my_local_conn);
if (ret == 1) if (ret == 1)
myLocalMode = STANDBY_MODE; my_local_mode = STANDBY_MODE;
else if (ret == 0) /* is the master */ else if (ret == 0) /* is the master */
myLocalMode = PRIMARY_MODE; my_local_mode = PRIMARY_MODE;
} }
/* /*
@@ -334,22 +334,22 @@ main(int argc, char **argv)
if (ret == -1) if (ret == -1)
terminate(1); terminate(1);
switch (myLocalMode) switch (my_local_mode)
{ {
case PRIMARY_MODE: case PRIMARY_MODE:
primary_options.node = local_options.node; primary_options.node = local_options.node;
strncpy(primary_options.conninfo, local_options.conninfo, strncpy(primary_options.conninfo, local_options.conninfo,
MAXLEN); MAXLEN);
primaryConn = myLocalConn; primary_conn = my_local_conn;
checkClusterConfiguration(myLocalConn); check_cluster_configuration(my_local_conn);
checkNodeConfiguration(); check_node_configuration();
if (reload_configuration(config_file, &local_options)) if (reload_config(config_file, &local_options))
{ {
PQfinish(myLocalConn); PQfinish(my_local_conn);
myLocalConn = establishDBConnection(local_options.conninfo, true); my_local_conn = establish_db_connection(local_options.conninfo, true);
primaryConn = myLocalConn; primary_conn = my_local_conn;
update_registration(); update_registration();
} }
@@ -367,7 +367,7 @@ main(int argc, char **argv)
*/ */
do do
{ {
if (CheckConnection(primaryConn, "master")) if (check_connection(primary_conn, "master"))
{ {
/* /*
* CheckActiveStandbiesConnections(); * CheckActiveStandbiesConnections();
@@ -387,13 +387,13 @@ main(int argc, char **argv)
{ {
/* /*
* if we can reload, then could need to change * if we can reload, then could need to change
* myLocalConn * my_local_conn
*/ */
if (reload_configuration(config_file, &local_options)) if (reload_config(config_file, &local_options))
{ {
PQfinish(myLocalConn); PQfinish(my_local_conn);
myLocalConn = establishDBConnection(local_options.conninfo, true); my_local_conn = establish_db_connection(local_options.conninfo, true);
primaryConn = myLocalConn; primary_conn = my_local_conn;
if (*local_options.logfile) if (*local_options.logfile)
{ {
@@ -414,26 +414,27 @@ main(int argc, char **argv)
} }
} while (!failover_done); } while (!failover_done);
break; break;
case WITNESS_MODE: case WITNESS_MODE:
case STANDBY_MODE: case STANDBY_MODE:
/* 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 */
log_info(_("%s Connecting to primary for cluster '%s'\n"), log_info(_("%s Connecting to primary for cluster '%s'\n"),
progname, local_options.cluster_name); progname, local_options.cluster_name);
primaryConn = getMasterConnection(myLocalConn, repmgr_schema, primary_conn = get_master_connection(my_local_conn, repmgr_schema,
local_options.cluster_name, local_options.cluster_name,
&primary_options.node, NULL); &primary_options.node, NULL);
if (primaryConn == NULL) if (primary_conn == NULL)
{ {
terminate(ERR_BAD_CONFIG); terminate(ERR_BAD_CONFIG);
} }
checkClusterConfiguration(myLocalConn); check_cluster_configuration(my_local_conn);
checkNodeConfiguration(); check_node_configuration();
if (reload_configuration(config_file, &local_options)) if (reload_config(config_file, &local_options))
{ {
PQfinish(myLocalConn); PQfinish(my_local_conn);
myLocalConn = establishDBConnection(local_options.conninfo, true); my_local_conn = establish_db_connection(local_options.conninfo, true);
update_registration(); update_registration();
} }
@@ -441,12 +442,12 @@ main(int argc, char **argv)
* Every local_options.monitor_interval_secs seconds, do * Every local_options.monitor_interval_secs seconds, do
* checks * checks
*/ */
if (myLocalMode == WITNESS_MODE) if (my_local_mode == WITNESS_MODE)
{ {
log_info(_("%s Starting continuous witness node monitoring\n"), log_info(_("%s Starting continuous witness node monitoring\n"),
progname); progname);
} }
else if (myLocalMode == STANDBY_MODE) else if (my_local_mode == STANDBY_MODE)
{ {
log_info(_("%s Starting continuous standby node monitoring\n"), log_info(_("%s Starting continuous standby node monitoring\n"),
progname); progname);
@@ -454,22 +455,22 @@ main(int argc, char **argv)
do do
{ {
if (myLocalMode == WITNESS_MODE) if (my_local_mode == WITNESS_MODE)
WitnessMonitor(); witness_monitor();
else if (myLocalMode == STANDBY_MODE) else if (my_local_mode == STANDBY_MODE)
StandbyMonitor(); standby_monitor();
sleep(local_options.monitor_interval_secs); sleep(local_options.monitor_interval_secs);
if (got_SIGHUP) if (got_SIGHUP)
{ {
/* /*
* if we can reload, then could need to change * if we can reload, then could need to change
* myLocalConn * my_local_conn
*/ */
if (reload_configuration(config_file, &local_options)) if (reload_config(config_file, &local_options))
{ {
PQfinish(myLocalConn); PQfinish(my_local_conn);
myLocalConn = establishDBConnection(local_options.conninfo, true); my_local_conn = establish_db_connection(local_options.conninfo, true);
update_registration(); update_registration();
} }
got_SIGHUP = false; got_SIGHUP = false;
@@ -486,7 +487,7 @@ main(int argc, char **argv)
} while (true); } while (true);
/* close the connection to the database and cleanup */ /* close the connection to the database and cleanup */
CloseConnections(); close_connections();
/* Shuts down logging system */ /* Shuts down logging system */
logger_shutdown(); logger_shutdown();
@@ -498,7 +499,7 @@ main(int argc, char **argv)
* *
*/ */
static void static void
WitnessMonitor(void) witness_monitor(void)
{ {
char monitor_witness_timestamp[MAXLEN]; char monitor_witness_timestamp[MAXLEN];
PGresult *res; PGresult *res;
@@ -507,13 +508,13 @@ WitnessMonitor(void)
* Check if the master is still available, if after 5 minutes of retries * Check if the master is still available, if after 5 minutes of retries
* we cannot reconnect, return false. * we cannot reconnect, return false.
*/ */
CheckConnection(primaryConn, "master"); /* this take up to check_connection(primary_conn, "master"); /* this take up to
* local_options.reconnect_atte * local_options.reconnect_atte
* mpts * * mpts *
* local_options.reconnect_intv * local_options.reconnect_intv
* l seconds */ * l seconds */
if (PQstatus(primaryConn) != CONNECTION_OK) if (PQstatus(primary_conn) != CONNECTION_OK)
{ {
/* /*
* If we can't reconnect, just exit... XXX we need to make witness * If we can't reconnect, just exit... XXX we need to make witness
@@ -530,19 +531,19 @@ WitnessMonitor(void)
* Cancel any query that is still being executed, so i can insert the * Cancel any query that is still being executed, so i can insert the
* current record * current record
*/ */
if (!CancelQuery(primaryConn, local_options.master_response_timeout)) if (!cancel_query(primary_conn, local_options.master_response_timeout))
return; return;
if (wait_connection_availability(primaryConn, if (wait_connection_availability(primary_conn,
local_options.master_response_timeout) != 1) local_options.master_response_timeout) != 1)
return; return;
/* Get local xlog info */ /* Get local xlog info */
sqlquery_snprintf(sqlquery, "SELECT CURRENT_TIMESTAMP "); sqlquery_snprintf(sqlquery, "SELECT CURRENT_TIMESTAMP ");
res = PQexec(myLocalConn, sqlquery); res = PQexec(my_local_conn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK) if (PQresultStatus(res) != PGRES_TUPLES_OK)
{ {
log_err(_("PQexec failed: %s\n"), PQerrorMessage(myLocalConn)); log_err(_("PQexec failed: %s\n"), PQerrorMessage(my_local_conn));
PQclear(res); PQclear(res);
/* if there is any error just let it be and retry in next loop */ /* if there is any error just let it be and retry in next loop */
return; return;
@@ -566,10 +567,10 @@ WitnessMonitor(void)
* Execute the query asynchronously, but don't check for a result. We will * Execute the query asynchronously, but don't check for a result. We will
* check the result next time we pause for a monitor step. * check the result next time we pause for a monitor step.
*/ */
log_debug("WitnessMonitor: %s\n", sqlquery); log_debug("witness_monitor: %s\n", sqlquery);
if (PQsendQuery(primaryConn, sqlquery) == 0) if (PQsendQuery(primary_conn, sqlquery) == 0)
log_warning(_("Query could not be sent to primary. %s\n"), log_warning(_("Query could not be sent to primary. %s\n"),
PQerrorMessage(primaryConn)); PQerrorMessage(primary_conn));
} }
@@ -579,7 +580,7 @@ WitnessMonitor(void)
* Also do the math to see how far are we in bytes for being uptodate * Also do the math to see how far are we in bytes for being uptodate
*/ */
static void static void
StandbyMonitor(void) standby_monitor(void)
{ {
PGresult *res; PGresult *res;
char monitor_standby_timestamp[MAXLEN]; char monitor_standby_timestamp[MAXLEN];
@@ -600,31 +601,31 @@ StandbyMonitor(void)
* Check if the master is still available, if after 5 minutes of retries * Check if the master is still available, if after 5 minutes of retries
* we cannot reconnect, try to get a new master. * we cannot reconnect, try to get a new master.
*/ */
CheckConnection(primaryConn, "master"); /* this take up to check_connection(primary_conn, "master"); /* this take up to
* local_options.reconnect_atte * local_options.reconnect_atte
* mpts * * mpts *
* local_options.reconnect_intv * local_options.reconnect_intv
* l seconds */ * l seconds */
if (!CheckConnection(myLocalConn, "standby")) if (!check_connection(my_local_conn, "standby"))
{ {
log_err("Failed to connect to local node, exiting!\n"); log_err("Failed to connect to local node, exiting!\n");
terminate(1); terminate(1);
} }
if (PQstatus(primaryConn) != CONNECTION_OK) if (PQstatus(primary_conn) != CONNECTION_OK)
{ {
PQfinish(primaryConn); PQfinish(primary_conn);
primaryConn = NULL; primary_conn = NULL;
if (local_options.failover == MANUAL_FAILOVER) if (local_options.failover == MANUAL_FAILOVER)
{ {
log_err(_("We couldn't reconnect to master. Now checking if another node has been promoted.\n")); log_err(_("We couldn't reconnect to master. Now checking if another node has been promoted.\n"));
for (connection_retries = 0; connection_retries < 6; connection_retries++) for (connection_retries = 0; connection_retries < 6; connection_retries++)
{ {
primaryConn = getMasterConnection(myLocalConn, repmgr_schema, primary_conn = get_master_connection(my_local_conn, repmgr_schema,
local_options.cluster_name, &primary_options.node, NULL); local_options.cluster_name, &primary_options.node, NULL);
if (PQstatus(primaryConn) == CONNECTION_OK) if (PQstatus(primary_conn) == CONNECTION_OK)
{ {
/* /*
* Connected, we can continue the process so break the * Connected, we can continue the process so break the
@@ -648,7 +649,7 @@ StandbyMonitor(void)
} }
} }
if (PQstatus(primaryConn) != CONNECTION_OK) if (PQstatus(primary_conn) != CONNECTION_OK)
{ {
log_err(_("We couldn't reconnect for long enough, exiting...\n")); log_err(_("We couldn't reconnect for long enough, exiting...\n"));
terminate(ERR_DB_CON); terminate(ERR_DB_CON);
@@ -658,7 +659,7 @@ StandbyMonitor(void)
{ {
/* /*
* When we returns from this function we will have a new primary * When we returns from this function we will have a new primary
* and a new primaryConn * and a new primary_conn
*/ */
do_failover(); do_failover();
return; return;
@@ -668,7 +669,7 @@ StandbyMonitor(void)
/* Check if we still are a standby, we could have been promoted */ /* Check if we still are a standby, we could have been promoted */
do do
{ {
ret = is_standby(myLocalConn); ret = is_standby(my_local_conn);
switch (ret) switch (ret)
{ {
@@ -681,7 +682,7 @@ StandbyMonitor(void)
log_err(_("Standby node disappeared, trying to reconnect...\n")); log_err(_("Standby node disappeared, trying to reconnect...\n"));
did_retry = true; did_retry = true;
if (!CheckConnection(myLocalConn, "standby")) if (!check_connection(my_local_conn, "standby"))
{ {
terminate(0); terminate(0);
} }
@@ -703,9 +704,9 @@ StandbyMonitor(void)
* Cancel any query that is still being executed, so i can insert the * Cancel any query that is still being executed, so i can insert the
* current record * current record
*/ */
if (!CancelQuery(primaryConn, local_options.master_response_timeout)) if (!cancel_query(primary_conn, local_options.master_response_timeout))
return; return;
if (wait_connection_availability(primaryConn, local_options.master_response_timeout) != 1) if (wait_connection_availability(primary_conn, local_options.master_response_timeout) != 1)
return; return;
/* Get local xlog info */ /* Get local xlog info */
@@ -714,10 +715,10 @@ StandbyMonitor(void)
"SELECT CURRENT_TIMESTAMP, pg_last_xlog_receive_location(), " "SELECT CURRENT_TIMESTAMP, pg_last_xlog_receive_location(), "
"pg_last_xlog_replay_location(), pg_last_xact_replay_timestamp()"); "pg_last_xlog_replay_location(), pg_last_xact_replay_timestamp()");
res = PQexec(myLocalConn, sqlquery); res = PQexec(my_local_conn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK) if (PQresultStatus(res) != PGRES_TUPLES_OK)
{ {
log_err(_("PQexec failed: %s\n"), PQerrorMessage(myLocalConn)); log_err(_("PQexec failed: %s\n"), PQerrorMessage(my_local_conn));
PQclear(res); PQclear(res);
/* if there is any error just let it be and retry in next loop */ /* if there is any error just let it be and retry in next loop */
return; return;
@@ -732,10 +733,10 @@ StandbyMonitor(void)
/* Get primary xlog info */ /* Get primary xlog info */
sqlquery_snprintf(sqlquery, "SELECT pg_current_xlog_location() "); sqlquery_snprintf(sqlquery, "SELECT pg_current_xlog_location() ");
res = PQexec(primaryConn, sqlquery); res = PQexec(primary_conn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK) if (PQresultStatus(res) != PGRES_TUPLES_OK)
{ {
log_err(_("PQexec failed: %s\n"), PQerrorMessage(primaryConn)); log_err(_("PQexec failed: %s\n"), PQerrorMessage(primary_conn));
PQclear(res); PQclear(res);
return; return;
} }
@@ -744,9 +745,9 @@ StandbyMonitor(void)
PQclear(res); PQclear(res);
/* Calculate the lag */ /* Calculate the lag */
lsn_primary = walLocationToBytes(last_wal_primary_location); lsn_primary = wal_location_to_bytes(last_wal_primary_location);
lsn_standby_received = walLocationToBytes(last_wal_standby_received); lsn_standby_received = wal_location_to_bytes(last_wal_standby_received);
lsn_standby_applied = walLocationToBytes(last_wal_standby_applied); lsn_standby_applied = wal_location_to_bytes(last_wal_standby_applied);
/* /*
* Build the SQL to execute on primary * Build the SQL to execute on primary
@@ -767,10 +768,10 @@ StandbyMonitor(void)
* Execute the query asynchronously, but don't check for a result. We will * Execute the query asynchronously, but don't check for a result. We will
* check the result next time we pause for a monitor step. * check the result next time we pause for a monitor step.
*/ */
log_debug("StandbyMonitor: %s\n", sqlquery); log_debug("standby_monitor: %s\n", sqlquery);
if (PQsendQuery(primaryConn, sqlquery) == 0) if (PQsendQuery(primary_conn, sqlquery) == 0)
log_warning(_("Query could not be sent to primary. %s\n"), log_warning(_("Query could not be sent to primary. %s\n"),
PQerrorMessage(primaryConn)); PQerrorMessage(primary_conn));
} }
@@ -795,16 +796,16 @@ do_failover(void)
char last_wal_standby_applied[MAXLEN]; char last_wal_standby_applied[MAXLEN];
PGconn *nodeConn = NULL; PGconn *node_conn = NULL;
/* /*
* will get info about until 50 nodes, which seems to be large enough for * will get info about until 50 nodes, which seems to be large enough for
* most scenarios * most scenarios
*/ */
nodeInfo nodes[50]; t_node_info nodes[50];
/* initialize to keep compiler quiet */ /* initialize to keep compiler quiet */
nodeInfo best_candidate = {-1, "", InvalidXLogRecPtr, false, false, false}; t_node_info best_candidate = {-1, "", InvalidXLogRecPtr, false, false, false};
/* get a list of standby nodes, including myself */ /* get a list of standby nodes, including myself */
sprintf(sqlquery, "SELECT id, conninfo, witness " sprintf(sqlquery, "SELECT id, conninfo, witness "
@@ -813,10 +814,10 @@ do_failover(void)
" ORDER BY priority, id ", " ORDER BY priority, id ",
repmgr_schema, local_options.cluster_name); repmgr_schema, local_options.cluster_name);
res = PQexec(myLocalConn, sqlquery); res = PQexec(my_local_conn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK) if (PQresultStatus(res) != PGRES_TUPLES_OK)
{ {
log_err(_("Can't get nodes' info: %s\n"), PQerrorMessage(myLocalConn)); log_err(_("Can't get nodes' info: %s\n"), PQerrorMessage(my_local_conn));
PQclear(res); PQclear(res);
terminate(ERR_DB_QUERY); terminate(ERR_DB_QUERY);
} }
@@ -833,8 +834,8 @@ do_failover(void)
*/ */
for (i = 0; i < total_nodes; i++) for (i = 0; i < total_nodes; i++)
{ {
nodes[i].nodeId = atoi(PQgetvalue(res, i, 0)); nodes[i].node_id = atoi(PQgetvalue(res, i, 0));
strncpy(nodes[i].conninfostr, PQgetvalue(res, i, 1), MAXLEN); strncpy(nodes[i].conninfo_str, PQgetvalue(res, i, 1), MAXLEN);
nodes[i].is_witness = (strcmp(PQgetvalue(res, i, 2), "t") == 0) ? true : false; nodes[i].is_witness = (strcmp(PQgetvalue(res, i, 2), "t") == 0) ? true : false;
/* /*
@@ -847,16 +848,16 @@ do_failover(void)
XLAssignValue(nodes[i].xlog_location, 0, 0); XLAssignValue(nodes[i].xlog_location, 0, 0);
log_debug(_("%s: node=%d conninfo=\"%s\" witness=%s\n"), log_debug(_("%s: node=%d conninfo=\"%s\" witness=%s\n"),
progname, nodes[i].nodeId, nodes[i].conninfostr, progname, nodes[i].node_id, nodes[i].conninfo_str,
(nodes[i].is_witness) ? "true" : "false"); (nodes[i].is_witness) ? "true" : "false");
nodeConn = establishDBConnection(nodes[i].conninfostr, false); node_conn = establish_db_connection(nodes[i].conninfo_str, false);
/* if we can't see the node just skip it */ /* if we can't see the node just skip it */
if (PQstatus(nodeConn) != CONNECTION_OK) if (PQstatus(node_conn) != CONNECTION_OK)
{ {
if (nodeConn != NULL) if (node_conn != NULL)
PQfinish(nodeConn); PQfinish(node_conn);
continue; continue;
} }
@@ -864,7 +865,7 @@ do_failover(void)
visible_nodes++; visible_nodes++;
nodes[i].is_visible = true; nodes[i].is_visible = true;
PQfinish(nodeConn); PQfinish(node_conn);
} }
PQclear(res); PQclear(res);
@@ -893,14 +894,14 @@ do_failover(void)
if (nodes[i].is_witness) if (nodes[i].is_witness)
continue; continue;
nodeConn = establishDBConnection(nodes[i].conninfostr, false); node_conn = establish_db_connection(nodes[i].conninfo_str, false);
/* /*
* XXX This shouldn't happen, if this happens it means this is a major * XXX This shouldn't happen, if this happens it means this is a major
* problem maybe network outages? anyway, is better for a human to * problem maybe network outages? anyway, is better for a human to
* react * react
*/ */
if (PQstatus(nodeConn) != CONNECTION_OK) if (PQstatus(node_conn) != CONNECTION_OK)
{ {
log_err(_("It seems new problems are arising, manual intervention is needed\n")); log_err(_("It seems new problems are arising, manual intervention is needed\n"));
terminate(ERR_FAILOVER_FAIL); terminate(ERR_FAILOVER_FAIL);
@@ -910,14 +911,14 @@ do_failover(void)
uxrecoff = 0; uxrecoff = 0;
sqlquery_snprintf(sqlquery, "SELECT pg_last_xlog_receive_location()"); sqlquery_snprintf(sqlquery, "SELECT pg_last_xlog_receive_location()");
res = PQexec(nodeConn, sqlquery); res = PQexec(node_conn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK) if (PQresultStatus(res) != PGRES_TUPLES_OK)
{ {
log_info(_("Can't get node's last standby location: %s\n"), log_info(_("Can't get node's last standby location: %s\n"),
PQerrorMessage(nodeConn)); PQerrorMessage(node_conn));
log_info(_("Connection details: %s\n"), nodes[i].conninfostr); log_info(_("Connection details: %s\n"), nodes[i].conninfo_str);
PQclear(res); PQclear(res);
PQfinish(nodeConn); PQfinish(node_conn);
terminate(ERR_FAILOVER_FAIL); terminate(ERR_FAILOVER_FAIL);
} }
@@ -926,13 +927,13 @@ do_failover(void)
PQgetvalue(res, 0, 0)); PQgetvalue(res, 0, 0));
log_debug("XLog position of node %d: log id=%u (%X), offset=%u (%X)\n", log_debug("XLog position of node %d: log id=%u (%X), offset=%u (%X)\n",
nodes[i].nodeId, uxlogid, uxlogid, uxrecoff, uxrecoff); nodes[i].node_id, uxlogid, uxlogid, uxrecoff, uxrecoff);
/* If position is 0/0, error */ /* If position is 0/0, error */
if (uxlogid == 0 && uxrecoff == 0) if (uxlogid == 0 && uxrecoff == 0)
{ {
PQclear(res); PQclear(res);
PQfinish(nodeConn); PQfinish(node_conn);
log_info(_("InvalidXLogRecPtr detected in a standby\n")); log_info(_("InvalidXLogRecPtr detected in a standby\n"));
terminate(ERR_FAILOVER_FAIL); terminate(ERR_FAILOVER_FAIL);
} }
@@ -940,17 +941,17 @@ do_failover(void)
XLAssignValue(nodes[i].xlog_location, uxlogid, uxrecoff); XLAssignValue(nodes[i].xlog_location, uxlogid, uxrecoff);
PQclear(res); PQclear(res);
PQfinish(nodeConn); PQfinish(node_conn);
} }
/* last we get info about this node, and update shared memory */ /* last we get info about this node, and update shared memory */
sprintf(sqlquery, "SELECT pg_last_xlog_receive_location()"); sprintf(sqlquery, "SELECT pg_last_xlog_receive_location()");
res = PQexec(myLocalConn, sqlquery); res = PQexec(my_local_conn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK) if (PQresultStatus(res) != PGRES_TUPLES_OK)
{ {
log_err(_("PQexec failed: %s.\nReport an invalid value to not be " log_err(_("PQexec failed: %s.\nReport an invalid value to not be "
" considered as new primary and exit.\n"), " considered as new primary and exit.\n"),
PQerrorMessage(myLocalConn)); PQerrorMessage(my_local_conn));
PQclear(res); PQclear(res);
sprintf(last_wal_standby_applied, "'%X/%X'", 0, 0); sprintf(last_wal_standby_applied, "'%X/%X'", 0, 0);
update_shared_memory(last_wal_standby_applied); update_shared_memory(last_wal_standby_applied);
@@ -987,14 +988,14 @@ do_failover(void)
if (nodes[i].is_ready) if (nodes[i].is_ready)
break; break;
nodeConn = establishDBConnection(nodes[i].conninfostr, false); node_conn = establish_db_connection(nodes[i].conninfo_str, false);
/* /*
* XXX This shouldn't happen, if this happens it means this is a * XXX This shouldn't happen, if this happens it means this is a
* major problem maybe network outages? anyway, is better for a * major problem maybe network outages? anyway, is better for a
* human to react * human to react
*/ */
if (PQstatus(nodeConn) != CONNECTION_OK) if (PQstatus(node_conn) != CONNECTION_OK)
{ {
/* XXX */ /* XXX */
log_info(_("At this point, it could be some race conditions " log_info(_("At this point, it could be some race conditions "
@@ -1008,14 +1009,14 @@ do_failover(void)
sqlquery_snprintf(sqlquery, "SELECT %s.repmgr_get_last_standby_location()", sqlquery_snprintf(sqlquery, "SELECT %s.repmgr_get_last_standby_location()",
repmgr_schema); repmgr_schema);
res = PQexec(nodeConn, sqlquery); res = PQexec(node_conn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK) if (PQresultStatus(res) != PGRES_TUPLES_OK)
{ {
log_err(_("PQexec failed: %s.\nReport an invalid value to not" log_err(_("PQexec failed: %s.\nReport an invalid value to not"
"be considered as new primary and exit.\n"), "be considered as new primary and exit.\n"),
PQerrorMessage(nodeConn)); PQerrorMessage(node_conn));
PQclear(res); PQclear(res);
PQfinish(nodeConn); PQfinish(node_conn);
terminate(ERR_DB_QUERY); terminate(ERR_DB_QUERY);
} }
@@ -1034,7 +1035,7 @@ do_failover(void)
PQclear(res); PQclear(res);
PQfinish(nodeConn); PQfinish(node_conn);
/* If position is 0/0, keep checking */ /* If position is 0/0, keep checking */
if (uxlogid == 0 && uxrecoff == 0) if (uxlogid == 0 && uxrecoff == 0)
continue; continue;
@@ -1047,7 +1048,7 @@ do_failover(void)
} }
log_debug("Last XLog position of node %d: log id=%u (%X), offset=%u (%X)\n", log_debug("Last XLog position of node %d: log id=%u (%X), offset=%u (%X)\n",
nodes[i].nodeId, uxlogid, uxlogid, nodes[i].node_id, uxlogid, uxlogid,
uxrecoff, uxrecoff); uxrecoff, uxrecoff);
ready_nodes++; ready_nodes++;
@@ -1056,8 +1057,8 @@ do_failover(void)
} }
/* Close the connection to this server */ /* Close the connection to this server */
PQfinish(myLocalConn); PQfinish(my_local_conn);
myLocalConn = NULL; my_local_conn = NULL;
/* /*
* determine which one is the best candidate to promote to primary * determine which one is the best candidate to promote to primary
@@ -1077,7 +1078,7 @@ do_failover(void)
* start with the first ready node, and then move on to the next * start with the first ready node, and then move on to the next
* one * one
*/ */
best_candidate.nodeId = nodes[i].nodeId; best_candidate.node_id = nodes[i].node_id;
XLAssign(best_candidate.xlog_location, nodes[i].xlog_location); XLAssign(best_candidate.xlog_location, nodes[i].xlog_location);
best_candidate.is_ready = nodes[i].is_ready; best_candidate.is_ready = nodes[i].is_ready;
best_candidate.is_witness = nodes[i].is_witness; best_candidate.is_witness = nodes[i].is_witness;
@@ -1093,7 +1094,7 @@ do_failover(void)
*/ */
if (XLByteLT(best_candidate.xlog_location, nodes[i].xlog_location)) if (XLByteLT(best_candidate.xlog_location, nodes[i].xlog_location))
{ {
best_candidate.nodeId = nodes[i].nodeId; best_candidate.node_id = nodes[i].node_id;
XLAssign(best_candidate.xlog_location, nodes[i].xlog_location); XLAssign(best_candidate.xlog_location, nodes[i].xlog_location);
best_candidate.is_ready = nodes[i].is_ready; best_candidate.is_ready = nodes[i].is_ready;
best_candidate.is_witness = nodes[i].is_witness; best_candidate.is_witness = nodes[i].is_witness;
@@ -1101,7 +1102,7 @@ do_failover(void)
} }
/* once we know who is the best candidate, promote it */ /* once we know who is the best candidate, promote it */
if (find_best && (best_candidate.nodeId == local_options.node)) if (find_best && (best_candidate.node_id == local_options.node))
{ {
if (best_candidate.is_witness) if (best_candidate.is_witness)
{ {
@@ -1139,7 +1140,7 @@ do_failover(void)
if (verbose) if (verbose)
log_info(_("%s: Node %d is the best candidate to be the new primary, we should follow it...\n"), log_info(_("%s: Node %d is the best candidate to be the new primary, we should follow it...\n"),
progname, best_candidate.nodeId); progname, best_candidate.node_id);
log_debug(_("follow command is: \"%s\"\n"), local_options.follow_command); log_debug(_("follow command is: \"%s\"\n"), local_options.follow_command);
/* /*
@@ -1170,12 +1171,12 @@ do_failover(void)
failover_done = true; failover_done = true;
/* and reconnect to the local database */ /* and reconnect to the local database */
myLocalConn = establishDBConnection(local_options.conninfo, true); my_local_conn = establish_db_connection(local_options.conninfo, true);
} }
static bool static bool
CheckConnection(PGconn *conn, const char *type) check_connection(PGconn *conn, const char *type)
{ {
int connection_retries; int connection_retries;
@@ -1217,7 +1218,7 @@ CheckConnection(PGconn *conn, const char *type)
static void static void
checkClusterConfiguration(PGconn *conn) check_cluster_configuration(PGconn *conn)
{ {
PGresult *res; PGresult *res;
@@ -1252,7 +1253,7 @@ checkClusterConfiguration(PGconn *conn)
static void static void
checkNodeConfiguration(void) check_node_configuration(void)
{ {
PGresult *res; PGresult *res;
@@ -1266,10 +1267,10 @@ checkNodeConfiguration(void)
repmgr_schema, local_options.node, repmgr_schema, local_options.node,
local_options.cluster_name); local_options.cluster_name);
res = PQexec(myLocalConn, sqlquery); res = PQexec(my_local_conn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK) if (PQresultStatus(res) != PGRES_TUPLES_OK)
{ {
log_err(_("PQexec failed: %s\n"), PQerrorMessage(myLocalConn)); log_err(_("PQexec failed: %s\n"), PQerrorMessage(my_local_conn));
PQclear(res); PQclear(res);
terminate(ERR_BAD_CONFIG); terminate(ERR_BAD_CONFIG);
} }
@@ -1283,7 +1284,7 @@ checkNodeConfiguration(void)
{ {
PQclear(res); PQclear(res);
if (myLocalMode == WITNESS_MODE) if (my_local_mode == WITNESS_MODE)
{ {
log_err(_("The witness is not configured\n")); log_err(_("The witness is not configured\n"));
terminate(ERR_BAD_CONFIG); terminate(ERR_BAD_CONFIG);
@@ -1299,10 +1300,10 @@ checkNodeConfiguration(void)
local_options.node_name, local_options.node_name,
local_options.conninfo); local_options.conninfo);
if (!PQexec(primaryConn, sqlquery)) if (!PQexec(primary_conn, sqlquery))
{ {
log_err(_("Cannot insert node details, %s\n"), log_err(_("Cannot insert node details, %s\n"),
PQerrorMessage(primaryConn)); PQerrorMessage(primary_conn));
terminate(ERR_BAD_CONFIG); terminate(ERR_BAD_CONFIG);
} }
} }
@@ -1314,7 +1315,7 @@ checkNodeConfiguration(void)
static unsigned long long int static unsigned long long int
walLocationToBytes(char *wal_location) wal_location_to_bytes(char *wal_location)
{ {
unsigned int xlogid; unsigned int xlogid;
unsigned int xrecoff; unsigned int xrecoff;
@@ -1379,7 +1380,7 @@ setup_event_handlers(void)
static void static void
terminate(int retval) terminate(int retval)
{ {
CloseConnections(); close_connections();
logger_shutdown(); logger_shutdown();
if (pid_file) if (pid_file)
@@ -1402,11 +1403,11 @@ update_shared_memory(char *last_wal_standby_applied)
repmgr_schema, last_wal_standby_applied); repmgr_schema, last_wal_standby_applied);
/* If an error happens, just inform about that and continue */ /* If an error happens, just inform about that and continue */
res = PQexec(myLocalConn, sqlquery); res = PQexec(my_local_conn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK) if (PQresultStatus(res) != PGRES_TUPLES_OK)
{ {
log_warning(_("Cannot update this standby's shared memory: %s\n"), log_warning(_("Cannot update this standby's shared memory: %s\n"),
PQerrorMessage(myLocalConn)); PQerrorMessage(my_local_conn));
/* XXX is this enough reason to terminate this repmgrd? */ /* XXX is this enough reason to terminate this repmgrd? */
} }
else if (strcmp(PQgetvalue(res, 0, 0), "f") == 0) else if (strcmp(PQgetvalue(res, 0, 0), "f") == 0)
@@ -1431,11 +1432,11 @@ update_registration(void)
repmgr_schema, local_options.conninfo, repmgr_schema, local_options.conninfo,
local_options.priority, local_options.node); local_options.priority, local_options.node);
res = PQexec(primaryConn, sqlquery); res = PQexec(primary_conn, sqlquery);
if (PQresultStatus(res) != PGRES_COMMAND_OK) if (PQresultStatus(res) != PGRES_COMMAND_OK)
{ {
log_err(_("Cannot update registration: %s\n"), log_err(_("Cannot update registration: %s\n"),
PQerrorMessage(primaryConn)); PQerrorMessage(primary_conn));
terminate(ERR_DB_CON); terminate(ERR_DB_CON);
} }
PQclear(res); PQclear(res);