Skip to content

Commit 15bac9e

Browse files
authored
ddl: show more jobs in the tidb_mdl_view (#40860)
close #40838
1 parent 1606b05 commit 15bac9e

File tree

2 files changed

+73
-31
lines changed

2 files changed

+73
-31
lines changed

infoschema/cluster_tables_test.go

+49-29
Original file line numberDiff line numberDiff line change
@@ -816,42 +816,62 @@ func (s *clusterTablesSuite) newTestKitWithRoot(t *testing.T) *testkit.TestKit {
816816
}
817817

818818
func TestMDLView(t *testing.T) {
819-
// setup suite
820-
s := new(clusterTablesSuite)
821-
s.store, s.dom = testkit.CreateMockStoreAndDomain(t)
822-
s.httpServer, s.mockAddr = s.setUpMockPDHTTPServer()
823-
s.startTime = time.Now()
824-
defer s.httpServer.Close()
825-
826-
tk := s.newTestKitWithRoot(t)
827-
tkDDL := s.newTestKitWithRoot(t)
828-
tk3 := s.newTestKitWithRoot(t)
829-
tk.MustExec("use test")
830-
tk.MustExec("set global tidb_enable_metadata_lock=1")
831-
tk.MustExec("create table t(a int);")
832-
tk.MustExec("insert into t values(1);")
819+
testCases := []struct {
820+
name string
821+
createTable string
822+
ddl string
823+
queryInTxn []string
824+
sqlDigest string
825+
}{
826+
{"add column", "create table t(a int)", "alter table test.t add column b int", []string{"select 1", "select * from t"}, "[\"begin\",\"select ?\",\"select * from `t`\"]"},
827+
{"change column in 1 step", "create table t(a int)", "alter table test.t change column a b int", []string{"select 1", "select * from t"}, "[\"begin\",\"select ?\",\"select * from `t`\"]"},
828+
}
829+
for _, c := range testCases {
830+
t.Run(c.name, func(t *testing.T) {
831+
// setup suite
832+
s := new(clusterTablesSuite)
833+
s.store, s.dom = testkit.CreateMockStoreAndDomain(t)
834+
s.httpServer, s.mockAddr = s.setUpMockPDHTTPServer()
835+
s.startTime = time.Now()
836+
defer s.httpServer.Close()
833837

834-
tk.MustExec("begin")
835-
tk.MustQuery("select 1;")
836-
tk.MustQuery("select * from t;")
838+
tk := s.newTestKitWithRoot(t)
839+
tkDDL := s.newTestKitWithRoot(t)
840+
tk3 := s.newTestKitWithRoot(t)
841+
tk.MustExec("use test")
842+
tk.MustExec("set global tidb_enable_metadata_lock=1")
843+
tk.MustExec(c.createTable)
844+
845+
tk.MustExec("begin")
846+
for _, q := range c.queryInTxn {
847+
tk.MustQuery(q)
848+
}
837849

838-
var wg sync.WaitGroup
839-
wg.Add(1)
840-
go func() {
841-
tkDDL.MustExec("alter table test.t add column b int;")
842-
wg.Done()
843-
}()
850+
var wg sync.WaitGroup
851+
wg.Add(1)
852+
go func() {
853+
tkDDL.MustExec(c.ddl)
854+
wg.Done()
855+
}()
844856

845-
time.Sleep(200 * time.Millisecond)
857+
time.Sleep(200 * time.Millisecond)
846858

847-
s.rpcserver, s.listenAddr = s.setUpRPCService(t, "127.0.0.1:0", tk3.Session().GetSessionManager())
848-
defer s.rpcserver.Stop()
859+
s.rpcserver, s.listenAddr = s.setUpRPCService(t, "127.0.0.1:0", tk3.Session().GetSessionManager())
860+
defer s.rpcserver.Stop()
849861

850-
tk3.MustQuery("select DB_NAME, QUERY, SQL_DIGESTS from mysql.tidb_mdl_view").Check(testkit.Rows("test alter table test.t add column b int; [\"begin\",\"select ? ;\",\"select * from `t` ;\"]"))
862+
tk3.MustQuery("select DB_NAME, QUERY, SQL_DIGESTS from mysql.tidb_mdl_view").Check(testkit.Rows(
863+
strings.Join([]string{
864+
"test",
865+
c.ddl,
866+
c.sqlDigest,
867+
}, " "),
868+
))
851869

852-
tk.MustExec("commit")
870+
tk.MustExec("commit")
853871

854-
wg.Wait()
872+
wg.Wait()
873+
})
874+
}
855875
}
856876

857877
func TestCreateBindingFromHistory(t *testing.T) {

session/bootstrap.go

+24-2
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,19 @@ const (
441441
);`
442442
// CreateMDLView is a view about metadata locks.
443443
CreateMDLView = `CREATE OR REPLACE VIEW mysql.tidb_mdl_view as (
444-
select JOB_ID, DB_NAME, TABLE_NAME, QUERY, SESSION_ID, TxnStart, TIDB_DECODE_SQL_DIGESTS(ALL_SQL_DIGESTS, 4096) AS SQL_DIGESTS from information_schema.ddl_jobs, information_schema.CLUSTER_TIDB_TRX, information_schema.CLUSTER_PROCESSLIST where ddl_jobs.STATE = 'running' and find_in_set(ddl_jobs.table_id, CLUSTER_TIDB_TRX.RELATED_TABLE_IDS) and CLUSTER_TIDB_TRX.SESSION_ID=CLUSTER_PROCESSLIST.ID
444+
SELECT job_id,
445+
db_name,
446+
table_name,
447+
query,
448+
session_id,
449+
txnstart,
450+
tidb_decode_sql_digests(all_sql_digests, 4096) AS SQL_DIGESTS
451+
FROM information_schema.ddl_jobs,
452+
information_schema.cluster_tidb_trx,
453+
information_schema.cluster_processlist
454+
WHERE (ddl_jobs.state != 'synced' and ddl_jobs.state != 'cancelled')
455+
AND Find_in_set(ddl_jobs.table_id, cluster_tidb_trx.related_table_ids)
456+
AND cluster_tidb_trx.session_id = cluster_processlist.id
445457
);`
446458

447459
// CreatePlanReplayerStatusTable is a table about plan replayer status
@@ -779,11 +791,13 @@ const (
779791
version110 = 110
780792
// version111 adds the table tidb_ttl_task and tidb_ttl_job_history
781793
version111 = 111
794+
// version112 modifies the view tidb_mdl_view
795+
version112 = 112
782796
)
783797

784798
// currentBootstrapVersion is defined as a variable, so we can modify its value for testing.
785799
// please make sure this is the largest version
786-
var currentBootstrapVersion int64 = version111
800+
var currentBootstrapVersion int64 = version112
787801

788802
// DDL owner key's expired time is ManagerSessionTTL seconds, we should wait the time and give more time to have a chance to finish it.
789803
var internalSQLTimeout = owner.ManagerSessionTTL + 15
@@ -902,6 +916,7 @@ var (
902916
upgradeToVer109,
903917
upgradeToVer110,
904918
upgradeToVer111,
919+
upgradeToVer112,
905920
}
906921
)
907922

@@ -2262,6 +2277,13 @@ func upgradeToVer111(s Session, ver int64) {
22622277
doReentrantDDL(s, CreateTTLJobHistory)
22632278
}
22642279

2280+
func upgradeToVer112(s Session, ver int64) {
2281+
if ver >= version112 {
2282+
return
2283+
}
2284+
doReentrantDDL(s, CreateMDLView)
2285+
}
2286+
22652287
func writeOOMAction(s Session) {
22662288
comment := "oom-action is `log` by default in v3.0.x, `cancel` by default in v4.0.11+"
22672289
mustExecute(s, `INSERT HIGH_PRIORITY INTO %n.%n VALUES (%?, %?, %?) ON DUPLICATE KEY UPDATE VARIABLE_VALUE= %?`,

0 commit comments

Comments
 (0)