Skip to content

Commit

Permalink
server: fix frequent alloc id (#1013)
Browse files Browse the repository at this point in the history
* server: fix frequent alloc id
  • Loading branch information
nolouch committed Apr 10, 2018
1 parent e221ffb commit 3ea73bb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
29 changes: 10 additions & 19 deletions server/schedule/replica_checker.go
Expand Up @@ -81,21 +81,8 @@ func (r *ReplicaChecker) Check(region *core.RegionInfo) *Operator {
return r.checkBestReplacement(region)
}

// SelectBestReplacedPeerToAddReplica returns a new peer that to be used to replace the old peer and distinct score.
func (r *ReplicaChecker) SelectBestReplacedPeerToAddReplica(region *core.RegionInfo, oldPeer *metapb.Peer, filters ...Filter) *metapb.Peer {
storeID, _ := r.selectBestReplacementStore(region, oldPeer, filters...)
if storeID == 0 {
log.Debugf("[region %d] no best store to add replica", region.GetId())
return nil
}
newPeer, err := r.cluster.AllocPeer(storeID)
if err != nil {
return nil
}
return newPeer
}

func (r *ReplicaChecker) selectBestReplacementStore(region *core.RegionInfo, oldPeer *metapb.Peer, filters ...Filter) (uint64, float64) {
// SelectBestReplacementStore returns a store id that to be used to replace the old peer and distinct score.
func (r *ReplicaChecker) SelectBestReplacementStore(region *core.RegionInfo, oldPeer *metapb.Peer, filters ...Filter) (uint64, float64) {
filters = append(filters, NewExcludedFilter(nil, region.GetStoreIds()))
newRegion := region.Clone()
newRegion.RemoveStorePeer(oldPeer.GetStoreId())
Expand Down Expand Up @@ -197,9 +184,13 @@ func (r *ReplicaChecker) checkOfflinePeer(region *core.RegionInfo) *Operator {
return CreateRemovePeerOperator("removePendingOfflineReplica", r.cluster, OpReplica, region, peer.GetStoreId())
}

newPeer := r.SelectBestReplacedPeerToAddReplica(region, peer)
if newPeer == nil {
log.Debugf("[region %d] no best peer to add replica", region.GetId())
storeID, _ := r.SelectBestReplacementStore(region, peer)
if storeID == 0 {
log.Debugf("[region %d] no best store to add replica", region.GetId())
return nil
}
newPeer, err := r.cluster.AllocPeer(storeID)
if err != nil {
return nil
}
return CreateMovePeerOperator("makeUpOfflineReplica", r.cluster, region, OpReplica, peer.GetStoreId(), newPeer.GetStoreId(), newPeer.GetId())
Expand All @@ -214,7 +205,7 @@ func (r *ReplicaChecker) checkBestReplacement(region *core.RegionInfo) *Operator
checkerCounter.WithLabelValues("replica_checker", "all_right").Inc()
return nil
}
storeID, newScore := r.selectBestReplacementStore(region, oldPeer)
storeID, newScore := r.SelectBestReplacementStore(region, oldPeer)
if storeID == 0 {
checkerCounter.WithLabelValues("replica_checker", "no_replacement_store").Inc()
return nil
Expand Down
13 changes: 9 additions & 4 deletions server/schedulers/balance_region.go
Expand Up @@ -137,13 +137,13 @@ func (s *balanceRegionScheduler) transferPeer(cluster schedule.Cluster, region *
scoreGuard := schedule.NewDistinctScoreFilter(cluster.GetLocationLabels(), stores, source)

checker := schedule.NewReplicaChecker(cluster, nil)
newPeer := checker.SelectBestReplacedPeerToAddReplica(region, oldPeer, scoreGuard)
if newPeer == nil {
schedulerCounter.WithLabelValues(s.GetName(), "no_peer").Inc()
storeID, _ := checker.SelectBestReplacementStore(region, oldPeer, scoreGuard)
if storeID == 0 {
schedulerCounter.WithLabelValues(s.GetName(), "no_store").Inc()
return nil
}

target := cluster.GetStore(newPeer.GetStoreId())
target := cluster.GetStore(storeID)
log.Debugf("[region %d] source store id is %v, target store id is %v", region.GetId(), source.GetId(), target.GetId())

sourceSize := source.RegionSize + int64(opInfluence.GetStoreInfluence(source.GetId()).RegionSize)
Expand All @@ -154,6 +154,11 @@ func (s *balanceRegionScheduler) transferPeer(cluster schedule.Cluster, region *
schedulerCounter.WithLabelValues(s.GetName(), "skip").Inc()
return nil
}
newPeer, err := cluster.AllocPeer(storeID)
if err != nil {
schedulerCounter.WithLabelValues(s.GetName(), "no_peer").Inc()
return nil
}

return schedule.CreateMovePeerOperator("balance-region", cluster, region, schedule.OpBalance, oldPeer.GetStoreId(), newPeer.GetStoreId(), newPeer.GetId())
}

0 comments on commit 3ea73bb

Please sign in to comment.