Skip to content

Commit

Permalink
ddl: Use the correct error (pingcap#4221)
Browse files Browse the repository at this point in the history
  • Loading branch information
zimulala authored and hanfei1991 committed Aug 17, 2017
1 parent 2399126 commit dcbfd71
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 22 deletions.
13 changes: 4 additions & 9 deletions ddl/owner_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/ngaut/log"
"github.com/pingcap/tidb/terror"
goctx "golang.org/x/net/context"
"google.golang.org/grpc"
)

// OwnerManager is used to campaign the owner and manage the owner information.
Expand Down Expand Up @@ -117,17 +116,17 @@ func newSession(ctx goctx.Context, flag string, etcdCli *clientv3.Client, retryC
var err error
var etcdSession *concurrency.Session
for i := 0; i < retryCnt; i++ {
if isContextDone(ctx) {
return etcdSession, errors.Trace(ctx.Err())
}

etcdSession, err = concurrency.NewSession(etcdCli,
concurrency.WithTTL(ttl), concurrency.WithContext(ctx))
if err == nil {
break
}
log.Warnf("[ddl] %s failed to new session, err %v", flag, err)
if isContextFinished(err) || terror.ErrorEqual(err, grpc.ErrClientConnClosing) {
break
}
time.Sleep(200 * time.Millisecond)
continue
}
return etcdSession, errors.Trace(err)
}
Expand Down Expand Up @@ -181,10 +180,6 @@ func (m *ownerManager) campaignLoop(ctx goctx.Context, etcdSession *concurrency.
err = elec.Campaign(ctx, m.ddlID)
if err != nil {
log.Infof("[ddl] %s failed to campaign, err %v", idInfo, err)
if isContextFinished(err) {
log.Warnf("[ddl] %s campaign loop, err %v", idInfo, err)
return
}
continue
}

Expand Down
19 changes: 6 additions & 13 deletions ddl/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/coreos/etcd/clientv3/concurrency"
"github.com/juju/errors"
"github.com/ngaut/log"
"github.com/pingcap/tidb/terror"
goctx "golang.org/x/net/context"
)

Expand Down Expand Up @@ -96,10 +95,8 @@ func (s *schemaVersionSyncer) putKV(ctx goctx.Context, retryCnt int, key, val st
opts ...clientv3.OpOption) error {
var err error
for i := 0; i < retryCnt; i++ {
select {
case <-ctx.Done():
if isContextDone(ctx) {
return errors.Trace(ctx.Err())
default:
}

childCtx, cancel := goctx.WithTimeout(ctx, keyOpDefaultTimeout)
Expand Down Expand Up @@ -184,10 +181,11 @@ func (s *schemaVersionSyncer) RemoveSelfVersionPath() error {
return errors.Trace(err)
}

func isContextFinished(err error) bool {
if terror.ErrorEqual(err, goctx.Canceled) ||
terror.ErrorEqual(err, goctx.DeadlineExceeded) {
func isContextDone(ctx goctx.Context) bool {
select {
case <-ctx.Done():
return true
default:
}
return false
}
Expand All @@ -199,16 +197,11 @@ func (s *schemaVersionSyncer) OwnerCheckAllVersions(ctx goctx.Context, latestVer
intervalCnt := int(time.Second / checkVersInterval)
updatedMap := make(map[string]struct{})
for {
select {
case <-ctx.Done():
if isContextDone(ctx) {
return errors.Trace(ctx.Err())
default:
}

resp, err := s.etcdCli.Get(ctx, DDLAllSchemaVersions, clientv3.WithPrefix())
if isContextFinished(err) {
return errors.Trace(err)
}
if err != nil {
log.Infof("[syncer] check all versions failed %v", err)
continue
Expand Down

0 comments on commit dcbfd71

Please sign in to comment.