Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] fix: delete instance backup #18900

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pkg/compute/regiondrivers/kvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ func (self *SKVMRegionDriver) RequestDeleteInstanceSnapshot(ctx context.Context,
func (self *SKVMRegionDriver) RequestDeleteInstanceBackup(ctx context.Context, ib *models.SInstanceBackup, task taskman.ITask) error {
backups, err := ib.GetBackups()
if err != nil {
return err
return errors.Wrap(err, "GetBackups")
}
if len(backups) == 0 {
task.SetStage("OnInstanceBackupDelete", nil)
Expand All @@ -671,9 +671,9 @@ func (self *SKVMRegionDriver) RequestDeleteInstanceBackup(ctx context.Context, i
params.Set("del_backup_id", jsonutils.NewString(backups[0].Id))
task.SetStage("OnKvmDiskBackupDelete", params)
forceDelete := jsonutils.QueryBoolean(task.GetParams(), "force_delete", false)
err = backups[0].StartBackupDeleteTask(ctx, task.GetUserCred(), task.GetTaskId(), forceDelete)
err = backups[len(backups)-1].StartBackupDeleteTask(ctx, task.GetUserCred(), task.GetTaskId(), forceDelete)
if err != nil {
return err
return errors.Wrap(err, "StartBackupDeleteTask")
}
return nil
}
Expand Down
44 changes: 24 additions & 20 deletions pkg/compute/tasks/disk_backup_delete_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,45 +36,49 @@ func init() {
taskman.RegisterTask(DiskBackupDeleteTask{})
}

func (self *DiskBackupDeleteTask) taskFailed(ctx context.Context, backup *models.SDiskBackup, reason jsonutils.JSONObject) {
func (tsk *DiskBackupDeleteTask) taskFailed(ctx context.Context, backup *models.SDiskBackup, reason jsonutils.JSONObject) {
reasonStr, _ := reason.GetString()
backup.SetStatus(self.UserCred, api.BACKUP_STATUS_DELETE_FAILED, reasonStr)
logclient.AddActionLogWithStartable(self, backup, logclient.ACT_DELETE, reason, self.UserCred, false)
self.SetStageFailed(ctx, reason)
backup.SetStatus(tsk.UserCred, api.BACKUP_STATUS_DELETE_FAILED, reasonStr)
logclient.AddActionLogWithStartable(tsk, backup, logclient.ACT_DELETE, reason, tsk.UserCred, false)
tsk.SetStageFailed(ctx, reason)
}

func (self *DiskBackupDeleteTask) taskSuccess(ctx context.Context, backup *models.SDiskBackup, data jsonutils.JSONObject) {
backup.RealDelete(ctx, self.UserCred)
self.SetStageComplete(ctx, nil)
func (tsk *DiskBackupDeleteTask) taskSuccess(ctx context.Context, backup *models.SDiskBackup, data jsonutils.JSONObject) {
backup.RealDelete(ctx, tsk.UserCred)
logclient.AddActionLogWithStartable(tsk, backup, logclient.ACT_DELETE, backup.GetShortDesc(ctx), tsk.UserCred, true)
tsk.SetStageComplete(ctx, nil)
}

func (self *DiskBackupDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
func (tsk *DiskBackupDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
backup := obj.(*models.SDiskBackup)
self.SetStage("OnDelete", nil)
rd, err := backup.GetRegionDriver()
if err != nil {
self.taskFailed(ctx, backup, jsonutils.NewString(err.Error()))
tsk.taskFailed(ctx, backup, jsonutils.NewString(err.Error()))
return
}
if err := rd.RequestDeleteBackup(ctx, backup, self); err != nil {
self.taskFailed(ctx, backup, jsonutils.NewString(err.Error()))
tsk.SetStage("OnDeleteComplete", nil)
if err := rd.RequestDeleteBackup(ctx, backup, tsk); err != nil {
tsk.taskFailed(ctx, backup, jsonutils.NewString(err.Error()))
return
}
}

func (self *DiskBackupDeleteTask) OnDelete(ctx context.Context, backup *models.SDiskBackup, data jsonutils.JSONObject) {
self.taskSuccess(ctx, backup, nil)
func (tsk *DiskBackupDeleteTask) OnDeleteComplete(ctx context.Context, backup *models.SDiskBackup, data jsonutils.JSONObject) {
tsk.taskSuccess(ctx, backup, nil)
}

func (self *DiskBackupDeleteTask) OnDeleteFailed(ctx context.Context, backup *models.SDiskBackup, data jsonutils.JSONObject) {
log.Infof("params: %s", self.Params)
func (tsk *DiskBackupDeleteTask) OnDeleteCompleteFailed(ctx context.Context, backup *models.SDiskBackup, data jsonutils.JSONObject) {
log.Infof("params: %s", tsk.Params)
log.Infof("OnDeleteFailed data: %s", data)
if forceDelete := jsonutils.QueryBoolean(self.Params, "force_delete", false); !forceDelete {
self.taskFailed(ctx, backup, data)
if forceDelete := jsonutils.QueryBoolean(tsk.Params, "force_delete", false); !forceDelete {
tsk.taskFailed(ctx, backup, data)
return
}
reason, _ := data.GetString("__reason__")
if !strings.Contains(reason, api.BackupStorageOffline) {
self.taskFailed(ctx, backup, data)
tsk.taskFailed(ctx, backup, data)
return
}
log.Infof("delete backup %s failed, force delete", backup.GetId())
self.taskSuccess(ctx, backup, nil)
tsk.taskSuccess(ctx, backup, nil)
}
50 changes: 25 additions & 25 deletions pkg/compute/tasks/instance_backup_delete_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,60 +34,60 @@ func init() {
taskman.RegisterTask(InstanceBackupDeleteTask{})
}

func (self *InstanceBackupDeleteTask) taskFailed(ctx context.Context, ib *models.SInstanceBackup, reason jsonutils.JSONObject) {
func (tsk *InstanceBackupDeleteTask) taskFailed(ctx context.Context, ib *models.SInstanceBackup, reason jsonutils.JSONObject) {
reasonStr, _ := reason.GetString()
ib.SetStatus(self.UserCred, compute.INSTANCE_BACKUP_STATUS_DELETE_FAILED, reasonStr)
logclient.AddActionLogWithStartable(self, ib, logclient.ACT_DELETE, reason, self.UserCred, false)
self.SetStageFailed(ctx, reason)
ib.SetStatus(tsk.UserCred, compute.INSTANCE_BACKUP_STATUS_DELETE_FAILED, reasonStr)
logclient.AddActionLogWithStartable(tsk, ib, logclient.ACT_DELETE, reason, tsk.UserCred, false)
tsk.SetStageFailed(ctx, reason)
}

func (self *InstanceBackupDeleteTask) taskSuccess(ctx context.Context, ib *models.SInstanceBackup, data jsonutils.JSONObject) {
ib.RealDelete(ctx, self.UserCred)
logclient.AddActionLogWithContext(ctx, ib, logclient.ACT_DELETE, nil, self.UserCred, true)
self.SetStageComplete(ctx, nil)
func (tsk *InstanceBackupDeleteTask) taskSuccess(ctx context.Context, ib *models.SInstanceBackup, data jsonutils.JSONObject) {
ib.RealDelete(ctx, tsk.UserCred)
logclient.AddActionLogWithContext(ctx, ib, logclient.ACT_DELETE, ib.GetShortDesc(ctx), tsk.UserCred, true)
tsk.SetStageComplete(ctx, nil)
}

func (self *InstanceBackupDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
func (tsk *InstanceBackupDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
ib := obj.(*models.SInstanceBackup)
self.SetStage("OnInstanceBackupDelete", nil)
if err := ib.GetRegionDriver().RequestDeleteInstanceBackup(ctx, ib, self); err != nil {
self.taskFailed(ctx, ib, jsonutils.NewString(err.Error()))
tsk.SetStage("OnInstanceBackupDelete", nil)
if err := ib.GetRegionDriver().RequestDeleteInstanceBackup(ctx, ib, tsk); err != nil {
tsk.taskFailed(ctx, ib, jsonutils.NewString(err.Error()))
return
}
}

func (self *InstanceBackupDeleteTask) OnKvmDiskBackupDelete(
func (tsk *InstanceBackupDeleteTask) OnKvmDiskBackupDelete(
ctx context.Context, isp *models.SInstanceBackup, data jsonutils.JSONObject) {
backupId, _ := self.Params.GetString("del_backup_id")
backupId, _ := tsk.Params.GetString("del_backup_id")
// detach backup and instance
isjp := new(models.SInstanceBackupJoint)
isjp.SetModelManager(models.InstanceBackupJointManager, isjp)
err := models.InstanceBackupJointManager.Query().
Equals("instance_backup_id", isp.Id).Equals("disk_backup_id", backupId).First(isjp)
if err != nil {
self.taskFailed(ctx, isp, jsonutils.NewString(err.Error()))
tsk.taskFailed(ctx, isp, jsonutils.NewString(err.Error()))
return
}
err = isjp.Delete(ctx, self.UserCred)
err = isjp.Delete(ctx, tsk.UserCred)
if err != nil {
self.taskFailed(ctx, isp, jsonutils.NewString(err.Error()))
tsk.taskFailed(ctx, isp, jsonutils.NewString(err.Error()))
return
}
if err := isp.GetRegionDriver().RequestDeleteInstanceBackup(ctx, isp, self); err != nil {
self.taskFailed(ctx, isp, jsonutils.NewString(err.Error()))
if err := isp.GetRegionDriver().RequestDeleteInstanceBackup(ctx, isp, tsk); err != nil {
tsk.taskFailed(ctx, isp, jsonutils.NewString(err.Error()))
return
}
}

func (self *InstanceBackupDeleteTask) OnKvmDiskBackupDeleteFailed(
func (tsk *InstanceBackupDeleteTask) OnKvmDiskBackupDeleteFailed(
ctx context.Context, ib *models.SInstanceBackup, data jsonutils.JSONObject) {
self.taskFailed(ctx, ib, data)
tsk.taskFailed(ctx, ib, data)
}

func (self *InstanceBackupDeleteTask) OnInstanceBackupDelete(ctx context.Context, ib *models.SInstanceBackup, data jsonutils.JSONObject) {
self.taskSuccess(ctx, ib, data)
func (tsk *InstanceBackupDeleteTask) OnInstanceBackupDelete(ctx context.Context, ib *models.SInstanceBackup, data jsonutils.JSONObject) {
tsk.taskSuccess(ctx, ib, data)
}

func (self *InstanceBackupDeleteTask) OnInstanceBackupDeleteFailed(ctx context.Context, ib *models.SInstanceBackup, data jsonutils.JSONObject) {
self.taskFailed(ctx, ib, data)
func (tsk *InstanceBackupDeleteTask) OnInstanceBackupDeleteFailed(ctx context.Context, ib *models.SInstanceBackup, data jsonutils.JSONObject) {
tsk.taskFailed(ctx, ib, data)
}