change config add scheduler check

This commit is contained in:
wanyaoqi
2019-03-05 15:02:21 +08:00
parent 766a8d30ff
commit 9963a5f2a1
4 changed files with 45 additions and 0 deletions
+2
View File
@@ -405,6 +405,8 @@ func (self *SKVMGuestDriver) OnGuestChangeCpuMemFailed(ctx context.Context, gues
fmt.Sprintf("Change config task failed but added cpu count %d", cpuAdded), task.GetUserCred())
logclient.AddActionLogWithContext(ctx, guest, logclient.ACT_VM_CHANGE_FLAVOR,
fmt.Sprintf("Change config task failed but added cpu count %d", cpuAdded), task.GetUserCred(), false)
models.HostManager.ClearSchedDescCache(guest.HostId)
}
return nil
}
+27
View File
@@ -28,6 +28,8 @@ import (
"yunion.io/x/onecloud/pkg/compute/options"
"yunion.io/x/onecloud/pkg/httperrors"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/mcclient/auth"
"yunion.io/x/onecloud/pkg/mcclient/modules"
"yunion.io/x/onecloud/pkg/mcclient/modules/notify"
"yunion.io/x/onecloud/pkg/util/httputils"
"yunion.io/x/onecloud/pkg/util/logclient"
@@ -1757,6 +1759,17 @@ func (self *SGuest) PerformChangeConfig(ctx context.Context, userCred mcclient.T
confs.Set("guest_online", jsonutils.JSONTrue)
}
// schedulr forecast
schedDesc := self.confToSchedDesc(addCpu, addMem, addDisk)
s := auth.GetAdminSession(ctx, options.Options.Region, "")
canChangeConf, err := modules.SchedManager.DoScheduleForecast(s, schedDesc, 1)
if err != nil {
return nil, err
}
if !canChangeConf {
return nil, httperrors.NewBadRequestError("Host resource is not enough")
}
log.Debugf("%s", confs.String())
pendingUsage := &SQuota{}
@@ -1787,6 +1800,20 @@ func (self *SGuest) PerformChangeConfig(ctx context.Context, userCred mcclient.T
return nil, nil
}
func (self *SGuest) confToSchedDesc(addCpu, addMem, addDisk int) *jsonutils.JSONDict {
desc := jsonutils.NewDict()
desc.Set("vmem_size", jsonutils.NewInt(int64(addMem)))
desc.Set("vcpu_count", jsonutils.NewInt(int64(addCpu)))
guestDisks := self.GetDisks()
diskInfo := guestDisks[0].ToDiskInfo()
diskInfo.Size = int64(addDisk)
desc.Set("disk.0", jsonutils.Marshal(diskInfo))
desc.Set("prefer_host_id", jsonutils.NewString(self.HostId))
desc.Set("hypervisor", jsonutils.NewString(self.GetHypervisor()))
desc.Set("owner_tenant_id", jsonutils.NewString(self.ProjectId))
return desc
}
func (self *SGuest) StartChangeConfigTask(ctx context.Context, userCred mcclient.TokenCredential,
data *jsonutils.JSONDict, parentTaskId string, pendingUsage quotas.IQuota) error {
self.SetStatus(userCred, VM_CHANGE_FLAVOR, "")
@@ -204,6 +204,7 @@ func (self *GuestChangeConfigTask) OnGuestChangeCpuMemSpecCompleteFailed(ctx con
}
func (self *GuestChangeConfigTask) OnGuestChangeCpuMemSpecFinish(ctx context.Context, guest *models.SGuest) {
models.HostManager.ClearSchedDescCache(guest.HostId)
self.SetStage("OnSyncConfigComplete", nil)
err := guest.StartSyncTask(ctx, self.UserCred, false, self.GetTaskId())
if err != nil {
+15
View File
@@ -57,6 +57,21 @@ func (this *SchedulerManager) DoSchedule(s *mcclient.ClientSession, params jsonu
return candidates, nil
}
func (this *SchedulerManager) DoScheduleForecast(s *mcclient.ClientSession, params jsonutils.JSONObject, count int) (bool, error) {
body := jsonutils.NewDict()
body.Add(params, this.Keyword)
if count <= 0 {
count = 1
}
body.Add(jsonutils.NewInt(int64(count)), "count")
res, err := this.DoForecast(s, body)
if err != nil {
return false, err
}
canCreate := jsonutils.QueryBoolean(res, "can_create", false)
return canCreate, nil
}
func newSchedURL(action string) string {
return fmt.Sprintf("/scheduler/%s", action)
}