Skip to content

Commit

Permalink
test: optimize session tikv test (pingcap#14709)
Browse files Browse the repository at this point in the history
  • Loading branch information
glorv authored Feb 12, 2020
1 parent b056fd7 commit 62e48ca
Show file tree
Hide file tree
Showing 19 changed files with 83 additions and 55 deletions.
2 changes: 1 addition & 1 deletion ddl/column_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type testColumnChangeSuite struct {
}

func (s *testColumnChangeSuite) SetUpSuite(c *C) {
WaitTimeWhenErrorOccured = 1 * time.Microsecond
SetWaitTimeWhenErrorOccurred(1 * time.Microsecond)
s.store = testCreateStore(c, "test_column_change")
s.dbInfo = &model.DBInfo{
Name: model.NewCIStr("test_column_change"),
Expand Down
2 changes: 1 addition & 1 deletion ddl/db_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type testStateChangeSuiteBase struct {

func (s *testStateChangeSuiteBase) SetUpSuite(c *C) {
s.lease = 200 * time.Millisecond
ddl.WaitTimeWhenErrorOccured = 1 * time.Microsecond
ddl.SetWaitTimeWhenErrorOccurred(1 * time.Microsecond)
var err error
s.store, err = mockstore.NewMockTikvStore()
c.Assert(err, IsNil)
Expand Down
2 changes: 1 addition & 1 deletion ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type testIntegrationSuite struct {
func setupIntegrationSuite(s *testIntegrationSuite, c *C) {
var err error
s.lease = 50 * time.Millisecond
ddl.WaitTimeWhenErrorOccured = 0
ddl.SetWaitTimeWhenErrorOccurred(0)

s.cluster = mocktikv.NewCluster()
mocktikv.BootstrapWithSingleStore(s.cluster)
Expand Down
8 changes: 5 additions & 3 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ var _ = Suite(&testDBSuite3{&testDBSuite{}})
var _ = Suite(&testDBSuite4{&testDBSuite{}})
var _ = Suite(&testDBSuite5{&testDBSuite{}})
var _ = Suite(&testDBSuite6{&testDBSuite{}})
var _ = SerialSuites(&testSerialDBSuite{&testDBSuite{}})

const defaultBatchSize = 1024

Expand All @@ -90,7 +91,7 @@ func setUpSuite(s *testDBSuite, c *C) {
session.DisableStats4Test()
s.schemaName = "test_db"
s.autoIDStep = autoid.GetStep()
ddl.WaitTimeWhenErrorOccured = 0
ddl.SetWaitTimeWhenErrorOccurred(0)

s.cluster = mocktikv.NewCluster()
mocktikv.BootstrapWithSingleStore(s.cluster)
Expand Down Expand Up @@ -134,6 +135,7 @@ type testDBSuite3 struct{ *testDBSuite }
type testDBSuite4 struct{ *testDBSuite }
type testDBSuite5 struct{ *testDBSuite }
type testDBSuite6 struct{ *testDBSuite }
type testSerialDBSuite struct{ *testDBSuite }

func (s *testDBSuite4) TestAddIndexWithPK(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
Expand Down Expand Up @@ -2861,7 +2863,7 @@ func (s *testDBSuite4) TestComment(c *C) {
s.tk.MustExec("drop table if exists ct, ct1")
}

func (s *testDBSuite4) TestRebaseAutoID(c *C) {
func (s *testSerialDBSuite) TestRebaseAutoID(c *C) {
c.Assert(failpoint.Enable("github.com/pingcap/tidb/meta/autoid/mockAutoIDChange", `return(true)`), IsNil)
defer func() {
c.Assert(failpoint.Disable("github.com/pingcap/tidb/meta/autoid/mockAutoIDChange"), IsNil)
Expand Down Expand Up @@ -3723,7 +3725,7 @@ func (s *testDBSuite1) TestSetTableFlashReplica(c *C) {
c.Assert(t.Meta().TiFlashReplica, IsNil)
}

func (s *testDBSuite4) TestAlterShardRowIDBits(c *C) {
func (s *testSerialDBSuite) TestAlterShardRowIDBits(c *C) {
c.Assert(failpoint.Enable("github.com/pingcap/tidb/meta/autoid/mockAutoIDChange", `return(true)`), IsNil)
defer func() {
c.Assert(failpoint.Disable("github.com/pingcap/tidb/meta/autoid/mockAutoIDChange"), IsNil)
Expand Down
18 changes: 14 additions & 4 deletions ddl/ddl_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,20 @@ var (
RunWorker = true
// ddlWorkerID is used for generating the next DDL worker ID.
ddlWorkerID = int32(0)
// WaitTimeWhenErrorOccured is waiting interval when processing DDL jobs encounter errors.
WaitTimeWhenErrorOccured = 1 * time.Second
// WaitTimeWhenErrorOccurred is waiting interval when processing DDL jobs encounter errors.
WaitTimeWhenErrorOccurred = int64(1 * time.Second)
)

// GetWaitTimeWhenErrorOccurred return waiting interval when processing DDL jobs encounter errors.
func GetWaitTimeWhenErrorOccurred() time.Duration {
return time.Duration(atomic.LoadInt64(&WaitTimeWhenErrorOccurred))
}

// SetWaitTimeWhenErrorOccurred update waiting interval when processing DDL jobs encounter errors.
func SetWaitTimeWhenErrorOccurred(dur time.Duration) {
atomic.StoreInt64(&WaitTimeWhenErrorOccurred, int64(dur))
}

type workerType byte

const (
Expand Down Expand Up @@ -435,8 +445,8 @@ func (w *worker) handleDDLJobQueue(d *ddlCtx) error {
// wait a while to retry again. If we don't wait here, DDL will retry this job immediately,
// which may act like a deadlock.
logutil.Logger(w.logCtx).Info("[ddl] run DDL job failed, sleeps a while then retries it.",
zap.Duration("waitTime", WaitTimeWhenErrorOccured), zap.Error(runJobErr))
time.Sleep(WaitTimeWhenErrorOccured)
zap.Duration("waitTime", GetWaitTimeWhenErrorOccurred()), zap.Error(runJobErr))
time.Sleep(GetWaitTimeWhenErrorOccurred())
}

if err != nil {
Expand Down
8 changes: 5 additions & 3 deletions ddl/ddl_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ import (
)

var _ = Suite(&testDDLSuite{})
var _ = Suite(&testDDLSerialSuite{})

type testDDLSuite struct{}
type testDDLSerialSuite struct{}

const testLease = 5 * time.Millisecond

func (s *testDDLSuite) SetUpSuite(c *C) {
WaitTimeWhenErrorOccured = 1 * time.Microsecond
func (s *testDDLSerialSuite) SetUpSuite(c *C) {
SetWaitTimeWhenErrorOccurred(1 * time.Microsecond)

// We hope that this test is serially executed. So put it here.
s.testRunWorker(c)
Expand All @@ -66,7 +68,7 @@ func (s *testDDLSuite) TestCheckOwner(c *C) {
}

// testRunWorker tests no job is handled when the value of RunWorker is false.
func (s *testDDLSuite) testRunWorker(c *C) {
func (s *testDDLSerialSuite) testRunWorker(c *C) {
store := testCreateStore(c, "test_run_worker")
defer store.Close()

Expand Down
2 changes: 1 addition & 1 deletion ddl/failtest/fail_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type testFailDBSuite struct {

func (s *testFailDBSuite) SetUpSuite(c *C) {
s.lease = 200 * time.Millisecond
ddl.WaitTimeWhenErrorOccured = 1 * time.Microsecond
ddl.SetWaitTimeWhenErrorOccurred(1 * time.Microsecond)
var err error
s.cluster = mocktikv.NewCluster()
mocktikv.BootstrapWithSingleStore(s.cluster)
Expand Down
10 changes: 5 additions & 5 deletions ddl/serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (s *testSerialSuite) SetUpSuite(c *C) {
newCfg.AlterPrimaryKey = false
config.StoreGlobalConfig(&newCfg)

ddl.WaitTimeWhenErrorOccured = 1 * time.Microsecond
ddl.SetWaitTimeWhenErrorOccurred(1 * time.Microsecond)
var err error
s.store, err = mockstore.NewMockTikvStore()
c.Assert(err, IsNil)
Expand Down Expand Up @@ -663,13 +663,13 @@ func (s *testSerialSuite) TestCanceledJobTakeTime(c *C) {
s.dom.DDL().(ddl.DDLForTest).SetHook(hook)
defer s.dom.DDL().(ddl.DDLForTest).SetHook(origHook)

originalWT := ddl.WaitTimeWhenErrorOccured
ddl.WaitTimeWhenErrorOccured = 1 * time.Second
defer func() { ddl.WaitTimeWhenErrorOccured = originalWT }()
originalWT := ddl.GetWaitTimeWhenErrorOccurred()
ddl.SetWaitTimeWhenErrorOccurred(1 * time.Second)
defer func() { ddl.SetWaitTimeWhenErrorOccurred(originalWT) }()
startTime := time.Now()
tk.MustGetErrCode("alter table t_cjtt add column b int", mysql.ErrNoSuchTable)
sub := time.Since(startTime)
c.Assert(sub, Less, ddl.WaitTimeWhenErrorOccured)
c.Assert(sub, Less, ddl.GetWaitTimeWhenErrorOccurred())
}

func (s *testSerialSuite) TestTableLocksEnable(c *C) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ require (
github.com/ngaut/sync2 v0.0.0-20141008032647-7a24ed77b2ef
github.com/opentracing/basictracer-go v1.0.0
github.com/opentracing/opentracing-go v1.0.2
github.com/pingcap/check v0.0.0-20191216031241-8a5a85928f12
github.com/pingcap/check v0.0.0-20200212061837-5e12011dc712
github.com/pingcap/errors v0.11.5-0.20190809092503-95897b64e011
github.com/pingcap/failpoint v0.0.0-20200210140405-f8f9fb234798
github.com/pingcap/fn v0.0.0-20191016082858-07623b84a47d
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ github.com/phf/go-queue v0.0.0-20170504031614-9abe38d0371d h1:U+PMnTlV2tu7RuMK5e
github.com/phf/go-queue v0.0.0-20170504031614-9abe38d0371d/go.mod h1:lXfE4PvvTW5xOjO6Mba8zDPyw8M93B6AQ7frTGnMlA8=
github.com/pingcap/check v0.0.0-20190102082844-67f458068fc8/go.mod h1:B1+S9LNcuMyLH/4HMTViQOJevkGiik3wW2AN9zb2fNQ=
github.com/pingcap/check v0.0.0-20191107115940-caf2b9e6ccf4/go.mod h1:PYMCGwN0JHjoqGr3HrZoD+b8Tgx8bKnArhSq8YVzUMc=
github.com/pingcap/check v0.0.0-20191216031241-8a5a85928f12 h1:rfD9v3+ppLPzoQBgZev0qYCpegrwyFx/BUpkApEiKdY=
github.com/pingcap/check v0.0.0-20191216031241-8a5a85928f12/go.mod h1:PYMCGwN0JHjoqGr3HrZoD+b8Tgx8bKnArhSq8YVzUMc=
github.com/pingcap/check v0.0.0-20200212061837-5e12011dc712 h1:R8gStypOBmpnHEx1qi//SaqxJVI4inOqljg/Aj5/390=
github.com/pingcap/check v0.0.0-20200212061837-5e12011dc712/go.mod h1:PYMCGwN0JHjoqGr3HrZoD+b8Tgx8bKnArhSq8YVzUMc=
github.com/pingcap/errcode v0.0.0-20180921232412-a1a7271709d9 h1:KH4f4Si9XK6/IW50HtoaiLIFHGkapOM6w83za47UYik=
github.com/pingcap/errcode v0.0.0-20180921232412-a1a7271709d9/go.mod h1:4b2X8xSqxIroj/IZ9MX/VGZhAwc11wB9wRIzHvz6SeM=
github.com/pingcap/errors v0.11.0/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8=
Expand Down
2 changes: 1 addition & 1 deletion planner/core/point_get_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
dto "github.com/prometheus/client_model/go"
)

var _ = Suite(&testPointGetSuite{})
var _ = SerialSuites(&testPointGetSuite{})

type testPointGetSuite struct {
store kv.Storage
Expand Down
2 changes: 1 addition & 1 deletion planner/core/prepare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type testPrepareSuite struct {
type testPrepareSerialSuite struct {
}

func (s *testPrepareSuite) TestPrepareCache(c *C) {
func (s *testPrepareSerialSuite) TestPrepareCache(c *C) {
defer testleak.AfterTest(c)()
store, dom, err := newStoreWithBootstrap()
c.Assert(err, IsNil)
Expand Down
8 changes: 4 additions & 4 deletions server/tidb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ func (ts *tidbTestSuiteBase) SetUpSuite(c *C) {
ts.server = server
go ts.server.Run()
ts.waitUntilServerOnline()

// Run this test here because parallel would affect the result of it.
ts.runTestStmtCount(c)
}

func (ts *tidbTestSuiteBase) TearDownSuite(c *C) {
Expand Down Expand Up @@ -136,10 +133,13 @@ func (ts *tidbTestSuite) TestPreparedTimestamp(c *C) {
// this test will change `kv.TxnTotalSizeLimit` which may affect other test suites,
// so we must make it running in serial.
func (ts *tidbTestSerialSuite) TestLoadData(c *C) {
c.Parallel()
ts.runTestLoadData(c, ts.server)
}

func (ts *tidbTestSerialSuite) TestStmtCount(c *C) {
ts.runTestStmtCount(c)
}

func (ts *tidbTestSuite) TestConcurrentUpdate(c *C) {
c.Parallel()
ts.runTestConcurrentUpdate(c)
Expand Down
34 changes: 23 additions & 11 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ import (
)

var (
withTiKVGlobalLock sync.RWMutex
withTiKV = flag.Bool("with-tikv", false, "run tests with TiKV cluster started. (not use the mock server)")
pdAddrs = flag.String("pd-addrs", "127.0.0.1:2379", "pd addrs")
withTiKV = flag.Bool("with-tikv", false, "run tests with TiKV cluster started. (not use the mock server)")
pdAddrs = flag.String("pd-addrs", "127.0.0.1:2379", "pd addrs")
pdAddrChan chan string
initPdAddrsOnce sync.Once
)

var _ = Suite(&testSessionSuite{})
Expand All @@ -73,6 +74,7 @@ type testSessionSuiteBase struct {
mvccStore mocktikv.MVCCStore
store kv.Storage
dom *domain.Domain
pdAddr string
}

type testSessionSuite struct {
Expand Down Expand Up @@ -136,21 +138,35 @@ func clearETCD(ebd tikv.EtcdBackend) error {
return nil
}

func initPdAddrs() {
initPdAddrsOnce.Do(func() {
addrs := strings.Split(*pdAddrs, ",")
pdAddrChan = make(chan string, len(addrs))
for _, addr := range addrs {
addr = strings.TrimSpace(addr)
if addr != "" {
pdAddrChan <- addr
}
}
})
}

func (s *testSessionSuiteBase) SetUpSuite(c *C) {
testleak.BeforeTest()
s.cluster = mocktikv.NewCluster()

if *withTiKV {
withTiKVGlobalLock.Lock()
initPdAddrs()
s.pdAddr = <-pdAddrChan
var d tikv.Driver
config.GetGlobalConfig().TxnLocalLatches.Enabled = false
store, err := d.Open(fmt.Sprintf("tikv://%s", *pdAddrs))
store, err := d.Open(fmt.Sprintf("tikv://%s", s.pdAddr))
c.Assert(err, IsNil)
err = clearStorage(store)
c.Assert(err, IsNil)
err = clearETCD(store.(tikv.EtcdBackend))
c.Assert(err, IsNil)
session.ResetForWithTiKVTest()
session.ResetStoreForWithTiKVTest(store)
s.store = store
} else {
mocktikv.BootstrapWithSingleStore(s.cluster)
Expand All @@ -161,7 +177,6 @@ func (s *testSessionSuiteBase) SetUpSuite(c *C) {
)
c.Assert(err, IsNil)
s.store = store
session.SetSchemaLease(0)
session.DisableStats4Test()
}

Expand All @@ -176,7 +191,7 @@ func (s *testSessionSuiteBase) TearDownSuite(c *C) {
s.store.Close()
testleak.AfterTest(c)()
if *withTiKV {
withTiKVGlobalLock.Unlock()
pdAddrChan <- s.pdAddr
}
}

Expand Down Expand Up @@ -1848,7 +1863,6 @@ type testSchemaSuiteBase struct {
cluster *mocktikv.Cluster
mvccStore mocktikv.MVCCStore
store kv.Storage
lease time.Duration
dom *domain.Domain
}

Expand Down Expand Up @@ -1880,8 +1894,6 @@ func (s *testSchemaSuiteBase) SetUpSuite(c *C) {
)
c.Assert(err, IsNil)
s.store = store
s.lease = 20 * time.Millisecond
session.SetSchemaLease(s.lease)
session.DisableStats4Test()
dom, err := session.BootstrapSession(s.store)
c.Assert(err, IsNil)
Expand Down
10 changes: 4 additions & 6 deletions session/tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,11 @@ var (
statsLease = int64(3 * time.Second)
)

// ResetForWithTiKVTest is only used in the test code.
// ResetStoreForWithTiKVTest is only used in the test code.
// TODO: Remove domap and storeBootstrapped. Use store.SetOption() to do it.
func ResetForWithTiKVTest() {
domap = &domainMap{
domains: map[string]*domain.Domain{},
}
storeBootstrapped = make(map[string]bool)
func ResetStoreForWithTiKVTest(store kv.Storage) {
domap.Delete(store)
unsetStoreBootstrapped(store.UUID())
}

func setStoreBootstrapped(storeUUID string) {
Expand Down
2 changes: 2 additions & 0 deletions session/tidb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"sync"
"sync/atomic"
"testing"
"time"

. "github.com/pingcap/check"
"github.com/pingcap/parser/auth"
Expand All @@ -38,6 +39,7 @@ func TestT(t *testing.T) {
logLevel := os.Getenv("log_level")
logutil.InitLogger(logutil.NewLogConfig(logLevel, logutil.DefaultLogFormat, "", logutil.EmptyFileLogConfig, false))
CustomVerboseFlag = true
SetSchemaLease(20 * time.Millisecond)
TestingT(t)
}

Expand Down
2 changes: 1 addition & 1 deletion store/tikv/sql_fail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (s *testSQLSuiteBase) SetUpSuite(c *C) {
s.store = NewTestStore(c).(Storage)
// actual this is better done in `OneByOneSuite.SetUpSuite`, but this would cause circle dependency
if *WithTiKV {
session.ResetForWithTiKVTest()
session.ResetStoreForWithTiKVTest(s.store)
}

s.dom, err = session.BootstrapSession(s.store)
Expand Down
Loading

0 comments on commit 62e48ca

Please sign in to comment.