Skip to content

Commit

Permalink
return early when leadership transfer fail due to protocol version
Browse files Browse the repository at this point in the history
  • Loading branch information
lgfa29 committed Mar 16, 2022
1 parent b15b7be commit 2dc1ff1
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions nomad/leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,23 @@ func (s *Server) monitorLeadership() {
func (s *Server) leadershipTransfer() error {
retryCount := 3
for i := 0; i < retryCount; i++ {
future := s.raft.LeadershipTransfer()
if err := future.Error(); err != nil {
s.logger.Error("failed to transfer leadership attempt, will retry",
"attempt", i,
"retry_limit", retryCount,
"error", err,
)
} else {
s.logger.Info("successfully transferred leadership",
"attempt", i,
"retry_limit", retryCount,
)
err := s.raft.LeadershipTransfer().Error()
if err == nil {
s.logger.Info("successfully transferred leadership")
return nil
}

// Don't retry if the Raft version doesn't support leadership transfer
// since this will never succeed.
if err == raft.ErrUnsupportedProtocol {
return fmt.Errorf("leadership transfer not supported with Raft version lower than 3")
}

s.logger.Error("failed to transfer leadership attempt, will retry",
"attempt", i,
"retry_limit", retryCount,
"error", err,
)
}
return fmt.Errorf("failed to transfer leadership in %d attempts", retryCount)
}
Expand Down

0 comments on commit 2dc1ff1

Please sign in to comment.