Files
cloudpods/pkg/scheduler/algorithm/predicates/instance_group_predicate.go
郑雨 b59a145184 fix: Fix that fail to create vm with unforced instance group
1. This is a bug because of latest modify about scheduler's instance_group_predicate.
Should set minFree as zero when founding that host can't be used to create vm
with requested forced instance group.
2. Fix some little error of instance group.
3. Add PerformEnable and PerformDisable for instance group.
4. Clean scheduler desc cache after having attached or detached guests and groups
and enabled group. Cleaning was necessary when disable group because disabled
group is prohibited.
5. Add package 'golang.org/x/sync/errgroup'.
2019-11-07 15:35:51 +08:00

157 lines
4.4 KiB
Go

// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package predicates
import (
"fmt"
"math"
"yunion.io/x/onecloud/pkg/scheduler/core"
)
type InstanceGroupPredicate struct {
BasePredicate
}
func (p *InstanceGroupPredicate) Name() string {
return "instance_group"
}
func (p *InstanceGroupPredicate) Clone() core.FitPredicate {
return &InstanceGroupPredicate{}
}
func (p *InstanceGroupPredicate) PreExecute(u *core.Unit, cs []core.Candidater) (bool, error) {
schedDate := u.SchedData()
if schedDate.InstanceGroupIds == nil || len(schedDate.InstanceGroupIds) == 0 {
return false, nil
}
return true, nil
}
func (p *InstanceGroupPredicate) Execute(u *core.Unit, c core.Candidater) (bool, []core.PredicateFailureReason, error) {
return true, nil, nil
}
type SForcedGroupPredicate struct {
InstanceGroupPredicate
}
func (p *SForcedGroupPredicate) Name() string {
return "forced_instance_group"
}
func (p *SForcedGroupPredicate) Clone() core.FitPredicate {
return &SForcedGroupPredicate{}
}
// SForcedGroupPredicate make sure that there is no more guest with same group whose IsForcedSpe is ture in a host
// for all forced groups in u.SchedData.InstanceGroupIds, so that the capacity is the min value of the FreeGroupCounts
func (p *SForcedGroupPredicate) Execute(u *core.Unit, c core.Candidater) (bool, []core.PredicateFailureReason, error) {
h := NewPredicateHelper(p, u, c)
schedDate := u.SchedData()
instanceGroups := c.Getter().InstanceGroups()
minFree := math.MaxInt32
for _, id := range schedDate.InstanceGroupIds {
detail := schedDate.InstanceGroupsDetail[id]
// SForcedGroupPredicate only deal with group whose ForceDispersion is ture
if detail.ForceDispersion.IsFalse() {
continue
}
var free int
if _, ok := instanceGroups[id]; ok {
free, _ = c.Getter().GetFreeGroupCount(id)
if free < 1 {
h.AppendPredicateFailMsg(fmt.Sprintf(
"the number of guests with same instance group '%s' in this host has reached the upper limit",
instanceGroups[id].GetName()))
minFree = 0
break
}
} else {
free = detail.Granularity
}
if free < minFree {
minFree = free
}
}
// chose the min capacity of groups
h.SetCapacity(int64(minFree))
return h.GetResult()
}
type SUnForcedGroupPredicate struct {
InstanceGroupPredicate
}
func (p *SUnForcedGroupPredicate) Name() string {
return "unforced_instance_group"
}
func (p *SUnForcedGroupPredicate) Clone() core.FitPredicate {
return &SUnForcedGroupPredicate{}
}
func (p *SUnForcedGroupPredicate) PreExecute(u *core.Unit, cs []core.Candidater) (bool, error) {
ret, err := p.InstanceGroupPredicate.PreExecute(u, cs)
if err != nil || !ret {
return ret, err
}
u.RegisterSelectPriorityUpdater(p.Name(), func(u *core.Unit, origin core.SSelectPriorityValue,
hostID string) core.SSelectPriorityValue {
return origin.SubOne()
})
return ret, err
}
// SUnForcedGroupPredicate make sure that the guests are assigned to these hosts who has enough FreeGroupCount of
// unforced groups, so that it will improve the priority of these hosts meet the conditions and the priority should
// be the max value of the FreeGroupCounts
func (p *SUnForcedGroupPredicate) Execute(u *core.Unit, c core.Candidater) (bool, []core.PredicateFailureReason,
error) {
h := NewPredicateHelper(p, u, c)
schedDate := u.SchedData()
instanceGroups := c.Getter().InstanceGroups()
maxPriority := 0
for _, id := range schedDate.InstanceGroupIds {
detail := schedDate.InstanceGroupsDetail[id]
// SUnForcedGroupPredicate only deal with group whose ForceDispersion is false
if detail.ForceDispersion.IsTrue() {
continue
}
var priority int
if _, ok := instanceGroups[id]; ok {
free, _ := c.Getter().GetFreeGroupCount(id)
if free < 1 {
priority = 0
}
priority = free
} else {
priority = detail.Granularity
}
if priority > maxPriority {
maxPriority = priority
}
}
// set priority
h.SetSelectPriority(maxPriority)
return h.GetResult()
}