1818 */
1919package org .apache .cloudstack .storage .motion ;
2020
21+ import java .util .Date ;
2122import java .util .LinkedList ;
2223import java .util .List ;
2324import java .util .Map ;
2425
2526import javax .inject .Inject ;
2627
28+ import com .cloud .storage .Volume ;
29+ import com .cloud .storage .VolumeVO ;
30+ import com .cloud .storage .dao .VolumeDao ;
31+ import org .apache .log4j .Logger ;
2732import org .springframework .stereotype .Component ;
2833
2934import org .apache .cloudstack .engine .subsystem .api .storage .CopyCommandResult ;
4045import com .cloud .utils .StringUtils ;
4146import com .cloud .utils .exception .CloudRuntimeException ;
4247
48+
4349@ Component
4450public class DataMotionServiceImpl implements DataMotionService {
51+ private static final Logger LOGGER = Logger .getLogger (DataMotionServiceImpl .class );
52+
4553 @ Inject
4654 StorageStrategyFactory storageStrategyFactory ;
55+ @ Inject
56+ VolumeDao volDao ;
4757
4858 @ Override
4959 public void copyAsync (DataObject srcData , DataObject destData , Host destHost , AsyncCompletionCallback <CopyCommandResult > callback ) {
@@ -61,13 +71,33 @@ public void copyAsync(DataObject srcData, DataObject destData, Host destHost, As
6171
6272 DataMotionStrategy strategy = storageStrategyFactory .getDataMotionStrategy (srcData , destData );
6373 if (strategy == null ) {
74+ // OfflineVmware volume migration
75+ // Cleanup volumes from target and reset the state of volume at source
76+ cleanUpVolumesForFailedMigrations (srcData , destData );
6477 throw new CloudRuntimeException ("Can't find strategy to move data. " + "Source: " + srcData .getType ().name () + " '" + srcData .getUuid () + ", Destination: " +
65- destData .getType ().name () + " '" + destData .getUuid () + "'" );
78+ destData .getType ().name () + " '" + destData .getUuid () + "'" );
6679 }
6780
6881 strategy .copyAsync (srcData , destData , destHost , callback );
6982 }
7083
84+ /**
85+ * Offline Vmware volume migration
86+ * Cleanup volumes after failed migrations and reset state of source volume
87+ *
88+ * @param srcData
89+ * @param destData
90+ */
91+ private void cleanUpVolumesForFailedMigrations (DataObject srcData , DataObject destData ) {
92+ VolumeVO destinationVO = volDao .findById (destData .getId ());
93+ VolumeVO sourceVO = volDao .findById (srcData .getId ());
94+ sourceVO .setState (Volume .State .Ready );
95+ volDao .update (sourceVO .getId (), sourceVO );
96+ destinationVO .setState (Volume .State .Expunged );
97+ destinationVO .setRemoved (new Date ());
98+ volDao .update (destinationVO .getId (), destinationVO );
99+ }
100+
71101 @ Override
72102 public void copyAsync (DataObject srcData , DataObject destData , AsyncCompletionCallback <CopyCommandResult > callback ) {
73103 copyAsync (srcData , destData , null , callback );
@@ -84,7 +114,7 @@ public void copyAsync(Map<VolumeInfo, DataStore> volumeMap, VirtualMachineTO vmT
84114 }
85115
86116 throw new CloudRuntimeException ("Can't find strategy to move data. " + "Source Host: " + srcHost .getName () + ", Destination Host: " + destHost .getName () +
87- ", Volume UUIDs: " + StringUtils .join (volumeIds , "," ));
117+ ", Volume UUIDs: " + StringUtils .join (volumeIds , "," ));
88118 }
89119
90120 strategy .copyAsync (volumeMap , vmTo , srcHost , destHost , callback );
0 commit comments