fix(region): set default stop time 300s (#25432)

This commit is contained in:
wanyaoqi
2026-08-26 15:22:11 +08:00
committed by GitHub
parent a2e80e5681
commit 575b0ed7be
6 changed files with 62 additions and 21 deletions
+1 -1
View File
@@ -726,7 +726,7 @@ func (base *SBaseGuestDriver) RequestUploadGuestStatus(ctx context.Context, gues
}
func (base *SBaseGuestDriver) CanStop(guest *models.SGuest) error {
if utils.IsInStringArray(guest.Status, []string{api.VM_RUNNING, api.VM_STOP_FAILED, api.POD_STATUS_CRASH_LOOP_BACK_OFF, api.POD_STATUS_CONTAINER_EXITED, api.VM_KICKSTART_INSTALLING, api.VM_KICKSTART_FAILED, api.VM_KICKSTART_COMPLETED}) {
if utils.IsInStringArray(guest.Status, []string{api.VM_RUNNING, api.VM_STOP_FAILED, api.VM_STOPPING, api.POD_STATUS_CRASH_LOOP_BACK_OFF, api.POD_STATUS_CONTAINER_EXITED, api.VM_KICKSTART_INSTALLING, api.VM_KICKSTART_FAILED, api.VM_KICKSTART_COMPLETED}) {
return nil
}
return errors.Wrapf(errors.ErrInvalidStatus, "Cannot stop server in status %s", guest.Status)
+1 -6
View File
@@ -28,7 +28,6 @@ import (
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/util/httputils"
"yunion.io/x/pkg/util/osprofile"
"yunion.io/x/pkg/util/rbacscope"
"yunion.io/x/pkg/utils"
"yunion.io/x/sqlchemy"
@@ -262,11 +261,7 @@ func (self *SKVMGuestDriver) RequestStopOnHost(ctx context.Context, guest *model
params := task.GetParams()
timeout, err := params.Int("timeout")
if err != nil {
if guest.OsType == osprofile.OS_TYPE_WINDOWS {
timeout = int64(options.Options.WindowsGuestStopTimeout)
} else {
timeout = int64(options.Options.LinuxGuestStopTimeout)
}
timeout = int64(options.Options.DefaultGuestStopTimeout)
}
isForce, _ := params.Bool("is_force")
if isForce {
+1 -4
View File
@@ -1428,10 +1428,7 @@ func (self *SGuest) StartGuestStopTask(ctx context.Context, userCred mcclient.To
if timeoutSecs != nil {
params.Add(jsonutils.NewInt(int64(*timeoutSecs)), "timeout")
} else {
timeout := options.Options.LinuxGuestStopTimeout
if self.OsType == osprofile.OS_TYPE_WINDOWS {
timeout = options.Options.WindowsGuestStopTimeout
}
timeout := options.Options.DefaultGuestStopTimeout
params.Add(jsonutils.NewInt(int64(timeout)), "timeout")
}
params.Add(jsonutils.NewBool(stopCharging), "stop_charging")
+1 -2
View File
@@ -98,8 +98,7 @@ type ComputeOptions struct {
DefaultDiskDriver string `help:"default disk driver" choices:"scsi|virtio|ide" default:"scsi"`
DefaultDiskCacheMode string `help:"default kvm disk cache mode" choices:"writeback|none|writethrough" default:"none"`
WindowsGuestStopTimeout int `help:"windows guest stop timeout" default:"120"`
LinuxGuestStopTimeout int `help:"linux stop timeout" default:"30"`
DefaultGuestStopTimeout int `help:"default guest stop timeout" default:"300"`
SystemAdminQuotaCheck bool `help:"Enable quota check for system admin, default False" default:"false"`
CloudaccountHealthStatusCheck bool `help:"Enable cloudaccount health status check, default True" default:"true"`
+23 -7
View File
@@ -65,6 +65,7 @@ type SGuestStopTask struct {
timeout int64
isFroce bool
startPowerdown time.Time
c chan context.Context
}
func NewGuestStopTask(guest *SKVMGuestInstance, ctx context.Context, timeout int64, isForce bool) *SGuestStopTask {
@@ -74,6 +75,7 @@ func NewGuestStopTask(guest *SKVMGuestInstance, ctx context.Context, timeout int
timeout: timeout,
isFroce: isForce,
startPowerdown: time.Time{},
c: make(chan context.Context),
}
}
@@ -86,6 +88,10 @@ func (s *SGuestStopTask) Start() {
s.checkGuestRunning()
}
func (s *SGuestStopTask) StopNow(ctx context.Context) {
s.c <- ctx
}
func (s *SGuestStopTask) onPowerdownGuest(results string) {
//s.ExitCleanup(true)
log.Debugf("system_powerdown callback successfully")
@@ -93,21 +99,31 @@ func (s *SGuestStopTask) onPowerdownGuest(results string) {
}
func (s *SGuestStopTask) checkGuestRunning() {
if !s.IsRunning() {
select {
case ctx := <-s.c:
s.Stop() // force stop
s.stopping = false
if ctx != nil {
hostutils.TaskComplete(ctx, nil)
}
hostutils.TaskComplete(s.ctx, nil)
} else if time.Now().Sub(s.startPowerdown) > time.Duration(s.timeout)*time.Second {
// timeout
if s.isFroce {
case <-time.After(time.Second * 1):
if !s.IsRunning() {
s.Stop() // force stop
s.stopping = false
hostutils.TaskComplete(s.ctx, nil)
} else if time.Now().Sub(s.startPowerdown) > time.Duration(s.timeout)*time.Second {
// timeout
if s.isFroce {
s.Stop() // force stop
s.stopping = false
hostutils.TaskComplete(s.ctx, nil)
} else {
hostutils.TaskFailed(s.ctx, fmt.Sprintf("guest stop timeout after %d seconds", s.timeout))
}
} else {
hostutils.TaskFailed(s.ctx, fmt.Sprintf("guest stop timeout after %d seconds", s.timeout))
go s.checkGuestRunning()
}
} else {
s.CheckGuestRunningLater()
}
}
+35 -1
View File
@@ -24,6 +24,7 @@ import (
"regexp"
"strconv"
"strings"
"sync"
"sync/atomic"
"syscall"
"time"
@@ -103,6 +104,9 @@ type SKVMInstanceRuntime struct {
StartupTask *SGuestResumeTask
MigrateTask *SGuestLiveMigrateTask
stopLock sync.Mutex
StopTask *SGuestStopTask
pciUninitialized bool
pciAddrs *desc.SGuestPCIAddresses
}
@@ -134,6 +138,19 @@ func NewKVMGuestInstance(id string, manager *SGuestManager) *SKVMGuestInstance {
}
}
func (s *SKVMGuestInstance) SetStopTask(task *SGuestStopTask) {
s.stopLock.Lock()
defer s.stopLock.Unlock()
s.StopTask = task
}
func (s *SKVMGuestInstance) GetStopTask() *SGuestStopTask {
s.stopLock.Lock()
defer s.stopLock.Unlock()
return s.StopTask
}
// update guest runtime desc from source desc
// and check is need regenerate runtime desc
// these property can't be upate in running guest
@@ -1660,6 +1677,7 @@ func (s *SKVMGuestInstance) guestRun(ctx context.Context) {
func (s *SKVMGuestInstance) onMonitorDisConnect(err error) {
log.Errorf("Guest %s on Monitor Disconnect reason: %v", s.Id, err)
s.CleanStartupTask()
s.detachStopTask()
s.scriptStop()
s.SyncStatus(fmt.Sprintf("monitor disconnect %v", err))
if s.guestAgent != nil {
@@ -1796,6 +1814,11 @@ func (s *SKVMGuestInstance) CleanStartupTask() {
}
}
func (s *SKVMGuestInstance) detachStopTask() {
log.Infof("[%s] detachStopTask", s.GetId())
s.SetStopTask(nil)
}
func (s *SKVMGuestInstance) onMonitorTimeout(ctx context.Context, err error) {
log.Errorf("Monitor connect timeout, VM %s frozen: %s force restart!!!!", s.Id, err)
s.ForceStop()
@@ -2111,6 +2134,7 @@ func (s *SKVMGuestInstance) ForceStop() bool {
func (s *SKVMGuestInstance) ExitCleanup(clear bool) {
s.cleanupKickstartMonitor()
s.detachStopTask()
if clear {
pid := s.GetPid()
if pid > 0 {
@@ -2345,7 +2369,17 @@ func (s *SKVMGuestInstance) ExecStopTask(ctx context.Context, params interface{}
if !ok {
return nil, hostutils.ParamsError
}
NewGuestStopTask(s, ctx, input.Timeout, input.IsForce).Start()
s.stopLock.Lock()
defer s.stopLock.Unlock()
if s.StopTask != nil {
if !input.IsForce {
return nil, errors.Errorf("guest %s is stopping", s.GetId())
}
s.StopTask.StopNow(ctx)
} else {
s.StopTask = NewGuestStopTask(s, ctx, input.Timeout, input.IsForce)
s.StopTask.Start()
}
return nil, nil
}