Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server: set timeout for MoveLeader #1538

Merged
merged 1 commit into from
May 16, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions server/leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ import (
log "github.com/sirupsen/logrus"
)

// IsLeader returns whether server is leader or not.
// The timeout to wait transfer etcd leader to complete.
const moveLeaderTimeout = 5 * time.Second

// IsLeader returns whether the server is leader or not.
func (s *Server) IsLeader() bool {
// If server is not started. Both leaderID and ID could be 0.
return !s.isClosed() && s.GetLeaderID() == s.ID()
Expand Down Expand Up @@ -142,7 +145,7 @@ func (s *Server) etcdLeaderLoop() {
break
}
if myPriority > leaderPriority {
err := s.etcd.Server.MoveLeader(ctx, etcdLeader, s.ID())
err := s.MoveEtcdLeader(ctx, etcdLeader, s.ID())
if err != nil {
log.Errorf("failed to transfer etcd leader: %v", err)
} else {
Expand All @@ -156,6 +159,13 @@ func (s *Server) etcdLeaderLoop() {
}
}

// MoveEtcdLeader tries to transfer etcd leader.
func (s *Server) MoveEtcdLeader(ctx context.Context, old, new uint64) error {
moveCtx, cancel := context.WithTimeout(ctx, moveLeaderTimeout)
defer cancel()
return errors.WithStack(s.etcd.Server.MoveLeader(moveCtx, old, new))
}

// getLeader gets server leader from etcd.
func getLeader(c *clientv3.Client, leaderPath string) (*pdpb.Member, int64, error) {
leader := &pdpb.Member{}
Expand Down Expand Up @@ -358,7 +368,7 @@ func (s *Server) ResignLeader(nextLeader string) error {
}
nextLeaderID := leaderIDs[rand.Intn(len(leaderIDs))]
log.Infof("%s ready to resign leader, next leader: %v", s.Name(), nextLeaderID)
err = s.etcd.Server.MoveLeader(s.serverLoopCtx, s.ID(), nextLeaderID)
err = s.MoveEtcdLeader(s.serverLoopCtx, s.ID(), nextLeaderID)
return errors.WithStack(err)
}

Expand Down