From 7572b6866844b1cc81014bd51715447dde893e2d Mon Sep 17 00:00:00 2001 From: Song Gao Date: Fri, 26 Feb 2021 17:00:14 +0800 Subject: [PATCH 01/10] fix race Signed-off-by: Song Gao --- config/config_util_test.go | 17 ++++++++++++++--- ddl/placement_sql_test.go | 16 ++++++++-------- executor/executor_test.go | 8 ++++---- expression/integration_test.go | 8 ++++---- session/session_test.go | 19 +++++++++++-------- 5 files changed, 41 insertions(+), 27 deletions(-) diff --git a/config/config_util_test.go b/config/config_util_test.go index 2b2056a309415..1cbd850bbc543 100644 --- a/config/config_util_test.go +++ b/config/config_util_test.go @@ -170,16 +170,27 @@ 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") } + +func SetLabelConfig() { + GetGlobalConfig() +} diff --git a/ddl/placement_sql_test.go b/ddl/placement_sql_test.go index 9c98942653b97..56694e97a42f5 100644 --- a/ddl/placement_sql_test.go +++ b/ddl/placement_sql_test.go @@ -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") @@ -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 @@ -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") @@ -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 diff --git a/executor/executor_test.go b/executor/executor_test.go index d672f45cc9438..4772423925b06 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -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") @@ -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) diff --git a/expression/integration_test.go b/expression/integration_test.go index 189efa4a16826..cdc720be6322c 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -8539,7 +8539,7 @@ func (s *testIntegrationSuite) TestIssue12209(c *C) { func (s *testIntegrationSerialSuite) TestCrossDCQuery(c *C) { defer func() { - config.GetGlobalConfig().Labels["zone"] = "" + config.RestoreFunc() }() tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") @@ -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) diff --git a/session/session_test.go b/session/session_test.go index 83c660c9edfe4..5e86f404d1896 100644 --- a/session/session_test.go +++ b/session/session_test.go @@ -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;") @@ -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;") @@ -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. @@ -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;") From 609267a361db0c0fbc8d89f5d28798da701fda6c Mon Sep 17 00:00:00 2001 From: Song Gao Date: Fri, 26 Feb 2021 17:28:22 +0800 Subject: [PATCH 02/10] fix lint Signed-off-by: Song Gao --- config/config_util_test.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/config/config_util_test.go b/config/config_util_test.go index 1cbd850bbc543..d047689adf4ef 100644 --- a/config/config_util_test.go +++ b/config/config_util_test.go @@ -190,7 +190,3 @@ func (s *testConfigSuite) TestTxnScopeValue(c *C) { c.Assert(isGlobal, IsFalse) c.Assert(v, Equals, "global") } - -func SetLabelConfig() { - GetGlobalConfig() -} From c17888733e24a78a8023fcd9e84b6cfabe9b8b6a Mon Sep 17 00:00:00 2001 From: Song Gao Date: Fri, 26 Feb 2021 17:56:15 +0800 Subject: [PATCH 03/10] address the comment Signed-off-by: Song Gao --- ddl/placement_sql_test.go | 8 ++------ executor/executor_test.go | 4 +--- expression/integration_test.go | 4 +--- session/session_test.go | 8 ++------ 4 files changed, 6 insertions(+), 18 deletions(-) diff --git a/ddl/placement_sql_test.go b/ddl/placement_sql_test.go index 56694e97a42f5..b9fad55c06d64 100644 --- a/ddl/placement_sql_test.go +++ b/ddl/placement_sql_test.go @@ -440,9 +440,7 @@ func (s *testDBSuite1) TestPlacementPolicyCache(c *C) { } func (s *testSerialDBSuite) TestTxnScopeConstraint(c *C) { - defer func() { - config.RestoreFunc() - }() + defer config.RestoreFunc() tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") tk.MustExec("drop table if exists t1") @@ -661,9 +659,7 @@ add placement policy } func (s *testSerialSuite) TestGlobalTxnState(c *C) { - defer func() { - config.RestoreFunc() - }() + defer config.RestoreFunc() tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") tk.MustExec("drop table if exists t1") diff --git a/executor/executor_test.go b/executor/executor_test.go index 4772423925b06..df7cc322dbf1a 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -7479,9 +7479,7 @@ func (s *testSuite) TestIssue15563(c *C) { } func (s *testSerialSuite) TestStalenessTransaction(c *C) { - defer func() { - config.RestoreFunc() - }() + defer config.RestoreFunc() c.Assert(failpoint.Enable("github.com/pingcap/tidb/executor/mockStalenessTxnSchemaVer", "return(false)"), IsNil) defer failpoint.Disable("github.com/pingcap/tidb/executor/mockStalenessTxnSchemaVer") testcases := []struct { diff --git a/expression/integration_test.go b/expression/integration_test.go index cdc720be6322c..079a1d5c0bb8f 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -8538,9 +8538,7 @@ func (s *testIntegrationSuite) TestIssue12209(c *C) { } func (s *testIntegrationSerialSuite) TestCrossDCQuery(c *C) { - defer func() { - config.RestoreFunc() - }() + defer config.RestoreFunc() tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") tk.MustExec("drop table if exists t1") diff --git a/session/session_test.go b/session/session_test.go index 5e86f404d1896..d5a6933716b35 100644 --- a/session/session_test.go +++ b/session/session_test.go @@ -3256,9 +3256,7 @@ func (s *testSessionSuite2) TestPerStmtTaskID(c *C) { } func (s *testSessionSerialSuite) TestSetTxnScope(c *C) { - defer func() { - config.RestoreFunc() - }() + defer config.RestoreFunc() config.UpdateGlobal(func(conf *config.Config) { conf.Labels["zone"] = "" }) @@ -3293,9 +3291,7 @@ func (s *testSessionSerialSuite) TestSetTxnScope(c *C) { } func (s *testSessionSerialSuite) TestGlobalAndLocalTxn(c *C) { - defer func() { - config.RestoreFunc() - }() + defer 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. if *withTiKV { From 637dfb65d9034f35297a4a8b625aa0c85f3ed092 Mon Sep 17 00:00:00 2001 From: Song Gao Date: Mon, 1 Mar 2021 11:41:58 +0800 Subject: [PATCH 04/10] fix test --- config/config_util_test.go | 2 +- ddl/placement_sql_test.go | 4 ++-- executor/executor_test.go | 2 +- expression/integration_test.go | 2 +- session/session_test.go | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/config/config_util_test.go b/config/config_util_test.go index d047689adf4ef..5d45589c06d88 100644 --- a/config/config_util_test.go +++ b/config/config_util_test.go @@ -170,7 +170,7 @@ engines = ["tikv", "tiflash", "tidb"] } func (s *testConfigSuite) TestTxnScopeValue(c *C) { - defer RestoreFunc() + defer RestoreFunc()() UpdateGlobal(func(conf *Config) { conf.Labels["zone"] = "bj" }) diff --git a/ddl/placement_sql_test.go b/ddl/placement_sql_test.go index b9fad55c06d64..2601a88b59e34 100644 --- a/ddl/placement_sql_test.go +++ b/ddl/placement_sql_test.go @@ -440,7 +440,7 @@ func (s *testDBSuite1) TestPlacementPolicyCache(c *C) { } func (s *testSerialDBSuite) TestTxnScopeConstraint(c *C) { - defer config.RestoreFunc() + defer config.RestoreFunc()() tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") tk.MustExec("drop table if exists t1") @@ -659,7 +659,7 @@ add placement policy } func (s *testSerialSuite) TestGlobalTxnState(c *C) { - defer config.RestoreFunc() + defer config.RestoreFunc()() tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") tk.MustExec("drop table if exists t1") diff --git a/executor/executor_test.go b/executor/executor_test.go index df7cc322dbf1a..8595b6ce01323 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -7479,7 +7479,7 @@ func (s *testSuite) TestIssue15563(c *C) { } func (s *testSerialSuite) TestStalenessTransaction(c *C) { - defer config.RestoreFunc() + defer config.RestoreFunc()() c.Assert(failpoint.Enable("github.com/pingcap/tidb/executor/mockStalenessTxnSchemaVer", "return(false)"), IsNil) defer failpoint.Disable("github.com/pingcap/tidb/executor/mockStalenessTxnSchemaVer") testcases := []struct { diff --git a/expression/integration_test.go b/expression/integration_test.go index 079a1d5c0bb8f..706f71adcb169 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -8538,7 +8538,7 @@ func (s *testIntegrationSuite) TestIssue12209(c *C) { } func (s *testIntegrationSerialSuite) TestCrossDCQuery(c *C) { - defer config.RestoreFunc() + defer config.RestoreFunc()() tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") tk.MustExec("drop table if exists t1") diff --git a/session/session_test.go b/session/session_test.go index d5a6933716b35..c31c0716b96e9 100644 --- a/session/session_test.go +++ b/session/session_test.go @@ -3256,7 +3256,7 @@ func (s *testSessionSuite2) TestPerStmtTaskID(c *C) { } func (s *testSessionSerialSuite) TestSetTxnScope(c *C) { - defer config.RestoreFunc() + defer config.RestoreFunc()() config.UpdateGlobal(func(conf *config.Config) { conf.Labels["zone"] = "" }) @@ -3291,7 +3291,7 @@ func (s *testSessionSerialSuite) TestSetTxnScope(c *C) { } func (s *testSessionSerialSuite) TestGlobalAndLocalTxn(c *C) { - defer config.RestoreFunc() + defer 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. if *withTiKV { From 5b00a48f4670c7ba0f7020554c8b09a51ef0252a Mon Sep 17 00:00:00 2001 From: Song Gao Date: Mon, 1 Mar 2021 11:54:24 +0800 Subject: [PATCH 05/10] fix test Signed-off-by: Song Gao --- config/config_util_test.go | 6 +++++- ddl/placement_sql_test.go | 12 ++++++++++-- executor/executor_test.go | 6 +++++- expression/integration_test.go | 6 +++++- session/session_test.go | 12 ++++++++++-- 5 files changed, 35 insertions(+), 7 deletions(-) diff --git a/config/config_util_test.go b/config/config_util_test.go index 5d45589c06d88..5fa6212791931 100644 --- a/config/config_util_test.go +++ b/config/config_util_test.go @@ -170,7 +170,11 @@ engines = ["tikv", "tiflash", "tidb"] } func (s *testConfigSuite) TestTxnScopeValue(c *C) { - defer RestoreFunc()() + defer func() { + UpdateGlobal(func(conf *Config) { + conf.Labels = map[string]string{} + }) + }() UpdateGlobal(func(conf *Config) { conf.Labels["zone"] = "bj" }) diff --git a/ddl/placement_sql_test.go b/ddl/placement_sql_test.go index 2601a88b59e34..a322462999d6f 100644 --- a/ddl/placement_sql_test.go +++ b/ddl/placement_sql_test.go @@ -440,7 +440,11 @@ func (s *testDBSuite1) TestPlacementPolicyCache(c *C) { } func (s *testSerialDBSuite) TestTxnScopeConstraint(c *C) { - defer config.RestoreFunc()() + defer func() { + config.UpdateGlobal(func(conf *config.Config) { + conf.Labels = map[string]string{} + }) + }() tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") tk.MustExec("drop table if exists t1") @@ -659,7 +663,11 @@ add placement policy } func (s *testSerialSuite) TestGlobalTxnState(c *C) { - defer config.RestoreFunc()() + defer func() { + config.UpdateGlobal(func(conf *config.Config) { + conf.Labels = map[string]string{} + }) + }() tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") tk.MustExec("drop table if exists t1") diff --git a/executor/executor_test.go b/executor/executor_test.go index 8595b6ce01323..0bc8ba40e6dcc 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -7479,7 +7479,11 @@ func (s *testSuite) TestIssue15563(c *C) { } func (s *testSerialSuite) TestStalenessTransaction(c *C) { - defer config.RestoreFunc()() + defer func() { + config.UpdateGlobal(func(conf *config.Config) { + conf.Labels = map[string]string{} + }) + }() c.Assert(failpoint.Enable("github.com/pingcap/tidb/executor/mockStalenessTxnSchemaVer", "return(false)"), IsNil) defer failpoint.Disable("github.com/pingcap/tidb/executor/mockStalenessTxnSchemaVer") testcases := []struct { diff --git a/expression/integration_test.go b/expression/integration_test.go index 706f71adcb169..7620f7c6a6156 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -8538,7 +8538,11 @@ func (s *testIntegrationSuite) TestIssue12209(c *C) { } func (s *testIntegrationSerialSuite) TestCrossDCQuery(c *C) { - defer config.RestoreFunc()() + defer func() { + config.UpdateGlobal(func(conf *config.Config) { + conf.Labels = map[string]string{} + }) + }() tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") tk.MustExec("drop table if exists t1") diff --git a/session/session_test.go b/session/session_test.go index 32c15a32c706a..acab83bc3724f 100644 --- a/session/session_test.go +++ b/session/session_test.go @@ -3256,7 +3256,11 @@ func (s *testSessionSuite2) TestPerStmtTaskID(c *C) { } func (s *testSessionSerialSuite) TestSetTxnScope(c *C) { - defer config.RestoreFunc()() + defer func() { + config.UpdateGlobal(func(conf *config.Config) { + conf.Labels = map[string]string{} + }) + }() config.UpdateGlobal(func(conf *config.Config) { conf.Labels["zone"] = "" }) @@ -3291,7 +3295,11 @@ func (s *testSessionSerialSuite) TestSetTxnScope(c *C) { } func (s *testSessionSerialSuite) TestGlobalAndLocalTxn(c *C) { - defer config.RestoreFunc()() + defer func() { + config.UpdateGlobal(func(conf *config.Config) { + conf.Labels = map[string]string{} + }) + }() // 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. if *withTiKV { From 47e3e38cd0d56671d1f2c142fea41daf26cac427 Mon Sep 17 00:00:00 2001 From: Song Gao Date: Mon, 1 Mar 2021 14:37:20 +0800 Subject: [PATCH 06/10] fix race test Signed-off-by: Song Gao --- config/config_util.go | 8 ++++++++ config/config_util_test.go | 21 +++++++-------------- ddl/placement_sql_test.go | 23 ++++++----------------- executor/executor_test.go | 11 +++-------- expression/integration_test.go | 7 ++++--- session/session_test.go | 15 ++++----------- 6 files changed, 32 insertions(+), 53 deletions(-) diff --git a/config/config_util.go b/config/config_util.go index 6018359802b5d..ce54255369507 100644 --- a/config/config_util.go +++ b/config/config_util.go @@ -25,6 +25,7 @@ import ( "github.com/BurntSushi/toml" "github.com/pingcap/errors" + "github.com/pingcap/failpoint" ) // CloneConf deeply clones this config. @@ -161,6 +162,13 @@ const ( // GetTxnScopeFromConfig extracts @@txn_scope value from config func GetTxnScopeFromConfig() (bool, string) { + failpoint.Inject("injectTxnScope", func(val failpoint.Value) { + v := val.(string) + if len(v) > 0 { + failpoint.Return(false, v) + } + failpoint.Return(true, globalTxnScope) + }) v, ok := GetGlobalConfig().Labels["zone"] if ok && len(v) > 0 { return false, v diff --git a/config/config_util_test.go b/config/config_util_test.go index 5fa6212791931..3ed621758f757 100644 --- a/config/config_util_test.go +++ b/config/config_util_test.go @@ -23,6 +23,7 @@ import ( "github.com/BurntSushi/toml" . "github.com/pingcap/check" + "github.com/pingcap/failpoint" ) func (s *testConfigSuite) TestCloneConf(c *C) { @@ -170,27 +171,19 @@ engines = ["tikv", "tiflash", "tidb"] } func (s *testConfigSuite) TestTxnScopeValue(c *C) { - defer func() { - UpdateGlobal(func(conf *Config) { - conf.Labels = map[string]string{} - }) - }() - UpdateGlobal(func(conf *Config) { - conf.Labels["zone"] = "bj" - }) + failpoint.Enable("github.com/pingcap/tidb/config/injectTxnScope", `return("bj")`) isGlobal, v := GetTxnScopeFromConfig() c.Assert(isGlobal, IsFalse) c.Assert(v, Equals, "bj") - UpdateGlobal(func(conf *Config) { - conf.Labels["zone"] = "" - }) + failpoint.Disable("github.com/pingcap/tidb/config/injectTxnScope") + failpoint.Enable("github.com/pingcap/tidb/config/injectTxnScope", `return("")`) isGlobal, v = GetTxnScopeFromConfig() c.Assert(isGlobal, IsTrue) c.Assert(v, Equals, "global") - UpdateGlobal(func(conf *Config) { - conf.Labels["zone"] = "global" - }) + failpoint.Disable("github.com/pingcap/tidb/config/injectTxnScope") + failpoint.Enable("github.com/pingcap/tidb/config/injectTxnScope", `return("global")`) isGlobal, v = GetTxnScopeFromConfig() c.Assert(isGlobal, IsFalse) c.Assert(v, Equals, "global") + failpoint.Disable("github.com/pingcap/tidb/config/injectTxnScope") } diff --git a/ddl/placement_sql_test.go b/ddl/placement_sql_test.go index a322462999d6f..91c48528a986a 100644 --- a/ddl/placement_sql_test.go +++ b/ddl/placement_sql_test.go @@ -18,8 +18,8 @@ import ( "sort" . "github.com/pingcap/check" + "github.com/pingcap/failpoint" "github.com/pingcap/parser/model" - "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/ddl" "github.com/pingcap/tidb/ddl/placement" "github.com/pingcap/tidb/session" @@ -440,11 +440,6 @@ func (s *testDBSuite1) TestPlacementPolicyCache(c *C) { } func (s *testSerialDBSuite) TestTxnScopeConstraint(c *C) { - defer func() { - config.UpdateGlobal(func(conf *config.Config) { - conf.Labels = map[string]string{} - }) - }() tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") tk.MustExec("drop table if exists t1") @@ -571,9 +566,8 @@ PARTITION BY RANGE (c) ( for _, testcase := range testCases { c.Log(testcase.name) - config.UpdateGlobal(func(conf *config.Config) { - conf.Labels["zone"] = testcase.zone - }) + failpoint.Enable("github.com/pingcap/tidb/config/injectTxnScope", + fmt.Sprintf(`return("%v")`, testcase.zone)) se, err := session.CreateSession4Test(s.store) c.Check(err, IsNil) tk.Se = se @@ -593,6 +587,7 @@ PARTITION BY RANGE (c) ( c.Assert(err, NotNil) c.Assert(err.Error(), Matches, testcase.err.Error()) } + failpoint.Disable("github.com/pingcap/tidb/config/injectTxnScope") } } @@ -663,11 +658,6 @@ add placement policy } func (s *testSerialSuite) TestGlobalTxnState(c *C) { - defer func() { - config.UpdateGlobal(func(conf *config.Config) { - conf.Labels = map[string]string{} - }) - }() tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") tk.MustExec("drop table if exists t1") @@ -708,9 +698,8 @@ PARTITION BY RANGE (c) ( }, }, } - config.UpdateGlobal(func(conf *config.Config) { - conf.Labels["zone"] = "bj" - }) + failpoint.Enable("github.com/pingcap/tidb/config/injectTxnScope", `return("bj")`) + defer failpoint.Disable("github.com/pingcap/tidb/config/injectTxnScope") dbInfo := testGetSchemaByName(c, tk.Se, "test") tk2 := testkit.NewTestKit(c, s.store) var chkErr error diff --git a/executor/executor_test.go b/executor/executor_test.go index 6b3abfe8793aa..e47a122fce543 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -7480,11 +7480,6 @@ func (s *testSuite) TestIssue15563(c *C) { } func (s *testSerialSuite) TestStalenessTransaction(c *C) { - defer func() { - config.UpdateGlobal(func(conf *config.Config) { - conf.Labels = map[string]string{} - }) - }() c.Assert(failpoint.Enable("github.com/pingcap/tidb/executor/mockStalenessTxnSchemaVer", "return(false)"), IsNil) defer failpoint.Disable("github.com/pingcap/tidb/executor/mockStalenessTxnSchemaVer") testcases := []struct { @@ -7546,9 +7541,8 @@ func (s *testSerialSuite) TestStalenessTransaction(c *C) { tk.MustExec("use test") for _, testcase := range testcases { c.Log(testcase.name) - config.UpdateGlobal(func(conf *config.Config) { - conf.Labels["zone"] = testcase.zone - }) + failpoint.Enable("github.com/pingcap/tidb/config/injectTxnScope", + fmt.Sprintf(`return("%v")`, testcase.zone)) tk.MustExec(fmt.Sprintf("set @@txn_scope=%v", testcase.txnScope)) tk.MustExec(testcase.preSQL) tk.MustExec(testcase.sql) @@ -7567,6 +7561,7 @@ func (s *testSerialSuite) TestStalenessTransaction(c *C) { } c.Assert(tk.Se.GetSessionVars().TxnCtx.IsStaleness, Equals, testcase.IsStaleness) tk.MustExec("commit") + failpoint.Disable("github.com/pingcap/tidb/config/injectTxnScope") } } diff --git a/expression/integration_test.go b/expression/integration_test.go index 7620f7c6a6156..edd1f97306cba 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -26,6 +26,7 @@ import ( . "github.com/pingcap/check" "github.com/pingcap/errors" + "github.com/pingcap/failpoint" "github.com/pingcap/parser/auth" "github.com/pingcap/parser/model" "github.com/pingcap/parser/mysql" @@ -8658,9 +8659,8 @@ PARTITION BY RANGE (c) ( } for _, testcase := range testcases { c.Log(testcase.name) - config.UpdateGlobal(func(conf *config.Config) { - conf.Labels["zone"] = testcase.zone - }) + failpoint.Enable("github.com/pingcap/tidb/config/injectTxnScope", + fmt.Sprintf(`return("%v")`, testcase.zone)) _, err = tk.Exec(fmt.Sprintf("set @@txn_scope='%v'", testcase.txnScope)) c.Assert(err, IsNil) res, err := tk.Exec(testcase.sql) @@ -8677,6 +8677,7 @@ PARTITION BY RANGE (c) ( } else { c.Assert(checkErr, IsNil) } + failpoint.Disable("github.com/pingcap/tidb/config/injectTxnScope") } } diff --git a/session/session_test.go b/session/session_test.go index 04785e079562d..801e514236511 100644 --- a/session/session_test.go +++ b/session/session_test.go @@ -3257,14 +3257,7 @@ func (s *testSessionSuite2) TestPerStmtTaskID(c *C) { } func (s *testSessionSerialSuite) TestSetTxnScope(c *C) { - defer func() { - config.UpdateGlobal(func(conf *config.Config) { - conf.Labels = map[string]string{} - }) - }() - config.UpdateGlobal(func(conf *config.Config) { - conf.Labels["zone"] = "" - }) + failpoint.Enable("github.com/pingcap/tidb/config/injectTxnScope",`return("")`) tk := testkit.NewTestKitWithInit(c, s.store) // assert default value result := tk.MustQuery("select @@txn_scope;") @@ -3275,9 +3268,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.UpdateGlobal(func(conf *config.Config) { - conf.Labels["zone"] = "bj" - }) + failpoint.Disable("github.com/pingcap/tidb/config/injectTxnScope") + failpoint.Enable("github.com/pingcap/tidb/config/injectTxnScope",`return("bj")`) + defer failpoint.Disable("github.com/pingcap/tidb/config/injectTxnScope") tk = testkit.NewTestKitWithInit(c, s.store) // assert default value result = tk.MustQuery("select @@txn_scope;") From 2ef2859bcc41e98df19065b1265b722cf718113d Mon Sep 17 00:00:00 2001 From: Song Gao Date: Mon, 1 Mar 2021 14:40:59 +0800 Subject: [PATCH 07/10] make fmt --- session/session_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/session/session_test.go b/session/session_test.go index 801e514236511..e9a9a0e538f2d 100644 --- a/session/session_test.go +++ b/session/session_test.go @@ -3257,7 +3257,7 @@ func (s *testSessionSuite2) TestPerStmtTaskID(c *C) { } func (s *testSessionSerialSuite) TestSetTxnScope(c *C) { - failpoint.Enable("github.com/pingcap/tidb/config/injectTxnScope",`return("")`) + failpoint.Enable("github.com/pingcap/tidb/config/injectTxnScope", `return("")`) tk := testkit.NewTestKitWithInit(c, s.store) // assert default value result := tk.MustQuery("select @@txn_scope;") @@ -3269,7 +3269,7 @@ func (s *testSessionSerialSuite) TestSetTxnScope(c *C) { result.Check(testkit.Rows(oracle.GlobalTxnScope)) c.Assert(tk.Se.GetSessionVars().CheckAndGetTxnScope(), Equals, oracle.GlobalTxnScope) failpoint.Disable("github.com/pingcap/tidb/config/injectTxnScope") - failpoint.Enable("github.com/pingcap/tidb/config/injectTxnScope",`return("bj")`) + failpoint.Enable("github.com/pingcap/tidb/config/injectTxnScope", `return("bj")`) defer failpoint.Disable("github.com/pingcap/tidb/config/injectTxnScope") tk = testkit.NewTestKitWithInit(c, s.store) // assert default value From d60e4ee109838a34ebc03fa69480a716ef0dc432 Mon Sep 17 00:00:00 2001 From: Song Gao Date: Mon, 1 Mar 2021 15:00:42 +0800 Subject: [PATCH 08/10] Update session_test.go --- session/session_test.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/session/session_test.go b/session/session_test.go index e9a9a0e538f2d..124d7e4db4406 100644 --- a/session/session_test.go +++ b/session/session_test.go @@ -3289,11 +3289,6 @@ func (s *testSessionSerialSuite) TestSetTxnScope(c *C) { } func (s *testSessionSerialSuite) TestGlobalAndLocalTxn(c *C) { - defer func() { - config.UpdateGlobal(func(conf *config.Config) { - conf.Labels = map[string]string{} - }) - }() // 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. if *withTiKV { @@ -3383,9 +3378,8 @@ 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.UpdateGlobal(func(conf *config.Config) { - conf.Labels["zone"] = "dc-1" - }) + failpoint.Enable("github.com/pingcap/tidb/config/injectTxnScope", `return("dc-1")`) + defer failpoint.Disable("github.com/pingcap/tidb/config/injectTxnScope") // set txn_scope to local tk.MustExec("set @@session.txn_scope = 'local';") result = tk.MustQuery("select @@txn_scope;") From 898adb2fd644f00fd6cc602b0a6dd219214d52d4 Mon Sep 17 00:00:00 2001 From: Song Gao Date: Mon, 1 Mar 2021 15:04:17 +0800 Subject: [PATCH 09/10] Update integration_test.go --- expression/integration_test.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/expression/integration_test.go b/expression/integration_test.go index edd1f97306cba..77b9ee6d060f4 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -8539,11 +8539,6 @@ func (s *testIntegrationSuite) TestIssue12209(c *C) { } func (s *testIntegrationSerialSuite) TestCrossDCQuery(c *C) { - defer func() { - config.UpdateGlobal(func(conf *config.Config) { - conf.Labels = map[string]string{} - }) - }() tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") tk.MustExec("drop table if exists t1") From e484fec13953689dba062f223cb6e9ccc53503eb Mon Sep 17 00:00:00 2001 From: Song Gao Date: Mon, 1 Mar 2021 15:07:09 +0800 Subject: [PATCH 10/10] make vet --- expression/integration_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/expression/integration_test.go b/expression/integration_test.go index 77b9ee6d060f4..bd0b8e673e250 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -31,7 +31,6 @@ import ( "github.com/pingcap/parser/model" "github.com/pingcap/parser/mysql" "github.com/pingcap/parser/terror" - "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/ddl/placement" "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/expression"