Skip to content

Commit

Permalink
Optimize the err defines for tso
Browse files Browse the repository at this point in the history
Signed-off-by: JmPotato <ghzpotato@gmail.com>
  • Loading branch information
JmPotato committed Sep 2, 2020
1 parent 6876790 commit 07c7f43
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
3 changes: 3 additions & 0 deletions pkg/errs/errno.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import "github.com/pingcap/errors"
// The internal error which is generated in PD project.
// tso errors
var (
ErrSaveTimestamp = errors.Normalize("save timestamp failed, %s", errors.RFCCodeText("PD:tso:ErrSaveTimestamp"))
ErrResetUserTimestamp = errors.Normalize("reset user timestamp failed, %s", errors.RFCCodeText("PD:tso:ErrResetUserTimestamp"))
ErrGenerateTimestamp = errors.Normalize("generate timestamp failed, %s", errors.RFCCodeText("PD:tso:ErrGenerateTimestamp"))
ErrInvalidTimestamp = errors.Normalize("invalid timestamp", errors.RFCCodeText("PD:tso:ErrInvalidTimestamp"))
ErrLogicOverflow = errors.Normalize("logic part overflow", errors.RFCCodeText("PD:tso:ErrLogicOverflow"))
ErrIncorrectSystemTime = errors.Normalize("incorrect system time", errors.RFCCodeText("PD:tso:ErrIncorrectSystemTime"))
Expand Down
9 changes: 4 additions & 5 deletions server/tso/global_allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"sync/atomic"
"time"

"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/pingcap/log"
Expand Down Expand Up @@ -87,7 +86,7 @@ func (gta *GlobalTSOAllocator) GenerateTSO(count uint32) (pdpb.Timestamp, error)
var resp pdpb.Timestamp

if count == 0 {
return resp, errors.New("tso count should be positive")
return resp, errs.ErrGenerateTimestamp.FastGenByArgs("tso count should be positive")
}

maxRetryCount := 10
Expand All @@ -105,7 +104,7 @@ func (gta *GlobalTSOAllocator) GenerateTSO(count uint32) (pdpb.Timestamp, error)
continue
}
log.Error("invalid timestamp", zap.Any("timestamp", current), errs.ZapError(errs.ErrInvalidTimestamp))
return pdpb.Timestamp{}, errors.New("can not get timestamp, may be not leader")
return pdpb.Timestamp{}, errs.ErrGenerateTimestamp.FastGenByArgs("may be not leader")
}

resp.Physical = current.physical.UnixNano() / int64(time.Millisecond)
Expand All @@ -120,11 +119,11 @@ func (gta *GlobalTSOAllocator) GenerateTSO(count uint32) (pdpb.Timestamp, error)
}
// In case lease expired after the first check.
if !gta.leadership.Check() {
return pdpb.Timestamp{}, errors.New("alloc timestamp failed, lease expired")
return pdpb.Timestamp{}, errs.ErrGenerateTimestamp.FastGenByArgs("may be not leader")
}
return resp, nil
}
return resp, errors.New("can not get timestamp")
return resp, errs.ErrGenerateTimestamp.FastGenByArgs("maximum number of retries exceeded")
}

// Reset is used to reset the TSO allocator.
Expand Down
13 changes: 6 additions & 7 deletions server/tso/tso.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"time"
"unsafe"

"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/log"
"github.com/tikv/pd/pkg/errs"
Expand Down Expand Up @@ -67,7 +66,7 @@ func (t *timestampOracle) getTimestampPath() string {
func (t *timestampOracle) loadTimestamp() (time.Time, error) {
data, err := etcdutil.GetValue(t.client, t.getTimestampPath())
if err != nil {
return typeutil.ZeroTime, err
return typeutil.ZeroTime, errs.ErrEtcdKVGet.Wrap(err).FastGenWithCause()
}
if len(data) == 0 {
return typeutil.ZeroTime, nil
Expand All @@ -84,10 +83,10 @@ func (t *timestampOracle) saveTimestamp(leadership *election.Leadership, ts time
Then(clientv3.OpPut(key, string(data))).
Commit()
if err != nil {
return errors.WithStack(err)
return errs.ErrEtcdTxn.Wrap(err).GenWithStackByCause()
}
if !resp.Succeeded {
return errors.New("save timestamp failed, maybe we lost leader")
return errs.ErrSaveTimestamp.FastGenByArgs("maybe we lost leader")
}

t.lastSavedTime.Store(ts)
Expand Down Expand Up @@ -141,7 +140,7 @@ func (t *timestampOracle) SyncTimestamp(leadership *election.Leadership) error {
func (t *timestampOracle) ResetUserTimestamp(leadership *election.Leadership, tso uint64) error {
if !leadership.Check() {
tsoCounter.WithLabelValues("err_lease_reset_ts").Inc()
return errors.New("Setup timestamp failed, lease expired")
return errs.ErrResetUserTimestamp.FastGenByArgs("lease expired")
}
physical, _ := tsoutil.ParseTS(tso)
next := physical.Add(time.Millisecond)
Expand All @@ -150,12 +149,12 @@ func (t *timestampOracle) ResetUserTimestamp(leadership *election.Leadership, ts
// do not update
if typeutil.SubTimeByWallClock(next, prev.physical) <= 3*updateTimestampGuard {
tsoCounter.WithLabelValues("err_reset_small_ts").Inc()
return errors.New("the specified ts too small than now")
return errs.ErrResetUserTimestamp.FastGenByArgs("the specified ts too small than now")
}

if typeutil.SubTimeByWallClock(next, prev.physical) >= t.maxResetTSGap() {
tsoCounter.WithLabelValues("err_reset_large_ts").Inc()
return errors.New("the specified ts too large than now")
return errs.ErrResetUserTimestamp.FastGenByArgs("the specified ts too large than now")
}

save := next.Add(t.saveInterval)
Expand Down
2 changes: 1 addition & 1 deletion tests/server/tso/tso_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,6 @@ func (s *testFollowerTsoSuite) TestRequest(c *C) {
c.Assert(err, IsNil)
_, err = tsoClient.Recv()
c.Assert(err, NotNil)
c.Assert(strings.Contains(err.Error(), "can not get timestamp"), IsTrue)
c.Assert(strings.Contains(err.Error(), "generate timestamp failed"), IsTrue)
failpoint.Disable("github.com/tikv/pd/server/tso/skipRetryGetTS")
}

0 comments on commit 07c7f43

Please sign in to comment.