Skip to content

Commit ddcbc50

Browse files
committed
panic if transport is not supported in preElectSelf.
1 parent 27a2e7d commit ddcbc50

File tree

1 file changed

+30
-25
lines changed

1 file changed

+30
-25
lines changed

raft.go

+30-25
Original file line numberDiff line numberDiff line change
@@ -1747,9 +1747,6 @@ func (r *Raft) requestPreVote(rpc RPC, req *RequestPreVoteRequest) {
17471747
}()
17481748

17491749
// Check if we have an existing leader [who's not the candidate] and also
1750-
// check the LeadershipTransfer flag is set. Usually votes are rejected if
1751-
// there is a known leader. But if the leader initiated a leadership transfer,
1752-
// vote!
17531750
var candidate ServerAddress
17541751
candidateID := ServerID(req.ID)
17551752

@@ -2050,6 +2047,14 @@ func (r *Raft) electSelf() <-chan *voteResult {
20502047
// vote for ourself).
20512048
// This must only be called from the main thread.
20522049
func (r *Raft) preElectSelf() <-chan *preVoteResult {
2050+
2051+
// At this point transport should support pre-vote
2052+
// but check just in case
2053+
prevoteTrans, prevoteTransSupported := r.trans.(WithPreVote)
2054+
if !prevoteTransSupported {
2055+
panic("preElection is not possible if the transport don't support pre-vote")
2056+
}
2057+
20532058
// Create a response channel
20542059
respCh := make(chan *preVoteResult, len(r.configurations.latest.Servers))
20552060

@@ -2068,30 +2073,30 @@ func (r *Raft) preElectSelf() <-chan *preVoteResult {
20682073
// Construct a function to ask for a vote
20692074
askPeer := func(peer Server) {
20702075
r.goFunc(func() {
2071-
defer metrics.MeasureSince([]string{"raft", "candidate", "electSelf"}, time.Now())
2076+
defer metrics.MeasureSince([]string{"raft", "candidate", "preElectSelf"}, time.Now())
20722077
resp := &preVoteResult{voterID: peer.ID}
2073-
if prevoteTrans, ok := r.trans.(WithPreVote); ok {
2074-
err := prevoteTrans.RequestPreVote(peer.ID, peer.Address, req, &resp.RequestPreVoteResponse)
2075-
2076-
// If the target server do not support Pre-vote RPC we count this as a granted vote to allow
2077-
// the cluster to progress.
2078-
if err != nil && strings.Contains(err.Error(), rpcUnexpectedCommandError) {
2079-
r.logger.Error("target do not support pre-vote RPC",
2080-
"target", peer,
2081-
"error", err,
2082-
"term", req.Term)
2083-
resp.Term = req.Term
2084-
resp.Granted = true
2085-
} else if err != nil {
2086-
r.logger.Error("failed to make requestVote RPC",
2087-
"target", peer,
2088-
"error", err,
2089-
"term", req.Term)
2090-
resp.Term = req.Term
2091-
resp.Granted = false
2092-
}
2093-
respCh <- resp
2078+
2079+
err := prevoteTrans.RequestPreVote(peer.ID, peer.Address, req, &resp.RequestPreVoteResponse)
2080+
2081+
// If the target server do not support Pre-vote RPC we count this as a granted vote to allow
2082+
// the cluster to progress.
2083+
if err != nil && strings.Contains(err.Error(), rpcUnexpectedCommandError) {
2084+
r.logger.Error("target do not support pre-vote RPC",
2085+
"target", peer,
2086+
"error", err,
2087+
"term", req.Term)
2088+
resp.Term = req.Term
2089+
resp.Granted = true
2090+
} else if err != nil {
2091+
r.logger.Error("failed to make requestVote RPC",
2092+
"target", peer,
2093+
"error", err,
2094+
"term", req.Term)
2095+
resp.Term = req.Term
2096+
resp.Granted = false
20942097
}
2098+
respCh <- resp
2099+
20952100
})
20962101
}
20972102

0 commit comments

Comments
 (0)