diff --git a/core/src/com/cloud/storage/dao/SnapshotDao.java b/core/src/com/cloud/storage/dao/SnapshotDao.java index b424a754ae8..4391add9a82 100644 --- a/core/src/com/cloud/storage/dao/SnapshotDao.java +++ b/core/src/com/cloud/storage/dao/SnapshotDao.java @@ -20,6 +20,7 @@ package com.cloud.storage.dao; import java.util.List; +import com.cloud.storage.Snapshot; import com.cloud.storage.SnapshotVO; import com.cloud.utils.db.Filter; import com.cloud.utils.db.GenericDao; @@ -29,5 +30,6 @@ public interface SnapshotDao extends GenericDao { List listByVolumeId(Filter filter, long volumeId); SnapshotVO findNextSnapshot(long parentSnapId); long getLastSnapshot(long volumeId, long snapId); + List listByVolumeIdType(long volumeId, String type); } diff --git a/core/src/com/cloud/storage/dao/SnapshotDaoImpl.java b/core/src/com/cloud/storage/dao/SnapshotDaoImpl.java index 67c59e2ed85..19f5b64fde8 100644 --- a/core/src/com/cloud/storage/dao/SnapshotDaoImpl.java +++ b/core/src/com/cloud/storage/dao/SnapshotDaoImpl.java @@ -22,6 +22,7 @@ import java.util.List; import javax.ejb.Local; +import com.cloud.storage.Snapshot; import com.cloud.storage.SnapshotVO; import com.cloud.utils.db.Filter; import com.cloud.utils.db.GenericDaoBase; @@ -33,6 +34,7 @@ import com.cloud.utils.db.SearchCriteria; public class SnapshotDaoImpl extends GenericDaoBase implements SnapshotDao { private final SearchBuilder VolumeIdSearch; + private final SearchBuilder VolumeIdTypeSearch; private final SearchBuilder ParentIdSearch; private final GenericSearchBuilder lastSnapSearch; @@ -42,6 +44,12 @@ public class SnapshotDaoImpl extends GenericDaoBase implements sc.setParameters("prevSnapshotId", snapshotId); return findOneIncludingRemovedBy(sc); } + + @Override + public List listByVolumeIdType(long volumeId, String type ) { + return listByVolumeIdType(null, volumeId, type); + } + @Override public List listByVolumeId(long volumeId) { @@ -54,12 +62,25 @@ public class SnapshotDaoImpl extends GenericDaoBase implements sc.setParameters("volumeId", volumeId); return listBy(sc, filter); } + + + public List listByVolumeIdType(Filter filter, long volumeId, String type ) { + SearchCriteria sc = VolumeIdTypeSearch.create(); + sc.setParameters("volumeId", volumeId); + sc.setParameters("type", type); + return listBy(sc, filter); + } protected SnapshotDaoImpl() { VolumeIdSearch = createSearchBuilder(); VolumeIdSearch.and("volumeId", VolumeIdSearch.entity().getVolumeId(), SearchCriteria.Op.EQ); VolumeIdSearch.done(); + VolumeIdTypeSearch = createSearchBuilder(); + VolumeIdTypeSearch.and("volumeId", VolumeIdTypeSearch.entity().getVolumeId(), SearchCriteria.Op.EQ); + VolumeIdTypeSearch.and("type", VolumeIdTypeSearch.entity().getTypeDescription(), SearchCriteria.Op.EQ); + VolumeIdTypeSearch.done(); + ParentIdSearch = createSearchBuilder(); ParentIdSearch.and("prevSnapshotId", ParentIdSearch.entity().getPrevSnapshotId(), SearchCriteria.Op.EQ); ParentIdSearch.done(); diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index 7fbc447e9a2..bf66acedbcc 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -6875,7 +6875,7 @@ public class ManagementServerImpl implements ManagementServer { public long deleteSnapshotAsync(long userId, long snapshotId) { Snapshot snapshot = findSnapshotById(snapshotId); long volumeId = snapshot.getVolumeId(); - List policies = _snapMgr.listPoliciesforSnapshot(snapshotId); + List policies = _snapMgr.listPoliciesforVolume(volumeId); // Return the job id of the last destroySnapshotAsync job which actually destroys the snapshot. // The rest of the async jobs just update the db and don't really do any meaningful thing. diff --git a/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java b/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java index ea0ad93f192..98356d4bc3a 100644 --- a/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java +++ b/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java @@ -606,8 +606,7 @@ public class SnapshotManagerImpl implements SnapshotManager { private void postCreateRecurringSnapshotForPolicy(long userId, long volumeId, long snapshotId, long policyId) { //Use count query - Filter searchFilter = new Filter(SnapshotVO.class, GenericDaoBase.CREATED_COLUMN, true, null, null); - List snaps = listSnapsforPolicy(policyId, searchFilter); + List snaps = listSnapsforVolumeType(volumeId, SnapshotType.RECURRING.name()); SnapshotPolicyVO policy = _snapshotPolicyDao.findById(policyId); while(snaps.size() > policy.getMaxSnaps() && snaps.size() > 1) { @@ -640,9 +639,8 @@ public class SnapshotManagerImpl implements SnapshotManager { while( lastSnapshot.getRemoved() != null ) { String BackupSnapshotId = lastSnapshot.getBackupSnapshotId(); if( BackupSnapshotId != null ) { - if( destroySnapshotBackUp(userId, snapshotId, policyId) ) { - lastSnapshot.setBackupSnapshotId(null); - _snapshotDao.update(lastId, lastSnapshot); + if( destroySnapshotBackUp(userId, lastId, policyId) ) { + } else { s_logger.debug("Destroying snapshot backup failed " + lastSnapshot); break; @@ -650,6 +648,9 @@ public class SnapshotManagerImpl implements SnapshotManager { } postDeleteSnapshot(userId, lastId, policyId); lastId = lastSnapshot.getPrevSnapshotId(); + if( lastId == 0 ) { + break; + } lastSnapshot = _snapshotDao.findById(lastId); } return true; @@ -699,8 +700,9 @@ public class SnapshotManagerImpl implements SnapshotManager { _pauseInterval, _shouldBeSnapshotCapable, volume.getInstanceId()); if ((answer != null) && answer.getResult()) { + snapshot.setBackupSnapshotId(null); + _snapshotDao.update(snapshotId, snapshot); // This is not the last snapshot. - postDeleteSnapshot(userId, snapshotId, policyId); success = true; details = "Successfully deleted snapshot " + snapshotId + " for volumeId: " + volumeId + " and policyId " + policyId; @@ -933,6 +935,10 @@ public class SnapshotManagerImpl implements SnapshotManager { public List listSnapsforVolume(long volumeId) { return _snapshotDao.listByVolumeId(volumeId); } + + public List listSnapsforVolumeType(long volumeId, String type) { + return _snapshotDao.listByVolumeIdType(volumeId, type); + } @Override public void deletePoliciesForVolume(Long volumeId) {