result_helper.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 core
  15. import (
  16. "fmt"
  17. "yunion.io/x/log"
  18. schedapi "yunion.io/x/onecloud/pkg/apis/scheduler"
  19. "yunion.io/x/onecloud/pkg/scheduler/api"
  20. )
  21. func ResultHelp(result *SchedResultItemList, schedInfo *api.SchedInfo) *ScheduleResult {
  22. out := new(ScheduleResult)
  23. out.Result = transToSchedResult(result, schedInfo)
  24. return out
  25. }
  26. func ResultHelpForForcast(result *SchedResultItemList, _ *api.SchedInfo) *ScheduleResult {
  27. out := new(ScheduleResult)
  28. out.ForecastResult = transToSchedForecastResult(result)
  29. return out
  30. }
  31. func ResultHelpForTest(result *SchedResultItemList, schedInfo *api.SchedInfo) *ScheduleResult {
  32. out := new(ScheduleResult)
  33. out.TestResult = transToSchedTestResult(result, schedInfo.SuggestionLimit)
  34. return out
  35. }
  36. type IResultHelper interface {
  37. ResultHelp(result *SchedResultItemList, schedInfo *api.SchedInfo) *ScheduleResult
  38. }
  39. // ResultHelperFunc type is an adapter to allwo the use of ordinary functions as a ResultHelper.
  40. // If f is a function with the appropriate signature, ResultHelperFunc(f) is a ResultHelper that calls f.
  41. type SResultHelperFunc func(result *SchedResultItemList, schedInfo *api.SchedInfo) *ScheduleResult
  42. func (r SResultHelperFunc) ResultHelp(result *SchedResultItemList, schedInfo *api.SchedInfo) *ScheduleResult {
  43. return r(result, schedInfo)
  44. }
  45. func transToSchedResult(result *SchedResultItemList, schedInfo *api.SchedInfo) *schedapi.ScheduleOutput {
  46. if schedInfo.Backup || len(schedInfo.InstanceGroupsDetail) > 0 {
  47. return transToInstanceGroupSchedResult(result, schedInfo)
  48. } else {
  49. return transToRegionSchedResult(result.Data, int64(schedInfo.Count), schedInfo.SessionId)
  50. }
  51. }
  52. // trans to region sched results
  53. func transToRegionSchedResult(result SchedResultItems, count int64, sid string) *schedapi.ScheduleOutput {
  54. apiResults := make([]*schedapi.CandidateResource, 0)
  55. succCount := 0
  56. storageUsed := NewStorageUsed()
  57. for _, nr := range result {
  58. for {
  59. if nr.Count <= 0 {
  60. break
  61. }
  62. tr := nr.ToCandidateResource(storageUsed)
  63. tr.SessionId = sid
  64. apiResults = append(apiResults, tr)
  65. nr.Count--
  66. succCount++
  67. }
  68. }
  69. for {
  70. if int64(succCount) >= count {
  71. break
  72. }
  73. er := &schedapi.CandidateResource{Error: "Out of resource"}
  74. apiResults = append(apiResults, er)
  75. succCount++
  76. }
  77. return &schedapi.ScheduleOutput{
  78. Candidates: apiResults,
  79. }
  80. }
  81. func hostInResultItemsIndex(hostId string, hosts SchedResultItems) int {
  82. for i := 0; i < len(hosts); i++ {
  83. if hosts[i].ID == hostId {
  84. return i
  85. }
  86. }
  87. return -1
  88. }
  89. func transToSchedTestResult(result *SchedResultItemList, limit int64) interface{} {
  90. return &api.SchedTestResult{
  91. Data: result.Data,
  92. Total: int64(result.Data.Len()),
  93. Limit: limit,
  94. Offset: 0,
  95. }
  96. }
  97. func transToSchedForecastResult(result *SchedResultItemList) *api.SchedForecastResult {
  98. unit := result.Unit
  99. schedData := unit.SchedData()
  100. filteredCandidates := make([]api.FilteredCandidate, 0)
  101. ret := &api.SchedForecastResult{
  102. ReqCount: int64(schedData.Count),
  103. }
  104. // build filteredCandidates
  105. failedLogs := unit.LogManager.FailedLogs()
  106. logIndex := func(item *SchedResultItem) string {
  107. getter := item.Candidater.Getter()
  108. name := getter.Name()
  109. id := getter.Id()
  110. return fmt.Sprintf("%s:%s", name, id)
  111. }
  112. for _, item := range result.Data {
  113. if item.Capacity > 0 {
  114. continue
  115. }
  116. filteredCandidate := api.FilteredCandidate{
  117. ID: item.ID,
  118. Name: item.Name,
  119. }
  120. for preName, capa := range item.CapacityDetails {
  121. if capa > 0 {
  122. continue
  123. }
  124. filteredCandidate.FilterName = preName
  125. }
  126. failedLog := failedLogs.Get(logIndex(item))
  127. if failedLog == nil {
  128. log.Errorf("candidate %q capacity is 0 but no failedLog found", item.Name)
  129. continue
  130. }
  131. for _, msg := range failedLog.Messages {
  132. filteredCandidate.Reasons = append(filteredCandidate.Reasons, msg.Info)
  133. }
  134. filteredCandidates = append(filteredCandidates, filteredCandidate)
  135. }
  136. ret.FilteredCandidates = filteredCandidates
  137. var (
  138. output = transToSchedResult(result, schedData)
  139. readyCount int64
  140. )
  141. for _, candi := range output.Candidates {
  142. if len(candi.Error) == 0 {
  143. readyCount++
  144. ret.Candidates = append(ret.Candidates, candi)
  145. continue
  146. }
  147. ret.NotAllowReasons = append(ret.NotAllowReasons, candi.Error)
  148. }
  149. ret.AllowCount = readyCount
  150. if ret.AllowCount < ret.ReqCount {
  151. ret.CanCreate = false
  152. } else {
  153. ret.CanCreate = true
  154. }
  155. return ret
  156. }