Skip to content

Commit 2cfc2d0

Browse files
committed
show more jobs in the tidb_mdl_view
Signed-off-by: YangKeao <yangkeao@chunibyo.icu>
1 parent 97e1563 commit 2cfc2d0

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
@@ -442,7 +442,19 @@ const (
442442
);`
443443
// CreateMDLView is a view about metadata locks.
444444
CreateMDLView = `CREATE OR REPLACE VIEW mysql.tidb_mdl_view as (
445-
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
445+
SELECT job_id,
446+
db_name,
447+
table_name,
448+
query,
449+
session_id,
450+
txnstart,
451+
tidb_decode_sql_digests(all_sql_digests, 4096) AS SQL_DIGESTS
452+
FROM information_schema.ddl_jobs,
453+
information_schema.cluster_tidb_trx,
454+
information_schema.cluster_processlist
455+
WHERE (ddl_jobs.state != 'synced' and ddl_jobs.state != 'cancelled')
456+
AND Find_in_set(ddl_jobs.table_id, cluster_tidb_trx.related_table_ids)
457+
AND cluster_tidb_trx.session_id = cluster_processlist.id
446458
);`
447459

448460
// CreatePlanReplayerStatusTable is a table about plan replayer status
@@ -780,11 +792,13 @@ const (
780792
version110 = 110
781793
// version111 adds the table tidb_ttl_task and tidb_ttl_job_history
782794
version111 = 111
795+
// version112 modifies the view tidb_mdl_view
796+
version112 = 112
783797
)
784798

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

789803
// 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.
790804
var internalSQLTimeout = owner.ManagerSessionTTL + 15
@@ -903,6 +917,7 @@ var (
903917
upgradeToVer109,
904918
upgradeToVer110,
905919
upgradeToVer111,
920+
upgradeToVer112,
906921
}
907922
)
908923

@@ -2263,6 +2278,13 @@ func upgradeToVer111(s Session, ver int64) {
22632278
doReentrantDDL(s, CreateTTLJobHistory)
22642279
}
22652280

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

0 commit comments

Comments
 (0)