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

fix(region): record snapshot last attched host on delete disk #21909

Merged
Merged
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
19 changes: 19 additions & 0 deletions pkg/compute/models/disks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2205,6 +2205,25 @@ func (self *SDisk) GetLastAttachedHost(ctx context.Context, userCred mcclient.To
return self.GetMetadata(ctx, api.DISK_META_LAST_ATTACHED_HOST, userCred)
}

func (self *SDisk) RecordDiskSnapshotsLastHost(ctx context.Context, userCred mcclient.TokenCredential, hostId string) error {
storage, err := self.GetStorage()
if err != nil {
return err
}
if storage.StorageType != api.STORAGE_SLVM {
return nil
}
// record disk snapshots master host
snaps := SnapshotManager.GetDiskSnapshots(self.Id)
for i := range snaps {
err = snaps[i].SetMetadata(ctx, api.DISK_META_LAST_ATTACHED_HOST, hostId, userCred)
if err != nil {
log.Errorf("snapshot %s failed set last attached host: %s", snaps[i].Id, err)
}
}
return nil
}

// 同步磁盘状态
func (self *SDisk) PerformSyncstatus(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input api.DiskSyncstatusInput) (jsonutils.JSONObject, error) {
var openTask = true
Expand Down
31 changes: 23 additions & 8 deletions pkg/compute/storagedrivers/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,37 @@ func (self *SBaseStorageDriver) RequestDeleteSnapshot(ctx context.Context, snaps
}
}
if guest == nil {
storage := snapshot.GetStorage()
host, err := storage.GetMasterHost()
if err != nil {
return err
var host *models.SHost
disk, err := models.DiskManager.FetchById(snapshot.DiskId)
if err != nil && err != sql.ErrNoRows {
return errors.Wrap(err, "get disk by snapshot")
}
if disk != nil {
sDisk := disk.(*models.SDisk)
if hostId := sDisk.GetLastAttachedHost(ctx, task.GetUserCred()); hostId != "" {
host = models.HostManager.FetchHostById(hostId)
}
} else {
if hostId := snapshot.GetMetadata(ctx, api.DISK_META_LAST_ATTACHED_HOST, task.GetUserCred()); hostId != "" {
host = models.HostManager.FetchHostById(hostId)
}
}
if host == nil {
storage := snapshot.GetStorage()
host, err = storage.GetMasterHost()
if err != nil {
return err
}
}

convertSnapshot, err := models.SnapshotManager.GetConvertSnapshot(snapshot)
if err != nil && err != sql.ErrNoRows {
return errors.Wrap(err, "get convert snapshot")
}
params := jsonutils.NewDict()
params.Set("delete_snapshot", jsonutils.NewString(snapshot.Id))
params.Set("disk_id", jsonutils.NewString(snapshot.DiskId))
disk, err := models.DiskManager.FetchById(snapshot.DiskId)
if err != nil && err != sql.ErrNoRows {
return errors.Wrap(err, "get disk by snapshot")
}

if disk != nil {
sDisk, _ := disk.(*models.SDisk)
if sDisk.IsEncrypted() {
Expand Down
4 changes: 4 additions & 0 deletions pkg/compute/tasks/disk_delete_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ func (self *DiskDeleteTask) startDeleteDisk(ctx context.Context, disk *models.SD
}
}

if host != nil {
disk.RecordDiskSnapshotsLastHost(ctx, self.UserCred, host.Id)
}

isPurge := false
if (host == nil || !host.GetEnabled()) && purgeParams {
isPurge = true
Expand Down
Loading