pgindent run

This commit is contained in:
Ian Barwick
2017-09-11 11:14:13 +09:00
parent 0e6699ecb6
commit a9f4a027a7
36 changed files with 3523 additions and 3288 deletions

View File

@@ -104,4 +104,3 @@ appendShellString(PQExpBuffer buf, const char *str)
appendPQExpBufferChar(buf, '\'');
}

View File

@@ -23,10 +23,8 @@
#ifndef _COMPAT_H_
#define _COMPAT_H_
extern void
appendConnStrVal(PQExpBuffer buf, const char *str);
extern void appendConnStrVal(PQExpBuffer buf, const char *str);
extern void
appendShellString(PQExpBuffer buf, const char *str);
extern void appendShellString(PQExpBuffer buf, const char *str);
#endif

View File

@@ -63,10 +63,10 @@ load_config(const char *config_file, bool verbose, bool terse, t_configuration_o
struct stat stat_config;
/*
* If a configuration file was provided, check it exists, otherwise
* emit an error and terminate. We assume that if a user explicitly
* provides a configuration file, they'll want to make sure it's
* used and not fall back to any of the defaults.
* If a configuration file was provided, check it exists, otherwise emit
* an error and terminate. We assume that if a user explicitly provides a
* configuration file, they'll want to make sure it's used and not fall
* back to any of the defaults.
*/
if (config_file != NULL && config_file[0] != '\0')
{
@@ -92,18 +92,19 @@ load_config(const char *config_file, bool verbose, bool terse, t_configuration_o
}
/*
/*-----------
* If no configuration file was provided, attempt to find a default file
* in this order:
* - current directory
* - /etc/repmgr.conf
* - default sysconfdir
*
* here we just check for the existence of the file; parse_config()
* will handle read errors etc.
* here we just check for the existence of the file; parse_config() will
* handle read errors etc.
*
* XXX modify this section so package maintainers can provide a patch
* specifying location of a distribution-specific configuration file
*-----------
*/
if (config_file_provided == false)
{
@@ -273,7 +274,8 @@ _parse_config(t_configuration_options *options, ItemList *error_list, ItemList *
/* default to 6 reconnection attempts at intervals of 10 seconds */
options->reconnect_attempts = DEFAULT_RECONNECTION_ATTEMPTS;
options->reconnect_interval = DEFAULT_RECONNECTION_INTERVAL;
options->monitoring_history = false; /* new in 4.0, replaces --monitoring-history */
options->monitoring_history = false; /* new in 4.0, replaces
* --monitoring-history */
options->degraded_monitoring_timeout = -1;
options->async_query_timeout = DEFAULT_ASYNC_QUERY_TIMEOUT;
options->primary_notification_timeout = DEFAULT_PRIMARY_NOTIFICATION_TIMEOUT;
@@ -333,9 +335,9 @@ _parse_config(t_configuration_options *options, ItemList *error_list, ItemList *
fp = fopen(config_file_path, "r");
/*
* A configuration file has been found, either provided by the user
* or found in one of the default locations. If we can't open it,
* fail with an error.
* A configuration file has been found, either provided by the user or
* found in one of the default locations. If we can't open it, fail with
* an error.
*/
if (fp == NULL)
{
@@ -524,7 +526,10 @@ _parse_config(t_configuration_options *options, ItemList *error_list, ItemList *
else if (strcmp(name, "promote_delay") == 0)
options->promote_delay = repmgr_atoi(value, name, error_list, 1);
/* Following parameters have been deprecated or renamed from 3.x - issue a warning */
/*
* Following parameters have been deprecated or renamed from 3.x -
* issue a warning
*/
else if (strcmp(name, "cluster") == 0)
{
item_list_append(warning_list,
@@ -578,14 +583,18 @@ _parse_config(t_configuration_options *options, ItemList *error_list, ItemList *
known_parameter = false;
log_warning(_("%s/%s: unknown name/value pair provided; ignoring"), name, value);
}
/*
* Raise an error if a known parameter is provided with an empty value.
* Currently there's no reason why empty parameters are needed; if
* we want to accept those, we'd need to add stricter default checking,
* as currently e.g. an empty `node` value will be converted to '0'.
* Raise an error if a known parameter is provided with an empty
* value. Currently there's no reason why empty parameters are needed;
* if we want to accept those, we'd need to add stricter default
* checking, as currently e.g. an empty `node` value will be converted
* to '0'.
*/
if (known_parameter == true && !strlen(value)) {
if (known_parameter == true && !strlen(value))
{
char error_message_buf[MAXLEN] = "";
maxlen_snprintf(error_message_buf,
_("\"%s\": no value provided"),
name);
@@ -618,10 +627,11 @@ _parse_config(t_configuration_options *options, ItemList *error_list, ItemList *
}
else
{
/* Sanity check the provided conninfo string
/*
* Sanity check the provided conninfo string
*
* NOTE: PQconninfoParse() verifies the string format and checks for valid options
* but does not sanity check values
* NOTE: PQconninfoParse() verifies the string format and checks for
* valid options but does not sanity check values
*/
PQconninfoOption *conninfo_options = NULL;
@@ -631,6 +641,7 @@ _parse_config(t_configuration_options *options, ItemList *error_list, ItemList *
if (conninfo_options == NULL)
{
char error_message_buf[MAXLEN] = "";
snprintf(error_message_buf,
MAXLEN,
_("\"conninfo\": %s (provided: \"%s\")"),
@@ -1234,7 +1245,8 @@ repmgr_atoi(const char *value, const char *config_item, ItemList *error_list, in
initPQExpBuffer(&errors);
/* It's possible that some versions of strtol() don't treat an empty
/*
* It's possible that some versions of strtol() don't treat an empty
* string as an error.
*/
if (*value == '\0')
@@ -1510,13 +1522,14 @@ parse_pg_basebackup_options(const char *pg_basebackup_options, t_basebackup_opti
char *options_string_ptr = NULL;
/*
* Add parsed options to this list, then copy to an array
* to pass to getopt
* Add parsed options to this list, then copy to an array to pass to
* getopt
*/
static ItemList option_argv = {NULL, NULL};
char *argv_item = NULL;
int c, argc_item = 1;
int c,
argc_item = 1;
char **argv_array = NULL;
ItemListCell *cell = NULL;
@@ -1536,8 +1549,8 @@ parse_pg_basebackup_options(const char *pg_basebackup_options, t_basebackup_opti
};
/*
* From PostgreSQL 10, --xlog-method is renamed --wal-method
* and there's also --no-slot, which we'll want to consider.
* From PostgreSQL 10, --xlog-method is renamed --wal-method and there's
* also --no-slot, which we'll want to consider.
*/
static struct option long_options_10[] =
{
@@ -1576,8 +1589,8 @@ parse_pg_basebackup_options(const char *pg_basebackup_options, t_basebackup_opti
/*
* Array of argument values to pass to getopt_long - this will need to
* include an empty string as the first value (normally this would be
* the program name)
* include an empty string as the first value (normally this would be the
* program name)
*/
argv_array = pg_malloc0(sizeof(char *) * (argc_item + 2));
@@ -1648,6 +1661,7 @@ parse_pg_basebackup_options(const char *pg_basebackup_options, t_basebackup_opti
{
int i;
for (i = 0; i < argc_item + 2; i++)
pfree(argv_array[i]);
}

View File

@@ -31,7 +31,8 @@
extern bool config_file_found;
extern char config_file_path[MAXPGPATH];
typedef enum {
typedef enum
{
FAILOVER_MANUAL,
FAILOVER_AUTOMATIC
} failover_mode_opt;
@@ -186,7 +187,8 @@ typedef struct
#define T_BASEBACKUP_OPTIONS_INITIALIZER { "", "", false }
typedef enum {
typedef enum
{
RTA_PAUSE,
RTA_PROMOTE,
RTA_SHUTDOWN

204
dbutils.c
View File

@@ -62,7 +62,8 @@ XLogRecPtr
parse_lsn(const char *str)
{
XLogRecPtr ptr = InvalidXLogRecPtr;
uint32 high, low;
uint32 high,
low;
if (sscanf(str, "%x/%x", &high, &low) == 2)
ptr = (((XLogRecPtr) high) << 32) + (XLogRecPtr) low;
@@ -175,7 +176,8 @@ _establish_db_connection(const char *conninfo, const bool exit_on_error, const b
}
/*
* set "synchronous_commit" to "local" in case synchronous replication is in use
* set "synchronous_commit" to "local" in case synchronous replication is
* in use
*
* XXX set this explicitly before any write operations
*/
@@ -218,7 +220,8 @@ establish_db_connection_quiet(const char *conninfo)
PGconn
*establish_primary_db_connection(PGconn *conn,
*
establish_primary_db_connection(PGconn *conn,
const bool exit_on_error)
{
t_node_info primary_node_info = T_NODE_INFO_INITIALIZER;
@@ -294,8 +297,8 @@ establish_db_connection_by_params(t_conninfo_param_list *param_list,
int i;
/*
* set "synchronous_commit" to "local" in case synchronous replication is in
* use (provided this is not a replication connection)
* set "synchronous_commit" to "local" in case synchronous replication
* is in use (provided this is not a replication connection)
*/
for (i = 0; param_list->keywords[i]; i++)
@@ -451,6 +454,7 @@ void
copy_conninfo_params(t_conninfo_param_list *dest_list, t_conninfo_param_list *source_list)
{
int c;
for (c = 0; c < source_list->size && source_list->keywords[c] != NULL; c++)
{
if (source_list->values[c] != NULL && source_list->values[c][0] != '\0')
@@ -489,6 +493,7 @@ param_set(t_conninfo_param_list *param_list, const char *param, const char *valu
if (c < param_list->size)
{
int param_len = strlen(param) + 1;
param_list->keywords[c] = pg_malloc0(param_len);
param_list->values[c] = pg_malloc0(value_len);
@@ -497,9 +502,9 @@ param_set(t_conninfo_param_list *param_list, const char *param, const char *valu
}
/*
* It's theoretically possible a parameter couldn't be added as
* the array is full, but it's highly improbable so we won't
* handle it at the moment.
* It's theoretically possible a parameter couldn't be added as the array
* is full, but it's highly improbable so we won't handle it at the
* moment.
*/
}
@@ -531,6 +536,7 @@ param_set_ine(t_conninfo_param_list *param_list, const char *param, const char *
if (c < param_list->size)
{
int param_len = strlen(param) + 1;
param_list->keywords[c] = pg_malloc0(param_len);
param_list->values[c] = pg_malloc0(value_len);
@@ -1261,8 +1267,7 @@ get_replication_info(PGconn *conn, ReplInfo *replication_info)
server_version_num = get_server_version(conn, NULL);
initPQExpBuffer(&query);
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
" SELECT ts, "
" last_wal_receive_lsn, "
" last_wal_replay_lsn, "
@@ -1277,8 +1282,7 @@ get_replication_info(PGconn *conn, ReplInfo *replication_info)
if (server_version_num >= 100000)
{
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
" SELECT CURRENT_TIMESTAMP AS ts, "
" pg_catalog.pg_last_wal_receive_lsn() AS last_wal_receive_lsn, "
" pg_catalog.pg_last_wal_replay_lsn() AS last_wal_replay_lsn, "
@@ -1286,16 +1290,14 @@ get_replication_info(PGconn *conn, ReplInfo *replication_info)
}
else
{
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
" SELECT CURRENT_TIMESTAMP AS ts, "
" pg_catalog.pg_last_xlog_receive_location() AS last_wal_receive_lsn, "
" pg_catalog.pg_last_xlog_replay_location() AS last_wal_replay_lsn, "
" pg_catalog.pg_last_xact_replay_timestamp() AS last_xact_replay_timestamp ");
}
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
" ) q ");
log_verbose(LOG_DEBUG, "get_replication_info():\n%s", query.data);
@@ -1318,6 +1320,7 @@ get_replication_info(PGconn *conn, ReplInfo *replication_info)
strncpy(replication_info->last_xact_replay_timestamp, PQgetvalue(res, 0, 3), MAXLEN);
replication_info->replication_lag_time = atoi(PQgetvalue(res, 0, 4));
replication_info->receiving_streamed_wal = atobool(PQgetvalue(res, 0, 5));
PQclear(res);
return true;
@@ -1334,8 +1337,7 @@ can_use_pg_rewind(PGconn *conn, const char *data_directory, PQExpBufferData *rea
if (server_version_num < 90500)
{
appendPQExpBuffer(
reason,
appendPQExpBuffer(reason,
_("pg_rewind available from PostgreSQL 9.5"));
return false;
}
@@ -1345,18 +1347,17 @@ can_use_pg_rewind(PGconn *conn, const char *data_directory, PQExpBufferData *rea
if (can_use == false)
appendPQExpBuffer(reason, "; ");
appendPQExpBuffer(
reason,
appendPQExpBuffer(reason,
_("\"full_page_writes\" must be set to \"on\""));
can_use = false;
}
/*
* "wal_log_hints" off - are data checksums available?
* Note: we're checking the local pg_control file here as the value will
* be the same throughout the cluster and saves a round-trip to the
* demotion candidate.
* "wal_log_hints" off - are data checksums available? Note: we're
* checking the local pg_control file here as the value will be the same
* throughout the cluster and saves a round-trip to the demotion
* candidate.
*/
if (guc_set(conn, "wal_log_hints", "=", "on") == false)
{
@@ -1367,8 +1368,7 @@ can_use_pg_rewind(PGconn *conn, const char *data_directory, PQExpBufferData *rea
if (can_use == false)
appendPQExpBuffer(reason, "; ");
appendPQExpBuffer(
reason,
appendPQExpBuffer(reason,
_("\"wal_log_hints\" is set to \"off\" but unable to determine checksum version"));
can_use = false;
}
@@ -1377,8 +1377,7 @@ can_use_pg_rewind(PGconn *conn, const char *data_directory, PQExpBufferData *rea
if (can_use == false)
appendPQExpBuffer(reason, "; ");
appendPQExpBuffer(
reason,
appendPQExpBuffer(reason,
_("\"wal_log_hints\" is set to \"off\" and checksums are disabled"));
can_use = false;
@@ -1456,8 +1455,8 @@ get_ready_archive_files(PGconn *conn, const char *data_directory)
basenamelen = (int) strlen(arcdir_ent->d_name) - 6;
/*
* count anything ending in ".ready"; for a more precise implementation
* see: src/backend/postmaster/pgarch.c
* count anything ending in ".ready"; for a more precise
* implementation see: src/backend/postmaster/pgarch.c
*/
if (strcmp(arcdir_ent->d_name + basenamelen, ".ready") == 0)
ready_count++;
@@ -1483,20 +1482,17 @@ get_replication_lag_seconds(PGconn *conn)
if (server_version_num >= 100000)
{
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
" SELECT CASE WHEN (pg_catalog.pg_last_wal_receive_lsn() = pg_catalog.pg_last_wal_replay_lsn()) ");
}
else
{
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
" SELECT CASE WHEN (pg_catalog.pg_last_xlog_receive_location() = pg_catalog.pg_last_xlog_replay_location()) ");
}
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
" THEN 0 "
" ELSE EXTRACT(epoch FROM (pg_catalog.clock_timestamp() - pg_catalog.pg_last_xact_replay_timestamp()))::INT "
" END "
@@ -1557,8 +1553,7 @@ repmgrd_set_local_node_id(PGconn *conn, int local_node_id)
initPQExpBuffer(&query);
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
" SELECT repmgr.set_local_node_id(%i)",
local_node_id);
@@ -1581,7 +1576,8 @@ repmgrd_set_local_node_id(PGconn *conn, int local_node_id)
/* result functions */
/* ================ */
bool atobool(const char *value)
bool
atobool(const char *value)
{
return (strcmp(value, "t") == 0)
? true
@@ -1895,7 +1891,8 @@ get_local_node_record(PGconn *conn, int node_id, t_node_info *node_info)
static
void _populate_node_records(PGresult *res, NodeInfoList *node_list)
void
_populate_node_records(PGresult *res, NodeInfoList *node_list)
{
int i;
@@ -1909,6 +1906,7 @@ void _populate_node_records(PGresult *res, NodeInfoList *node_list)
for (i = 0; i < PQntuples(res); i++)
{
NodeInfoListCell *cell;
cell = (NodeInfoListCell *) pg_malloc0(sizeof(NodeInfoListCell));
cell->node_info = pg_malloc0(sizeof(t_node_info));
@@ -1936,8 +1934,7 @@ get_all_node_records(PGconn *conn, NodeInfoList *node_list)
initPQExpBuffer(&query);
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
" SELECT " REPMGR_NODES_COLUMNS
" FROM repmgr.nodes "
"ORDER BY node_id ");
@@ -1963,8 +1960,7 @@ get_downstream_node_records(PGconn *conn, int node_id, NodeInfoList *node_list)
initPQExpBuffer(&query);
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
" SELECT " REPMGR_NODES_COLUMNS
" FROM repmgr.nodes "
" WHERE upstream_node_id = %i "
@@ -1993,8 +1989,7 @@ get_active_sibling_node_records(PGconn *conn, int node_id, int upstream_node_id,
initPQExpBuffer(&query);
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
" SELECT " REPMGR_NODES_COLUMNS
" FROM repmgr.nodes "
" WHERE upstream_node_id = %i "
@@ -2026,8 +2021,7 @@ get_node_records_by_priority(PGconn *conn, NodeInfoList *node_list)
initPQExpBuffer(&query);
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
" SELECT " REPMGR_NODES_COLUMNS
" FROM repmgr.nodes "
"ORDER BY priority DESC, node_name ");
@@ -2053,8 +2047,7 @@ get_all_node_records_with_upstream(PGconn *conn, NodeInfoList *node_list)
initPQExpBuffer(&query);
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
" SELECT n.node_id, n.type, n.upstream_node_id, n.node_name, n.conninfo, n.repluser, "
" n.slot_name, n.location, n.priority, n.active, un.node_name AS upstream_node_name "
" FROM repmgr.nodes n "
@@ -2120,10 +2113,11 @@ _create_update_node_record(PGconn *conn, char *action, t_node_info *node_info)
if (node_info->upstream_node_id == NO_UPSTREAM_NODE && node_info->type == STANDBY)
{
/*
* No explicit upstream node id provided for standby - attempt to
* get primary node id
* No explicit upstream node id provided for standby - attempt to get
* primary node id
*/
int primary_node_id = get_primary_node_id(conn);
maxlen_snprintf(upstream_node_id, "%i", primary_node_id);
upstream_node_id_ptr = upstream_node_id;
}
@@ -2394,8 +2388,7 @@ update_node_record_conn_priority(PGconn *conn, t_configuration_options *options)
initPQExpBuffer(&query);
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
"UPDATE repmgr.nodes "
" SET conninfo = '%s', "
" priority = %d "
@@ -2459,8 +2452,7 @@ get_node_replication_stats(PGconn *conn, t_node_info *node_info)
initPQExpBuffer(&query);
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
" SELECT current_setting('max_wal_senders')::INT AS max_wal_senders, "
" (SELECT COUNT(*) FROM pg_catalog.pg_stat_replication) AS attached_wal_receivers, "
" current_setting('max_replication_slots')::INT AS max_replication_slots, "
@@ -2503,8 +2495,7 @@ is_downstream_node_attached(PGconn *conn, char *node_name)
initPQExpBuffer(&query);
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
" SELECT COUNT(*) FROM pg_catalog.pg_stat_replication "
" WHERE application_name = '%s'",
node_name);
@@ -2592,8 +2583,7 @@ get_datadir_configuration_files(PGconn *conn, KeyValueList *list)
initPQExpBuffer(&query);
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
"WITH files AS ( "
" WITH dd AS ( "
" SELECT setting "
@@ -2647,8 +2637,7 @@ get_configuration_file_locations(PGconn *conn, t_configfile_list *list)
initPQExpBuffer(&query);
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
" WITH dd AS ( "
" SELECT setting AS data_directory"
" FROM pg_catalog.pg_settings "
@@ -2697,8 +2686,7 @@ get_configuration_file_locations(PGconn *conn, t_configfile_list *list)
/* Fetch locations of pg_hba.conf and pg_ident.conf */
initPQExpBuffer(&query);
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
" WITH dd AS ( "
" SELECT setting AS data_directory"
" FROM pg_catalog.pg_settings "
@@ -2860,7 +2848,8 @@ _create_event(PGconn *conn, t_configuration_options *options, int node_id, char
* Only attempt to write a record if a connection handle was provided.
* Also check that the repmgr schema has been properly initialised - if
* not it means no configuration file was provided, which can happen with
* e.g. `repmgr standby clone`, and we won't know which schema to write to.
* e.g. `repmgr standby clone`, and we won't know which schema to write
* to.
*/
if (conn != NULL && PQstatus(conn) == CONNECTION_OK)
{
@@ -2924,8 +2913,8 @@ _create_event(PGconn *conn, t_configuration_options *options, int node_id, char
/*
* If no database connection provided, or the query failed, generate a
* current timestamp ourselves. This isn't quite the same
* format as PostgreSQL, but is close enough for diagnostic use.
* current timestamp ourselves. This isn't quite the same format as
* PostgreSQL, but is close enough for diagnostic use.
*/
if (!strlen(event_timestamp))
{
@@ -2949,9 +2938,9 @@ _create_event(PGconn *conn, t_configuration_options *options, int node_id, char
int r = 0;
/*
* If configuration option 'event_notifications' was provided,
* check if this event is one of the ones listed; if not listed,
* don't execute the notification script.
* If configuration option 'event_notifications' was provided, check
* if this event is one of the ones listed; if not listed, don't
* execute the notification script.
*
* (If 'event_notifications' was not provided, we assume the script
* should be executed for all events).
@@ -2971,7 +2960,8 @@ _create_event(PGconn *conn, t_configuration_options *options, int node_id, char
}
/*
* Event type not found in the 'event_notifications' list - return early
* Event type not found in the 'event_notifications' list - return
* early
*/
if (notify_ok == false)
{
@@ -3099,8 +3089,8 @@ create_replication_slot(PGconn *conn, char *slot_name, int server_version_num, P
/*
* Check whether slot exists already; if it exists and is active, that
* means another active standby is using it, which creates an error situation;
* if not we can reuse it as-is
* means another active standby is using it, which creates an error
* situation; if not we can reuse it as-is
*/
record_status = get_slot_record(conn, slot_name, &slot_info);
@@ -3117,7 +3107,7 @@ create_replication_slot(PGconn *conn, char *slot_name, int server_version_num, P
if (slot_info.active == false)
{
// XXX is this a good idea?
/* XXX is this a good idea? */
log_debug("replication slot \"%s\" exists but is inactive; reusing",
slot_name);
@@ -3435,8 +3425,7 @@ add_monitoring_record(
initPQExpBuffer(&query);
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
"INSERT INTO repmgr.monitoring_history "
" (primary_node_id, "
" standby_node_id, "
@@ -3549,22 +3538,20 @@ request_vote(PGconn *conn, t_node_info *this_node, t_node_info *other_node, int
log_debug("NULL returned by repmgr.request_vote()");
/*
* get the node's last receive location anyway
* TODO: have repmgr.request_vote() return two values
* get the node's last receive location anyway TODO: have
* repmgr.request_vote() return two values
*/
initPQExpBuffer(&query);
if (server_version_num >= 100000)
{
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
"SELECT pg_catalog.pg_last_wal_receive_lsn()");
}
else
{
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
"SELECT pg_catalog.pg_last_xlog_receive_location()");
}
@@ -3676,7 +3663,7 @@ notify_follow_primary(PGconn *conn, int primary_node_id)
primary_node_id);
log_verbose(LOG_DEBUG, "notify_follow_primary():\n %s", query.data);
// XXX handle failure
/* XXX handle failure */
res = PQexec(conn, query.data);
termPQExpBuffer(&query);
@@ -3706,7 +3693,7 @@ get_new_primary(PGconn *conn, int *primary_node_id)
res = PQexec(conn, query.data);
termPQExpBuffer(&query);
// XXX handle error
/* XXX handle error */
new_primary_node_id = atoi(PQgetvalue(res, 0, 0));
@@ -3738,7 +3725,7 @@ reset_voting_status(PGconn *conn)
res = PQexec(conn, query.data);
termPQExpBuffer(&query);
// COMMAND_OK?
/* COMMAND_OK? */
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
log_error(_("unable to execute repmgr.reset_voting_status():\n %s"),
@@ -3820,8 +3807,7 @@ is_bdr_db(PGconn *conn, PQExpBufferData *output)
initPQExpBuffer(&query);
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
"SELECT COUNT(*) FROM pg_catalog.pg_extension WHERE extname='bdr'");
res = PQexec(conn, query.data);
@@ -3852,8 +3838,7 @@ is_bdr_db(PGconn *conn, PQExpBufferData *output)
initPQExpBuffer(&query);
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
"SELECT bdr.bdr_is_active_in_db()");
res = PQexec(conn, query.data);
termPQExpBuffer(&query);
@@ -3884,8 +3869,7 @@ is_active_bdr_node(PGconn *conn, const char *node_name)
bool is_active_bdr_node = false;
initPQExpBuffer(&query);
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
" SELECT COALESCE(s.active, TRUE) AS active"
" FROM bdr.bdr_nodes n "
" LEFT JOIN pg_replication_slots s "
@@ -3920,8 +3904,7 @@ is_bdr_repmgr(PGconn *conn)
initPQExpBuffer(&query);
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
"SELECT COUNT(*)"
" FROM repmgr.nodes"
" WHERE type != 'bdr' ");
@@ -3952,8 +3935,7 @@ is_table_in_bdr_replication_set(PGconn *conn, const char *tablename, const char
initPQExpBuffer(&query);
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
"SELECT COUNT(*) "
" FROM UNNEST(bdr.table_get_replication_sets('repmgr.%s')) AS repset "
" WHERE repset='%s' ",
@@ -3987,8 +3969,7 @@ add_table_to_bdr_replication_set(PGconn *conn, const char *tablename, const char
initPQExpBuffer(&query);
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
"SELECT bdr.table_set_replication_sets('repmgr.%s', '{%s}')",
tablename,
set);
@@ -4023,8 +4004,7 @@ bdr_node_exists(PGconn *conn, const char *node_name)
initPQExpBuffer(&query);
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
"SELECT COUNT(*)"
" FROM bdr.bdr_nodes"
" WHERE node_name = '%s'",
@@ -4057,8 +4037,7 @@ get_bdr_node_replication_slot_status(PGconn *conn, const char *node_name)
initPQExpBuffer(&query);
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
" SELECT s.active "
" FROM pg_catalog.pg_replication_slots s "
" WHERE slot_name = "
@@ -4097,8 +4076,7 @@ get_bdr_other_node_name(PGconn *conn, int node_id, char *node_name)
initPQExpBuffer(&query);
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
" SELECT node_name "
" FROM repmgr.nodes "
" WHERE node_id != %i",
@@ -4132,8 +4110,7 @@ add_extension_tables_to_bdr_replication_set(PGconn *conn)
initPQExpBuffer(&query);
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
" SELECT c.relname "
" FROM pg_class c "
"INNER JOIN pg_namespace n "
@@ -4146,7 +4123,7 @@ add_extension_tables_to_bdr_replication_set(PGconn *conn)
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
//
/* */
}
else
{
@@ -4174,8 +4151,7 @@ get_all_bdr_node_records(PGconn *conn, BdrNodeInfoList *node_list)
initPQExpBuffer(&query);
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
" SELECT " BDR_NODES_COLUMNS
" FROM bdr.bdr_nodes "
"ORDER BY node_seq_id ");
@@ -4199,8 +4175,7 @@ get_bdr_node_record_by_name(PGconn *conn, const char *node_name, t_bdr_node_info
initPQExpBuffer(&query);
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
" SELECT " BDR_NODES_COLUMNS
" FROM bdr.bdr_nodes "
" WHERE node_name = '%s'",
@@ -4236,7 +4211,8 @@ get_bdr_node_record_by_name(PGconn *conn, const char *node_name, t_bdr_node_info
static
void _populate_bdr_node_records(PGresult *res, BdrNodeInfoList *node_list)
void
_populate_bdr_node_records(PGresult *res, BdrNodeInfoList *node_list)
{
int i;
@@ -4250,6 +4226,7 @@ void _populate_bdr_node_records(PGresult *res, BdrNodeInfoList *node_list)
for (i = 0; i < PQntuples(res); i++)
{
BdrNodeInfoListCell *cell;
cell = (BdrNodeInfoListCell *) pg_malloc0(sizeof(BdrNodeInfoListCell));
cell->node_info = pg_malloc0(sizeof(t_bdr_node_info));
@@ -4294,8 +4271,7 @@ am_bdr_failover_handler(PGconn *conn, int node_id)
initPQExpBuffer(&query);
appendPQExpBuffer(
&query,
appendPQExpBuffer(&query,
"SELECT repmgr.am_bdr_failover_handler(%i)",
node_id);
@@ -4322,9 +4298,9 @@ void
unset_bdr_failover_handler(PGconn *conn)
{
PGresult *res = NULL;
res = PQexec(conn, "SELECT repmgr.unset_bdr_failover_handler()");
PQclear(res);
return;
}

View File

@@ -33,52 +33,60 @@
#define ERRBUFF_SIZE 512
typedef enum {
typedef enum
{
UNKNOWN = 0,
PRIMARY,
STANDBY,
BDR
} t_server_type;
typedef enum {
typedef enum
{
REPMGR_INSTALLED = 0,
REPMGR_AVAILABLE,
REPMGR_UNAVAILABLE,
REPMGR_UNKNOWN
} ExtensionStatus;
typedef enum {
typedef enum
{
RECTYPE_UNKNOWN = -1,
RECTYPE_PRIMARY,
RECTYPE_STANDBY
} RecoveryType;
typedef enum {
typedef enum
{
RECORD_ERROR = -1,
RECORD_FOUND,
RECORD_NOT_FOUND
} RecordStatus;
typedef enum {
typedef enum
{
MS_NORMAL = 0,
MS_DEGRADED = 1
} MonitoringState;
typedef enum {
typedef enum
{
NODE_STATUS_UNKNOWN = -1,
NODE_STATUS_UP,
NODE_STATUS_DOWN,
NODE_STATUS_UNCLEAN_SHUTDOWN
} NodeStatus;
typedef enum {
typedef enum
{
VR_VOTE_REFUSED = -1,
VR_POSITIVE_VOTE,
VR_NEGATIVE_VOTE
} VoteRequestResult;
typedef enum {
typedef enum
{
SLOT_UNKNOWN = -1,
SLOT_INACTIVE,
SLOT_ACTIVE
@@ -260,7 +268,8 @@ typedef struct BdrNodeInfoList
0 \
}
typedef struct {
typedef struct
{
char current_timestamp[MAXLEN];
uint64 last_wal_receive_lsn;
uint64 last_wal_replay_lsn;
@@ -321,7 +330,8 @@ extern int server_version_num;
XLogRecPtr parse_lsn(const char *str);
extern void wrap_ddl_query(PQExpBufferData *query_buf, int replication_type, const char *fmt, ...)
extern void
wrap_ddl_query(PQExpBufferData *query_buf, int replication_type, const char *fmt,...)
__attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
bool atobool(const char *value);
@@ -445,7 +455,8 @@ int wait_connection_availability(PGconn *conn, long long timeout);
bool is_server_available(const char *conninfo);
/* monitoring functions */
void add_monitoring_record(
void
add_monitoring_record(
PGconn *primary_conn,
PGconn *local_conn,
int primary_node_id,
@@ -494,4 +505,3 @@ bool am_bdr_failover_handler(PGconn *conn, int node_id);
void unset_bdr_failover_handler(PGconn *conn);
#endif /* _REPMGR_DBUTILS_H_ */

View File

@@ -321,4 +321,3 @@ unlink_dir_callback(const char *fpath, const struct stat *sb, int typeflag, stru
return rv;
}

View File

@@ -45,4 +45,3 @@
#define ERR_OUT_OF_MEMORY 21
#endif /* _ERRCODE_H_ */

29
log.c
View File

@@ -37,7 +37,8 @@
/* #define REPMGR_DEBUG */
static int detect_log_facility(const char *facility);
static void _stderr_log_with_level(const char *level_name, int level, const char *fmt, va_list ap)
static void
_stderr_log_with_level(const char *level_name, int level, const char *fmt, va_list ap)
__attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 0)));
int log_type = REPMGR_STDERR;
@@ -45,6 +46,7 @@ int log_level = LOG_NOTICE;
int last_log_level = LOG_INFO;
int verbose_logging = false;
int terse_logging = false;
/*
* Global variable to be set by the main application to ensure any log output
* emitted before logger_init is called, is output in the correct format
@@ -67,8 +69,8 @@ _stderr_log_with_level(const char *level_name, int level, const char *fmt, va_li
char buf[100];
/*
* Store the requested level so that if there's a subsequent
* log_hint() or log_detail(), we can suppress that if appropriate.
* Store the requested level so that if there's a subsequent log_hint() or
* log_detail(), we can suppress that if appropriate.
*/
last_log_level = level;
@@ -80,6 +82,7 @@ _stderr_log_with_level(const char *level_name, int level, const char *fmt, va_li
{
time_t t;
struct tm *tm;
time(&t);
tm = localtime(&t);
strftime(buf, 100, "[%Y-%m-%d %H:%M:%S]", tm);
@@ -202,8 +205,8 @@ logger_init(t_configuration_options *opts, const char *ident)
}
/*
* STDERR only logging requested - finish here without setting up any further
* logging facility.
* STDERR only logging requested - finish here without setting up any
* further logging facility.
*/
if (logger_output_mode == OM_COMMAND_LINE)
return true;
@@ -251,9 +254,10 @@ logger_init(t_configuration_options *opts, const char *ident)
{
FILE *fd;
/* Check if we can write to the specified file before redirecting
* stderr - if freopen() fails, stderr output will vanish into
* the ether and the user won't know what's going on.
/*
* Check if we can write to the specified file before redirecting
* stderr - if freopen() fails, stderr output will vanish into the
* ether and the user won't know what's going on.
*/
fd = fopen(opts->log_file, "a");
@@ -270,9 +274,9 @@ logger_init(t_configuration_options *opts, const char *ident)
fd = freopen(opts->log_file, "a", stderr);
/*
* It's possible freopen() may still fail due to e.g. a race condition;
* as it's not feasible to restore stderr after a failed freopen(),
* we'll write to stdout as a last resort.
* It's possible freopen() may still fail due to e.g. a race
* condition; as it's not feasible to restore stderr after a failed
* freopen(), we'll write to stdout as a last resort.
*/
if (fd == NULL)
{
@@ -318,7 +322,8 @@ logger_set_verbose(void)
* options and hints.
*/
void logger_set_terse(void)
void
logger_set_terse(void)
{
terse_logging = true;
}

9
log.h
View File

@@ -129,11 +129,14 @@ bool logger_shutdown(void);
void logger_set_verbose(void);
void logger_set_terse(void);
void log_detail(const char *fmt, ...)
void
log_detail(const char *fmt,...)
__attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
void log_hint(const char *fmt, ...)
void
log_hint(const char *fmt,...)
__attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
void log_verbose(int level, const char *fmt, ...)
void
log_verbose(int level, const char *fmt,...)
__attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
extern int log_type;

View File

@@ -136,15 +136,15 @@ do_bdr_register(void)
}
/*
* before adding the extension tables to the replication set,
* if any other BDR nodes exist, populate repmgr.nodes with a copy
* of existing entries
* before adding the extension tables to the replication set, if any other
* BDR nodes exist, populate repmgr.nodes with a copy of existing entries
*
* currently we won't copy the contents of any other tables
*
*/
{
NodeInfoList local_node_records = T_NODE_INFO_LIST_INITIALIZER;
get_all_node_records(conn, &local_node_records);
if (local_node_records.node_count == 0)
@@ -236,9 +236,10 @@ do_bdr_register(void)
if (record_status == RECORD_FOUND)
{
bool node_updated = false;
/*
* At this point we will have established there are no non-BDR records,
* so no need to verify the node type
* At this point we will have established there are no non-BDR
* records, so no need to verify the node type
*/
if (!runtime_options.force)
{
@@ -250,8 +251,8 @@ do_bdr_register(void)
}
/*
* don't permit changing the node name - this must match the
* BDR node name set when the node was registered.
* don't permit changing the node name - this must match the BDR node
* name set when the node was registered.
*/
if (strncmp(node_info.node_name, config_file_options.node_name, MAXLEN) != 0)

View File

@@ -27,7 +27,8 @@
#define SHOW_HEADER_COUNT 7
typedef enum {
typedef enum
{
SHOW_ID = 0,
SHOW_NAME,
SHOW_ROLE,
@@ -39,7 +40,8 @@ typedef enum {
#define EVENT_HEADER_COUNT 6
typedef enum {
typedef enum
{
EV_NODE_ID = 0,
EV_NODE_NAME,
EV_EVENT,
@@ -50,7 +52,8 @@ typedef enum {
struct ColHeader {
struct ColHeader
{
char title[MAXLEN];
int max_length;
int cur_length;
@@ -106,8 +109,8 @@ do_cluster_show(void)
strncpy(headers_show[SHOW_CONNINFO].title, _("Connection string"), MAXLEN);
/*
* NOTE: if repmgr is ever localized into non-ASCII locales,
* use pg_wcssize() or similar to establish printed column length
* NOTE: if repmgr is ever localized into non-ASCII locales, use
* pg_wcssize() or similar to establish printed column length
*/
for (i = 0; i < SHOW_HEADER_COUNT; i++)
@@ -136,7 +139,8 @@ do_cluster_show(void)
/*
* TODO: count nodes marked as "? unreachable" and add a hint about
* the other cluster commands for better determining whether unreachable.
* the other cluster commands for better determining whether
* unreachable.
*/
switch (cell->node_info->type)
{
@@ -345,6 +349,7 @@ do_cluster_show(void)
for (i = 0; i < SHOW_HEADER_COUNT; i++)
{
int j;
for (j = 0; j < headers_show[i].max_length; j++)
printf("-");
@@ -365,8 +370,8 @@ do_cluster_show(void)
int recovery_type = RECTYPE_UNKNOWN;
/*
* here we explicitly convert the RecoveryType to integer values to
* avoid implicit dependency on the values in the enum
* here we explicitly convert the RecoveryType to integer values
* to avoid implicit dependency on the values in the enum
*/
switch (cell->node_info->recovery_type)
{
@@ -516,7 +521,8 @@ do_cluster_event(void)
exit(ERR_DB_QUERY);
}
if (PQntuples(res) == 0) {
if (PQntuples(res) == 0)
{
/* print this message directly, rather than as a log line */
printf(_("no matching events found\n"));
PQclear(res);
@@ -567,6 +573,7 @@ do_cluster_event(void)
for (i = 0; i < EVENT_HEADER_COUNT; i++)
{
int j;
for (j = 0; j < headers_event[i].max_length; j++)
printf("-");
@@ -607,7 +614,8 @@ do_cluster_event(void)
void
do_cluster_crosscheck(void)
{
int i = 0, n = 0;
int i = 0,
n = 0;
char c;
const char *node_header = "Name";
int name_length = strlen(node_header);
@@ -642,22 +650,23 @@ do_cluster_crosscheck(void)
int node_ix = 0;
/*
* The value of entry (i,j) is equal to the
* maximum value of all the (i,j,k). Indeed:
* The value of entry (i,j) is equal to the maximum value of all
* the (i,j,k). Indeed:
*
* - if one of the (i,j,k) is 0 (node up), then 0
* (the node is up);
* - if one of the (i,j,k) is 0 (node up), then 0 (the node is
* up);
*
* - if the (i,j,k) are either -1 (down) or -2
* (unknown), then -1 (the node is down);
* - if the (i,j,k) are either -1 (down) or -2 (unknown), then -1
* (the node is down);
*
* - if all the (i,j,k) are -2 (unknown), then -2
* (the node is in an unknown state).
* - if all the (i,j,k) are -2 (unknown), then -2 (the node is in
* an unknown state).
*/
for (node_ix = 0; node_ix < n; node_ix++)
{
int node_status = cube[node_ix]->matrix_list_rec[i]->node_status_list[column_node_ix]->node_status;
if (node_status > max_node_status)
max_node_status = node_status;
}
@@ -685,7 +694,9 @@ do_cluster_crosscheck(void)
/* clean up allocated cube array */
{
int h, j;
int h,
j;
for (h = 0; h < n; h++)
{
for (i = 0; i < n; i++)
@@ -710,7 +721,9 @@ do_cluster_crosscheck(void)
void
do_cluster_matrix()
{
int i = 0, j = 0, n = 0;
int i = 0,
j = 0,
n = 0;
const char *node_header = "Name";
int name_length = strlen(node_header);
@@ -789,7 +802,8 @@ do_cluster_matrix()
static void
matrix_set_node_status(t_node_matrix_rec **matrix_rec_list, int n, int node_id, int connection_node_id, int connection_status)
{
int i, j;
int i,
j;
for (i = 0; i < n; i++)
{
@@ -813,7 +827,8 @@ static int
build_cluster_matrix(t_node_matrix_rec ***matrix_rec_dest, int *name_length)
{
PGconn *conn = NULL;
int i = 0, j = 0;
int i = 0,
j = 0;
int local_node_id = UNKNOWN_NODE_ID;
int node_count = 0;
NodeInfoList nodes = T_NODE_INFO_LIST_INITIALIZER;
@@ -852,9 +867,7 @@ build_cluster_matrix(t_node_matrix_rec ***matrix_rec_dest, int *name_length)
/*
* Allocate an empty matrix record list
*
* -2 == NULL ?
* -1 == Error x
* 0 == OK *
* -2 == NULL ? -1 == Error x 0 == OK *
*/
matrix_rec_list = (t_node_matrix_rec **) pg_malloc0(sizeof(t_node_matrix_rec) * nodes.node_count);
@@ -902,9 +915,11 @@ build_cluster_matrix(t_node_matrix_rec ***matrix_rec_dest, int *name_length)
{
int connection_status = 0;
t_conninfo_param_list remote_conninfo = T_CONNINFO_PARAM_LIST_INITIALIZER;
char *host = NULL, *p = NULL;
char *host = NULL,
*p = NULL;
int connection_node_id = cell->node_info->node_id;
int x, y;
int x,
y;
PGconn *node_conn = NULL;
initialize_conninfo_params(&remote_conninfo, false);
@@ -948,9 +963,9 @@ build_cluster_matrix(t_node_matrix_rec ***matrix_rec_dest, int *name_length)
initPQExpBuffer(&command);
/*
* We'll pass cluster name and database connection string to the remote
* repmgr - those are the only values it needs to work, and saves us
* making assumptions about the location of repmgr.conf
* We'll pass cluster name and database connection string to the
* remote repmgr - those are the only values it needs to work, and
* saves us making assumptions about the location of repmgr.conf
*/
appendPQExpBuffer(&command,
"\"%s -d '%s' ",
@@ -1026,7 +1041,9 @@ static int
build_cluster_crosscheck(t_node_status_cube ***dest_cube, int *name_length)
{
PGconn *conn = NULL;
int h, i, j;
int h,
i,
j;
NodeInfoList nodes = T_NODE_INFO_LIST_INITIALIZER;
NodeInfoListCell *cell = NULL;
@@ -1056,9 +1073,7 @@ build_cluster_crosscheck(t_node_status_cube ***dest_cube, int *name_length)
/*
* Allocate an empty cube matrix structure
*
* -2 == NULL
* -1 == Error
* 0 == OK
* -2 == NULL -1 == Error 0 == OK
*/
cube = (t_node_status_cube **) pg_malloc(sizeof(t_node_status_cube *) * nodes.node_count);
@@ -1243,7 +1258,9 @@ build_cluster_crosscheck(t_node_status_cube ***dest_cube, int *name_length)
static void
cube_set_node_status(t_node_status_cube **cube, int n, int execute_node_id, int matrix_node_id, int connection_node_id, int connection_status)
{
int h, i, j;
int h,
i,
j;
for (h = 0; h < n; h++)

View File

@@ -268,6 +268,7 @@ do_node_status(void)
if (node_info.max_replication_slots > 0)
{
PQExpBufferData slotinfo;
initPQExpBuffer(&slotinfo);
appendPQExpBuffer(
@@ -456,7 +457,8 @@ do_node_status(void)
*/
static
void _do_node_status_is_shutdown_cleanly(void)
void
_do_node_status_is_shutdown_cleanly(void)
{
PGPing ping_status;
PQExpBufferData output;
@@ -503,7 +505,10 @@ void _do_node_status_is_shutdown_cleanly(void)
if (db_state != DB_SHUTDOWNED && db_state != DB_SHUTDOWNED_IN_RECOVERY)
{
/* node is not running, but pg_controldata says it is - unclean shutdown */
/*
* node is not running, but pg_controldata says it is - unclean
* shutdown
*/
if (node_status != NODE_STATUS_UP)
{
node_status = NODE_STATUS_UNCLEAN_SHUTDOWN;
@@ -517,7 +522,11 @@ void _do_node_status_is_shutdown_cleanly(void)
{
node_status = NODE_STATUS_UNKNOWN;
}
/* if still "UNKNOWN" at this point, then the node must be cleanly shut down */
/*
* if still "UNKNOWN" at this point, then the node must be cleanly shut
* down
*/
else if (node_status == NODE_STATUS_UNKNOWN)
{
node_status = NODE_STATUS_DOWN;
@@ -576,8 +585,9 @@ do_node_check(void)
/* add replication statistics to node record */
get_node_replication_stats(conn, &node_info);
/* handle specific checks
* ====================== */
/*
* handle specific checks ======================
*/
if (runtime_options.archive_ready == true)
{
(void) do_node_check_archive_ready(conn, runtime_options.output_mode, NULL);
@@ -1269,6 +1279,7 @@ do_node_check_downstream(PGconn *conn, OutputMode mode, CheckStatusList *list_ou
{
ItemListCell *missing_cell = NULL;
bool first = true;
status = CHECK_STATUS_CRITICAL;
appendPQExpBuffer(
@@ -1330,6 +1341,7 @@ do_node_check_downstream(PGconn *conn, OutputMode mode, CheckStatusList *list_ou
{
ItemListCell *attached_cell = NULL;
bool first = true;
if (missing_nodes_count)
printf("; ");
printf("attached: ");
@@ -1433,7 +1445,7 @@ do_node_service(void)
log_notice(_("issuing CHECKPOINT"));
// check superuser conn!
/* check superuser conn! */
checkpoint(conn);
PQfinish(conn);
@@ -1661,8 +1673,8 @@ do_node_rejoin(void)
}
/*
* Forcibly rewind node if requested (this is mainly for use when
* this action is being executed by "repmgr standby switchover")
* Forcibly rewind node if requested (this is mainly for use when this
* action is being executed by "repmgr standby switchover")
*/
if (runtime_options.force_rewind == true)
{
@@ -1830,8 +1842,8 @@ _do_node_archive_config(void)
}
/*
* attempt to remove any existing files in the directory
* TODO: collate problem files into list
* attempt to remove any existing files in the directory TODO: collate
* problem files into list
*/
while ((arcdir_ent = readdir(arcdir)) != NULL)
{
@@ -2027,8 +2039,9 @@ _do_node_restore_config(void)
}
/*
* Finally, delete directory - it should be empty unless it's been interfered
* with for some reason, in which case manual intervention is required
* Finally, delete directory - it should be empty unless it's been
* interfered with for some reason, in which case manual intervention is
* required
*/
if (rmdir(archive_dir) != 0 && errno != EEXIST)
{
@@ -2063,7 +2076,8 @@ format_archive_dir(char *archive_dir)
static bool
copy_file(const char *src_file, const char *dest_file)
{
FILE *ptr_old, *ptr_new;
FILE *ptr_old,
*ptr_new;
int a = 0;
ptr_old = fopen(src_file, "r");

View File

@@ -83,8 +83,8 @@ do_primary_register(void)
/*
* In --dry-run mode we can't proceed any further as the following code
* attempts to query the repmgr metadata, which won't exist until
* the extension is installed
* attempts to query the repmgr metadata, which won't exist until the
* extension is installed
*/
if (runtime_options.dry_run == true)
{
@@ -100,7 +100,10 @@ do_primary_register(void)
{
if (current_primary_id != config_file_options.node_id)
{
/* it's impossible to add a second primary to a streaming replication cluster */
/*
* it's impossible to add a second primary to a streaming
* replication cluster
*/
log_error(_("there is already an active registered primary (node ID: %i) in this cluster"), current_primary_id);
PQfinish(primary_conn);
PQfinish(conn);
@@ -115,8 +118,9 @@ do_primary_register(void)
begin_transaction(conn);
/*
* Check for an active primary node record with a different ID. This shouldn't
* happen, but could do if an existing primary was shut down without being unregistered.
* Check for an active primary node record with a different ID. This
* shouldn't happen, but could do if an existing primary was shut down
* without being unregistered.
*/
current_primary_id = get_primary_node_id(conn);
if (current_primary_id != NODE_NOT_FOUND && current_primary_id != config_file_options.node_id)
@@ -130,8 +134,8 @@ do_primary_register(void)
}
/*
* Check whether there's an existing record for this node, and
* update it if --force set
* Check whether there's an existing record for this node, and update it
* if --force set
*/
record_status = get_node_record(conn, config_file_options.node_id, &node_info);
@@ -257,8 +261,8 @@ do_primary_unregister(void)
get_local_node_record(local_conn, config_file_options.node_id, &local_node_info);
/*
* Obtain a connection to the current primary node - if this isn't possible,
* abort as we won't be able to update the "nodes" table anyway.
* Obtain a connection to the current primary node - if this isn't
* possible, abort as we won't be able to update the "nodes" table anyway.
*/
primary_conn = establish_primary_db_connection(local_conn, false);
@@ -367,7 +371,10 @@ do_primary_unregister(void)
/* Node appears to be a standby */
if (recovery_type == RECTYPE_STANDBY)
{
/* We'll refuse to do anything unless the node record shows it as a primary */
/*
* We'll refuse to do anything unless the node record shows it as
* a primary
*/
if (target_node_info_ptr->type != PRIMARY)
{
log_error(_("node %s (ID: %i) is a %s, unable to unregister"),
@@ -376,9 +383,11 @@ do_primary_unregister(void)
get_node_type_string(target_node_info_ptr->type));
can_unregister = false;
}
/*
* If --F/--force not set, hint that it might be appropriate to
* register the node as a standby rather than unregister as primary
* register the node as a standby rather than unregister as
* primary
*/
else if (!runtime_options.force)
{
@@ -415,8 +424,10 @@ do_primary_unregister(void)
PQfinish(primary_conn);
exit(ERR_BAD_CONFIG);
}
/* This appears to be the cluster primary - cowardly refuse
* to delete the record
/*
* This appears to be the cluster primary - cowardly refuse to
* delete the record
*/
if (primary_node_info.node_id == target_node_info_ptr->node_id)
{

File diff suppressed because it is too large Load Diff

View File

@@ -160,12 +160,14 @@ typedef struct
}
typedef enum {
typedef enum
{
barman,
pg_basebackup
} standy_clone_mode;
typedef enum {
typedef enum
{
ACTION_UNKNOWN = -1,
ACTION_NONE,
ACTION_START,

View File

@@ -109,9 +109,9 @@ main(int argc, char **argv)
set_progname(argv[0]);
/*
* Tell the logger we're a command-line program - this will
* ensure any output logged before the logger is initialized
* will be formatted correctly. Can be overriden with "--log-to-file".
* Tell the logger we're a command-line program - this will ensure any
* output logged before the logger is initialized will be formatted
* correctly. Can be overriden with "--log-to-file".
*/
logger_output_mode = OM_COMMAND_LINE;
@@ -119,14 +119,13 @@ main(int argc, char **argv)
* Initialize and pre-populate conninfo parameters; these will be
* overwritten if matching command line parameters are provided.
*
* Only some actions will need these, but we need to do this before
* the command line is parsed.
* Only some actions will need these, but we need to do this before the
* command line is parsed.
*
* Note: PQconndefaults() does not provide a default value for
* "dbname", but if none is provided will default to "username"
* when the connection is made. We won't set "dbname" here if no
* default available, as that would break the libpq behaviour if
* non-default username is provided.
* Note: PQconndefaults() does not provide a default value for "dbname",
* but if none is provided will default to "username" when the connection
* is made. We won't set "dbname" here if no default available, as that
* would break the libpq behaviour if non-default username is provided.
*/
initialize_conninfo_params(&default_conninfo, true);
@@ -180,16 +179,16 @@ main(int argc, char **argv)
&optindex)) != -1)
{
/*
* NOTE: some integer parameters (e.g. -p/--port) are stored internally
* as strings. We use repmgr_atoi() to check these but discard the
* returned integer; repmgr_atoi() will append the error message to the
* provided list.
* NOTE: some integer parameters (e.g. -p/--port) are stored
* internally as strings. We use repmgr_atoi() to check these but
* discard the returned integer; repmgr_atoi() will append the error
* message to the provided list.
*/
switch (c)
{
/*
* Options which cause repmgr to exit in this block;
* these are the only ones which can be executed as root user
* Options which cause repmgr to exit in this block; these are
* the only ones which can be executed as root user
*/
case OPT_HELP: /* --help */
help_option = true;
@@ -202,9 +201,11 @@ main(int argc, char **argv)
}
break;
case 'V':
/*
* in contrast to repmgr3 and earlier, we only display the repmgr version
* as it's not specific to a particular PostgreSQL version
* in contrast to repmgr3 and earlier, we only display the
* repmgr version as it's not specific to a particular
* PostgreSQL version
*/
printf("%s %s\n", progname(), REPMGR_VERSION);
exit(SUCCESS);
@@ -250,16 +251,20 @@ main(int argc, char **argv)
*/
/*
* These are the standard database connection options; with the
* exception of -d/--dbname (which could be a conninfo string)
* we'll also set these values in "source_conninfo" (overwriting
* preset values from environment variables).
* XXX check this is same as psql
* These are the standard database connection options; with
* the exception of -d/--dbname (which could be a conninfo
* string) we'll also set these values in "source_conninfo"
* (overwriting preset values from environment variables). XXX
* check this is same as psql
*/
/* -d/--dbname */
case 'd':
strncpy(runtime_options.dbname, optarg, MAXLEN);
/* dbname will be set in source_conninfo later after checking if it's a conninfo string */
/*
* dbname will be set in source_conninfo later after checking
* if it's a conninfo string
*/
runtime_options.connection_param_provided = true;
break;
@@ -322,8 +327,9 @@ main(int argc, char **argv)
strncpy(runtime_options.node_name, optarg, MAXLEN);
break;
/* standby options *
* --------------- */
/*
* standby options * ---------------
*/
/* --upstream-node-id */
case OPT_UPSTREAM_NODE_ID:
@@ -502,6 +508,7 @@ main(int argc, char **argv)
case 'L':
{
int detected_log_level = detect_log_level(optarg);
if (detected_log_level != -1)
{
strncpy(runtime_options.log_level, optarg, MAXLEN);
@@ -509,6 +516,7 @@ main(int argc, char **argv)
else
{
PQExpBufferData invalid_log_level;
initPQExpBuffer(&invalid_log_level);
appendPQExpBuffer(&invalid_log_level, _("invalid log level \"%s\" provided"), optarg);
item_list_append(&cli_errors, invalid_log_level.data);
@@ -597,6 +605,7 @@ main(int argc, char **argv)
if (opts == NULL)
{
PQExpBufferData conninfo_error;
initPQExpBuffer(&conninfo_error);
appendPQExpBuffer(&conninfo_error, _("error parsing conninfo:\n%s"), errmsg);
item_list_append(&cli_errors, conninfo_error.data);
@@ -613,6 +622,7 @@ main(int argc, char **argv)
* settings take priority
*/
PQconninfoOption *opt;
for (opt = opts; opt->keyword != NULL; opt++)
{
if (opt->val != NULL && opt->val[0] != '\0')
@@ -654,9 +664,9 @@ main(int argc, char **argv)
}
/*
* Disallow further running as root to prevent directory ownership problems.
* We check this here to give the root user a chance to execute --help/--version
* options.
* Disallow further running as root to prevent directory ownership
* problems. We check this here to give the root user a chance to execute
* --help/--version options.
*/
if (geteuid() == 0 && help_option == false)
{
@@ -681,13 +691,14 @@ main(int argc, char **argv)
/*
* Determine the node type and action; following are valid:
*
* { PRIMARY | MASTER } REGISTER |
* STANDBY {REGISTER | UNREGISTER | CLONE [node] | PROMOTE | FOLLOW [node] | SWITCHOVER | REWIND} |
* BDR { REGISTER | UNREGISTER } |
* NODE { STATUS | CHECK | REJOIN | ARCHIVE-CONFIG | RESTORE-CONFIG | SERVICE } |
* CLUSTER { CROSSCHECK | MATRIX | SHOW | CLEANUP | EVENT }
* { PRIMARY | MASTER } REGISTER | STANDBY {REGISTER | UNREGISTER | CLONE
* [node] | PROMOTE | FOLLOW [node] | SWITCHOVER | REWIND} | BDR {
* REGISTER | UNREGISTER } | NODE { STATUS | CHECK | REJOIN |
* ARCHIVE-CONFIG | RESTORE-CONFIG | SERVICE } | CLUSTER { CROSSCHECK |
* MATRIX | SHOW | CLEANUP | EVENT }
*
* [node] is an optional hostname, provided instead of the -h/--host option
* [node] is an optional hostname, provided instead of the -h/--host
* option
*/
if (optind < argc)
{
@@ -823,6 +834,7 @@ main(int argc, char **argv)
if (action == NO_ACTION)
{
PQExpBufferData command_error;
initPQExpBuffer(&command_error);
if (repmgr_command == NULL)
@@ -853,7 +865,10 @@ main(int argc, char **argv)
item_list_append(&cli_errors, command_error.data);
}
/* STANDBY CLONE historically accepts the upstream hostname as an additional argument */
/*
* STANDBY CLONE historically accepts the upstream hostname as an
* additional argument
*/
if (action == STANDBY_CLONE)
{
if (optind < argc)
@@ -861,6 +876,7 @@ main(int argc, char **argv)
if (runtime_options.host_param_provided == true)
{
PQExpBufferData additional_host_arg;
initPQExpBuffer(&additional_host_arg);
appendPQExpBuffer(&additional_host_arg,
_("host name provided both with %s and as an extra parameter"),
@@ -879,6 +895,7 @@ main(int argc, char **argv)
if (optind < argc)
{
PQExpBufferData too_many_args;
initPQExpBuffer(&too_many_args);
appendPQExpBuffer(&too_many_args, _("too many command-line arguments (first extra is \"%s\")"), argv[optind]);
item_list_append(&cli_errors, too_many_args.data);
@@ -886,9 +903,9 @@ main(int argc, char **argv)
/*
* The configuration file is not required for some actions (e.g. 'standby clone'),
* however if available we'll parse it anyway for options like 'log_level',
* 'use_replication_slots' etc.
* The configuration file is not required for some actions (e.g. 'standby
* clone'), however if available we'll parse it anyway for options like
* 'log_level', 'use_replication_slots' etc.
*/
load_config(runtime_options.config_file,
runtime_options.verbose,
@@ -899,8 +916,8 @@ main(int argc, char **argv)
check_cli_parameters(action);
/*
* Sanity checks for command line parameters completed by now;
* any further errors will be runtime ones
* Sanity checks for command line parameters completed by now; any further
* errors will be runtime ones
*/
if (cli_errors.head != NULL)
{
@@ -909,8 +926,8 @@ main(int argc, char **argv)
}
/*
* Print any warnings about inappropriate command line options,
* unless -t/--terse set
* Print any warnings about inappropriate command line options, unless
* -t/--terse set
*/
if (cli_warnings.head != NULL && runtime_options.terse == false)
{
@@ -918,8 +935,10 @@ main(int argc, char **argv)
print_item_list(&cli_warnings);
}
/* post-processing following command line parameter checks
* ======================================================= */
/*
* post-processing following command line parameter checks
* =======================================================
*/
if (runtime_options.csv == true)
{
@@ -953,18 +972,27 @@ main(int argc, char **argv)
}
/* Check for configuration file items which can be overriden by runtime options */
/* ============================================================================ */
/*
* Check for configuration file items which can be overriden by runtime
* options
*/
/* Command-line parameter -L/--log-level overrides any setting in config file*/
/*
* ============================================================================
*/
/*
* Command-line parameter -L/--log-level overrides any setting in config
* file
*/
if (*runtime_options.log_level != '\0')
{
strncpy(config_file_options.log_level, runtime_options.log_level, MAXLEN);
}
/*
* Initialise pg_bindir - command line parameter will override
* any setting in the configuration file
* Initialise pg_bindir - command line parameter will override any setting
* in the configuration file
*/
if (!strlen(runtime_options.pg_bindir))
{
@@ -975,6 +1003,7 @@ main(int argc, char **argv)
if (strlen(runtime_options.pg_bindir))
{
int len = strlen(runtime_options.pg_bindir);
if (runtime_options.pg_bindir[len - 1] != '/')
{
maxlen_snprintf(pg_bindir, "%s/", runtime_options.pg_bindir);
@@ -987,11 +1016,12 @@ main(int argc, char **argv)
/*
* Initialize the logger. We've previously requested STDERR logging only
* to ensure the repmgr command doesn't have its output diverted to a logging
* facility (which usually doesn't make sense for a command line program).
* to ensure the repmgr command doesn't have its output diverted to a
* logging facility (which usually doesn't make sense for a command line
* program).
*
* If required (e.g. when calling repmgr from repmgrd), this behaviour can be
* overridden with "--log-to-file".
* If required (e.g. when calling repmgr from repmgrd), this behaviour can
* be overridden with "--log-to-file".
*/
logger_init(&config_file_options, progname());
@@ -1025,11 +1055,11 @@ main(int argc, char **argv)
* If a node was specified (by --node-id or --node-name), check it exists
* (and pre-populate a record for later use).
*
* At this point check_cli_parameters() will already have determined
* if provision of these is valid for the action, otherwise it unsets them.
* At this point check_cli_parameters() will already have determined if
* provision of these is valid for the action, otherwise it unsets them.
*
* We need to check this much later than other command line parameters
* as we need to wait until the configuration file is parsed and we can
* We need to check this much later than other command line parameters as
* we need to wait until the configuration file is parsed and we can
* obtain the conninfo string.
*/
@@ -1062,6 +1092,7 @@ main(int argc, char **argv)
else if (runtime_options.node_name[0] != '\0')
{
char *escaped = escape_string(conn, runtime_options.node_name);
if (escaped == NULL)
{
log_error(_("unable to escape value provided for --node-name"));
@@ -1196,7 +1227,8 @@ main(int argc, char **argv)
static void
check_cli_parameters(const int action)
{
/* ========================================================================
/*
* ========================================================================
* check all parameters required for an action are provided, and warn
* about ineffective actions
* ========================================================================
@@ -1244,7 +1276,11 @@ check_cli_parameters(const int action)
action_name(action));
}
// XXX if -D/--pgdata provided, and also config_file_options.pgdaga, warn -D/--pgdata will be ignored
/*
* XXX if -D/--pgdata provided, and also
* config_file_options.pgdaga, warn -D/--pgdata will be
* ignored
*/
if (*runtime_options.upstream_conninfo)
{
@@ -1267,8 +1303,8 @@ check_cli_parameters(const int action)
case STANDBY_FOLLOW:
{
/*
* if `repmgr standby follow` executed with host params, ensure data
* directory was provided
* if `repmgr standby follow` executed with host params,
* ensure data directory was provided
*/
}
break;
@@ -1306,7 +1342,8 @@ check_cli_parameters(const int action)
}
/* ========================================================================
/*
* ========================================================================
* warn if parameters provided for an action where they're not relevant
* ========================================================================
*/
@@ -1948,12 +1985,13 @@ int
test_ssh_connection(char *host, char *remote_user)
{
char script[MAXLEN] = "";
int r = 1, i;
int r = 1,
i;
/* On some OS, true is located in a different place than in Linux
* we have to try them all until all alternatives are gone or we
* found `true' because the target OS may differ from the source
* OS
/*
* On some OS, true is located in a different place than in Linux we have
* to try them all until all alternatives are gone or we found `true'
* because the target OS may differ from the source OS
*/
const char *bin_true_paths[] = {
"/bin/true",
@@ -2142,12 +2180,12 @@ copy_remote_files(char *host, char *remote_user, char *remote_path,
}
/*
* When copying the main PGDATA directory, certain files and contents
* of certain directories need to be excluded.
* When copying the main PGDATA directory, certain files and contents of
* certain directories need to be excluded.
*
* See function 'sendDir()' in 'src/backend/replication/basebackup.c' -
* we're basically simulating what pg_basebackup does, but with rsync rather
* than the BASEBACKUP replication protocol command.
* we're basically simulating what pg_basebackup does, but with rsync
* rather than the BASEBACKUP replication protocol command.
*
* *However* currently we'll always copy the contents of the 'pg_replslot'
* directory and delete later if appropriate.
@@ -2268,13 +2306,15 @@ create_recovery_file(t_node_info *node_record, t_conninfo_param_list *recovery_c
if (strlen(runtime_options.upstream_conninfo))
{
char *escaped = escape_recovery_conf_value(runtime_options.upstream_conninfo);
maxlen_snprintf(line, "primary_conninfo = '%s'\n",
escaped);
free(escaped);
}
/*
* otherwise use the conninfo inferred from the upstream connection
* and/or node record
* otherwise use the conninfo inferred from the upstream connection and/or
* node record
*/
else
{
@@ -2320,7 +2360,10 @@ create_recovery_file(t_node_info *node_record, t_conninfo_param_list *recovery_c
log_debug("recovery.conf: %s", line);
}
/* If restore_command is set, we use it as restore_command in recovery.conf */
/*
* If restore_command is set, we use it as restore_command in
* recovery.conf
*/
if (strcmp(config_file_options.restore_command, "") != 0)
{
maxlen_snprintf(line, "restore_command = '%s'\n",
@@ -2750,8 +2793,8 @@ void
get_node_data_directory(char *data_dir_buf)
{
/*
* the configuration file setting has priority, and will always be
* set when a configuration file was provided
* the configuration file setting has priority, and will always be set
* when a configuration file was provided
*/
if (config_file_options.data_directory[0] != '\0')
{

View File

@@ -49,7 +49,8 @@
PG_MODULE_MAGIC;
typedef enum {
typedef enum
{
LEADER_NODE,
FOLLOWER_NODE,
CANDIDATE_NODE
@@ -81,40 +82,52 @@ void _PG_fini(void);
static void repmgr_shmem_startup(void);
Datum set_local_node_id(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(set_local_node_id);
Datum standby_set_last_updated(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(standby_set_last_updated);
Datum standby_get_last_updated(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(standby_get_last_updated);
Datum request_vote(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(request_vote);
Datum get_voting_status(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(get_voting_status);
Datum set_voting_status_initiated(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(set_voting_status_initiated);
Datum other_node_is_candidate(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(other_node_is_candidate);
Datum notify_follow_primary(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(notify_follow_primary);
Datum get_new_primary(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(get_new_primary);
Datum reset_voting_status(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(reset_voting_status);
Datum am_bdr_failover_handler(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(am_bdr_failover_handler);
Datum unset_bdr_failover_handler(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(unset_bdr_failover_handler);
@@ -331,7 +344,7 @@ request_vote(PG_FUNCTION_ARGS)
LWLockRelease(shared_state->lock);
// should we free "query" here?
/* should we free "query" here? */
SPI_finish();
PG_RETURN_LSN(our_lsn);

View File

@@ -65,8 +65,7 @@ monitor_bdr(void)
}
/*
* Verify that database is a BDR one
* TODO: check if supported BDR version?
* Verify that database is a BDR one TODO: check if supported BDR version?
*/
log_info(_("connected to database, checking for BDR"));
@@ -81,7 +80,10 @@ monitor_bdr(void)
log_error(_("repmgr metadata table 'repmgr.%s' is not in the 'repmgr' replication set"),
"nodes");
/* TODO: add `repmgr bdr sync` or similar for this situation, and hint here */
/*
* TODO: add `repmgr bdr sync` or similar for this situation, and hint
* here
*/
exit(ERR_BAD_CONFIG);
}
@@ -100,8 +102,8 @@ monitor_bdr(void)
record_status = get_node_record(local_conn, config_file_options.node_id, &local_node_info);
/*
* Terminate if we can't find the local node record. This is a "fix-the-config"
* situation, not a lot else we can do.
* Terminate if we can't find the local node record. This is a
* "fix-the-config" situation, not a lot else we can do.
*/
if (record_status != RECORD_FOUND)
{
@@ -139,7 +141,8 @@ monitor_bdr(void)
NULL);
/*
* retrieve list of all nodes - we'll need these if the DB connection goes away
* retrieve list of all nodes - we'll need these if the DB connection goes
* away
*/
get_all_node_records(local_conn, &nodes);
@@ -191,6 +194,7 @@ monitor_bdr(void)
if (cell->node_info->node_status == NODE_STATUS_UP)
{
instr_time node_unreachable_start;
INSTR_TIME_SET_CURRENT(node_unreachable_start);
cell->node_info->node_status = NODE_STATUS_DOWN;
@@ -282,8 +286,7 @@ monitor_bdr(void)
if (got_SIGHUP)
{
/*
* if we can reload, then could need to change
* local_conn
* if we can reload, then could need to change local_conn
*/
if (reload_config(&config_file_options))
{
@@ -372,7 +375,10 @@ do_bdr_failover(NodeInfoList *nodes, t_node_info *monitored_node)
{
log_debug("do_bdr_failover() %s", cell->node_info->node_name);
/* don't attempt to connect to the current monitored node, as that's the one which has failed */
/*
* don't attempt to connect to the current monitored node, as that's
* the one which has failed
*/
if (cell->node_info->node_id == monitored_node->node_id)
continue;
@@ -459,11 +465,11 @@ do_bdr_failover(NodeInfoList *nodes, t_node_info *monitored_node)
/*
* Create an event record
*
* If we were able to connect to another node, we'll update the
* event log there.
* If we were able to connect to another node, we'll update the event log
* there.
*
* In any case the event notification command will be triggered
* with the event "bdr_failover"
* In any case the event notification command will be triggered with the
* event "bdr_failover"
*/
create_event_notification_extended(

View File

@@ -24,7 +24,8 @@
#include "repmgrd-physical.h"
typedef enum {
typedef enum
{
FAILOVER_STATE_UNKNOWN = -1,
FAILOVER_STATE_NONE,
FAILOVER_STATE_PROMOTED,
@@ -41,7 +42,8 @@ typedef enum {
} FailoverState;
typedef enum {
typedef enum
{
ELECTION_NOT_CANDIDATE = -1,
ELECTION_WON,
ELECTION_LOST,
@@ -91,14 +93,14 @@ do_physical_node_check(void)
{
#ifndef BDR_ONLY
/*
* Check if node record is active - if not, and `failover=automatic`, the node
* won't be considered as a promotion candidate; this often happens when
* a failed primary is recloned and the node was not re-registered, giving
* the impression failover capability is there when it's not. In this case
* abort with an error and a hint about registering.
* Check if node record is active - if not, and `failover=automatic`, the
* node won't be considered as a promotion candidate; this often happens
* when a failed primary is recloned and the node was not re-registered,
* giving the impression failover capability is there when it's not. In
* this case abort with an error and a hint about registering.
*
* If `failover=manual`, repmgrd can continue to passively monitor the node, but
* we should nevertheless issue a warning and the same hint.
* If `failover=manual`, repmgrd can continue to passively monitor the
* node, but we should nevertheless issue a warning and the same hint.
*/
if (local_node_info.active == false)
@@ -138,8 +140,8 @@ do_physical_node_check(void)
if (config_file_options.service_promote_command[0] != '\0')
{
/*
* if repmgrd executes "service_promote_command" directly, repmgr metadata
* won't get updated
* if repmgrd executes "service_promote_command" directly,
* repmgr metadata won't get updated
*/
log_hint(_("\"service_promote_command\" is set, but can only be executed by \"repmgr standby promote\""));
}
@@ -242,8 +244,8 @@ monitor_streaming_primary(void)
PQfinish(local_conn);
/*
* as we're monitoring the primary, no point in trying to write
* the event to the database
* as we're monitoring the primary, no point in trying to
* write the event to the database
*
* XXX possible pre-action event
*/
@@ -346,9 +348,9 @@ monitor_streaming_primary(void)
/*
* possibly attempt to find another node from cached list
* check if there's a new primary - if so add hook for fencing?
* loop, if starts up check status, switch monitoring mode
* possibly attempt to find another node from cached list check if
* there's a new primary - if so add hook for fencing? loop, if
* starts up check status, switch monitoring mode
*/
}
loop:
@@ -421,9 +423,9 @@ monitor_streaming_standby(void)
log_debug("monitor_streaming_standby()");
/*
* If no upstream node id is specified in the metadata, we'll try
* and determine the current cluster primary in the assumption we
* should connect to that by default.
* If no upstream node id is specified in the metadata, we'll try and
* determine the current cluster primary in the assumption we should
* connect to that by default.
*/
if (local_node_info.upstream_node_id == UNKNOWN_NODE_ID)
{
@@ -431,9 +433,9 @@ monitor_streaming_standby(void)
/*
* Terminate if there doesn't appear to be an active cluster primary.
* There could be one or more nodes marked as inactive primaries, and one
* of them could actually be a primary, but we can't sensibly monitor
* in that state.
* There could be one or more nodes marked as inactive primaries, and
* one of them could actually be a primary, but we can't sensibly
* monitor in that state.
*/
if (local_node_info.upstream_node_id == NODE_NOT_FOUND)
{
@@ -446,9 +448,9 @@ monitor_streaming_standby(void)
record_status = get_node_record(local_conn, local_node_info.upstream_node_id, &upstream_node_info);
/*
* Terminate if we can't find the record for the node we're supposed
* to monitor. This is a "fix-the-config" situation, not a lot else we
* can do.
* Terminate if we can't find the record for the node we're supposed to
* monitor. This is a "fix-the-config" situation, not a lot else we can
* do.
*/
if (record_status == RECORD_NOT_FOUND)
{
@@ -473,8 +475,8 @@ monitor_streaming_standby(void)
/*
* Upstream node must be running at repmgrd startup.
*
* We could possibly have repmgrd skip to degraded monitoring mode until it
* comes up, but there doesn't seem to be much point in doing that.
* We could possibly have repmgrd skip to degraded monitoring mode until
* it comes up, but there doesn't seem to be much point in doing that.
*/
if (PQstatus(upstream_conn) != CONNECTION_OK)
{
@@ -486,15 +488,18 @@ monitor_streaming_standby(void)
exit(ERR_DB_CONN);
}
/* refresh upstream node record from upstream node, so it's as up-to-date as possible */
/*
* refresh upstream node record from upstream node, so it's as up-to-date
* as possible
*/
record_status = get_node_record(upstream_conn, upstream_node_info.node_id, &upstream_node_info);
if (upstream_node_info.type == STANDBY)
{
/*
* Currently cascaded standbys need to be able to connect to the primary.
* We could possibly add a limited connection mode for cases where this isn't
* possible.
* Currently cascaded standbys need to be able to connect to the
* primary. We could possibly add a limited connection mode for cases
* where this isn't possible.
*/
primary_conn = establish_primary_db_connection(upstream_conn, false);
@@ -518,6 +523,7 @@ monitor_streaming_standby(void)
if (startup_event_logged == false)
{
PQExpBufferData event_details;
initPQExpBuffer(&event_details);
appendPQExpBuffer(&event_details,
@@ -617,7 +623,10 @@ monitor_streaming_standby(void)
failover_done = do_upstream_standby_failover();
}
/* XXX it's possible it will make sense to return in all cases to restart monitoring */
/*
* XXX it's possible it will make sense to return in all
* cases to restart monitoring
*/
if (failover_done == true)
{
primary_node_id = get_primary_node_id(local_conn);
@@ -641,10 +650,13 @@ monitor_streaming_standby(void)
if (PQstatus(upstream_conn) == CONNECTION_OK)
{
// XXX check here if upstream is still primary
// -> will be a problem if another node was promoted in the meantime
// and upstream is now former primary
// XXX scan other nodes to see if any has become primary
/* XXX check here if upstream is still primary */
/*
* -> will be a problem if another node was promoted in
* the meantime
*/
/* and upstream is now former primary */
/* XXX scan other nodes to see if any has become primary */
upstream_node_info.node_status = NODE_STATUS_UP;
monitoring_state = MS_NORMAL;
@@ -684,8 +696,8 @@ monitor_streaming_standby(void)
else
{
/*
* unable to connect to former primary - check if another node has
* been promoted
* unable to connect to former primary - check if another node
* has been promoted
*/
NodeInfoListCell *cell;
@@ -697,12 +709,13 @@ monitor_streaming_standby(void)
log_notice(_("local node is primary, checking local node record"));
/*
* There may be a delay between the node being promoted and the local
* record being updated, so if the node record still shows it as a
* standby, do nothing, we'll catch the update during the next loop.
* (e.g. node was manually
* promoted) we'll do nothing, as the repmgr metadata is now out-of-sync.
* If it does get fixed, we'll catch it here on a future iteration.
* There may be a delay between the node being promoted
* and the local record being updated, so if the node
* record still shows it as a standby, do nothing, we'll
* catch the update during the next loop. (e.g. node was
* manually promoted) we'll do nothing, as the repmgr
* metadata is now out-of-sync. If it does get fixed,
* we'll catch it here on a future iteration.
*/
/* refresh own internal node record */
@@ -779,6 +792,7 @@ monitor_streaming_standby(void)
if (log_status_interval_elapsed >= config_file_options.log_status_interval)
{
PQExpBufferData monitoring_summary;
initPQExpBuffer(&monitoring_summary);
appendPQExpBuffer(
@@ -827,6 +841,7 @@ monitor_streaming_standby(void)
if (update_node_record_set_active(primary_conn, local_node_info.node_id, false) == true)
{
PQExpBufferData event_details;
initPQExpBuffer(&event_details);
local_node_info.active = false;
@@ -853,7 +868,8 @@ monitor_streaming_standby(void)
}
}
}
else {
else
{
if (local_node_info.active == false)
{
if (PQstatus(primary_conn) == CONNECTION_OK)
@@ -861,6 +877,7 @@ monitor_streaming_standby(void)
if (update_node_record_set_active(primary_conn, local_node_info.node_id, true) == true)
{
PQExpBufferData event_details;
initPQExpBuffer(&event_details);
local_node_info.active = true;
@@ -954,8 +971,8 @@ do_primary_failover(void)
best_candidate = poll_best_candidate(&standby_nodes);
/*
* this can occur in a tie-break situation, where this node establishes
* it is the best candidate
* this can occur in a tie-break situation, where this node
* establishes it is the best candidate
*/
if (best_candidate->node_id == local_node_info.node_id)
{
@@ -1012,8 +1029,8 @@ do_primary_failover(void)
/*
* node has decided it is a follower, so will await notification
* from the candidate that it has promoted itself and can be followed
* node has decided it is a follower, so will await notification from the
* candidate that it has promoted itself and can be followed
*/
if (failover_state == FAILOVER_STATE_WAITING_NEW_PRIMARY)
{
@@ -1060,6 +1077,7 @@ do_primary_failover(void)
else
{
PQExpBufferData event_details;
initPQExpBuffer(&event_details);
appendPQExpBuffer(&event_details,
_("node %i is in manual failover mode and is now disconnected from streaming replication"),
@@ -1072,7 +1090,11 @@ do_primary_failover(void)
&config_file_options,
local_node_info.node_id,
"standby_disconnect_manual",
/* here "true" indicates the action has occurred as expected */
/*
* here "true" indicates the action has occurred as
* expected
*/
true,
event_details.data);
PQfinish(new_primary_conn);
@@ -1112,7 +1134,10 @@ do_primary_failover(void)
case FAILOVER_STATE_PRIMARY_REAPPEARED:
log_debug("failover state is PRIMARY_REAPPEARED");
/* notify siblings that they should resume following the original primary */
/*
* notify siblings that they should resume following the original
* primary
*/
notify_followers(&standby_nodes, upstream_node_info.node_id);
/* we no longer care about our former siblings */
@@ -1151,6 +1176,7 @@ do_primary_failover(void)
return false;
case FAILOVER_STATE_FOLLOW_FAIL:
/*
* for whatever reason we were unable to follow the new primary -
* continue monitoring in degraded state
@@ -1233,7 +1259,10 @@ update_monitoring_history(void)
}
else
{
/* This should never happen, but in case it does set replication lag to zero */
/*
* This should never happen, but in case it does set replication lag
* to zero
*/
log_warning("primary xlog (%X/%X) location appears less than standby receive location (%X/%X)",
format_lsn(primary_last_wal_location),
format_lsn(replication_info.last_wal_receive_lsn));
@@ -1286,9 +1315,10 @@ do_upstream_standby_failover(void)
log_error(_("unable to retrieve primary node record"));
return false;
}
/*
* Verify that we can still talk to the cluster primary, even though
* the node's upstream is not available
* Verify that we can still talk to the cluster primary, even though the
* node's upstream is not available
*/
check_connection(&primary_node_info, &primary_conn);
@@ -1344,9 +1374,9 @@ do_upstream_standby_failover(void)
log_error("%s", event_details.data);
/* It may not possible to write to the event notification
* table but we should be able to generate an external notification
* if required.
/*
* It may not possible to write to the event notification table but we
* should be able to generate an external notification if required.
*/
create_event_notification(
primary_conn,
@@ -1390,8 +1420,9 @@ do_upstream_standby_failover(void)
record_status = get_node_record(primary_conn, local_node_info.node_id, &local_node_info);
/*
* highly improbable this will happen, but in case we're unable to retrieve
* our node record from the primary, update it ourselves, and hope for the best
* highly improbable this will happen, but in case we're unable to
* retrieve our node record from the primary, update it ourselves, and
* hope for the best
*/
if (record_status != RECORD_FOUND)
{
@@ -1434,8 +1465,8 @@ promote_self(void)
/*
* optionally add a delay before promoting the standby; this is mainly
* useful for testing (e.g. for reappearance of the original primary)
* and is not documented.
* useful for testing (e.g. for reappearance of the original primary) and
* is not documented.
*/
if (config_file_options.promote_delay > 0)
{
@@ -1475,7 +1506,7 @@ promote_self(void)
if (PQstatus(local_conn) != CONNECTION_OK)
{
log_error(_("unable to reconnect to local node"));
// XXX handle this
/* XXX handle this */
return FAILOVER_STATE_LOCAL_NODE_FAILURE;
}
}
@@ -1507,10 +1538,10 @@ promote_self(void)
termPQExpBuffer(&event_details);
//primary_conn = NULL;
/* primary_conn = NULL; */
// XXX handle this!
// -> we'll need to let the other nodes know too....
/* XXX handle this! */
/* -> we'll need to let the other nodes know too.... */
/* no failover occurred but we'll want to restart connections */
return FAILOVER_STATE_PRIMARY_REAPPEARED;
@@ -1610,8 +1641,7 @@ poll_best_candidate(NodeInfoList *standby_nodes)
* cases we could end up with two candidate nodes, so they should each
* come to the same conclusion.
*
* XXX check there are no cases where the standby node's LSN is
* not set
* XXX check there are no cases where the standby node's LSN is not set
*/
for (cell = standby_nodes->head; cell; cell = cell->next)
{
@@ -1709,7 +1739,7 @@ follow_new_primary(int new_primary_id)
return FAILOVER_STATE_FOLLOW_FAIL;
}
// XXX check if new_primary_id == failed_primary.node_id?
/* XXX check if new_primary_id == failed_primary.node_id? */
if (log_type == REPMGR_STDERR && *config_file_options.log_file)
{
@@ -1721,6 +1751,7 @@ follow_new_primary(int new_primary_id)
if (PQstatus(upstream_conn) == CONNECTION_OK)
{
RecoveryType primary_recovery_type = get_recovery_type(upstream_conn);
if (primary_recovery_type == RECTYPE_PRIMARY)
{
new_primary_ok = true;
@@ -1739,8 +1770,8 @@ follow_new_primary(int new_primary_id)
}
/*
* disconnect from local node, as follow operation will result in
* a server restart
* disconnect from local node, as follow operation will result in a server
* restart
*/
PQfinish(local_conn);
@@ -1762,24 +1793,27 @@ follow_new_primary(int new_primary_id)
if (r != 0)
{
PGconn *old_primary_conn;
/*
* The follow action could still fail due to the original primary reappearing
* before the candidate could promote itself ("repmgr standby follow" will
* refuse to promote another node if the primary is available). However
* the new primary will only instruct use to follow it after it's successfully
* promoted itself, so that very likely won't be the reason for the failure.
* The follow action could still fail due to the original primary
* reappearing before the candidate could promote itself ("repmgr
* standby follow" will refuse to promote another node if the primary
* is available). However the new primary will only instruct use to
* follow it after it's successfully promoted itself, so that very
* likely won't be the reason for the failure.
*
*
* TODO: check the new primary too - we could have a split-brain
* situation where the old primary reappeared just after the new
* one promoted itself.
* situation where the old primary reappeared just after the new one
* promoted itself.
*/
old_primary_conn = establish_db_connection(failed_primary.conninfo, false);
if (PQstatus(old_primary_conn) == CONNECTION_OK)
{
// XXX add event notifications
/* XXX add event notifications */
RecoveryType upstream_recovery_type = get_recovery_type(old_primary_conn);
PQfinish(old_primary_conn);
if (upstream_recovery_type == RECTYPE_PRIMARY)
@@ -1898,8 +1932,8 @@ do_election(void)
int visible_nodes = 1;
/*
* get voting status from shared memory - should be one of "VS_NO_VOTE"
* or "VS_VOTE_REQUEST_RECEIVED". If VS_NO_VOTE, we declare ourselves as
* get voting status from shared memory - should be one of "VS_NO_VOTE" or
* "VS_VOTE_REQUEST_RECEIVED". If VS_NO_VOTE, we declare ourselves as
* candidate and initiate the voting process.
*/
NodeVotingStatus voting_status;
@@ -1910,13 +1944,13 @@ do_election(void)
bool other_node_is_ahead = false;
/*
* Check if at least one server in the primary's location is visible;
* if not we'll assume a network split between this node and the primary
* Check if at least one server in the primary's location is visible; if
* not we'll assume a network split between this node and the primary
* location, and not promote any standby.
*
* NOTE: this function is only ever called by standbys attached to the current
* (unreachable) primary, so "upstream_node_info" will always contain the
* primary node record.
* NOTE: this function is only ever called by standbys attached to the
* current (unreachable) primary, so "upstream_node_info" will always
* contain the primary node record.
*/
bool primary_location_seen = false;
@@ -1968,10 +2002,10 @@ do_election(void)
}
/*
* Here we mark ourselves as candidate, so any further vote requests
* are rejected. However it's possible another node has done the
* same thing, so when announcing ourselves as candidate to the other
* nodes, we'll check for that and withdraw our candidature.
* Here we mark ourselves as candidate, so any further vote requests are
* rejected. However it's possible another node has done the same thing,
* so when announcing ourselves as candidate to the other nodes, we'll
* check for that and withdraw our candidature.
*/
electoral_term = set_voting_status_initiated(local_conn);
@@ -2007,15 +2041,17 @@ do_election(void)
}
/*
* tell the other node we're candidate - if the node has already declared
* itself, we withdraw
* tell the other node we're candidate - if the node has already
* declared itself, we withdraw
*
* XXX check for situations where more than one node could end up as candidate?
* XXX check for situations where more than one node could end up as
* candidate?
*
* XXX note it's possible some nodes accepted our candidature before we
* found out about the other candidate, check what happens in that situation
* -> other node will have info from all the nodes, even if not the vote,
* so it should be able to determine the best node anyway
* XXX note it's possible some nodes accepted our candidature before
* we found out about the other candidate, check what happens in that
* situation -> other node will have info from all the nodes, even if
* not the vote, so it should be able to determine the best node
* anyway
*/
if (announce_candidature(cell->node_info->conn, &local_node_info, cell->node_info, electoral_term) == false)
@@ -2028,8 +2064,8 @@ do_election(void)
}
/*
* see if the node is in the primary's location (but skip the check
* if we've seen
* see if the node is in the primary's location (but skip the check if
* we've seen
*/
if (primary_location_seen == false)
{

View File

@@ -154,9 +154,11 @@ main(int argc, char **argv)
exit(SUCCESS);
case 'V':
/*
* in contrast to repmgr3 and earlier, we only display the repmgr version
* as it's not specific to a particular PostgreSQL version
* in contrast to repmgr3 and earlier, we only display the
* repmgr version as it's not specific to a particular
* PostgreSQL version
*/
printf("%s %s\n", progname(), REPMGR_VERSION);
exit(SUCCESS);
@@ -183,6 +185,7 @@ main(int argc, char **argv)
case 'L':
{
int detected_cli_log_level = detect_log_level(optarg);
if (detected_cli_log_level != -1)
{
strncpy(cli_log_level, optarg, MAXLEN);
@@ -190,6 +193,7 @@ main(int argc, char **argv)
else
{
PQExpBufferData invalid_log_level;
initPQExpBuffer(&invalid_log_level);
appendPQExpBuffer(&invalid_log_level,
_("invalid log level \"%s\" provided"),
@@ -223,6 +227,7 @@ main(int argc, char **argv)
}
startup_event_logged = false;
/*
* Tell the logger we're a daemon - this will ensure any output logged
* before the logger is initialized will be formatted correctly
@@ -230,15 +235,19 @@ main(int argc, char **argv)
logger_output_mode = OM_DAEMON;
/*
* Parse the configuration file, if provided. If no configuration file
* was provided, or one was but was incomplete, parse_config() will
* abort anyway, with an appropriate message.
* Parse the configuration file, if provided. If no configuration file was
* provided, or one was but was incomplete, parse_config() will abort
* anyway, with an appropriate message.
*/
load_config(config_file, verbose, false, &config_file_options, argv[0]);
/* Some configuration file items can be overriden by command line options */
/* Command-line parameter -L/--log-level overrides any setting in config file*/
/*
* Command-line parameter -L/--log-level overrides any setting in config
* file
*/
if (*cli_log_level != '\0')
{
strncpy(config_file_options.log_level, cli_log_level, MAXLEN);
@@ -304,8 +313,8 @@ main(int argc, char **argv)
* point, but we'll skip that and assume the presence of a node record
* means we're dealing with a supported installation.
*
* The absence of a node record will also indicate that either the node
* or repmgr has not been properly configured.
* The absence of a node record will also indicate that either the node or
* repmgr has not been properly configured.
*/
/* Retrieve record for this node from the local database */
@@ -400,11 +409,13 @@ update_registration(PGconn *conn)
{
bool success = update_node_record_conn_priority(local_conn,
&config_file_options);
// check values have actually changed
/* check values have actually changed */
if (success == false)
{
PQExpBufferData errmsg;
initPQExpBuffer(&errmsg);
appendPQExpBuffer(&errmsg,
@@ -635,10 +646,9 @@ try_reconnect(t_node_info *node_info)
log_notice(_("node has recovered, reconnecting"));
/*
* XXX we should also handle the case where node is pingable
* but connection denied due to connection exhaustion
* - fall back to degraded monitoring?
* - make that configurable
* XXX we should also handle the case where node is pingable but
* connection denied due to connection exhaustion - fall back to
* degraded monitoring? - make that configurable
*/
conn = establish_db_connection(node_info->conninfo, false);
if (PQstatus(conn) == CONNECTION_OK)
@@ -730,5 +740,3 @@ terminate(int retval)
exit(retval);
}

View File

@@ -33,14 +33,16 @@
#define MAXLEN_STR STR(MAXLEN)
typedef enum {
typedef enum
{
CHECK_STATUS_OK = 0,
CHECK_STATUS_WARNING,
CHECK_STATUS_CRITICAL,
CHECK_STATUS_UNKNOWN
} CheckStatus;
typedef enum {
typedef enum
{
OM_NOT_SET = -1,
OM_TEXT,
OM_CSV,
@@ -119,8 +121,7 @@ __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
extern void
key_value_list_set_output_mode(KeyValueList *item_list, const char *key, OutputMode mode);
extern const char *
key_value_list_get(KeyValueList *item_list, const char *key);
extern const char *key_value_list_get(KeyValueList *item_list, const char *key);
extern void
key_value_list_free(KeyValueList *item_list);
@@ -137,18 +138,15 @@ check_status_list_free(CheckStatusList *list);
extern const char *output_check_status(CheckStatus status);
extern char *
escape_recovery_conf_value(const char *src);
extern char *escape_recovery_conf_value(const char *src);
extern char *
escape_string(PGconn *conn, const char *string);
extern char *escape_string(PGconn *conn, const char *string);
extern void
append_where_clause(PQExpBufferData *where_clause, const char *clause,...)
__attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
extern char *
string_skip_prefix(const char *prefix, char *string);
extern char *string_skip_prefix(const char *prefix, char *string);
extern char
*string_remove_trailing_newlines(char *string);

View File

@@ -18,7 +18,8 @@
#ifndef _VOTING_H_
#define _VOTING_H_
typedef enum {
typedef enum
{
VS_UNKNOWN = -1,
VS_NO_VOTE,
VS_VOTE_REQUEST_RECEIVED,