Skip to content

Commit

Permalink
*: Fix mysql_test and make autoid rebase method more efficient. (ping…
Browse files Browse the repository at this point in the history
…cap#1637)

* *: fix mysql_test and more efficient autoid rebase method.
  • Loading branch information
zimulala authored Aug 25, 2016
1 parent 2cec538 commit 2b5d2b1
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 45 deletions.
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, true); err != nil {
if err = tb.RebaseAutoID(tbInfo.AutoIncID-1, false); err != nil {
return errors.Trace(err)
}
return nil
Expand Down
15 changes: 2 additions & 13 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, true)
t.RebaseAutoID(val, true)
}

touched[colIndex] = true
Expand Down Expand Up @@ -419,10 +419,6 @@ 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 @@ -470,9 +466,6 @@ 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 @@ -735,11 +728,7 @@ func (e *InsertValues) initDefaultValues(row []types.Datum, marked map[int]struc
return errors.Trace(err)
}
if val != 0 {
e.needRebase = true
if e.rebaseID < val {
e.rebaseID = val
}
e.Table.RebaseAutoID(e.rebaseID, true, false)
e.Table.RebaseAutoID(val, true)
continue
}
}
Expand Down
19 changes: 5 additions & 14 deletions meta/autoid/autoid.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ 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.
// 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
Rebase(tableID, newBase int64, allocIDs bool) error
}

type allocator struct {
Expand All @@ -59,7 +57,7 @@ func GetStep() int64 {
}

// Rebase implements autoid.Allocator Rebase interface.
func (alloc *allocator) Rebase(tableID, newBase int64, allocIDs bool, updateKV bool) error {
func (alloc *allocator) Rebase(tableID, newBase int64, allocIDs bool) error {
if tableID == 0 {
return errInvalidTableID.Gen("Invalid tableID")
}
Expand All @@ -73,13 +71,6 @@ func (alloc *allocator) Rebase(tableID, newBase int64, allocIDs bool, updateKV b
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 All @@ -88,8 +79,8 @@ func (alloc *allocator) Rebase(tableID, newBase int64, allocIDs bool, updateKV b
return errors.Trace(err)
}

if newBase <= end {
return nil
if newBase < end {
newBase = end
}
newStep := newBase - end + step
if !allocIDs {
Expand Down Expand Up @@ -160,7 +151,7 @@ type memoryAllocator struct {
}

// Rebase implements autoid.Allocator Rebase interface.
func (alloc *memoryAllocator) Rebase(tableID, newBase int64, allocIDs bool, updateKV bool) error {
func (alloc *memoryAllocator) Rebase(tableID, newBase int64, allocIDs 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, true)
err = alloc.Rebase(1, int64(1), 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, true)
err = alloc.Rebase(1, int64(3), 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, true)
err = alloc.Rebase(1, int64(10), 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, true)
err = alloc.Rebase(1, int64(3010), 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, true)
err = alloc.Rebase(2, int64(1), false)
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, true)
err = alloc.Rebase(3, int64(3210), false)
c.Assert(err, IsNil)
alloc = autoid.NewAllocator(store, 1)
c.Assert(alloc, NotNil)
err = alloc.Rebase(3, int64(3000), false, true)
err = alloc.Rebase(3, int64(3000), false)
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, true)
err = alloc.Rebase(3, int64(6543), false)
c.Assert(err, IsNil)
id, err = alloc.Alloc(3)
c.Assert(err, IsNil)
Expand Down
4 changes: 1 addition & 3 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ 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.
// 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
RebaseAutoID(newBase int64, allocIDs 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, updateKV bool) error {
return t.alloc.Rebase(t.ID, newBase, isSetStep, updateKV)
func (t *BoundedTable) RebaseAutoID(newBase int64, isSetStep bool) error {
return t.alloc.Rebase(t.ID, newBase, isSetStep)
}

// 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, updateKV bool) error {
return t.alloc.Rebase(t.ID, newBase, isSetStep, updateKV)
func (t *MemoryTable) RebaseAutoID(newBase int64, isSetStep bool) error {
return t.alloc.Rebase(t.ID, newBase, isSetStep)
}

// 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, updateKV bool) error {
return t.alloc.Rebase(t.ID, newBase, isSetStep, updateKV)
func (t *Table) RebaseAutoID(newBase int64, isSetStep bool) error {
return t.alloc.Rebase(t.ID, newBase, isSetStep)
}

// Seek implements table.Table Seek interface.
Expand Down

0 comments on commit 2b5d2b1

Please sign in to comment.