Skip to content

Commit

Permalink
*: add errcode for structure, tablecodec, admin files (#14054)
Browse files Browse the repository at this point in the history
  • Loading branch information
cfzjywxk authored and tiancaiamao committed Dec 16, 2019
1 parent b8dad33 commit cbf7415
Show file tree
Hide file tree
Showing 15 changed files with 110 additions and 58 deletions.
4 changes: 2 additions & 2 deletions executor/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ func (s *testSuite2) TestAdminCheckPartitionTableFailed(c *C) {
err = txn.Commit(context.Background())
c.Assert(err, IsNil)
err = tk.ExecToErr("admin check table admin_test_p")
c.Assert(err.Error(), Equals, fmt.Sprintf("[executor:8003]admin_test_p err:[admin:1]index:<nil> != record:&admin.RecordData{Handle:%d, Values:[]types.Datum{types.Datum{k:0x1, collation:0x0, decimal:0x0, length:0x0, i:%d, b:[]uint8(nil), x:interface {}(nil)}}}", i, i))
c.Assert(err.Error(), Equals, fmt.Sprintf("[executor:8003]admin_test_p err:[admin:8223]index:<nil> != record:&admin.RecordData{Handle:%d, Values:[]types.Datum{types.Datum{k:0x1, collation:0x0, decimal:0x0, length:0x0, i:%d, b:[]uint8(nil), x:interface {}(nil)}}}", i, i))
c.Assert(executor.ErrAdminCheckTable.Equal(err), IsTrue)
// TODO: fix admin recover for partition table.
//r := tk.MustQuery("admin recover index admin_test_p idx")
Expand Down Expand Up @@ -527,7 +527,7 @@ func (s *testSuite5) TestAdminCheckTableFailed(c *C) {
c.Assert(err, IsNil)
err = tk.ExecToErr("admin check table admin_test")
c.Assert(err.Error(), Equals,
"[executor:8003]admin_test err:[admin:1]index:<nil> != record:&admin.RecordData{Handle:-1, Values:[]types.Datum{types.Datum{k:0x1, collation:0x0, decimal:0x0, length:0x0, i:-10, b:[]uint8(nil), x:interface {}(nil)}}}")
"[executor:8003]admin_test err:[admin:8223]index:<nil> != record:&admin.RecordData{Handle:-1, Values:[]types.Datum{types.Datum{k:0x1, collation:0x0, decimal:0x0, length:0x0, i:-10, b:[]uint8(nil), x:interface {}(nil)}}}")
c.Assert(executor.ErrAdminCheckTable.Equal(err), IsTrue)
r := tk.MustQuery("admin recover index admin_test c2")
r.Check(testkit.Rows("1 7"))
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ require (
github.com/pingcap/goleveldb v0.0.0-20171020122428-b9ff6c35079e
github.com/pingcap/kvproto v0.0.0-20191202044712-32be31591b03
github.com/pingcap/log v0.0.0-20191012051959-b742a5d432e9
github.com/pingcap/parser v0.0.0-20191210060830-bdf23a7ade01
github.com/pingcap/parser v0.0.0-20191216031109-53ef01243aa6
github.com/pingcap/pd v1.1.0-beta.0.20191210055626-676ddd3fbd2d
github.com/pingcap/sysutil v0.0.0-20191126040022-986c5b3ed9a3
github.com/pingcap/tidb-tools v3.0.6-0.20191106033616-90632dda3863+incompatible
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ github.com/pingcap/kvproto v0.0.0-20191202044712-32be31591b03/go.mod h1:WWLmULLO
github.com/pingcap/log v0.0.0-20190715063458-479153f07ebd/go.mod h1:WpHUKhNZ18v116SvGrmjkA9CBhYmuUTKL+p8JC9ANEw=
github.com/pingcap/log v0.0.0-20191012051959-b742a5d432e9 h1:AJD9pZYm72vMgPcQDww9rkZ1DnWfl0pXV3BOWlkYIjA=
github.com/pingcap/log v0.0.0-20191012051959-b742a5d432e9/go.mod h1:4rbK1p9ILyIfb6hU7OG2CiWSqMXnp3JMbiaVJ6mvoY8=
github.com/pingcap/parser v0.0.0-20191210060830-bdf23a7ade01 h1:q1rGnV/296//bArDP7cDWWaSrhaeEKZY+gIo+Jb0Gyk=
github.com/pingcap/parser v0.0.0-20191210060830-bdf23a7ade01/go.mod h1:1FNvfp9+J0wvc4kl8eGNh7Rqrxveg15jJoWo/a0uHwA=
github.com/pingcap/parser v0.0.0-20191216031109-53ef01243aa6 h1:aAhPfDloqGNCYkY0/vpcRGneyum5Sph4+foxA4XzarU=
github.com/pingcap/parser v0.0.0-20191216031109-53ef01243aa6/go.mod h1:1FNvfp9+J0wvc4kl8eGNh7Rqrxveg15jJoWo/a0uHwA=
github.com/pingcap/pd v1.1.0-beta.0.20191210055626-676ddd3fbd2d h1:0uUwCayeh1U4r1n7kb4gHf2ZbiI9fuVtXPCEpb2piBY=
github.com/pingcap/pd v1.1.0-beta.0.20191210055626-676ddd3fbd2d/go.mod h1:Z/VMtXHpkOP+MnYnk4TL5VHc3ZwO1qHwc89zDuf5n8Q=
github.com/pingcap/sysutil v0.0.0-20191126040022-986c5b3ed9a3 h1:HCNif3lukL83gNC2EBAoh2Qbz36+2p0bm0LjgnNfl1s=
Expand Down
2 changes: 1 addition & 1 deletion meta/meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ func (s *testSuite) TestSnapshot(c *C) {
c.Assert(n, Equals, int64(1))
_, err = snapMeta.GenGlobalID()
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[structure:5]write on snapshot")
c.Assert(err.Error(), Equals, "[structure:8220]write on snapshot")
}

func (s *testSuite) TestDDL(c *C) {
Expand Down
8 changes: 4 additions & 4 deletions structure/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (meta hashMeta) IsEmpty() bool {
// HSet sets the string value of a hash field.
func (t *TxStructure) HSet(key []byte, field []byte, value []byte) error {
if t.readWriter == nil {
return errWriteOnSnapshot
return ErrWriteOnSnapshot
}
return t.updateHash(key, field, func([]byte) ([]byte, error) {
return value, nil
Expand Down Expand Up @@ -76,7 +76,7 @@ func (t *TxStructure) EncodeHashAutoIDKeyValue(key []byte, field []byte, val int
// the value after the increment.
func (t *TxStructure) HInc(key []byte, field []byte, step int64) (int64, error) {
if t.readWriter == nil {
return 0, errWriteOnSnapshot
return 0, ErrWriteOnSnapshot
}
base := int64(0)
err := t.updateHash(key, field, func(oldValue []byte) ([]byte, error) {
Expand Down Expand Up @@ -156,7 +156,7 @@ func (t *TxStructure) HLen(key []byte) (int64, error) {
// HDel deletes one or more hash fields.
func (t *TxStructure) HDel(key []byte, fields ...[]byte) error {
if t.readWriter == nil {
return errWriteOnSnapshot
return ErrWriteOnSnapshot
}
metaKey := t.encodeHashMetaKey(key)
meta, err := t.loadHashMeta(metaKey)
Expand Down Expand Up @@ -332,7 +332,7 @@ func (t *TxStructure) loadHashMeta(metaKey []byte) (hashMeta, error) {
}

if len(v) != 8 {
return meta, errInvalidListMetaData
return meta, ErrInvalidListMetaData
}

meta.FieldCount = int64(binary.BigEndian.Uint64(v[0:8]))
Expand Down
12 changes: 6 additions & 6 deletions structure/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (t *TxStructure) RPush(key []byte, values ...[]byte) error {

func (t *TxStructure) listPush(key []byte, left bool, values ...[]byte) error {
if t.readWriter == nil {
return errWriteOnSnapshot
return ErrWriteOnSnapshot
}
if len(values) == 0 {
return nil
Expand Down Expand Up @@ -92,7 +92,7 @@ func (t *TxStructure) RPop(key []byte) ([]byte, error) {

func (t *TxStructure) listPop(key []byte, left bool) ([]byte, error) {
if t.readWriter == nil {
return nil, errWriteOnSnapshot
return nil, ErrWriteOnSnapshot
}
metaKey := t.encodeListMetaKey(key)
meta, err := t.loadListMeta(metaKey)
Expand Down Expand Up @@ -176,7 +176,7 @@ func (t *TxStructure) LIndex(key []byte, index int64) ([]byte, error) {
// LSet updates an element in the list by its index.
func (t *TxStructure) LSet(key []byte, index int64, value []byte) error {
if t.readWriter == nil {
return errWriteOnSnapshot
return ErrWriteOnSnapshot
}
metaKey := t.encodeListMetaKey(key)
meta, err := t.loadListMeta(metaKey)
Expand All @@ -189,13 +189,13 @@ func (t *TxStructure) LSet(key []byte, index int64, value []byte) error {
if index >= meta.LIndex && index < meta.RIndex {
return t.readWriter.Set(t.encodeListDataKey(key, index), value)
}
return errInvalidListIndex.GenWithStack("invalid list index %d", index)
return ErrInvalidListIndex.GenWithStack("invalid list index %d", index)
}

// LClear removes the list of the key.
func (t *TxStructure) LClear(key []byte) error {
if t.readWriter == nil {
return errWriteOnSnapshot
return ErrWriteOnSnapshot
}
metaKey := t.encodeListMetaKey(key)
meta, err := t.loadListMeta(metaKey)
Expand Down Expand Up @@ -228,7 +228,7 @@ func (t *TxStructure) loadListMeta(metaKey []byte) (listMeta, error) {
}

if len(v) != 16 {
return meta, errInvalidListMetaData
return meta, ErrInvalidListMetaData
}

meta.LIndex = int64(binary.BigEndian.Uint64(v[0:8]))
Expand Down
6 changes: 3 additions & 3 deletions structure/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
// Set sets the string value of the key.
func (t *TxStructure) Set(key []byte, value []byte) error {
if t.readWriter == nil {
return errWriteOnSnapshot
return ErrWriteOnSnapshot
}
ek := t.encodeStringDataKey(key)
return t.readWriter.Set(ek, value)
Expand Down Expand Up @@ -55,7 +55,7 @@ func (t *TxStructure) GetInt64(key []byte) (int64, error) {
// the value after the increment.
func (t *TxStructure) Inc(key []byte, step int64) (int64, error) {
if t.readWriter == nil {
return 0, errWriteOnSnapshot
return 0, ErrWriteOnSnapshot
}
ek := t.encodeStringDataKey(key)
// txn Inc will lock this key, so we don't lock it here.
Expand All @@ -69,7 +69,7 @@ func (t *TxStructure) Inc(key []byte, step int64) (int64, error) {
// Clear removes the string value of the key.
func (t *TxStructure) Clear(key []byte) error {
if t.readWriter == nil {
return errWriteOnSnapshot
return ErrWriteOnSnapshot
}
ek := t.encodeStringDataKey(key)
err := t.readWriter.Delete(ek)
Expand Down
31 changes: 20 additions & 11 deletions structure/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,20 @@
package structure

import (
"github.com/pingcap/parser/mysql"
"github.com/pingcap/parser/terror"
"github.com/pingcap/tidb/kv"
)

// structure error codes.
const (
codeInvalidHashKeyFlag terror.ErrCode = 1
codeInvalidListMetaData terror.ErrCode = 4
codeWriteOnSnapshot terror.ErrCode = 5
)

var (
errInvalidHashKeyFlag = terror.ClassStructure.New(codeInvalidHashKeyFlag, "invalid encoded hash key flag")
errInvalidListIndex = terror.ClassStructure.New(codeInvalidListMetaData, "invalid list index")
errInvalidListMetaData = terror.ClassStructure.New(codeInvalidListMetaData, "invalid list meta data")
errWriteOnSnapshot = terror.ClassStructure.New(codeWriteOnSnapshot, "write on snapshot")
// ErrInvalidHashKeyFlag used by structure
ErrInvalidHashKeyFlag = terror.ClassStructure.New(mysql.ErrInvalidHashKeyFlag, mysql.MySQLErrName[mysql.ErrInvalidHashKeyFlag])
// ErrInvalidListIndex used by structure
ErrInvalidListIndex = terror.ClassStructure.New(mysql.ErrInvalidListIndex, mysql.MySQLErrName[mysql.ErrInvalidListIndex])
// ErrInvalidListMetaData used by structure
ErrInvalidListMetaData = terror.ClassStructure.New(mysql.ErrInvalidListMetaData, mysql.MySQLErrName[mysql.ErrInvalidListMetaData])
// ErrWriteOnSnapshot used by structure
ErrWriteOnSnapshot = terror.ClassStructure.New(mysql.ErrWriteOnSnapshot, mysql.MySQLErrName[mysql.ErrWriteOnSnapshot])
)

// NewStructure creates a TxStructure with Retriever, RetrieverMutator and key prefix.
Expand All @@ -48,3 +46,14 @@ type TxStructure struct {
readWriter kv.RetrieverMutator
prefix []byte
}

func init() {
// Register terror to mysql error map.
mySQLErrCodes := map[terror.ErrCode]uint16{
mysql.ErrInvalidHashKeyFlag: mysql.ErrInvalidHashKeyFlag,
mysql.ErrInvalidListIndex: mysql.ErrInvalidListIndex,
mysql.ErrInvalidListMetaData: mysql.ErrInvalidListMetaData,
mysql.ErrWriteOnSnapshot: mysql.ErrWriteOnSnapshot,
}
terror.ErrClassToMySQLCodes[terror.ClassStructure] = mySQLErrCodes
}
15 changes: 15 additions & 0 deletions structure/structure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"testing"

. "github.com/pingcap/check"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/parser/terror"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/structure"
Expand Down Expand Up @@ -395,3 +397,16 @@ func (s *testTxStructureSuite) TestHash(c *C) {
})
c.Assert(err, IsNil)
}

func (*testTxStructureSuite) TestError(c *C) {
kvErrs := []*terror.Error{
structure.ErrInvalidHashKeyFlag,
structure.ErrInvalidListIndex,
structure.ErrInvalidListMetaData,
structure.ErrWriteOnSnapshot,
}
for _, err := range kvErrs {
code := err.ToSQLError().Code
c.Assert(code != mysql.ErrUnknown && code == uint16(err.Code()), IsTrue, Commentf("err: %v", err))
}
}
2 changes: 1 addition & 1 deletion structure/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (t *TxStructure) decodeHashDataKey(ek kv.Key) ([]byte, []byte, error) {
if err != nil {
return nil, nil, errors.Trace(err)
} else if TypeFlag(tp) != HashData {
return nil, nil, errInvalidHashKeyFlag.GenWithStack("invalid encoded hash data key flag %c", byte(tp))
return nil, nil, ErrInvalidHashKeyFlag.GenWithStack("invalid encoded hash data key flag %c", byte(tp))
}

_, field, err = codec.DecodeBytes(ek, nil)
Expand Down
20 changes: 11 additions & 9 deletions tablecodec/tablecodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ import (
)

var (
errInvalidKey = terror.ClassXEval.New(codeInvalidKey, "invalid key")
errInvalidRecordKey = terror.ClassXEval.New(codeInvalidRecordKey, "invalid record key")
errInvalidIndexKey = terror.ClassXEval.New(codeInvalidIndexKey, "invalid index key")
errInvalidKey = terror.ClassXEval.New(mysql.ErrInvalidKey, mysql.MySQLErrName[mysql.ErrInvalidKey])
errInvalidRecordKey = terror.ClassXEval.New(mysql.ErrInvalidRecordKey, mysql.MySQLErrName[mysql.ErrInvalidRecordKey])
errInvalidIndexKey = terror.ClassXEval.New(mysql.ErrInvalidIndexKey, mysql.MySQLErrName[mysql.ErrInvalidIndexKey])
)

var (
Expand Down Expand Up @@ -721,9 +721,11 @@ func GetTableIndexKeyRange(tableID, indexID int64) (startKey, endKey []byte) {
return
}

const (
codeInvalidRecordKey = 4
codeInvalidColumnCount = 5
codeInvalidKey = 6
codeInvalidIndexKey = 7
)
func init() {
mySQLErrCodes := map[terror.ErrCode]uint16{
mysql.ErrInvalidKey: mysql.ErrInvalidKey,
mysql.ErrInvalidRecordKey: mysql.ErrInvalidRecordKey,
mysql.ErrInvalidIndexKey: mysql.ErrInvalidIndexKey,
}
terror.ErrClassToMySQLCodes[terror.ClassXEval] = mySQLErrCodes
}
13 changes: 13 additions & 0 deletions tablecodec/tablecodec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
. "github.com/pingcap/check"
"github.com/pingcap/failpoint"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/parser/terror"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/types"
Expand Down Expand Up @@ -519,3 +520,15 @@ func BenchmarkEncodeValue(b *testing.B) {
}
}
}

func (s *testTableCodecSuite) TestError(c *C) {
kvErrs := []*terror.Error{
errInvalidKey,
errInvalidRecordKey,
errInvalidIndexKey,
}
for _, err := range kvErrs {
code := err.ToSQLError().Code
c.Assert(code != mysql.ErrUnknown && code == uint16(err.Code()), IsTrue, Commentf("err: %v", err))
}
}
27 changes: 15 additions & 12 deletions util/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,21 +432,24 @@ func iterRecords(sessCtx sessionctx.Context, retriever kv.Retriever, t table.Tab
return nil
}

// admin error codes.
const (
codeDataNotEqual terror.ErrCode = iota + 1
codeDDLJobNotFound
codeCancelFinishedJob
codeCannotCancelDDLJob
)

var (
// ErrDataInConsistent indicate that meets inconsistent data.
ErrDataInConsistent = terror.ClassAdmin.New(codeDataNotEqual, "data isn't equal")
ErrDataInConsistent = terror.ClassAdmin.New(mysql.ErrDataInConsistent, mysql.MySQLErrName[mysql.ErrDataInConsistent])
// ErrDDLJobNotFound indicates the job id was not found.
ErrDDLJobNotFound = terror.ClassAdmin.New(codeDDLJobNotFound, "DDL Job:%v not found")
ErrDDLJobNotFound = terror.ClassAdmin.New(mysql.ErrDDLJobNotFound, mysql.MySQLErrName[mysql.ErrDDLJobNotFound])
// ErrCancelFinishedDDLJob returns when cancel a finished ddl job.
ErrCancelFinishedDDLJob = terror.ClassAdmin.New(codeCancelFinishedJob, "This job:%v is finished, so can't be cancelled")
ErrCancelFinishedDDLJob = terror.ClassAdmin.New(mysql.ErrCancelFinishedDDLJob, mysql.MySQLErrName[mysql.ErrCancelFinishedDDLJob])
// ErrCannotCancelDDLJob returns when cancel a almost finished ddl job, because cancel in now may cause data inconsistency.
ErrCannotCancelDDLJob = terror.ClassAdmin.New(codeCannotCancelDDLJob, "This job:%v is almost finished, can't be cancelled now")
ErrCannotCancelDDLJob = terror.ClassAdmin.New(mysql.ErrCannotCancelDDLJob, mysql.MySQLErrName[mysql.ErrCannotCancelDDLJob])
)

func init() {
// Register terror to mysql error map.
mySQLErrCodes := map[terror.ErrCode]uint16{
mysql.ErrDataInConsistent: mysql.ErrDataInConsistent,
mysql.ErrDDLJobNotFound: mysql.ErrDDLJobNotFound,
mysql.ErrCancelFinishedDDLJob: mysql.ErrCancelFinishedDDLJob,
mysql.ErrCannotCancelDDLJob: mysql.ErrCannotCancelDDLJob,
}
terror.ErrClassToMySQLCodes[terror.ClassAdmin] = mySQLErrCodes
}
15 changes: 15 additions & 0 deletions util/admin/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (

. "github.com/pingcap/check"
"github.com/pingcap/parser/model"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/parser/terror"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta"
"github.com/pingcap/tidb/store/mockstore"
Expand Down Expand Up @@ -343,3 +345,16 @@ func (s *testSuite) TestIsJobRollbackable(c *C) {
c.Assert(re == ca.result, IsTrue)
}
}

func (s *testSuite) TestError(c *C) {
kvErrs := []*terror.Error{
ErrDataInConsistent,
ErrDDLJobNotFound,
ErrCancelFinishedDDLJob,
ErrCannotCancelDDLJob,
}
for _, err := range kvErrs {
code := err.ToSQLError().Code
c.Assert(code != mysql.ErrUnknown && code == uint16(err.Code()), IsTrue, Commentf("err: %v", err))
}
}
7 changes: 1 addition & 6 deletions util/ranger/points.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,7 @@ import (

// Error instances.
var (
ErrUnsupportedType = terror.ClassOptimizer.New(CodeUnsupportedType, "Unsupported type")
)

// Error codes.
const (
CodeUnsupportedType terror.ErrCode = 1
ErrUnsupportedType = terror.ClassOptimizer.New(mysql.ErrUnsupportedType, mysql.MySQLErrName[mysql.ErrUnsupportedType])
)

// RangeType is alias for int.
Expand Down

0 comments on commit cbf7415

Please sign in to comment.