Skip to content

Commit

Permalink
executor: return error if autoid overflows shard bits. (#8936) (#8976)
Browse files Browse the repository at this point in the history
  • Loading branch information
winkyao authored and zz-jason committed Jan 8, 2019
1 parent 936b988 commit f788f7c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
19 changes: 19 additions & 0 deletions executor/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/pingcap/parser/mysql"
"github.com/pingcap/parser/terror"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/meta/autoid"
plannercore "github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/table"
Expand Down Expand Up @@ -406,6 +407,24 @@ func (s *testSuite) TestShardRowIDBits(c *C) {
_, err = tk.Exec("alter table auto shard_row_id_bits = 4")
c.Assert(err, NotNil)
tk.MustExec("alter table auto shard_row_id_bits = 0")

// Test overflow
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t1 (a int) shard_row_id_bits = 15")
defer tk.MustExec("drop table if exists t1")

tbl, err = domain.GetDomain(tk.Se).InfoSchema().TableByName(model.NewCIStr("test"), model.NewCIStr("t1"))
c.Assert(err, IsNil)
maxID := 1<<(64-15-1) - 1
err = tbl.RebaseAutoID(tk.Se, int64(maxID)-1, false)
c.Assert(err, IsNil)
tk.MustExec("insert into t1 values(1)")

// continue inserting will fail.
_, err = tk.Exec("insert into t1 values(2)")
c.Assert(autoid.ErrAutoincReadFailed.Equal(err), IsTrue, Commentf("err:%v", err))
_, err = tk.Exec("insert into t1 values(3)")
c.Assert(autoid.ErrAutoincReadFailed.Equal(err), IsTrue, Commentf("err:%v", err))
}

func (s *testSuite) TestMaxHandleAddIndex(c *C) {
Expand Down
32 changes: 32 additions & 0 deletions meta/autoid/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2018 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package autoid

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

// Error instances.
var (
ErrAutoincReadFailed = terror.ClassAutoid.New(mysql.ErrAutoincReadFailed, mysql.MySQLErrName[mysql.ErrAutoincReadFailed])
)

func init() {
// Map error codes to mysql error codes.
tableMySQLErrCodes := map[terror.ErrCode]uint16{
mysql.ErrAutoincReadFailed: mysql.ErrAutoincReadFailed,
}
terror.ErrClassToMySQLCodes[terror.ClassAutoid] = tableMySQLErrCodes
}
16 changes: 16 additions & 0 deletions table/tables/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,16 @@ func (t *tableCommon) AllocAutoID(ctx sessionctx.Context) (int64, error) {
return 0, errors.Trace(err)
}
if t.meta.ShardRowIDBits > 0 {
if t.overflowShardBits(rowID) {
// If overflow, the rowID may be duplicated. For examples,
// t.meta.ShardRowIDBits = 4
// rowID = 0010111111111111111111111111111111111111111111111111111111111111
// shard = 01000000000000000000000000000000000000000000000000000000000000000
// will be duplicated with:
// rowID = 0100111111111111111111111111111111111111111111111111111111111111
// shard = 0010000000000000000000000000000000000000000000000000000000000000
return 0, autoid.ErrAutoincReadFailed
}
txnCtx := ctx.GetSessionVars().TxnCtx
if txnCtx.Shard == nil {
shard := t.calcShard(txnCtx.StartTS)
Expand All @@ -919,6 +929,12 @@ func (t *tableCommon) AllocAutoID(ctx sessionctx.Context) (int64, error) {
return rowID, nil
}

// overflowShardBits check whether the rowID overflow `1<<(64-t.meta.ShardRowIDBits-1) -1`.
func (t *tableCommon) overflowShardBits(rowID int64) bool {
mask := (1<<t.meta.ShardRowIDBits - 1) << (64 - t.meta.ShardRowIDBits - 1)
return rowID&int64(mask) > 0
}

func (t *tableCommon) calcShard(startTS uint64) int64 {
var buf [8]byte
binary.LittleEndian.PutUint64(buf[:], startTS)
Expand Down

0 comments on commit f788f7c

Please sign in to comment.