Arik Hadas has uploaded a new change for review.

Change subject: core: [WIP] memory state handling on preview/commit/undo
......................................................................

core: [WIP] memory state handling on preview/commit/undo

Change-Id: I58a51cd0ce1cbc04f69ce71c6a7e4589da6acdd6
Signed-off-by: Arik Hadas <[email protected]>
---
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RestoreAllSnapshotsCommand.java
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/TryBackToAllSnapshotsOfVmCommand.java
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/snapshots/SnapshotsManager.java
3 files changed, 118 insertions(+), 26 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/07/14707/1

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RestoreAllSnapshotsCommand.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RestoreAllSnapshotsCommand.java
index edeee1b..fea3703 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RestoreAllSnapshotsCommand.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RestoreAllSnapshotsCommand.java
@@ -26,6 +26,7 @@
 import org.ovirt.engine.core.common.action.RestoreFromSnapshotParameters;
 import org.ovirt.engine.core.common.action.VdcActionType;
 import org.ovirt.engine.core.common.action.VdcReturnValueBase;
+import org.ovirt.engine.core.common.businessentities.Disk;
 import org.ovirt.engine.core.common.businessentities.DiskImage;
 import org.ovirt.engine.core.common.businessentities.ImageStatus;
 import org.ovirt.engine.core.common.businessentities.Snapshot;
@@ -35,10 +36,15 @@
 import org.ovirt.engine.core.common.errors.VdcBllErrors;
 import org.ovirt.engine.core.common.locks.LockingGroup;
 import org.ovirt.engine.core.common.utils.Pair;
+import 
org.ovirt.engine.core.common.vdscommands.DeleteImageGroupVDSCommandParameters;
+import org.ovirt.engine.core.common.vdscommands.VDSCommandType;
+import org.ovirt.engine.core.common.vdscommands.VDSReturnValue;
 import org.ovirt.engine.core.compat.Guid;
 import org.ovirt.engine.core.compat.NGuid;
 import org.ovirt.engine.core.dal.VdcBllMessages;
 import org.ovirt.engine.core.dao.SnapshotDao;
+import org.ovirt.engine.core.utils.linq.LinqUtils;
+import org.ovirt.engine.core.utils.linq.Predicate;
 
 /**
  * Restores the given snapshot, including all the VM configuration that was 
stored in it.<br>
@@ -125,8 +131,47 @@
 
     protected void removeSnapshotsFromDB() {
         for (Guid snapshotId : snapshotsToRemove) {
+            Snapshot snapshot = getSnapshotDao().get(snapshotId);
+            if (!snapshot.getMemoryVolume().isEmpty()) {
+                removeMemory(snapshot);
+            }
             getSnapshotDao().remove(snapshotId);
         }
+    }
+
+    private void removeMemory(final Snapshot snapshot) {
+        String[] strings = snapshot.getMemoryVolume().split(",");
+        Guid[] guids = new Guid[strings.length];
+        for (int i=0; i<strings.length; ++i) {
+            guids[i]= new Guid(strings[i]);
+        }
+        // get all vm disks in order to check post zero - if one of the
+        // disks is marked with wipe_after_delete
+        boolean postZero =
+                LinqUtils.filter(getDiskDao().getAllForVm(getVm().getId()),
+                        new Predicate<Disk>() {
+                    @Override
+                    public boolean eval(Disk disk) {
+                        return disk.isWipeAfterDelete();
+                    }
+                }).size() > 0;
+
+                // delete first image
+                // the next 'DeleteImageGroup' command should also take care 
of the
+                // image removal:
+                VDSReturnValue vdsRetValue1 = runVdsCommand(
+                        VDSCommandType.DeleteImageGroup,
+                        new DeleteImageGroupVDSCommandParameters(guids[1], 
guids[0], guids[2],
+                                postZero, false, 
getVm().getVdsGroupCompatibilityVersion().toString()));
+
+                if (!vdsRetValue1.getSucceeded()) {
+                    // TODO
+                }
+                else {
+                    Guid guid1 =
+                            createTask(vdsRetValue1.getCreationInfo(), 
VdcActionType.RemoveImage, VdcObjectType.Storage, guids[0]);
+                    getReturnValue().getTaskIdList().add(guid1);
+                }
     }
 
     protected void deleteOrphanedImages() {
@@ -218,7 +263,7 @@
      */
     private void restoreConfiguration(Snapshot targetSnapshot) {
         SnapshotsManager snapshotsManager = new SnapshotsManager();
-        removedSnapshotId = getSnapshotDao().getId(getVmId(), 
SnapshotType.ACTIVE);
+        removedSnapshotId = getSnapshotDao().getId(getVmId(), 
SnapshotType.ACTIVE);;
         snapshotsToRemove.add(removedSnapshotId);
         snapshotsManager.removeAllIllegalDisks(removedSnapshotId, getVmId());
 
@@ -228,7 +273,11 @@
                 getCompensationContext(), 
getVm().getVdsGroupCompatibilityVersion());
         getSnapshotDao().remove(targetSnapshot.getId());
         // add active snapshot with status locked, so that other commands that 
