priorities.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 priorities
  15. import (
  16. "yunion.io/x/onecloud/pkg/scheduler/core"
  17. "yunion.io/x/onecloud/pkg/scheduler/core/score"
  18. )
  19. // PriorityHelper is a struct that as a base interface for all priorities.
  20. type PriorityHelper struct {
  21. priority core.Priority
  22. unit *core.Unit
  23. Candidate core.Candidater
  24. score score.SScore
  25. err error
  26. }
  27. func NewPriorityHelper(p core.Priority, u *core.Unit, c core.Candidater) *PriorityHelper {
  28. return &PriorityHelper{
  29. priority: p,
  30. unit: u,
  31. Candidate: c,
  32. }
  33. }
  34. func (h *PriorityHelper) setRawScore(val int) score.SScore {
  35. h.score = score.NewScore(
  36. score.TScore(val),
  37. h.priority.Name())
  38. return h.score
  39. }
  40. func (h *PriorityHelper) SetScore(val int) {
  41. h.setRawScore(val)
  42. h.unit.SetScore(h.Candidate.IndexKey(), h.score)
  43. }
  44. func (h *PriorityHelper) SetPreferScore(val int) {
  45. h.setRawScore(val)
  46. h.unit.SetPreferScore(h.Candidate.IndexKey(), h.score)
  47. }
  48. func (h *PriorityHelper) SetAvoidScore(val int) {
  49. h.setRawScore(val)
  50. h.unit.SetAvoidScore(h.Candidate.IndexKey(), h.score)
  51. }
  52. func (h *PriorityHelper) SetError(err error) {
  53. h.err = err
  54. }
  55. func (h *PriorityHelper) GetResult() (core.HostPriority, error) {
  56. return core.HostPriority{
  57. Host: h.Candidate.IndexKey(),
  58. Candidate: h.Candidate,
  59. }, h.err
  60. }
  61. // BasePriority is a default struct for priority that all the priorities
  62. // will contain it and implement its PreExecute(),Map(),Reduce() and
  63. // Name() methods.
  64. type BasePriority struct {
  65. }
  66. func (b *BasePriority) PreExecute(u *core.Unit, cs []core.Candidater) (bool, []core.PredicateFailureReason, error) {
  67. return true, nil, nil
  68. }
  69. func (b *BasePriority) Map(u *core.Unit, c core.Candidater) (core.HostPriority, error) {
  70. return core.HostPriority{}, nil
  71. }
  72. func (b *BasePriority) Reduce(u *core.Unit, cs []core.Candidater, result core.HostPriorityList) error {
  73. return nil
  74. }
  75. func (b *BasePriority) Name() string {
  76. return "base_priorites_should_not_be_called"
  77. }
  78. func (b *BasePriority) ScoreIntervals() score.Intervals {
  79. return score.NewIntervals(0, 1, 2)
  80. }