scheduler: forecast api

This commit is contained in:
Zexi Li
2019-03-01 20:08:07 +08:00
parent 9efe501319
commit af4a3360f0
3 changed files with 93 additions and 0 deletions
+11
View File
@@ -900,3 +900,14 @@ type SchedTestResult struct {
Limit int64 `json:"limit"`
Offset int64 `json:"offset"`
}
type ForecastInfo struct {
Filter string `json:"filter"`
Message string `json:"message"`
Count int64 `json:"count"`
}
type SchedForecastResult struct {
CanCreate bool `json:"can_create"`
Infos []ForecastInfo `json:"infos"`
}
+53
View File
@@ -0,0 +1,53 @@
package handler
import (
"yunion.io/x/onecloud/pkg/scheduler/api"
"yunion.io/x/onecloud/pkg/scheduler/core"
)
func isSelectedHost(item *core.SchedResultItem) {
if item.Count > 0 {
return true
}
return false
}
func transToSchedForecastResult(result *core.SchedResultItemList) interface{} {
unit := result.Unit
reqCount := unit.SchedData().Count
readyCount := 0
infos := make([]api.ForecastInfo, 0)
infoMap := make(map[string]*ForecastInfo)
getOrNewInfo := func(preName string) *ForecastInfo {
if info, ok := infoMap[preName]; !ok {
i := &ForecastInfo{
Filter: preName,
Count: 1,
}
infoMap[preName] = true
return i
} else {
return info
}
}
addInfos := func(capaDetail map[string]int64) {
for preName, cnt := range capaDetail {
if cnt <= 0 {
getOrNewInfo(preName)
}
}
}
for _, item := range result.Data {
readyCount += item.Count
}
canCreate := true
if readyCount < reqCount {
canCreate = false
}
return &api.SchedForecastResult{
CanCreate: false,
Infos: nil,
}
}
+29
View File
@@ -50,6 +50,8 @@ func schedulerActionHandler(c *gin.Context) {
switch act {
case "test":
doSchedulerTest(c)
case "forecast":
doSchedulerForecast(c)
case "candidate-list":
doCandidateList(c)
case "cleanup":
@@ -117,6 +119,33 @@ func transToSchedTestResult(result *core.SchedResultItemList, limit int64) inter
}
}
func doSchedulerForecast(c *gin.Context) {
if !schedman.IsReady() {
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("Global scheduler not init"))
return
}
sjson, err := simplejson.NewFromReader(c.Request.Body)
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
schedInfo, err := api.NewSchedInfo(sjson, true)
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
schedInfo.IsSuggestion = true
schedInfo.ShowSuggestionDetails = true
result, err := schedman.Schedule(schedInfo)
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
c.JSON(http.StatusOK, transToSchedForecastResult(result, schedInfo))
}
func doCandidateList(c *gin.Context) {
sjson, err := simplejson.NewFromReader(c.Request.Body)
if err != nil {