|
15 | 15 | package metadatalocktest
|
16 | 16 |
|
17 | 17 | import (
|
| 18 | + "context" |
18 | 19 | "fmt"
|
19 | 20 | "sync"
|
20 | 21 | "testing"
|
21 | 22 | "time"
|
22 | 23 |
|
| 24 | + "github.com/pingcap/failpoint" |
| 25 | + "github.com/pingcap/tidb/pkg/ddl" |
23 | 26 | ingesttestutil "github.com/pingcap/tidb/pkg/ddl/ingest/testutil"
|
24 | 27 | mysql "github.com/pingcap/tidb/pkg/errno"
|
| 28 | + "github.com/pingcap/tidb/pkg/meta/model" |
25 | 29 | "github.com/pingcap/tidb/pkg/server"
|
26 | 30 | "github.com/pingcap/tidb/pkg/testkit"
|
27 | 31 | "github.com/pingcap/tidb/pkg/testkit/testfailpoint"
|
@@ -997,6 +1001,103 @@ func TestMDLPreparePlanCacheExecute2(t *testing.T) {
|
997 | 1001 | tk.MustExec("admin check table t")
|
998 | 1002 | }
|
999 | 1003 |
|
| 1004 | +// TestMDLPreparePlanCacheExecuteInsert makes sure the insert statement handle the schema correctly in plan cache. |
| 1005 | +func TestMDLPreparePlanCacheExecuteInsert(t *testing.T) { |
| 1006 | + store, dom := testkit.CreateMockStoreAndDomain(t) |
| 1007 | + defer ingesttestutil.InjectMockBackendMgr(t, store)() |
| 1008 | + |
| 1009 | + sv := server.CreateMockServer(t, store) |
| 1010 | + |
| 1011 | + sv.SetDomain(dom) |
| 1012 | + dom.InfoSyncer().SetSessionManager(sv) |
| 1013 | + defer sv.Close() |
| 1014 | + |
| 1015 | + conn1 := server.CreateMockConn(t, sv) |
| 1016 | + tk := testkit.NewTestKitWithSession(t, store, conn1.Context().Session) |
| 1017 | + conn2 := server.CreateMockConn(t, sv) |
| 1018 | + tkDDL := testkit.NewTestKitWithSession(t, store, conn2.Context().Session) |
| 1019 | + conn3 := server.CreateMockConn(t, sv) |
| 1020 | + tk3 := testkit.NewTestKitWithSession(t, store, conn3.Context().Session) |
| 1021 | + tk.MustExec("use test") |
| 1022 | + tk.MustExec("set global tidb_enable_metadata_lock=1") |
| 1023 | + tk.MustExec("create table t(a int primary key, b int);") |
| 1024 | + tk.MustExec("create table t2(a int);") |
| 1025 | + tk.MustExec("insert into t values(1, 1), (2, 2), (3, 3), (4, 4);") |
| 1026 | + |
| 1027 | + tk.MustExec(`begin`) |
| 1028 | + tk.MustExec(`prepare delete_stmt from 'delete from t where a = ?'`) |
| 1029 | + tk.MustExec(`prepare insert_stmt from 'insert into t values (?, ?)'`) |
| 1030 | + tk.MustExec(`commit`) |
| 1031 | + |
| 1032 | + tk.MustExec(`begin`) |
| 1033 | + tk.MustExec(`set @a = 4, @b= 4;`) |
| 1034 | + tk.MustExec(`execute delete_stmt using @a;`) |
| 1035 | + tk.MustExec(`execute insert_stmt using @a, @b;`) |
| 1036 | + tk.MustExec(`commit`) |
| 1037 | + |
| 1038 | + tk.MustExec("begin") |
| 1039 | + |
| 1040 | + ch := make(chan struct{}) |
| 1041 | + |
| 1042 | + first := true |
| 1043 | + testfailpoint.EnableCall(t, "github.com/pingcap/tidb/pkg/ddl/onJobUpdated", func(job *model.Job) { |
| 1044 | + switch job.SchemaState { |
| 1045 | + case model.StateWriteReorganization: |
| 1046 | + tbl, _ := dom.InfoSchema().TableByID(context.Background(), job.TableID) |
| 1047 | + idx := tbl.Meta().FindIndexByName("idx") |
| 1048 | + switch idx.BackfillState { |
| 1049 | + case model.BackfillStateRunning: |
| 1050 | + if first { |
| 1051 | + tk.MustExec(`begin`) |
| 1052 | + tk.MustExec(`set @a=9;`) |
| 1053 | + tk.MustExec(`execute delete_stmt using @a;`) |
| 1054 | + tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Rows("0")) |
| 1055 | + tk.MustExec(`set @a=6, @b=4;`) |
| 1056 | + tk.MustExec(`execute insert_stmt using @a, @b;`) |
| 1057 | + tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Rows("0")) |
| 1058 | + tk.MustExec(`commit`) |
| 1059 | + tk.MustExec(`begin`) |
| 1060 | + tk.MustExec(`set @a=4;`) |
| 1061 | + tk.MustExec(`execute delete_stmt using @a;`) |
| 1062 | + tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Rows("1")) |
| 1063 | + tk.MustExec(`set @a=4, @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 | + |
| 1068 | + tk.MustExec("begin") |
| 1069 | + // Activate txn. |
| 1070 | + tk.MustExec("select * from t2") |
| 1071 | + first = false |
| 1072 | + tk3.MustExec("insert into test.t values(10000, 1000)") |
| 1073 | + return |
| 1074 | + } |
| 1075 | + } |
| 1076 | + } |
| 1077 | + }) |
| 1078 | + |
| 1079 | + ddl.MockDMLExecutionMerging = func() { |
| 1080 | + tk.MustExec(`execute delete_stmt using @a;`) |
| 1081 | + tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Rows("0")) |
| 1082 | + tk.MustExec(`execute insert_stmt using @a, @b;`) |
| 1083 | + tk.MustExec("commit") |
| 1084 | + } |
| 1085 | + require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/pkg/ddl/mockDMLExecutionMerging", "1*return(true)->return(false)")) |
| 1086 | + |
| 1087 | + var wg sync.WaitGroup |
| 1088 | + wg.Add(1) |
| 1089 | + go func() { |
| 1090 | + <-ch |
| 1091 | + tkDDL.MustExec("alter table test.t add index idx(a);") |
| 1092 | + wg.Done() |
| 1093 | + }() |
| 1094 | + |
| 1095 | + ch <- struct{}{} |
| 1096 | + wg.Wait() |
| 1097 | + |
| 1098 | + tk.MustExec("admin check table t") |
| 1099 | +} |
| 1100 | + |
1000 | 1101 | func TestMDLDisable2Enable(t *testing.T) {
|
1001 | 1102 | store, dom := testkit.CreateMockStoreAndDomain(t)
|
1002 | 1103 | sv := server.CreateMockServer(t, store)
|
|
0 commit comments