Browse Source

fix leadership transfer on leave suggestions (#11387)

* add suggestions

* set isLeader to false when leadership transfer succeed
pull/11290/head
Dhia Ayachi 3 years ago committed by GitHub
parent
commit
58f5686c08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      .changelog/11376.txt
  2. 33
      agent/consul/server.go

2
.changelog/11376.txt

@ -1,3 +1,3 @@
```release-note:enhancement
try to transfer leadership to another server when leaving.
raft: Consul leaders will attempt to transfer leadership to another server as part of gracefully leaving the cluster.
```

33
agent/consul/server.go

@ -977,6 +977,22 @@ func (s *Server) Shutdown() error {
return nil
}
func (s *Server) attemptLeadershipTransfer() (success bool) {
leadershipTransferVersion := version.Must(version.NewVersion(LeaderTransferMinVersion))
ok, _ := ServersInDCMeetMinimumVersion(s, s.config.Datacenter, leadershipTransferVersion)
if !ok {
return false
}
future := s.raft.LeadershipTransfer()
if err := future.Error(); err != nil {
s.logger.Error("failed to transfer leadership, removing the server", "error", err)
return false
}
return true
}
// Leave is used to prepare for a graceful shutdown of the server
func (s *Server) Leave() error {
s.logger.Info("server starting leave")
@ -996,24 +1012,9 @@ func (s *Server) Leave() error {
// removed for some reasonable period of time.
isLeader := s.IsLeader()
if isLeader && numPeers > 1 {
leadershipTransferVersion := version.Must(version.NewVersion(LeaderTransferMinVersion))
removeServer := false
if ok, _ := ServersInDCMeetMinimumVersion(s, s.config.Datacenter, leadershipTransferVersion); !ok {
// Transfer leadership to another node then leave the cluster
future := s.raft.LeadershipTransfer()
if err := future.Error(); err != nil {
s.logger.Error("failed to transfer leadership, removing the server", "error", err)
// leadership transfer failed, fallback to removing the server from raft
removeServer = true
} else {
// we are not leader anymore, continue the flow to leave as follower
if s.attemptLeadershipTransfer() {
isLeader = false
}
} else {
// Leadership transfer is not available in the current version, fallback to removing the server from raft
removeServer = true
}
if removeServer {
future := s.raft.RemoveServer(raft.ServerID(s.config.NodeID), 0, 0)
if err := future.Error(); err != nil {
s.logger.Error("failed to remove ourself as raft peer", "error", err)

Loading…
Cancel
Save