depend on the VM's snapshots won't run in parallel
-        snapshotsManager.addActiveSnapshot(targetSnapshot.getId(), getVm(), 
SnapshotStatus.LOCKED, getCompensationContext());
+        snapshotsManager.addActiveSnapshot(targetSnapshot.getId(),
+                getVm(),
+                SnapshotStatus.LOCKED,
+                targetSnapshot.getMemoryVolume(),
+                getCompensationContext());
     }
 
     /**
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/TryBackToAllSnapshotsOfVmCommand.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/TryBackToAllSnapshotsOfVmCommand.java
index 56bb4fb..7d41a23 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/TryBackToAllSnapshotsOfVmCommand.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/TryBackToAllSnapshotsOfVmCommand.java
@@ -40,6 +40,7 @@
 public class TryBackToAllSnapshotsOfVmCommand<T extends 
TryBackToAllSnapshotsOfVmParameters> extends VmCommand<T> {
 
     private final SnapshotsManager snapshotsManager = new SnapshotsManager();
+    private Snapshot cachedSnapshot;
 
     /**
      * Constructor for command creation when compensation is applied on startup
@@ -67,12 +68,13 @@
 
     @Override
     protected void endWithFailure() {
-        Guid previouslyActiveSnapshotId =
-                getSnapshotDao().getId(getVmId(), SnapshotType.PREVIEW, 
SnapshotStatus.LOCKED);
-        getSnapshotDao().remove(previouslyActiveSnapshotId);
+        Snapshot previouslyActiveSnapshot =
+                getSnapshotDao().get(getVmId(), SnapshotType.PREVIEW, 
SnapshotStatus.LOCKED);
+        getSnapshotDao().remove(previouslyActiveSnapshot.getId());
         getSnapshotDao().remove(getSnapshotDao().getId(getVmId(), 
SnapshotType.ACTIVE));
 
-        snapshotsManager.addActiveSnapshot(previouslyActiveSnapshotId, 
getVm(), getCompensationContext());
+        snapshotsManager.addActiveSnapshot(previouslyActiveSnapshot.getId(), 
getVm(),
+                previouslyActiveSnapshot.getMemoryVolume(), 
getCompensationContext());
 
         super.endWithFailure();
     }
@@ -105,7 +107,7 @@
                 SnapshotStatus.OK);
 
         snapshotsManager.attempToRestoreVmConfigurationFromSnapshot(getVm(),
-                getSnapshotDao().get(getParameters().getDstSnapshotId()),
+                getDstSnapshot(),
                 getSnapshotDao().getId(getVm().getId(), SnapshotType.ACTIVE),
                 getCompensationContext(), 
getVm().getVdsGroupCompatibilityVersion());
     }
@@ -114,10 +116,11 @@
     protected void executeVmCommand() {
 
         final Guid newActiveSnapshotId = Guid.NewGuid();
+        final Snapshot snapshotToBePreviewed = getDstSnapshot();
         final List<DiskImage> images = DbFacade
                 .getInstance()
                 .getDiskImageDao()
-                
.getAllSnapshotsForVmSnapshot(getParameters().getDstSnapshotId());
+                .getAllSnapshotsForVmSnapshot(snapshotToBePreviewed.getId());
 
         TransactionSupport.executeInNewTransaction(new 
TransactionMethod<Void>() {
             @Override
@@ -130,10 +133,12 @@
                         "Active VM before the preview",
                         SnapshotType.PREVIEW,
                         getVm(),
+                        previousActiveSnapshot.getMemoryVolume(),
                         getCompensationContext());
-                snapshotsManager.addActiveSnapshot(newActiveSnapshotId, 
getVm(), getCompensationContext());
-                //if there are no images there's no reason the save the 
compensation data to DB as
-                //the update is being executed in the same transaction so we 
can restore the vm config and end the command.
+                snapshotsManager.addActiveSnapshot(newActiveSnapshotId, 
getVm(),
+                        snapshotToBePreviewed.getMemoryVolume(), 
getCompensationContext());
+                // if there are no images there's no reason to save the 
compensation data to DB as the update is
+                // being executed in the same transaction so we can restore 
the vm config and end the command.
                 if (!images.isEmpty()) {
                     getCompensationContext().stateChanged();
                 } else {
@@ -144,37 +149,41 @@
             }
         });
 
-        if (images.size() > 0) {
+        if (!images.isEmpty()) {
             VmHandler.LockVm(getVm().getDynamicData(), 
getCompensationContext());
             freeLock();
             TransactionSupport.executeInNewTransaction(new 
TransactionMethod<Void>() {
                 @Override
                 public Void runInTransaction() {
                     for (DiskImage image : images) {
-                        ImagesContainterParametersBase tempVar = new 
ImagesContainterParametersBase(image.getImageId());
-                        
tempVar.setParentCommand(VdcActionType.TryBackToAllSnapshotsOfVm);
-                        tempVar.setVmSnapshotId(newActiveSnapshotId);
-                        tempVar.setEntityId(getParameters().getEntityId());
-                        tempVar.setParentParameters(getParameters());
-                        tempVar.setQuotaId(image.getQuotaId());
-                        ImagesContainterParametersBase p = tempVar;
                         VdcReturnValueBase vdcReturnValue =
                                 
Backend.getInstance().runInternalAction(VdcActionType.TryBackToSnapshot,
-                                        p,
+                                        
buildTryBackToSnapshotParameters(newActiveSnapshotId, image),
                                         
ExecutionHandler.createDefaultContexForTasks(getExecutionContext()));
 
                         if (vdcReturnValue.getSucceeded()) {
                             
getTaskIdList().addAll(vdcReturnValue.getInternalTaskIdList());
                         } else if (vdcReturnValue.getFault() != null) {
                             // if we have a fault, forward it to the user
-                            throw new 
VdcBLLException(vdcReturnValue.getFault().getError(), vdcReturnValue.getFault()
-                                    .getMessage());
+                            throw new 
VdcBLLException(vdcReturnValue.getFault().getError(),
+                                    vdcReturnValue.getFault().getMessage());
                         } else {
                             log.error("Cannot create snapshot");
                             throw new 
VdcBLLException(VdcBllErrors.IRS_IMAGE_STATUS_ILLEGAL);
                         }
                     }
                     return null;
+                }
+
+                private ImagesContainterParametersBase 
buildTryBackToSnapshotParameters(
+                        final Guid newActiveSnapshotId, DiskImage image) {
+                    ImagesContainterParametersBase params = new 
ImagesContainterParametersBase(image.getImageId());
+                    
params.setParentCommand(VdcActionType.TryBackToAllSnapshotsOfVm);
+                    params.setVmSnapshotId(newActiveSnapshotId);
+                    params.setEntityId(getParameters().getEntityId());
+                    params.setParentParameters(getParameters());
+                    params.setQuotaId(image.getQuotaId());
+                    return params;
                 }
             });
         }
@@ -199,7 +208,7 @@
 
     @Override
     protected boolean canDoAction() {
-        Snapshot snapshot = 
getSnapshotDao().get(getParameters().getDstSnapshotId());
+        Snapshot snapshot = getDstSnapshot();
         SnapshotsValidator snapshotsValidator = new SnapshotsValidator();
         VmValidator vmValidator = new VmValidator(getVm());
         if (!validate(vmValidator.vmDown())
@@ -247,6 +256,13 @@
         return true;
     }
 
+    private Snapshot getDstSnapshot() {
+        if (cachedSnapshot == null) {
+            cachedSnapshot = 
getSnapshotDao().get(getParameters().getDstSnapshotId());
+        }
+        return cachedSnapshot;
+    }
+
     @Override
     protected void setActionMessageParameters() {
         addCanDoActionMessage(VdcBllMessages.VAR__ACTION__PREVIEW);
@@ -271,7 +287,7 @@
     @Override
     public String getSnapshotName() {
         if (super.getSnapshotName() == null) {
-            final Snapshot snapshot = 
getSnapshotDao().get(getParameters().getDstSnapshotId());
+            final Snapshot snapshot = getDstSnapshot();
             if (snapshot != null) {
                 setSnapshotName(snapshot.getDescription());
             }
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/snapshots/SnapshotsManager.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/snapshots/SnapshotsManager.java
index 251b9da..d12df75 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/snapshots/SnapshotsManager.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/snapshots/SnapshotsManager.java
@@ -68,6 +68,29 @@
         return addActiveSnapshot(snapshotId,
                 vm,
                 SnapshotStatus.OK,
+                "",
+                compensationContext);
+    }
+
+    public Snapshot addActiveSnapshot(Guid snapshotId,
+            VM vm,
+            SnapshotStatus snapshotStatus,
+            final CompensationContext compensationContext) {
+        return addActiveSnapshot(snapshotId,
+                vm,
+                snapshotStatus,
+                "",
+                compensationContext);
+    }
+
+    public Snapshot addActiveSnapshot(Guid snapshotId,
+            VM vm,
+            String memoryVolume,
+            final CompensationContext compensationContext) {
+        return addActiveSnapshot(snapshotId,
+                vm,
+                SnapshotStatus.OK,
+                memoryVolume,
                 compensationContext);
     }
 
@@ -87,6 +110,7 @@
     public Snapshot addActiveSnapshot(Guid snapshotId,
             VM vm,
             SnapshotStatus snapshotStatus,
+            String memoryVolume,
             final CompensationContext compensationContext) {
         return addSnapshot(snapshotId,
                 "Active VM",
@@ -94,7 +118,7 @@
                 SnapshotType.ACTIVE,
                 vm,
                 false,
-                StringUtils.EMPTY,
+                memoryVolume,
                 compensationContext);
     }
 
@@ -111,6 +135,8 @@
      *            The snapshot type.
      * @param vm
      *            The VM to save in configuration.
+     * @param memoryVolume
+     *            the volume in which the snapshot's memory is stored
      * @param compensationContext
      *            Context for saving compensation details.
      * @return the added snapshot
@@ -119,9 +145,10 @@
             String description,
             SnapshotType snapshotType,
             VM vm,
+            String memoryVolume,
             final CompensationContext compensationContext) {
         return addSnapshot(snapshotId, description, SnapshotStatus.LOCKED,
-                snapshotType, vm, true, StringUtils.EMPTY, 
compensationContext);
+                snapshotType, vm, true, memoryVolume, compensationContext);
     }
 
     /**


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I58a51cd0ce1cbc04f69ce71c6a7e4589da6acdd6
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Arik Hadas <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to