SadiJr commented on a change in pull request #5903:
URL: https://github.com/apache/cloudstack/pull/5903#discussion_r801784677



##########
File path: 
server/src/main/java/org/apache/cloudstack/backup/BackupManagerImpl.java
##########
@@ -287,24 +288,40 @@ public boolean assignVMToBackupOffering(Long vmId, Long 
offeringId) {
             throw new CloudRuntimeException("Failed to get the backup provider 
for the zone, please contact the administrator");
         }
 
-        vm.setBackupOfferingId(offering.getId());
-        
vm.setBackupVolumes(createVolumeInfoFromVolumes(volumeDao.findByInstance(vm.getId())));
-        if (vmInstanceDao.update(vm.getId(), vm)) {
-            
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VM_BACKUP_OFFERING_ASSIGN, 
vm.getAccountId(), vm.getDataCenterId(), vm.getId(),
-                    "Backup-" + vm.getHostName() + "-" + vm.getUuid(), 
vm.getBackupOfferingId(), null, null,
-                    Backup.class.getSimpleName(), vm.getUuid());
-        } else {
-            throw new CloudRuntimeException("Failed to update VM assignment to 
the backup offering in the DB, please try again.");
-        }
+        return transactionAssignVMToBackupOffering(vm, offering, 
backupProvider) != null;
+    }
 
-        try {
-            if (backupProvider.assignVMToBackupOffering(vm, offering)) {
-                return vmInstanceDao.update(vm.getId(), vm);
+    private VMInstanceVO transactionAssignVMToBackupOffering(VMInstanceVO vm, 
BackupOfferingVO offering, BackupProvider backupProvider) {
+        return Transaction.execute(TransactionLegacy.CLOUD_DB, new 
TransactionCallback<VMInstanceVO>() {
+            @Override
+            public VMInstanceVO doInTransaction(final TransactionStatus 
status) {
+                try {
+                    long vmId = vm.getId();
+                    vm.setBackupOfferingId(offering.getId());
+                    
vm.setBackupVolumes(createVolumeInfoFromVolumes(volumeDao.findByInstance(vmId)));
+
+                    if (vmInstanceDao.update(vmId, vm)) {
+                        if (backupProvider.assignVMToBackupOffering(vm, 
offering)) {
+                            vmInstanceDao.update(vmId, vm);

Review comment:
       This generates a more accurate error message, where you can 
differentiate whether the error occurred in Veeam or in the ACS itself. But, it 
is possible to change the flow in order to perform only one update, see if it 
was good.

##########
File path: 
server/src/main/java/org/apache/cloudstack/backup/BackupManagerImpl.java
##########
@@ -287,24 +288,40 @@ public boolean assignVMToBackupOffering(Long vmId, Long 
offeringId) {
             throw new CloudRuntimeException("Failed to get the backup provider 
for the zone, please contact the administrator");
         }
 
-        vm.setBackupOfferingId(offering.getId());
-        
vm.setBackupVolumes(createVolumeInfoFromVolumes(volumeDao.findByInstance(vm.getId())));
-        if (vmInstanceDao.update(vm.getId(), vm)) {
-            
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VM_BACKUP_OFFERING_ASSIGN, 
vm.getAccountId(), vm.getDataCenterId(), vm.getId(),
-                    "Backup-" + vm.getHostName() + "-" + vm.getUuid(), 
vm.getBackupOfferingId(), null, null,
-                    Backup.class.getSimpleName(), vm.getUuid());
-        } else {
-            throw new CloudRuntimeException("Failed to update VM assignment to 
the backup offering in the DB, please try again.");
-        }
+        return transactionAssignVMToBackupOffering(vm, offering, 
backupProvider) != null;
+    }
 
-        try {
-            if (backupProvider.assignVMToBackupOffering(vm, offering)) {
-                return vmInstanceDao.update(vm.getId(), vm);
+    private VMInstanceVO transactionAssignVMToBackupOffering(VMInstanceVO vm, 
BackupOfferingVO offering, BackupProvider backupProvider) {
+        return Transaction.execute(TransactionLegacy.CLOUD_DB, new 
TransactionCallback<VMInstanceVO>() {
+            @Override
+            public VMInstanceVO doInTransaction(final TransactionStatus 
status) {
+                try {
+                    long vmId = vm.getId();
+                    vm.setBackupOfferingId(offering.getId());
+                    
vm.setBackupVolumes(createVolumeInfoFromVolumes(volumeDao.findByInstance(vmId)));
+
+                    if (vmInstanceDao.update(vmId, vm)) {
+                        if (backupProvider.assignVMToBackupOffering(vm, 
offering)) {
+                            vmInstanceDao.update(vmId, vm);
+                            
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VM_BACKUP_OFFERING_ASSIGN, 
vm.getAccountId(), vm.getDataCenterId(), vmId,
+                                    "Backup-" + vm.getHostName() + "-" + 
vm.getUuid(), vm.getBackupOfferingId(), null, null,
+                                    Backup.class.getSimpleName(), 
vm.getUuid());
+                            LOG.debug(String.format("VM [%s] successfully 
added to Backup Offering [%s].", 
ReflectionToStringBuilderUtils.reflectOnlySelectedFields(vm, "uuid", 
"instanceName", "backupOfferingId", "backupVolumes"),
+                                    
ReflectionToStringBuilderUtils.reflectOnlySelectedFields(offering, "uuid", 
"name", "externalId", "provider")));
+                            return vm;
+                        }
+                    } else {
+                        throw new CloudRuntimeException("Failed to update VM 
assignment to the backup offering in the DB, please try again.");
+                    }
+                } catch (Exception e) {
+                    LOG.error(String.format("Failed to assign VM [%s] to the 
Backup Offering [%s], using provider [name: %s, class: %s], due to: [%s].",
+                            
ReflectionToStringBuilderUtils.reflectOnlySelectedFields(vm, "uuid", 
"instanceName", "backupOfferingId", "backupVolumes"),
+                            
ReflectionToStringBuilderUtils.reflectOnlySelectedFields(offering, "uuid", 
"name", "externalId", "provider"),
+                            backupProvider.getName(), 
backupProvider.getClass().getSimpleName(), e.getMessage()), e);

Review comment:
       I did not understand. For me it makes more sense for the stacktrace to 
appear in an error log than in a debug, in addition, this message will only be 
visible to operators, and the only error message the user will see is "Failed 
to assign the VM to the backup offering, please try removing the assignment and 
try again." or "Failed to update VM assignment to the backup offering in the 
DB, please try again.".




-- 
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