Skip to content

Commit be24733

Browse files
authored
StorPool: fix of delete snapshot (#9855)
* StorPool: fix of delete snapshot Mark the DB record as destroyed when a snapshot is deleted * Addressed reviews * addressed review * addressed review
1 parent 019f2c6 commit be24733

File tree

2 files changed

+36
-26
lines changed

2 files changed

+36
-26
lines changed

plugins/storage/volume/storpool/src/main/java/org/apache/cloudstack/storage/datastore/util/StorPoolUtil.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ public static void spLog(String fmt, Object... args) {
138138

139139
public static final String SP_TIER = "SP_QOSCLASS";
140140

141+
public static final String OBJECT_DOES_NOT_EXIST = "objectDoesNotExist";
142+
141143
public static enum StorpoolRights {
142144
RO("ro"), RW("rw"), DETACH("detach");
143145

@@ -458,7 +460,7 @@ public static JsonArray templatesStats(SpConnectionDesc conn) {
458460
}
459461

460462
private static boolean objectExists(SpApiError err) {
461-
if (!err.getName().equals("objectDoesNotExist")) {
463+
if (!err.getName().equals(OBJECT_DOES_NOT_EXIST)) {
462464
throw new CloudRuntimeException(err.getDescr());
463465
}
464466
return false;

plugins/storage/volume/storpool/src/main/java/org/apache/cloudstack/storage/snapshot/StorPoolSnapshotStrategy.java

+33-25
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,19 @@
1616
// under the License.
1717
package org.apache.cloudstack.storage.snapshot;
1818

19-
import java.util.ArrayList;
20-
import java.util.List;
21-
22-
import javax.inject.Inject;
19+
import com.cloud.exception.InvalidParameterValueException;
20+
import com.cloud.hypervisor.kvm.storage.StorPoolStorageAdaptor;
21+
import com.cloud.storage.DataStoreRole;
22+
import com.cloud.storage.Snapshot;
23+
import com.cloud.storage.SnapshotVO;
24+
import com.cloud.storage.VolumeVO;
25+
import com.cloud.storage.dao.SnapshotDao;
26+
import com.cloud.storage.dao.SnapshotDetailsDao;
27+
import com.cloud.storage.dao.SnapshotDetailsVO;
28+
import com.cloud.storage.dao.SnapshotZoneDao;
29+
import com.cloud.storage.dao.VolumeDao;
30+
import com.cloud.utils.exception.CloudRuntimeException;
31+
import com.cloud.utils.fsm.NoTransitionException;
2332

2433
import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
2534
import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager;
@@ -40,23 +49,13 @@
4049
import org.apache.cloudstack.storage.datastore.util.StorPoolUtil.SpApiResponse;
4150
import org.apache.cloudstack.storage.datastore.util.StorPoolUtil.SpConnectionDesc;
4251
import org.apache.commons.collections.CollectionUtils;
43-
import org.apache.logging.log4j.Logger;
4452
import org.apache.logging.log4j.LogManager;
53+
import org.apache.logging.log4j.Logger;
4554
import org.springframework.stereotype.Component;
4655

47-
import com.cloud.exception.InvalidParameterValueException;
48-
import com.cloud.hypervisor.kvm.storage.StorPoolStorageAdaptor;
49-
import com.cloud.storage.DataStoreRole;
50-
import com.cloud.storage.Snapshot;
51-
import com.cloud.storage.SnapshotVO;
52-
import com.cloud.storage.VolumeVO;
53-
import com.cloud.storage.dao.SnapshotDao;
54-
import com.cloud.storage.dao.SnapshotDetailsDao;
55-
import com.cloud.storage.dao.SnapshotDetailsVO;
56-
import com.cloud.storage.dao.SnapshotZoneDao;
57-
import com.cloud.storage.dao.VolumeDao;
58-
import com.cloud.utils.exception.CloudRuntimeException;
59-
import com.cloud.utils.fsm.NoTransitionException;
56+
import javax.inject.Inject;
57+
import java.util.ArrayList;
58+
import java.util.List;
6059

6160

6261
@Component
@@ -117,10 +116,11 @@ public boolean deleteSnapshot(Long snapshotId, Long zoneId) {
117116
if (resp.getError() != null) {
118117
final String err = String.format("Failed to clean-up Storpool snapshot %s. Error: %s", name, resp.getError());
119118
StorPoolUtil.spLog(err);
120-
markSnapshotAsDestroyedIfAlreadyRemoved(snapshotId, resp);
119+
markSnapshotAsDestroyedIfAlreadyRemoved(snapshotId, resp.getError().getName().equals(StorPoolUtil.OBJECT_DOES_NOT_EXIST));
121120
throw new CloudRuntimeException(err);
122121
} else {
123122
res = deleteSnapshotFromDbIfNeeded(snapshotVO, zoneId);
123+
markSnapshotAsDestroyedIfAlreadyRemoved(snapshotId,true);
124124
StorPoolUtil.spLog("StorpoolSnapshotStrategy.deleteSnapshot: executed successfully=%s, snapshot uuid=%s, name=%s", res, snapshotVO.getUuid(), name);
125125
}
126126
} catch (Exception e) {
@@ -129,15 +129,23 @@ public boolean deleteSnapshot(Long snapshotId, Long zoneId) {
129129
}
130130
}
131131

132+
List<SnapshotDataStoreVO> snapshots = _snapshotStoreDao.listBySnapshotIdAndState(snapshotId, State.Ready);
133+
if (res || CollectionUtils.isEmpty(snapshots)) {
134+
updateSnapshotToDestroyed(snapshotVO);
135+
return true;
136+
}
132137
return res;
133138
}
134139

135-
private void markSnapshotAsDestroyedIfAlreadyRemoved(Long snapshotId, SpApiResponse resp) {
136-
if (resp.getError().getName().equals("objectDoesNotExist")) {
137-
SnapshotDataStoreVO snapshotOnPrimary = _snapshotStoreDao.findBySourceSnapshot(snapshotId, DataStoreRole.Primary);
138-
if (snapshotOnPrimary != null) {
139-
snapshotOnPrimary.setState(State.Destroyed);
140-
_snapshotStoreDao.update(snapshotOnPrimary.getId(), snapshotOnPrimary);
140+
private void markSnapshotAsDestroyedIfAlreadyRemoved(Long snapshotId, boolean isSnapshotDeleted) {
141+
if (!isSnapshotDeleted) {
142+
return;
143+
}
144+
List<SnapshotDataStoreVO> snapshotsOnStore = _snapshotStoreDao.listBySnapshotIdAndState(snapshotId, State.Ready);
145+
for (SnapshotDataStoreVO snapshot : snapshotsOnStore) {
146+
if (snapshot.getInstallPath() != null && snapshot.getInstallPath().contains(StorPoolUtil.SP_DEV_PATH)) {
147+
snapshot.setState(State.Destroyed);
148+
_snapshotStoreDao.update(snapshot.getId(), snapshot);
141149
}
142150
}
143151
}

0 commit comments

Comments
 (0)