More efficient autoid rebase methord (#1631)
Prevent update kv everytime when rebase autoid
This commit is contained in:
2
Makefile
2
Makefile
@ -13,7 +13,7 @@ ARCH := "`uname -s`"
|
||||
LINUX := "Linux"
|
||||
MAC := "Darwin"
|
||||
PACKAGES := $$(go list ./...| grep -vE 'vendor')
|
||||
FILES := $$(find . | grep -vE 'vendor'| grep '\.go')
|
||||
FILES := $$(find . -name '*.go' | grep -vE 'vendor')
|
||||
|
||||
LDFLAGS += -X "github.com/pingcap/tidb/util/printer.TiDBBuildTS=$(shell date -u '+%Y-%m-%d %I:%M:%S')"
|
||||
LDFLAGS += -X "github.com/pingcap/tidb/util/printer.TiDBGitHash=$(shell git rev-parse HEAD)"
|
||||
|
||||
@ -904,7 +904,7 @@ func (d *ddl) handleAutoIncID(tbInfo *model.TableInfo, schemaID int64) error {
|
||||
// The operation of the minus 1 to make sure that the current value doesn't be used,
|
||||
// the next Alloc operation will get this value.
|
||||
// Its behavior is consistent with MySQL.
|
||||
if err = tb.RebaseAutoID(tbInfo.AutoIncID-1, false); err != nil {
|
||||
if err = tb.RebaseAutoID(tbInfo.AutoIncID-1, false, true); err != nil {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
return nil
|
||||
|
||||
@ -186,7 +186,7 @@ func updateRecord(ctx context.Context, h int64, oldData, newData []types.Datum,
|
||||
if err != nil {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
t.RebaseAutoID(val, true)
|
||||
t.RebaseAutoID(val, true, true)
|
||||
}
|
||||
|
||||
touched[colIndex] = true
|
||||
@ -419,6 +419,10 @@ type InsertValues struct {
|
||||
Lists [][]ast.ExprNode
|
||||
Setlist []*ast.Assignment
|
||||
IsPrepare bool
|
||||
|
||||
// Used for rebase autoinc id.
|
||||
needRebase bool
|
||||
rebaseID int64
|
||||
}
|
||||
|
||||
// InsertExec represents an insert executor.
|
||||
@ -466,6 +470,9 @@ func (e *InsertExec) Next() (*Row, error) {
|
||||
if err != nil {
|
||||
return nil, errors.Trace(err)
|
||||
}
|
||||
if e.needRebase {
|
||||
e.Table.RebaseAutoID(e.rebaseID, true, true)
|
||||
}
|
||||
|
||||
for _, row := range rows {
|
||||
if len(e.OnDuplicate) == 0 && !e.Ignore {
|
||||
@ -728,7 +735,11 @@ func (e *InsertValues) initDefaultValues(row []types.Datum, marked map[int]struc
|
||||
return errors.Trace(err)
|
||||
}
|
||||
if val != 0 {
|
||||
e.Table.RebaseAutoID(val, true)
|
||||
e.needRebase = true
|
||||
if e.rebaseID < val {
|
||||
e.rebaseID = val
|
||||
}
|
||||
e.Table.RebaseAutoID(e.rebaseID, true, false)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
@ -40,7 +40,9 @@ type Allocator interface {
|
||||
// Rebase rebases the autoID base for table with tableID and the new base value.
|
||||
// If allocIDs is true, it will allocate some IDs and save to the cache.
|
||||
// If allocIDs is false, it will not allocate IDs.
|
||||
Rebase(tableID, newBase int64, allocIDs bool) error
|
||||
// If updateKV is true, allocator should update global kv if necessary.
|
||||
// If updateKV is false, allocator will not update global kv.
|
||||
Rebase(tableID, newBase int64, allocIDs bool, updateKV bool) error
|
||||
}
|
||||
|
||||
type allocator struct {
|
||||
@ -57,7 +59,7 @@ func GetStep() int64 {
|
||||
}
|
||||
|
||||
// Rebase implements autoid.Allocator Rebase interface.
|
||||
func (alloc *allocator) Rebase(tableID, newBase int64, allocIDs bool) error {
|
||||
func (alloc *allocator) Rebase(tableID, newBase int64, allocIDs bool, updateKV bool) error {
|
||||
if tableID == 0 {
|
||||
return errInvalidTableID.Gen("Invalid tableID")
|
||||
}
|
||||
@ -71,6 +73,13 @@ func (alloc *allocator) Rebase(tableID, newBase int64, allocIDs bool) error {
|
||||
alloc.base = newBase
|
||||
return nil
|
||||
}
|
||||
if !updateKV {
|
||||
alloc.base = newBase
|
||||
if newBase > alloc.end {
|
||||
alloc.end = newBase
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
return kv.RunInNewTxn(alloc.store, true, func(txn kv.Transaction) error {
|
||||
m := meta.NewMeta(txn)
|
||||
@ -151,7 +160,7 @@ type memoryAllocator struct {
|
||||
}
|
||||
|
||||
// Rebase implements autoid.Allocator Rebase interface.
|
||||
func (alloc *memoryAllocator) Rebase(tableID, newBase int64, allocIDs bool) error {
|
||||
func (alloc *memoryAllocator) Rebase(tableID, newBase int64, allocIDs bool, updateKV bool) error {
|
||||
// TODO: implement it.
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -67,22 +67,22 @@ func (*testSuite) TestT(c *C) {
|
||||
c.Assert(err, NotNil)
|
||||
|
||||
// rebase
|
||||
err = alloc.Rebase(1, int64(1), true)
|
||||
err = alloc.Rebase(1, int64(1), true, true)
|
||||
c.Assert(err, IsNil)
|
||||
id, err = alloc.Alloc(1)
|
||||
c.Assert(err, IsNil)
|
||||
c.Assert(id, Equals, int64(3))
|
||||
err = alloc.Rebase(1, int64(3), true)
|
||||
err = alloc.Rebase(1, int64(3), true, true)
|
||||
c.Assert(err, IsNil)
|
||||
id, err = alloc.Alloc(1)
|
||||
c.Assert(err, IsNil)
|
||||
c.Assert(id, Equals, int64(4))
|
||||
err = alloc.Rebase(1, int64(10), true)
|
||||
err = alloc.Rebase(1, int64(10), true, true)
|
||||
c.Assert(err, IsNil)
|
||||
id, err = alloc.Alloc(1)
|
||||
c.Assert(err, IsNil)
|
||||
c.Assert(id, Equals, int64(11))
|
||||
err = alloc.Rebase(1, int64(3010), true)
|
||||
err = alloc.Rebase(1, int64(3010), true, true)
|
||||
c.Assert(err, IsNil)
|
||||
id, err = alloc.Alloc(1)
|
||||
c.Assert(err, IsNil)
|
||||
@ -96,7 +96,7 @@ func (*testSuite) TestT(c *C) {
|
||||
|
||||
alloc = autoid.NewAllocator(store, 1)
|
||||
c.Assert(alloc, NotNil)
|
||||
err = alloc.Rebase(2, int64(1), false)
|
||||
err = alloc.Rebase(2, int64(1), false, true)
|
||||
c.Assert(err, IsNil)
|
||||
id, err = alloc.Alloc(2)
|
||||
c.Assert(err, IsNil)
|
||||
@ -104,16 +104,16 @@ func (*testSuite) TestT(c *C) {
|
||||
|
||||
alloc = autoid.NewAllocator(store, 1)
|
||||
c.Assert(alloc, NotNil)
|
||||
err = alloc.Rebase(3, int64(3210), false)
|
||||
err = alloc.Rebase(3, int64(3210), false, true)
|
||||
c.Assert(err, IsNil)
|
||||
alloc = autoid.NewAllocator(store, 1)
|
||||
c.Assert(alloc, NotNil)
|
||||
err = alloc.Rebase(3, int64(3000), false)
|
||||
err = alloc.Rebase(3, int64(3000), false, true)
|
||||
c.Assert(err, IsNil)
|
||||
id, err = alloc.Alloc(3)
|
||||
c.Assert(err, IsNil)
|
||||
c.Assert(id, Equals, int64(3211))
|
||||
err = alloc.Rebase(3, int64(6543), false)
|
||||
err = alloc.Rebase(3, int64(6543), false, true)
|
||||
c.Assert(err, IsNil)
|
||||
id, err = alloc.Alloc(3)
|
||||
c.Assert(err, IsNil)
|
||||
|
||||
@ -109,7 +109,9 @@ type Table interface {
|
||||
// RebaseAutoID rebases the auto_increment ID base.
|
||||
// If allocIDs is true, it will allocate some IDs and save to the cache.
|
||||
// If allocIDs is false, it will not allocate IDs.
|
||||
RebaseAutoID(newBase int64, allocIDs bool) error
|
||||
// If updateKV is true, allocator should update global kv if necessary.
|
||||
// If updateKV is false, allocator will not update global kv.
|
||||
RebaseAutoID(newBase int64, allocIDs bool, updateKV bool) error
|
||||
|
||||
// Meta returns TableInfo.
|
||||
Meta() *model.TableInfo
|
||||
|
||||
@ -277,8 +277,8 @@ func (t *BoundedTable) AllocAutoID() (int64, error) {
|
||||
}
|
||||
|
||||
// RebaseAutoID implements table.Table RebaseAutoID interface.
|
||||
func (t *BoundedTable) RebaseAutoID(newBase int64, isSetStep bool) error {
|
||||
return t.alloc.Rebase(t.ID, newBase, isSetStep)
|
||||
func (t *BoundedTable) RebaseAutoID(newBase int64, isSetStep bool, updateKV bool) error {
|
||||
return t.alloc.Rebase(t.ID, newBase, isSetStep, updateKV)
|
||||
}
|
||||
|
||||
// IterRecords implements table.Table IterRecords interface.
|
||||
|
||||
@ -249,8 +249,8 @@ func (t *MemoryTable) AllocAutoID() (int64, error) {
|
||||
}
|
||||
|
||||
// RebaseAutoID implements table.Table RebaseAutoID interface.
|
||||
func (t *MemoryTable) RebaseAutoID(newBase int64, isSetStep bool) error {
|
||||
return t.alloc.Rebase(t.ID, newBase, isSetStep)
|
||||
func (t *MemoryTable) RebaseAutoID(newBase int64, isSetStep bool, updateKV bool) error {
|
||||
return t.alloc.Rebase(t.ID, newBase, isSetStep, updateKV)
|
||||
}
|
||||
|
||||
// IterRecords implements table.Table IterRecords interface.
|
||||
|
||||
@ -643,8 +643,8 @@ func (t *Table) AllocAutoID() (int64, error) {
|
||||
}
|
||||
|
||||
// RebaseAutoID implements table.Table RebaseAutoID interface.
|
||||
func (t *Table) RebaseAutoID(newBase int64, isSetStep bool) error {
|
||||
return t.alloc.Rebase(t.ID, newBase, isSetStep)
|
||||
func (t *Table) RebaseAutoID(newBase int64, isSetStep bool, updateKV bool) error {
|
||||
return t.alloc.Rebase(t.ID, newBase, isSetStep, updateKV)
|
||||
}
|
||||
|
||||
// Seek implements table.Table Seek interface.
|
||||
|
||||
Reference in New Issue
Block a user