Skip to content

Commit

Permalink
ddl: fix race in table lock config (pingcap#10848)
Browse files Browse the repository at this point in the history
  • Loading branch information
crazycs520 committed Apr 28, 2020
1 parent cd9e3bb commit 37f1a21
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
31 changes: 8 additions & 23 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
"github.com/pingcap/tidb/tablecodec"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/admin"
"github.com/pingcap/tidb/util/israce"
"github.com/pingcap/tidb/util/mock"
"github.com/pingcap/tidb/util/testkit"
"github.com/pingcap/tidb/util/testutil"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -3224,18 +3227,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)
Expand Down Expand Up @@ -3411,7 +3402,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
Expand All @@ -3421,15 +3415,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"
Expand Down Expand Up @@ -3462,7 +3447,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) {
Expand Down
20 changes: 19 additions & 1 deletion ddl/serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package ddl_test
import (
"context"
"fmt"
"github.com/pingcap/tidb/config"
"math"
"strings"
"sync"
Expand All @@ -27,6 +26,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"
Expand Down Expand Up @@ -802,3 +802,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)
}

0 comments on commit 37f1a21

Please sign in to comment.