Skip to content

Commit f4fe834

Browse files
wjhuang2016ti-chi-bot
authored andcommitted
This is an automated cherry-pick of #51897
Signed-off-by: ti-chi-bot <[email protected]>
1 parent ddc6543 commit f4fe834

File tree

14 files changed

+2037
-3
lines changed

14 files changed

+2037
-3
lines changed

ddl/metadatalocktest/BUILD.bazel

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,25 @@ go_test(
77
"mdl_test.go",
88
],
99
flaky = True,
10-
shard_count = 34,
10+
shard_count = 36,
1111
deps = [
12+
<<<<<<< HEAD:ddl/metadatalocktest/BUILD.bazel
1213
"//config",
1314
"//ddl",
1415
"//errno",
1516
"//server",
1617
"//testkit",
1718
"//testkit/testsetup",
1819
"//util/logutil",
20+
=======
21+
"//pkg/config",
22+
"//pkg/ddl",
23+
"//pkg/ddl/ingest/testutil",
24+
"//pkg/errno",
25+
"//pkg/server",
26+
"//pkg/testkit",
27+
"//pkg/testkit/testsetup",
28+
>>>>>>> 70a825397f3 (*: add metadata lock when using the plan cache (#51897)):pkg/ddl/tests/metadatalock/BUILD.bazel
1929
"@com_github_pingcap_failpoint//:failpoint",
2030
"@com_github_stretchr_testify//require",
2131
"@org_uber_go_goleak//:goleak",

ddl/metadatalocktest/mdl_test.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,17 @@ import (
2424
"time"
2525

2626
"github.com/pingcap/failpoint"
27+
<<<<<<< HEAD:ddl/metadatalocktest/mdl_test.go
2728
mysql "github.com/pingcap/tidb/errno"
2829
"github.com/pingcap/tidb/server"
2930
"github.com/pingcap/tidb/testkit"
3031
"github.com/pingcap/tidb/util/logutil"
32+
=======
33+
ingesttestutil "github.com/pingcap/tidb/pkg/ddl/ingest/testutil"
34+
mysql "github.com/pingcap/tidb/pkg/errno"
35+
"github.com/pingcap/tidb/pkg/server"
36+
"github.com/pingcap/tidb/pkg/testkit"
37+
>>>>>>> 70a825397f3 (*: add metadata lock when using the plan cache (#51897)):pkg/ddl/tests/metadatalock/mdl_test.go
3138
"github.com/stretchr/testify/require"
3239
"go.uber.org/zap"
3340
)
@@ -872,6 +879,103 @@ func TestMDLPreparePlanCacheInvalid(t *testing.T) {
872879
tk.MustQuery(`execute stmt_test_1 using @a;`).Check(testkit.Rows("1 <nil>", "2 <nil>", "3 <nil>", "4 <nil>"))
873880
}
874881

882+
func TestMDLPreparePlanCacheExecute(t *testing.T) {
883+
store, dom := testkit.CreateMockStoreAndDomain(t)
884+
defer ingesttestutil.InjectMockBackendMgr(t, store)()
885+
886+
sv := server.CreateMockServer(t, store)
887+
888+
sv.SetDomain(dom)
889+
dom.InfoSyncer().SetSessionManager(sv)
890+
defer sv.Close()
891+
892+
conn1 := server.CreateMockConn(t, sv)
893+
tk := testkit.NewTestKitWithSession(t, store, conn1.Context().Session)
894+
conn2 := server.CreateMockConn(t, sv)
895+
tkDDL := testkit.NewTestKitWithSession(t, store, conn2.Context().Session)
896+
tk.MustExec("use test")
897+
tk.MustExec("set global tidb_enable_metadata_lock=1")
898+
tk.MustExec("create table t(a int);")
899+
tk.MustExec("create table t2(a int);")
900+
tk.MustExec("insert into t values(1), (2), (3), (4);")
901+
902+
tk.MustExec(`prepare stmt_test_1 from 'update t set a = ? where a = ?';`)
903+
tk.MustExec(`set @a = 1, @b = 3;`)
904+
tk.MustExec(`execute stmt_test_1 using @a, @b;`)
905+
906+
tk.MustExec("begin")
907+
908+
ch := make(chan struct{})
909+
910+
var wg sync.WaitGroup
911+
wg.Add(1)
912+
go func() {
913+
<-ch
914+
tkDDL.MustExec("alter table test.t add index idx(a);")
915+
wg.Done()
916+
}()
917+
918+
tk.MustQuery("select * from t2")
919+
tk.MustExec(`set @a = 2, @b=4;`)
920+
tk.MustExec(`execute stmt_test_1 using @a, @b;`)
921+
tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Rows("1"))
922+
// The plan is from cache, the metadata lock should be added to block the DDL.
923+
ch <- struct{}{}
924+
925+
time.Sleep(5 * time.Second)
926+
927+
tk.MustExec("commit")
928+
929+
wg.Wait()
930+
931+
tk.MustExec("admin check table t")
932+
}
933+
934+
func TestMDLPreparePlanCacheExecute2(t *testing.T) {
935+
store, dom := testkit.CreateMockStoreAndDomain(t)
936+
defer ingesttestutil.InjectMockBackendMgr(t, store)()
937+
938+
sv := server.CreateMockServer(t, store)
939+
940+
sv.SetDomain(dom)
941+
dom.InfoSyncer().SetSessionManager(sv)
942+
defer sv.Close()
943+
944+
conn1 := server.CreateMockConn(t, sv)
945+
tk := testkit.NewTestKitWithSession(t, store, conn1.Context().Session)
946+
conn2 := server.CreateMockConn(t, sv)
947+
tkDDL := testkit.NewTestKitWithSession(t, store, conn2.Context().Session)
948+
tk.MustExec("use test")
949+
tk.MustExec("set global tidb_enable_metadata_lock=1")
950+
tk.MustExec("create table t(a int);")
951+
tk.MustExec("create table t2(a int);")
952+
tk.MustExec("insert into t values(1), (2), (3), (4);")
953+
954+
tk.MustExec(`prepare stmt_test_1 from 'select * from t where a = ?';`)
955+
tk.MustExec(`set @a = 1;`)
956+
tk.MustExec(`execute stmt_test_1 using @a;`)
957+
958+
tk.MustExec("begin")
959+
tk.MustQuery("select * from t2")
960+
961+
var wg sync.WaitGroup
962+
wg.Add(1)
963+
go func() {
964+
tkDDL.MustExec("alter table test.t add index idx(a);")
965+
wg.Done()
966+
}()
967+
968+
wg.Wait()
969+
970+
tk.MustExec(`set @a = 2;`)
971+
tk.MustExec(`execute stmt_test_1 using @a;`)
972+
// The plan should not be from cache because the schema has changed.
973+
tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Rows("0"))
974+
tk.MustExec("commit")
975+
976+
tk.MustExec("admin check table t")
977+
}
978+
875979
func TestMDLDisable2Enable(t *testing.T) {
876980
store, dom := testkit.CreateMockStoreAndDomain(t)
877981
sv := server.CreateMockServer(t, store)

domain/plan_replayer_dump.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,7 @@ func dumpExplain(ctx sessionctx.Context, zw *zip.Writer, execStmts []ast.StmtNod
526526
return nil
527527
}
528528

529+
// extractTableNames extracts table names from the given stmts.
529530
func extractTableNames(ctx context.Context, sctx sessionctx.Context,
530531
ExecStmts []ast.StmtNode, curDB model.CIStr) (map[tableNamePair]struct{}, error) {
531532
tableExtractor := &tableNameExtractor{

executor/executor.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1984,7 +1984,16 @@ func ResetContextOfStmt(ctx sessionctx.Context, s ast.StmtNode) (err error) {
19841984
vars.DiskTracker.Detach()
19851985
vars.DiskTracker.ResetMaxConsumed()
19861986
vars.MemTracker.SessionID.Store(vars.ConnectionID)
1987+
<<<<<<< HEAD:executor/executor.go
19871988
vars.StmtCtx.TableStats = make(map[int64]interface{})
1989+
=======
1990+
vars.MemTracker.Killer = &vars.SQLKiller
1991+
vars.DiskTracker.Killer = &vars.SQLKiller
1992+
vars.SQLKiller.Reset()
1993+
vars.SQLKiller.ConnID = vars.ConnectionID
1994+
vars.StmtCtx.TableStats = make(map[int64]any)
1995+
sc.MDLRelatedTableIDs = make(map[int64]struct{})
1996+
>>>>>>> 70a825397f3 (*: add metadata lock when using the plan cache (#51897)):pkg/executor/executor.go
19881997

19891998
isAnalyze := false
19901999
if execStmt, ok := s.(*ast.ExecuteStmt); ok {

executor/prepared.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
"github.com/pingcap/errors"
2121
"github.com/pingcap/log"
22+
<<<<<<< HEAD:executor/prepared.go
2223
"github.com/pingcap/tidb/expression"
2324
"github.com/pingcap/tidb/infoschema"
2425
"github.com/pingcap/tidb/parser"
@@ -32,6 +33,27 @@ import (
3233
"github.com/pingcap/tidb/util/sqlexec"
3334
"github.com/pingcap/tidb/util/topsql"
3435
topsqlstate "github.com/pingcap/tidb/util/topsql/state"
36+
=======
37+
"github.com/pingcap/tidb/pkg/bindinfo"
38+
"github.com/pingcap/tidb/pkg/executor/internal/exec"
39+
"github.com/pingcap/tidb/pkg/expression"
40+
"github.com/pingcap/tidb/pkg/infoschema"
41+
"github.com/pingcap/tidb/pkg/parser"
42+
"github.com/pingcap/tidb/pkg/parser/ast"
43+
"github.com/pingcap/tidb/pkg/parser/mysql"
44+
plannercore "github.com/pingcap/tidb/pkg/planner/core"
45+
"github.com/pingcap/tidb/pkg/planner/core/base"
46+
"github.com/pingcap/tidb/pkg/sessionctx"
47+
"github.com/pingcap/tidb/pkg/sessiontxn"
48+
"github.com/pingcap/tidb/pkg/types"
49+
"github.com/pingcap/tidb/pkg/util"
50+
"github.com/pingcap/tidb/pkg/util/chunk"
51+
"github.com/pingcap/tidb/pkg/util/dbterror/exeerrors"
52+
"github.com/pingcap/tidb/pkg/util/dbterror/plannererrors"
53+
"github.com/pingcap/tidb/pkg/util/sqlexec"
54+
"github.com/pingcap/tidb/pkg/util/topsql"
55+
topsqlstate "github.com/pingcap/tidb/pkg/util/topsql/state"
56+
>>>>>>> 70a825397f3 (*: add metadata lock when using the plan cache (#51897)):pkg/executor/prepared.go
3557
"go.uber.org/zap"
3658
)
3759

@@ -117,7 +139,11 @@ func (e *PrepareExec) Next(ctx context.Context, req *chunk.Chunk) error {
117139
return err
118140
}
119141
}
142+
<<<<<<< HEAD:executor/prepared.go
120143
stmt, p, paramCnt, err := plannercore.GeneratePlanCacheStmtWithAST(ctx, e.ctx, stmt0)
144+
=======
145+
stmt, p, paramCnt, err := plannercore.GeneratePlanCacheStmtWithAST(ctx, e.Ctx(), true, stmt0.Text(), stmt0, sessiontxn.GetTxnManager(e.Ctx()).GetTxnInfoSchema())
146+
>>>>>>> 70a825397f3 (*: add metadata lock when using the plan cache (#51897)):pkg/executor/prepared.go
121147
if err != nil {
122148
return err
123149
}
@@ -208,10 +234,17 @@ func (e *DeallocateExec) Next(ctx context.Context, req *chunk.Chunk) error {
208234
}
209235
prepared := preparedObj.PreparedAst
210236
delete(vars.PreparedStmtNameToID, e.Name)
237+
<<<<<<< HEAD:executor/prepared.go
211238
if e.ctx.GetSessionVars().EnablePreparedPlanCache {
212239
bindSQL, _ := plannercore.GetBindSQL4PlanCache(e.ctx, preparedObj)
213240
cacheKey, err := plannercore.NewPlanCacheKey(vars, preparedObj.StmtText, preparedObj.StmtDB, prepared.SchemaVersion,
214241
0, bindSQL)
242+
=======
243+
if e.Ctx().GetSessionVars().EnablePreparedPlanCache {
244+
bindSQL, _ := bindinfo.MatchSQLBindingForPlanCache(e.Ctx(), preparedObj.PreparedAst.Stmt, &preparedObj.BindingInfo)
245+
cacheKey, err := plannercore.NewPlanCacheKey(vars, preparedObj.StmtText, preparedObj.StmtDB, preparedObj.SchemaVersion,
246+
0, bindSQL, expression.ExprPushDownBlackListReloadTimeStamp.Load(), preparedObj.RelateVersion)
247+
>>>>>>> 70a825397f3 (*: add metadata lock when using the plan cache (#51897)):pkg/executor/prepared.go
215248
if err != nil {
216249
return err
217250
}

meta/meta.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,8 @@ func (m *Meta) UpdateTable(dbID int64, tableInfo *model.TableInfo) error {
822822
return errors.Trace(err)
823823
}
824824

825+
tableInfo.Revision++
826+
825827
data, err := json.Marshal(tableInfo)
826828
if err != nil {
827829
return errors.Trace(err)

parser/model/model.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,20 @@ type TableInfo struct {
567567
ExchangePartitionInfo *ExchangePartitionInfo `json:"exchange_partition_info"`
568568

569569
TTLInfo *TTLInfo `json:"ttl_info"`
570+
<<<<<<< HEAD:parser/model/model.go
571+
=======
572+
573+
// Revision is per table schema's version, it will be increased when the schema changed.
574+
Revision uint64 `json:"revision"`
575+
576+
DBID int64 `json:"-"`
577+
}
578+
579+
// TableNameInfo provides meta data describing a table name info.
580+
type TableNameInfo struct {
581+
ID int64 `json:"id"`
582+
Name CIStr `json:"name"`
583+
>>>>>>> 70a825397f3 (*: add metadata lock when using the plan cache (#51897)):pkg/parser/model/model.go
570584
}
571585

572586
// SepAutoInc decides whether _rowid and auto_increment id use separate allocator.

0 commit comments

Comments
 (0)