From 3ed71f050b9c8c663310bc3659e6aca6715c1592 Mon Sep 17 00:00:00 2001 From: disksing Date: Thu, 16 May 2019 16:35:03 +0800 Subject: [PATCH 1/2] server: set timeout for MoveLeader Signed-off-by: disksing --- server/leader.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/server/leader.go b/server/leader.go index 6a103536edb..5f90784099c 100644 --- a/server/leader.go +++ b/server/leader.go @@ -35,7 +35,10 @@ var ( nextLeaderTTL = 10 // in seconds ) -// 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 { return atomic.LoadInt64(&s.isLeader) == 1 } @@ -135,7 +138,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 { @@ -152,6 +155,13 @@ func getLeaderAddr(leader *pdpb.Member) string { return strings.Join(leader.GetClientUrls(), ",") } +// 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, error) { leader := &pdpb.Member{} @@ -344,7 +354,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.leaderLoopCtx, s.ID(), nextLeaderID) + err = s.MoveEtcdLeader(s.serverLoopCtx, s.ID(), nextLeaderID) return errors.Trace(err) } From 93f3361d1d74ce53104d4714f26b01fac4e32be3 Mon Sep 17 00:00:00 2001 From: disksing Date: Thu, 16 May 2019 16:44:44 +0800 Subject: [PATCH 2/2] fix compile Signed-off-by: disksing --- server/leader.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/leader.go b/server/leader.go index 5f90784099c..8281c59b07b 100644 --- a/server/leader.go +++ b/server/leader.go @@ -159,7 +159,7 @@ func getLeaderAddr(leader *pdpb.Member) string { 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)) + return errors.Trace(s.etcd.Server.MoveLeader(moveCtx, old, new)) } // getLeader gets server leader from etcd. @@ -354,7 +354,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.MoveEtcdLeader(s.serverLoopCtx, s.ID(), nextLeaderID) + err = s.MoveEtcdLeader(s.leaderLoopCtx, s.ID(), nextLeaderID) return errors.Trace(err) }