From dc2835a45444968d0ac27fa1662e5372d5dfbed4 Mon Sep 17 00:00:00 2001 From: sunwp <244372610@qq.com> Date: Wed, 17 Nov 2021 10:23:13 +0800 Subject: [PATCH] Avoid using up the bandwidth of the CDN (#801) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * rename getdistance to getAffinity Signed-off-by: 孙伟鹏 * CDN has the lowest priority among all peers with the same number of pieces Signed-off-by: 孙伟鹏 --- scheduler/core/evaluator/basic/basic_evaluator.go | 6 +++--- scheduler/supervisor/peer.go | 8 ++++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/scheduler/core/evaluator/basic/basic_evaluator.go b/scheduler/core/evaluator/basic/basic_evaluator.go index f48fae00e..9e9462e53 100644 --- a/scheduler/core/evaluator/basic/basic_evaluator.go +++ b/scheduler/core/evaluator/basic/basic_evaluator.go @@ -96,7 +96,7 @@ func (eval *baseEvaluator) Evaluate(parent *supervisor.Peer, child *supervisor.P load := getHostLoad(parent.Host) - dist := getDistance(parent, child) + dist := getAffinity(parent, child) return profits * load * dist } @@ -128,8 +128,8 @@ func getHostLoad(host *supervisor.Host) float64 { return 1.0 - host.GetUploadLoadPercent() } -// getDistance 0.0~1.0 larger and better -func getDistance(dst *supervisor.Peer, src *supervisor.Peer) float64 { +// getAffinity 0.0~1.0 larger and better +func getAffinity(dst *supervisor.Peer, src *supervisor.Peer) float64 { hostDist := 40.0 if dst.Host == src.Host { hostDist = 0.0 diff --git a/scheduler/supervisor/peer.go b/scheduler/supervisor/peer.go index 45dd6dec5..b9638177d 100644 --- a/scheduler/supervisor/peer.go +++ b/scheduler/supervisor/peer.go @@ -404,8 +404,12 @@ func (peer *Peer) SortedValue() int { defer peer.lock.RUnlock() pieceCount := peer.TotalPieceCount.Load() - hostLoad := peer.getFreeLoad() - return int(pieceCount*HostMaxLoad + hostLoad) + freeLoad := peer.getFreeLoad() + if peer.Host.IsCDN { + // if peer's host is CDN, peer has the lowest priority among all peers with the same number of pieces + return int(pieceCount * HostMaxLoad) + } + return int(pieceCount*HostMaxLoad + freeLoad) } func (peer *Peer) getFreeLoad() int32 {