fix(scheduler): get candidates error more info

This commit is contained in:
Zexi Li
2020-12-14 01:34:00 +08:00
parent 408e768a62
commit 7d836f77a6
7 changed files with 113 additions and 86 deletions
+7 -9
View File
@@ -123,9 +123,10 @@ func init() {
})
type SchedulerHistoryListOptions struct {
Limit int `default:"50" help:"Page limit"`
Offset int `default:"0" help:"Page offset"`
All bool `help:"Show all histories, including scheduler-test"`
Limit int `default:"50" help:"Page limit"`
Offset int `default:"0" help:"Page offset"`
All bool `help:"Show all histories, including scheduler-test"`
IsSuggestion bool `help:"Only show forcast suggestion history"`
}
R(&SchedulerHistoryListOptions{}, "scheduler-history-list", "Show scheduler history list",
func(s *mcclient.ClientSession, args *SchedulerHistoryListOptions) error {
@@ -138,17 +139,14 @@ func init() {
if args.Offset > 0 {
params.Add(jsonutils.NewInt(int64(args.Offset)), "offset")
}
if args.All {
params.Add(jsonutils.JSONTrue, "all")
} else {
params.Add(jsonutils.JSONFalse, "all")
}
params.Add(jsonutils.NewBool(args.All), "all")
params.Add(jsonutils.NewBool(args.IsSuggestion), "is_suggestion")
result, err := modules.SchedManager.HistoryList(s, params)
if err != nil {
return err
}
printList(modulebase.JSON2ListResult(result), []string{
"session_id", "time", "status", "consuming",
"session_id", "time", "status", "consuming", "is_suggestion",
})
return nil
})
+8 -3
View File
@@ -21,9 +21,10 @@ import (
)
type HistoryArgs struct {
Offset int64
Limit int64
All bool
Offset int64
Limit int64
All bool
IsSuggestion bool
}
type HistoryItem struct {
@@ -69,6 +70,10 @@ func NewHistoryArgs(sjson *simplejson.Json) (*HistoryArgs, error) {
args.All = all.MustBool()
}
if isSuggestion, ok := sjson.CheckGet("is_suggestion"); ok {
args.IsSuggestion = isSuggestion.MustBool()
}
return args, nil
}
+5
View File
@@ -91,6 +91,11 @@ func FetchSchedInfo(req *http.Request) (*SchedInfo, error) {
if net.Domain == "" {
net.Domain = domainId
}
netObj, err := models.NetworkManager.FetchByIdOrName(data.UserCred, net.Network)
if err != nil {
return nil, errors.Wrapf(err, "fetch network %s", net.Network)
}
net.Network = netObj.GetId()
}
if data.InstanceGroupIds == nil || len(data.InstanceGroupIds) == 0 {
@@ -18,6 +18,7 @@ import (
"fmt"
"time"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/utils"
"yunion.io/x/onecloud/pkg/scheduler/cache"
@@ -195,12 +196,12 @@ type CandidateManager struct {
func (cm *CandidateManager) GetCandidates(args CandidateGetArgs) ([]core.Candidater, error) {
impl, err := cm.getImpl(args.ResType)
if err != nil {
return nil, err
return nil, errors.Wrapf(err, "GetCandidates implement by resource type %s", args.ResType)
}
candidates, err2 := impl.GetCandidates()
if err2 != nil {
return nil, err2
candidates, err := impl.GetCandidates()
if err != nil {
return nil, errors.Wrapf(err, "GetCandidates from implement")
}
result := []core.Candidater{}
+6 -54
View File
@@ -17,13 +17,11 @@ package manager
import (
"encoding/json"
"fmt"
"strings"
"time"
"k8s.io/client-go/kubernetes"
"yunion.io/x/log"
"yunion.io/x/pkg/utils"
"yunion.io/x/onecloud/pkg/scheduler/api"
"yunion.io/x/onecloud/pkg/scheduler/cache/candidate"
@@ -355,17 +353,18 @@ func Cleanup(cleanupArgs *api.CleanupArgs) (*api.CleanupResult, error) {
return r, nil
}
func GetHistoryList(historyArgs *api.HistoryArgs) (*api.HistoryResult, error) {
offset, limit, all := historyArgs.Offset, historyArgs.Limit, historyArgs.All
func GetHistoryList(args *api.HistoryArgs) (*api.HistoryResult, error) {
offset, limit, all, isSuggestion := args.Offset, args.Limit, args.All, args.IsSuggestion
if limit == int64(0) {
limit = int64(50)
}
historyItems, total := schedManager.HistoryManager.GetHistoryList(offset, limit, all)
historyItems, total := schedManager.HistoryManager.GetHistoryList(offset, limit, all, isSuggestion)
items := []*api.HistoryItem{}
for _, hi := range historyItems {
items = append(items, newHistoryItem(hi))
for idx := range historyItems {
hi := historyItems[idx]
items = append(items, hi.ToAPI())
}
return &api.HistoryResult{
@@ -376,53 +375,6 @@ func GetHistoryList(historyArgs *api.HistoryArgs) (*api.HistoryResult, error) {
}, nil
}
func newHistoryItem(historyItem *HistoryItem) *api.HistoryItem {
task := historyItem.Task
schedInfo := task.SchedInfo
tenants := []string{}
forGuests := []string{}
countDict := make(map[string]int64)
data := schedInfo
tenants = append(tenants, data.Project)
for _, forGuest := range data.ForGuests {
//forGuests = append(forGuests, fmt.Sprintf("%v(%v)", forGuest.ID, forGuest.Name))
forGuests = append(forGuests, fmt.Sprintf("%v", forGuest))
}
guestType := data.Hypervisor
if c, ok := countDict[guestType]; !ok {
countDict[guestType] = int64(data.Count)
} else {
countDict[guestType] = c + int64(data.Count)
}
counts := []string{}
for guestType, count := range countDict {
s := ""
if count > 1 {
s = "s"
}
counts = append(counts, fmt.Sprintf("%v %v%v", count, guestType, s))
}
countStr := strings.Join(counts, ", ")
return &api.HistoryItem{
Time: historyItem.Time.Local().Format("2006-01-02 15:04:05"),
Consuming: fmt.Sprintf("%s", task.Consuming),
SessionID: task.GetSessionID(),
Status: task.GetStatus(),
Tenants: utils.Distinct(tenants),
Guests: forGuests,
Count: countStr,
IsSuggestion: schedInfo.IsSuggestion,
}
}
func GetHistoryDetail(historyDetailArgs *api.HistoryDetailArgs) (*api.HistoryDetailResult, error) {
historyItem := schedManager.HistoryManager.GetHistory(historyDetailArgs.ID)
if historyItem == nil {
+11
View File
@@ -15,6 +15,9 @@
package manager
import (
"yunion.io/x/jsonutils"
"yunion.io/x/pkg/errors"
"yunion.io/x/onecloud/pkg/scheduler/api"
"yunion.io/x/onecloud/pkg/scheduler/core"
"yunion.io/x/onecloud/pkg/scheduler/data_manager"
@@ -35,6 +38,9 @@ func candidatesByProvider(provider CandidatesProvider, schedData *api.SchedInfo)
candidateManager := provider.CandidateManager()
if len(schedData.PreferCandidates) >= schedData.RequiredCandidates {
hosts, err = candidateManager.GetCandidatesByIds(provider.CandidateType(), schedData.PreferCandidates)
if err != nil {
err = errors.Wrapf(err, "GetCandidatesByIds %v", schedData.PreferCandidates)
}
} else {
args := data_manager.CandidateGetArgs{
ResType: provider.CandidateType(),
@@ -44,6 +50,11 @@ func candidatesByProvider(provider CandidatesProvider, schedData *api.SchedInfo)
HostTypes: schedData.GetCandidateHostTypes(),
}
hosts, err = candidateManager.GetCandidates(args)
if err != nil {
err = errors.Wrapf(err, "GetCandidates by args %s", jsonutils.Marshal(args))
} else if len(hosts) == 0 {
err = errors.Errorf("Scheduler not found candidates by args %s", jsonutils.Marshal(args))
}
}
if err != nil {
return nil, err
+71 -16
View File
@@ -16,6 +16,8 @@ package manager
import (
"container/list"
"fmt"
"strings"
"sync"
"time"
@@ -23,6 +25,7 @@ import (
"yunion.io/x/pkg/util/wait"
u "yunion.io/x/pkg/utils"
"yunion.io/x/onecloud/pkg/scheduler/api"
"yunion.io/x/onecloud/pkg/scheduler/models"
o "yunion.io/x/onecloud/pkg/scheduler/options"
)
@@ -39,6 +42,50 @@ func NewHistoryItem(task *Task) *HistoryItem {
}
}
func (h *HistoryItem) ToAPI() *api.HistoryItem {
task := h.Task
schedInfo := task.SchedInfo
tenants := []string{}
forGuests := []string{}
countDict := make(map[string]int64)
tenants = append(tenants, schedInfo.Project)
for _, forGuest := range schedInfo.ForGuests {
//forGuests = append(forGuests, fmt.Sprintf("%v(%v)", forGuest.ID, forGuest.Name))
forGuests = append(forGuests, fmt.Sprintf("%v", forGuest))
}
guestType := schedInfo.Hypervisor
if c, ok := countDict[guestType]; !ok {
countDict[guestType] = int64(schedInfo.Count)
} else {
countDict[guestType] = c + int64(schedInfo.Count)
}
counts := []string{}
for guestType, count := range countDict {
s := ""
if count > 1 {
s = "s"
}
counts = append(counts, fmt.Sprintf("%v %v%v", count, guestType, s))
}
countStr := strings.Join(counts, ", ")
return &api.HistoryItem{
Time: h.Time.Local().Format("2006-01-02 15:04:05"),
Consuming: fmt.Sprintf("%s", task.Consuming),
SessionID: task.GetSessionID(),
Status: task.GetStatus(),
Tenants: u.Distinct(tenants),
Guests: forGuests,
Count: countStr,
IsSuggestion: schedInfo.IsSuggestion,
}
}
func (h *HistoryItem) ToMap() map[string]string {
ret := make(map[string]string)
ret["SessionID"] = h.Task.GetSessionID()
@@ -116,38 +163,46 @@ func (m *HistoryManager) Run() {
go wait.Until(m.cleanHistoryMap, u.ToDuration(o.GetOptions().SchedulerHistoryCleanPeriod), m.stopCh)
}
func (m *HistoryManager) GetHistoryList(offset int64, limit int64, all bool) ([]*HistoryItem, int64) {
func (m *HistoryManager) GetHistoryList(offset int64, limit int64, all bool, isSuggestion bool) ([]*HistoryItem, int64) {
m.lock.Lock()
defer m.lock.Unlock()
var hList *list.List
if all {
if all || isSuggestion {
hList = m.historyList
} else {
hList = m.normalHistoryList
}
total := int64(hList.Len())
historyItems := []*HistoryItem{}
element := hList.Front()
for index := int64(0); index < offset; index++ {
if element != nil {
element = element.Next()
} else {
return historyItems, total
for idx := 0; idx < hList.Len(); idx++ {
item := element.Value.(*HistoryItem)
if isSuggestion {
if !item.IsSuggestion() {
element = element.Next()
continue
}
}
historyItems = append(historyItems, item)
element = element.Next()
}
for index := int64(0); index < limit; index++ {
if element != nil {
historyItems = append(historyItems, element.Value.(*HistoryItem))
element = element.Next()
} else {
break
}
total := len(historyItems)
ret := make([]*HistoryItem, 0)
if offset <= int64(total) {
historyItems = historyItems[offset:]
} else {
return ret, int64(total)
}
return historyItems, total
for index := 0; int64(index) < limit && index < len(historyItems); index++ {
ret = append(ret, historyItems[index])
}
return historyItems, int64(total)
}
func (m *HistoryManager) GetHistory(sessionId string) *HistoryItem {