server: mark volume snapshots as Destroyed if it does not exist on primary and secondary storage when delete a volume (#6057)

* server: mark volume snapshots as Destroyed in some cases when delete a volume in QCOW2 format

when delete a volume in QCOW2 format, if volume snapshot does not exist on primary and secondary storage, mark the snapshot as Destroyed.

* Update #6057: remove check on volume format
This commit is contained in:
Wei Zhou 2022-03-10 12:49:03 +01:00 committed by GitHub
parent 00c1bdb365
commit 3a456f1b31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 2 deletions

View File

@ -118,4 +118,6 @@ public interface SnapshotApiService {
Snapshot backupSnapshotFromVmSnapshot(Long snapshotId, Long vmId, Long volumeId, Long vmSnapshotId);
SnapshotPolicy updateSnapshotPolicy(UpdateSnapshotPolicyCmd updateSnapshotPolicyCmd);
void markVolumeSnapshotsAsDestroyed(Volume volume);
}

View File

@ -122,6 +122,7 @@ import com.cloud.storage.VolumeVO;
import com.cloud.storage.dao.VMTemplatePoolDao;
import com.cloud.storage.dao.VolumeDao;
import com.cloud.storage.dao.VolumeDetailsDao;
import com.cloud.storage.snapshot.SnapshotApiService;
import com.cloud.storage.snapshot.SnapshotManager;
import com.cloud.storage.template.TemplateProp;
import com.cloud.user.AccountManager;
@ -194,6 +195,8 @@ public class VolumeServiceImpl implements VolumeService {
private StorageManager _storageMgr;
@Inject
private AnnotationDao annotationDao;
@Inject
private SnapshotApiService snapshotApiService;
private final static String SNAPSHOT_ID = "SNAPSHOT_ID";
@ -448,9 +451,9 @@ public class VolumeServiceImpl implements VolumeService {
volDao.remove(vo.getId());
}
SnapshotDataStoreVO snapStoreVo = _snapshotStoreDao.findByVolume(vo.getId(), DataStoreRole.Primary);
List<SnapshotDataStoreVO> snapStoreVOs = _snapshotStoreDao.listAllByVolumeAndDataStore(vo.getId(), DataStoreRole.Primary);
if (snapStoreVo != null) {
for (SnapshotDataStoreVO snapStoreVo : snapStoreVOs) {
long storagePoolId = snapStoreVo.getDataStoreId();
StoragePoolVO storagePoolVO = storagePoolDao.findById(storagePoolId);
@ -468,6 +471,7 @@ public class VolumeServiceImpl implements VolumeService {
_snapshotStoreDao.remove(snapStoreVo.getId());
}
}
snapshotApiService.markVolumeSnapshotsAsDestroyed(vo);
} else {
vo.processEvent(Event.OperationFailed);
apiResult.setResult(result.getResult());

View File

@ -60,6 +60,7 @@ import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
import org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreDao;
import org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO;
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
@ -1543,4 +1544,16 @@ public class SnapshotManagerImpl extends MutualExclusiveIdsManagerBase implement
_resourceLimitMgr.incrementResourceCount(volume.getAccountId(), ResourceType.secondary_storage, new Long(volume.getSize()));
return snapshot;
}
@Override
public void markVolumeSnapshotsAsDestroyed(Volume volume) {
List<SnapshotVO> snapshots = _snapshotDao.listByVolumeId(volume.getId());
for (SnapshotVO snapshot: snapshots) {
List<SnapshotDataStoreVO> snapshotDataStoreVOs = _snapshotStoreDao.findBySnapshotId(snapshot.getId());
if (CollectionUtils.isEmpty(snapshotDataStoreVOs)) {
snapshot.setState(Snapshot.State.Destroyed);
_snapshotDao.update(snapshot.getId(), snapshot);
}
}
}
}