Skip to content

Commit

Permalink
Merge branch 'master' into uniform
Browse files Browse the repository at this point in the history
  • Loading branch information
lhy1024 authored Jul 13, 2022
2 parents f155432 + 1f5dcb3 commit 42e1375
Show file tree
Hide file tree
Showing 38 changed files with 1,867 additions and 1,507 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ require (
github.com/montanaflynn/stats v0.5.0
github.com/petermattis/goid v0.0.0-20211229010228-4d14c490ee36 // indirect
github.com/phf/go-queue v0.0.0-20170504031614-9abe38d0371d
github.com/pingcap/check v0.0.0-20211026125417-57bd13f7b5f0
github.com/pingcap/errcode v0.3.0
github.com/pingcap/errors v0.11.5-0.20211224045212-9687c2b0f87c
github.com/pingcap/failpoint v0.0.0-20200702092429-9f69995143ce
Expand Down
3 changes: 1 addition & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,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-20211026125417-57bd13f7b5f0 h1:HVl5539r48eA+uDuX/ziBmQCxzT1pGrzWbKuXT46Bq0=
github.com/pingcap/check v0.0.0-20211026125417-57bd13f7b5f0/go.mod h1:PYMCGwN0JHjoqGr3HrZoD+b8Tgx8bKnArhSq8YVzUMc=
github.com/pingcap/errcode v0.3.0 h1:IF6LC/4+b1KNwrMlr2rBTUrojFPMexXBcDWZSpNwxjg=
github.com/pingcap/errcode v0.3.0/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 pkg/assertutil/assertutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ func TestNilFail(t *testing.T) {
}
re.Nil(checker.IsNil)
checker.AssertNil(nil)
re.NotNil(failErr)
re.Error(failErr)
}
119 changes: 46 additions & 73 deletions pkg/testutil/operator_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,39 @@
package testutil

import (
"github.com/pingcap/check"
"github.com/stretchr/testify/require"
"github.com/tikv/pd/server/schedule/operator"
)

