1818
1919import javax .inject .Inject ;
2020
21- import org .apache .cloudstack .engine .subsystem .api .storage .DataStore ;
22- import org .apache .cloudstack .engine .subsystem .api .storage .DataStoreManager ;
21+ import org .apache .cloudstack .engine .subsystem .api .storage .*;
2322import org .apache .cloudstack .engine .subsystem .api .storage .ObjectInDataStoreStateMachine .Event ;
2423import org .apache .cloudstack .engine .subsystem .api .storage .ObjectInDataStoreStateMachine .State ;
25- import org .apache .cloudstack .engine .subsystem .api .storage .SnapshotDataFactory ;
26- import org .apache .cloudstack .engine .subsystem .api .storage .SnapshotInfo ;
27- import org .apache .cloudstack .engine .subsystem .api .storage .SnapshotService ;
2824import org .apache .cloudstack .storage .command .CreateObjectAnswer ;
2925import org .apache .cloudstack .storage .datastore .db .SnapshotDataStoreDao ;
3026import org .apache .cloudstack .storage .datastore .db .SnapshotDataStoreVO ;
@@ -65,7 +61,8 @@ public class XenserverSnapshotStrategy extends SnapshotStrategyBase {
6561 @ Override
6662 public SnapshotInfo backupSnapshot (SnapshotInfo snapshot ) {
6763 SnapshotInfo parentSnapshot = snapshot .getParent ();
68- if (parentSnapshot != null && parentSnapshot .getPath ().equalsIgnoreCase (snapshot .getPath ())) {
64+
65+ if (parentSnapshot != null && snapshot .getPath ().equalsIgnoreCase (parentSnapshot .getPath ())) {
6966 s_logger .debug ("backup an empty snapshot" );
7067 // don't need to backup this snapshot
7168 SnapshotDataStoreVO parentSnapshotOnBackupStore = this .snapshotStoreDao .findBySnapshot (
@@ -79,6 +76,7 @@ public SnapshotInfo backupSnapshot(SnapshotInfo snapshot) {
7976
8077 SnapshotObjectTO snapTO = new SnapshotObjectTO ();
8178 snapTO .setPath (parentSnapshotOnBackupStore .getInstallPath ());
79+
8280 CreateObjectAnswer createSnapshotAnswer = new CreateObjectAnswer (snapTO );
8381
8482 snapshotOnImageStore .processEvent (Event .OperationSuccessed , createSnapshotAnswer );
@@ -109,7 +107,9 @@ public SnapshotInfo backupSnapshot(SnapshotInfo snapshot) {
109107 for (i = 1 ; i < deltaSnap ; i ++) {
110108 parentSnapshotOnBackupStore = this .snapshotStoreDao .findBySnapshot (parentSnapshot .getId (),
111109 DataStoreRole .Image );
112-
110+ if (parentSnapshotOnBackupStore == null ) {
111+ break ;
112+ }
113113 Long prevBackupId = parentSnapshotOnBackupStore .getParentSnapshotId ();
114114
115115 if (prevBackupId == 0 ) {
@@ -193,8 +193,39 @@ public boolean deleteSnapshot(Long snapshotId) {
193193
194194 @ Override
195195 public SnapshotInfo takeSnapshot (SnapshotInfo snapshot ) {
196- snapshot = snapshotSvr .takeSnapshot (snapshot ).getSnashot ();
197- return this .backupSnapshot (snapshot );
196+ SnapshotResult result = snapshotSvr .takeSnapshot (snapshot );
197+ if (result .isFailed ()) {
198+ s_logger .debug ("Failed to take snapshot: " + result .getResult ());
199+ throw new CloudRuntimeException (result .getResult ());
200+ }
201+ snapshot = result .getSnashot ();
202+ DataStore primaryStore = snapshot .getDataStore ();
203+
204+ SnapshotInfo backupedSnapshot = this .backupSnapshot (snapshot );
205+ try {
206+ SnapshotInfo parent = snapshot .getParent ();
207+ if (backupedSnapshot != null && parent != null ) {
208+ Long parentSnapshotId = parent .getId ();
209+ while (parentSnapshotId != null && parentSnapshotId != 0L ) {
210+ SnapshotDataStoreVO snapshotDataStoreVO = snapshotStoreDao .findByStoreSnapshot (primaryStore .getRole (),primaryStore .getId (), parentSnapshotId );
211+ if (snapshotDataStoreVO != null ) {
212+ parentSnapshotId = snapshotDataStoreVO .getParentSnapshotId ();
213+ snapshotStoreDao .remove (snapshotDataStoreVO .getId ());
214+ } else {
215+ parentSnapshotId = null ;
216+ }
217+ }
218+ SnapshotDataStoreVO snapshotDataStoreVO = snapshotStoreDao .findByStoreSnapshot (primaryStore .getRole (), primaryStore .getId (),
219+ snapshot .getId ());
220+ if (snapshotDataStoreVO != null ) {
221+ snapshotDataStoreVO .setParentSnapshotId (0L );
222+ snapshotStoreDao .update (snapshotDataStoreVO .getId (), snapshotDataStoreVO );
223+ }
224+ }
225+ } catch (Exception e ) {
226+ s_logger .debug ("Failed to clean up snapshots on primary storage" , e );
227+ }
228+ return backupedSnapshot ;
198229 }
199230
200231 @ Override
0 commit comments