diff --git a/executor/admin_test.go b/executor/admin_test.go index 568b37bc6484a..ba4e9606b901b 100644 --- a/executor/admin_test.go +++ b/executor/admin_test.go @@ -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: != 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: != 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") @@ -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: != 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: != 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")) diff --git a/go.mod b/go.mod index f0de1a8ba6e9f..d299d4e4d6e07 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 7b7a8df8cdcdf..6b74cbcb8d722 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/meta/meta_test.go b/meta/meta_test.go index 8c8c23f0e0b57..61b52e2eee4eb 100644 --- a/meta/meta_test.go +++ b/meta/meta_test.go @@ -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) { diff --git a/structure/hash.go b/structure/hash.go index 8d16b626b7520..9d19a05dbdf58 100644 --- a/structure/hash.go +++ b/structure/hash.go @@ -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 @@ -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) { @@ -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) @@ -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])) diff --git a/structure/list.go b/structure/list.go index 079a5d2765a23..c55109a197b56 100644 --- a/structure/list.go +++ b/structure/list.go @@ -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 @@ -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) @@ -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) @@ -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) @@ -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])) diff --git a/structure/string.go b/structure/string.go index b00082e74ea9f..57a1fa4ec2dfe 100644 --- a/structure/string.go +++ b/structure/string.go @@ -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) @@ -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. @@ -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) diff --git a/structure/structure.go b/structure/structure.go index b736ec9daf981..7617e08eb7813 100644 --- a/structure/structure.go +++ b/structure/structure.go @@ -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. @@ -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 +} diff --git a/structure/structure_test.go b/structure/structure_test.go index e6e55fcf5dfef..62fdeb8f60159 100644 --- a/structure/structure_test.go +++ b/structure/structure_test.go @@ -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" @@ -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)) + } +} diff --git a/structure/type.go b/structure/type.go index 7096d70e86984..3411aad81703d 100644 --- a/structure/type.go +++ b/structure/type.go @@ -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) diff --git a/tablecodec/tablecodec.go b/tablecodec/tablecodec.go index f25f0a88a272b..8e982f6295b9e 100644 --- a/tablecodec/tablecodec.go +++ b/tablecodec/tablecodec.go @@ -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 ( @@ -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 +} diff --git a/tablecodec/tablecodec_test.go b/tablecodec/tablecodec_test.go index 94f2a363c8e81..a71b62b161ea4 100644 --- a/tablecodec/tablecodec_test.go +++ b/tablecodec/tablecodec_test.go @@ -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" @@ -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)) + } +} diff --git a/util/admin/admin.go b/util/admin/admin.go index fb5f302d4828f..d8cf0f7c243ba 100644 --- a/util/admin/admin.go +++ b/util/admin/admin.go @@ -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 +} diff --git a/util/admin/admin_test.go b/util/admin/admin_test.go index 5aede319c0c9a..d40c0f4b9cd9b 100644 --- a/util/admin/admin_test.go +++ b/util/admin/admin_test.go @@ -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" @@ -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)) + } +} diff --git a/util/ranger/points.go b/util/ranger/points.go index b9f3a41eb5298..ed1a8d198067c 100644 --- a/util/ranger/points.go +++ b/util/ranger/points.go @@ -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.