Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scheduler: add leader verify in balance-region scheduler #2966

Merged
merged 8 commits into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions pkg/mock/mockcluster/mockcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@ func (mc *Cluster) AddRegionStore(storeID uint64, regionCount int) {
mc.PutStore(store)
}

// AddRegionInfo supports add regionInfo directly
func (mc *Cluster) AddRegionInfo(regionInfo *core.RegionInfo) {
Yisaer marked this conversation as resolved.
Show resolved Hide resolved
mc.PutRegion(regionInfo)
}

// AddRegionStoreWithLeader adds store with specified count of region and leader.
func (mc *Cluster) AddRegionStoreWithLeader(storeID uint64, regionCount int, leaderCounts ...int) {
leaderCount := regionCount
Expand Down Expand Up @@ -292,7 +297,7 @@ func (mc *Cluster) AddLeaderRegion(regionID uint64, leaderStoreID uint64, follow

// AddRegionWithLearner adds region with specified leader, followers and learners.
func (mc *Cluster) AddRegionWithLearner(regionID uint64, leaderStoreID uint64, followerStoreIDs, learnerStoreIDs []uint64) *core.RegionInfo {
origin := mc.MockRegionInfo(regionID, leaderStoreID, followerStoreIDs, learnerStoreIDs, nil)
origin := mc.MockRegionInfo(regionID, &leaderStoreID, followerStoreIDs, learnerStoreIDs, nil)
region := origin.Clone(core.SetApproximateSize(10), core.SetApproximateKeys(10))
mc.PutRegion(region)
return region
Expand Down Expand Up @@ -535,7 +540,7 @@ func (mc *Cluster) UpdateStoreStatus(id uint64) {
}

func (mc *Cluster) newMockRegionInfo(regionID uint64, leaderStoreID uint64, followerStoreIDs ...uint64) *core.RegionInfo {
return mc.MockRegionInfo(regionID, leaderStoreID, followerStoreIDs, []uint64{}, nil)
return mc.MockRegionInfo(regionID, &leaderStoreID, followerStoreIDs, []uint64{}, nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When does nil need to be passed in here? In addition, 0 can also mean that store does not exist.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think check 0 is better.

}

// CheckLabelProperty checks label property.
Expand Down Expand Up @@ -574,7 +579,8 @@ func (mc *Cluster) RemoveScheduler(name string) error {
}

// MockRegionInfo returns a mock region
func (mc *Cluster) MockRegionInfo(regionID uint64, leaderStoreID uint64,
// If leaderStoreID is undefined, the regions would have no leader
func (mc *Cluster) MockRegionInfo(regionID uint64, leaderStoreID *uint64,
followerStoreIDs, learnerStoreIDs []uint64, epoch *metapb.RegionEpoch) *core.RegionInfo {

region := &metapb.Region{
Expand All @@ -583,8 +589,12 @@ func (mc *Cluster) MockRegionInfo(regionID uint64, leaderStoreID uint64,
EndKey: []byte(fmt.Sprintf("%20d", regionID+1)),
RegionEpoch: epoch,
}
leader, _ := mc.AllocPeer(leaderStoreID)
region.Peers = []*metapb.Peer{leader}
region.Peers = []*metapb.Peer{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
region.Peers = []*metapb.Peer{}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.

var leader *metapb.Peer
if leaderStoreID != nil {
leader, _ = mc.AllocPeer(*leaderStoreID)
region.Peers = append(region.Peers, leader)
}
for _, storeID := range followerStoreIDs {
peer, _ := mc.AllocPeer(storeID)
region.Peers = append(region.Peers, peer)
Expand Down
19 changes: 19 additions & 0 deletions pkg/pointer/pointer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2020 TiKV Project Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package pointer
Yisaer marked this conversation as resolved.
Show resolved Hide resolved

// UInt64Ptr return the pointer for the given value
func UInt64Ptr(value uint64) *uint64 {
return &value
}
7 changes: 2 additions & 5 deletions server/schedule/filter/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package filter
import (
"fmt"

"github.com/golang/protobuf/proto"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/log"
"github.com/tikv/pd/pkg/slice"
Expand Down Expand Up @@ -683,11 +684,7 @@ func (f *isolationFilter) Target(opt *config.PersistOptions, store *core.StoreIn
// FitRegion in filter
func createRegionForRuleFit(startKey, endKey []byte,
peers []*metapb.Peer, leader *metapb.Peer, opts ...core.RegionCreateOption) *core.RegionInfo {
copyLeader := &metapb.Peer{
Id: leader.Id,
StoreId: leader.StoreId,
Role: leader.Role,
}
copyLeader := proto.Clone(leader).(*metapb.Peer)
copyPeers := make([]*metapb.Peer, 0, len(peers))
for _, p := range peers {
peer := &metapb.Peer{
Expand Down
9 changes: 5 additions & 4 deletions server/schedule/operator_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/tikv/pd/pkg/mock/mockcluster"
"github.com/tikv/pd/pkg/pointer"
"github.com/tikv/pd/server/config"
"github.com/tikv/pd/server/core"
"github.com/tikv/pd/server/core/storelimit"
Expand Down Expand Up @@ -428,15 +429,15 @@ func (t *testOperatorControllerSuite) TestDispatchOutdatedRegion(c *C) {
c.Assert(stream.MsgLength(), Equals, 1)

// report the result of transferring leader
region := cluster.MockRegionInfo(1, 2, []uint64{1, 2}, []uint64{},
region := cluster.MockRegionInfo(1, pointer.UInt64Ptr(2), []uint64{1, 2}, []uint64{},
&metapb.RegionEpoch{ConfVer: 0, Version: 0})

controller.Dispatch(region, DispatchFromHeartBeat)
c.Assert(op.ConfVerChanged(region), Equals, uint64(0))
c.Assert(stream.MsgLength(), Equals, 2)

// report the result of removing peer
region = cluster.MockRegionInfo(1, 2, []uint64{2}, []uint64{},
region = cluster.MockRegionInfo(1, pointer.UInt64Ptr(2), []uint64{2}, []uint64{},
&metapb.RegionEpoch{ConfVer: 0, Version: 0})

controller.Dispatch(region, DispatchFromHeartBeat)
Expand All @@ -452,7 +453,7 @@ func (t *testOperatorControllerSuite) TestDispatchOutdatedRegion(c *C) {
c.Assert(stream.MsgLength(), Equals, 3)

// report region with an abnormal confver
region = cluster.MockRegionInfo(1, 1, []uint64{1, 2}, []uint64{},
region = cluster.MockRegionInfo(1, pointer.UInt64Ptr(1), []uint64{1, 2}, []uint64{},
&metapb.RegionEpoch{ConfVer: 1, Version: 0})
controller.Dispatch(region, DispatchFromHeartBeat)
c.Assert(op.ConfVerChanged(region), Equals, uint64(0))
Expand All @@ -470,7 +471,7 @@ func (t *testOperatorControllerSuite) TestDispatchUnfinishedStep(c *C) {
// so the two peers are {peerid: 1, storeid: 1}, {peerid: 2, storeid: 2}
// The peer on store 1 is the leader
epoch := &metapb.RegionEpoch{ConfVer: 0, Version: 0}
region := cluster.MockRegionInfo(1, 1, []uint64{2}, []uint64{}, epoch)
region := cluster.MockRegionInfo(1, pointer.UInt64Ptr(1), []uint64{2}, []uint64{}, epoch)
// Put region into cluster, otherwise, AddOperator will fail because of
// missing region
cluster.PutRegion(region)
Expand Down
6 changes: 6 additions & 0 deletions server/schedulers/balance_region.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ func (s *balanceRegionScheduler) Schedule(cluster opt.Cluster) []*operator.Opera
schedulerCounter.WithLabelValues(s.GetName(), "region-hot").Inc()
continue
}
// Check region whether have leader
if region.GetLeader() == nil {
Yisaer marked this conversation as resolved.
Show resolved Hide resolved
log.Debug("region have no leader", zap.String("scheduler", s.GetName()), zap.Uint64("region-id", region.GetID()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it may be warn?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.

schedulerCounter.WithLabelValues(s.GetName(), "no-leader").Inc()
continue
}

oldPeer := region.GetStorePeer(sourceID)
if op := s.transferPeer(cluster, region, oldPeer); op != nil {
Expand Down
17 changes: 17 additions & 0 deletions server/schedulers/balance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,23 @@ func (s *testBalanceRegionSchedulerSuite) checkReplacePendingRegion(c *C, tc *mo
testutil.CheckTransferPeer(c, sb.Schedule(tc)[0], operator.OpKind(0), 1, 4)
}

func (s *testBalanceRegionSchedulerSuite) TestShouldNotBalance(c *C) {
opt := config.NewTestOptions()
tc := mockcluster.NewCluster(opt)
tc.DisableFeature(versioninfo.JointConsensus)
oc := schedule.NewOperatorController(s.ctx, nil, nil)
sb, err := schedule.CreateScheduler(BalanceRegionType, oc, core.NewStorage(kv.NewMemoryKV()), schedule.ConfigSliceDecoder(BalanceRegionType, []string{"", ""}))
c.Assert(err, IsNil)
region := tc.MockRegionInfo(1, nil, []uint64{2, 3, 4}, nil, nil)
tc.AddRegionInfo(region)
operators := sb.Schedule(tc)
if operators != nil {
c.Assert(len(operators), Equals, 0)
} else {
c.Assert(operators, IsNil)
}
}

var _ = Suite(&testRandomMergeSchedulerSuite{})

type testRandomMergeSchedulerSuite struct{}
Expand Down