diff --git a/store/driver/txn/error.go b/store/driver/txn/error.go index 31f1fd1544..9f92c9b1cf 100644 --- a/store/driver/txn/error.go +++ b/store/driver/txn/error.go @@ -155,6 +155,10 @@ func toTiDBErr(err error) error { if tikverr.IsErrNotFound(err) { return kv.ErrNotExist } + + if errors.ErrorEqual(err, tikverr.ErrInvalidTxn) { + return kv.ErrInvalidTxn + } return errors.Trace(err) } diff --git a/store/tikv/error/error.go b/store/tikv/error/error.go index ffa2baf963..987d80e591 100644 --- a/store/tikv/error/error.go +++ b/store/tikv/error/error.go @@ -27,6 +27,8 @@ var ( ErrTiDBShuttingDown = errors.New("tidb server shutting down") // ErrNotExist means the related data not exist. ErrNotExist = errors.New("not exist") + // ErrInvalidTxn is the error when commits or rollbacks in an invalid transaction. + ErrInvalidTxn = errors.New("invalid transaction") ) // MismatchClusterID represents the message that the cluster ID of the PD client does not match the PD. diff --git a/store/tikv/tests/2pc_fail_test.go b/store/tikv/tests/2pc_fail_test.go index ed8820a302..d8b35e4b28 100644 --- a/store/tikv/tests/2pc_fail_test.go +++ b/store/tikv/tests/2pc_fail_test.go @@ -20,7 +20,7 @@ import ( "github.com/pingcap/errors" "github.com/pingcap/failpoint" "github.com/pingcap/parser/terror" - "github.com/pingcap/tidb/kv" + tikverr "github.com/pingcap/tidb/store/tikv/error" ) // TestFailCommitPrimaryRpcErrors tests rpc errors are handled properly when @@ -40,7 +40,7 @@ func (s *testCommitterSuite) TestFailCommitPrimaryRpcErrors(c *C) { // We don't need to call "Rollback" after "Commit" fails. err = t1.Rollback() - c.Assert(err, Equals, kv.ErrInvalidTxn) + c.Assert(err, Equals, tikverr.ErrInvalidTxn) } // TestFailCommitPrimaryRegionError tests RegionError is handled properly when diff --git a/store/tikv/tests/async_commit_fail_test.go b/store/tikv/tests/async_commit_fail_test.go index 5e519afb51..a791f16c54 100644 --- a/store/tikv/tests/async_commit_fail_test.go +++ b/store/tikv/tests/async_commit_fail_test.go @@ -23,7 +23,6 @@ import ( "github.com/pingcap/failpoint" "github.com/pingcap/kvproto/pkg/kvrpcpb" "github.com/pingcap/parser/terror" - "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/store/tikv" tikverr "github.com/pingcap/tidb/store/tikv/error" "github.com/pingcap/tidb/store/tikv/util" @@ -65,7 +64,7 @@ func (s *testAsyncCommitFailSuite) TestFailAsyncCommitPrewriteRpcErrors(c *C) { // We don't need to call "Rollback" after "Commit" fails. err = t1.Rollback() - c.Assert(err, Equals, kv.ErrInvalidTxn) + c.Assert(err, Equals, tikverr.ErrInvalidTxn) // Create a new transaction to check. The previous transaction should actually commit. t2 := s.beginAsyncCommit(c) diff --git a/store/tikv/txn.go b/store/tikv/txn.go index e78eaba81f..3ef4ad545e 100644 --- a/store/tikv/txn.go +++ b/store/tikv/txn.go @@ -218,7 +218,7 @@ func (txn *KVTxn) Commit(ctx context.Context) error { defer trace.StartRegion(ctx, "CommitTxn").End() if !txn.valid { - return tidbkv.ErrInvalidTxn + return tikverr.ErrInvalidTxn } defer txn.close() @@ -314,7 +314,7 @@ func (txn *KVTxn) close() { // Rollback undoes the transaction operations to KV store. func (txn *KVTxn) Rollback() error { if !txn.valid { - return tidbkv.ErrInvalidTxn + return tikverr.ErrInvalidTxn } start := time.Now() // Clean up pessimistic lock.