Skip to content

Commit 4bf8b58

Browse files
wjhuang2016ti-chi-bot
authored andcommitted
This is an automated cherry-pick of pingcap#57964
Signed-off-by: ti-chi-bot <[email protected]>
1 parent 0e847c3 commit 4bf8b58

File tree

3 files changed

+634
-0
lines changed

3 files changed

+634
-0
lines changed

ddl/metadatalocktest/mdl_test.go

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,26 @@
1515
package metadatalocktest
1616

1717
import (
18+
"context"
1819
"fmt"
1920
"sync"
2021
"testing"
2122
"time"
2223

2324
"github.com/pingcap/failpoint"
25+
<<<<<<< HEAD:ddl/metadatalocktest/mdl_test.go
2426
mysql "github.com/pingcap/tidb/errno"
2527
"github.com/pingcap/tidb/server"
2628
"github.com/pingcap/tidb/testkit"
29+
=======
30+
"github.com/pingcap/tidb/pkg/ddl"
31+
ingesttestutil "github.com/pingcap/tidb/pkg/ddl/ingest/testutil"
32+
mysql "github.com/pingcap/tidb/pkg/errno"
33+
"github.com/pingcap/tidb/pkg/meta/model"
34+
"github.com/pingcap/tidb/pkg/server"
35+
"github.com/pingcap/tidb/pkg/testkit"
36+
"github.com/pingcap/tidb/pkg/testkit/testfailpoint"
37+
>>>>>>> 6b17068d75f (planner: fix incorrectly using the schema for plan cache (#57964)):pkg/ddl/tests/metadatalock/mdl_test.go
2738
"github.com/stretchr/testify/require"
2839
)
2940

@@ -895,6 +906,207 @@ func TestMDLPreparePlanCacheInvalid(t *testing.T) {
895906
tk.MustQuery(`execute stmt_test_1 using @a;`).Check(testkit.Rows("1 <nil>", "2 <nil>", "3 <nil>", "4 <nil>"))
896907
}
897908

909+
<<<<<<< HEAD:ddl/metadatalocktest/mdl_test.go
910+
=======
911+
func TestMDLPreparePlanCacheExecute(t *testing.T) {
912+
store, dom := testkit.CreateMockStoreAndDomain(t)
913+
defer ingesttestutil.InjectMockBackendMgr(t, store)()
914+
915+
sv := server.CreateMockServer(t, store)
916+
917+
sv.SetDomain(dom)
918+
dom.InfoSyncer().SetSessionManager(sv)
919+
defer sv.Close()
920+
921+
conn1 := server.CreateMockConn(t, sv)
922+
tk := testkit.NewTestKitWithSession(t, store, conn1.Context().Session)
923+
conn2 := server.CreateMockConn(t, sv)
924+
tkDDL := testkit.NewTestKitWithSession(t, store, conn2.Context().Session)
925+
tk.MustExec("use test")
926+
tk.MustExec("set global tidb_enable_metadata_lock=1")
927+
tk.MustExec("create table t(a int);")
928+
tk.MustExec("create table t2(a int);")
929+
tk.MustExec("insert into t values(1), (2), (3), (4);")
930+
931+
tk.MustExec(`prepare stmt_test_1 from 'update t set a = ? where a = ?';`)
932+
tk.MustExec(`set @a = 1, @b = 3;`)
933+
tk.MustExec(`execute stmt_test_1 using @a, @b;`)
934+
935+
tk.MustExec("begin")
936+
937+
ch := make(chan struct{})
938+
939+
var wg sync.WaitGroup
940+
wg.Add(1)
941+
go func() {
942+
<-ch
943+
tkDDL.MustExec("alter table test.t add index idx(a);")
944+
wg.Done()
945+
}()
946+
947+
tk.MustQuery("select * from t2")
948+
tk.MustExec(`set @a = 2, @b=4;`)
949+
tk.MustExec(`execute stmt_test_1 using @a, @b;`) // can't reuse the prior plan created outside this txn.
950+
tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Rows("0"))
951+
tk.MustExec(`execute stmt_test_1 using @a, @b;`) // can't reuse the prior plan since this table becomes dirty.
952+
tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Rows("0"))
953+
tk.MustExec(`execute stmt_test_1 using @a, @b;`) // can't reuse the prior plan now.
954+
tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Rows("1"))
955+
// The plan is from cache, the metadata lock should be added to block the DDL.
956+
ch <- struct{}{}
957+
958+
time.Sleep(5 * time.Second)
959+
960+
tk.MustExec("commit")
961+
962+
wg.Wait()
963+
964+
tk.MustExec("admin check table t")
965+
}
966+
967+
func TestMDLPreparePlanCacheExecute2(t *testing.T) {
968+
store, dom := testkit.CreateMockStoreAndDomain(t)
969+
defer ingesttestutil.InjectMockBackendMgr(t, store)()
970+
971+
sv := server.CreateMockServer(t, store)
972+
973+
sv.SetDomain(dom)
974+
dom.InfoSyncer().SetSessionManager(sv)
975+
defer sv.Close()
976+
977+
conn1 := server.CreateMockConn(t, sv)
978+
tk := testkit.NewTestKitWithSession(t, store, conn1.Context().Session)
979+
conn2 := server.CreateMockConn(t, sv)
980+
tkDDL := testkit.NewTestKitWithSession(t, store, conn2.Context().Session)
981+
tk.MustExec("use test")
982+
tk.MustExec("set global tidb_enable_metadata_lock=1")
983+
tk.MustExec("create table t(a int);")
984+
tk.MustExec("create table t2(a int);")
985+
tk.MustExec("insert into t values(1), (2), (3), (4);")
986+
987+
tk.MustExec(`prepare stmt_test_1 from 'select * from t where a = ?';`)
988+
tk.MustExec(`set @a = 1;`)
989+
tk.MustExec(`execute stmt_test_1 using @a;`)
990+
991+
tk.MustExec("begin")
992+
tk.MustQuery("select * from t2")
993+
994+
var wg sync.WaitGroup
995+
wg.Add(1)
996+
go func() {
997+
tkDDL.MustExec("alter table test.t add index idx(a);")
998+
wg.Done()
999+
}()
1000+
1001+
wg.Wait()
1002+
1003+
tk.MustExec(`set @a = 2;`)
1004+
tk.MustExec(`execute stmt_test_1 using @a;`)
1005+
// The plan should not be from cache because the schema has changed.
1006+
tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Rows("0"))
1007+
tk.MustExec("commit")
1008+
1009+
tk.MustExec("admin check table t")
1010+
}
1011+
1012+
// TestMDLPreparePlanCacheExecuteInsert makes sure the insert statement handle the schema correctly in plan cache.
1013+
func TestMDLPreparePlanCacheExecuteInsert(t *testing.T) {
1014+
store, dom := testkit.CreateMockStoreAndDomain(t)
1015+
defer ingesttestutil.InjectMockBackendMgr(t, store)()
1016+
1017+
sv := server.CreateMockServer(t, store)
1018+
1019+
sv.SetDomain(dom)
1020+
dom.InfoSyncer().SetSessionManager(sv)
1021+
defer sv.Close()
1022+
1023+
conn1 := server.CreateMockConn(t, sv)
1024+
tk := testkit.NewTestKitWithSession(t, store, conn1.Context().Session)
1025+
conn2 := server.CreateMockConn(t, sv)
1026+
tkDDL := testkit.NewTestKitWithSession(t, store, conn2.Context().Session)
1027+
conn3 := server.CreateMockConn(t, sv)
1028+
tk3 := testkit.NewTestKitWithSession(t, store, conn3.Context().Session)
1029+
tk.MustExec("use test")
1030+
tk.MustExec("set global tidb_enable_metadata_lock=1")
1031+
tk.MustExec("create table t(a int primary key, b int);")
1032+
tk.MustExec("create table t2(a int);")
1033+
tk.MustExec("insert into t values(1, 1), (2, 2), (3, 3), (4, 4);")
1034+
1035+
tk.MustExec(`begin`)
1036+
tk.MustExec(`prepare delete_stmt from 'delete from t where a = ?'`)
1037+
tk.MustExec(`prepare insert_stmt from 'insert into t values (?, ?)'`)
1038+
tk.MustExec(`commit`)
1039+
1040+
tk.MustExec(`begin`)
1041+
tk.MustExec(`set @a = 4, @b= 4;`)
1042+
tk.MustExec(`execute delete_stmt using @a;`)
1043+
tk.MustExec(`execute insert_stmt using @a, @b;`)
1044+
tk.MustExec(`commit`)
1045+
1046+
tk.MustExec("begin")
1047+
1048+
ch := make(chan struct{})
1049+
1050+
first := true
1051+
testfailpoint.EnableCall(t, "github.com/pingcap/tidb/pkg/ddl/onJobUpdated", func(job *model.Job) {
1052+
switch job.SchemaState {
1053+
case model.StateWriteReorganization:
1054+
tbl, _ := dom.InfoSchema().TableByID(context.Background(), job.TableID)
1055+
idx := tbl.Meta().FindIndexByName("idx")
1056+
switch idx.BackfillState {
1057+
case model.BackfillStateRunning:
1058+
if first {
1059+
tk.MustExec(`begin`)
1060+
tk.MustExec(`set @a=9;`)
1061+
tk.MustExec(`execute delete_stmt using @a;`)
1062+
tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Rows("0"))
1063+
tk.MustExec(`set @a=6, @b=4;`)
1064+
tk.MustExec(`execute insert_stmt using @a, @b;`)
1065+
tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Rows("0"))
1066+
tk.MustExec(`commit`)
1067+
tk.MustExec(`begin`)
1068+
tk.MustExec(`set @a=4;`)
1069+
tk.MustExec(`execute delete_stmt using @a;`)
1070+
tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Rows("1"))
1071+
tk.MustExec(`set @a=4, @b=4;`)
1072+
tk.MustExec(`execute insert_stmt using @a, @b;`)
1073+
tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Rows("0"))
1074+
tk.MustExec(`commit`)
1075+
1076+
tk.MustExec("begin")
1077+
// Activate txn.
1078+
tk.MustExec("select * from t2")
1079+
first = false
1080+
tk3.MustExec("insert into test.t values(10000, 1000)")
1081+
return
1082+
}
1083+
}
1084+
}
1085+
})
1086+
1087+
ddl.MockDMLExecutionMerging = func() {
1088+
tk.MustExec(`execute delete_stmt using @a;`)
1089+
tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Rows("0"))
1090+
tk.MustExec(`execute insert_stmt using @a, @b;`)
1091+
tk.MustExec("commit")
1092+
}
1093+
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/pkg/ddl/mockDMLExecutionMerging", "1*return(true)->return(false)"))
1094+
1095+
var wg sync.WaitGroup
1096+
wg.Add(1)
1097+
go func() {
1098+
<-ch
1099+
tkDDL.MustExec("alter table test.t add index idx(a);")
1100+
wg.Done()
1101+
}()
1102+
1103+
ch <- struct{}{}
1104+
wg.Wait()
1105+
1106+
tk.MustExec("admin check table t")
1107+
}
1108+
1109+
>>>>>>> 6b17068d75f (planner: fix incorrectly using the schema for plan cache (#57964)):pkg/ddl/tests/metadatalock/mdl_test.go
8981110
func TestMDLDisable2Enable(t *testing.T) {
8991111
store, dom := testkit.CreateMockStoreAndDomain(t)
9001112
sv := server.CreateMockServer(t, store)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
load("@io_bazel_rules_go//go:def.bzl", "go_test")
2+
3+
go_test(
4+
name = "metadatalock_test",
5+
timeout = "short",
6+
srcs = [
7+
"main_test.go",
8+
"mdl_test.go",
9+
],
10+
flaky = True,
11+
shard_count = 37,
12+
deps = [
13+
"//pkg/config",
14+
"//pkg/ddl",
15+
"//pkg/ddl/ingest/testutil",
16+
"//pkg/errno",
17+
"//pkg/meta/model",
18+
"//pkg/server",
19+
"//pkg/testkit",
20+
"//pkg/testkit/testfailpoint",
21+
"//pkg/testkit/testsetup",
22+
"@com_github_pingcap_failpoint//:failpoint",
23+
"@com_github_stretchr_testify//require",
24+
"@org_uber_go_goleak//:goleak",
25+
],
26+
)

0 commit comments

Comments
 (0)