Arik Hadas has uploaded a new change for review.

Change subject: core: introduce RunVmCommand#RunVmFlow
......................................................................

core: introduce RunVmCommand#RunVmFlow

RunVmCommand is a complicated command since it contains 4 different
flows:
- Resume paused VM
- Create snapshot and then run the VM for running stateless VMs
- Remove snapshot in case the VM already contains stateless snapshot
- Run the VM in all other cases

This patch introduced an enumeration that represent those 4 flows. The
benefits of using an enum:
- It replaces local flags that indicates the different flows (mResume,
  isFailedStatelessSnapshot)
- It makes the code more structured
- It can be used to eliminate unnecessary operations in certain flows
  more easily. In this patch, I used it in order to prevent initialize
  the VM object in flows where we don't really need it
- In the context of the planned enhancements for RunVmCommand, it allows
  us to query the flow from outside of this the command

Change-Id: I2a3f062323c4246b45757d3062478c0109d70708
Signed-off-by: Arik Hadas <aha...@redhat.com>
---
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RunVmCommand.java
1 file changed, 82 insertions(+), 26 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/63/22663/1

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RunVmCommand.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RunVmCommand.java
index ffb5432..6046a99 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RunVmCommand.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RunVmCommand.java
@@ -82,10 +82,19 @@
 public class RunVmCommand<T extends RunVmParams> extends RunVmCommandBase<T>
         implements QuotaVdsDependent {
 
-    private boolean mResume;
+    enum RunVmFlow {
+        RUN,
+        RESUME,
+        CREATE_STATELESS_IMAGES,
+        REMOVE_STATELESS_IMAGES;
+    }
+
+    /** Used to mark if the VM disks were already fetched */
+    private boolean disksFetched;
+    /** Cache the current flow the command is in. use {@link #getFlow()} to 
retrieve the flow */
+    private RunVmFlow cachedFlow;
     /** Note: this field should not be used directly, use {@link 
#isVmRunningStateless()} instead */
     private Boolean cachedVmIsRunningStateless;
-    private boolean isFailedStatlessSnapshot;
     /** Indicates whether restoration of memory from snapshot is supported for 
the VM */
     private boolean memorySnapshotSupported;
     /** The memory volume which is stored in the active snapshot of the VM */
@@ -264,28 +273,64 @@
         // configuration option change
         VmDeviceUtils.updateVmDevices(getVm().getStaticData());
         setActionReturnValue(VMStatus.Down);
+
         initVm();
-        if (getVm().getStatus() == VMStatus.Paused) { // resume
-            resumeVm();
-        } else { // run vm
-            if (!_isRerun && 
Boolean.TRUE.equals(getParameters().getRunAsStateless())
-                    && getVm().getStatus() != VMStatus.Suspended) {
-                if (getVm().getDiskList().isEmpty()) { // If there are no 
snappable disks, there is no meaning for
-                    // running as stateless, log a warning and run normally
-                    warnIfNotAllDisksPermitSnapshots();
-                    runVm();
-                }
-                else {
-                    statelessVmTreatment();
-                }
-            } else if (!isInternalExecution() && !_isRerun
-                    && getVm().getStatus() != VMStatus.Suspended
-                    && isStatelessSnapshotExistsForVm()
-                    && !isVMPartOfManualPool()) {
-                removeVmStatlessImages();
-            } else {
-                runVm();
+        perform();
+    }
+
+    protected RunVmFlow getFlow() {
+        if (_isRerun) {
+            return cachedFlow = RunVmFlow.RUN;
+        }
+
+        if (cachedFlow != null) {
+            return cachedFlow;
+        }
+
+        if (getVm().getStatus() == VMStatus.Paused) {
+            return cachedFlow = RunVmFlow.RESUME;
+        }
+
+        if (getVm().getStatus() == VMStatus.Suspended) {
+            return cachedFlow = RunVmFlow.RUN;
+        }
+
+        if (Boolean.TRUE.equals(getParameters().getRunAsStateless())) {
+            fetchVmDisksFromDb();
+            if (getVm().getDiskList().isEmpty()) {
+                // If there are no snappable disks, there is no meaning for
+                // running as stateless, log a warning and run normally
+                warnIfNotAllDisksPermitSnapshots();
+                return cachedFlow = RunVmFlow.RUN;
             }
+            else {
+                return cachedFlow = RunVmFlow.CREATE_STATELESS_IMAGES;
+            }
+        }
+
+        if (!isInternalExecution()
+                && isStatelessSnapshotExistsForVm()
+                && !isVMPartOfManualPool()) {
+            return cachedFlow = RunVmFlow.REMOVE_STATELESS_IMAGES;
+        }
+
+        return cachedFlow = RunVmFlow.RUN;
+    }
+
+    protected void perform() {
+        switch(getFlow()) {
+        case RESUME:
+            resumeVm();
+            break;
+        case REMOVE_STATELESS_IMAGES:
+            removeVmStatlessImages();
+            break;
+        case CREATE_STATELESS_IMAGES:
+            statelessVmTreatment();
+            break;
+        case RUN:
+        default:
+            runVm();
         }
     }
 
@@ -410,7 +455,6 @@
     }
 
     private void removeVmStatlessImages() {
-        isFailedStatlessSnapshot = true;
         VmPoolHandler.processVmPoolOnStopVm(getVm().getId(), new 
CommandContext(getExecutionContext(), getLock()));
         // setting lock to null in order not to release lock twice
         setLock(null);
@@ -508,10 +552,10 @@
     public AuditLogType getAuditLogTypeValue() {
         switch (getActionState()) {
         case EXECUTE:
-            if (isFailedStatlessSnapshot) {
+            if (getFlow() == RunVmFlow.REMOVE_STATELESS_IMAGES) {
                 return 
AuditLogType.USER_RUN_VM_FAILURE_STATELESS_SNAPSHOT_LEFT;
             }
-            if (mResume) {
+            if (getFlow() == RunVmFlow.RESUME) {
                 return getSucceeded() ? AuditLogType.USER_RESUME_VM : 
AuditLogType.USER_FAILED_RESUME_VM;
             } else if (isInternalExecution()) {
                 if (getSucceeded()) {
@@ -566,7 +610,11 @@
     }
 
     protected void initVm() {
-        VmHandler.updateDisksFromDb(getVm());
+        if (getFlow() != RunVmFlow.RUN) {
+            return;
+        }
+
+        fetchVmDisksFromDb();
         getVm().setKvmEnable(getParameters().getKvmEnable());
         getVm().setRunAndPause(getParameters().getRunAndPause() == null ? 
getVm().isRunAndPause() : getParameters().getRunAndPause());
         getVm().setAcpiEnable(getParameters().getAcpiEnable());
@@ -605,6 +653,14 @@
                         .getVdsGroupCompatibilityVersion()));
     }
 
+    private void fetchVmDisksFromDb() {
+        if (disksFetched) {
+            return;
+        }
+        VmHandler.updateDisksFromDb(getVm());
+        disksFetched = true;
+    }
+
     protected boolean getVdsToRunOn() {
         // use destination vds or default vds or none
         VDS destinationVds = getDestinationVds();


-- 
To view, visit http://gerrit.ovirt.org/22663
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2a3f062323c4246b45757d3062478c0109d70708
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Arik Hadas <aha...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to