ofri masad has uploaded a new change for review.

Change subject: core: Fix handle quota release (#848107)
......................................................................

core: Fix handle quota release (#848107)

https://bugzilla.redhat.com/848107

Add a release for storage quota when a vm is stopped or removed.

The caching of the quota is actuly dropped - so the next time it will be
called it will be recalculated from the DB.

Change-Id: I9e80028d4c3bf3821a12cbfd164db614a416c464
Signed-off-by: Ofri Masad <[email protected]>
---
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RemoveVmCommand.java
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/StopVmCommandBase.java
M 
backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfVmReader.java
M 
backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfVmWriter.java
4 files changed, 96 insertions(+), 14 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/37/7437/1

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RemoveVmCommand.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RemoveVmCommand.java
index 0617fba..8fcc6ef 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RemoveVmCommand.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RemoveVmCommand.java
@@ -1,23 +1,20 @@
 package org.ovirt.engine.core.bll;
 
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 import org.apache.commons.lang.StringUtils;
 import org.ovirt.engine.core.bll.job.ExecutionHandler;
+import org.ovirt.engine.core.bll.quota.QuotaManager;
 import org.ovirt.engine.core.bll.snapshots.SnapshotsManager;
 import org.ovirt.engine.core.bll.snapshots.SnapshotsValidator;
 import org.ovirt.engine.core.common.AuditLogType;
+import org.ovirt.engine.core.common.PermissionSubject;
+import org.ovirt.engine.core.common.Quotable;
 import org.ovirt.engine.core.common.action.RemoveAllVmImagesParameters;
 import org.ovirt.engine.core.common.action.RemoveVmParameters;
 import org.ovirt.engine.core.common.action.VdcActionType;
 import org.ovirt.engine.core.common.action.VdcReturnValueBase;
-import org.ovirt.engine.core.common.businessentities.DiskImage;
-import org.ovirt.engine.core.common.businessentities.LunDisk;
-import org.ovirt.engine.core.common.businessentities.VM;
-import org.ovirt.engine.core.common.businessentities.VMStatus;
+import org.ovirt.engine.core.common.businessentities.*;
 import org.ovirt.engine.core.common.locks.LockingGroup;
 import org.ovirt.engine.core.compat.Guid;
 import org.ovirt.engine.core.dal.VdcBllMessages;
@@ -27,7 +24,7 @@
 
 @LockIdNameAttribute
 @NonTransactiveCommandAttribute(forceCompensation = true)
-public class RemoveVmCommand<T extends RemoveVmParameters> extends 
VmCommand<T> {
+public class RemoveVmCommand<T extends RemoveVmParameters> extends 
VmCommand<T> implements Quotable{
 
     private static final long serialVersionUID = -3202434016040084728L;
     private boolean hasImages;
@@ -45,6 +42,7 @@
     public RemoveVmCommand(T parameters) {
         super(parameters);
         parameters.setEntityId(getVmId());
+        setStoragePoolId(getVm().getstorage_pool_id());
     }
 
     @Override
@@ -251,4 +249,38 @@
             AddCustomValue("DisksNames", StringUtils.join(disksLeftInVm, ","));
         }
     }
+
+    @Override
+    public boolean validateAndSetQuota() {
+        this.rollbackQuota();
+        return true;
+    }
+
+    @Override
+    public void rollbackQuota() {
+        Guid quotaId = getVm().getQuotaId();
+
+        if (quotaId != null) {
+            // Uncache the details of this quota. next time the quota will be 
called, a new calculation
+            // would be done base on the DB.
+            QuotaManager.getInstance().rollbackQuota(getStoragePool(), 
Arrays.asList(quotaId));
+        }
+        for (DiskImage image : getVm().getDiskList()){
+            quotaId = image.getQuotaId();
+
+            if (quotaId != null) {
+                QuotaManager.getInstance().rollbackQuota(getStoragePool(), 
Arrays.asList(quotaId));
+            }
+        }
+    }
+
+    @Override
+    public Guid getQuotaId() {
+        return null;
+    }
+
+    @Override
+    public void addQuotaPermissionSubject(List<PermissionSubject> 
quotaPermissionList) {
+        //
+    }
 }
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/StopVmCommandBase.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/StopVmCommandBase.java
index 336c4cf..722f75d 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/StopVmCommandBase.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/StopVmCommandBase.java
@@ -1,12 +1,13 @@
 package org.ovirt.engine.core.bll;
 
+import java.util.Arrays;
 import java.util.List;
 
+import org.ovirt.engine.core.bll.quota.QuotaManager;
+import org.ovirt.engine.core.common.PermissionSubject;
+import org.ovirt.engine.core.common.Quotable;
 import org.ovirt.engine.core.common.action.VmOperationParameterBase;
-import org.ovirt.engine.core.common.businessentities.VM;
-import org.ovirt.engine.core.common.businessentities.VMStatus;
-import org.ovirt.engine.core.common.businessentities.VmDevice;
-import org.ovirt.engine.core.common.businessentities.VmDynamic;
+import org.ovirt.engine.core.common.businessentities.*;
 import org.ovirt.engine.core.common.utils.VmDeviceCommonUtils;
 import org.ovirt.engine.core.common.vdscommands.DestroyVmVDSCommandParameters;
 import 
org.ovirt.engine.core.common.vdscommands.UpdateVmDynamicDataVDSCommandParameters;
@@ -18,7 +19,7 @@
 import org.ovirt.engine.core.utils.log.Log;
 import org.ovirt.engine.core.utils.log.LogFactory;
 
-public abstract class StopVmCommandBase<T extends VmOperationParameterBase> 
extends VmOperationCommandBase<T> {
+public abstract class StopVmCommandBase<T extends VmOperationParameterBase> 
extends VmOperationCommandBase<T> implements Quotable {
     private boolean privateSuspendedVm;
 
     public StopVmCommandBase(T parameters) {
@@ -166,4 +167,44 @@
 
     private static Log log = LogFactory.getLog(StopVmCommandBase.class);
 
+    @Override
+    public boolean validateAndSetQuota() {
+        return true;
+    }
+
+    @Override
+    public void rollbackQuota() {
+        Guid quotaId = getVm().getQuotaId();
+        if (quotaId != null) {
+            // Uncache the details of this quota. next time the quota will be 
called, a new calculation
+            // would be done base on the DB.
+            QuotaManager.getInstance().rollbackQuota(getStoragePool(), 
Arrays.asList(quotaId));
+        }
+        VmHandler.updateDisksFromDb(getVm());
+        for (DiskImage image : getVm().getDiskList()) {
+            quotaId = image.getQuotaId();
+
+            if (quotaId != null) {
+                QuotaManager.getInstance().rollbackQuota(getStoragePool(), 
Arrays.asList(quotaId));
+            }
+        }
+    }
+
+    @Override
+    public Guid getQuotaId() {
+        return null;
+    }
+
+    @Override
+    public void addQuotaPermissionSubject(List<PermissionSubject> 
quotaPermissionList) {
+        //
+    }
+
+    @Override
+    protected void setSucceeded(boolean value) {
+        super.setSucceeded(value);
+        if (value) {
+            rollbackQuota();
+        }
+    }
 }
diff --git 
a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfVmReader.java
 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfVmReader.java
index e910812..7f6f3bf 100644
--- 
a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfVmReader.java
+++ 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfVmReader.java
@@ -202,6 +202,10 @@
         if (node != null) {
             
_vm.getStaticData().setis_stateless(Boolean.parseBoolean(node.InnerText));
         }
+        node = content.SelectSingleNode("quota_id");
+        if (node != null) {
+            _vm.getStaticData().setQuotaId(new Guid(node.InnerText));
+        }
 
         XmlNodeList list = content.SelectNodes("Section");
         for (XmlNode section : list) {
diff --git 
a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfVmWriter.java
 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfVmWriter.java
index ad9680c..3b06cf6 100644
--- 
a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfVmWriter.java
+++ 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ovf/OvfVmWriter.java
@@ -90,6 +90,11 @@
             _writer.WriteRaw(_vm.getapp_list());
             _writer.WriteEndElement();
         }
+        if (_vm.getQuotaId() != null) {
+            _writer.WriteStartElement("quota_id");
+            _writer.WriteRaw(_vm.getQuotaId().toString());
+            _writer.WriteEndElement();
+        }
         _writer.WriteStartElement("VmType");
         _writer.WriteRaw(String.valueOf(_vm.getvm_type().getValue()));
         _writer.WriteEndElement();


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

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

Reply via email to