mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-05-31 13:00:41 +08:00
scheduler: fix prefer avoid score
This commit is contained in:
@@ -110,16 +110,14 @@ func (p *AggregatePredicate) exec(h *PredicateHelper) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func SetCandidateScoreBySchedtag(u *core.Unit, c core.Candidater, aggCountMap map[string]int, postiveScore bool) {
|
||||
func SetCandidateScoreBySchedtag(u *core.Unit, c core.Candidater, aggCountMap map[string]int, prefer bool) {
|
||||
stepScore := core.PriorityStep
|
||||
if !postiveScore {
|
||||
stepScore = -stepScore
|
||||
doSet := u.SetPreferScore
|
||||
if !prefer {
|
||||
doSet = u.SetAvoidScore
|
||||
}
|
||||
for n, count := range aggCountMap {
|
||||
u.SetFrontScore(
|
||||
c.IndexKey(),
|
||||
score.NewScore(score.TScore(count*stepScore), n),
|
||||
)
|
||||
doSet(c.IndexKey(), score.NewScore(score.TScore(count*stepScore), n))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -83,18 +83,25 @@ func (p *SchedtagPredicate) Check(candidate ISchedtagCandidate) error {
|
||||
func GetSchedtagCount(inTags []computeapi.SchedtagConfig, objTags []models.SSchedtag, strategy string) (countMap map[string]int) {
|
||||
countMap = make(map[string]int)
|
||||
|
||||
in := func(objTag models.SSchedtag, inTags []computeapi.SchedtagConfig) bool {
|
||||
in := func(objTag models.SSchedtag, inTags []computeapi.SchedtagConfig) (bool, int) {
|
||||
for _, tag := range inTags {
|
||||
if tag.Id == objTag.Id || tag.Id == objTag.Name {
|
||||
return true
|
||||
return true, tag.Weight
|
||||
}
|
||||
}
|
||||
return false
|
||||
return false, 0
|
||||
}
|
||||
|
||||
for _, objTag := range objTags {
|
||||
if in(objTag, inTags) {
|
||||
countMap[fmt.Sprintf("%s:%s:%s", objTag.Id, objTag.Name, strategy)]++
|
||||
if ok, weight := in(objTag, inTags); ok {
|
||||
key := fmt.Sprintf("%s:%s:%s", objTag.Id, objTag.Name, strategy)
|
||||
score, ok := countMap[key]
|
||||
if ok {
|
||||
score += weight
|
||||
} else {
|
||||
score = weight
|
||||
}
|
||||
countMap[key] = score
|
||||
}
|
||||
}
|
||||
return
|
||||
@@ -116,7 +123,10 @@ func GetRequestSchedtags(reqTags []*computeapi.SchedtagConfig, allTags []models.
|
||||
|
||||
appendedTagIds := make(map[string]int)
|
||||
|
||||
appendTagByStrategy := func(tag *computeapi.SchedtagConfig) {
|
||||
appendTagByStrategy := func(tag *computeapi.SchedtagConfig, defaultWeight int) {
|
||||
if tag.Weight <= 0 {
|
||||
tag.Weight = defaultWeight
|
||||
}
|
||||
switch tag.Strategy {
|
||||
case models.STRATEGY_REQUIRE:
|
||||
requireTags = append(requireTags, *tag)
|
||||
@@ -130,7 +140,7 @@ func GetRequestSchedtags(reqTags []*computeapi.SchedtagConfig, allTags []models.
|
||||
}
|
||||
|
||||
for _, tag := range reqTags {
|
||||
appendTagByStrategy(tag)
|
||||
appendTagByStrategy(tag, 10)
|
||||
|
||||
appendedTagIds[tag.Id] = 1
|
||||
}
|
||||
@@ -141,7 +151,7 @@ func GetRequestSchedtags(reqTags []*computeapi.SchedtagConfig, allTags []models.
|
||||
|
||||
if !(nameOk || idOk) {
|
||||
apiTag := &computeapi.SchedtagConfig{Id: tag.Id, Strategy: tag.DefaultStrategy}
|
||||
appendTagByStrategy(apiTag)
|
||||
appendTagByStrategy(apiTag, 1)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ func (p *AvoidSameHostPriority) Map(u *core.Unit, c core.Candidater) (core.HostP
|
||||
|
||||
ownerTenantID := u.SchedData().Project
|
||||
if count, ok := c.Getter().ProjectGuests()[ownerTenantID]; ok && count > 0 {
|
||||
h.SetFrontRawScore(-1 * int(count))
|
||||
h.SetScore(-1 * int(count))
|
||||
}
|
||||
|
||||
return h.GetResult()
|
||||
|
||||
@@ -35,7 +35,7 @@ func (p *CapacityPriority) Map(u *core.Unit, c core.Candidater) (core.HostPriori
|
||||
h := priorities.NewPriorityHelper(p, u, c)
|
||||
|
||||
capacity := u.GetCapacity(c.IndexKey())
|
||||
h.SetRawScore(int(capacity))
|
||||
h.SetScore(int(capacity))
|
||||
|
||||
return h.GetResult()
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ func (p *CreatingPriority) Map(u *core.Unit, c core.Candidater) (core.HostPriori
|
||||
creatingGuestCount := c.Getter().CreatingGuestCount()
|
||||
if creatingGuestCount > 0 {
|
||||
score := -int(creatingGuestCount)
|
||||
h.SetFrontScore(score)
|
||||
h.SetScore(score)
|
||||
}
|
||||
|
||||
return h.GetResult()
|
||||
|
||||
@@ -36,13 +36,6 @@ func NewPriorityHelper(p core.Priority, u *core.Unit, c core.Candidater) *Priori
|
||||
}
|
||||
}
|
||||
|
||||
func (h *PriorityHelper) setIntervalScore(val int) score.SScore {
|
||||
h.score = score.NewScore(
|
||||
h.priority.ScoreIntervals().ToScore(int64(val)),
|
||||
h.priority.Name())
|
||||
return h.score
|
||||
}
|
||||
|
||||
func (h *PriorityHelper) setRawScore(val int) score.SScore {
|
||||
h.score = score.NewScore(
|
||||
score.TScore(val),
|
||||
@@ -51,23 +44,18 @@ func (h *PriorityHelper) setRawScore(val int) score.SScore {
|
||||
}
|
||||
|
||||
func (h *PriorityHelper) SetScore(val int) {
|
||||
h.setIntervalScore(val)
|
||||
h.unit.SetScore(h.Candidate.IndexKey(), h.score)
|
||||
}
|
||||
|
||||
func (h *PriorityHelper) SetFrontScore(val int) {
|
||||
h.setIntervalScore(val)
|
||||
h.unit.SetFrontScore(h.Candidate.IndexKey(), h.score)
|
||||
}
|
||||
|
||||
func (h *PriorityHelper) SetRawScore(val int) {
|
||||
h.setRawScore(val)
|
||||
h.unit.SetScore(h.Candidate.IndexKey(), h.score)
|
||||
}
|
||||
|
||||
func (h *PriorityHelper) SetFrontRawScore(val int) {
|
||||
func (h *PriorityHelper) SetPreferScore(val int) {
|
||||
h.setRawScore(val)
|
||||
h.unit.SetFrontScore(h.Candidate.IndexKey(), h.score)
|
||||
h.unit.SetPreferScore(h.Candidate.IndexKey(), h.score)
|
||||
}
|
||||
|
||||
func (h *PriorityHelper) SetAvoidScore(val int) {
|
||||
h.setRawScore(val)
|
||||
h.unit.SetAvoidScore(h.Candidate.IndexKey(), h.score)
|
||||
}
|
||||
|
||||
func (h *PriorityHelper) SetError(err error) {
|
||||
|
||||
Reference in New Issue
Block a user