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

ddl,executor: enhance test for auto_increment with AUTO_ID_CACHE=1 #38808

Merged
merged 54 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
1903ad8
ddl,executor: enhance test for auto_increment with AUTO_ID_CACHE=1
tiancaiamao Nov 2, 2022
0fd6c0b
Merge branch 'master' into autoid-test
tiancaiamao Nov 3, 2022
b792d93
fix test
tiancaiamao Nov 3, 2022
3546191
Merge branch 'master' into autoid-test
tiancaiamao Nov 4, 2022
3b281c3
Merge branch 'master' into autoid-test
tiancaiamao Nov 4, 2022
3685f4f
address comment
tiancaiamao Nov 7, 2022
53349f7
add the Build.bazel file
tiancaiamao Nov 7, 2022
59bba11
flaky = True and race = "on"
tiancaiamao Nov 7, 2022
ebb3d5c
format bazel.build
hawkingrei Nov 7, 2022
30643cc
Merge branch 'master' into autoid-test
ti-chi-bot Nov 10, 2022
9066663
Merge branch 'master' into autoid-test
ti-chi-bot Nov 11, 2022
622ee7a
Merge branch 'master' into autoid-test
ti-chi-bot Nov 11, 2022
a07fe08
Merge branch 'master' into autoid-test
ti-chi-bot Nov 11, 2022
ea1dfbe
Merge branch 'master' into autoid-test
ti-chi-bot Nov 11, 2022
03a5986
Merge branch 'master' into autoid-test
ti-chi-bot Nov 11, 2022
4910cc0
Merge branch 'master' into autoid-test
ti-chi-bot Nov 11, 2022
0767768
Merge branch 'master' into autoid-test
ti-chi-bot Nov 11, 2022
c0a77c1
Merge branch 'master' into autoid-test
ti-chi-bot Nov 11, 2022
9088221
Merge branch 'master' into autoid-test
ti-chi-bot Nov 11, 2022
f4f2f4b
Merge branch 'master' into autoid-test
ti-chi-bot Nov 11, 2022
50017e8
Merge branch 'master' into autoid-test
ti-chi-bot Nov 11, 2022
17ed49d
Merge branch 'master' into autoid-test
ti-chi-bot Nov 11, 2022
96ab4c5
Merge branch 'master' into autoid-test
ti-chi-bot Nov 12, 2022
f5551d1
Merge branch 'master' into autoid-test
ti-chi-bot Nov 13, 2022
e1eb8e1
Merge branch 'master' into autoid-test
ti-chi-bot Nov 14, 2022
cfe2026
Merge branch 'master' into autoid-test
ti-chi-bot Nov 14, 2022
3a5349a
Merge branch 'master' into autoid-test
ti-chi-bot Nov 14, 2022
4edbafb
Merge branch 'master' into autoid-test
ti-chi-bot Nov 14, 2022
0ca627b
Merge branch 'master' into autoid-test
ti-chi-bot Nov 14, 2022
63960d3
Merge branch 'master' into autoid-test
ti-chi-bot Nov 14, 2022
4b4ba8b
Merge branch 'master' into autoid-test
ti-chi-bot Nov 14, 2022
8b6488c
Merge branch 'master' into autoid-test
ti-chi-bot Nov 14, 2022
706aae9
Merge branch 'master' into autoid-test
ti-chi-bot Nov 14, 2022
38928b4
Merge branch 'master' into autoid-test
ti-chi-bot Nov 14, 2022
5599dcd
Merge branch 'master' into autoid-test
ti-chi-bot Nov 14, 2022
bb93e97
Merge branch 'master' into autoid-test
ti-chi-bot Nov 14, 2022
9f41fad
Merge branch 'master' into autoid-test
ti-chi-bot Nov 14, 2022
22f40db
Merge branch 'master' into autoid-test
ti-chi-bot Nov 14, 2022
86722c3
Merge branch 'master' into autoid-test
ti-chi-bot Nov 14, 2022
2200f4d
Merge branch 'master' into autoid-test
ti-chi-bot Nov 15, 2022
dc1c82c
Merge branch 'master' into autoid-test
ti-chi-bot Nov 15, 2022
a0ea7b4
Merge branch 'master' into autoid-test
ti-chi-bot Nov 15, 2022
5b6672b
Merge branch 'master' into autoid-test
ti-chi-bot Nov 15, 2022
897d058
Merge branch 'master' into autoid-test
ti-chi-bot Nov 15, 2022
03d085b
Merge branch 'master' into autoid-test
ti-chi-bot Nov 15, 2022
8f6b478
Merge branch 'master' into autoid-test
ti-chi-bot Nov 15, 2022
fa2a4ff
Merge branch 'master' into autoid-test
ti-chi-bot Nov 16, 2022
c15e8eb
Merge branch 'master' into autoid-test
ti-chi-bot Nov 16, 2022
be7700b
Merge branch 'master' into autoid-test
ti-chi-bot Nov 16, 2022
0f010ea
Merge branch 'master' into autoid-test
ti-chi-bot Nov 16, 2022
934dcdd
Merge branch 'master' into autoid-test
ti-chi-bot Nov 16, 2022
93c6e5b
fix CI
tiancaiamao Nov 16, 2022
b995f69
make bazel_prepare
tiancaiamao Nov 16, 2022
cdf533a
Merge branch 'master' into autoid-test
ti-chi-bot Nov 16, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 23 additions & 5 deletions autoid_service/autoid.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,30 @@ func (s *Service) allocAutoID(ctx context.Context, req *autoid.AutoIDRequest) (*

val := s.getAlloc(req.DbID, req.TblID, req.IsUnsigned)

if req.N == 0 && val.base != 0 {
base := val.base
if req.N == 0 {
if val.base != 0 {
return &autoid.AutoIDResponse{
Min: val.base,
Max: val.base,
}, nil
}
// This item is not initialized, get the data from remote.
var currentEnd int64
ctx = kv.WithInternalSourceType(ctx, kv.InternalTxnMeta)
err := kv.RunInNewTxn(ctx, s.store, true, func(ctx context.Context, txn kv.Transaction) error {
idAcc := meta.NewMeta(txn).GetAutoIDAccessors(req.DbID, req.TblID).RowID()
var err1 error
currentEnd, err1 = idAcc.Get()
if err1 != nil {
return err1
}
val.end = currentEnd
return nil
})
return &autoid.AutoIDResponse{
Min: base,
Max: base,
}, nil
Min: currentEnd,
Max: currentEnd,
}, err
}

val.Lock()
Expand Down
186 changes: 102 additions & 84 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2894,17 +2894,23 @@ func TestAutoIncrementTableOption(t *testing.T) {
tk.MustExec("create database test_auto_inc_table_opt;")
tk.MustExec("use test_auto_inc_table_opt;")

// Empty auto_inc allocator should not cause error.
tk.MustExec("create table t (a bigint primary key clustered) auto_increment = 10;")
tk.MustExec("alter table t auto_increment = 10;")
tk.MustExec("alter table t auto_increment = 12345678901234567890;")

// Rebase the auto_inc allocator to a large integer should work.
tk.MustExec("drop table t;")
tk.MustExec("create table t (a bigint unsigned auto_increment, unique key idx(a));")
tk.MustExec("alter table t auto_increment = 12345678901234567890;")
tk.MustExec("insert into t values ();")
tk.MustQuery("select * from t;").Check(testkit.Rows("12345678901234567890"))
for _, str := range []string{"", " AUTO_ID_CACHE 1"} {
// Empty auto_inc allocator should not cause error.
tk.MustExec("create table t (a bigint primary key clustered) auto_increment = 10" + str)
tk.MustExec("alter table t auto_increment = 10;")
tk.MustExec("alter table t auto_increment = 12345678901234567890;")
tk.MustExec("drop table t;")

// Rebase the auto_inc allocator to a large integer should work.
tk.MustExec("create table t (a bigint unsigned auto_increment, unique key idx(a))" + str)
// Set auto_inc to negative is not supported
err := tk.ExecToErr("alter table t auto_increment = -1;")
require.Error(t, err)
tk.MustExec("alter table t auto_increment = 12345678901234567890;")
tk.MustExec("insert into t values ();")
tk.MustQuery("select * from t;").Check(testkit.Rows("12345678901234567890"))
tk.MustExec("drop table t;")
}
}

func TestAutoIncrementForce(t *testing.T) {
Expand All @@ -2919,83 +2925,95 @@ func TestAutoIncrementForce(t *testing.T) {
require.NoError(t, err)
return gid
}
// Rebase _tidb_row_id.
tk.MustExec("create table t (a int);")
tk.MustExec("alter table t force auto_increment = 2;")
tk.MustExec("insert into t values (1),(2);")
tk.MustQuery("select a, _tidb_rowid from t;").Check(testkit.Rows("1 2", "2 3"))
// Cannot set next global ID to 0.
tk.MustGetErrCode("alter table t force auto_increment = 0;", errno.ErrAutoincReadFailed)
tk.MustExec("alter table t force auto_increment = 1;")
require.Equal(t, uint64(1), getNextGlobalID())
// inserting new rows can overwrite the existing data.
tk.MustExec("insert into t values (3);")
require.Equal(t, "[kv:1062]Duplicate entry '2' for key 't.PRIMARY'", tk.ExecToErr("insert into t values (3);").Error())
tk.MustQuery("select a, _tidb_rowid from t;").Check(testkit.Rows("3 1", "1 2", "2 3"))

// Rebase auto_increment.
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int primary key auto_increment, b int);")
tk.MustExec("insert into t values (1, 1);")
tk.MustExec("insert into t values (100000000, 1);")
tk.MustExec("delete from t where a = 100000000;")
require.Greater(t, getNextGlobalID(), uint64(100000000))
// Cannot set next global ID to 0.
tk.MustGetErrCode("alter table t /*T![force_inc] force */ auto_increment = 0;", errno.ErrAutoincReadFailed)
tk.MustExec("alter table t /*T![force_inc] force */ auto_increment = 2;")
require.Equal(t, uint64(2), getNextGlobalID())
tk.MustExec("insert into t(b) values (2);")
tk.MustQuery("select a, b from t;").Check(testkit.Rows("1 1", "2 2"))

// Rebase auto_random.
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a bigint primary key auto_random(5));")
tk.MustExec("insert into t values ();")
tk.MustExec("set @@allow_auto_random_explicit_insert = true")
tk.MustExec("insert into t values (100000000);")
tk.MustExec("delete from t where a = 100000000;")
require.Greater(t, getNextGlobalID(), uint64(100000000))
// Cannot set next global ID to 0.
tk.MustGetErrCode("alter table t force auto_random_base = 0;", errno.ErrAutoincReadFailed)
tk.MustExec("alter table t force auto_random_base = 2;")
require.Equal(t, uint64(2), getNextGlobalID())
tk.MustExec("insert into t values ();")
tk.MustQuery("select (a & 3) from t order by 1;").Check(testkit.Rows("1", "2"))

