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



##########
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.");
+                    }

Review comment:
       We could adjust these statements to reduce indentation, like:
   
   ```suggestion
                       if (!vmInstanceDao.update(vmId, vm) || 
!backupProvider.assignVMToBackupOffering(vm, offering)) {
                           throw new CloudRuntimeException("Failed to update VM 
assignment to the backup offering in the DB, please try again.");
                       }
   
                       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;
   ```

##########
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:
       Is it necessary to call `vmInstanceDao.update` twice?




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