Skip to content
This repository has been archived by the owner on Nov 24, 2023. It is now read-only.

Commit

Permalink
election: address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
csuzhangxc committed Nov 21, 2019
1 parent 5c4c820 commit b99ae46
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions pkg/election/election.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ func (e *Election) toBeLeader() {
case e.leaderCh <- true:
default:
}
e.l.Info("become the leader", zap.String("member", e.id))
}

func (e *Election) retireLeader() {
Expand All @@ -212,7 +211,6 @@ func (e *Election) retireLeader() {
case e.leaderCh <- false:
default:
}
e.l.Info("retire from the leader", zap.String("member", e.id))
}

func (e *Election) watchLeader(ctx context.Context, session *concurrency.Session, key string) {
Expand All @@ -231,6 +229,7 @@ func (e *Election) watchLeader(ctx context.Context, session *concurrency.Session
}

for _, ev := range resp.Events {
// user may use some etcd client (like etcdctl) to delete the leader key and trigger a new campaign.
if ev.Type == mvccpb.DELETE {
e.l.Info("fail to watch, the leader is deleted", zap.ByteString("key", ev.Kv.Key))
return
Expand All @@ -249,6 +248,8 @@ func (e *Election) newSession(ctx context.Context, retryCnt int) (*concurrency.S
err error
session *concurrency.Session
)

forLoop:
for i := 0; i < retryCnt; i++ {
if i > 0 {
select {
Expand All @@ -259,7 +260,7 @@ func (e *Election) newSession(ctx context.Context, retryCnt int) (*concurrency.S
select {
case <-time.After(newSessionRetryInterval):
case <-ctx.Done():
break
break forLoop
}
}

Expand All @@ -268,7 +269,7 @@ func (e *Election) newSession(ctx context.Context, retryCnt int) (*concurrency.S
// so we can close the session when the client is still valid.
session, err = concurrency.NewSession(e.cli, concurrency.WithTTL(e.sessionTTL))
if err == nil || errors.Cause(err) == ctx.Err() {
break
break forLoop
}
}
return session, err
Expand Down

0 comments on commit b99ae46

Please sign in to comment.