3131import com .cloud .exception .InvalidParameterValueException ;
3232import com .cloud .exception .OperationTimedoutException ;
3333import com .cloud .host .HostVO ;
34+ import com .cloud .host .Status ;
3435import com .cloud .host .dao .HostDao ;
3536import com .cloud .hypervisor .Hypervisor ;
3637import com .cloud .resource .ResourceState ;
126127import java .util .Objects ;
127128import java .util .Optional ;
128129import java .util .Set ;
130+ import java .util .concurrent .ExecutionException ;
129131import java .util .stream .Collectors ;
130132
131133import static org .apache .cloudstack .backup .dao .BackupDetailDao .CURRENT ;
@@ -311,7 +313,7 @@ public boolean takeBackup(VirtualMachine vm, boolean quiesceVm, Long backupSched
311313
312314 try {
313315 outcome .get ();
314- } catch (InterruptedException | java . util . concurrent . ExecutionException e ) {
316+ } catch (InterruptedException | ExecutionException e ) {
315317 throw new CloudRuntimeException (String .format ("Unable to retrieve result from job takeBackup due to [%s]. VM [%s]." , e .getMessage (), vm .getUuid ()), e );
316318 }
317319
@@ -331,6 +333,16 @@ public Boolean orchestrateTakeBackup(Backup backup, boolean quiesceVm) {
331333 BackupVO backupVO = (BackupVO ) backup ;
332334 long vmId = backup .getVmId ();
333335 VirtualMachine userVm = virtualMachineManager .findById (vmId );
336+ Long hostId = vmSnapshotHelper .pickRunningHost (vmId );
337+ HostVO hostVO = hostDao .findById (hostId );
338+
339+ if (hostVO .getStatus () == Status .Down || hostVO .getStatus () == Status .Disconnected ) {
340+ backupVO .setStatus (Backup .Status .Failed );
341+ backupDao .update (backupVO .getId (), backupVO );
342+
343+ logger .error ("No available host found to create backup [{}] of VM [{}]. Setting the backup as Failed." , backupVO .getUuid (), userVm .getUuid ());
344+ return false ;
345+ }
334346
335347 validateVmState (userVm , "take backup" );
336348 List <VolumeObjectTO > volumeTOs ;
@@ -346,7 +358,6 @@ public Boolean orchestrateTakeBackup(Backup backup, boolean quiesceVm) {
346358
347359 logger .info ("Starting VM backup process for VM [{}]." , userVm .getUuid ());
348360
349- Long hostId = vmSnapshotHelper .pickRunningHost (vmId );
350361 BackupOfferingVO backupOfferingVO = backupOfferingDao .findByIdIncludingRemoved (backup .getBackupOfferingId ());
351362 NativeBackupOfferingVO nativeBackupOfferingVO = nativeBackupOfferingDao .findByUuidIncludingRemoved (backupOfferingVO .getExternalId ());
352363
@@ -412,7 +423,7 @@ public boolean deleteBackup(Backup backup, boolean forced) {
412423
413424 try {
414425 outcome .get ();
415- } catch (InterruptedException | java . util . concurrent . ExecutionException e ) {
426+ } catch (InterruptedException | ExecutionException e ) {
416427 throw new CloudRuntimeException (String .format ("Unable to retrieve result from job deleteBackup due to [%s]. Backup [%s]." , e .getMessage (), backup .getUuid ()), e );
417428 }
418429
@@ -509,7 +520,7 @@ public boolean restoreVMFromBackup(VirtualMachine vm, Backup backup, boolean qui
509520
510521 try {
511522 outcome .get ();
512- } catch (InterruptedException | java . util . concurrent . ExecutionException e ) {
523+ } catch (InterruptedException | ExecutionException e ) {
513524 throw new CloudRuntimeException (String .format ("Unable to retrieve result from job restoreVMFromBackup due to [%s]. Backup [%s]." , e .getMessage (), backup .getUuid ()), e );
514525 }
515526
@@ -602,7 +613,7 @@ public Pair<Boolean, String> restoreBackedUpVolume(Backup backup, String volumeU
602613
603614 try {
604615 outcome .get ();
605- } catch (InterruptedException | java . util . concurrent . ExecutionException e ) {
616+ } catch (InterruptedException | ExecutionException e ) {
606617 throw new CloudRuntimeException (String .format ("Unable to retrieve result from job restoreBackedUpVolume due to [%s]. Backup [%s]." , e .getMessage (), backup .getUuid ()), e );
607618 }
608619
@@ -833,7 +844,6 @@ private boolean finalizeQuickRestore(VirtualMachine vm, List<VolumeInfo> volumes
833844 return consolidateVolumes (vm , hostId , volumesToConsolidate );
834845 }
835846
836-
837847 private List <VolumeInfo > getVolumesToConsolidate (VirtualMachine vm , List <NativeBackupDataStoreVO > deltasOnSecondary , List <VolumeObjectTO > volumeObjectTOS , long hostId ) {
838848 List <VolumeInfo > volumesToConsolidate = new ArrayList <>();
839849
@@ -1001,7 +1011,7 @@ private HostVO getHostToRestore(VirtualMachine vm, boolean quickRestore, Long ho
10011011 throw new AgentUnavailableException (String .format ("No host found to quick restore VM [%s]. Please check the logs." , vm .getUuid ()), -1 );
10021012 }
10031013 host = hostDao .findByIdIncludingRemoved (hostId );
1004- if (host .getStatus () != com . cloud . host . Status .Up || host .isInMaintenanceStates () || host .getResourceState () != ResourceState .Enabled ) {
1014+ if (host .getStatus () != Status .Up || host .isInMaintenanceStates () || host .getResourceState () != ResourceState .Enabled ) {
10051015 logger .error ("Cannot quick restore if the VM's last host is in maintenance, not Up, or disabled. You may try to start it in an available host and stop it before quick" +
10061016 " restoring. Otherwise, use the normal restore." );
10071017 throw new AgentUnavailableException (String .format ("No host found to quick restore VM [%s]. Please check the logs." , vm .getUuid ()), -1 );
0 commit comments