Skip to content

Commit

Permalink
More efficient autoid rebase methord (pingcap#1631)
Browse files Browse the repository at this point in the history
Prevent update kv everytime when rebase autoid
  • Loading branch information
shenli authored Aug 24, 2016
1 parent 06b2a64 commit fa06043
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down
2 changes: 1 addition & 1 deletion ddl/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions executor/executor_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
}
Expand Down
15 changes: 12 additions & 3 deletions meta/autoid/autoid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
}
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
16 changes: 8 additions & 8 deletions meta/autoid/autoid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -96,24 +96,24 @@ 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)
c.Assert(id, Equals, int64(2))

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)
Expand Down
4 changes: 3 additions & 1 deletion table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions table/tables/bounded_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions table/tables/memory_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions table/tables/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit fa06043

Please sign in to comment.