Skip to content

Commit

Permalink
ddl: fix enable metadata lock (#41913)
Browse files Browse the repository at this point in the history
close #41796
  • Loading branch information
jiyfhust authored Mar 8, 2023
1 parent e1cc3ac commit ed04e26
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
25 changes: 4 additions & 21 deletions ddl/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -1239,33 +1239,16 @@ func (d *ddl) startCleanDeadTableLock() {
}
}

// SwitchMDL enables MDL or disable DDL.
// SwitchMDL enables MDL or disable MDL.
func (d *ddl) SwitchMDL(enable bool) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()

if enable {
sql := fmt.Sprintf("UPDATE HIGH_PRIORITY %[1]s.%[2]s SET VARIABLE_VALUE = %[4]d WHERE VARIABLE_NAME = '%[3]s'",
mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBEnableMDL, 0)
sess, err := d.sessPool.get()
if err != nil {
logutil.BgLogger().Warn("[ddl] get session failed", zap.Error(err))
return nil
}
defer d.sessPool.put(sess)
se := newSession(sess)
_, err = se.execute(ctx, sql, "disableMDL")
if err != nil {
logutil.BgLogger().Warn("[ddl] disable MDL failed", zap.Error(err))
}
return nil
}

isEnableBefore := variable.EnableMDL.Load()
if isEnableBefore == enable {
return nil
}

ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()

// Check if there is any DDL running.
// This check can not cover every corner cases, so users need to guarantee that there is no DDL running by themselves.
sess, err := d.sessPool.get()
Expand Down
21 changes: 21 additions & 0 deletions ddl/metadatalocktest/mdl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,27 @@ func TestMDLEnable2Disable(t *testing.T) {
tk.MustExec("admin check table t")
}

func TestSwitchMDL(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
sv := server.CreateMockServer(t, store)

sv.SetDomain(dom)
dom.InfoSyncer().SetSessionManager(sv)
defer sv.Close()

conn := server.CreateMockConn(t, sv)
tk := testkit.NewTestKitWithSession(t, store, conn.Context().Session)

tk.MustExec("set global tidb_enable_metadata_lock=0")
tk.MustQuery("show global variables like 'tidb_enable_metadata_lock'").Check(testkit.Rows("tidb_enable_metadata_lock OFF"))

tk.MustExec("set global tidb_enable_metadata_lock=1")
tk.MustQuery("show global variables like 'tidb_enable_metadata_lock'").Check(testkit.Rows("tidb_enable_metadata_lock ON"))

tk.MustExec("set global tidb_enable_metadata_lock=0")
tk.MustQuery("show global variables like 'tidb_enable_metadata_lock'").Check(testkit.Rows("tidb_enable_metadata_lock OFF"))
}

func TestMDLViewItself(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
sv := server.CreateMockServer(t, store)
Expand Down

0 comments on commit ed04e26

Please sign in to comment.