Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: fix data race caused by config.Labels #22977

Merged
merged 17 commits into from
Mar 1, 2021
Merged
13 changes: 10 additions & 3 deletions config/config_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,22 @@ engines = ["tikv", "tiflash", "tidb"]
}

func (s *testConfigSuite) TestTxnScopeValue(c *C) {
GetGlobalConfig().Labels["zone"] = "bj"
defer RestoreFunc()
UpdateGlobal(func(conf *Config) {
conf.Labels["zone"] = "bj"
})
isGlobal, v := GetTxnScopeFromConfig()
c.Assert(isGlobal, IsFalse)
c.Assert(v, Equals, "bj")
GetGlobalConfig().Labels["zone"] = ""
UpdateGlobal(func(conf *Config) {
conf.Labels["zone"] = ""
})
isGlobal, v = GetTxnScopeFromConfig()
c.Assert(isGlobal, IsTrue)
c.Assert(v, Equals, "global")
GetGlobalConfig().Labels["zone"] = "global"
UpdateGlobal(func(conf *Config) {
conf.Labels["zone"] = "global"
})
isGlobal, v = GetTxnScopeFromConfig()
c.Assert(isGlobal, IsFalse)
c.Assert(v, Equals, "global")
Expand Down
16 changes: 8 additions & 8 deletions ddl/placement_sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ func (s *testDBSuite1) TestPlacementPolicyCache(c *C) {

func (s *testSerialDBSuite) TestTxnScopeConstraint(c *C) {
defer func() {
config.GetGlobalConfig().Labels["zone"] = ""
config.RestoreFunc()
}()
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down Expand Up @@ -569,9 +569,9 @@ PARTITION BY RANGE (c) (

for _, testcase := range testCases {
c.Log(testcase.name)
config.GetGlobalConfig().Labels = map[string]string{
"zone": testcase.zone,
}
config.UpdateGlobal(func(conf *config.Config) {
conf.Labels["zone"] = testcase.zone
})
se, err := session.CreateSession4Test(s.store)
c.Check(err, IsNil)
tk.Se = se
Expand Down Expand Up @@ -662,7 +662,7 @@ add placement policy

func (s *testSerialSuite) TestGlobalTxnState(c *C) {
defer func() {
config.GetGlobalConfig().Labels["zone"] = ""
config.RestoreFunc()
}()
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down Expand Up @@ -704,9 +704,9 @@ PARTITION BY RANGE (c) (
},
},
}
config.GetGlobalConfig().Labels = map[string]string{
"zone": "bj",
}
config.UpdateGlobal(func(conf *config.Config) {
conf.Labels["zone"] = "bj"
})
dbInfo := testGetSchemaByName(c, tk.Se, "test")
tk2 := testkit.NewTestKit(c, s.store)
var chkErr error
Expand Down
8 changes: 4 additions & 4 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7480,7 +7480,7 @@ func (s *testSuite) TestIssue15563(c *C) {

func (s *testSerialSuite) TestStalenessTransaction(c *C) {
defer func() {
config.GetGlobalConfig().Labels["zone"] = ""
config.RestoreFunc()
}()
c.Assert(failpoint.Enable("github.com/pingcap/tidb/executor/mockStalenessTxnSchemaVer", "return(false)"), IsNil)
defer failpoint.Disable("github.com/pingcap/tidb/executor/mockStalenessTxnSchemaVer")
Expand Down Expand Up @@ -7543,9 +7543,9 @@ func (s *testSerialSuite) TestStalenessTransaction(c *C) {
tk.MustExec("use test")
for _, testcase := range testcases {
c.Log(testcase.name)
config.GetGlobalConfig().Labels = map[string]string{
"zone": testcase.zone,
}
config.UpdateGlobal(func(conf *config.Config) {
conf.Labels["zone"] = testcase.zone
})
tk.MustExec(fmt.Sprintf("set @@txn_scope=%v", testcase.txnScope))
tk.MustExec(testcase.preSQL)
tk.MustExec(testcase.sql)
Expand Down
8 changes: 4 additions & 4 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8539,7 +8539,7 @@ func (s *testIntegrationSuite) TestIssue12209(c *C) {

func (s *testIntegrationSerialSuite) TestCrossDCQuery(c *C) {
defer func() {
config.GetGlobalConfig().Labels["zone"] = ""
config.RestoreFunc()
Yisaer marked this conversation as resolved.
Show resolved Hide resolved
}()
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down Expand Up @@ -8656,9 +8656,9 @@ PARTITION BY RANGE (c) (
}
for _, testcase := range testcases {
c.Log(testcase.name)
config.GetGlobalConfig().Labels = map[string]string{
"zone": testcase.zone,
}
config.UpdateGlobal(func(conf *config.Config) {
conf.Labels["zone"] = testcase.zone
})
_, err = tk.Exec(fmt.Sprintf("set @@txn_scope='%v'", testcase.txnScope))
c.Assert(err, IsNil)
res, err := tk.Exec(testcase.sql)
Expand Down
19 changes: 11 additions & 8 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3257,9 +3257,11 @@ func (s *testSessionSuite2) TestPerStmtTaskID(c *C) {

func (s *testSessionSerialSuite) TestSetTxnScope(c *C) {
defer func() {
config.GetGlobalConfig().Labels["zone"] = ""
config.RestoreFunc()
}()
config.GetGlobalConfig().Labels["zone"] = ""
config.UpdateGlobal(func(conf *config.Config) {
conf.Labels["zone"] = ""
})
tk := testkit.NewTestKitWithInit(c, s.store)
// assert default value
result := tk.MustQuery("select @@txn_scope;")
Expand All @@ -3270,8 +3272,9 @@ func (s *testSessionSerialSuite) TestSetTxnScope(c *C) {
result = tk.MustQuery("select @@txn_scope;")
result.Check(testkit.Rows(oracle.GlobalTxnScope))
c.Assert(tk.Se.GetSessionVars().CheckAndGetTxnScope(), Equals, oracle.GlobalTxnScope)

config.GetGlobalConfig().Labels["zone"] = "bj"
config.UpdateGlobal(func(conf *config.Config) {
conf.Labels["zone"] = "bj"
})
tk = testkit.NewTestKitWithInit(c, s.store)
// assert default value
result = tk.MustQuery("select @@txn_scope;")
Expand All @@ -3291,7 +3294,7 @@ func (s *testSessionSerialSuite) TestSetTxnScope(c *C) {

func (s *testSessionSerialSuite) TestGlobalAndLocalTxn(c *C) {
defer func() {
config.GetGlobalConfig().Labels["zone"] = ""
config.RestoreFunc()
}()
// Because the PD config of check_dev_2 test is not compatible with local/global txn yet,
// so we will skip this test for now.
Expand Down Expand Up @@ -3382,9 +3385,9 @@ PARTITION BY RANGE (c) (
result = tk.MustQuery("select * from t1") // read dc-1 and dc-2 with global scope
c.Assert(len(result.Rows()), Equals, 3)

config.GetGlobalConfig().Labels = map[string]string{
"zone": "dc-1",
}
config.UpdateGlobal(func(conf *config.Config) {
conf.Labels["zone"] = "dc-1"
})
// set txn_scope to local
tk.MustExec("set @@session.txn_scope = 'local';")
result = tk.MustQuery("select @@txn_scope;")
Expand Down