Skip to content

Commit 2c28f02

Browse files
committed
fix snapshot deletion
1 parent be254ea commit 2c28f02

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

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

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.apache.cloudstack.engine.subsystem.api.storage.DataObject;
3737
import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
3838
import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager;
39+
import org.apache.cloudstack.engine.subsystem.api.storage.ObjectInDataStoreStateMachine;
3940
import org.apache.cloudstack.engine.subsystem.api.storage.ObjectInDataStoreStateMachine.Event;
4041
import org.apache.cloudstack.engine.subsystem.api.storage.ObjectInDataStoreStateMachine.State;
4142
import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotDataFactory;
@@ -154,35 +155,49 @@ public boolean deleteSnapshot(Long snapshotId, Long zoneId) {
154155

155156
private boolean deleteSnapshot(Long snapshotId, Long zoneId, SnapshotVO snapshotVO, String name, StoragePoolVO storage) {
156157

157-
boolean res;
158+
boolean res = false;
158159
SpConnectionDesc conn = StorPoolUtil.getSpConnection(storage.getUuid(), storage.getId(), storagePoolDetailsDao, _primaryDataStoreDao);
159160
SpApiResponse resp = StorPoolUtil.snapshotDelete(name, conn);
161+
List<SnapshotInfo> snapshotInfos = snapshotDataFactory.getSnapshots(snapshotId, zoneId);
162+
processResult(snapshotInfos, ObjectInDataStoreStateMachine.Event.DestroyRequested);
160163
if (resp.getError() != null) {
161164
if (resp.getError().getDescr().contains("still exported")) {
165+
processResult(snapshotInfos, Event.OperationFailed);
162166
throw new CloudRuntimeException(String.format("The snapshot [%s] was exported to another cluster. [%s]", name, resp.getError()));
163167
}
164168
final String err = String.format("Failed to clean-up Storpool snapshot %s. Error: %s", name, resp.getError());
165169
StorPoolUtil.spLog(err);
166170
if (resp.getError().getName().equals("objectDoesNotExist")) {
167-
markSnapshotAsDestroyedIfAlreadyRemoved(snapshotId, storage.getId());
171+
return true;
168172
}
169-
res = false;
170173
} else {
171-
markSnapshotAsDestroyedIfAlreadyRemoved(snapshotId, storage.getId());
172174
res = deleteSnapshotFromDbIfNeeded(snapshotVO, zoneId);
173175
StorPoolUtil.spLog("StorpoolSnapshotStrategy.deleteSnapshot: executed successfully=%s, snapshot uuid=%s, name=%s", res, snapshotVO.getUuid(), name);
174176
}
177+
if (res) {
178+
processResult(snapshotInfos, Event.OperationSuccessed);
179+
cleanUpDestroyedRecords(snapshotId);
180+
} else {
181+
processResult(snapshotInfos, Event.OperationFailed);
182+
}
175183
return res;
176184
}
177185

178-
private void markSnapshotAsDestroyedIfAlreadyRemoved(Long snapshotId, Long storeId) {
179-
SnapshotDataStoreVO snapshotOnPrimary = _snapshotStoreDao.findByStoreSnapshot(DataStoreRole.Primary, storeId, snapshotId);
180-
if (snapshotOnPrimary != null) {
181-
snapshotOnPrimary.setState(State.Destroyed);
182-
_snapshotStoreDao.update(snapshotOnPrimary.getId(), snapshotOnPrimary);
186+
private void cleanUpDestroyedRecords(Long snapshotId) {
187+
List<SnapshotDataStoreVO> snapshots = _snapshotStoreDao.listBySnapshotId(snapshotId);
188+
for (SnapshotDataStoreVO snapshot : snapshots) {
189+
if (snapshot.getInstallPath().contains("/dev/storpool-byid") && State.Destroyed.equals(snapshot.getState())) {
190+
_snapshotStoreDao.remove(snapshot.getId());
191+
}
183192
}
184193
}
185194

195+
private void processResult(List<SnapshotInfo> snapshotInfos, ObjectInDataStoreStateMachine.Event event) {
196+
for (SnapshotInfo snapshot : snapshotInfos) {
197+
SnapshotObject snapshotObject = (SnapshotObject) snapshot;
198+
snapshotObject.processEvent(event);
199+
}
200+
}
186201
@Override
187202
public StrategyPriority canHandle(Snapshot snapshot, Long zoneId, SnapshotOperation op) {
188203
logger.debug(String.format("StorpoolSnapshotStrategy.canHandle: snapshot=%s, uuid=%s, op=%s", snapshot.getName(), snapshot.getUuid(), op));
@@ -195,9 +210,15 @@ public StrategyPriority canHandle(Snapshot snapshot, Long zoneId, SnapshotOperat
195210
return StrategyPriority.CANT_HANDLE;
196211
}
197212
List<SnapshotJoinVO> snapshots = snapshotJoinDao.listBySnapshotIdAndZoneId(zoneId, snapshot.getId());
198-
boolean snapshotNotOnStorPool = snapshots.stream().filter(s -> s.getStoreRole().equals(DataStoreRole.Primary)).count() == 0;
213+
boolean snapshotNotOnStorPool = snapshots.stream().filter(s -> DataStoreRole.Primary.equals(s.getStoreRole())).count() == 0;
199214

200215
if (snapshotNotOnStorPool) {
216+
for (SnapshotJoinVO snapshotOnStore : snapshots) {
217+
SnapshotDataStoreVO snap = _snapshotStoreDao.findOneBySnapshotAndDatastoreRole(snapshot.getId(), DataStoreRole.Image);
218+
if (snap != null && snap.getInstallPath() != null && snap.getInstallPath().startsWith(StorPoolUtil.SP_DEV_PATH)) {
219+
return StrategyPriority.HIGHEST;
220+
}
221+
}
201222
return StrategyPriority.CANT_HANDLE;
202223
}
203224
for (StoragePoolVO pool : pools) {

0 commit comments

Comments
 (0)