From f4e8bf891ddf7de41283e3706e3fcd66e5127eed Mon Sep 17 00:00:00 2001 From: Ian Barwick Date: Wed, 28 Jun 2017 17:28:20 +0900 Subject: [PATCH] interim commit --- dbutils.c | 11 ++++++----- repmgr--4.0.sql | 16 ++++++++++------ repmgr.c | 45 ++++++++++++++++++++++++++++++++++++++++++--- repmgrd.c | 9 +++++++-- 4 files changed, 65 insertions(+), 16 deletions(-) diff --git a/dbutils.c b/dbutils.c index da02c1b9..a83cb993 100644 --- a/dbutils.c +++ b/dbutils.c @@ -2292,14 +2292,13 @@ int request_vote(PGconn *conn, int this_node_id, int this_node_priority, XLogRec { PQExpBufferData query; PGresult *res; - int vote; + int lsn_diff; initPQExpBuffer(&query); appendPQExpBuffer(&query, - "SELECT repmgr.request_vote(%i, %i, '%X/%X'::pg_lsn)", + "SELECT repmgr.request_vote(%i, '%X/%X'::pg_lsn)", this_node_id, - this_node_priority, (uint32) (last_wal_receive_lsn >> 32), (uint32) last_wal_receive_lsn); @@ -2307,10 +2306,12 @@ int request_vote(PGconn *conn, int this_node_id, int this_node_priority, XLogRec termPQExpBuffer(&query); - vote = (strcmp(PQgetvalue(res, 0, 0), "t") == 0) ? 1 : 0; + lsn_diff = atoi(PQgetvalue(res, 0, 0)); + + log_debug("XXX lsn_diff %i", lsn_diff); PQclear(res); - return vote; + return lsn_diff; } diff --git a/repmgr--4.0.sql b/repmgr--4.0.sql index ac57e98f..56937266 100644 --- a/repmgr--4.0.sql +++ b/repmgr--4.0.sql @@ -34,11 +34,15 @@ CREATE VIEW show_nodes AS LEFT JOIN nodes un ON un.node_id = n.upstream_node_id; -CREATE FUNCTION request_vote(int) RETURNS boolean -AS '$libdir/repmgr', 'request_vote' -LANGUAGE C STRICT; +/* repmgrd functions */ + +CREATE FUNCTION request_vote(INT, pg_lsn) + RETURNS INT + 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 get_voting_status() + RETURNS INT + AS '$libdir/repmgr', 'get_voting_status' + LANGUAGE C STRICT; diff --git a/repmgr.c b/repmgr.c index 97c77f78..cc712665 100644 --- a/repmgr.c +++ b/repmgr.c @@ -18,8 +18,16 @@ #include "storage/shmem.h" #include "storage/spin.h" #include "utils/builtins.h" +#include "utils/pg_lsn.h" #include "utils/timestamp.h" +#include "executor/spi.h" +#include "lib/stringinfo.h" +#include "access/xact.h" +#include "utils/snapmgr.h" +#include "pgstat.h" + + #include "voting.h" #define MAXFNAMELEN 64 @@ -137,11 +145,42 @@ repmgr_shmem_startup(void) Datum request_vote(PG_FUNCTION_ARGS) { - uint32 node_id = PG_GETARG_INT32(0); + /* node_id used for logging purposes */ + int requesting_node_id = PG_GETARG_INT32(0); + XLogRecPtr requesting_node_last_lsn = PG_GETARG_LSN(1); + StringInfoData query; - elog(INFO, "id is %i", node_id); + int ret; + bool isnull; - PG_RETURN_BOOL(true); + int lsn_diff; + + elog(INFO, "id is %i, lsn: %X/%X", + requesting_node_id, + (uint32) (requesting_node_last_lsn >> 32), + (uint32) requesting_node_last_lsn); + + SPI_connect(); + + initStringInfo(&query); + appendStringInfo( + &query, + "SELECT '%X/%X'::pg_lsn - pg_catalog.pg_last_wal_receive_lsn()::pg_lsn", + (uint32) (requesting_node_last_lsn >> 32), + (uint32) requesting_node_last_lsn); + + ret = SPI_execute(query.data, false, 0); + + lsn_diff = DatumGetInt32(SPI_getbinval(SPI_tuptable->vals[0], + SPI_tuptable->tupdesc, + 1, &isnull)); + + elog(INFO, "XXX diff is %i", + lsn_diff); + + SPI_finish(); + + PG_RETURN_INT32(lsn_diff); } diff --git a/repmgrd.c b/repmgrd.c index 8f33e51d..fe532317 100644 --- a/repmgrd.c +++ b/repmgrd.c @@ -483,8 +483,8 @@ monitor_streaming_standby(void) // check result (void) get_node_record(local_conn, local_node_info.upstream_node_id, &upstream_node_info); - // check result, fail if not up (must start on running node) - upstream_conn = establish_db_connection(config_file_options.conninfo, false); + // handle failure - do we want to loop here? + upstream_conn = establish_db_connection(upstream_node_info.conninfo, false); // fix for cascaded standbys primary_conn = upstream_conn; @@ -635,8 +635,13 @@ do_election(void) local_node_info.node_id, local_node_info.priority, last_wal_receive_lsn); + + PQfinish(cell->node_info->conn); + cell->node_info->conn = NULL; } + log_notice(_("%i of of %i votes"), votes_for_me, visible_nodes); + return VS_VOTE_WON; }