From d759f836ce8fb4c54ce9d13c5f5b8fd98d0a7c4e Mon Sep 17 00:00:00 2001 From: Zexi Li Date: Wed, 19 Feb 2025 17:02:38 +0800 Subject: [PATCH] =?UTF-8?q?fix(scheduler):=20=E5=B0=BD=E6=97=A9=E5=8F=91?= =?UTF-8?q?=E7=8E=B0=E5=AD=90=E7=BD=91ip=E4=B8=8D=E8=B6=B3=E5=BC=95?= =?UTF-8?q?=E8=B5=B7=E7=9A=84=E8=B0=83=E5=BA=A6=E5=A4=B1=E8=B4=A5=20(#2213?= =?UTF-8?q?1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../algorithm/predicates/network_predicate.go | 3 +- pkg/scheduler/cache/candidate/hosts.go | 3 +- pkg/scheduler/models/pending_usage.go | 42 ++++++++++++++++--- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/pkg/scheduler/algorithm/predicates/network_predicate.go b/pkg/scheduler/algorithm/predicates/network_predicate.go index d942009b57..be9c86be34 100644 --- a/pkg/scheduler/algorithm/predicates/network_predicate.go +++ b/pkg/scheduler/algorithm/predicates/network_predicate.go @@ -30,6 +30,7 @@ import ( "yunion.io/x/onecloud/pkg/scheduler/algorithm/plugin" "yunion.io/x/onecloud/pkg/scheduler/api" "yunion.io/x/onecloud/pkg/scheduler/core" + schedmodels "yunion.io/x/onecloud/pkg/scheduler/models" ) // NetworkPredicate will filter the current network information with @@ -315,7 +316,7 @@ func (p *NetworkPredicate) Execute(ctx context.Context, u *core.Unit, c core.Can getFreePort := func(id string) int { if _, ok := p.networkFreePortCount[id]; ok { - return p.networkFreePortCount[id] - c.Getter().GetPendingUsage().NetUsage.Get(id) + return p.networkFreePortCount[id] - schedmodels.HostPendingUsageManager.GetNetPendingUsage(id) } return c.Getter().GetFreePort(id) } diff --git a/pkg/scheduler/cache/candidate/hosts.go b/pkg/scheduler/cache/candidate/hosts.go index 65ccc98e7f..c0c2ff494c 100644 --- a/pkg/scheduler/cache/candidate/hosts.go +++ b/pkg/scheduler/cache/candidate/hosts.go @@ -35,6 +35,7 @@ import ( "yunion.io/x/onecloud/pkg/compute/baremetal" computemodels "yunion.io/x/onecloud/pkg/compute/models" "yunion.io/x/onecloud/pkg/scheduler/core" + schedmodels "yunion.io/x/onecloud/pkg/scheduler/models" o "yunion.io/x/onecloud/pkg/scheduler/options" "yunion.io/x/onecloud/pkg/util/cgrouputils/cpuset" ) @@ -792,7 +793,7 @@ func (h *HostDesc) freeStorageSizeOfType(storageType string, mediumType string, func (h *HostDesc) GetFreePort(netId string) int { freeCnt := h.BaseHostDesc.GetFreePort(netId) - return freeCnt - h.GetPendingUsage().NetUsage.Get(netId) + return freeCnt - schedmodels.HostPendingUsageManager.GetNetPendingUsage(netId) } func reservedResourceCal( diff --git a/pkg/scheduler/models/pending_usage.go b/pkg/scheduler/models/pending_usage.go index 8fedfd1e74..9060ffb708 100644 --- a/pkg/scheduler/models/pending_usage.go +++ b/pkg/scheduler/models/pending_usage.go @@ -59,6 +59,10 @@ func (m *SHostPendingUsageManager) GetPendingUsage(hostId string) (*SPendingUsag return m.getPendingUsage(hostId) } +func (m *SHostPendingUsageManager) GetNetPendingUsage(netId string) int { + return m.store.GetNetPendingUsage(netId) +} + func (m *SHostPendingUsageManager) getPendingUsage(hostId string) (*SPendingUsage, error) { pending, err := m.store.GetPendingUsage(hostId) if err != nil { @@ -173,6 +177,18 @@ func (self *SHostMemoryPendingUsageStore) DeleteSessionUsage(usage *SessionPendi self.store.Delete(self.sessionUsageKey(usage.SessionId, usage.Usage.HostId)) } +func (self *SHostMemoryPendingUsageStore) GetNetPendingUsage(id string) int { + total := 0 + self.store.Range(func(hostId, usageObj interface{}) bool { + usage, ok := usageObj.(*SPendingUsage) + if ok { + total += usage.NetUsage.Get(id) + } + return true + }) + return total +} + type SessionPendingUsage struct { HostId string SessionId string @@ -354,13 +370,27 @@ func NewPendingUsageBySchedInfo(hostId string, req *api.SchedInfo, candidate *sc u.DiskUsage.Set(backend, osize+size) } - for _, net := range req.Networks { - id := net.Network - if id == "" { - continue + if candidate != nil && len(candidate.Nets) > 0 { + for _, net := range candidate.Nets { + // 只对建议 network_id 为1个的时候设置 pending_usage + // 多个的情况下只有交给 region 那边自己判断 + // 这里只是尽让调度器提前判断出子网是否空闲 ip + if len(net.NetworkIds) != 1 { + continue + } + id := net.NetworkIds[0] + ocount := u.NetUsage.Get(id) + u.NetUsage.Set(id, ocount+1) + } + } else { + for _, net := range req.Networks { + id := net.Network + if id == "" { + continue + } + ocount := u.NetUsage.Get(id) + u.NetUsage.Set(id, ocount+1) } - ocount := u.NetUsage.Get(id) - u.NetUsage.Set(id, ocount+1) } // group add