-
Notifications
You must be signed in to change notification settings - Fork 726
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
*: add hot region schedule #611
Changes from 6 commits
e2982b7
2a2ef7f
ca4c248
7d8e7bf
0f6c35c
382986e
465e6e7
20e78b0
0d21c20
5050c94
f5c9de4
e4f281d
034e975
9228598
a787547
1097525
ef4fe14
3eae118
f18d1c4
d9ad9f7
95e3530
5e8f5a8
174782e
1c3605a
30bcb69
ce794ce
bad2c8a
18e60e9
c2469d7
7d80df0
2760bc9
79a39d8
79b9de3
91e69c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/pingcap/pd/server" | ||
"github.com/unrolled/render" | ||
) | ||
|
||
type hotStatusHandler struct { | ||
svr *server.Server | ||
rd *render.Render | ||
} | ||
|
||
func newHotStatusHandler(svr *server.Server, rd *render.Render) *hotStatusHandler { | ||
return &hotStatusHandler{ | ||
svr: svr, | ||
rd: rd, | ||
} | ||
} | ||
|
||
func (h *hotStatusHandler) GetHot(w http.ResponseWriter, r *http.Request) { | ||
h.rd.JSON(w, http.StatusOK, h.svr.GetHotRegions()) | ||
} | ||
|
||
func (h *hotStatusHandler) GetHotStores(w http.ResponseWriter, r *http.Request) { | ||
h.rd.JSON(w, http.StatusOK, h.svr.GetHotStores()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ package server | |
|
||
import ( | ||
"math" | ||
"math/rand" | ||
"sort" | ||
"time" | ||
|
||
"github.com/montanaflynn/stats" | ||
|
@@ -370,3 +372,210 @@ func (r *replicaChecker) checkBestReplacement(region *RegionInfo) Operator { | |
} | ||
return newTransferPeer(region, oldPeer, newPeer) | ||
} | ||
|
||
type RegionStateValue struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/RegionStateValue/RegionStat |
||
RegionID uint64 `json:"region_id"` | ||
WriteBytes uint64 `json:"write_bytes"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Written |
||
UpdateTimes int `json:"update_times"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. UpdateCount |
||
LastUpdateTime time.Time `json:"last_update_time"` | ||
StoreID uint64 `json: "-"` | ||
antiTimes int `json:"-"` | ||
version uint64 `json:"-"` | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any need to explain what do WrittenBytes / HotDegree / antiCount mean and what they are used for? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for JSON encoding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess @disksing wants to know the meaning of some fields, not only for JSON. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep. I know what they do generally, but I think there should be some explanation here, for potential future readers. |
||
type MetaRegionStatus []RegionStateValue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add test for the slice sort |
||
|
||
func (m MetaRegionStatus) Len() int { return len(m) } | ||
func (m MetaRegionStatus) Swap(i, j int) { m[i], m[j] = m[j], m[i] } | ||
func (m MetaRegionStatus) Less(i, j int) bool { return m[i].WriteBytes > m[j].WriteBytes } | ||
|
||
type RegionHotStatus struct { | ||
TotalWriteBytes uint64 `json:"total_write"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What the difference between |
||
MetaStatus MetaRegionStatus `json:"status"` | ||
} | ||
|
||
type balanceHotRegionScheduler struct { | ||
opt *scheduleOption | ||
limit uint64 | ||
scoreStatus map[uint64]RegionHotStatus // store id -> regions status in this store | ||
r *rand.Rand | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not using global Rand directly? Using Source directly is not thread safe. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It just used in one thread. |
||
} | ||
|
||
func newBalanceHotRegionScheduler(opt *scheduleOption) *balanceHotRegionScheduler { | ||
return &balanceHotRegionScheduler{ | ||
opt: opt, | ||
limit: 1, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we modify the limit? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use a const var |
||
scoreStatus: make(map[uint64]RegionHotStatus), | ||
r: rand.New(rand.NewSource(time.Now().UnixNano())), | ||
} | ||
} | ||
|
||
func (l *balanceHotRegionScheduler) GetName() string { | ||
return "balance-hot-region-scheduler" | ||
} | ||
|
||
func (l *balanceHotRegionScheduler) GetResourceKind() ResourceKind { | ||
return priorityKind | ||
} | ||
|
||
func (l *balanceHotRegionScheduler) GetResourceLimit() uint64 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/l/s/ |
||
return l.limit | ||
} | ||
|
||
func (l *balanceHotRegionScheduler) Prepare(cluster *clusterInfo) error { return nil } | ||
|
||
func (l *balanceHotRegionScheduler) Cleanup(cluster *clusterInfo) {} | ||
|
||
func (l *balanceHotRegionScheduler) Schedule(cluster *clusterInfo) Operator { | ||
l.CalculateScore(cluster) | ||
region, leader := l.SelectTransferLeader(cluster) | ||
if region == nil { | ||
return nil | ||
} | ||
if leader != nil { | ||
return newPriorityTransferLeader(region, leader) | ||
} | ||
peer := l.SelectTransferPeer(cluster, region) | ||
return newPriorityTransferPeer(region, region.Leader, peer) | ||
} | ||
|
||
func (l *balanceHotRegionScheduler) clearScore() { | ||
for sid, status := range l.scoreStatus { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer clear the map directly. |
||
status.TotalWriteBytes = 0 | ||
if status.MetaStatus.Len() != 0 { | ||
status.MetaStatus = status.MetaStatus[0:0] | ||
} | ||
l.scoreStatus[sid] = status | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need to abstract a function here. It's only 1 line and be called only at 1 place. |
||
func (l *balanceHotRegionScheduler) CalculateScore(cluster *clusterInfo) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need to public? |
||
l.clearScore() | ||
items := cluster.writeStatus.GetList() | ||
for _, item := range items { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it safe to access items after unlocked? |
||
r := item.(RegionStateValue) | ||
if r.UpdateTimes < 5 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make this 5 a const? |
||
continue | ||
} | ||
|
||
id := r.RegionID | ||
regionInfo := cluster.getRegion(id) | ||
if regionInfo.WriteBytes == 0 { | ||
log.Infof("Debug error meet 0 write_bytes %d \n", regionInfo.GetId()) | ||
continue | ||
} | ||
storeID := regionInfo.Leader.GetStoreId() | ||
status, ok := l.scoreStatus[storeID] | ||
if !ok { | ||
status = RegionHotStatus{ | ||
MetaStatus: make(MetaRegionStatus, 0, 100), | ||
} | ||
l.scoreStatus[storeID] = status | ||
} | ||
status.TotalWriteBytes += regionInfo.WriteBytes | ||
status.MetaStatus = append(status.MetaStatus, RegionStateValue{id, regionInfo.WriteBytes, r.UpdateTimes, r.LastUpdateTime, storeID, r.antiTimes, r.version}) | ||
l.scoreStatus[storeID] = status | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line is redundant. |
||
} | ||
|
||
for sid, rs := range l.scoreStatus { | ||
sort.Sort(rs.MetaStatus) | ||
l.scoreStatus[sid] = rs | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
should be ok. |
||
} | ||
func (l *balanceHotRegionScheduler) SelectSourceRegion(cluster *clusterInfo) *RegionInfo { | ||
var ( | ||
maxWrite uint64 | ||
sourceStore uint64 | ||
) | ||
for sid, s := range l.scoreStatus { | ||
if s.MetaStatus.Len() < 2 { | ||
continue | ||
} | ||
if maxWrite < s.TotalWriteBytes { | ||
maxWrite = s.TotalWriteBytes | ||
sourceStore = sid | ||
} | ||
} | ||
if sourceStore == 0 { | ||
return nil | ||
} | ||
|
||
length := l.scoreStatus[sourceStore].MetaStatus.Len() | ||
for i := 0; i < 10; i++ { | ||
rr := l.r.Int31n(int32((length+1)/2)) + int32(length/2) | ||
rid := l.scoreStatus[sourceStore].MetaStatus[rr].RegionID | ||
region := cluster.getRegion(rid) | ||
if len(region.DownPeers) != 0 || len(region.PendingPeers) != 0 { | ||
log.Info("Debug not select peer", region.DownPeers, region.PendingPeers) | ||
continue | ||
} | ||
return region | ||
} | ||
return nil | ||
} | ||
|
||
func (l *balanceHotRegionScheduler) GetStatus() map[uint64]RegionHotStatus { | ||
return l.scoreStatus | ||
} | ||
|
||
func (l *balanceHotRegionScheduler) SelectTransferLeader(cluster *clusterInfo) (*RegionInfo, *metapb.Peer) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function do two different things(select source region and select target leader), the function name make me confused. |
||
sourceRegion := l.SelectSourceRegion(cluster) | ||
if sourceRegion == nil { | ||
log.Info("Debug not select source region") | ||
return nil, nil | ||
} | ||
sourceStoreWriteBytes := l.scoreStatus[sourceRegion.Leader.GetStoreId()].TotalWriteBytes | ||
var targetPeer *metapb.Peer | ||
|
||
//select follow to transfer leader | ||
var least uint64 = math.MaxUint64 | ||
for _, peer := range sourceRegion.GetFollowers() { | ||
if s, ok := l.scoreStatus[peer.GetStoreId()]; ok { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add comment for the logic of this algorithm. |
||
if least >= s.TotalWriteBytes && uint64(float64(sourceStoreWriteBytes)*hotRegionScheduleFactor) > s.TotalWriteBytes+2*sourceRegion.WriteBytes { | ||
targetPeer = peer | ||
least = s.TotalWriteBytes | ||
} | ||
} else { | ||
targetPeer = peer | ||
least = 0 | ||
} | ||
} | ||
if targetPeer != nil { | ||
log.Infof("Debug Transfer Leader Source:%d Target:%d Write:%d,RegionID:%d Key:%s\n", sourceRegion.Leader.GetStoreId(), targetPeer.GetStoreId(), sourceRegion.WriteBytes, sourceRegion.GetId(), sourceRegion.GetEndKey()) | ||
} | ||
return sourceRegion, targetPeer | ||
} | ||
|
||
func (l *balanceHotRegionScheduler) SelectTransferPeer(cluster *clusterInfo, region *RegionInfo) *metapb.Peer { | ||
filter := newExcludedFilter(region.GetStoreIds(), region.GetStoreIds()) | ||
sourceStoreWriteBytes := l.scoreStatus[region.Leader.GetStoreId()].TotalWriteBytes | ||
stores := cluster.getStores() | ||
var bestStore *storeInfo | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/bestStore/TargetStore |
||
var least uint64 = math.MaxUint64 | ||
for _, store := range stores { | ||
if filter.FilterSource(store) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
continue | ||
} | ||
if s, ok := l.scoreStatus[store.GetId()]; ok { | ||
if least >= s.TotalWriteBytes && uint64(float64(sourceStoreWriteBytes)*hotRegionScheduleFactor) > s.TotalWriteBytes+2*region.WriteBytes { | ||
least = s.TotalWriteBytes | ||
bestStore = store | ||
} | ||
} else { | ||
least = 0 | ||
bestStore = store | ||
break | ||
} | ||
} | ||
if bestStore == nil { | ||
return nil | ||
} | ||
newPeer, err := cluster.allocPeer(bestStore.GetId()) | ||
if err != nil { | ||
log.Errorf("failed to allocate peer: %v", err) | ||
return nil | ||
} | ||
if newPeer != nil { | ||
log.Infof("Debug Transfer Peer Source:%d Target:%d Write:%d RegionID:%d Key:%s \n", region.Leader.GetStoreId(), newPeer.GetStoreId(), region.WriteBytes, region.GetId(), region.GetEndKey()) | ||
} | ||
|
||
return newPeer | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hot or hotspot?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/api/v1/hotspot/host
/api/v1/hotspot/store?