Kobi Ianko has uploaded a new change for review.

Change subject: core: Enhancing the quota to apply to internal commands
......................................................................

core: Enhancing the quota to apply to internal commands

The Hot Plug feature has introduced an internal command that consumes quota,
to handle that option a new annotation is introduced, 
QuotaVdsDependentForInternalCommand.
Marking the internal command with the annotation will enable it to consume 
quota.

Change-Id: I3e4a050cb845165630741c4857f0ba7de12fa60e
Bug-Url: https://bugzilla.redhat.com/1083177
Signed-off-by: Kobi Ianko <k...@redhat.com>
---
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/CommandBase.java
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/HotSetNumberOfCpusCommand.java
A 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/quota/QuotaVdsDependentForInternalCommand.java
M 
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/action/VdcActionType.java
4 files changed, 64 insertions(+), 4 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/54/26654/1

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/CommandBase.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/CommandBase.java
index e5777e8..b0c0969 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/CommandBase.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/CommandBase.java
@@ -34,6 +34,7 @@
 import org.ovirt.engine.core.bll.quota.QuotaManager;
 import org.ovirt.engine.core.bll.quota.QuotaStorageDependent;
 import org.ovirt.engine.core.bll.quota.QuotaVdsDependent;
+import org.ovirt.engine.core.bll.quota.QuotaVdsDependentForInternalCommand;
 import org.ovirt.engine.core.bll.session.SessionDataContainer;
 import org.ovirt.engine.core.bll.tasks.AsyncTaskUtils;
 import org.ovirt.engine.core.bll.tasks.SPMAsyncTaskHandler;
