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

[DNM]server: add follower balance support and change score weight. #156

Closed
Closed
Changes from all 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
31 changes: 25 additions & 6 deletions server/balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (cb *capacityBalancer) score(store *storeInfo, regionCount int) float64 {
usedRatioScore := store.usedRatio()
leaderScore := store.leaderScore(regionCount)
log.Infof("capacity balancer store %d, used ratio score: %v, leader score: %v [region count: %d]", store.store.GetId(), usedRatioScore, leaderScore, regionCount)
return usedRatioScore*0.5 + leaderScore*0.5
return usedRatioScore*0.6 + leaderScore*0.4
}

func (cb *capacityBalancer) selectFromStore(stores []*storeInfo, regionCount int, useFilter bool) *storeInfo {
Expand Down Expand Up @@ -224,10 +224,25 @@ func (cb *capacityBalancer) Balance(cluster *clusterInfo) (*balanceOperator, err
followerPeers[storeID] = peer
}

var err error
removePeer := oldLeader
doLeaderBalance := true
newLeader := cb.selectNewLeaderPeer(cluster, followerPeers)
if newLeader == nil {
log.Warn("new leader peer cannot be found to do balance")
return nil, nil
log.Warn("new leader peer cannot be found to do balance, try to do follower peer balance")

// If we cannot find a region peer to do leader transfer,
// then we should balance a follower peer instead of leader peer.
doLeaderBalance = false

removePeer, err = cb.selectRemovePeer(cluster, followerPeers)
if err != nil {
return nil, errors.Trace(err)
}
if removePeer == nil {
log.Warnf("find no store to remove peer for region %v", region)
return nil, nil
}
}

// Thirdly, select one store to add new peer.
Expand All @@ -240,11 +255,15 @@ func (cb *capacityBalancer) Balance(cluster *clusterInfo) (*balanceOperator, err
return nil, nil
}

leaderTransferOperator := newTransferLeaderOperator(oldLeader, newLeader, maxWaitCount)
addPeerOperator := newAddPeerOperator(newPeer)
removePeerOperator := newRemovePeerOperator(oldLeader)
removePeerOperator := newRemovePeerOperator(removePeer)

if doLeaderBalance {
leaderTransferOperator := newTransferLeaderOperator(oldLeader, newLeader, maxWaitCount)
return newBalanceOperator(region, leaderTransferOperator, addPeerOperator, removePeerOperator), nil
}

return newBalanceOperator(region, leaderTransferOperator, addPeerOperator, removePeerOperator), nil
return newBalanceOperator(region, addPeerOperator, removePeerOperator), nil
}

// defaultBalancer is used for default config change, like add/remove peer.
Expand Down