group_predicate.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package guest
  15. /*import (
  16. "fmt"
  17. "yunion.io/x/onecloud/pkg/scheduler/algorithm/plugin"
  18. "yunion.io/x/onecloud/pkg/scheduler/algorithm/predicates"
  19. "yunion.io/x/onecloud/pkg/scheduler/core"
  20. "yunion.io/x/onecloud/pkg/scheduler/core/score"
  21. )
  22. // GroupPredicate filter the packet based on the label information,
  23. // the same group of guests should avoid schedule on same host.
  24. type GroupPredicate struct {
  25. predicates.BasePredicate
  26. plugin.BasePlugin
  27. ExcludeGroups []string
  28. RequireGroups []string
  29. AvoidGroups []string
  30. PreferGroups []string
  31. }
  32. func (p *GroupPredicate) Name() string {
  33. return "host_group"
  34. }
  35. func (p *GroupPredicate) Clone() core.FitPredicate {
  36. return &GroupPredicate{}
  37. }
  38. func (p *GroupPredicate) PreExecute(u *core.Unit, cs []core.Candidater) (bool, error) {
  39. d := u.SchedData()
  40. if len(d.GroupRelations) == 0 {
  41. return false, nil
  42. }
  43. for _, r := range d.GroupRelations {
  44. if r.Strategy == "exclude" {
  45. p.ExcludeGroups = append(p.ExcludeGroups, r.GroupId)
  46. } else if r.Strategy == "require" {
  47. p.RequireGroups = append(p.RequireGroups, r.GroupId)
  48. } else if r.Strategy == "avoid" {
  49. p.AvoidGroups = append(p.AvoidGroups, r.GroupId)
  50. } else if r.Strategy == "prefer" {
  51. p.PreferGroups = append(p.PreferGroups, r.GroupId)
  52. }
  53. }
  54. u.AppendSelectPlugin(p)
  55. return true, nil
  56. }
  57. func (p *GroupPredicate) Execute(u *core.Unit, c core.Candidater) (bool, []core.PredicateFailureReason, error) {
  58. h := predicates.NewPredicateHelper(p, u, c)
  59. g, err := h.GetGroupCounts()
  60. if err != nil {
  61. return false, nil, err
  62. }
  63. if len(p.ExcludeGroups) > 0 {
  64. for _, groupId := range p.ExcludeGroups {
  65. if g.ExistsGroup(groupId) {
  66. h.Exclude(fmt.Sprintf("exclude by %v:exclude", groupId))
  67. break
  68. }
  69. }
  70. } else if len(p.RequireGroups) > 0 {
  71. for _, groupId := range p.RequireGroups {
  72. if !g.ExistsGroup(groupId) {
  73. h.Exclude(fmt.Sprintf("exclude by %v:require", groupId))
  74. break
  75. }
  76. }
  77. }
  78. return h.GetResult()
  79. }
  80. func (p *GroupPredicate) OnPriorityEnd(u *core.Unit, c core.Candidater) {
  81. if len(p.AvoidGroups) > 0 {
  82. u.SetFrontScore(
  83. c.IndexKey(),
  84. score.NewScore(
  85. score.TScore(-core.PriorityStep*len(p.AvoidGroups)),
  86. p.Name()+":avoid",
  87. ))
  88. }
  89. if len(p.PreferGroups) > 0 {
  90. u.SetFrontScore(
  91. c.IndexKey(),
  92. score.NewScore(
  93. score.TScore(core.PriorityStep*len(p.PreferGroups)),
  94. p.Name()+":prefer",
  95. ))
  96. }
  97. }*/