@@ -762,8 +763,9 @@
     }
 
     private boolean internalValidateAndSetQuota() {
-        // Quota accounting is done only in the most external Command.
-        if (isInternalExecution() || !isQuotaDependant()) {
+        // Quota accounting is done only in the most external Command,
+        // Quota will account to an internal command only if it is marked as 
@QuotaVdsDependentForInternalCommand
+        if ((isInternalExecution() && !isQuotaInternalDependent()) || 
!isQuotaDependant()) {
             return true;
         }
 
@@ -794,6 +796,10 @@
         return result;
     }
 
+    private boolean isQuotaInternalDependent() {
+        return 
(this.getClass().isAnnotationPresent(QuotaVdsDependentForInternalCommand.class))
 ? true : false;
+    }
+
     private boolean isQuotaDependant() {
         return getActionType().getQuotaDependency() != 
VdcActionType.QuotaDependency.NONE;
     }
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/HotSetNumberOfCpusCommand.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/HotSetNumberOfCpusCommand.java
index 3a4cd46..b456250 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/HotSetNumberOfCpusCommand.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/HotSetNumberOfCpusCommand.java
@@ -1,5 +1,12 @@
 package org.ovirt.engine.core.bll;
 
+import java.util.ArrayList;
+import java.util.List;
+
+import org.ovirt.engine.core.bll.quota.QuotaConsumptionParameter;
+import org.ovirt.engine.core.bll.quota.QuotaVdsDependent;
+import org.ovirt.engine.core.bll.quota.QuotaVdsDependentForInternalCommand;
+import org.ovirt.engine.core.bll.quota.QuotaVdsGroupConsumptionParameter;
 import org.ovirt.engine.core.bll.scheduling.SlaValidator;
 import org.ovirt.engine.core.bll.validator.LocalizedVmStatus;
 import org.ovirt.engine.core.common.AuditLogType;
@@ -21,7 +28,8 @@
  * The execute will never throw an exception. it will rather wrap a return 
value in case of failure.
  */
 @NonTransactiveCommandAttribute
-public class HotSetNumberOfCpusCommand<T extends HotSetNumerOfCpusParameters> 
extends VmManagementCommandBase<T> {
+@QuotaVdsDependentForInternalCommand
+public class HotSetNumberOfCpusCommand<T extends HotSetNumerOfCpusParameters> 
extends VmManagementCommandBase<T> implements QuotaVdsDependent {
 
     public static final String LOGABLE_FIELD_NUMBER_OF_CPUS = "numberOfCpus";
     public static final String LOGABLE_FIELD_ERROR_MESSAGE = "ErrorMessage";
@@ -98,4 +106,33 @@
         addCanDoActionMessage(String.format("$clusterVersion %1$s", 
getVm().getVdsGroupCompatibilityVersion() ));
         addCanDoActionMessage(String.format("$architecture %1$s", 
getVm().getClusterArch()));
     }
+
+    @Override
+    public List<QuotaConsumptionParameter> getQuotaVdsConsumptionParameters() {
+        List<QuotaConsumptionParameter> list = new ArrayList<>();
+
+        // Calculate the change in CPU consumption, result above Zero means we 
add CPUs to the VM
+        // result bellow Zero means we subtracted CPUs from the VM
+        int cpuToConsume = getParameters().getVm().getNumOfCpus() - 
getVm().getNumOfCpus();
+
+        if (cpuToConsume > 0) {
+            // Consume CPU quota
+            list.add(new 
QuotaVdsGroupConsumptionParameter(getVm().getQuotaId(),
+                    null,
+                    QuotaConsumptionParameter.QuotaAction.CONSUME,
+                    getVm().getVdsGroupId(),
+                    getVm().getCpuPerSocket() * cpuToConsume,
+                    0));
+
+        } else if (cpuToConsume < 0) {
+            // Release CPU quota
+            list.add(new 
QuotaVdsGroupConsumptionParameter(getVm().getQuotaId(),
+                    null,
+                    QuotaConsumptionParameter.QuotaAction.RELEASE,
+                    getVm().getVdsGroupId(),
+                    getVm().getCpuPerSocket() * Math.abs(cpuToConsume),
+                    0));
+        }
+        return list;
+    }
 }
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/quota/QuotaVdsDependentForInternalCommand.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/quota/QuotaVdsDependentForInternalCommand.java
new file mode 100644
index 0000000..2348661
--- /dev/null
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/quota/QuotaVdsDependentForInternalCommand.java
@@ -0,0 +1,17 @@
+package org.ovirt.engine.core.bll.quota;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Usually in internal commands we do not want to calculate quota consumption, 
but there are cases which we do. To
+ * indicate the the internal command should be calculated for quota, use this 
annotation in a Class level
+ */
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.TYPE)
+public @interface QuotaVdsDependentForInternalCommand {
+
+}
diff --git 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/action/VdcActionType.java
 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/action/VdcActionType.java
index 2bd8d96..8f75c5e 100644
--- 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/action/VdcActionType.java
+++ 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/action/VdcActionType.java
@@ -41,7 +41,7 @@
     DetachDiskFromVm(181, ActionGroup.CONFIGURE_VM_STORAGE, false, 
QuotaDependency.NONE),
     HotPlugDiskToVm(182, ActionGroup.CONFIGURE_VM_STORAGE, false, 
QuotaDependency.NONE),
     HotUnPlugDiskFromVm(183, ActionGroup.CONFIGURE_VM_STORAGE, false, 
QuotaDependency.NONE),
-    HotSetNumberOfCpus(184, ActionGroup.EDIT_VM_PROPERTIES, false, 
QuotaDependency.NONE),
+    HotSetNumberOfCpus(184, ActionGroup.EDIT_VM_PROPERTIES, false, 
QuotaDependency.VDS_GROUP),
     ChangeFloppy(35, QuotaDependency.NONE),
     ImportVm(36, ActionGroup.IMPORT_EXPORT_VM, QuotaDependency.STORAGE),
     RemoveVmFromImportExport(37, ActionGroup.DELETE_VM, QuotaDependency.NONE),


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

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

Reply via email to