SadiJr commented on code in PR #7238:
URL: https://github.com/apache/cloudstack/pull/7238#discussion_r1112979629


##########
server/src/main/java/org/apache/cloudstack/backup/BackupManagerImpl.java:
##########
@@ -585,17 +595,64 @@ public boolean restoreBackup(final Long backupId) {
 
         final BackupOffering offering = 
backupOfferingDao.findByIdIncludingRemoved(vm.getBackupOfferingId());
         if (offering == null) {
-            throw new CloudRuntimeException("Failed to find backup offering of 
the VM backup");
+            throw new CloudRuntimeException("Failed to find backup offering of 
the VM backup.");
         }
 
-        final BackupProvider backupProvider = 
getBackupProvider(offering.getProvider());
-        if (!backupProvider.restoreVMFromBackup(vm, backup)) {
-            throw new CloudRuntimeException("Error restoring VM from backup ID 
" + backup.getId());
+        String backupDetailsInMessage = 
ReflectionToStringBuilderUtils.reflectOnlySelectedFields(backup, "uuid", 
"externalId", "vmId", "type", "status", "date");
+        try {
+            updateVmState(vm, VirtualMachine.Event.RestoringRequested);
+            updateVolumeState(vm, Volume.Event.RestoreRequested);
+            final BackupProvider backupProvider = 
getBackupProvider(offering.getProvider());
+            if (!backupProvider.restoreVMFromBackup(vm, backup)) {
+                throw new CloudRuntimeException(String.format("Error restoring 
%s from backup [%s].", vm, backupDetailsInMessage));
+            }
+        } catch (Exception e) {
+            LOG.error(String.format("Failed to restore backup [%s] due to: 
[%s].", backupDetailsInMessage, e.getMessage()), e);
+            updateVolumeState(vm, Volume.Event.RestoreFailed);
+            updateVmState(vm, VirtualMachine.Event.RestoringFailed);
+            throw new CloudRuntimeException(String.format("Error restoring VM 
from backup [%s].", backupDetailsInMessage));
         }
+        updateVolumeState(vm, Volume.Event.RestoreSucceeded);
+        updateVmState(vm, VirtualMachine.Event.RestoringSuccess);
+
         return importRestoredVM(vm.getDataCenterId(), vm.getDomainId(), 
vm.getAccountId(), vm.getUserId(),
                 vm.getInstanceName(), vm.getHypervisorType(), backup);
     }
 
+    private void updateVmState(VMInstanceVO vm, VirtualMachine.Event event) {
+        LOG.debug(String.format("Trying to update state of VM [%s] with event 
[%s].", vm, event));
+        Transaction.execute(TransactionLegacy.CLOUD_DB, 
(TransactionCallback<VMInstanceVO>) status -> {
+            try {
+                if (!virtualMachineManager.stateTransitTo(vm, event, 
vm.getHostId())) {
+                    throw new CloudRuntimeException(String.format("Unable to 
change state of VM [%s] to [%s].", vm, VirtualMachine.State.Restoring));
+                }
+            } catch (NoTransitionException e) {
+                String errMsg = String.format("Failed to update state of VM 
[%s] with event [%s] due to [%s].", vm, event, e.getMessage());
+                LOG.error(errMsg, e);
+                throw new RuntimeException(errMsg);
+            }
+            return null;
+        });
+    }
+
+    private void updateVolumeState(VMInstanceVO vm, Volume.Event event) {
+        Transaction.execute(TransactionLegacy.CLOUD_DB, 
(TransactionCallback<VolumeVO>) status -> {
+            for (VolumeVO volume : 
volumeDao.findIncludingRemovedByInstanceAndType(vm.getId(), null)) {
+                LOG.debug(String.format("Trying to update state of volume [%s] 
with event [%s].", volume, event));
+                try {
+                    if (!volumeApiService.stateTransitTo(volume, event)) {
+                        throw new CloudRuntimeException(String.format("Unable 
to change state of volume [%s] to [%s].", volume, Volume.State.Restoring));
+                    }
+                } catch (NoTransitionException e) {
+                    String errMsg = String.format("Failed to update state of 
volume [%s] with event [%s] due to [%s].", volume, event, e.getMessage());
+                    LOG.error(errMsg, e);
+                    throw new RuntimeException(errMsg);
+                }

Review Comment:
   Done, thanks for the suggestion.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to