diff --git a/server/src/main/java/com/cloud/storage/snapshot/SnapshotManagerImpl.java b/server/src/main/java/com/cloud/storage/snapshot/SnapshotManagerImpl.java index d60bff095406..62a02111ae92 100755 --- a/server/src/main/java/com/cloud/storage/snapshot/SnapshotManagerImpl.java +++ b/server/src/main/java/com/cloud/storage/snapshot/SnapshotManagerImpl.java @@ -1632,18 +1632,25 @@ public SnapshotInfo takeSnapshot(VolumeInfo volume) throws ResourceAllocationExc boolean isKvmAndFileBasedStorage = isHypervisorKvmAndFileBasedStorage(volume, storagePool); boolean backupSnapToSecondary = isBackupSnapshotToSecondaryForZone(volume.getDataCenterId()); - if (isKvmAndFileBasedStorage && backupSnapToSecondary) { - DataStore imageStore = snapshotSrv.findSnapshotImageStore(snapshot); - if (imageStore == null) { - throw new CloudRuntimeException(String.format("Could not find any secondary storage to allocate snapshot [%s].", snapshot)); - } - snapshot.setImageStore(imageStore); - } - updateSnapshotPayload(volume.getPoolId(), payload, isKvmAndFileBasedStorage, clusterId); snapshot.addPayload(payload); + try { + if (isKvmAndFileBasedStorage && backupSnapToSecondary) { + try { + DataStore imageStore = snapshotSrv.findSnapshotImageStore(snapshot); + if (imageStore == null) { + throw new CloudRuntimeException(String.format("Could not find any secondary storage to allocate snapshot [%s].", snapshot)); + } + snapshot.setImageStore(imageStore); + } catch (CloudRuntimeException ex) { + logger.error("There was an error while selecting image store for snapshot: {}", ex.getMessage()); + handleErrorInUncreatedSnapshot(snapshotId); + throw new CloudRuntimeException(ex.getMessage()); + } + } + SnapshotStrategy snapshotStrategy = _storageStrategyFactory.getSnapshotStrategy(snapshot, SnapshotOperation.TAKE); if (snapshotStrategy == null) { @@ -1738,6 +1745,12 @@ private void postSnapshotDirectlyToSecondary(SnapshotInfo snapshot, SnapshotInfo snapshotDetailsDao.removeDetail(snapshotOnPrimary.getId(), AsyncJob.Constants.MS_ID); } + private void handleErrorInUncreatedSnapshot(Long snapshotId) { + SnapshotVO snapshot = _snapshotDao.findById(snapshotId); + snapshot.setState(Snapshot.State.Error); + _snapshotDao.persist(snapshot); + } + @Override public boolean isHypervisorKvmAndFileBasedStorage(VolumeInfo volumeInfo, StoragePool storagePool) { Set fileBasedStores = Set.of(Storage.StoragePoolType.SharedMountPoint, Storage.StoragePoolType.NetworkFilesystem, Storage.StoragePoolType.Filesystem);