3737import org .apache .cloudstack .engine .subsystem .api .storage .DataObject ;
3838import org .apache .cloudstack .engine .subsystem .api .storage .DataStore ;
3939import org .apache .cloudstack .engine .subsystem .api .storage .DataStoreManager ;
40+ import org .apache .cloudstack .engine .subsystem .api .storage .ObjectInDataStoreStateMachine ;
4041import org .apache .cloudstack .engine .subsystem .api .storage .ObjectInDataStoreStateMachine .Event ;
4142import org .apache .cloudstack .engine .subsystem .api .storage .ObjectInDataStoreStateMachine .State ;
4243import org .apache .cloudstack .engine .subsystem .api .storage .SnapshotDataFactory ;
@@ -157,35 +158,49 @@ public boolean deleteSnapshot(Long snapshotId, Long zoneId) {
157158
158159 private boolean deleteSnapshot (Long snapshotId , Long zoneId , SnapshotVO snapshotVO , String name , StoragePoolVO storage ) {
159160
160- boolean res ;
161+ boolean res = false ;
161162 SpConnectionDesc conn = StorPoolUtil .getSpConnection (storage .getUuid (), storage .getId (), storagePoolDetailsDao , _primaryDataStoreDao );
162163 SpApiResponse resp = StorPoolUtil .snapshotDelete (name , conn );
164+ List <SnapshotInfo > snapshotInfos = snapshotDataFactory .getSnapshots (snapshotId , zoneId );
165+ processResult (snapshotInfos , ObjectInDataStoreStateMachine .Event .DestroyRequested );
163166 if (resp .getError () != null ) {
164167 if (resp .getError ().getDescr ().contains ("still exported" )) {
168+ processResult (snapshotInfos , Event .OperationFailed );
165169 throw new CloudRuntimeException (String .format ("The snapshot [%s] was exported to another cluster. [%s]" , name , resp .getError ()));
166170 }
167171 final String err = String .format ("Failed to clean-up Storpool snapshot %s. Error: %s" , name , resp .getError ());
168172 StorPoolUtil .spLog (err );
169173 if (resp .getError ().getName ().equals ("objectDoesNotExist" )) {
170- markSnapshotAsDestroyedIfAlreadyRemoved ( snapshotId , storage . getId ()) ;
174+ return true ;
171175 }
172- res = false ;
173176 } else {
174- markSnapshotAsDestroyedIfAlreadyRemoved (snapshotId , storage .getId ());
175177 res = deleteSnapshotFromDbIfNeeded (snapshotVO , zoneId );
176178 StorPoolUtil .spLog ("StorpoolSnapshotStrategy.deleteSnapshot: executed successfully=%s, snapshot uuid=%s, name=%s" , res , snapshotVO .getUuid (), name );
177179 }
180+ if (res ) {
181+ processResult (snapshotInfos , Event .OperationSuccessed );
182+ cleanUpDestroyedRecords (snapshotId );
183+ } else {
184+ processResult (snapshotInfos , Event .OperationFailed );
185+ }
178186 return res ;
179187 }
180188
181- private void markSnapshotAsDestroyedIfAlreadyRemoved (Long snapshotId , Long storeId ) {
182- SnapshotDataStoreVO snapshotOnPrimary = _snapshotStoreDao .findByStoreSnapshot (DataStoreRole .Primary , storeId , snapshotId );
183- if (snapshotOnPrimary != null ) {
184- snapshotOnPrimary .setState (State .Destroyed );
185- _snapshotStoreDao .update (snapshotOnPrimary .getId (), snapshotOnPrimary );
189+ private void cleanUpDestroyedRecords (Long snapshotId ) {
190+ List <SnapshotDataStoreVO > snapshots = _snapshotStoreDao .listBySnapshotId (snapshotId );
191+ for (SnapshotDataStoreVO snapshot : snapshots ) {
192+ if (snapshot .getInstallPath ().contains ("/dev/storpool-byid" ) && State .Destroyed .equals (snapshot .getState ())) {
193+ _snapshotStoreDao .remove (snapshot .getId ());
194+ }
186195 }
187196 }
188197
198+ private void processResult (List <SnapshotInfo > snapshotInfos , ObjectInDataStoreStateMachine .Event event ) {
199+ for (SnapshotInfo snapshot : snapshotInfos ) {
200+ SnapshotObject snapshotObject = (SnapshotObject ) snapshot ;
201+ snapshotObject .processEvent (event );
202+ }
203+ }
189204 @ Override
190205 public StrategyPriority canHandle (Snapshot snapshot , Long zoneId , SnapshotOperation op ) {
191206 logger .debug ("StorpoolSnapshotStrategy.canHandle: snapshot {}, op={}" , snapshot , op );
@@ -198,9 +213,15 @@ public StrategyPriority canHandle(Snapshot snapshot, Long zoneId, SnapshotOperat
198213 return StrategyPriority .CANT_HANDLE ;
199214 }
200215 List <SnapshotJoinVO > snapshots = snapshotJoinDao .listBySnapshotIdAndZoneId (zoneId , snapshot .getId ());
201- boolean snapshotNotOnStorPool = snapshots .stream ().filter (s -> s . getStoreRole () .equals (DataStoreRole . Primary )).count () == 0 ;
216+ boolean snapshotNotOnStorPool = snapshots .stream ().filter (s -> DataStoreRole . Primary .equals (s . getStoreRole () )).count () == 0 ;
202217
203218 if (snapshotNotOnStorPool ) {
219+ for (SnapshotJoinVO snapshotOnStore : snapshots ) {
220+ SnapshotDataStoreVO snap = _snapshotStoreDao .findOneBySnapshotAndDatastoreRole (snapshot .getId (), DataStoreRole .Image );
221+ if (snap != null && snap .getInstallPath () != null && snap .getInstallPath ().startsWith (StorPoolUtil .SP_DEV_PATH )) {
222+ return StrategyPriority .HIGHEST ;
223+ }
224+ }
204225 return StrategyPriority .CANT_HANDLE ;
205226 }
206227 for (StoragePoolVO pool : pools ) {
0 commit comments