This is an automated email from the ASF dual-hosted git repository.

gabriel pushed a commit to branch snapshot-deletion-issues
in repository https://gitbox.apache.org/repos/asf/cloudstack.git

commit bc9387fb91918e02cc1296a3c82f563795bf89ea
Author: GabrielBrascher <gabr...@apache.org>
AuthorDate: Mon Nov 25 17:21:09 2019 -0200

    Fix log message missing %d and remove snapshot on DB
---
 .../storage/snapshot/DefaultSnapshotStrategy.java  | 28 +++++++++++++++-------
 1 file changed, 19 insertions(+), 9 deletions(-)

diff --git 
a/engine/storage/snapshot/src/main/java/org/apache/cloudstack/storage/snapshot/DefaultSnapshotStrategy.java
 
b/engine/storage/snapshot/src/main/java/org/apache/cloudstack/storage/snapshot/DefaultSnapshotStrategy.java
index 3e8e473..a819384 100644
--- 
a/engine/storage/snapshot/src/main/java/org/apache/cloudstack/storage/snapshot/DefaultSnapshotStrategy.java
+++ 
b/engine/storage/snapshot/src/main/java/org/apache/cloudstack/storage/snapshot/DefaultSnapshotStrategy.java
@@ -266,7 +266,7 @@ public class DefaultSnapshotStrategy extends 
SnapshotStrategyBase {
 
         boolean deletedOnSecondary = false;
         if (snapshotOnImage == null) {
-            s_logger.debug(String.format("Can't find snapshot [snapshot id: 
%d] on backup storage"));
+            s_logger.debug(String.format("Can't find snapshot [snapshot id: 
%d] on backup storage", snapshotId));
         } else {
             SnapshotObject obj = (SnapshotObject)snapshotOnImage;
             try {
@@ -321,19 +321,29 @@ public class DefaultSnapshotStrategy extends 
SnapshotStrategyBase {
      * In case of failure, it will throw one of the following exceptions: 
CloudRuntimeException, InterruptedException, or ExecutionException. </br>
      */
     private boolean deleteSnapshotOnPrimary(Long snapshotId) {
-        boolean result = false;
+        SnapshotDataStoreVO snapshotOnPrimary = 
snapshotStoreDao.findBySnapshot(snapshotId, DataStoreRole.Primary);
+        SnapshotInfo snapshotOnPrimaryInfo = 
snapshotDataFactory.getSnapshot(snapshotId, DataStoreRole.Primary);
+        if (isSnapshotOnPrimaryStorage(snapshotId) && 
snapshotSvr.deleteSnapshot(snapshotOnPrimaryInfo)) {
+            snapshotOnPrimary.setState(State.Destroyed);
+            snapshotStoreDao.update(snapshotOnPrimary.getId(), 
snapshotOnPrimary);
+            snapshotDao.remove(snapshotId);
+            return true;
+        }
+        return false;
+    }
+
+    /**
+     * Returns true if the snapshot volume is on primary storage.
+     */
+    private boolean isSnapshotOnPrimaryStorage(long snapshotId) {
         SnapshotDataStoreVO snapshotOnPrimary = 
snapshotStoreDao.findBySnapshot(snapshotId, DataStoreRole.Primary);
         if (snapshotOnPrimary != null) {
-            SnapshotInfo snapshotOnPrimaryInfo = 
snapshotDataFactory.getSnapshot(snapshotId, DataStoreRole.Primary);
             long volumeId = snapshotOnPrimary.getVolumeId();
             VolumeVO volumeVO = volumeDao.findById(volumeId);
-            if 
(((PrimaryDataStoreImpl)snapshotOnPrimaryInfo.getDataStore()).getPoolType() == 
StoragePoolType.RBD && volumeVO != null) {
-                result = snapshotSvr.deleteSnapshot(snapshotOnPrimaryInfo);
-            }
-            snapshotOnPrimary.setState(State.Destroyed);
-            snapshotStoreDao.update(snapshotOnPrimary.getId(), 
snapshotOnPrimary);
+            boolean isVolumeOnPrimary = volumeVO != null && 
volumeVO.getRemoved() == null;
+            return isVolumeOnPrimary;
         }
-        return result;
+        return false;
     }
 
     @Override

Reply via email to