6666import org .apache .cloudstack .storage .datastore .db .StoragePoolVO ;
6767import org .apache .cloudstack .utils .reflectiontostringbuilderutils .ReflectionToStringBuilderUtils ;
6868import org .apache .commons .collections .CollectionUtils ;
69+ import org .apache .commons .lang3 .BooleanUtils ;
6970import org .apache .commons .lang3 .StringUtils ;
7071import org .apache .log4j .Logger ;
7172
@@ -634,9 +635,9 @@ public boolean restoreBackupVolumeAndAttachToVM(final String backedUpVolumeUuid,
634635 }
635636 accountManager .checkAccess (CallContext .current ().getCallingAccount (), null , true , vmFromBackup );
636637
637- Pair <String , String > restoreInfo = getRestoreVolumeHostAndDatastore (vm );
638- String hostIp = restoreInfo .first ();
639- String datastoreUuid = restoreInfo .second ();
638+ Pair <HostVO , StoragePoolVO > restoreInfo = getRestoreVolumeHostAndDatastore (vm );
639+ HostVO host = restoreInfo .first ();
640+ StoragePoolVO datastore = restoreInfo .second ();
640641
641642 LOG .debug ("Asking provider to restore volume " + backedUpVolumeUuid + " from backup " + backupId +
642643 " (with external ID " + backup .getExternalId () + ") and attach it to VM: " + vm .getUuid ());
@@ -647,17 +648,47 @@ public boolean restoreBackupVolumeAndAttachToVM(final String backedUpVolumeUuid,
647648 }
648649
649650 BackupProvider backupProvider = getBackupProvider (offering .getProvider ());
650- Pair <Boolean , String > result = backupProvider .restoreBackedUpVolume (backup , backedUpVolumeUuid , hostIp , datastoreUuid );
651- if (!result .first ()) {
652- throw new CloudRuntimeException ("Error restoring volume " + backedUpVolumeUuid );
651+ LOG .debug (String .format ("Trying to restore volume using host private IP address: [%s]." , host .getPrivateIpAddress ()));
652+
653+ String [] hostPossibleValues = {host .getPrivateIpAddress (), host .getName ()};
654+ String [] datastoresPossibleValues = {datastore .getUuid (), datastore .getName ()};
655+
656+ Pair <Boolean , String > result = restoreBackedUpVolume (backedUpVolumeUuid , backup , backupProvider , hostPossibleValues , datastoresPossibleValues );
657+
658+ if (BooleanUtils .isFalse (result .first ())) {
659+ throw new CloudRuntimeException (String .format ("Error restoring volume [%s] of VM [%s] to host [%s] using backup provider [%s] due to: [%s]." ,
660+ backedUpVolumeUuid , vm .getUuid (), host .getUuid (), backupProvider .getName (), result .second ()));
653661 }
654662 if (!attachVolumeToVM (vm .getDataCenterId (), result .second (), vmFromBackup .getBackupVolumeList (),
655- backedUpVolumeUuid , vm , datastoreUuid , backup )) {
656- throw new CloudRuntimeException ("Error attaching volume " + backedUpVolumeUuid + " to VM " + vm .getUuid ());
663+ backedUpVolumeUuid , vm , datastore . getUuid () , backup )) {
664+ throw new CloudRuntimeException (String . format ( "Error attaching volume [%s] to VM [%s]. " + backedUpVolumeUuid , vm .getUuid () ));
657665 }
658666 return true ;
659667 }
660668
669+ protected Pair <Boolean , String > restoreBackedUpVolume (final String backedUpVolumeUuid , final BackupVO backup , BackupProvider backupProvider , String [] hostPossibleValues ,
670+ String [] datastoresPossibleValues ) {
671+ Pair <Boolean , String > result = new Pair <>(false , "" );
672+ for (String hostData : hostPossibleValues ) {
673+ for (String datastoreData : datastoresPossibleValues ) {
674+ LOG .debug (String .format ("Trying to restore volume [UUID: %s], using host [%s] and datastore [%s]." ,
675+ backedUpVolumeUuid , hostData , datastoreData ));
676+
677+ try {
678+ result = backupProvider .restoreBackedUpVolume (backup , backedUpVolumeUuid , hostData , datastoreData );
679+
680+ if (BooleanUtils .isTrue (result .first ())) {
681+ return result ;
682+ }
683+ } catch (Exception e ) {
684+ LOG .debug (String .format ("Failed to restore volume [UUID: %s], using host [%s] and datastore [%s] due to: [%s]." ,
685+ backedUpVolumeUuid , hostData , datastoreData , e .getMessage ()), e );
686+ }
687+ }
688+ }
689+ return result ;
690+ }
691+
661692 @ Override
662693 @ ActionEvent (eventType = EventTypes .EVENT_VM_BACKUP_DELETE , eventDescription = "deleting VM backup" , async = true )
663694 public boolean deleteBackup (final Long backupId ) {
@@ -694,31 +725,31 @@ public boolean deleteBackup(final Long backupId) {
694725 /**
695726 * Get the pair: hostIp, datastoreUuid in which to restore the volume, based on the VM to be attached information
696727 */
697- private Pair <String , String > getRestoreVolumeHostAndDatastore (VMInstanceVO vm ) {
728+ private Pair <HostVO , StoragePoolVO > getRestoreVolumeHostAndDatastore (VMInstanceVO vm ) {
698729 List <VolumeVO > rootVmVolume = volumeDao .findIncludingRemovedByInstanceAndType (vm .getId (), Volume .Type .ROOT );
699730 Long poolId = rootVmVolume .get (0 ).getPoolId ();
700731 StoragePoolVO storagePoolVO = primaryDataStoreDao .findById (poolId );
701- String datastoreUuid = storagePoolVO .getUuid ();
702- String hostIp = vm .getHostId () == null ?
703- getHostIp (storagePoolVO ) :
704- hostDao .findById (vm .getHostId ()).getPrivateIpAddress ();
705- return new Pair <>(hostIp , datastoreUuid );
732+ HostVO hostVO = vm .getHostId () == null ?
733+ getFirstHostFromStoragePool (storagePoolVO ) :
734+ hostDao .findById (vm .getHostId ());
735+ return new Pair <>(hostVO , storagePoolVO );
706736 }
707737
708738 /**
709- * Find a host IP from storage pool access
739+ * Find a host from storage pool access
710740 */
711- private String getHostIp (StoragePoolVO storagePoolVO ) {
741+ private HostVO getFirstHostFromStoragePool (StoragePoolVO storagePoolVO ) {
712742 List <HostVO > hosts = null ;
713743 if (storagePoolVO .getScope ().equals (ScopeType .CLUSTER )) {
714744 hosts = hostDao .findByClusterId (storagePoolVO .getClusterId ());
715745
716746 } else if (storagePoolVO .getScope ().equals (ScopeType .ZONE )) {
717747 hosts = hostDao .findByDataCenterId (storagePoolVO .getDataCenterId ());
718748 }
719- return hosts .get (0 ). getPrivateIpAddress () ;
749+ return hosts .get (0 );
720750 }
721751
752+
722753 /**
723754 * Attach volume to VM
724755 */
0 commit comments