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