diff --git a/dbutils.c b/dbutils.c index c7ecef9e..9525f882 100644 --- a/dbutils.c +++ b/dbutils.c @@ -3647,102 +3647,6 @@ get_voting_status(PGconn *conn) } -VoteRequestResult -request_vote(PGconn *conn, t_node_info *this_node, t_node_info *other_node, int electoral_term) -{ - PQExpBufferData query; - PGresult *res = NULL; - int lsn_diff = 0; - - other_node->last_wal_receive_lsn = InvalidXLogRecPtr; - - initPQExpBuffer(&query); - - appendPQExpBuffer(&query, - "SELECT repmgr.request_vote(%i, %i)", - this_node->node_id, - electoral_term); - - res = PQexec(conn, query.data); - termPQExpBuffer(&query); - - /* check for NULL */ - if (PQgetisnull(res, 0, 0)) - { - PQclear(res); - - log_debug("NULL returned by repmgr.request_vote()"); - - /* - * 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, - "SELECT pg_catalog.pg_last_wal_receive_lsn()"); - } - else - { - appendPQExpBuffer(&query, - "SELECT pg_catalog.pg_last_xlog_receive_location()"); - } - - res = PQexec(conn, query.data); - termPQExpBuffer(&query); - - if (PQresultStatus(res) == PGRES_TUPLES_OK) - { - other_node->last_wal_receive_lsn = parse_lsn(PQgetvalue(res, 0, 0)); - } - - PQclear(res); - - return VR_VOTE_REFUSED; - } - - other_node->last_wal_receive_lsn = parse_lsn(PQgetvalue(res, 0, 0)); - - PQclear(res); - - lsn_diff = this_node->last_wal_receive_lsn - other_node->last_wal_receive_lsn; - - log_debug("lsn_diff %i", lsn_diff); - - /* we're ahead */ - if (lsn_diff > 0) - { - log_debug("local node is ahead"); - return VR_POSITIVE_VOTE; - } - - - /* other node is ahead */ - if (lsn_diff < 0) - { - log_debug("other node is ahead"); - return VR_NEGATIVE_VOTE; - } - - /* tiebreak */ - - /* other node is higher priority */ - if (this_node->priority < other_node->priority) - { - log_debug("other node has higher priority"); - return VR_NEGATIVE_VOTE; - } - - /* still tiebreak - we're the candidate, so we win */ - log_debug("win by default"); - return VR_POSITIVE_VOTE; - -} - - void set_voting_status_initiated(PGconn *conn, int electoral_term) { diff --git a/dbutils.h b/dbutils.h index c4dc9843..8c78c05a 100644 --- a/dbutils.h +++ b/dbutils.h @@ -77,14 +77,6 @@ typedef enum NODE_STATUS_UNCLEAN_SHUTDOWN } NodeStatus; -typedef enum -{ - VR_VOTE_REFUSED = -1, - VR_POSITIVE_VOTE, - VR_NEGATIVE_VOTE -} VoteRequestResult; - - typedef enum { SLOT_UNKNOWN = -1, @@ -477,7 +469,6 @@ bool delete_monitoring_records(PGconn *primary_conn, int keep_history); int get_current_term(PGconn *conn); void increment_current_term(PGconn *conn); NodeVotingStatus get_voting_status(PGconn *conn); -VoteRequestResult request_vote(PGconn *conn, t_node_info *this_node, t_node_info *other_node, int electoral_term); void set_voting_status_initiated(PGconn *conn, int electoral_term); bool announce_candidature(PGconn *conn, t_node_info *this_node, t_node_info *other_node, int electoral_term); void notify_follow_primary(PGconn *conn, int primary_node_id); diff --git a/repmgr--4.0.sql b/repmgr--4.0.sql index c9811f70..83f907b1 100644 --- a/repmgr--4.0.sql +++ b/repmgr--4.0.sql @@ -118,49 +118,11 @@ CREATE FUNCTION standby_get_last_updated() /* failover functions */ - -DO $repmgr$ -DECLARE - DECLARE server_version_num INT; -BEGIN - SELECT setting - FROM pg_catalog.pg_settings - WHERE name = 'server_version_num' - INTO server_version_num; - - IF server_version_num >= 90400 THEN - EXECUTE $repmgr_func$ -CREATE FUNCTION request_vote(INT,INT) - RETURNS pg_lsn - AS 'MODULE_PATHNAME', 'request_vote' - LANGUAGE C STRICT; - $repmgr_func$; - ELSE - EXECUTE $repmgr_func$ -CREATE FUNCTION request_vote(INT,INT) - RETURNS TEXT - AS 'MODULE_PATHNAME', 'request_vote' - LANGUAGE C STRICT; - $repmgr_func$; - END IF; -END$repmgr$; - - -CREATE FUNCTION get_voting_status() - RETURNS INT - AS 'MODULE_PATHNAME', 'get_voting_status' - LANGUAGE C STRICT; - CREATE FUNCTION set_voting_status_initiated(INT) RETURNS VOID AS 'MODULE_PATHNAME', 'set_voting_status_initiated' LANGUAGE C STRICT; -CREATE FUNCTION other_node_is_candidate(INT, INT) - RETURNS BOOL - AS 'MODULE_PATHNAME', 'other_node_is_candidate' - LANGUAGE C STRICT; - CREATE FUNCTION notify_follow_primary(INT) RETURNS VOID AS 'MODULE_PATHNAME', 'notify_follow_primary' diff --git a/repmgr--unpackaged--4.0.sql b/repmgr--unpackaged--4.0.sql index e819f733..7215e1f9 100644 --- a/repmgr--unpackaged--4.0.sql +++ b/repmgr--unpackaged--4.0.sql @@ -108,26 +108,11 @@ CREATE FUNCTION standby_get_last_updated() /* failover functions */ -CREATE FUNCTION request_vote(INT,INT) - RETURNS pg_lsn - AS '$libdir/repmgr', 'request_vote' - LANGUAGE C STRICT; - -CREATE FUNCTION get_voting_status() - RETURNS INT - AS '$libdir/repmgr', 'get_voting_status' - LANGUAGE C STRICT; - CREATE FUNCTION set_voting_status_initiated() RETURNS INT AS '$libdir/repmgr', 'set_voting_status_initiated' LANGUAGE C STRICT; -CREATE FUNCTION other_node_is_candidate(INT, INT) - RETURNS BOOL - AS '$libdir/repmgr', 'other_node_is_candidate' - LANGUAGE C STRICT; - CREATE FUNCTION notify_follow_primary(INT) RETURNS VOID AS '$libdir/repmgr', 'notify_follow_primary' diff --git a/repmgr.c b/repmgr.c index bd25c520..347148d5 100644 --- a/repmgr.c +++ b/repmgr.c @@ -67,7 +67,6 @@ typedef struct repmgrdSharedState TimestampTz last_updated; int local_node_id; /* streaming failover */ - NodeState node_state; NodeVotingStatus voting_status; int current_electoral_term; int candidate_node_id; @@ -98,23 +97,10 @@ 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); @@ -291,141 +277,6 @@ standby_get_last_updated(PG_FUNCTION_ARGS) /* failover functions */ /* ===================*/ -Datum -request_vote(PG_FUNCTION_ARGS) -{ -#ifndef BDR_ONLY - StringInfoData query; - -#if (PG_VERSION_NUM >= 90400) - XLogRecPtr our_lsn = InvalidXLogRecPtr; - bool isnull; -#else - char *value = NULL; - char lsn_text[64] = ""; -#endif - - /* node_id used for logging purposes */ - int requesting_node_id = UNKNOWN_NODE_ID; - int current_electoral_term = UNKNOWN_NODE_ID; - - int ret; - - if (!shared_state) - PG_RETURN_NULL(); - - if (PG_ARGISNULL(0) || PG_ARGISNULL(1)) - PG_RETURN_NULL(); - - requesting_node_id = PG_GETARG_INT32(0); - current_electoral_term = PG_GETARG_INT32(1); - - LWLockAcquire(shared_state->lock, LW_SHARED); - - /* only do something if local_node_id is initialised */ - if (shared_state->local_node_id != UNKNOWN_NODE_ID) - { - /* this node has initiated voting or already responded to another node */ - if (shared_state->voting_status != VS_NO_VOTE) - { - LWLockRelease(shared_state->lock); - - PG_RETURN_NULL(); - } - - elog(INFO, "node %i has received request from node %i for electoral term %i (our term: %i)", - shared_state->local_node_id, - requesting_node_id, current_electoral_term, - shared_state->current_electoral_term); - - SPI_connect(); - - initStringInfo(&query); - - appendStringInfo( - &query, -#if (PG_VERSION_NUM >= 100000) - "SELECT pg_catalog.pg_last_wal_receive_lsn()"); -#else - "SELECT pg_catalog.pg_last_xlog_receive_location()"); -#endif - - elog(DEBUG1, "query: %s", query.data); - ret = SPI_execute(query.data, true, 0); - - if (ret < 0) - { - SPI_finish(); - elog(WARNING, "unable to retrieve last received LSN"); - LWLockRelease(shared_state->lock); - -#if (PG_VERSION_NUM >= 90400) - PG_RETURN_LSN(InvalidOid); -#else - PG_RETURN_TEXT_P(cstring_to_text("0/0")); -#endif - } - -#if (PG_VERSION_NUM >= 90400) - our_lsn = DatumGetLSN(SPI_getbinval(SPI_tuptable->vals[0], - SPI_tuptable->tupdesc, - 1, &isnull)); - - elog(DEBUG1, "our LSN is %X/%X", - (uint32) (our_lsn >> 32), - (uint32) our_lsn); -#else - value = SPI_getvalue(SPI_tuptable->vals[0], - SPI_tuptable->tupdesc, - 1); - strncpy(lsn_text, value, 64); - pfree(value); - elog(DEBUG1, "our LSN is %s", lsn_text); -#endif - - LWLockRelease(shared_state->lock); - LWLockAcquire(shared_state->lock, LW_EXCLUSIVE); - - /* indicate this node has responded to a vote request */ - shared_state->voting_status = VS_VOTE_REQUEST_RECEIVED; - shared_state->current_electoral_term = current_electoral_term; - - /* should we free "query" here? */ - SPI_finish(); - } - - LWLockRelease(shared_state->lock); - -#if (PG_VERSION_NUM >= 90400) - PG_RETURN_LSN(our_lsn); -#else - PG_RETURN_TEXT_P(cstring_to_text(lsn_text)); -#endif -#else - PG_RETURN(InvalidOid); -#endif -} - - - -Datum -get_voting_status(PG_FUNCTION_ARGS) -{ -#ifndef BDR_ONLY - NodeVotingStatus voting_status; - - if (!shared_state) - PG_RETURN_NULL(); - - LWLockAcquire(shared_state->lock, LW_SHARED); - voting_status = shared_state->voting_status; - LWLockRelease(shared_state->lock); - - PG_RETURN_INT32(voting_status); -#else - PG_RETURN_INT32(VS_UNKNOWN); -#endif -} Datum @@ -463,53 +314,6 @@ set_voting_status_initiated(PG_FUNCTION_ARGS) } -Datum -other_node_is_candidate(PG_FUNCTION_ARGS) -{ -#ifndef BDR_ONLY - - int requesting_node_id = UNKNOWN_NODE_ID; - int electoral_term = UNKNOWN_NODE_ID; - - if (!shared_state) - PG_RETURN_NULL(); - - if (PG_ARGISNULL(0) || PG_ARGISNULL(1)) - PG_RETURN_NULL(); - - requesting_node_id = PG_GETARG_INT32(0); - electoral_term = PG_GETARG_INT32(1); - - LWLockAcquire(shared_state->lock, LW_SHARED); - - /* only do something if local_node_id is initialised */ - if (shared_state->local_node_id != UNKNOWN_NODE_ID) - { - if (shared_state->current_electoral_term == electoral_term) - { - if (shared_state->candidate_node_id != UNKNOWN_NODE_ID) - { - elog(INFO, "node %i requesting candidature, but node %i already candidate", - requesting_node_id, - shared_state->candidate_node_id); - PG_RETURN_BOOL(false); - } - } - - LWLockRelease(shared_state->lock); - LWLockAcquire(shared_state->lock, LW_EXCLUSIVE); - shared_state->candidate_node_id = requesting_node_id; - elog(INFO, "node %i is candidate", requesting_node_id); - } - - LWLockRelease(shared_state->lock); - - PG_RETURN_BOOL(true); -#else - PG_RETURN_BOOL(false); -#endif -} - Datum notify_follow_primary(PG_FUNCTION_ARGS) {