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

balance hot regions by peer && by leader #638

Merged
merged 21 commits into from
May 27, 2017

Conversation

zhangjinpeng87
Copy link
Member

@zhangjinpeng87 zhangjinpeng87 commented May 8, 2017

We just consider the leader when do "hot regions" balance, this will lead to a bad situation that a store contains many hot regions’ peer, and TiKV nodes load various from each other.
@nolouch @disksing @siddontang PTAL

RegionsCountAsLeader int `json:"regions_count_as_leader"`
RegionsStatAsLeader RegionsStat

// Hot regions in this store as the role of Follower or Follower
Copy link
Contributor

Choose a reason for hiding this comment

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

what is of Follower or Follower?

// Hot regions in this store as the role of replica
WrittenBytesAsPeer uint64 `json:"total_written_bytes_as_peer"`
RegionsCountAsPeer int `json:"regions_count_as_peer"`
RegionsStatAsPeer RegionsStat `json:"statistics_as_peer"`
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 you can use 2 maps to store 2 types of hot regions.

if !ok {
status = &StoreHotRegions{
RegionsStat: make(RegionsStat, 0, storeHotRegionsDefaultLen),
LeaderStoreID := regionInfo.Leader.GetStoreId()
Copy link
Contributor

Choose a reason for hiding this comment

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

LeaderStoreID -> leaderStoreID

status = &StoreHotRegions{
RegionsStat: make(RegionsStat, 0, storeHotRegionsDefaultLen),
LeaderStoreID := regionInfo.Leader.GetStoreId()
StoreIDs := regionInfo.GetStoreIds()
Copy link
Contributor

Choose a reason for hiding this comment

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

StoreIDs -> storeIDs

minRegionsCount := int(math.MaxInt32)
for _, storeID := range candidateStoreIDs {
if s, ok := h.scoreStatus[storeID]; ok {
if srcHotRegionsCount-s.RegionsStatAsPeer.Len() > 1 && minRegionsCount > s.RegionsStatAsLeader.Len() {
Copy link
Contributor

Choose a reason for hiding this comment

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

s/RegionsStatAsLeader/RegionsStatAsPeer

}

avgRegionCount := hotRegionTotalCount / float64(len(h.scoreStatus))
// Multiplied by hotRegionLimitFactor to avoid transfer back and forth
limit := uint64((float64(s.RegionsStat.Len()) - avgRegionCount) * hotRegionLimitFactor)
limit := uint64((float64(s.RegionsStatAsPeer.Len()) - avgRegionCount) * hotRegionLimitFactor)
Copy link
Contributor

Choose a reason for hiding this comment

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

need we adjust limit by leader?

@siddontang
Copy link
Contributor

PTAL @disksing @nolouch

func (h *balanceHotRegionScheduler) GetStatus() *StoreHotRegionInfos {
h.RLock()
defer h.RUnlock()
asPeer := make(map[uint64]*HotRegionsStat)
Copy link
Contributor

Choose a reason for hiding this comment

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

make with capacity

}

func newBalanceHotRegionScheduler(opt *scheduleOption) *balanceHotRegionScheduler {
return &balanceHotRegionScheduler{
opt: opt,
limit: 1,
scoreStatus: make(map[uint64]*StoreHotRegions),
statisticsAsPeer: make(map[uint64]*HotRegionsStat),
statisticsAsLeader: make(map[uint64]*HotRegionsStat),
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems not well formatted? Have you tried make dev?


if srcPeer == nil {
// Todo: should panic here
return nil, nil, nil
Copy link
Contributor

Choose a reason for hiding this comment

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

Why should panic here? Is there a chance that the hot region statistics and region cache are not perfectly synced?

sourceStore = sid
if maxHotStoreRegionCount == statistics.RegionsStat.Len() && maxWrittenBytes < statistics.WrittenBytes {
maxWrittenBytes = statistics.WrittenBytes
srcStoreID = storeID
Copy link
Contributor

Choose a reason for hiding this comment

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

I suppose you can make it simpler:

count, writtenBytes := statistics.RegionStat.Len(), statistics.WrittenBytes
if count >= 2 && (count > maxRegionCount || count == maxRegionCount && writtenBytes > maxWrittenBytes) {
    maxRegionCount, maxWrittenBytes = count, writtenBytes
    srcStoreID = storeID
}

destStoreID = h.selectDestStoreByPeer(destStoreIDs, srcRegion, srcStoreID)
if destStoreID != 0 {
srcRegion.WrittenBytes = rs.WrittenBytes
h.adjustBalanceLimitByPeer(srcStoreID)
Copy link
Contributor

Choose a reason for hiding this comment

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

If the peer is relatively balanced, the limit will not adjust when balance leader.

@siddontang
Copy link
Contributor

Any update?

@zhangjinpeng87
Copy link
Member Author

@nolouch PTAL

@@ -31,6 +30,13 @@ const (
bootstrapBalanceDiff = 2
)

type BalanceType int
Copy link
Contributor

Choose a reason for hiding this comment

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

please fix this remind by make dev. and then pass the ci

Copy link
Contributor

@nolouch nolouch left a comment

Choose a reason for hiding this comment

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

rest LGTM

@disksing
Copy link
Contributor

disksing commented May 27, 2017

# github.com/pingcap/pd/server
server/coordinator.go:186: cannot range over status (type *StoreHotRegionInfos)
make: *** [build] Error 2

@zhangjinpeng1987 Please merge master and fix the compile error.

@disksing
Copy link
Contributor

LGTM.

@disksing disksing merged commit 04d9bec into master May 27, 2017
@disksing disksing deleted the zhangjinpeng/balance-hot-peer branch May 27, 2017 05:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants