Skip to content

Commit

Permalink
owner: revoke on ctx.Done. (pingcap#4624)
Browse files Browse the repository at this point in the history
  • Loading branch information
coocood authored Sep 26, 2017
1 parent 0094512 commit e19e1ac
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions owner/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,19 +164,15 @@ func (m *ownerManager) campaignLoop(ctx goctx.Context, etcdSession *concurrency.
select {
case <-etcdSession.Done():
log.Infof("%s etcd session is done, creates a new one", logPrefix)
leaseID := etcdSession.Lease()
etcdSession, err = NewSession(ctx, logPrefix, m.etcdCli, NewSessionRetryUnlimited, ManagerSessionTTL)
if err != nil {
log.Infof("%s break campaign loop, err %v", logPrefix, err)
log.Infof("%s break campaign loop, NewSession err %v", logPrefix, err)
m.revokeSession(logPrefix, leaseID)
return
}
case <-ctx.Done():
// Revoke the session lease.
// If revoke takes longer than the ttl, lease is expired anyway.
cancelCtx, cancel := goctx.WithTimeout(goctx.Background(),
time.Duration(ManagerSessionTTL)*time.Second)
_, err = m.etcdCli.Revoke(cancelCtx, etcdSession.Lease())
cancel()
log.Infof("%s break campaign loop err %v", logPrefix, err)
m.revokeSession(logPrefix, etcdSession.Lease())
return
default:
}
Expand All @@ -203,12 +199,21 @@ func (m *ownerManager) campaignLoop(ctx goctx.Context, etcdSession *concurrency.
continue
}
m.SetOwner(true)

m.watchOwner(ctx, etcdSession, ownerKey)
m.SetOwner(false)
}
}

func (m *ownerManager) revokeSession(logPrefix string, leaseID clientv3.LeaseID) {
// Revoke the session lease.
// If revoke takes longer than the ttl, lease is expired anyway.
cancelCtx, cancel := goctx.WithTimeout(goctx.Background(),
time.Duration(ManagerSessionTTL)*time.Second)
_, err := m.etcdCli.Revoke(cancelCtx, leaseID)
cancel()
log.Infof("%s break campaign loop, revoke err %v", logPrefix, err)
}

// GetOwnerID implements Manager.GetOwnerID interface.
func (m *ownerManager) GetOwnerID(ctx goctx.Context) (string, error) {
resp, err := m.etcdCli.Get(ctx, m.key, clientv3.WithFirstCreate()...)
Expand Down

0 comments on commit e19e1ac

Please sign in to comment.