9696import com .cloud .storage .VolumeDetailVO ;
9797import com .cloud .storage .VolumeVO ;
9898import com .cloud .storage .dao .SnapshotDetailsDao ;
99- import com .cloud .storage .dao .SnapshotDetailsVO ;
10099import com .cloud .storage .dao .StoragePoolHostDao ;
101100import com .cloud .storage .dao .VMTemplateDetailsDao ;
102101import com .cloud .storage .dao .VolumeDao ;
@@ -132,9 +131,9 @@ public class StorPoolPrimaryDataStoreDriver implements PrimaryDataStoreDriver {
132131 @ Inject
133132 private HostDao hostDao ;
134133 @ Inject
135- private ResourceTagDao _resourceTagDao ;
134+ private ResourceTagDao resourceTagDao ;
136135 @ Inject
137- private SnapshotDetailsDao _snapshotDetailsDao ;
136+ private SnapshotDetailsDao snapshotDetailsDao ;
138137 @ Inject
139138 private SnapshotDataStoreDao snapshotDataStoreDao ;
140139 @ Inject
@@ -464,7 +463,7 @@ public void copyAsync(DataObject srcData, DataObject dstData, AsyncCompletionCal
464463 try {
465464 if (srcType == DataObjectType .SNAPSHOT && dstType == DataObjectType .VOLUME ) {
466465 SnapshotInfo sinfo = (SnapshotInfo )srcData ;
467- final String snapshotName = StorPoolHelper .getSnapshotName (srcData .getId (), srcData .getUuid (), snapshotDataStoreDao , _snapshotDetailsDao );
466+ final String snapshotName = StorPoolHelper .getSnapshotName (srcData .getId (), srcData .getUuid (), snapshotDataStoreDao , snapshotDetailsDao );
468467
469468 VolumeInfo vinfo = (VolumeInfo )dstData ;
470469 final String volumeName = vinfo .getUuid ();
@@ -485,55 +484,14 @@ public void copyAsync(DataObject srcData, DataObject dstData, AsyncCompletionCal
485484 StorPoolUtil .spLog ("Snapshot %s does not exists on StorPool, will try to create a volume from a snopshot on secondary storage" , snapshotName );
486485 SnapshotDataStoreVO snap = getSnapshotImageStoreRef (sinfo .getId (), vinfo .getDataCenterId ());
487486 if (snap != null && StorPoolStorageAdaptor .getVolumeNameFromPath (snap .getInstallPath (), false ) == null ) {
488- resp = StorPoolUtil .volumeCreate (srcData .getUuid (), null , size , null , "no" , "snapshot" , sinfo .getBaseVolume ().getMaxIops (), conn );
489- if (resp .getError () == null ) {
490- VolumeObjectTO dstTO = (VolumeObjectTO ) dstData .getTO ();
491- dstTO .setSize (size );
492- dstTO .setPath (StorPoolUtil .devPath (StorPoolUtil .getNameFromResponse (resp , false )));
493- cmd = new StorPoolDownloadTemplateCommand (srcData .getTO (), dstTO , StorPoolHelper .getTimeout (StorPoolHelper .PrimaryStorageDownloadWait , configDao ), VirtualMachineManager .ExecuteInSequence .value (), "volume" );
494-
495- EndPoint ep = selector .select (srcData , dstData );
496- if (ep == null ) {
497- err = "No remote endpoint to send command, check if host or ssvm is down?" ;
498- } else {
499- answer = ep .sendMessage (cmd );
500- }
501-
502- if (answer != null && answer .getResult ()) {
503- SpApiResponse resp2 = StorPoolUtil .volumeFreeze (StorPoolUtil .getNameFromResponse (resp , true ), conn );
504- if (resp2 .getError () != null ) {
505- err = String .format ("Could not freeze Storpool volume %s. Error: %s" , srcData .getUuid (), resp2 .getError ());
506- } else {
507- String name = StorPoolUtil .getNameFromResponse (resp , false );
508- SnapshotDetailsVO snapshotDetails = _snapshotDetailsDao .findDetail (sinfo .getId (), sinfo .getUuid ());
509- if (snapshotDetails != null ) {
510- StorPoolHelper .updateSnapshotDetailsValue (snapshotDetails .getId (), StorPoolUtil .devPath (name ), "snapshot" );
511- }else {
512- StorPoolHelper .addSnapshotDetails (sinfo .getId (), sinfo .getUuid (), StorPoolUtil .devPath (name ), _snapshotDetailsDao );
513- }
514- resp = StorPoolUtil .volumeCreate (volumeName , StorPoolUtil .getNameFromResponse (resp , true ), size , null , null , "volume" , sinfo .getBaseVolume ().getMaxIops (), conn );
515- if (resp .getError () == null ) {
516- updateStoragePool (dstData .getDataStore ().getId (), size );
517-
518- VolumeObjectTO to = (VolumeObjectTO ) dstData .getTO ();
519- to .setPath (StorPoolUtil .devPath (StorPoolUtil .getNameFromResponse (resp , false )));
520- to .setSize (size );
521- // successfully downloaded snapshot to primary storage
522- answer = new CopyCmdAnswer (to );
523- StorPoolUtil .spLog ("Created volume=%s with uuid=%s from snapshot=%s with uuid=%s" , name , to .getUuid (), snapshotName , sinfo .getUuid ());
524-
525- } else {
526- err = String .format ("Could not create Storpool volume %s from snapshot %s. Error: %s" , volumeName , snapshotName , resp .getError ());
527- }
528- }
529- } else {
530- err = answer != null ? answer .getDetails () : "Unknown error while downloading template. Null answer returned." ;
531- }
487+ SpApiResponse emptyVolumeCreateResp = StorPoolUtil .volumeCreate (volumeName , null , size , null , null , "volume" , null , conn );
488+ if (emptyVolumeCreateResp .getError () == null ) {
489+ answer = createVolumeFromSnapshot (srcData , dstData , size , emptyVolumeCreateResp );
532490 } else {
533- err = String .format ("Could not create Storpool volume %s from snapshot %s. Error: %s" , volumeName , snapshotName , resp .getError ());
491+ answer = new Answer ( cmd , false , String .format ("Could not create Storpool volume %s from snapshot %s. Error: %s" , volumeName , snapshotName , emptyVolumeCreateResp .getError () ));
534492 }
535493 } else {
536- err = String .format ("The snapshot %s does not exists neither on primary, neither on secondary storage. Cannot create volume from snapshot" , snapshotName );
494+ answer = new Answer ( cmd , false , String .format ("The snapshot %s does not exists neither on primary, neither on secondary storage. Cannot create volume from snapshot" , snapshotName ) );
537495 }
538496 } else {
539497 err = String .format ("Could not create Storpool volume %s from snapshot %s. Error: %s" , volumeName , snapshotName , resp .getError ());
@@ -632,22 +590,17 @@ public void copyAsync(DataObject srcData, DataObject dstData, AsyncCompletionCal
632590 err = String .format ("Could not create Storpool volume for CS template %s. Error: %s" , name , resp .getError ());
633591 } else {
634592 String volumeNameToSnapshot = StorPoolUtil .getNameFromResponse (resp , true );
635- SpApiResponse resp2 = StorPoolUtil .volumeFreeze (volumeNameToSnapshot , conn );
636- if (resp2 .getError () != null ) {
637- err = String .format ("Could not freeze Storpool volume %s. Error: %s" , name , resp2 .getError ());
638- } else {
639- StorPoolUtil .spLog ("Storpool snapshot [%s] for a template exists. Creating template on Storpool with name [%s]" , tinfo .getUuid (), name );
640- TemplateObjectTO dstTO = (TemplateObjectTO ) dstData .getTO ();
641- dstTO .setPath (StorPoolUtil .devPath (StorPoolUtil .getNameFromResponse (resp , false )));
642- dstTO .setSize (size );
643- answer = new CopyCmdAnswer (dstTO );
644- }
593+ TemplateObjectTO dstTO = (TemplateObjectTO ) dstData .getTO ();
594+
595+ answer = createVolumeSnapshot (cmd , size , conn , volumeNameToSnapshot , dstTO );
596+ StorPoolUtil .volumeDelete (volumeNameToSnapshot , conn );
645597 }
646598 } else {
647599 resp = StorPoolUtil .volumeCreate (name , null , size , null , "no" , "template" , null , conn );
648600 if (resp .getError () != null ) {
649601 err = String .format ("Could not create Storpool volume for CS template %s. Error: %s" , name , resp .getError ());
650602 } else {
603+ String volName = StorPoolUtil .getNameFromResponse (resp , true );
651604 TemplateObjectTO dstTO = (TemplateObjectTO )dstData .getTO ();
652605 dstTO .setPath (StorPoolUtil .devPath (StorPoolUtil .getNameFromResponse (resp , false )));
653606 dstTO .setSize (size );
@@ -663,19 +616,11 @@ public void copyAsync(DataObject srcData, DataObject dstData, AsyncCompletionCal
663616
664617 if (answer != null && answer .getResult ()) {
665618 // successfully downloaded template to primary storage
666- SpApiResponse resp2 = StorPoolUtil .volumeFreeze (StorPoolUtil .getNameFromResponse (resp , true ), conn );
667- if (resp2 .getError () != null ) {
668- err = String .format ("Could not freeze Storpool volume %s. Error: %s" , name , resp2 .getError ());
669- }
619+ answer = createVolumeSnapshot (cmd , size , conn , volName , dstTO );
670620 } else {
671621 err = answer != null ? answer .getDetails () : "Unknown error while downloading template. Null answer returned." ;
672622 }
673- }
674- }
675- if (err != null ) {
676- resp = StorPoolUtil .volumeDelete (StorPoolUtil .getNameFromResponse (resp , true ), conn );
677- if (resp .getError () != null ) {
678- log .warn (String .format ("Could not clean-up Storpool volume %s. Error: %s" , name , resp .getError ()));
623+ StorPoolUtil .volumeDelete (volName , conn );
679624 }
680625 }
681626 } else if (srcType == DataObjectType .TEMPLATE && dstType == DataObjectType .VOLUME ) {
@@ -846,6 +791,42 @@ public void copyAsync(DataObject srcData, DataObject dstData, AsyncCompletionCal
846791 callback .complete (res );
847792 }
848793
794+ private Answer createVolumeSnapshot (StorageSubSystemCommand cmd , Long size , SpConnectionDesc conn ,
795+ String volName , TemplateObjectTO dstTO ) {
796+ Answer answer ;
797+ SpApiResponse resp2 = StorPoolUtil .volumeSnapshot (volName , dstTO .getUuid (), null , "template" , null , conn );
798+ if (resp2 .getError () != null ) {
799+ answer = new Answer (cmd , false , String .format ("Could not snapshot volume. Error: %s" , resp2 .getError ()));
800+ } else {
801+ dstTO .setPath (StorPoolUtil .devPath (
802+ StorPoolUtil .getSnapshotNameFromResponse (resp2 , false , StorPoolUtil .GLOBAL_ID )));
803+ dstTO .setSize (size );
804+ answer = new CopyCmdAnswer (dstTO );
805+ }
806+ return answer ;
807+ }
808+
809+ private Answer createVolumeFromSnapshot (DataObject srcData , DataObject dstData , final Long size ,
810+ SpApiResponse emptyVolumeCreateResp ) {
811+ Answer answer ;
812+ String name = StorPoolUtil .getNameFromResponse (emptyVolumeCreateResp , false );
813+ VolumeObjectTO dstTO = (VolumeObjectTO ) dstData .getTO ();
814+ dstTO .setSize (size );
815+ dstTO .setPath (StorPoolUtil .devPath (name ));
816+ StorageSubSystemCommand cmd1 = new StorPoolDownloadTemplateCommand (srcData .getTO (), dstTO , StorPoolHelper .getTimeout (StorPoolHelper .PrimaryStorageDownloadWait , configDao ), VirtualMachineManager .ExecuteInSequence .value (), "volume" );
817+
818+ EndPoint ep = selector .select (srcData , dstData );
819+ if (ep == null ) {
820+ answer = new Answer (cmd1 , false , "\" No remote endpoint to send command, check if host or ssvm is down?\" " );
821+ } else {
822+ answer = ep .sendMessage (cmd1 );
823+ }
824+ if (answer == null || !answer .getResult ()) {
825+ answer = new Answer (cmd1 , false , answer != null ? answer .getDetails () : "Unknown error while downloading template. Null answer returned." );
826+ }
827+ return answer ;
828+ }
829+
849830 private void updateVolumePoolType (VolumeInfo vinfo ) {
850831 VolumeVO volumeVO = volumeDao .findById (vinfo .getId ());
851832 volumeVO .setPoolType (StoragePoolType .StorPool );
@@ -977,9 +958,9 @@ public void takeSnapshot(SnapshotInfo snapshot, AsyncCompletionCallback<CreateCm
977958 SnapshotObjectTO snapTo = (SnapshotObjectTO )snapshot .getTO ();
978959 snapTo .setPath (StorPoolUtil .devPath (name .split ("~" )[1 ]));
979960 answer = new CreateObjectAnswer (snapTo );
980- StorPoolHelper .addSnapshotDetails (snapshot .getId (), snapshot .getUuid (), snapTo .getPath (), _snapshotDetailsDao );
961+ StorPoolHelper .addSnapshotDetails (snapshot .getId (), snapshot .getUuid (), snapTo .getPath (), snapshotDetailsDao );
981962 //add primary storage of snapshot
982- StorPoolHelper .addSnapshotDetails (snapshot .getId (), StorPoolUtil .SP_STORAGE_POOL_ID , String .valueOf (snapshot .getDataStore ().getId ()), _snapshotDetailsDao );
963+ StorPoolHelper .addSnapshotDetails (snapshot .getId (), StorPoolUtil .SP_STORAGE_POOL_ID , String .valueOf (snapshot .getDataStore ().getId ()), snapshotDetailsDao );
983964 StorPoolUtil .spLog ("StorpoolPrimaryDataStoreDriverImpl.takeSnapshot: snapshot: name=%s, uuid=%s, volume: name=%s, uuid=%s" , name , snapshot .getUuid (), volumeName , vinfo .getUuid ());
984965 }
985966 } catch (Exception e ) {
@@ -994,7 +975,7 @@ public void takeSnapshot(SnapshotInfo snapshot, AsyncCompletionCallback<CreateCm
994975 @ Override
995976 public void revertSnapshot (final SnapshotInfo snapshot , final SnapshotInfo snapshotOnPrimaryStore , final AsyncCompletionCallback <CommandResult > callback ) {
996977 final VolumeInfo vinfo = snapshot .getBaseVolume ();
997- final String snapshotName = StorPoolHelper .getSnapshotName (snapshot .getId (), snapshot .getUuid (), snapshotDataStoreDao , _snapshotDetailsDao );
978+ final String snapshotName = StorPoolHelper .getSnapshotName (snapshot .getId (), snapshot .getUuid (), snapshotDataStoreDao , snapshotDetailsDao );
998979 final String volumeName = StorPoolStorageAdaptor .getVolumeNameFromPath (vinfo .getPath (), true );
999980 StorPoolUtil .spLog ("StorpoolPrimaryDataStoreDriverImpl.revertSnapshot: snapshot: name=%s, uuid=%s, volume: name=%s, uuid=%s" , snapshotName , snapshot .getUuid (), volumeName , vinfo .getUuid ());
1000981 String err = null ;
@@ -1049,7 +1030,7 @@ public void revertSnapshot(final SnapshotInfo snapshot, final SnapshotInfo snaps
10491030 }
10501031
10511032 private String getVcPolicyTag (Long vmId ) {
1052- ResourceTag resourceTag = vmId != null ? _resourceTagDao .findByKey (vmId , ResourceObjectType .UserVm , StorPoolUtil .SP_VC_POLICY ) : null ;
1033+ ResourceTag resourceTag = vmId != null ? resourceTagDao .findByKey (vmId , ResourceObjectType .UserVm , StorPoolUtil .SP_VC_POLICY ) : null ;
10531034 return resourceTag != null ? resourceTag .getValue () : "" ;
10541035 }
10551036
0 commit comments