// Change next global ID.
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a bigint primary key auto_increment);")
tk.MustExec("insert into t values (1);")
bases := []uint64{1, 65535, 10, math.MaxUint64, math.MaxInt64 + 1, 1, math.MaxUint64, math.MaxInt64, 2}
lastBase := fmt.Sprintf("%d", bases[len(bases)-1])
for _, b := range bases {
tk.MustExec(fmt.Sprintf("alter table t force auto_increment = %d;", b))
require.Equal(t, b, getNextGlobalID())

for _, str := range []string{"", " AUTO_ID_CACHE 1"} {
// Rebase _tidb_row_id.
tk.MustExec("create table t (a int)" + str)
tk.MustExec("alter table t force auto_increment = 2;")
tk.MustExec("insert into t values (1),(2);")
tk.MustQuery("select a, _tidb_rowid from t;").Check(testkit.Rows("1 2", "2 3"))
// Cannot set next global ID to 0.
tk.MustGetErrCode("alter table t force auto_increment = 0;", errno.ErrAutoincReadFailed)
tk.MustExec("alter table t force auto_increment = 1;")
require.Equal(t, uint64(1), getNextGlobalID())
// inserting new rows can overwrite the existing data.
tk.MustExec("insert into t values (3);")
require.Equal(t, "[kv:1062]Duplicate entry '2' for key 't.PRIMARY'", tk.ExecToErr("insert into t values (3);").Error())
tk.MustQuery("select a, _tidb_rowid from t;").Check(testkit.Rows("3 1", "1 2", "2 3"))
tk.MustExec("drop table if exists t;")
}
tk.MustExec("insert into t values ();")
tk.MustQuery("select a from t;").Check(testkit.Rows("1", lastBase))
// Force alter unsigned int auto_increment column.
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a bigint unsigned primary key auto_increment);")
for _, b := range bases {
tk.MustExec(fmt.Sprintf("alter table t force auto_increment = %d;", b))
require.Equal(t, b, getNextGlobalID())

for _, str := range []string{"", " AUTO_ID_CACHE 1"} {
// Rebase auto_increment.
tk.MustExec("create table t (a int primary key auto_increment, b int)" + str)
tk.MustExec("insert into t values (1, 1);")
tk.MustExec("insert into t values (100000000, 1);")
tk.MustExec("delete from t where a = 100000000;")
require.Greater(t, getNextGlobalID(), uint64(100000000))
// Cannot set next global ID to 0.
tk.MustGetErrCode("alter table t /*T![force_inc] force */ auto_increment = 0;", errno.ErrAutoincReadFailed)
tk.MustExec("alter table t /*T![force_inc] force */ auto_increment = 2;")
require.Equal(t, uint64(2), getNextGlobalID())
tk.MustExec("insert into t(b) values (2);")
tk.MustQuery("select a, b from t;").Check(testkit.Rows("1 1", "2 2"))
tk.MustExec("drop table if exists t;")
}

for _, str := range []string{"", " AUTO_ID_CACHE 1"} {
// Rebase auto_random.
tk.MustExec("create table t (a bigint primary key auto_random(5))" + str)
tk.MustExec("insert into t values ();")
tk.MustExec("set @@allow_auto_random_explicit_insert = true")
tk.MustExec("insert into t values (100000000);")
tk.MustExec("delete from t where a = 100000000;")
require.Greater(t, getNextGlobalID(), uint64(100000000))
// Cannot set next global ID to 0.
tk.MustGetErrCode("alter table t force auto_random_base = 0;", errno.ErrAutoincReadFailed)
tk.MustExec("alter table t force auto_random_base = 2;")
require.Equal(t, uint64(2), getNextGlobalID())
tk.MustExec("insert into t values ();")
tk.MustQuery("select a from t;").Check(testkit.Rows(fmt.Sprintf("%d", b)))
tk.MustExec("delete from t;")
tk.MustQuery("select (a & 3) from t order by 1;").Check(testkit.Rows("1", "2"))
tk.MustExec("drop table if exists t;")
}

// Force alter with @@auto_increment_increment and @@auto_increment_offset.
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t(a int key auto_increment);")
tk.MustExec("set @@auto_increment_offset=2;")
tk.MustExec("set @@auto_increment_increment = 11;")
tk.MustExec("insert into t values (500);")
tk.MustExec("alter table t force auto_increment=100;")
tk.MustExec("insert into t values (), ();")
tk.MustQuery("select * from t;").Check(testkit.Rows("101", "112", "500"))
tk.MustQuery("select * from t order by a;").Check(testkit.Rows("101", "112", "500"))
tk.MustExec("drop table if exists t;")
for _, str := range []string{"", " AUTO_ID_CACHE 1"} {
// Change next global ID.
tk.MustExec("create table t (a bigint primary key auto_increment)" + str)
tk.MustExec("insert into t values (1);")
bases := []uint64{1, 65535, 10, math.MaxUint64, math.MaxInt64 + 1, 1, math.MaxUint64, math.MaxInt64, 2}
lastBase := fmt.Sprintf("%d", bases[len(bases)-1])
for _, b := range bases {
fmt.Println("execute alter table force increment to ==", b)
tk.MustExec(fmt.Sprintf("alter table t force auto_increment = %d;", b))
require.Equal(t, b, getNextGlobalID())
}
tk.MustExec("insert into t values ();")
tk.MustQuery("select a from t;").Check(testkit.Rows("1", lastBase))
// Force alter unsigned int auto_increment column.
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a bigint unsigned primary key auto_increment)" + str)
for _, b := range bases {
tk.MustExec(fmt.Sprintf("alter table t force auto_increment = %d;", b))
require.Equal(t, b, getNextGlobalID())
tk.MustExec("insert into t values ();")
tk.MustQuery("select a from t;").Check(testkit.Rows(fmt.Sprintf("%d", b)))
tk.MustExec("delete from t;")
}
tk.MustExec("drop table if exists t;")
}

