Skip to content

Commit 72a4436

Browse files
tiancaiamaoti-chi-bot
authored andcommitted
This is an automated cherry-pick of pingcap#52626
Signed-off-by: ti-chi-bot <[email protected]>
1 parent 8b15640 commit 72a4436

File tree

5 files changed

+828
-4
lines changed

5 files changed

+828
-4
lines changed

autoid_service/autoid.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func (alloc *autoIDValue) rebase4Unsigned(ctx context.Context,
198198
return nil
199199
}
200200
// Satisfied by alloc.end, need to update alloc.base.
201-
if requiredBase <= uint64(alloc.end) {
201+
if requiredBase > uint64(alloc.base) && requiredBase <= uint64(alloc.end) {
202202
alloc.base = int64(requiredBase)
203203
return nil
204204
}
@@ -241,7 +241,7 @@ func (alloc *autoIDValue) rebase4Signed(ctx context.Context, store kv.Storage, d
241241
return nil
242242
}
243243
// Satisfied by alloc.end, need to update alloc.base.
244-
if requiredBase <= alloc.end {
244+
if requiredBase > alloc.base && requiredBase <= alloc.end {
245245
alloc.base = requiredBase
246246
return nil
247247
}
@@ -491,6 +491,7 @@ func (s *Service) allocAutoID(ctx context.Context, req *autoid.AutoIDRequest) (*
491491
if err1 != nil {
492492
return err1
493493
}
494+
val.base = currentEnd
494495
val.end = currentEnd
495496
return nil
496497
})

executor/autoidtest/BUILD.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ go_test(
99
],
1010
flaky = True,
1111
race = "on",
12+
<<<<<<< HEAD:executor/autoidtest/BUILD.bazel
1213
shard_count = 10,
14+
=======
15+
shard_count = 5,
16+
>>>>>>> f5e591d107b (*: fix 'Duplicate entry' error when @@auto_increment_increment and @@auto_increment_offset is set (#52626)):pkg/executor/test/autoidtest/BUILD.bazel
1317
deps = [
1418
"//autoid_service",
1519
"//config",

executor/autoidtest/autoid_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,7 @@ func TestIssue39528(t *testing.T) {
768768
require.False(t, codeRun)
769769
}
770770

771+
<<<<<<< HEAD:executor/autoidtest/autoid_test.go
771772
func TestAutoIDConstraint(t *testing.T) {
772773
// Remove the constraint that auto id column must be defined as a key
773774
// See https://github.com/pingcap/tidb/issues/40580
@@ -805,4 +806,44 @@ func TestAutoIDConstraint(t *testing.T) {
805806
// Cover case: create table with auto id column as key, and remove it later
806807
tk.MustExec("create table tt2 (id int, c int auto_increment, key c_idx(c))")
807808
tk.MustExec("alter table tt2 drop index c_idx")
809+
=======
810+
func TestIssue52622(t *testing.T) {
811+
store := testkit.CreateMockStore(t)
812+
813+
tk := testkit.NewTestKit(t, store)
814+
tk.MustExec("use test")
815+
tk.MustExec(`set @@auto_increment_increment = 66;`)
816+
tk.MustExec(`set @@auto_increment_offset = 9527;`)
817+
818+
tk.MustQuery(`select @@auto_increment_increment;`).Check(testkit.Rows("66"))
819+
tk.MustQuery(`select @@auto_increment_offset;`).Check(testkit.Rows("9527"))
820+
821+
for i := 0; i < 2; i++ {
822+
createTableSQL := "create table issue52622 (id int primary key auto_increment, k int)"
823+
if i == 0 {
824+
createTableSQL = createTableSQL + " AUTO_ID_CACHE 1"
825+
}
826+
827+
tk.MustExec(createTableSQL)
828+
tk.MustExec("insert into issue52622 (k) values (1),(2),(3);")
829+
tk.MustQuery("select * from issue52622").Check(testkit.Rows("1 1", "67 2", "133 3"))
830+
if i == 0 {
831+
tk.MustQuery("show create table issue52622").CheckContain("134")
832+
}
833+
tk.MustExec("insert into issue52622 (k) values (4);")
834+
tk.MustQuery("select * from issue52622").Check(testkit.Rows("1 1", "67 2", "133 3", "199 4"))
835+
836+
tk.MustExec("truncate table issue52622;")
837+
tk.MustExec("insert into issue52622 (k) values (1)")
838+
tk.MustExec("insert into issue52622 (k) values (2)")
839+
tk.MustExec("insert into issue52622 (k) values (3)")
840+
if i == 0 {
841+
tk.MustQuery("show create table issue52622").CheckContain("134")
842+
}
843+
tk.MustExec("insert into issue52622 (k) values (4);")
844+
tk.MustQuery("select * from issue52622").Check(testkit.Rows("1 1", "67 2", "133 3", "199 4"))
845+
846+
tk.MustExec("drop table issue52622;")
847+
}
848+
>>>>>>> f5e591d107b (*: fix 'Duplicate entry' error when @@auto_increment_increment and @@auto_increment_offset is set (#52626)):pkg/executor/test/autoidtest/autoid_test.go
808849
}

table/table.go

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"context"
2323
"time"
2424

25+
<<<<<<< HEAD:table/table.go
2526
mysql "github.com/pingcap/tidb/errno"
2627
"github.com/pingcap/tidb/kv"
2728
"github.com/pingcap/tidb/meta/autoid"
@@ -31,6 +32,20 @@ import (
3132
"github.com/pingcap/tidb/util/dbterror"
3233
"github.com/pingcap/tidb/util/sqlexec"
3334
"github.com/pingcap/tidb/util/tracing"
35+
=======
36+
mysql "github.com/pingcap/tidb/pkg/errno"
37+
"github.com/pingcap/tidb/pkg/expression"
38+
"github.com/pingcap/tidb/pkg/kv"
39+
"github.com/pingcap/tidb/pkg/meta/autoid"
40+
"github.com/pingcap/tidb/pkg/parser/model"
41+
"github.com/pingcap/tidb/pkg/sessionctx"
42+
"github.com/pingcap/tidb/pkg/sessionctx/variable"
43+
tbctx "github.com/pingcap/tidb/pkg/table/context"
44+
"github.com/pingcap/tidb/pkg/types"
45+
"github.com/pingcap/tidb/pkg/util/dbterror"
46+
"github.com/pingcap/tidb/pkg/util/sqlexec"
47+
"github.com/pingcap/tidb/pkg/util/tracing"
48+
>>>>>>> f5e591d107b (*: fix 'Duplicate entry' error when @@auto_increment_increment and @@auto_increment_offset is set (#52626)):pkg/table/table.go
3449
)
3550

3651
// Type is used to distinguish between different tables that store data in different ways.
@@ -204,13 +219,30 @@ type Table interface {
204219
GetPartitionedTable() PartitionedTable
205220
}
206221

222+
func getIncrementAndOffset(vars *variable.SessionVars) (int, int) {
223+
increment := vars.AutoIncrementIncrement
224+
offset := vars.AutoIncrementOffset
225+
// When the value of auto_increment_offset is greater than that of auto_increment_increment,
226+
// the value of auto_increment_offset is ignored.
227+
// Ref https://dev.mysql.com/doc/refman/8.0/en/replication-options-source.html
228+
if offset > increment {
229+
offset = 1
230+
}
231+
return increment, offset
232+
}
233+
207234
// AllocAutoIncrementValue allocates an auto_increment value for a new row.
208235
func AllocAutoIncrementValue(ctx context.Context, t Table, sctx sessionctx.Context) (int64, error) {
209236
r, ctx := tracing.StartRegionEx(ctx, "table.AllocAutoIncrementValue")
210237
defer r.End()
238+
<<<<<<< HEAD:table/table.go
211239
increment := sctx.GetSessionVars().AutoIncrementIncrement
212240
offset := sctx.GetSessionVars().AutoIncrementOffset
213241
alloc := t.Allocators(sctx).Get(autoid.AutoIncrementType)
242+
=======
243+
increment, offset := getIncrementAndOffset(sctx.GetSessionVars())
244+
alloc := t.Allocators(sctx.GetTableCtx()).Get(autoid.AutoIncrementType)
245+
>>>>>>> f5e591d107b (*: fix 'Duplicate entry' error when @@auto_increment_increment and @@auto_increment_offset is set (#52626)):pkg/table/table.go
214246
_, max, err := alloc.Alloc(ctx, uint64(1), int64(increment), int64(offset))
215247
if err != nil {
216248
return 0, err
@@ -220,18 +252,25 @@ func AllocAutoIncrementValue(ctx context.Context, t Table, sctx sessionctx.Conte
220252

221253
// AllocBatchAutoIncrementValue allocates batch auto_increment value for rows, returning firstID, increment and err.
222254
// The caller can derive the autoID by adding increment to firstID for N-1 times.
255+
<<<<<<< HEAD:table/table.go
223256
func AllocBatchAutoIncrementValue(ctx context.Context, t Table, sctx sessionctx.Context, N int) (firstID int64, increment int64, err error) {
224257
increment = int64(sctx.GetSessionVars().AutoIncrementIncrement)
225258
offset := int64(sctx.GetSessionVars().AutoIncrementOffset)
226259
alloc := t.Allocators(sctx).Get(autoid.AutoIncrementType)
227260
min, max, err := alloc.Alloc(ctx, uint64(N), increment, offset)
261+
=======
262+
func AllocBatchAutoIncrementValue(ctx context.Context, t Table, sctx sessionctx.Context, N int) ( /* firstID */ int64 /* increment */, int64 /* err */, error) {
263+
increment1, offset := getIncrementAndOffset(sctx.GetSessionVars())
264+
alloc := t.Allocators(sctx.GetTableCtx()).Get(autoid.AutoIncrementType)
265+
min, max, err := alloc.Alloc(ctx, uint64(N), int64(increment1), int64(offset))
266+
>>>>>>> f5e591d107b (*: fix 'Duplicate entry' error when @@auto_increment_increment and @@auto_increment_offset is set (#52626)):pkg/table/table.go
228267
if err != nil {
229268
return min, max, err
230269
}
231270
// SeekToFirstAutoIDUnSigned seeks to first autoID. Because AutoIncrement always allocate from 1,
232271
// signed and unsigned value can be unified as the unsigned handle.
233-
nr := int64(autoid.SeekToFirstAutoIDUnSigned(uint64(min), uint64(increment), uint64(offset)))
234-
return nr, increment, nil
272+
nr := int64(autoid.SeekToFirstAutoIDUnSigned(uint64(min), uint64(increment1), uint64(offset)))
273+
return nr, int64(increment1), nil
235274
}
236275

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

0 commit comments

Comments
 (0)