Skip to content

Commit

Permalink
This is an automated cherry-pick of #4096
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
  • Loading branch information
HunDunDM authored and ti-chi-bot committed Sep 16, 2021
1 parent 605dc23 commit 3f6a510
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
45 changes: 45 additions & 0 deletions server/schedulers/evict_leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,52 @@ func (s *evictLeaderScheduler) Schedule(cluster opt.Cluster) []*operator.Operato
break
}
}
<<<<<<< HEAD

=======
return ops
}

func scheduleEvictLeaderOnce(name string, cluster opt.Cluster, storeRanges map[uint64][]core.KeyRange) []*operator.Operator {
ops := make([]*operator.Operator, 0, len(storeRanges))
for id, ranges := range storeRanges {
var filters []filter.Filter
region := cluster.RandLeaderRegion(id, ranges, opt.HealthRegion(cluster))
if region == nil {
// try to pick unhealthy region
region = cluster.RandLeaderRegion(id, ranges)
if region == nil {
schedulerCounter.WithLabelValues(name, "no-leader").Inc()
continue
}
schedulerCounter.WithLabelValues(name, "pick-unhealthy-region").Inc()
unhealthyPeerStores := make(map[uint64]struct{})
for _, peer := range region.GetDownPeers() {
unhealthyPeerStores[peer.GetPeer().GetStoreId()] = struct{}{}
}
for _, peer := range region.GetPendingPeers() {
unhealthyPeerStores[peer.GetStoreId()] = struct{}{}
}
filters = append(filters, filter.NewExcludedFilter(EvictLeaderName, nil, unhealthyPeerStores))
}

filters = append(filters, &filter.StoreStateFilter{ActionScope: EvictLeaderName, TransferLeader: true})
target := filter.NewCandidates(cluster.GetFollowerStores(region)).
FilterTarget(cluster.GetOpts(), filters...).RandomPick()
if target == nil {
schedulerCounter.WithLabelValues(name, "no-target-store").Inc()
continue
}
op, err := operator.CreateTransferLeaderOperator(EvictLeaderType, cluster, region, region.GetLeader().GetStoreId(), target.GetID(), operator.OpLeader)
if err != nil {
log.Debug("fail to create evict leader operator", errs.ZapError(err))
continue
}
op.SetPriorityLevel(core.HighPriority)
op.Counters = append(op.Counters, schedulerCounter.WithLabelValues(name, "new-operator"))
ops = append(ops, op)
}
>>>>>>> 3b6d07be3 (schedulers: evict-leader supports schedule the regions with unhealthy peers (#4096))
return ops
}

Expand Down
35 changes: 35 additions & 0 deletions server/schedulers/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

. "github.com/pingcap/check"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/tikv/pd/pkg/mock/mockcluster"
"github.com/tikv/pd/pkg/mock/mockoption"
"github.com/tikv/pd/pkg/testutil"
Expand Down Expand Up @@ -359,6 +360,40 @@ func (s *testEvictLeaderSuite) TestEvictLeader(c *C) {
testutil.CheckTransferLeader(c, op[0], operator.OpLeader, 1, 2)
}

func (s *testEvictLeaderSuite) TestEvictLeaderWithUnhealthyPeer(c *C) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
opt := config.NewTestOptions()
tc := mockcluster.NewCluster(ctx, opt)
sl, err := schedule.CreateScheduler(EvictLeaderType, schedule.NewOperatorController(ctx, nil, nil), core.NewStorage(kv.NewMemoryKV()), schedule.ConfigSliceDecoder(EvictLeaderType, []string{"1"}))
c.Assert(err, IsNil)

// Add stores 1, 2, 3
tc.AddLeaderStore(1, 0)
tc.AddLeaderStore(2, 0)
tc.AddLeaderStore(3, 0)
// Add region 1, which has 3 peers. 1 is leader. 2 is healthy or pending, 3 is healthy or down.
tc.AddLeaderRegion(1, 1, 2, 3)
region := tc.MockRegionInfo(1, 1, []uint64{2, 3}, nil, nil)
withDownPeer := core.WithDownPeers([]*pdpb.PeerStats{{
Peer: region.GetPeers()[2],
DownSeconds: 1000,
}})
withPendingPeer := core.WithPendingPeers([]*metapb.Peer{region.GetPeers()[1]})

// only pending
tc.PutRegion(region.Clone(withPendingPeer))
op := sl.Schedule(tc)
testutil.CheckTransferLeader(c, op[0], operator.OpLeader, 1, 3)
// only down
tc.PutRegion(region.Clone(withDownPeer))
op = sl.Schedule(tc)
testutil.CheckTransferLeader(c, op[0], operator.OpLeader, 1, 2)
// pending + down
tc.PutRegion(region.Clone(withPendingPeer, withDownPeer))
c.Assert(sl.Schedule(tc), HasLen, 0)
}

var _ = Suite(&testShuffleRegionSuite{})

type testShuffleRegionSuite struct{}
Expand Down

0 comments on commit 3f6a510

Please sign in to comment.