// CheckTransferLeader checks if the operator is to transfer leader between the specified source and target stores.
func CheckTransferLeader(c *check.C, op *operator.Operator, kind operator.OpKind, sourceID, targetID uint64) {
c.Assert(op, check.NotNil)
c.Assert(op.Len(), check.Equals, 1)
c.Assert(op.Step(0), check.DeepEquals, operator.TransferLeader{FromStore: sourceID, ToStore: targetID})
func CheckTransferLeader(re *require.Assertions, op *operator.Operator, kind operator.OpKind, sourceID, targetID uint64) {
re.NotNil(op)
re.Equal(1, op.Len())
re.Equal(operator.TransferLeader{FromStore: sourceID, ToStore: targetID}, op.Step(0))
kind |= operator.OpLeader
c.Assert(op.Kind()&kind, check.Equals, kind)
re.Equal(kind, op.Kind()&kind)
}

// CheckTransferLeaderFrom checks if the operator is to transfer leader out of the specified store.
func CheckTransferLeaderFrom(c *check.C, op *operator.Operator, kind operator.OpKind, sourceID uint64) {
c.Assert(op, check.NotNil)
c.Assert(op.Len(), check.Equals, 1)
c.Assert(op.Step(0).(operator.TransferLeader).FromStore, check.Equals, sourceID)
func CheckTransferLeaderFrom(re *require.Assertions, op *operator.Operator, kind operator.OpKind, sourceID uint64) {
re.NotNil(op)
re.Equal(1, op.Len())
re.Equal(sourceID, op.Step(0).(operator.TransferLeader).FromStore)
kind |= operator.OpLeader
c.Assert(op.Kind()&kind, check.Equals, kind)
re.Equal(kind, op.Kind()&kind)
}

// CheckMultiTargetTransferLeader checks if the operator is to transfer leader from the specified source to one of the target stores.
func CheckMultiTargetTransferLeader(c *check.C, op *operator.Operator, kind operator.OpKind, sourceID uint64, targetIDs []uint64) {
c.Assert(op, check.NotNil)
c.Assert(op.Len(), check.Equals, 1)
func CheckMultiTargetTransferLeader(re *require.Assertions, op *operator.Operator, kind operator.OpKind, sourceID uint64, targetIDs []uint64) {
re.NotNil(op)
re.Equal(1, op.Len())
expectedOps := make([]interface{}, 0, len(targetIDs))
for _, targetID := range targetIDs {
expectedOps = append(expectedOps, operator.TransferLeader{FromStore: sourceID, ToStore: targetID, ToStores: targetIDs})
}
c.Assert(op.Step(0), check.DeepEqualsIn, expectedOps)
re.Contains(expectedOps, op.Step(0))
kind |= operator.OpLeader
c.Assert(op.Kind()&kind, check.Equals, kind)
re.Equal(kind, op.Kind()&kind)
}

func trimTransferLeaders(op *operator.Operator) (steps []operator.OpStep, lastLeader uint64) {
Expand All @@ -64,61 +63,57 @@ func trimTransferLeaders(op *operator.Operator) (steps []operator.OpStep, lastLe
}

// CheckTransferPeer checks if the operator is to transfer peer between the specified source and target stores.
func CheckTransferPeer(c *check.C, op *operator.Operator, kind operator.OpKind, sourceID, targetID uint64) {
c.Assert(op, check.NotNil)

func CheckTransferPeer(re *require.Assertions, op *operator.Operator, kind operator.OpKind, sourceID, targetID uint64) {
re.NotNil(op)
steps, _ := trimTransferLeaders(op)
c.Assert(steps, check.HasLen, 3)
c.Assert(steps[0].(operator.AddLearner).ToStore, check.Equals, targetID)
c.Assert(steps[1], check.FitsTypeOf, operator.PromoteLearner{})
c.Assert(steps[2].(operator.RemovePeer).FromStore, check.Equals, sourceID)
re.Len(steps, 3)
re.Equal(targetID, steps[0].(operator.AddLearner).ToStore)
re.IsType(operator.PromoteLearner{}, steps[1])
re.Equal(sourceID, steps[2].(operator.RemovePeer).FromStore)
kind |= operator.OpRegion
c.Assert(op.Kind()&kind, check.Equals, kind)
re.Equal(kind, op.Kind()&kind)
}

// CheckTransferLearner checks if the operator is to transfer learner between the specified source and target stores.
func CheckTransferLearner(c *check.C, op *operator.Operator, kind operator.OpKind, sourceID, targetID uint64) {
c.Assert(op, check.NotNil)

func CheckTransferLearner(re *require.Assertions, op *operator.Operator, kind operator.OpKind, sourceID, targetID uint64) {
re.NotNil(op)
steps, _ := trimTransferLeaders(op)
c.Assert(steps, check.HasLen, 2)
c.Assert(steps[0].(operator.AddLearner).ToStore, check.Equals, targetID)
c.Assert(steps[1].(operator.RemovePeer).FromStore, check.Equals, sourceID)
re.Len(steps, 2)
re.Equal(targetID, steps[0].(operator.AddLearner).ToStore)
re.Equal(sourceID, steps[1].(operator.RemovePeer).FromStore)
kind |= operator.OpRegion
c.Assert(op.Kind()&kind, check.Equals, kind)
re.Equal(kind, op.Kind()&kind)
}

// CheckTransferPeerWithLeaderTransfer checks if the operator is to transfer
// peer between the specified source and target stores and it meanwhile
// transfers the leader out of source store.
func CheckTransferPeerWithLeaderTransfer(c *check.C, op *operator.Operator, kind operator.OpKind, sourceID, targetID uint64) {
c.Assert(op, check.NotNil)

func CheckTransferPeerWithLeaderTransfer(re *require.Assertions, op *operator.Operator, kind operator.OpKind, sourceID, targetID uint64) {
re.NotNil(op)
steps, lastLeader := trimTransferLeaders(op)
c.Assert(steps, check.HasLen, 3)
c.Assert(steps[0].(operator.AddLearner).ToStore, check.Equals, targetID)
c.Assert(steps[1], check.FitsTypeOf, operator.PromoteLearner{})
c.Assert(steps[2].(operator.RemovePeer).FromStore, check.Equals, sourceID)
c.Assert(lastLeader, check.Not(check.Equals), uint64(0))
c.Assert(lastLeader, check.Not(check.Equals), sourceID)
re.Len(steps, 3)
re.Equal(targetID, steps[0].(operator.AddLearner).ToStore)
re.IsType(operator.PromoteLearner{}, steps[1])
re.Equal(sourceID, steps[2].(operator.RemovePeer).FromStore)
re.NotZero(lastLeader)
re.NotEqual(sourceID, lastLeader)
kind |= operator.OpRegion
c.Assert(op.Kind()&kind, check.Equals, kind)
re.Equal(kind, op.Kind()&kind)
}

// CheckTransferPeerWithLeaderTransferFrom checks if the operator is to transfer
// peer out of the specified store and it meanwhile transfers the leader out of
// the store.
func CheckTransferPeerWithLeaderTransferFrom(c *check.C, op *operator.Operator, kind operator.OpKind, sourceID uint64) {
c.Assert(op, check.NotNil)

func CheckTransferPeerWithLeaderTransferFrom(re *require.Assertions, op *operator.Operator, kind operator.OpKind, sourceID uint64) {
re.NotNil(op)
steps, lastLeader := trimTransferLeaders(op)
c.Assert(steps[0], check.FitsTypeOf, operator.AddLearner{})
c.Assert(steps[1], check.FitsTypeOf, operator.PromoteLearner{})
c.Assert(steps[2].(operator.RemovePeer).FromStore, check.Equals, sourceID)
c.Assert(lastLeader, check.Not(check.Equals), uint64(0))
c.Assert(lastLeader, check.Not(check.Equals), sourceID)
re.IsType(operator.AddLearner{}, steps[0])
re.IsType(operator.PromoteLearner{}, steps[1])
re.Equal(sourceID, steps[2].(operator.RemovePeer).FromStore)
re.NotZero(lastLeader)
re.NotEqual(sourceID, lastLeader)
kind |= operator.OpRegion | operator.OpLeader
c.Assert(op.Kind()&kind, check.Equals, kind)
re.Equal(kind, op.Kind()&kind)
}

// CheckAddPeer checks if the operator is to add peer on specified store.
Expand All @@ -143,31 +138,9 @@ func CheckRemovePeer(re *require.Assertions, op *operator.Operator, storeID uint
}
}

// CheckTransferLeaderWithTestify checks if the operator is to transfer leader between the specified source and target stores.
func CheckTransferLeaderWithTestify(re *require.Assertions, op *operator.Operator, kind operator.OpKind, sourceID, targetID uint64) {
re.NotNil(op)
re.Equal(1, op.Len())
re.Equal(operator.TransferLeader{FromStore: sourceID, ToStore: targetID}, op.Step(0))
kind |= operator.OpLeader
re.Equal(kind, op.Kind()&kind)
}

// CheckTransferPeerWithTestify checks if the operator is to transfer peer between the specified source and target stores.
func CheckTransferPeerWithTestify(re *require.Assertions, op *operator.Operator, kind operator.OpKind, sourceID, targetID uint64) {
re.NotNil(op)

steps, _ := trimTransferLeaders(op)
re.Len(steps, 3)
re.Equal(targetID, steps[0].(operator.AddLearner).ToStore)
re.IsType(operator.PromoteLearner{}, steps[1])
re.Equal(sourceID, steps[2].(operator.RemovePeer).FromStore)
kind |= operator.OpRegion
re.Equal(kind, op.Kind()&kind)
}

// CheckSteps checks if the operator matches the given steps.
func CheckSteps(re *require.Assertions, op *operator.Operator, steps []operator.OpStep) {
re.NotEqual(0, op.Kind()&operator.OpMerge)
re.NotZero(op.Kind() & operator.OpMerge)
re.NotNil(steps)
re.Len(steps, op.Len())
for i := range steps {
Expand Down
8 changes: 4 additions & 4 deletions server/api/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (suite *checkerTestSuite) TearDownSuite() {
func (suite *checkerTestSuite) TestAPI() {
suite.testErrCases()

cases := []struct {
testCases := []struct {
name string
}{
{name: "learner"},
Expand All @@ -67,9 +67,9 @@ func (suite *checkerTestSuite) TestAPI() {
{name: "merge"},
{name: "joint-state"},
}
for _, ca := range cases {
suite.testGetStatus(ca.name)
suite.testPauseOrResume(ca.name)
for _, testCase := range testCases {
suite.testGetStatus(testCase.name)
suite.testPauseOrResume(testCase.name)
}
}

Expand Down
42 changes: 21 additions & 21 deletions server/api/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (suite *scheduleTestSuite) TestAPI() {
opt string
value interface{}
}
cases := []struct {
testCases := []struct {
name string
createdName string
args []arg
Expand Down Expand Up @@ -321,25 +321,25 @@ func (suite *scheduleTestSuite) TestAPI() {
},
},
}
for _, ca := range cases {
for _, testCase := range testCases {
input := make(map[string]interface{})
input["name"] = ca.name
for _, a := range ca.args {
input["name"] = testCase.name
for _, a := range testCase.args {
input[a.opt] = a.value
}
body, err := json.Marshal(input)
suite.NoError(err)
suite.testPauseOrResume(ca.name, ca.createdName, body)
suite.testPauseOrResume(testCase.name, testCase.createdName, body)
}

// test pause and resume all schedulers.

// add schedulers.
cases = cases[:3]
for _, ca := range cases {
testCases = testCases[:3]
for _, testCase := range testCases {
input := make(map[string]interface{})
input["name"] = ca.name
for _, a := range ca.args {
input["name"] = testCase.name
for _, a := range testCase.args {
input[a.opt] = a.value
}
body, err := json.Marshal(input)
Expand All @@ -355,10 +355,10 @@ func (suite *scheduleTestSuite) TestAPI() {
err = tu.CheckPostJSON(testDialClient, suite.urlPrefix+"/all", pauseArgs, tu.StatusOK(re))
suite.NoError(err)
handler := suite.svr.GetHandler()
for _, ca := range cases {
createdName := ca.createdName
for _, testCase := range testCases {
createdName := testCase.createdName
if createdName == "" {
createdName = ca.name
createdName = testCase.name
}
isPaused, err := handler.IsSchedulerPaused(createdName)
suite.NoError(err)
Expand All @@ -370,10 +370,10 @@ func (suite *scheduleTestSuite) TestAPI() {
err = tu.CheckPostJSON(testDialClient, suite.urlPrefix+"/all", pauseArgs, tu.StatusOK(re))
suite.NoError(err)
time.Sleep(time.Second)
for _, ca := range cases {
createdName := ca.createdName
for _, testCase := range testCases {
createdName := testCase.createdName
if createdName == "" {
createdName = ca.name
createdName = testCase.name
}
isPaused, err := handler.IsSchedulerPaused(createdName)
suite.NoError(err)
Expand All @@ -391,21 +391,21 @@ func (suite *scheduleTestSuite) TestAPI() {
suite.NoError(err)
err = tu.CheckPostJSON(testDialClient, suite.urlPrefix+"/all", pauseArgs, tu.StatusOK(re))
suite.NoError(err)
for _, ca := range cases {
createdName := ca.createdName
for _, testCase := range testCases {
createdName := testCase.createdName
if createdName == "" {
createdName = ca.name
createdName = testCase.name
}
isPaused, err := handler.IsSchedulerPaused(createdName)
suite.NoError(err)
suite.False(isPaused)
}

// delete schedulers.
for _, ca := range cases {
createdName := ca.createdName
for _, testCase := range testCases {
createdName := testCase.createdName
if createdName == "" {
createdName = ca.name
createdName = testCase.name
}
suite.deleteScheduler(createdName)
}
Expand Down
10 changes: 5 additions & 5 deletions server/cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestStoreHeartbeat(t *testing.T) {
re.NoError(cluster.HandleStoreHeartbeat(coldHeartBeat))
time.Sleep(20 * time.Millisecond)
storeStats = cluster.hotStat.RegionStats(statistics.Read, 1)
re.Len(storeStats[1], 0)
re.Empty(storeStats[1])
// After hot heartbeat, we can find region 1 peer again
re.NoError(cluster.HandleStoreHeartbeat(hotHeartBeat))
time.Sleep(20 * time.Millisecond)
Expand All @@ -150,14 +150,14 @@ func TestStoreHeartbeat(t *testing.T) {
re.NoError(cluster.HandleStoreHeartbeat(coldHeartBeat))
time.Sleep(20 * time.Millisecond)
storeStats = cluster.hotStat.RegionStats(statistics.Read, 0)
re.Len(storeStats[1], 0)
re.Empty(storeStats[1])
re.Nil(cluster.HandleStoreHeartbeat(hotHeartBeat))
time.Sleep(20 * time.Millisecond)
storeStats = cluster.hotStat.RegionStats(statistics.Read, 1)
re.Len(storeStats[1], 1)
re.Equal(uint64(1), storeStats[1][0].RegionID)
storeStats = cluster.hotStat.RegionStats(statistics.Read, 3)
re.Len(storeStats[1], 0)
re.Empty(storeStats[1])
// after 2 hot heartbeats, wo can find region 1 peer again
re.NoError(cluster.HandleStoreHeartbeat(hotHeartBeat))
re.NoError(cluster.HandleStoreHeartbeat(hotHeartBeat))
Expand Down Expand Up @@ -614,7 +614,7 @@ func TestRegionHeartbeatHotStat(t *testing.T) {
time.Sleep(1 * time.Second)
stats = cluster.hotStat.RegionStats(statistics.Write, 0)
re.Len(stats[1], 1)
re.Len(stats[2], 0)
re.Empty(stats[2])
re.Len(stats[3], 1)
re.Len(stats[4], 1)
}
Expand Down Expand Up @@ -675,7 +675,7 @@ func TestBucketHeartbeat(t *testing.T) {
newRegion2 := regions[1].Clone(core.WithIncConfVer(), core.SetBuckets(nil))
re.NoError(cluster.processRegionHeartbeat(newRegion2))
re.Nil(cluster.GetRegion(uint64(1)).GetBuckets())
re.Len(cluster.GetRegion(uint64(1)).GetBuckets().GetKeys(), 0)
re.Empty(cluster.GetRegion(uint64(1)).GetBuckets().GetKeys())
}

func TestRegionHeartbeat(t *testing.T) {
Expand Down
Loading

0 comments on commit 42e1375

Please sign in to comment.