Skip to content

Commit

Permalink
Merge pull request #18757 from swordqiu/hotfix/qj-hot-add-mem-for-esxi
Browse files Browse the repository at this point in the history
fix: hot add mem check for esxi vm
  • Loading branch information
zexi authored Nov 23, 2023
2 parents ba762a3 + db1f8c8 commit 51ecf05
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 12 deletions.
4 changes: 4 additions & 0 deletions pkg/apis/compute/guest_const.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,10 @@ const (
VM_METADATA_OS_VERSION = "os_version"
VM_METADATA_CGROUP_CPUSET = "cgroup_cpuset"
VM_METADATA_ENABLE_MEMCLEAN = "enable_memclean"
VM_METADATA_HOTPLUG_CPU_MEM = "hotplug_cpu_mem"
VM_METADATA_HOT_REMOVE_NIC = "hot_remove_nic"
VM_METADATA_START_VMEM_MB = "start_vmem_mb"
VM_METADATA_START_VCPU_COUNT = "start_vcpu_count"
)

func Hypervisors2HostTypes(hypervisors []string) []string {
Expand Down
2 changes: 1 addition & 1 deletion pkg/compute/guestdrivers/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func (drv *SBaseGuestDriver) IsSupportPublicIp() bool {
return false
}

func (drv *SBaseGuestDriver) NeedStopForChangeSpec(ctx context.Context, guest *models.SGuest, cpuChanged, memChanged bool) bool {
func (drv *SBaseGuestDriver) NeedStopForChangeSpec(ctx context.Context, guest *models.SGuest, addCpu int, addMemMb int) bool {
return false
}

Expand Down
21 changes: 21 additions & 0 deletions pkg/compute/guestdrivers/esxi.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"
"sort"
"strconv"
"time"

"yunion.io/x/cloudmux/pkg/cloudprovider"
Expand Down Expand Up @@ -861,3 +862,23 @@ func (drv *SESXiGuestDriver) RequestUndeployGuestOnHost(ctx context.Context, gue
})
return nil
}

func (drv *SESXiGuestDriver) NeedStopForChangeSpec(ctx context.Context, guest *models.SGuest, addCpu int, addMemMb int) bool {
// https://kb.vmware.com/s/article/2008405
startVmem := guest.VmemSize
vmemMbStr := guest.GetMetadata(ctx, api.VM_METADATA_START_VMEM_MB, nil)
if len(vmemMbStr) > 0 {
vmemMb, _ := strconv.Atoi(vmemMbStr)
if vmemMb > 0 {
startVmem = int(vmemMb)
}
}
maxAllowVmem := 16 * startVmem
if startVmem <= 3*1024 {
maxAllowVmem = 3 * 1024
}
if guest.VmemSize+addMemMb > maxAllowVmem {
return true
}
return false
}
8 changes: 3 additions & 5 deletions pkg/compute/guestdrivers/kvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,10 +425,8 @@ func (self *SKVMGuestDriver) RequestAssociateEip(ctx context.Context, userCred m
return nil
}

