From 1dfd41697b79c5f89f178be4e6b621e63de2176a Mon Sep 17 00:00:00 2001 From: wanyaoqi <18528551+wanyaoqi@users.noreply.github.com> Date: Fri, 16 Aug 2024 09:56:37 +0800 Subject: [PATCH] Automated cherry pick of #21039: fix(region,host,host-deployer): deploy guest ssh port add lock (#21040) * fix(host): check nodes count on alloc cpuset * fix(host-deployer): get ssh port add lock * feat(region): guest clone add prefer host --- pkg/apis/compute/guests.go | 4 ++++ pkg/compute/models/guest_actions.go | 10 ++++++++++ pkg/hostman/diskutils/qemu_kvm/driver.go | 4 ++++ pkg/hostman/guestman/guesthelper.go | 4 ++++ 4 files changed, 22 insertions(+) diff --git a/pkg/apis/compute/guests.go b/pkg/apis/compute/guests.go index 19ce765667..f9c73b0070 100644 --- a/pkg/apis/compute/guests.go +++ b/pkg/apis/compute/guests.go @@ -1029,6 +1029,10 @@ type ServerSnapshotAndCloneInput struct { // ignore InstanceSnapshotId string `json:"instance_snapshot_id"` + + // Perfer clone destination host + // 指定期望的迁移目标宿主机 + PreferHostId string `json:"prefer_host_id"` } type ServerInstanceSnapshot struct { diff --git a/pkg/compute/models/guest_actions.go b/pkg/compute/models/guest_actions.go index a1dfe7a4f2..5ae3e16de3 100644 --- a/pkg/compute/models/guest_actions.go +++ b/pkg/compute/models/guest_actions.go @@ -5351,6 +5351,16 @@ func (self *SGuest) PerformSnapshotAndClone( return nil, httperrors.NewInputParameterError("count must > 0") } } + if len(input.PreferHostId) > 0 { + if len(input.PreferHostId) > 0 { + iHost, _ := HostManager.FetchByIdOrName(ctx, userCred, input.PreferHostId) + if iHost == nil { + return nil, httperrors.NewBadRequestError("Host %s not found", input.PreferHostId) + } + host := iHost.(*SHost) + input.PreferHostId = host.Id + } + } lockman.LockRawObject(ctx, InstanceSnapshotManager.Keyword(), "name") defer lockman.ReleaseRawObject(ctx, InstanceSnapshotManager.Keyword(), "name") diff --git a/pkg/hostman/diskutils/qemu_kvm/driver.go b/pkg/hostman/diskutils/qemu_kvm/driver.go index 434a7d7184..06ed93e77c 100644 --- a/pkg/hostman/diskutils/qemu_kvm/driver.go +++ b/pkg/hostman/diskutils/qemu_kvm/driver.go @@ -66,6 +66,7 @@ type QemuDeployManager struct { hugepage bool hugepageSizeKB int portsInUse *sync.Map + sshPortLock *sync.Mutex lastUsedSshPort int qemuCmd string memSizeMb int @@ -152,6 +153,8 @@ func (m *QemuDeployManager) unsetPort(port int) { } func (m *QemuDeployManager) GetSshFreePort() int { + m.sshPortLock.Lock() + defer m.sshPortLock.Unlock() port := m.GetFreePortByBase(BASE_SSH_PORT + m.lastUsedSshPort) m.lastUsedSshPort = port - BASE_SSH_PORT if m.lastUsedSshPort >= 2000 { @@ -230,6 +233,7 @@ func InitQemuDeployManager( hugepageSizeKB: hugepageSizeKB, memSizeMb: memSizeMb, portsInUse: new(sync.Map), + sshPortLock: new(sync.Mutex), c: make(chan struct{}, deployConcurrent), qemuCmd: qemuCmd, } diff --git a/pkg/hostman/guestman/guesthelper.go b/pkg/hostman/guestman/guesthelper.go index ed0425d5c3..e3832f410e 100644 --- a/pkg/hostman/guestman/guesthelper.go +++ b/pkg/hostman/guestman/guesthelper.go @@ -320,6 +320,10 @@ func (pq *CpuSetCounter) AllocCpuset(vcpuCount int, memSizeKB int64, perferNumaN pq.Lock.Lock() defer pq.Lock.Unlock() + if len(pq.Nodes) == 0 { + return nil, nil + } + if pq.NumaEnabled { err := pq.AllocNumaNodes(vcpuCount, memSizeKB, perferNumaNode, res) return res, err