Skip to content

Commit

Permalink
pkg: move operator_check out from test_util
Browse files Browse the repository at this point in the history
Signed-off-by: lhy1024 <admin@liudos.us>
  • Loading branch information
lhy1024 committed Mar 14, 2023
1 parent 0b36238 commit 63311d2
Show file tree
Hide file tree
Showing 13 changed files with 184 additions and 182 deletions.
25 changes: 13 additions & 12 deletions pkg/schedule/checker/merge_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/tikv/pd/pkg/schedule/labeler"
"github.com/tikv/pd/pkg/schedule/operator"
"github.com/tikv/pd/pkg/schedule/placement"
"github.com/tikv/pd/pkg/utils/operatorutil"
"github.com/tikv/pd/pkg/utils/testutil"
"github.com/tikv/pd/pkg/versioninfo"
"go.uber.org/goleak"
Expand Down Expand Up @@ -251,7 +252,7 @@ func (suite *mergeCheckerTestSuite) TestMatchPeers() {
// partial store overlap not including leader
ops := suite.mc.Check(suite.regions[2])
suite.NotNil(ops)
testutil.CheckSteps(suite.Require(), ops[0], []operator.OpStep{
operatorutil.CheckSteps(suite.Require(), ops[0], []operator.OpStep{
operator.AddLearner{ToStore: 1},
operator.PromoteLearner{ToStore: 1},
operator.RemovePeer{FromStore: 2},
Expand All @@ -265,7 +266,7 @@ func (suite *mergeCheckerTestSuite) TestMatchPeers() {
IsPassive: false,
},
})
testutil.CheckSteps(suite.Require(), ops[1], []operator.OpStep{
operatorutil.CheckSteps(suite.Require(), ops[1], []operator.OpStep{
operator.MergeRegion{
FromRegion: suite.regions[2].GetMeta(),
ToRegion: suite.regions[1].GetMeta(),
Expand All @@ -285,7 +286,7 @@ func (suite *mergeCheckerTestSuite) TestMatchPeers() {
suite.regions[2] = newRegion
suite.cluster.PutRegion(suite.regions[2])
ops = suite.mc.Check(suite.regions[2])
testutil.CheckSteps(suite.Require(), ops[0], []operator.OpStep{
operatorutil.CheckSteps(suite.Require(), ops[0], []operator.OpStep{
operator.AddLearner{ToStore: 4},
operator.PromoteLearner{ToStore: 4},
operator.RemovePeer{FromStore: 6},
Expand All @@ -295,7 +296,7 @@ func (suite *mergeCheckerTestSuite) TestMatchPeers() {
IsPassive: false,
},
})
testutil.CheckSteps(suite.Require(), ops[1], []operator.OpStep{
operatorutil.CheckSteps(suite.Require(), ops[1], []operator.OpStep{
operator.MergeRegion{
FromRegion: suite.regions[2].GetMeta(),
ToRegion: suite.regions[1].GetMeta(),
Expand All @@ -311,14 +312,14 @@ func (suite *mergeCheckerTestSuite) TestMatchPeers() {
}))
suite.cluster.PutRegion(suite.regions[2])
ops = suite.mc.Check(suite.regions[2])
testutil.CheckSteps(suite.Require(), ops[0], []operator.OpStep{
operatorutil.CheckSteps(suite.Require(), ops[0], []operator.OpStep{
operator.MergeRegion{
FromRegion: suite.regions[2].GetMeta(),
ToRegion: suite.regions[1].GetMeta(),
IsPassive: false,
},
})
testutil.CheckSteps(suite.Require(), ops[1], []operator.OpStep{
operatorutil.CheckSteps(suite.Require(), ops[1], []operator.OpStep{
operator.MergeRegion{
FromRegion: suite.regions[2].GetMeta(),
ToRegion: suite.regions[1].GetMeta(),
Expand All @@ -334,7 +335,7 @@ func (suite *mergeCheckerTestSuite) TestMatchPeers() {
}), core.WithLeader(&metapb.Peer{Id: 109, StoreId: 2}))
suite.cluster.PutRegion(suite.regions[2])
ops = suite.mc.Check(suite.regions[2])
testutil.CheckSteps(suite.Require(), ops[0], []operator.OpStep{
operatorutil.CheckSteps(suite.Require(), ops[0], []operator.OpStep{
operator.AddLearner{ToStore: 1},
operator.PromoteLearner{ToStore: 1},
operator.RemovePeer{FromStore: 3},
Expand All @@ -351,7 +352,7 @@ func (suite *mergeCheckerTestSuite) TestMatchPeers() {
IsPassive: false,
},
})
testutil.CheckSteps(suite.Require(), ops[1], []operator.OpStep{
operatorutil.CheckSteps(suite.Require(), ops[1], []operator.OpStep{
operator.MergeRegion{
FromRegion: suite.regions[2].GetMeta(),
ToRegion: suite.regions[1].GetMeta(),
Expand All @@ -370,7 +371,7 @@ func (suite *mergeCheckerTestSuite) TestMatchPeers() {
)
suite.cluster.PutRegion(suite.regions[1])
ops = suite.mc.Check(suite.regions[2])
testutil.CheckSteps(suite.Require(), ops[0], []operator.OpStep{
operatorutil.CheckSteps(suite.Require(), ops[0], []operator.OpStep{
operator.AddLearner{ToStore: 1},
operator.PromoteLearner{ToStore: 1},
operator.RemovePeer{FromStore: 3},
Expand All @@ -390,7 +391,7 @@ func (suite *mergeCheckerTestSuite) TestMatchPeers() {
IsPassive: false,
},
})
testutil.CheckSteps(suite.Require(), ops[1], []operator.OpStep{
operatorutil.CheckSteps(suite.Require(), ops[1], []operator.OpStep{
operator.MergeRegion{
FromRegion: suite.regions[2].GetMeta(),
ToRegion: suite.regions[1].GetMeta(),
Expand All @@ -417,7 +418,7 @@ func (suite *mergeCheckerTestSuite) TestMatchPeers() {
)
suite.cluster.PutRegion(suite.regions[1])
ops = suite.mc.Check(suite.regions[2])
testutil.CheckSteps(suite.Require(), ops[0], []operator.OpStep{
operatorutil.CheckSteps(suite.Require(), ops[0], []operator.OpStep{
operator.AddLearner{ToStore: 1},
operator.PromoteLearner{ToStore: 1},
operator.RemovePeer{FromStore: 3},
Expand All @@ -431,7 +432,7 @@ func (suite *mergeCheckerTestSuite) TestMatchPeers() {
IsPassive: false,
},
})
testutil.CheckSteps(suite.Require(), ops[1], []operator.OpStep{
operatorutil.CheckSteps(suite.Require(), ops[1], []operator.OpStep{
operator.MergeRegion{
FromRegion: suite.regions[2].GetMeta(),
ToRegion: suite.regions[1].GetMeta(),
Expand Down
78 changes: 39 additions & 39 deletions pkg/schedule/checker/replica_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/tikv/pd/pkg/mock/mockconfig"
"github.com/tikv/pd/pkg/schedule/config"
"github.com/tikv/pd/pkg/schedule/operator"
"github.com/tikv/pd/pkg/utils/testutil"
"github.com/tikv/pd/pkg/utils/operatorutil"
"github.com/tikv/pd/pkg/versioninfo"
)

Expand Down Expand Up @@ -219,7 +219,7 @@ func (suite *replicaCheckerTestSuite) TestBasic() {

// Region has 2 peers, we need to add a new peer.
region := tc.GetRegion(1)
testutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 4)
operatorutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 4)

// Disable make up replica feature.
tc.SetEnableMakeUpReplica(false)
Expand All @@ -229,17 +229,17 @@ func (suite *replicaCheckerTestSuite) TestBasic() {
// Test healthFilter.
// If store 4 is down, we add to store 3.
tc.SetStoreDown(4)
testutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 3)
operatorutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 3)
tc.SetStoreUp(4)
testutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 4)
operatorutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 4)

// Test snapshotCountFilter.
// If snapshotCount > MaxSnapshotCount, we add to store 3.
tc.UpdateSnapshotCount(4, 3)
testutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 3)
operatorutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 3)
// If snapshotCount < MaxSnapshotCount, we can add peer again.
tc.UpdateSnapshotCount(4, 1)
testutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 4)
operatorutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 4)

// Add peer in store 4, and we have enough replicas.
peer4, _ := tc.AllocPeer(4)
Expand All @@ -249,7 +249,7 @@ func (suite *replicaCheckerTestSuite) TestBasic() {
// Add peer in store 3, and we have redundant replicas.
peer3, _ := tc.AllocPeer(3)
region = region.Clone(core.WithAddPeer(peer3))
testutil.CheckRemovePeer(suite.Require(), rc.Check(region), 1)
operatorutil.CheckRemovePeer(suite.Require(), rc.Check(region), 1)

// Disable remove extra replica feature.
tc.SetEnableRemoveExtraReplica(false)
Expand All @@ -266,13 +266,13 @@ func (suite *replicaCheckerTestSuite) TestBasic() {
}

region = region.Clone(core.WithDownPeers(append(region.GetDownPeers(), downPeer)))
testutil.CheckTransferPeer(suite.Require(), rc.Check(region), operator.OpReplica, 2, 1)
operatorutil.CheckTransferPeer(suite.Require(), rc.Check(region), operator.OpReplica, 2, 1)
region = region.Clone(core.WithDownPeers(nil))
suite.Nil(rc.Check(region))

// Peer in store 3 is offline, transfer peer to store 1.
tc.SetStoreOffline(3)
testutil.CheckTransferPeer(suite.Require(), rc.Check(region), operator.OpReplica, 3, 1)
operatorutil.CheckTransferPeer(suite.Require(), rc.Check(region), operator.OpReplica, 3, 1)
}

func (suite *replicaCheckerTestSuite) TestLostStore() {
Expand Down Expand Up @@ -311,36 +311,36 @@ func (suite *replicaCheckerTestSuite) TestOffline() {
region := tc.GetRegion(1)

// Store 2 has different zone and smallest region score.
testutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 2)
operatorutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 2)
peer2, _ := tc.AllocPeer(2)
region = region.Clone(core.WithAddPeer(peer2))

// Store 3 has different zone and smallest region score.
testutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 3)
operatorutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 3)
peer3, _ := tc.AllocPeer(3)
region = region.Clone(core.WithAddPeer(peer3))

// Store 4 has the same zone with store 3 and larger region score.
peer4, _ := tc.AllocPeer(4)
region = region.Clone(core.WithAddPeer(peer4))
testutil.CheckRemovePeer(suite.Require(), rc.Check(region), 4)
operatorutil.CheckRemovePeer(suite.Require(), rc.Check(region), 4)

// Test offline
// the number of region peers more than the maxReplicas
// remove the peer
tc.SetStoreOffline(3)
testutil.CheckRemovePeer(suite.Require(), rc.Check(region), 3)
operatorutil.CheckRemovePeer(suite.Require(), rc.Check(region), 3)
region = region.Clone(core.WithRemoveStorePeer(4))
// the number of region peers equals the maxReplicas
// Transfer peer to store 4.
testutil.CheckTransferPeer(suite.Require(), rc.Check(region), operator.OpReplica, 3, 4)
operatorutil.CheckTransferPeer(suite.Require(), rc.Check(region), operator.OpReplica, 3, 4)

// Store 5 has a same label score with store 4, but the region score smaller than store 4, we will choose store 5.
tc.AddLabelsStore(5, 3, map[string]string{"zone": "z4", "rack": "r1", "host": "h1"})
testutil.CheckTransferPeer(suite.Require(), rc.Check(region), operator.OpReplica, 3, 5)
operatorutil.CheckTransferPeer(suite.Require(), rc.Check(region), operator.OpReplica, 3, 5)
// Store 5 has too many snapshots, choose store 4
tc.UpdateSnapshotCount(5, 100)
testutil.CheckTransferPeer(suite.Require(), rc.Check(region), operator.OpReplica, 3, 4)
operatorutil.CheckTransferPeer(suite.Require(), rc.Check(region), operator.OpReplica, 3, 4)
tc.UpdatePendingPeerCount(4, 100)
suite.Nil(rc.Check(region))
}
Expand All @@ -360,49 +360,49 @@ func (suite *replicaCheckerTestSuite) TestDistinctScore() {
// We need 3 replicas.
tc.AddLeaderRegion(1, 1)
region := tc.GetRegion(1)
testutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 2)
operatorutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 2)
peer2, _ := tc.AllocPeer(2)
region = region.Clone(core.WithAddPeer(peer2))

// Store 1,2,3 have the same zone, rack, and host.
tc.AddLabelsStore(3, 5, map[string]string{"zone": "z1", "rack": "r1", "host": "h1"})
testutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 3)
operatorutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 3)

// Store 4 has smaller region score.
tc.AddLabelsStore(4, 4, map[string]string{"zone": "z1", "rack": "r1", "host": "h1"})
testutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 4)
operatorutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 4)

// Store 5 has a different host.
tc.AddLabelsStore(5, 5, map[string]string{"zone": "z1", "rack": "r1", "host": "h2"})
testutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 5)
operatorutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 5)

// Store 6 has a different rack.
tc.AddLabelsStore(6, 6, map[string]string{"zone": "z1", "rack": "r2", "host": "h1"})
testutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 6)
operatorutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 6)

// Store 7 has a different zone.
tc.AddLabelsStore(7, 7, map[string]string{"zone": "z2", "rack": "r1", "host": "h1"})
testutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 7)
operatorutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 7)

// Test stateFilter.
tc.SetStoreOffline(7)
testutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 6)
operatorutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 6)
tc.SetStoreUp(7)
testutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 7)
operatorutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 7)

// Add peer to store 7.
peer7, _ := tc.AllocPeer(7)
region = region.Clone(core.WithAddPeer(peer7))

// Replace peer in store 1 with store 6 because it has a different rack.
testutil.CheckTransferPeer(suite.Require(), rc.Check(region), operator.OpReplica, 1, 6)
operatorutil.CheckTransferPeer(suite.Require(), rc.Check(region), operator.OpReplica, 1, 6)
// Disable locationReplacement feature.
tc.SetEnableLocationReplacement(false)
suite.Nil(rc.Check(region))
tc.SetEnableLocationReplacement(true)
peer6, _ := tc.AllocPeer(6)
region = region.Clone(core.WithAddPeer(peer6))
testutil.CheckRemovePeer(suite.Require(), rc.Check(region), 1)
operatorutil.CheckRemovePeer(suite.Require(), rc.Check(region), 1)
region = region.Clone(core.WithRemoveStorePeer(1), core.WithLeader(region.GetStorePeer(2)))
suite.Nil(rc.Check(region))

Expand All @@ -416,10 +416,10 @@ func (suite *replicaCheckerTestSuite) TestDistinctScore() {
// Store 2 and 6 have the same distinct score, but store 2 has larger region score.
// So replace peer in store 2 with store 10.
tc.AddLabelsStore(10, 1, map[string]string{"zone": "z3", "rack": "r1", "host": "h1"})
testutil.CheckTransferPeer(suite.Require(), rc.Check(region), operator.OpReplica, 2, 10)
operatorutil.CheckTransferPeer(suite.Require(), rc.Check(region), operator.OpReplica, 2, 10)
peer10, _ := tc.AllocPeer(10)
region = region.Clone(core.WithAddPeer(peer10))
testutil.CheckRemovePeer(suite.Require(), rc.Check(region), 2)
operatorutil.CheckRemovePeer(suite.Require(), rc.Check(region), 2)
region = region.Clone(core.WithRemoveStorePeer(2))
suite.Nil(rc.Check(region))
}
Expand All @@ -443,11 +443,11 @@ func (suite *replicaCheckerTestSuite) TestDistinctScore2() {
tc.AddLeaderRegion(1, 1, 2, 4)
region := tc.GetRegion(1)

testutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 6)
operatorutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 6)
peer6, _ := tc.AllocPeer(6)
region = region.Clone(core.WithAddPeer(peer6))

testutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 5)
operatorutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 5)
peer5, _ := tc.AllocPeer(5)
region = region.Clone(core.WithAddPeer(peer5))

Expand Down Expand Up @@ -475,7 +475,7 @@ func (suite *replicaCheckerTestSuite) TestStorageThreshold() {

// Move peer to better location.
tc.UpdateStorageRatio(4, 0, 1)
testutil.CheckTransferPeer(suite.Require(), rc.Check(region), operator.OpReplica, 1, 4)
operatorutil.CheckTransferPeer(suite.Require(), rc.Check(region), operator.OpReplica, 1, 4)
// If store4 is almost full, do not add peer on it.
tc.UpdateStorageRatio(4, 0.9, 0.1)
suite.Nil(rc.Check(region))
Expand All @@ -484,10 +484,10 @@ func (suite *replicaCheckerTestSuite) TestStorageThreshold() {
region = tc.GetRegion(2)
// Add peer on store4.
tc.UpdateStorageRatio(4, 0, 1)
testutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 4)
operatorutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 4)
// If store4 is almost full, do not add peer on it.
tc.UpdateStorageRatio(4, 0.8, 0)
testutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 2)
operatorutil.CheckAddPeer(suite.Require(), rc.Check(region), operator.OpReplica, 2)
}

func (suite *replicaCheckerTestSuite) TestOpts() {
Expand All @@ -513,9 +513,9 @@ func (suite *replicaCheckerTestSuite) TestOpts() {
}))
tc.SetStoreOffline(2)
// RemoveDownReplica has higher priority than replaceOfflineReplica.
testutil.CheckTransferPeer(suite.Require(), rc.Check(region), operator.OpReplica, 1, 4)
operatorutil.CheckTransferPeer(suite.Require(), rc.Check(region), operator.OpReplica, 1, 4)
tc.SetEnableRemoveDownReplica(false)
testutil.CheckTransferPeer(suite.Require(), rc.Check(region), operator.OpReplica, 2, 4)
operatorutil.CheckTransferPeer(suite.Require(), rc.Check(region), operator.OpReplica, 2, 4)
tc.SetEnableReplaceOfflineReplica(false)
suite.Nil(rc.Check(region))
}
Expand All @@ -542,10 +542,10 @@ func (suite *replicaCheckerTestSuite) TestFixDownPeer() {
region = region.Clone(core.WithDownPeers([]*pdpb.PeerStats{
{Peer: region.GetStorePeer(4), DownSeconds: 6000},
}))
testutil.CheckTransferPeer(suite.Require(), rc.Check(region), operator.OpRegion, 4, 5)
operatorutil.CheckTransferPeer(suite.Require(), rc.Check(region), operator.OpRegion, 4, 5)

tc.SetStoreDown(5)
testutil.CheckTransferPeer(suite.Require(), rc.Check(region), operator.OpRegion, 4, 2)
operatorutil.CheckTransferPeer(suite.Require(), rc.Check(region), operator.OpRegion, 4, 2)

tc.SetIsolationLevel("zone")
suite.Nil(rc.Check(region))
Expand All @@ -570,10 +570,10 @@ func (suite *replicaCheckerTestSuite) TestFixOfflinePeer() {
suite.Nil(rc.Check(region))

tc.SetStoreOffline(4)
testutil.CheckTransferPeer(suite.Require(), rc.Check(region), operator.OpRegion, 4, 5)
operatorutil.CheckTransferPeer(suite.Require(), rc.Check(region), operator.OpRegion, 4, 5)

tc.SetStoreOffline(5)
testutil.CheckTransferPeer(suite.Require(), rc.Check(region), operator.OpRegion, 4, 2)
operatorutil.CheckTransferPeer(suite.Require(), rc.Check(region), operator.OpRegion, 4, 2)

tc.SetIsolationLevel("zone")
suite.Nil(rc.Check(region))
Expand Down
Loading

0 comments on commit 63311d2

Please sign in to comment.