Skip to content

Commit

Permalink
*: cherry-pick #17964 and update pd to v4.0.4 (#19105)
Browse files Browse the repository at this point in the history
* test: fix data race caused by update global config (#17964)

Co-authored-by: pingcap-github-bot <sre-bot@pingcap.com>
Signed-off-by: Neil Shen <overvenus@gmail.com>

* fix make dev

Signed-off-by: Neil Shen <overvenus@gmail.com>

* update kvproto and pd

Signed-off-by: Neil Shen <overvenus@gmail.com>

Co-authored-by: Evan Zhou <coocood@gmail.com>
Co-authored-by: pingcap-github-bot <sre-bot@pingcap.com>
  • Loading branch information
3 people authored Aug 20, 2020
1 parent e8bbaf4 commit c5b7658
Show file tree
Hide file tree
Showing 30 changed files with 301 additions and 242 deletions.
16 changes: 16 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,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 @@ -807,7 +809,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
41 changes: 30 additions & 11 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1614,8 +1614,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 @@ -1632,7 +1633,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 @@ -1641,7 +1644,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 @@ -1650,7 +1655,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 @@ -1686,7 +1693,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 @@ -1695,11 +1704,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 @@ -1953,7 +1966,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 @@ -1999,12 +2014,16 @@ func (s *testIntegrationSuite7) TestAddExpressionIndex(c *C) {
tk.MustQuery("select * from t;").Check(testkit.Rows("1 2.1"))

// 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
12 changes: 9 additions & 3 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,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 @@ -4418,7 +4420,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 @@ -4432,7 +4436,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
61 changes: 26 additions & 35 deletions ddl/serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,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)
var err error
Expand All @@ -88,15 +86,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 @@ -125,15 +119,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_test2 add primary key(a)")
c.Assert(infoschema.ErrMultiplePriKey.Equal(err), IsTrue)
Expand All @@ -151,8 +140,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 @@ -812,13 +802,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 @@ -896,11 +883,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
11 changes: 5 additions & 6 deletions executor/executor_pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,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
})

c.Assert(failpoint.Enable("github.com/pingcap/tidb/executor/testSortedRowContainerSpill", "return(true)"), IsNil)
defer func() {
Expand Down
Loading

0 comments on commit c5b7658

Please sign in to comment.