func (self *SKVMGuestDriver) NeedStopForChangeSpec(ctx context.Context, guest *models.SGuest, cpuChanged, memChanged bool) bool {
return guest.GetMetadata(ctx, "hotplug_cpu_mem", nil) != "enable" || apis.IsARM(guest.OsArch)
// (memChanged && guest.GetMetadata(ctx, "__hugepage", nil) == "native") ||
// apis.IsARM(guest.OsArch)
func (self *SKVMGuestDriver) NeedStopForChangeSpec(ctx context.Context, guest *models.SGuest, addCpu int, addMemMb int) bool {
return guest.GetMetadata(ctx, api.VM_METADATA_HOTPLUG_CPU_MEM, nil) != "enable" || apis.IsARM(guest.OsArch) && addMemMb > 0
}

func (self *SKVMGuestDriver) RequestChangeVmConfig(ctx context.Context, guest *models.SGuest, task taskman.ITask, instanceType string, vcpuCount, cpuSockets, vmemSize int64) error {
Expand Down Expand Up @@ -866,7 +864,7 @@ func (self *SKVMGuestDriver) RequestCancelLiveMigrate(ctx context.Context, guest
}

func (self *SKVMGuestDriver) ValidateDetachNetwork(ctx context.Context, userCred mcclient.TokenCredential, guest *models.SGuest) error {
if guest.Status == api.VM_RUNNING && guest.GetMetadata(ctx, "hot_remove_nic", nil) != "enable" {
if guest.Status == api.VM_RUNNING && guest.GetMetadata(ctx, api.VM_METADATA_HOT_REMOVE_NIC, nil) != "enable" {
return httperrors.NewBadRequestError("Guest %s can't hot remove nic", guest.GetName())
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/compute/models/guest_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2762,7 +2762,7 @@ func (self *SGuest) PerformChangeConfig(ctx context.Context, userCred mcclient.T
confs.Set("cpu_sockets", jsonutils.NewInt(int64(*input.CpuSockets)))
}

if self.Status == api.VM_RUNNING && (cpuChanged || memChanged || cpuSocketsChanged) && self.GetDriver().NeedStopForChangeSpec(ctx, self, cpuChanged, memChanged) {
if self.Status == api.VM_RUNNING && (cpuChanged || memChanged || cpuSocketsChanged) && self.GetDriver().NeedStopForChangeSpec(ctx, self, addCpu, addMem) {
return nil, httperrors.NewInvalidStatusError("cannot change CPU/Memory spec in status %s", self.Status)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/compute/models/guestdrivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ type IGuestDriver interface {
IsSupportPublicIp() bool
ValidateCreateEip(ctx context.Context, userCred mcclient.TokenCredential, input api.ServerCreateEipInput) error

NeedStopForChangeSpec(ctx context.Context, guest *SGuest, cpuChanged, memChanged bool) bool
NeedStopForChangeSpec(ctx context.Context, guest *SGuest, addCpu int, addMemMb int) bool

OnGuestChangeCpuMemFailed(ctx context.Context, guest *SGuest, data *jsonutils.JSONDict, task taskman.ITask) error
IsSupportGuestClone() bool
Expand Down
5 changes: 5 additions & 0 deletions pkg/compute/tasks/guest_start_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func (self *GuestStartTask) RequestStart(ctx context.Context, guest *models.SGue

func (task *GuestStartTask) OnStartComplete(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
guest := obj.(*models.SGuest)
// save start mem and cpu
guest.SetMetadata(ctx, api.VM_METADATA_START_VCPU_COUNT, guest.VcpuCount, task.UserCred)
guest.SetMetadata(ctx, api.VM_METADATA_START_VMEM_MB, guest.VmemSize, task.UserCred)
// sync Vpc Topology
isVpc, err := guest.IsOneCloudVpcNetwork()
if err != nil {
log.Errorf("IsOneCloudVpcNetwork fail: %s", err)
Expand All @@ -68,6 +72,7 @@ func (task *GuestStartTask) OnStartComplete(ctx context.Context, obj db.IStandal
log.Errorf("vpcagent.VpcAgent.DoSync fail %s", err)
}
}
// log
db.OpsLog.LogEvent(guest, db.ACT_START, guest.GetShortDesc(ctx), task.UserCred)
logclient.AddActionLogWithStartable(task, guest, logclient.ACT_VM_START, guest.GetShortDesc(ctx), task.UserCred, true)
task.taskComplete(ctx, guest)
Expand Down
8 changes: 4 additions & 4 deletions pkg/hostman/guestman/qemu-kvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -759,8 +759,8 @@ func (s *SKVMGuestInstance) onImportGuestMonitorConnected(ctx context.Context) {
log.Infof("Guest %s qemu version %s", s.Id, version)
s.QemuVersion = version
meta := jsonutils.NewDict()
meta.Set("hotplug_cpu_mem", jsonutils.NewString("disable"))
meta.Set("hot_remove_nic", jsonutils.NewString("disable"))
meta.Set(api.VM_METADATA_HOTPLUG_CPU_MEM, jsonutils.NewString("disable"))
meta.Set(api.VM_METADATA_HOT_REMOVE_NIC, jsonutils.NewString("disable"))
meta.Set("__qemu_version", jsonutils.NewString(s.GetQemuVersionStr()))
s.SyncMetadata(meta)
s.SyncStatus("")
Expand Down Expand Up @@ -2602,8 +2602,8 @@ func (s *SKVMGuestInstance) OnResumeSyncMetadataInfo() {
meta.Set("__qemu_version", jsonutils.NewString(s.GetQemuVersionStr()))
meta.Set("__vnc_port", jsonutils.NewInt(int64(s.GetVncPort())))
meta.Set("__enable_cgroup_cpuset", jsonutils.JSONTrue)
meta.Set("hotplug_cpu_mem", jsonutils.NewString("enable"))
meta.Set("hot_remove_nic", jsonutils.NewString("enable"))
meta.Set(api.VM_METADATA_HOTPLUG_CPU_MEM, jsonutils.NewString("enable"))
meta.Set(api.VM_METADATA_HOT_REMOVE_NIC, jsonutils.NewString("enable"))
if len(s.VncPassword) > 0 {
meta.Set("__vnc_password", jsonutils.NewString(s.VncPassword))
}
Expand Down

0 comments on commit 51ecf05

Please sign in to comment.