1616// under the License.
1717package 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
2433import org .apache .cloudstack .engine .subsystem .api .storage .DataStore ;
2534import org .apache .cloudstack .engine .subsystem .api .storage .DataStoreManager ;
4049import org .apache .cloudstack .storage .datastore .util .StorPoolUtil .SpApiResponse ;
4150import org .apache .cloudstack .storage .datastore .util .StorPoolUtil .SpConnectionDesc ;
4251import org .apache .commons .collections .CollectionUtils ;
43- import org .apache .logging .log4j .Logger ;
4452import org .apache .logging .log4j .LogManager ;
53+ import org .apache .logging .log4j .Logger ;
4554import 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