Arik Hadas has uploaded a new change for review.

Change subject: core: introduce Flow enum
......................................................................

core: introduce Flow enum

Change-Id: I350e263bb326c8d3c6be78b2d5b2bd1a3bd0535f
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, 94 insertions(+), 59 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/40/22640/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 4ddb5ec..b42f6f5 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,16 @@
 public class RunVmCommand<T extends RunVmParams> extends RunVmCommandBase<T>
         implements QuotaVdsDependent {
 
-    private boolean mResume;
+    enum Flow {
+        RUN,
+        RESUME,
+        CREATE_STATELESS_IMAGES,
+        REMOVE_STATELESS_IMAGES;
+    }
+
+    private Flow cachedFlow;
     /** Note: this field should not be used directly, use {@link 
#isVmRunningStateless()} instead */
     private Boolean cachedVmIsRunningStateless;
-    private boolean isFailedStatlessSnapshot;
     /** The memory volume which is stored in the active snapshot of the VM */
     private String memoryVolumeFromSnapshot = StringUtils.EMPTY;
     /** This flag is used to indicate that the disks might be dirty since the 
memory
@@ -254,29 +260,66 @@
     protected void executeVmCommand() {
         setActionReturnValue(VMStatus.Down);
 
-        mResume = getVm().getStatus() == VMStatus.Paused;
         initVm();
-        if (mResume) {
-            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 Flow getFlow() {
+        if (_isRerun) {
+            return cachedFlow = Flow.RUN;
+        }
+
+        if (cachedFlow != null) {
+            return cachedFlow;
+        }
+
+        if (getVm().getStatus() == VMStatus.Paused) {
+            return cachedFlow = Flow.RESUME;
+        }
+
+        if (getVm().getStatus() == VMStatus.Suspended) {
+            return cachedFlow = Flow.RUN;
+        }
+
+        if (Boolean.TRUE.equals(getParameters().getRunAsStateless())) {
+            if (getVm().getDiskList().isEmpty()) {
+                // If there are no snappable disks, there is no meaning for
+                // running as stateless, log a warning and run normally
+                // TODO: log
+                return cachedFlow = Flow.RUN;
             }
+            if (isStatelessSnapshotExistsForVm()) {
+                log.errorFormat(
+                        "VM {0} ({1}) already contains stateless snapshot, 
removing it",
+                        getVm().getName(), getVm().getId());
+                return cachedFlow = Flow.REMOVE_STATELESS_IMAGES;
+            }
+            return cachedFlow = Flow.CREATE_STATELESS_IMAGES;
+        }
+
+        if (!isInternalExecution()
+                && isStatelessSnapshotExistsForVm()
+                && !isVMPartOfManualPool()) {
+            return cachedFlow = Flow.REMOVE_STATELESS_IMAGES;
+        }
+
+        return cachedFlow = Flow.RUN;
+    }
+
+    protected void perform() {
+        switch(getFlow()) {
+        case RESUME:
+            resumeVm();
+            break;
+        case REMOVE_STATELESS_IMAGES:
+            removeVmStatlessImages();
+            break;
+        case CREATE_STATELESS_IMAGES:
+            createVmStatlessImages();
+            break;
+        case RUN:
+        default:
+            runVm();
         }
     }
 
@@ -314,42 +357,35 @@
         return IsoDomainListSyncronizer.getInstance();
     }
 
-    private void statelessVmTreatment() {
+    private void createVmStatlessImages() {
         warnIfNotAllDisksPermitSnapshots();
 
-        if (isStatelessSnapshotExistsForVm()) {
-            log.errorFormat(
-                    "VM {0} ({1}) already contains stateless snapshot, 
removing it",
-                    getVm().getName(), getVm().getId());
-            removeVmStatlessImages();
+        log.infoFormat("Creating stateless snapshot for VM {0} ({1})",
+                getVm().getName(), getVm().getId());
+        CreateAllSnapshotsFromVmParameters createAllSnapshotsFromVmParameters 
= buildCreateSnapshotParameters();
+
+        VdcReturnValueBase vdcReturnValue =
+                
getBackend().runInternalAction(VdcActionType.CreateAllSnapshotsFromVm,
+                        createAllSnapshotsFromVmParameters,
+                        createContextForStatelessSnapshotCreation());
+
+        // setting lock to null in order not to release lock twice
+        setLock(null);
+        setSucceeded(vdcReturnValue.getSucceeded());
+
+        if (vdcReturnValue.getSucceeded()) {
+
+            
getReturnValue().getVdsmTaskIdList().addAll(vdcReturnValue.getInternalVdsmTaskIdList());
+            // save RunVmParams so we'll know how to run
+            // the stateless VM in the endAction part.
+            VmHandler.updateDisksFromDb(getVm());
         } else {
-            log.infoFormat("Creating stateless snapshot for VM {0} ({1})",
-                    getVm().getName(), getVm().getId());
-            CreateAllSnapshotsFromVmParameters 
createAllSnapshotsFromVmParameters = buildCreateSnapshotParameters();
-
-            VdcReturnValueBase vdcReturnValue =
-                    
getBackend().runInternalAction(VdcActionType.CreateAllSnapshotsFromVm,
-                            createAllSnapshotsFromVmParameters,
-                            createContextForStatelessSnapshotCreation());
-
-            // setting lock to null in order not to release lock twice
-            setLock(null);
-            setSucceeded(vdcReturnValue.getSucceeded());
-
-            if (vdcReturnValue.getSucceeded()) {
-
-                
getReturnValue().getVdsmTaskIdList().addAll(vdcReturnValue.getInternalVdsmTaskIdList());
-                // save RunVmParams so we'll know how to run
-                // the stateless VM in the endAction part.
-                VmHandler.updateDisksFromDb(getVm());
-            } else {
-                if (areDisksLocked(vdcReturnValue)) {
-                    throw new 
VdcBLLException(VdcBllErrors.IRS_IMAGE_STATUS_ILLEGAL);
-                }
-                getReturnValue().setFault(vdcReturnValue.getFault());
-                log.errorFormat("Failed to create stateless snapshot for VM 
{0} ({1})",
-                        getVm().getName(), getVm().getId());
+            if (areDisksLocked(vdcReturnValue)) {
+                throw new 
VdcBLLException(VdcBllErrors.IRS_IMAGE_STATUS_ILLEGAL);
             }
+            getReturnValue().setFault(vdcReturnValue.getFault());
+            log.errorFormat("Failed to create stateless snapshot for VM {0} 
({1})",
+                    getVm().getName(), getVm().getId());
         }
     }
 
@@ -404,7 +440,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);
@@ -473,10 +508,10 @@
     public AuditLogType getAuditLogTypeValue() {
         switch (getActionState()) {
         case EXECUTE:
-            if (isFailedStatlessSnapshot) {
+            if (getFlow() == Flow.REMOVE_STATELESS_IMAGES) {
                 return 
AuditLogType.USER_RUN_VM_FAILURE_STATELESS_SNAPSHOT_LEFT;
             }
-            if (mResume) {
+            if (getFlow() == Flow.RESUME) {
                 return getSucceeded() ? AuditLogType.USER_RESUME_VM : 
AuditLogType.USER_FAILED_RESUME_VM;
             } else if (isInternalExecution()) {
                 if (getSucceeded()) {
@@ -590,7 +625,7 @@
     }
 
     protected Guid initVds() {
-        Guid selectedVdsId = resume ? getVm().getRunOnVds() : schedule();
+        Guid selectedVdsId = getFlow() == Flow.RESUME ? getVm().getRunOnVds() 
: schedule();
         initVds(selectedVdsId);
         return selectedVdsId;
     }


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I350e263bb326c8d3c6be78b2d5b2bd1a3bd0535f
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