Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tso: optimize the err handling and make their specification consistent #2878

Merged
merged 4 commits into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/errs/errno.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import "github.com/pingcap/errors"
// The internal error which is generated in PD project.
// tso errors
var (
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")
JmPotato marked this conversation as resolved.
Show resolved Hide resolved
}
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
11 changes: 5 additions & 6 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 @@ -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.ErrEtcdKVPut.Wrap(err).GenWithStackByCause()
}
if !resp.Succeeded {
return errors.New("save timestamp failed, maybe we lost leader")
return errs.ErrEtcdTxn.FastGenByArgs()
}

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")
}