Add some sanity checks for calls to repmgrd functions

This commit is contained in:
Ian Barwick
2017-09-21 16:45:41 +09:00
parent 8c373488af
commit 7c3f6a00bd

View File

@@ -233,7 +233,13 @@ set_local_node_id(PG_FUNCTION_ARGS)
PG_RETURN_NULL(); PG_RETURN_NULL();
LWLockAcquire(shared_state->lock, LW_SHARED); LWLockAcquire(shared_state->lock, LW_SHARED);
/* only set local_node_id once, as it should never change */
if (shared_state->local_node_id == UNKNOWN_NODE_ID)
{
shared_state->local_node_id = local_node_id; shared_state->local_node_id = local_node_id;
}
LWLockRelease(shared_state->lock); LWLockRelease(shared_state->lock);
PG_RETURN_VOID(); PG_RETURN_VOID();
@@ -300,15 +306,14 @@ request_vote(PG_FUNCTION_ARGS)
int ret; int ret;
if (!shared_state) if (!shared_state)
PG_RETURN_NULL(); PG_RETURN_NULL();
LWLockAcquire(shared_state->lock, LW_SHARED); 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 */ /* this node has initiated voting or already responded to another node */
if (shared_state->voting_status != VS_NO_VOTE) if (shared_state->voting_status != VS_NO_VOTE)
{ {
@@ -341,6 +346,7 @@ request_vote(PG_FUNCTION_ARGS)
{ {
SPI_finish(); SPI_finish();
elog(WARNING, "unable to retrieve last received LSN"); elog(WARNING, "unable to retrieve last received LSN");
LWLockRelease(shared_state->lock);
#if (PG_VERSION_NUM >= 90400) #if (PG_VERSION_NUM >= 90400)
PG_RETURN_LSN(InvalidOid); PG_RETURN_LSN(InvalidOid);
@@ -370,10 +376,11 @@ request_vote(PG_FUNCTION_ARGS)
shared_state->voting_status = VS_VOTE_REQUEST_RECEIVED; shared_state->voting_status = VS_VOTE_REQUEST_RECEIVED;
shared_state->current_electoral_term = current_electoral_term; shared_state->current_electoral_term = current_electoral_term;
LWLockRelease(shared_state->lock);
/* should we free "query" here? */ /* should we free "query" here? */
SPI_finish(); SPI_finish();
}
LWLockRelease(shared_state->lock);
#if (PG_VERSION_NUM >= 90400) #if (PG_VERSION_NUM >= 90400)
PG_RETURN_LSN(our_lsn); PG_RETURN_LSN(our_lsn);
@@ -410,16 +417,22 @@ Datum
set_voting_status_initiated(PG_FUNCTION_ARGS) set_voting_status_initiated(PG_FUNCTION_ARGS)
{ {
#ifndef BDR_ONLY #ifndef BDR_ONLY
int electoral_term; int electoral_term = -1;
LWLockAcquire(shared_state->lock, LW_SHARED); LWLockAcquire(shared_state->lock, LW_SHARED);
/* only do something if local_node_id is initialised */
if (shared_state->local_node_id != UNKNOWN_NODE_ID)
{
shared_state->voting_status = VS_VOTE_INITIATED; shared_state->voting_status = VS_VOTE_INITIATED;
shared_state->current_electoral_term += 1; shared_state->current_electoral_term += 1;
electoral_term = shared_state->current_electoral_term; electoral_term = shared_state->current_electoral_term;
LWLockRelease(shared_state->lock);
elog(INFO, "setting voting term to %i", electoral_term); elog(INFO, "setting voting term to %i", electoral_term);
}
LWLockRelease(shared_state->lock);
PG_RETURN_INT32(electoral_term); PG_RETURN_INT32(electoral_term);
#else #else
@@ -439,6 +452,9 @@ other_node_is_candidate(PG_FUNCTION_ARGS)
LWLockAcquire(shared_state->lock, LW_SHARED); 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->current_electoral_term == electoral_term)
{ {
if (shared_state->candidate_node_id != UNKNOWN_NODE_ID) if (shared_state->candidate_node_id != UNKNOWN_NODE_ID)
@@ -451,9 +467,11 @@ other_node_is_candidate(PG_FUNCTION_ARGS)
} }
shared_state->candidate_node_id = requesting_node_id; shared_state->candidate_node_id = requesting_node_id;
elog(INFO, "node %i is candidate", requesting_node_id);
}
LWLockRelease(shared_state->lock); LWLockRelease(shared_state->lock);
elog(INFO, "node %i is candidate", requesting_node_id);
PG_RETURN_BOOL(true); PG_RETURN_BOOL(true);
#else #else
PG_RETURN_BOOL(false); PG_RETURN_BOOL(false);
@@ -471,6 +489,9 @@ notify_follow_primary(PG_FUNCTION_ARGS)
LWLockAcquire(shared_state->lock, LW_SHARED); LWLockAcquire(shared_state->lock, LW_SHARED);
/* only do something if local_node_id is initialised */
if (shared_state->local_node_id != UNKNOWN_NODE_ID)
{
elog(INFO, "node %i received notification to follow node %i", elog(INFO, "node %i received notification to follow node %i",
shared_state->local_node_id, shared_state->local_node_id,
primary_node_id); primary_node_id);
@@ -478,6 +499,8 @@ notify_follow_primary(PG_FUNCTION_ARGS)
/* Explicitly set the primary node id */ /* Explicitly set the primary node id */
shared_state->candidate_node_id = primary_node_id; shared_state->candidate_node_id = primary_node_id;
shared_state->follow_new_primary = true; shared_state->follow_new_primary = true;
}
LWLockRelease(shared_state->lock); LWLockRelease(shared_state->lock);
#endif #endif
PG_RETURN_VOID(); PG_RETURN_VOID();
@@ -513,9 +536,13 @@ reset_voting_status(PG_FUNCTION_ARGS)
LWLockAcquire(shared_state->lock, LW_SHARED); LWLockAcquire(shared_state->lock, LW_SHARED);
/* only do something if local_node_id is initialised */
if (shared_state->local_node_id != UNKNOWN_NODE_ID)
{
shared_state->voting_status = VS_NO_VOTE; shared_state->voting_status = VS_NO_VOTE;
shared_state->candidate_node_id = UNKNOWN_NODE_ID; shared_state->candidate_node_id = UNKNOWN_NODE_ID;
shared_state->follow_new_primary = false; shared_state->follow_new_primary = false;
}
LWLockRelease(shared_state->lock); LWLockRelease(shared_state->lock);
#endif #endif
@@ -556,9 +583,13 @@ unset_bdr_failover_handler(PG_FUNCTION_ARGS)
if (!shared_state) if (!shared_state)
PG_RETURN_NULL(); PG_RETURN_NULL();
/* only do something if local_node_id is initialised */
if (shared_state->local_node_id != UNKNOWN_NODE_ID)
{
LWLockAcquire(shared_state->lock, LW_SHARED); LWLockAcquire(shared_state->lock, LW_SHARED);
shared_state->bdr_failover_handler = UNKNOWN_NODE_ID; shared_state->bdr_failover_handler = UNKNOWN_NODE_ID;
LWLockRelease(shared_state->lock); LWLockRelease(shared_state->lock);
}
PG_RETURN_VOID(); PG_RETURN_VOID();
} }