3636import org .apache .cloudstack .engine .subsystem .api .storage .DataObject ;
3737import org .apache .cloudstack .engine .subsystem .api .storage .DataStore ;
3838import org .apache .cloudstack .engine .subsystem .api .storage .DataStoreManager ;
39+ import org .apache .cloudstack .engine .subsystem .api .storage .ObjectInDataStoreStateMachine ;
3940import org .apache .cloudstack .engine .subsystem .api .storage .ObjectInDataStoreStateMachine .Event ;
4041import org .apache .cloudstack .engine .subsystem .api .storage .ObjectInDataStoreStateMachine .State ;
4142import org .apache .cloudstack .engine .subsystem .api .storage .SnapshotDataFactory ;
@@ -155,35 +156,49 @@ public boolean deleteSnapshot(Long snapshotId, Long zoneId) {
155156
156157 private boolean deleteSnapshot (Long snapshotId , Long zoneId , SnapshotVO snapshotVO , String name , StoragePoolVO storage ) {
157158
158- boolean res ;
159+ boolean res = false ;
159160 SpConnectionDesc conn = StorPoolUtil .getSpConnection (storage .getUuid (), storage .getId (), storagePoolDetailsDao , _primaryDataStoreDao );
160161 SpApiResponse resp = StorPoolUtil .snapshotDelete (name , conn );
162+ List <SnapshotInfo > snapshotInfos = snapshotDataFactory .getSnapshots (snapshotId , zoneId );
163+ processResult (snapshotInfos , ObjectInDataStoreStateMachine .Event .DestroyRequested );
161164 if (resp .getError () != null ) {
162165 if (resp .getError ().getDescr ().contains ("still exported" )) {
166+ processResult (snapshotInfos , Event .OperationFailed );
163167 throw new CloudRuntimeException (String .format ("The snapshot [%s] was exported to another cluster. [%s]" , name , resp .getError ()));
164168 }
165169 final String err = String .format ("Failed to clean-up Storpool snapshot %s. Error: %s" , name , resp .getError ());
166170 StorPoolUtil .spLog (err );
167171 if (resp .getError ().getName ().equals ("objectDoesNotExist" )) {
168- markSnapshotAsDestroyedIfAlreadyRemoved ( snapshotId , storage . getId ()) ;
172+ return true ;
169173 }
170- res = false ;
171174 } else {
172- markSnapshotAsDestroyedIfAlreadyRemoved (snapshotId , storage .getId ());
173175 res = deleteSnapshotFromDbIfNeeded (snapshotVO , zoneId );
174176 StorPoolUtil .spLog ("StorpoolSnapshotStrategy.deleteSnapshot: executed successfully=%s, snapshot uuid=%s, name=%s" , res , snapshotVO .getUuid (), name );
175177 }
178+ if (res ) {
179+ processResult (snapshotInfos , Event .OperationSuccessed );
180+ cleanUpDestroyedRecords (snapshotId );
181+ } else {
182+ processResult (snapshotInfos , Event .OperationFailed );
183+ }
176184 return res ;
177185 }
178186
179- private void markSnapshotAsDestroyedIfAlreadyRemoved (Long snapshotId , Long storeId ) {
180- SnapshotDataStoreVO snapshotOnPrimary = _snapshotStoreDao .findByStoreSnapshot (DataStoreRole .Primary , storeId , snapshotId );
181- if (snapshotOnPrimary != null ) {
182- snapshotOnPrimary .setState (State .Destroyed );
183- _snapshotStoreDao .update (snapshotOnPrimary .getId (), snapshotOnPrimary );
187+ private void cleanUpDestroyedRecords (Long snapshotId ) {
188+ List <SnapshotDataStoreVO > snapshots = _snapshotStoreDao .listBySnapshotId (snapshotId );
189+ for (SnapshotDataStoreVO snapshot : snapshots ) {
190+ if (snapshot .getInstallPath ().contains ("/dev/storpool-byid" ) && State .Destroyed .equals (snapshot .getState ())) {
191+ _snapshotStoreDao .remove (snapshot .getId ());
192+ }
184193 }
185194 }
186195
196+ private void processResult (List <SnapshotInfo > snapshotInfos , ObjectInDataStoreStateMachine .Event event ) {
197+ for (SnapshotInfo snapshot : snapshotInfos ) {
198+ SnapshotObject snapshotObject = (SnapshotObject ) snapshot ;
199+ snapshotObject .processEvent (event );
200+ }
201+ }
187202 @ Override
188203 public StrategyPriority canHandle (Snapshot snapshot , Long zoneId , SnapshotOperation op ) {
189204 logger .debug (String .format ("StorpoolSnapshotStrategy.canHandle: snapshot=%s, uuid=%s, op=%s" , snapshot .getName (), snapshot .getUuid (), op ));
@@ -196,9 +211,15 @@ public StrategyPriority canHandle(Snapshot snapshot, Long zoneId, SnapshotOperat
196211 return StrategyPriority .CANT_HANDLE ;
197212 }
198213 List <SnapshotJoinVO > snapshots = snapshotJoinDao .listBySnapshotIdAndZoneId (zoneId , snapshot .getId ());
199- boolean snapshotNotOnStorPool = snapshots .stream ().filter (s -> s . getStoreRole () .equals (DataStoreRole . Primary )).count () == 0 ;
214+ boolean snapshotNotOnStorPool = snapshots .stream ().filter (s -> DataStoreRole . Primary .equals (s . getStoreRole () )).count () == 0 ;
200215
201216 if (snapshotNotOnStorPool ) {
217+ for (SnapshotJoinVO snapshotOnStore : snapshots ) {
218+ SnapshotDataStoreVO snap = _snapshotStoreDao .findOneBySnapshotAndDatastoreRole (snapshot .getId (), DataStoreRole .Image );
219+ if (snap != null && snap .getInstallPath () != null && snap .getInstallPath ().startsWith (StorPoolUtil .SP_DEV_PATH )) {
220+ return StrategyPriority .HIGHEST ;
221+ }
222+ }
202223 return StrategyPriority .CANT_HANDLE ;
203224 }
204225 for (StoragePoolVO pool : pools ) {
0 commit comments