From ba7777a159a01ff55422e609d288ffd701f103bc Mon Sep 17 00:00:00 2001 From: crazycs Date: Thu, 20 Jun 2019 10:47:05 +0800 Subject: [PATCH] ddl: fix race in table lock config (#10848) --- ddl/db_test.go | 31 ++++++++----------------------- ddl/serial_test.go | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/ddl/db_test.go b/ddl/db_test.go index 5de33ac90e6de..2927cff560cba 100644 --- a/ddl/db_test.go +++ b/ddl/db_test.go @@ -16,6 +16,7 @@ package ddl_test import ( "context" "fmt" + "github.com/pingcap/tidb/util/israce" "io" "math" "math/rand" @@ -92,6 +93,8 @@ func setUpSuite(s *testDBSuite, c *C) { s.schemaName = "test_db" s.autoIDStep = autoid.GetStep() ddl.WaitTimeWhenErrorOccured = 0 + // Test for table lock. + config.GetGlobalConfig().EnableTableLock = true s.cluster = mocktikv.NewCluster() mocktikv.BootstrapWithSingleStore(s.cluster) @@ -3211,18 +3214,6 @@ func (s *testDBSuite2) TestLockTables(c *C) { tk.MustExec("create table t1 (a int)") tk.MustExec("create table t2 (a int)") - // recover table lock config. - originValue := config.GetGlobalConfig().EnableTableLock - defer func() { - config.GetGlobalConfig().EnableTableLock = originValue - }() - - // Test for enable table lock config. - config.GetGlobalConfig().EnableTableLock = false - tk.MustExec("lock tables t1 write") - checkTableLock(c, tk.Se, "test", "t1", model.TableLockNone) - config.GetGlobalConfig().EnableTableLock = true - // Test lock 1 table. tk.MustExec("lock tables t1 write") checkTableLock(c, tk.Se, "test", "t1", model.TableLockWrite) @@ -3414,7 +3405,10 @@ func (s *testDBSuite2) TestLockTables(c *C) { } // TestConcurrentLockTables test concurrent lock/unlock tables. -func (s *testDBSuite2) TestConcurrentLockTables(c *C) { +func (s *testDBSuite4) TestConcurrentLockTables(c *C) { + if israce.RaceEnabled { + c.Skip("skip race test") + } s.tk = testkit.NewTestKit(c, s.store) tk2 := testkit.NewTestKit(c, s.store) tk := s.tk @@ -3424,15 +3418,6 @@ func (s *testDBSuite2) TestConcurrentLockTables(c *C) { tk.MustExec("create table t1 (a int)") tk2.MustExec("use test") - // recover table lock config. - originValue := config.GetGlobalConfig().EnableTableLock - defer func() { - config.GetGlobalConfig().EnableTableLock = originValue - }() - - // Test for enable table lock config. - config.GetGlobalConfig().EnableTableLock = true - // Test concurrent lock tables read. sql1 := "lock tables t1 read" sql2 := "lock tables t1 read" @@ -3465,7 +3450,7 @@ func (s *testDBSuite2) TestConcurrentLockTables(c *C) { tk2.MustExec("unlock tables") } -func (s *testDBSuite2) testParallelExecSQL(c *C, sql1, sql2 string, se1, se2 session.Session, f checkRet) { +func (s *testDBSuite4) testParallelExecSQL(c *C, sql1, sql2 string, se1, se2 session.Session, f checkRet) { callback := &ddl.TestDDLCallback{} times := 0 callback.OnJobRunBeforeExported = func(job *model.Job) { diff --git a/ddl/serial_test.go b/ddl/serial_test.go index 6b6d73d2d86fd..965823d7cb8ef 100644 --- a/ddl/serial_test.go +++ b/ddl/serial_test.go @@ -27,6 +27,7 @@ import ( "github.com/pingcap/failpoint" "github.com/pingcap/parser/model" "github.com/pingcap/parser/mysql" + "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/ddl" "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/infoschema" @@ -743,3 +744,21 @@ func (s *testSerialSuite) TestCanceledJobTakeTime(c *C) { sub := time.Since(startTime) c.Assert(sub, Less, ddl.WaitTimeWhenErrorOccured) } + +func (s *testSerialSuite) TestTableLocksEnable(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t1") + defer tk.MustExec("drop table if exists t1") + tk.MustExec("create table t1 (a int)") + // recover table lock config. + originValue := config.GetGlobalConfig().EnableTableLock + defer func() { + config.GetGlobalConfig().EnableTableLock = originValue + }() + + // Test for enable table lock config. + config.GetGlobalConfig().EnableTableLock = false + tk.MustExec("lock tables t1 write") + checkTableLock(c, tk.Se, "test", "t1", model.TableLockNone) +}