for _, str := range []string{"", " AUTO_ID_CACHE 1"} {
// Force alter with @@auto_increment_increment and @@auto_increment_offset.
tk.MustExec("create table t(a int key auto_increment)" + str)
tk.MustExec("set @@auto_increment_offset=2;")
tk.MustExec("set @@auto_increment_increment = 11;")
tk.MustExec("insert into t values (500);")
tk.MustExec("alter table t force auto_increment=100;")
tk.MustExec("insert into t values (), ();")
tk.MustQuery("select * from t;").Check(testkit.Rows("101", "112", "500"))
tk.MustQuery("select * from t order by a;").Check(testkit.Rows("101", "112", "500"))
tk.MustExec("drop table if exists t;")
}

// Check for warning in case we can't set the auto_increment to the desired value
tk.MustExec("create table t(a int primary key auto_increment)")
Expand Down
23 changes: 23 additions & 0 deletions executor/autoidtest/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
load("@io_bazel_rules_go//go:def.bzl", "go_test")

go_test(
name = "autoidtest_test",
srcs = [
"autoid_test.go",
"main_test.go",
],
flaky = True,
race = "on",
deps = [
"//config",
"//ddl/testutil",
"//meta/autoid",
"//parser/mysql",
"//sessionctx/variable",
"//testkit",
"//testkit/testutil",
"@com_github_stretchr_testify//require",
"@com_github_tikv_client_go_v2//tikv",
"@org_uber_go_goleak//:goleak",
],
)