Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: refactor table.Allocator to improve readability #17238

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5adf901
validate auto_increment_{increment|offset} session variables
tangenta May 13, 2020
596dce1
refactor Alloc() and Rebase() in allocator to reduce code duplication
tangenta May 14, 2020
585e606
Enhance the usability of Allocator.Alloc()
tangenta May 14, 2020
cb42200
make format
tangenta May 14, 2020
ee79d4f
improve one-by-one allocation performance
tangenta May 14, 2020
2466252
improve readability of allocator.alloc()
tangenta May 15, 2020
468c176
remove unused code snippet
tangenta May 15, 2020
51b7b0d
make auto_random aware auto_increment_{increment|offset}
tangenta May 15, 2020
ed010f3
add a failpoint to skip increment and offset validation
tangenta May 15, 2020
8f0fe5c
add comments about IDIterator
tangenta May 15, 2020
22f4411
Merge branch 'master' into refactor-allocator
AilinKid May 15, 2020
f78f7c6
Merge branch 'master' into refactor-allocator
AilinKid May 18, 2020
544dd2d
clean up code
tangenta May 18, 2020
d550ac6
Merge branch 'master' into refactor-allocator
AilinKid May 18, 2020
aabbdcf
update comments
tangenta May 19, 2020
b94569f
Merge branch 'master' into refactor-allocator
AilinKid May 20, 2020
160f812
address comment
tangenta May 21, 2020
4465755
remove unrelated code
tangenta Jun 3, 2020
6b87b49
Merge remote-tracking branch 'upstream/master' into refactor-allocator
tangenta Jun 19, 2020
134f514
adjust return type order
tangenta Jun 19, 2020
bba214d
Merge remote-tracking branch 'upstream/master' into refactor-allocator
tangenta Oct 14, 2020
d374505
address comments
tangenta Oct 14, 2020
ebbc73c
make fmt
tangenta Oct 14, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions ddl/serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"context"
"fmt"
"math"
"strconv"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -950,6 +951,66 @@ func (s *testSerialSuite) TestAutoRandom(c *C) {
assertExperimentDisabled("create table auto_random_table (a int primary key auto_random(3))")
}

func (s *testSerialSuite) TestAutoRandomIncBitsIncrementAndOffset(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("create database if not exists auto_random_db")
defer tk.MustExec("drop database if exists auto_random_db")
tk.MustExec("use auto_random_db")
tk.MustExec("drop table if exists t")

testutil.ConfigTestUtils.SetupAutoRandomTestConfig()
defer testutil.ConfigTestUtils.RestoreAutoRandomTestConfig()

recreateTable := func() {
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a bigint auto_random(6) primary key)")
}
truncateTable := func() {
_, _ = tk.Exec("delete from t")
}
insertTable := func() {
tk.MustExec("insert into t values ()")
}
assertIncBitsValues := func(values ...int) {
mask := strings.Repeat("1", 64-1-6)
sql := fmt.Sprintf(`select a & b'%s' from t order by a & b'%s' asc`, mask, mask)
vs := make([]string, len(values))
for i, value := range values {
vs[i] = strconv.Itoa(value)
}
tk.MustQuery(sql).Check(testkit.Rows(vs...))
}

const truncate, recreate = true, false
expect := func(vs ...int) []int { return vs }
testCase := []struct {
setupAction bool // truncate or recreate
increment int // @@auto_increment_increment
offset int // @@auto_increment_offset
results []int // the implicit allocated auto_random incremental-bit part of values
}{
{recreate, 5, 10, expect(10, 15, 20)},
{recreate, 2, 10, expect(10, 12, 14)},
{truncate, 5, 10, expect(15, 20, 25)},
{truncate, 10, 10, expect(30, 40, 50)},
{truncate, 5, 10, expect(55, 60, 65)},
}
for _, tc := range testCase {
switch tc.setupAction {
case recreate:
recreateTable()
case truncate:
truncateTable()
}
tk.Se.GetSessionVars().AutoIncrementIncrement = tc.increment
tk.Se.GetSessionVars().AutoIncrementOffset = tc.offset
for range tc.results {
insertTable()
}
assertIncBitsValues(tc.results...)
}
}

func (s *testSerialSuite) TestModifyingColumn4NewCollations(c *C) {
collate.SetNewCollationEnabledForTest(true)
defer collate.SetNewCollationEnabledForTest(false)
Expand Down
23 changes: 13 additions & 10 deletions executor/insert_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,23 +702,23 @@ func (e *InsertValues) lazyAdjustAutoIncrementDatum(ctx context.Context, rows []
i++
cnt++
}
// AllocBatchAutoIncrementValue allocates batch N consecutive autoIDs.
// The max value can be derived from adding the increment value to min for cnt-1 times.
min, increment, err := table.AllocBatchAutoIncrementValue(ctx, e.Table, e.ctx, cnt)
idIter, err := table.AllocBatchAutoIncrementValue(ctx, e.Table, e.ctx, cnt)
if e.handleErr(col, &autoDatum, cnt, err) != nil {
return nil, err
}
// It's compatible with mysql setting the first allocated autoID to lastInsertID.
// Cause autoID may be specified by user, judge only the first row is not suitable.
if e.lastInsertID == 0 {
e.lastInsertID = uint64(min)
}
// Assign autoIDs to rows.
for j := 0; j < cnt; j++ {
offset := j + start
d := rows[offset][colIdx]

id := int64(uint64(min) + uint64(j)*uint64(increment))
_, id := idIter.Next()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first return value is ignored, it may be false.


// It's compatible with mysql setting the first allocated autoID to lastInsertID.
// Cause autoID may be specified by user, judge only the first row is not suitable.
if j == 0 && e.lastInsertID == 0 {
e.lastInsertID = uint64(id)
}

d.SetAutoID(id, col.Flag)
retryInfo.AddAutoIncrementID(id)

Expand Down Expand Up @@ -889,10 +889,13 @@ func (e *InsertValues) adjustAutoRandomDatum(ctx context.Context, d types.Datum,
func (e *InsertValues) allocAutoRandomID(fieldType *types.FieldType) (int64, error) {
alloc := e.Table.Allocators(e.ctx).Get(autoid.AutoRandomType)
tableInfo := e.Table.Meta()
_, autoRandomID, err := alloc.Alloc(tableInfo.ID, 1, 1, 1)
increment := e.ctx.GetSessionVars().AutoIncrementIncrement
offset := e.ctx.GetSessionVars().AutoIncrementOffset
idIter, err := alloc.Alloc(tableInfo.ID, 1, int64(increment), int64(offset))
if err != nil {
return 0, err
}
autoRandomID := idIter.First()

layout := autoid.NewAutoRandomIDLayout(fieldType, tableInfo.AutoRandomBits)
if tables.OverflowShardBits(autoRandomID, tableInfo.AutoRandomBits, layout.TypeBitsLength, layout.HasSignBit) {
Expand Down
Loading