get_all_node_records(): display any error encountered and return success status

In many cases we'll want to bail out with an error if the node list can't
be retrieved for any reason. This saves some repetitive coding.
This commit is contained in:
Ian Barwick
2018-06-26 10:44:31 +09:00
parent bb4fdcda98
commit b0a2ee2259
6 changed files with 41 additions and 18 deletions

View File

@@ -2098,12 +2098,12 @@ _populate_node_records(PGresult *res, NodeInfoList *node_list)
}
void
bool
get_all_node_records(PGconn *conn, NodeInfoList *node_list)
{
PQExpBufferData query;
PGresult *res = NULL;
bool success = true;
initPQExpBuffer(&query);
appendPQExpBuffer(&query,
@@ -2115,21 +2115,22 @@ get_all_node_records(PGconn *conn, NodeInfoList *node_list)
res = PQexec(conn, query.data);
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
log_db_error(conn, query.data, _("get_all_node_records(): unable to execute query"));
}
termPQExpBuffer(&query);
/* this will return an empty list if there was an error executing the query */
_populate_node_records(res, node_list);
PQclear(res);
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
log_db_error(conn, query.data, _("get_all_node_records(): unable to execute query"));
success = false;
}
return;
PQclear(res);
termPQExpBuffer(&query);
return success;
}
void
get_downstream_node_records(PGconn *conn, int node_id, NodeInfoList *node_list)
{

View File

@@ -421,7 +421,7 @@ t_node_info *get_node_record_pointer(PGconn *conn, int node_id);
bool get_local_node_record(PGconn *conn, int node_id, t_node_info *node_info);
bool get_primary_node_record(PGconn *conn, t_node_info *node_info);
void get_all_node_records(PGconn *conn, NodeInfoList *node_list);
bool get_all_node_records(PGconn *conn, NodeInfoList *node_list);
void get_downstream_node_records(PGconn *conn, int node_id, NodeInfoList *nodes);
void get_active_sibling_node_records(PGconn *conn, int node_id, int upstream_node_id, NodeInfoList *node_list);
void get_node_records_by_priority(PGconn *conn, NodeInfoList *node_list);

View File

@@ -191,7 +191,7 @@ do_bdr_register(void)
{
NodeInfoList local_node_records = T_NODE_INFO_LIST_INITIALIZER;
get_all_node_records(conn, &local_node_records);
(void) get_all_node_records(conn, &local_node_records);
if (local_node_records.node_count == 0)
{
@@ -239,7 +239,7 @@ do_bdr_register(void)
continue;
}
get_all_node_records(bdr_node_conn, &existing_nodes);
(void) get_all_node_records(bdr_node_conn, &existing_nodes);
for (cell = existing_nodes.head; cell; cell = cell->next)
{

View File

@@ -913,7 +913,12 @@ build_cluster_matrix(t_node_matrix_rec ***matrix_rec_dest, int *name_length)
local_node_id = runtime_options.node_id;
}
get_all_node_records(conn, &nodes);
if (get_all_node_records(conn, &nodes) == false)
{
/* get_all_node_records() will display the error */
PQfinish(conn);
exit(ERR_BAD_CONFIG);
}
PQfinish(conn);
conn = NULL;
@@ -1118,7 +1123,12 @@ build_cluster_crosscheck(t_node_status_cube ***dest_cube, int *name_length)
else
conn = establish_db_connection_by_params(&source_conninfo, true);
get_all_node_records(conn, &nodes);
if (get_all_node_records(conn, &nodes) == false)
{
/* get_all_node_records() will display the error */
PQfinish(conn);
exit(ERR_BAD_CONFIG);
}
PQfinish(conn);
conn = NULL;

View File

@@ -218,7 +218,13 @@ do_witness_register(void)
* if repmgr.nodes contains entries, delete if -F/--force provided,
* otherwise exit with error
*/
get_all_node_records(witness_conn, &nodes);
if (get_all_node_records(witness_conn, &nodes) == false)
{
/* get_all_node_records() will display the error */
PQfinish(witness_conn);
PQfinish(primary_conn);
exit(ERR_BAD_CONFIG);
}
log_verbose(LOG_DEBUG, "%i node records found", nodes.node_count);

View File

@@ -150,7 +150,13 @@ monitor_bdr(void)
* retrieve list of all nodes - we'll need these if the DB connection goes
* away
*/
get_all_node_records(local_conn, &nodes);
if (get_all_node_records(local_conn, &nodes) == false)
{
/* get_all_node_records() will display the error */
PQfinish(local_conn);
exit(ERR_BAD_CONFIG);
}
/* we're expecting all (both) nodes to be up */
for (cell = nodes.head; cell; cell = cell->next)