9595import com .cloud .storage .VolumeDetailVO ;
9696import com .cloud .storage .VolumeVO ;
9797import com .cloud .storage .dao .SnapshotDetailsDao ;
98- import com .cloud .storage .dao .SnapshotDetailsVO ;
9998import com .cloud .storage .dao .StoragePoolHostDao ;
10099import com .cloud .storage .dao .VMTemplateDetailsDao ;
101100import com .cloud .storage .dao .VolumeDao ;
@@ -133,9 +132,9 @@ public class StorPoolPrimaryDataStoreDriver implements PrimaryDataStoreDriver {
133132 @ Inject
134133 private HostDao hostDao ;
135134 @ Inject
136- private ResourceTagDao _resourceTagDao ;
135+ private ResourceTagDao resourceTagDao ;
137136 @ Inject
138- private SnapshotDetailsDao _snapshotDetailsDao ;
137+ private SnapshotDetailsDao snapshotDetailsDao ;
139138 @ Inject
140139 private SnapshotDataStoreDao snapshotDataStoreDao ;
141140 @ Inject
@@ -474,7 +473,7 @@ public void copyAsync(DataObject srcData, DataObject dstData, AsyncCompletionCal
474473 try {
475474 if (srcType == DataObjectType .SNAPSHOT && dstType == DataObjectType .VOLUME ) {
476475 SnapshotInfo sinfo = (SnapshotInfo )srcData ;
477- final String snapshotName = StorPoolHelper .getSnapshotName (srcData .getId (), srcData .getUuid (), snapshotDataStoreDao , _snapshotDetailsDao );
476+ final String snapshotName = StorPoolHelper .getSnapshotName (srcData .getId (), srcData .getUuid (), snapshotDataStoreDao , snapshotDetailsDao );
478477
479478 VolumeInfo vinfo = (VolumeInfo )dstData ;
480479 final String volumeName = vinfo .getUuid ();
@@ -495,55 +494,14 @@ public void copyAsync(DataObject srcData, DataObject dstData, AsyncCompletionCal
495494 StorPoolUtil .spLog ("Snapshot %s does not exists on StorPool, will try to create a volume from a snopshot on secondary storage" , snapshotName );
496495 SnapshotDataStoreVO snap = getSnapshotImageStoreRef (sinfo .getId (), vinfo .getDataCenterId ());
497496 if (snap != null && StorPoolStorageAdaptor .getVolumeNameFromPath (snap .getInstallPath (), false ) == null ) {
498- resp = StorPoolUtil .volumeCreate (srcData .getUuid (), null , size , null , "no" , "snapshot" , sinfo .getBaseVolume ().getMaxIops (), conn );
499- if (resp .getError () == null ) {
500- VolumeObjectTO dstTO = (VolumeObjectTO ) dstData .getTO ();
501- dstTO .setSize (size );
502- dstTO .setPath (StorPoolUtil .devPath (StorPoolUtil .getNameFromResponse (resp , false )));
503- cmd = new StorPoolDownloadTemplateCommand (srcData .getTO (), dstTO , StorPoolHelper .getTimeout (StorPoolHelper .PrimaryStorageDownloadWait , configDao ), VirtualMachineManager .ExecuteInSequence .value (), "volume" );
504-
505- EndPoint ep = selector .select (srcData , dstData );
506- if (ep == null ) {
507- err = "No remote endpoint to send command, check if host or ssvm is down?" ;
508- } else {
509- answer = ep .sendMessage (cmd );
510- }
511-
512- if (answer != null && answer .getResult ()) {
513- SpApiResponse resp2 = StorPoolUtil .volumeFreeze (StorPoolUtil .getNameFromResponse (resp , true ), conn );
514- if (resp2 .getError () != null ) {
515- err = String .format ("Could not freeze Storpool volume %s. Error: %s" , srcData .getUuid (), resp2 .getError ());
516- } else {
517- String name = StorPoolUtil .getNameFromResponse (resp , false );
518- SnapshotDetailsVO snapshotDetails = _snapshotDetailsDao .findDetail (sinfo .getId (), sinfo .getUuid ());
519- if (snapshotDetails != null ) {
520- StorPoolHelper .updateSnapshotDetailsValue (snapshotDetails .getId (), StorPoolUtil .devPath (name ), "snapshot" );
521- }else {
522- StorPoolHelper .addSnapshotDetails (sinfo .getId (), sinfo .getUuid (), StorPoolUtil .devPath (name ), _snapshotDetailsDao );
523- }
524- resp = StorPoolUtil .volumeCreate (volumeName , StorPoolUtil .getNameFromResponse (resp , true ), size , null , null , "volume" , sinfo .getBaseVolume ().getMaxIops (), conn );
525- if (resp .getError () == null ) {
526- updateStoragePool (dstData .getDataStore ().getId (), size );
527-
528- VolumeObjectTO to = (VolumeObjectTO ) dstData .getTO ();
529- to .setPath (StorPoolUtil .devPath (StorPoolUtil .getNameFromResponse (resp , false )));
530- to .setSize (size );
531- // successfully downloaded snapshot to primary storage
532- answer = new CopyCmdAnswer (to );
533- StorPoolUtil .spLog ("Created volume=%s with uuid=%s from snapshot=%s with uuid=%s" , name , to .getUuid (), snapshotName , sinfo .getUuid ());
534-
535- } else {
536- err = String .format ("Could not create Storpool volume %s from snapshot %s. Error: %s" , volumeName , snapshotName , resp .getError ());
537- }
538- }
539- } else {
540- err = answer != null ? answer .getDetails () : "Unknown error while downloading template. Null answer returned." ;
541- }
497+ SpApiResponse emptyVolumeCreateResp = StorPoolUtil .volumeCreate (volumeName , null , size , null , null , "volume" , null , conn );
498+ if (emptyVolumeCreateResp .getError () == null ) {
499+ answer = createVolumeFromSnapshot (srcData , dstData , size , emptyVolumeCreateResp );
542500 } else {
543- err = String .format ("Could not create Storpool volume %s from snapshot %s. Error: %s" , volumeName , snapshotName , resp .getError ());
501+ answer = new Answer ( cmd , false , String .format ("Could not create Storpool volume %s from snapshot %s. Error: %s" , volumeName , snapshotName , emptyVolumeCreateResp .getError () ));
544502 }
545503 } else {
546- err = String .format ("The snapshot %s does not exists neither on primary, neither on secondary storage. Cannot create volume from snapshot" , snapshotName );
504+ 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 ) );
547505 }
548506 } else {
549507 err = String .format ("Could not create Storpool volume %s from snapshot %s. Error: %s" , volumeName , snapshotName , resp .getError ());
@@ -642,22 +600,17 @@ public void copyAsync(DataObject srcData, DataObject dstData, AsyncCompletionCal
642600 err = String .format ("Could not create Storpool volume for CS template %s. Error: %s" , name , resp .getError ());
643601 } else {
644602 String volumeNameToSnapshot = StorPoolUtil .getNameFromResponse (resp , true );
645- SpApiResponse resp2 = StorPoolUtil .volumeFreeze (volumeNameToSnapshot , conn );
646- if (resp2 .getError () != null ) {
647- err = String .format ("Could not freeze Storpool volume %s. Error: %s" , name , resp2 .getError ());
648- } else {
649- StorPoolUtil .spLog ("Storpool snapshot [%s] for a template exists. Creating template on Storpool with name [%s]" , tinfo .getUuid (), name );
650- TemplateObjectTO dstTO = (TemplateObjectTO ) dstData .getTO ();
651- dstTO .setPath (StorPoolUtil .devPath (StorPoolUtil .getNameFromResponse (resp , false )));
652- dstTO .setSize (size );
653- answer = new CopyCmdAnswer (dstTO );
654- }
603+ TemplateObjectTO dstTO = (TemplateObjectTO ) dstData .getTO ();
604+
605+ answer = createVolumeSnapshot (cmd , size , conn , volumeNameToSnapshot , dstTO );
606+ StorPoolUtil .volumeDelete (volumeNameToSnapshot , conn );
655607 }
656608 } else {
657609 resp = StorPoolUtil .volumeCreate (name , null , size , null , "no" , "template" , null , conn );
658610 if (resp .getError () != null ) {
659611 err = String .format ("Could not create Storpool volume for CS template %s. Error: %s" , name , resp .getError ());
660612 } else {
613+ String volName = StorPoolUtil .getNameFromResponse (resp , true );
661614 TemplateObjectTO dstTO = (TemplateObjectTO )dstData .getTO ();
662615 dstTO .setPath (StorPoolUtil .devPath (StorPoolUtil .getNameFromResponse (resp , false )));
663616 dstTO .setSize (size );
@@ -673,19 +626,12 @@ public void copyAsync(DataObject srcData, DataObject dstData, AsyncCompletionCal
673626
674627 if (answer != null && answer .getResult ()) {
675628 // successfully downloaded template to primary storage
676- SpApiResponse resp2 = StorPoolUtil .volumeFreeze (StorPoolUtil .getNameFromResponse (resp , true ), conn );
677- if (resp2 .getError () != null ) {
678- err = String .format ("Could not freeze Storpool volume %s. Error: %s" , name , resp2 .getError ());
679- }
629+ answer = createVolumeSnapshot (cmd , size , conn , volName , dstTO );
680630 } else {
681631 err = answer != null ? answer .getDetails () : "Unknown error while downloading template. Null answer returned." ;
682632 }
683- }
684- }
685- if (err != null ) {
686- resp = StorPoolUtil .volumeDelete (StorPoolUtil .getNameFromResponse (resp , true ), conn );
687- if (resp .getError () != null ) {
688- logger .warn (String .format ("Could not clean-up Storpool volume %s. Error: %s" , name , resp .getError ()));
633+
634+ StorPoolUtil .volumeDelete (volName , conn );
689635 }
690636 }
691637 } else if (srcType == DataObjectType .TEMPLATE && dstType == DataObjectType .VOLUME ) {
@@ -856,6 +802,42 @@ public void copyAsync(DataObject srcData, DataObject dstData, AsyncCompletionCal
856802 callback .complete (res );
857803 }
858804
805+ private Answer createVolumeSnapshot (StorageSubSystemCommand cmd , Long size , SpConnectionDesc conn ,
806+ String volName , TemplateObjectTO dstTO ) {
807+ Answer answer ;
808+ SpApiResponse resp2 = StorPoolUtil .volumeSnapshot (volName , dstTO .getUuid (), null , "template" , null , conn );
809+ if (resp2 .getError () != null ) {
810+ answer = new Answer (cmd , false , String .format ("Could not snapshot volume. Error: %s" , resp2 .getError ()));
811+ } else {
812+ dstTO .setPath (StorPoolUtil .devPath (
813+ StorPoolUtil .getSnapshotNameFromResponse (resp2 , false , StorPoolUtil .GLOBAL_ID )));
814+ dstTO .setSize (size );
815+ answer = new CopyCmdAnswer (dstTO );
816+ }
817+ return answer ;
818+ }
819+
820+ private Answer createVolumeFromSnapshot (DataObject srcData , DataObject dstData , final Long size ,
821+ SpApiResponse emptyVolumeCreateResp ) {
822+ Answer answer ;
823+ String name = StorPoolUtil .getNameFromResponse (emptyVolumeCreateResp , false );
824+ VolumeObjectTO dstTO = (VolumeObjectTO ) dstData .getTO ();
825+ dstTO .setSize (size );
826+ dstTO .setPath (StorPoolUtil .devPath (name ));
827+ StorageSubSystemCommand cmd1 = new StorPoolDownloadTemplateCommand (srcData .getTO (), dstTO , StorPoolHelper .getTimeout (StorPoolHelper .PrimaryStorageDownloadWait , configDao ), VirtualMachineManager .ExecuteInSequence .value (), "volume" );
828+
829+ EndPoint ep = selector .select (srcData , dstData );
830+ if (ep == null ) {
831+ answer = new Answer (cmd1 , false , "\" No remote endpoint to send command, check if host or ssvm is down?\" " );
832+ } else {
833+ answer = ep .sendMessage (cmd1 );
834+ }
835+ if (answer == null || !answer .getResult ()) {
836+ answer = new Answer (cmd1 , false , answer != null ? answer .getDetails () : "Unknown error while downloading template. Null answer returned." );
837+ }
838+ return answer ;
839+ }
840+
859841 private void updateVolumePoolType (VolumeInfo vinfo ) {
860842 VolumeVO volumeVO = volumeDao .findById (vinfo .getId ());
861843 volumeVO .setPoolType (StoragePoolType .StorPool );
@@ -987,9 +969,9 @@ public void takeSnapshot(SnapshotInfo snapshot, AsyncCompletionCallback<CreateCm
987969 SnapshotObjectTO snapTo = (SnapshotObjectTO )snapshot .getTO ();
988970 snapTo .setPath (StorPoolUtil .devPath (name .split ("~" )[1 ]));
989971 answer = new CreateObjectAnswer (snapTo );
990- StorPoolHelper .addSnapshotDetails (snapshot .getId (), snapshot .getUuid (), snapTo .getPath (), _snapshotDetailsDao );
972+ StorPoolHelper .addSnapshotDetails (snapshot .getId (), snapshot .getUuid (), snapTo .getPath (), snapshotDetailsDao );
991973 //add primary storage of snapshot
992- StorPoolHelper .addSnapshotDetails (snapshot .getId (), StorPoolUtil .SP_STORAGE_POOL_ID , String .valueOf (snapshot .getDataStore ().getId ()), _snapshotDetailsDao );
974+ StorPoolHelper .addSnapshotDetails (snapshot .getId (), StorPoolUtil .SP_STORAGE_POOL_ID , String .valueOf (snapshot .getDataStore ().getId ()), snapshotDetailsDao );
993975 StorPoolUtil .spLog ("StorpoolPrimaryDataStoreDriverImpl.takeSnapshot: snapshot: name=%s, uuid=%s, volume: name=%s, uuid=%s" , name , snapshot .getUuid (), volumeName , vinfo .getUuid ());
994976 }
995977 } catch (Exception e ) {
@@ -1004,7 +986,7 @@ public void takeSnapshot(SnapshotInfo snapshot, AsyncCompletionCallback<CreateCm
1004986 @ Override
1005987 public void revertSnapshot (final SnapshotInfo snapshot , final SnapshotInfo snapshotOnPrimaryStore , final AsyncCompletionCallback <CommandResult > callback ) {
1006988 final VolumeInfo vinfo = snapshot .getBaseVolume ();
1007- final String snapshotName = StorPoolHelper .getSnapshotName (snapshot .getId (), snapshot .getUuid (), snapshotDataStoreDao , _snapshotDetailsDao );
989+ final String snapshotName = StorPoolHelper .getSnapshotName (snapshot .getId (), snapshot .getUuid (), snapshotDataStoreDao , snapshotDetailsDao );
1008990 final String volumeName = StorPoolStorageAdaptor .getVolumeNameFromPath (vinfo .getPath (), true );
1009991 StorPoolUtil .spLog ("StorpoolPrimaryDataStoreDriverImpl.revertSnapshot: snapshot: name=%s, uuid=%s, volume: name=%s, uuid=%s" , snapshotName , snapshot .getUuid (), volumeName , vinfo .getUuid ());
1010992 String err = null ;
@@ -1059,7 +1041,7 @@ public void revertSnapshot(final SnapshotInfo snapshot, final SnapshotInfo snaps
10591041 }
10601042
10611043 private String getVcPolicyTag (Long vmId ) {
1062- ResourceTag resourceTag = vmId != null ? _resourceTagDao .findByKey (vmId , ResourceObjectType .UserVm , StorPoolUtil .SP_VC_POLICY ) : null ;
1044+ ResourceTag resourceTag = vmId != null ? resourceTagDao .findByKey (vmId , ResourceObjectType .UserVm , StorPoolUtil .SP_VC_POLICY ) : null ;
10631045 return resourceTag != null ? resourceTag .getValue () : "" ;
10641046 }
10651047
0 commit comments