Skip to content

Commit

Permalink
test: fix data race caused by update global config (pingcap#17964)
Browse files Browse the repository at this point in the history
Co-authored-by: pingcap-github-bot <sre-bot@pingcap.com>
  • Loading branch information
coocood and sre-bot authored Jun 12, 2020
1 parent 368acb8 commit a8da23c
Show file tree
Hide file tree
Showing 26 changed files with 231 additions and 212 deletions.
16 changes: 16 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,22 @@ func (c *Config) Valid() error {
return l.UnmarshalText([]byte(c.Log.Level))
}

// UpdateGlobal updates the global config, and provide a restore function that can be used to restore to the original.
func UpdateGlobal(f func(conf *Config)) {
g := GetGlobalConfig()
newConf := *g
f(&newConf)
StoreGlobalConfig(&newConf)
}

// RestoreFunc gets a function that restore the config to the current value.
func RestoreFunc() (restore func()) {
g := GetGlobalConfig()
return func() {
StoreGlobalConfig(g)
}
}

func hasRootPrivilege() bool {
return os.Geteuid() == 0
}
Expand Down
8 changes: 6 additions & 2 deletions ddl/db_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ func (s *testStateChangeSuiteBase) TearDownSuite(c *C) {

// TestShowCreateTable tests the result of "show create table" when we are running "add index" or "add column".
func (s *serialTestStateChangeSuite) TestShowCreateTable(c *C) {
config.GetGlobalConfig().Experimental.AllowsExpressionIndex = true
config.UpdateGlobal(func(conf *config.Config) {
conf.Experimental.AllowsExpressionIndex = true
})
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table t (id int)")
Expand Down Expand Up @@ -858,7 +860,9 @@ func (s *testStateChangeSuite) TestParallelAlterAddIndex(c *C) {
}

func (s *serialTestStateChangeSuite) TestParallelAlterAddExpressionIndex(c *C) {
config.GetGlobalConfig().Experimental.AllowsExpressionIndex = true
config.UpdateGlobal(func(conf *config.Config) {
conf.Experimental.AllowsExpressionIndex = true
})
sql1 := "ALTER TABLE t add index expr_index_b((b+1));"
sql2 := "CREATE INDEX expr_index_b ON t ((c+1));"
f := func(c *C, err1, err2 error) {
Expand Down
45 changes: 33 additions & 12 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1712,8 +1712,9 @@ func (s *testIntegrationSuite1) TestTreatOldVersionUTF8AsUTF8MB4(c *C) {
" `b` varchar(10) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL,\n" +
" `c` varchar(10) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))

config.GetGlobalConfig().TreatOldVersionUTF8AsUTF8MB4 = false
config.UpdateGlobal(func(conf *config.Config) {
conf.TreatOldVersionUTF8AsUTF8MB4 = false
})
s.tk.MustExec("alter table t drop column c;") // reload schema.
s.tk.MustGetErrCode("insert into t set a= x'f09f8c80'", errno.ErrTruncatedWrongValueForField)
s.tk.MustQuery("show create table t").Check(testkit.Rows("t CREATE TABLE `t` (\n" +
Expand All @@ -1730,7 +1731,9 @@ func (s *testIntegrationSuite1) TestTreatOldVersionUTF8AsUTF8MB4(c *C) {
tblInfo.Columns[0].Version = model.ColumnInfoVersion0
updateTableInfo(tblInfo)

config.GetGlobalConfig().TreatOldVersionUTF8AsUTF8MB4 = true
config.UpdateGlobal(func(conf *config.Config) {
conf.TreatOldVersionUTF8AsUTF8MB4 = true
})
s.tk.MustExec("alter table t add column c varchar(10);") // load latest schema.
s.tk.MustExec("insert into t set a= x'f09f8c80'")
s.tk.MustQuery("show create table t").Check(testkit.Rows("t CREATE TABLE `t` (\n" +
Expand All @@ -1739,7 +1742,9 @@ func (s *testIntegrationSuite1) TestTreatOldVersionUTF8AsUTF8MB4(c *C) {
" `c` varchar(10) DEFAULT NULL\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))

config.GetGlobalConfig().TreatOldVersionUTF8AsUTF8MB4 = false
config.UpdateGlobal(func(conf *config.Config) {
conf.TreatOldVersionUTF8AsUTF8MB4 = false
})
s.tk.MustExec("alter table t drop column c;") // reload schema.
s.tk.MustGetErrCode("insert into t set a= x'f09f8c80'", errno.ErrTruncatedWrongValueForField)
s.tk.MustQuery("show create table t").Check(testkit.Rows("t CREATE TABLE `t` (\n" +
Expand All @@ -1748,7 +1753,9 @@ func (s *testIntegrationSuite1) TestTreatOldVersionUTF8AsUTF8MB4(c *C) {
") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin"))

// Test modify column charset.
config.GetGlobalConfig().TreatOldVersionUTF8AsUTF8MB4 = true
config.UpdateGlobal(func(conf *config.Config) {
conf.TreatOldVersionUTF8AsUTF8MB4 = true
})
s.tk.MustExec("alter table t modify column a varchar(10) character set utf8mb4") // change column charset.
tbl = testGetTableByName(c, s.ctx, "test", "t")
c.Assert(tbl.Meta().Columns[0].Charset, Equals, charset.CharsetUTF8MB4)
Expand Down Expand Up @@ -1784,7 +1791,9 @@ func (s *testIntegrationSuite1) TestTreatOldVersionUTF8AsUTF8MB4(c *C) {
" `b` varchar(20) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))

config.GetGlobalConfig().TreatOldVersionUTF8AsUTF8MB4 = false
config.UpdateGlobal(func(conf *config.Config) {
conf.TreatOldVersionUTF8AsUTF8MB4 = false
})
s.tk.MustExec("alter table t change column b b varchar(30) character set ascii") // reload schema.
s.tk.MustGetErrCode("insert into t set a= x'f09f8c80'", errno.ErrTruncatedWrongValueForField)
s.tk.MustQuery("show create table t").Check(testkit.Rows("t CREATE TABLE `t` (\n" +
Expand All @@ -1793,11 +1802,15 @@ func (s *testIntegrationSuite1) TestTreatOldVersionUTF8AsUTF8MB4(c *C) {
") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin"))

// Test for alter table convert charset
config.GetGlobalConfig().TreatOldVersionUTF8AsUTF8MB4 = true
config.UpdateGlobal(func(conf *config.Config) {
conf.TreatOldVersionUTF8AsUTF8MB4 = true
})
s.tk.MustExec("alter table t drop column b") // reload schema.
s.tk.MustExec("alter table t convert to charset utf8mb4;")

config.GetGlobalConfig().TreatOldVersionUTF8AsUTF8MB4 = false
config.UpdateGlobal(func(conf *config.Config) {
conf.TreatOldVersionUTF8AsUTF8MB4 = false
})
s.tk.MustExec("alter table t add column b varchar(50);") // reload schema.
s.tk.MustQuery("show create table t").Check(testkit.Rows("t CREATE TABLE `t` (\n" +
" `a` varchar(20) DEFAULT NULL,\n" +
Expand Down Expand Up @@ -2044,7 +2057,9 @@ func (s *testIntegrationSuite3) TestParserIssue284(c *C) {
}

func (s *testIntegrationSuite7) TestAddExpressionIndex(c *C) {
config.GetGlobalConfig().Experimental.AllowsExpressionIndex = true
config.UpdateGlobal(func(conf *config.Config) {
conf.Experimental.AllowsExpressionIndex = true
})
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t;")
Expand Down Expand Up @@ -2096,12 +2111,16 @@ func (s *testIntegrationSuite7) TestAddExpressionIndex(c *C) {
tk.MustExec("alter table t1 alter index ei_ab invisible;")

// Test experiment switch.
config.GetGlobalConfig().Experimental.AllowsExpressionIndex = false
config.UpdateGlobal(func(conf *config.Config) {
conf.Experimental.AllowsExpressionIndex = false
})
tk.MustGetErrMsg("create index d on t((a+1))", "[ddl:8200]Unsupported creating expression index without allow-expression-index in config")
}

func (s *testIntegrationSuite7) TestCreateExpressionIndexError(c *C) {
config.GetGlobalConfig().Experimental.AllowsExpressionIndex = true
config.UpdateGlobal(func(conf *config.Config) {
conf.Experimental.AllowsExpressionIndex = true
})

tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down Expand Up @@ -2263,7 +2282,9 @@ func (s *testIntegrationSuite3) TestCreateTableWithAutoIdCache(c *C) {
}

func (s *testIntegrationSuite4) TestAlterIndexVisibility(c *C) {
config.GetGlobalConfig().Experimental.AllowsExpressionIndex = true
config.UpdateGlobal(func(conf *config.Config) {
conf.Experimental.AllowsExpressionIndex = true
})
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("create database if not exists alter_index_test")
tk.MustExec("USE alter_index_test;")
Expand Down
4 changes: 3 additions & 1 deletion ddl/db_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1138,7 +1138,9 @@ func (s *testIntegrationSuite4) TestExchangePartitionTableCompatiable(c *C) {
}

func (s *testIntegrationSuite7) TestExchangePartitionExpressIndex(c *C) {
config.GetGlobalConfig().Experimental.AllowsExpressionIndex = true
config.UpdateGlobal(func(conf *config.Config) {
conf.Experimental.AllowsExpressionIndex = true
})
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists pt1;")
Expand Down
12 changes: 9 additions & 3 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,9 @@ func (s *testDBSuite2) TestAddUniqueIndexRollback(c *C) {
}

func (s *testSerialDBSuite) TestAddExpressionIndexRollback(c *C) {
config.GetGlobalConfig().Experimental.AllowsExpressionIndex = true
config.UpdateGlobal(func(conf *config.Config) {
conf.Experimental.AllowsExpressionIndex = true
})
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test_db")
tk.MustExec("drop table if exists t1")
Expand Down Expand Up @@ -4454,7 +4456,9 @@ func (s *testDBSuite2) TestTablesLockDelayClean(c *C) {

tk.MustExec("lock tables t1 write")
checkTableLock(c, tk.Se, "test", "t1", model.TableLockWrite)
config.GetGlobalConfig().DelayCleanTableLock = 100
config.UpdateGlobal(func(conf *config.Config) {
conf.DelayCleanTableLock = 100
})
var wg sync.WaitGroup
wg.Add(1)
var startTime time.Time
Expand All @@ -4468,7 +4472,9 @@ func (s *testDBSuite2) TestTablesLockDelayClean(c *C) {
wg.Wait()
c.Assert(time.Since(startTime).Seconds() > 0.1, IsTrue)
checkTableLock(c, tk.Se, "test", "t1", model.TableLockNone)
config.GetGlobalConfig().DelayCleanTableLock = 0
config.UpdateGlobal(func(conf *config.Config) {
conf.DelayCleanTableLock = 0
})
}

// TestConcurrentLockTables test concurrent lock/unlock tables.
Expand Down
15 changes: 7 additions & 8 deletions ddl/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,13 @@ func TestT(t *testing.T) {
ReorgWaitTimeout = 30 * time.Millisecond
batchInsertDeleteRangeSize = 2

cfg := config.GetGlobalConfig()
newCfg := *cfg
// Test for table lock.
newCfg.EnableTableLock = true
newCfg.Log.SlowThreshold = 10000
// Test for add/drop primary key.
newCfg.AlterPrimaryKey = true
config.StoreGlobalConfig(&newCfg)
config.UpdateGlobal(func(conf *config.Config) {
// Test for table lock.
conf.EnableTableLock = true
conf.Log.SlowThreshold = 10000
// Test for add/drop primary key.
conf.AlterPrimaryKey = true
})

testleak.BeforeTest()
TestingT(t)
Expand Down
82 changes: 36 additions & 46 deletions ddl/serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,10 @@ type testSerialSuite struct {
func (s *testSerialSuite) SetUpSuite(c *C) {
session.SetSchemaLease(200 * time.Millisecond)
session.DisableStats4Test()

cfg := config.GetGlobalConfig()
newCfg := *cfg
// Test for add/drop primary key.
newCfg.AlterPrimaryKey = false
config.StoreGlobalConfig(&newCfg)
config.UpdateGlobal(func(conf *config.Config) {
// Test for add/drop primary key.
conf.AlterPrimaryKey = false
})

ddl.SetWaitTimeWhenErrorOccurred(1 * time.Microsecond)

Expand Down Expand Up @@ -95,15 +93,11 @@ func (s *testSerialSuite) TearDownSuite(c *C) {

func (s *testSerialSuite) TestChangeMaxIndexLength(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
cfg := config.GetGlobalConfig()
newCfg := *cfg
originalMaxIndexLen := cfg.MaxIndexLength
newCfg.MaxIndexLength = config.DefMaxOfMaxIndexLength
config.StoreGlobalConfig(&newCfg)
defer func() {
newCfg.MaxIndexLength = originalMaxIndexLen
config.StoreGlobalConfig(&newCfg)
}()

defer config.RestoreFunc()()
config.UpdateGlobal(func(conf *config.Config) {
conf.MaxIndexLength = config.DefMaxOfMaxIndexLength
})

tk.MustExec("create table t (c1 varchar(3073), index(c1)) charset = ascii;")
tk.MustExec(fmt.Sprintf("create table t1 (c1 varchar(%d), index(c1)) charset = ascii;", config.DefMaxOfMaxIndexLength))
Expand Down Expand Up @@ -132,15 +126,10 @@ func (s *testSerialSuite) TestPrimaryKey(c *C) {
tk.MustExec("create table primary_key_test1 (a int, b varchar(10), primary key(a))")
tk.MustExec("create table primary_key_test2 (a int, b varchar(10), primary key(b))")
tk.MustExec("create table primary_key_test3 (a int, b varchar(10))")
cfg := config.GetGlobalConfig()
newCfg := *cfg
orignalAlterPrimaryKey := newCfg.AlterPrimaryKey
newCfg.AlterPrimaryKey = true
config.StoreGlobalConfig(&newCfg)
defer func() {
newCfg.AlterPrimaryKey = orignalAlterPrimaryKey
config.StoreGlobalConfig(&newCfg)
}()
defer config.RestoreFunc()()
config.UpdateGlobal(func(conf *config.Config) {
conf.AlterPrimaryKey = true
})

_, err = tk.Exec("alter table primary_key_test1 drop primary key")
c.Assert(err.Error(), Equals, "[ddl:8200]Unsupported drop primary key when the table's pkIsHandle is true")
Expand All @@ -150,8 +139,9 @@ func (s *testSerialSuite) TestPrimaryKey(c *C) {

// for "drop index `primary` on ..." syntax
tk.MustExec("create table primary_key_test4 (a int, b varchar(10), primary key(a))")
newCfg.AlterPrimaryKey = false
config.StoreGlobalConfig(&newCfg)
config.UpdateGlobal(func(conf *config.Config) {
conf.AlterPrimaryKey = false
})
_, err = tk.Exec("drop index `primary` on primary_key_test4")
c.Assert(err.Error(), Equals, "[ddl:8200]Unsupported drop primary key when alter-primary-key is false")
// for the index name is `primary`
Expand Down Expand Up @@ -814,13 +804,10 @@ func (s *testSerialSuite) TestTableLocksEnable(c *C) {
tk.MustExec("create table t1 (a int)")

// Test for enable table lock config.
cfg := config.GetGlobalConfig()
newCfg := *cfg
newCfg.EnableTableLock = false
config.StoreGlobalConfig(&newCfg)
defer func() {
config.StoreGlobalConfig(cfg)
}()
defer config.RestoreFunc()()
config.UpdateGlobal(func(conf *config.Config) {
conf.EnableTableLock = false
})

tk.MustExec("lock tables t1 write")
checkTableLock(c, tk.Se, "test", "t1", model.TableLockNone)
Expand Down Expand Up @@ -898,11 +885,15 @@ func (s *testSerialSuite) TestAutoRandom(c *C) {
assertPKIsNotHandle("create table t (a bigint auto_random(3), b int, c char, primary key (a, c))", "a")

// PKIsNotHandle: table is created when alter-primary-key = true.
config.GetGlobalConfig().AlterPrimaryKey = true
config.UpdateGlobal(func(conf *config.Config) {
conf.AlterPrimaryKey = true
})
assertPKIsNotHandle("create table t (a bigint auto_random(3) primary key, b int)", "a")
assertPKIsNotHandle("create table t (a bigint auto_random(3) primary key, b int)", "a")
assertPKIsNotHandle("create table t (a int, b bigint auto_random(3) primary key)", "b")
config.GetGlobalConfig().AlterPrimaryKey = false
config.UpdateGlobal(func(conf *config.Config) {
conf.AlterPrimaryKey = false
})

// Can not set auto_random along with auto_increment.
assertWithAutoInc("create table t (a bigint auto_random(3) primary key auto_increment)")
Expand Down Expand Up @@ -1223,15 +1214,10 @@ func (s *testSerialSuite) TestInvisibleIndex(c *C) {
tk.MustExec("insert into t values (11, 12)")
tk.MustQuery("select * from t").Check(testkit.Rows("1 2", "3 4", "5 6", "7 8", "9 10", "11 12"))

cfg := config.GetGlobalConfig()
newCfg := *cfg
orignalAlterPrimaryKey := newCfg.AlterPrimaryKey
newCfg.AlterPrimaryKey = true
config.StoreGlobalConfig(&newCfg)
defer func() {
newCfg.AlterPrimaryKey = orignalAlterPrimaryKey
config.StoreGlobalConfig(&newCfg)
}()
defer config.RestoreFunc()()
config.UpdateGlobal(func(conf *config.Config) {
conf.AlterPrimaryKey = true
})

// Limitation: Primary key cannot be invisible index
tk.MustGetErrCode("create table t1 (a int, primary key (a) invisible)", errno.ErrPKIndexCantBeInvisible)
Expand Down Expand Up @@ -1291,7 +1277,9 @@ func (s *testSerialSuite) TestCreateClusteredIndex(c *C) {
c.Assert(err, IsNil)
c.Assert(tbl.Meta().IsCommonHandle, IsFalse)

config.GetGlobalConfig().AlterPrimaryKey = true
config.UpdateGlobal(func(conf *config.Config) {
conf.AlterPrimaryKey = true
})
tk.MustExec("CREATE TABLE t5 (a varchar(255) primary key, b int)")
tk.MustExec("CREATE TABLE t6 (a int, b int, c int, primary key (a, b))")
is = domain.GetDomain(ctx).InfoSchema()
Expand All @@ -1301,7 +1289,9 @@ func (s *testSerialSuite) TestCreateClusteredIndex(c *C) {
tbl, err = is.TableByName(model.NewCIStr("test"), model.NewCIStr("t6"))
c.Assert(err, IsNil)
c.Assert(tbl.Meta().IsCommonHandle, IsFalse)
config.GetGlobalConfig().AlterPrimaryKey = false
config.UpdateGlobal(func(conf *config.Config) {
conf.AlterPrimaryKey = false
})

tk.MustExec("CREATE TABLE t21 like t2")
tk.MustExec("CREATE TABLE t31 like t3")
Expand Down
11 changes: 5 additions & 6 deletions executor/executor_pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,11 @@ func assertEqualStrings(c *C, got []field, expect []string) {
}

func (s *testExecSerialSuite) TestSortSpillDisk(c *C) {
originCfg := config.GetGlobalConfig()
newConf := *originCfg
newConf.OOMUseTmpStorage = true
newConf.MemQuotaQuery = 1
config.StoreGlobalConfig(&newConf)
defer config.StoreGlobalConfig(originCfg)
defer config.RestoreFunc()()
config.UpdateGlobal(func(conf *config.Config) {
conf.OOMUseTmpStorage = true
conf.MemQuotaQuery = 1
})

ctx := mock.NewContext()
ctx.GetSessionVars().InitChunkSize = variable.DefMaxChunkSize
Expand Down
Loading

0 comments on commit a8da23c

Please sign in to comment.