Skip to content

Commit

Permalink
This is an automated cherry-pick of pingcap#52626
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
  • Loading branch information
tiancaiamao authored and ti-chi-bot committed May 7, 2024
1 parent 1e19051 commit bdf2bf2
Show file tree
Hide file tree
Showing 5 changed files with 818 additions and 4 deletions.
5 changes: 3 additions & 2 deletions pkg/autoid_service/autoid.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (alloc *autoIDValue) rebase4Unsigned(ctx context.Context,
return nil
}
// Satisfied by alloc.end, need to update alloc.base.
if requiredBase <= uint64(alloc.end) {
if requiredBase > uint64(alloc.base) && requiredBase <= uint64(alloc.end) {
alloc.base = int64(requiredBase)
return nil
}
Expand Down Expand Up @@ -227,7 +227,7 @@ func (alloc *autoIDValue) rebase4Signed(ctx context.Context, store kv.Storage, d
return nil
}
// Satisfied by alloc.end, need to update alloc.base.
if requiredBase <= alloc.end {
if requiredBase > alloc.base && requiredBase <= alloc.end {
alloc.base = requiredBase
return nil
}
Expand Down Expand Up @@ -473,6 +473,7 @@ func (s *Service) allocAutoID(ctx context.Context, req *autoid.AutoIDRequest) (*
if err1 != nil {
return err1
}
val.base = currentEnd
val.end = currentEnd
return nil
})
Expand Down
4 changes: 4 additions & 0 deletions pkg/executor/test/autoidtest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ go_test(
],
flaky = True,
race = "on",
<<<<<<< HEAD
shard_count = 10,
=======
shard_count = 5,
>>>>>>> f5e591d107b (*: fix 'Duplicate entry' error when @@auto_increment_increment and @@auto_increment_offset is set (#52626))
deps = [
"//pkg/autoid_service",
"//pkg/config",
Expand Down
41 changes: 41 additions & 0 deletions pkg/executor/test/autoidtest/autoid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,7 @@ func TestIssue39528(t *testing.T) {
require.False(t, codeRun)
}

<<<<<<< HEAD
func TestAutoIDConstraint(t *testing.T) {
// Remove the constraint that auto id column must be defined as a key
// See https://github.com/pingcap/tidb/issues/40580
Expand Down Expand Up @@ -805,4 +806,44 @@ func TestAutoIDConstraint(t *testing.T) {
// Cover case: create table with auto id column as key, and remove it later
tk.MustExec("create table tt2 (id int, c int auto_increment, key c_idx(c))")
tk.MustExec("alter table tt2 drop index c_idx")
=======
func TestIssue52622(t *testing.T) {
store := testkit.CreateMockStore(t)

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec(`set @@auto_increment_increment = 66;`)
tk.MustExec(`set @@auto_increment_offset = 9527;`)

tk.MustQuery(`select @@auto_increment_increment;`).Check(testkit.Rows("66"))
tk.MustQuery(`select @@auto_increment_offset;`).Check(testkit.Rows("9527"))

for i := 0; i < 2; i++ {
createTableSQL := "create table issue52622 (id int primary key auto_increment, k int)"
if i == 0 {
createTableSQL = createTableSQL + " AUTO_ID_CACHE 1"
}

tk.MustExec(createTableSQL)
tk.MustExec("insert into issue52622 (k) values (1),(2),(3);")
tk.MustQuery("select * from issue52622").Check(testkit.Rows("1 1", "67 2", "133 3"))
if i == 0 {
tk.MustQuery("show create table issue52622").CheckContain("134")
}
tk.MustExec("insert into issue52622 (k) values (4);")
tk.MustQuery("select * from issue52622").Check(testkit.Rows("1 1", "67 2", "133 3", "199 4"))

tk.MustExec("truncate table issue52622;")
tk.MustExec("insert into issue52622 (k) values (1)")
tk.MustExec("insert into issue52622 (k) values (2)")
tk.MustExec("insert into issue52622 (k) values (3)")
if i == 0 {
tk.MustQuery("show create table issue52622").CheckContain("134")
}
tk.MustExec("insert into issue52622 (k) values (4);")
tk.MustQuery("select * from issue52622").Check(testkit.Rows("1 1", "67 2", "133 3", "199 4"))

tk.MustExec("drop table issue52622;")
}
>>>>>>> f5e591d107b (*: fix 'Duplicate entry' error when @@auto_increment_increment and @@auto_increment_offset is set (#52626))
}
33 changes: 31 additions & 2 deletions pkg/table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ import (
"github.com/pingcap/tidb/pkg/meta/autoid"
"github.com/pingcap/tidb/pkg/parser/model"
"github.com/pingcap/tidb/pkg/sessionctx"
<<<<<<< HEAD
=======
"github.com/pingcap/tidb/pkg/sessionctx/variable"
tbctx "github.com/pingcap/tidb/pkg/table/context"
>>>>>>> f5e591d107b (*: fix 'Duplicate entry' error when @@auto_increment_increment and @@auto_increment_offset is set (#52626))
"github.com/pingcap/tidb/pkg/types"
"github.com/pingcap/tidb/pkg/util/dbterror"
"github.com/pingcap/tidb/pkg/util/sqlexec"
Expand Down Expand Up @@ -206,13 +211,30 @@ type Table interface {
GetPartitionedTable() PartitionedTable
}

func getIncrementAndOffset(vars *variable.SessionVars) (int, int) {
increment := vars.AutoIncrementIncrement
offset := vars.AutoIncrementOffset
// When the value of auto_increment_offset is greater than that of auto_increment_increment,
// the value of auto_increment_offset is ignored.
// Ref https://dev.mysql.com/doc/refman/8.0/en/replication-options-source.html
if offset > increment {
offset = 1
}
return increment, offset
}

// AllocAutoIncrementValue allocates an auto_increment value for a new row.
func AllocAutoIncrementValue(ctx context.Context, t Table, sctx sessionctx.Context) (int64, error) {
r, ctx := tracing.StartRegionEx(ctx, "table.AllocAutoIncrementValue")
defer r.End()
<<<<<<< HEAD
increment := sctx.GetSessionVars().AutoIncrementIncrement
offset := sctx.GetSessionVars().AutoIncrementOffset
alloc := t.Allocators(sctx).Get(autoid.AutoIncrementType)
=======
increment, offset := getIncrementAndOffset(sctx.GetSessionVars())
alloc := t.Allocators(sctx.GetTableCtx()).Get(autoid.AutoIncrementType)
>>>>>>> f5e591d107b (*: fix 'Duplicate entry' error when @@auto_increment_increment and @@auto_increment_offset is set (#52626))
_, max, err := alloc.Alloc(ctx, uint64(1), int64(increment), int64(offset))
if err != nil {
return 0, err
Expand All @@ -222,18 +244,25 @@ func AllocAutoIncrementValue(ctx context.Context, t Table, sctx sessionctx.Conte

// AllocBatchAutoIncrementValue allocates batch auto_increment value for rows, returning firstID, increment and err.
// The caller can derive the autoID by adding increment to firstID for N-1 times.
<<<<<<< HEAD
func AllocBatchAutoIncrementValue(ctx context.Context, t Table, sctx sessionctx.Context, N int) (firstID int64, increment int64, err error) {
increment = int64(sctx.GetSessionVars().AutoIncrementIncrement)
offset := int64(sctx.GetSessionVars().AutoIncrementOffset)
alloc := t.Allocators(sctx).Get(autoid.AutoIncrementType)
min, max, err := alloc.Alloc(ctx, uint64(N), increment, offset)
=======
func AllocBatchAutoIncrementValue(ctx context.Context, t Table, sctx sessionctx.Context, N int) ( /* firstID */ int64 /* increment */, int64 /* err */, error) {
increment1, offset := getIncrementAndOffset(sctx.GetSessionVars())
alloc := t.Allocators(sctx.GetTableCtx()).Get(autoid.AutoIncrementType)
min, max, err := alloc.Alloc(ctx, uint64(N), int64(increment1), int64(offset))
>>>>>>> f5e591d107b (*: fix 'Duplicate entry' error when @@auto_increment_increment and @@auto_increment_offset is set (#52626))
if err != nil {
return min, max, err
}
// SeekToFirstAutoIDUnSigned seeks to first autoID. Because AutoIncrement always allocate from 1,
// signed and unsigned value can be unified as the unsigned handle.
nr := int64(autoid.SeekToFirstAutoIDUnSigned(uint64(min), uint64(increment), uint64(offset)))
return nr, increment, nil
nr := int64(autoid.SeekToFirstAutoIDUnSigned(uint64(min), uint64(increment1), uint64(offset)))
return nr, int64(increment1), nil
}

// PhysicalTable is an abstraction for two kinds of table representation: partition or non-partitioned table.
Expand Down
Loading

0 comments on commit bdf2bf2

Please sign in to comment.