You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by GitBox <gi...@apache.org> on 2022/12/02 14:29:51 UTC

[GitHub] [cloudstack] nvazquez commented on a diff in pull request #6899: Count Resource Virtual Router

nvazquez commented on code in PR #6899:
URL: https://github.com/apache/cloudstack/pull/6899#discussion_r1038193298


##########
engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java:
##########
@@ -1398,15 +1405,152 @@ private void logBootModeParameters(Map<VirtualMachineProfile.Param, Object> para
         }
     }
 
+    /**
+     * Returns the service offering by the given configuration.
+     *
+     * @param configName name of the config
+     * @return the service offering found or null if not found
+     */
+    public ServiceOffering getServiceOfferingByConfig(String configName) {
+        ServiceOffering defaultRouterOffering = null;
+        final String globalRouterOffering = configDao.getValue(configName);

Review Comment:
   Instead of the configDao, can we access the setting via `VirtualNetworkApplianceManager.VirtualRouterServiceOffering` (for `router.service.offering`)?



##########
engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java:
##########
@@ -1398,15 +1405,152 @@ private void logBootModeParameters(Map<VirtualMachineProfile.Param, Object> para
         }
     }
 
+    /**
+     * Returns the service offering by the given configuration.
+     *
+     * @param configName name of the config
+     * @return the service offering found or null if not found
+     */
+    public ServiceOffering getServiceOfferingByConfig(String configName) {
+        ServiceOffering defaultRouterOffering = null;
+        final String globalRouterOffering = configDao.getValue(configName);
+
+        if (globalRouterOffering != null) {
+            defaultRouterOffering = _serviceOfferingDao.findByUuid(globalRouterOffering);
+        }
+
+        if (defaultRouterOffering == null) {
+            defaultRouterOffering =  _serviceOfferingDao.findByName(ServiceOffering.routerDefaultOffUniqueName);
+        }
+
+        return defaultRouterOffering;
+    }
+
+    /**
+     * Counts VR resources for the domain if global setting is true.
+     * if value is "all" count all VR resources else get diff of
+     * current VR offering and default VR offering
+     *
+     * @param offering VR service offering
+     * @param defaultRouterOffering default VR service offering
+     * @param owner account
+     * @return a Pair of cpu and ram
+     */
+    private Pair<Long, Long> resolveCpuAndMemoryCount(ServiceOffering offering, ServiceOffering defaultRouterOffering, Account owner) {
+        Integer cpuCount = 0;
+        Integer memoryCount = 0;
+        if (COUNT_ALL_VR_RESOURCES.equalsIgnoreCase(ResourceCountRoutersType.valueIn(owner.getDomainId()))) {
+            cpuCount = offering.getCpu();
+            memoryCount = offering.getRamSize();
+        } else if (COUNT_DELTA_VR_RESOURCES.equalsIgnoreCase(ResourceCountRoutersType.valueIn(owner.getDomainId()))) {
+            // Default offering value can be greater than current offering value
+            if (offering.getCpu() >= defaultRouterOffering.getCpu()) {
+                cpuCount = offering.getCpu() - defaultRouterOffering.getCpu();
+            }
+            if (offering.getRamSize() >= defaultRouterOffering.getRamSize()) {
+                memoryCount = offering.getRamSize() - defaultRouterOffering.getRamSize();
+            }
+        }
+
+        return Pair.of(cpuCount.longValue(), memoryCount.longValue());
+    }
+
+    private void validateResourceCount(Pair<Long, Long> cpuMemoryCount, Account owner) {
+        final Long cpuCount = cpuMemoryCount.first();
+        final Long memoryCount = cpuMemoryCount.second();
+        try {
+            if (cpuCount > 0) {
+                _resourceLimitMgr.checkResourceLimit(owner, ResourceType.cpu, cpuCount);
+            }
+            if (memoryCount > 0) {
+                _resourceLimitMgr.checkResourceLimit(owner, ResourceType.memory, memoryCount);
+            }
+        } catch (ResourceAllocationException ex) {
+            throw new CloudRuntimeException(String.format("Unable to deploy/start routers due to {}", ex.getMessage()));
+        }
+    }
+
+    /**
+     * Check if resource count can be allocated to account/domain
+     *
+     * @param cpuMemoryCount a Pair of cpu and ram
+     * @param owner the account
+     */
+    private void calculateResourceCount(Pair<Long, Long> cpuMemoryCount, Account owner, boolean isIncrement) {
+        validateResourceCount(cpuMemoryCount, owner);
+        final Long cpuCount = cpuMemoryCount.first();
+        final Long memoryCount = cpuMemoryCount.second();
+
+        // Increment the resource count
+        if (s_logger.isDebugEnabled()) {
+            if(isIncrement) {
+                s_logger.debug(String.format("Incrementing the CPU count with value %s and RAM value with %s", cpuCount, memoryCount));
+            } else {
+                s_logger.debug(String.format("Decrementing cpu resource count with value %s and memory resource with value %s",cpuCount, memoryCount));
+            }
+        }
+
+        if(isIncrement) {
+            _resourceLimitMgr.incrementResourceCount(owner.getAccountId(), ResourceType.cpu, cpuCount);
+            _resourceLimitMgr.incrementResourceCount(owner.getAccountId(), ResourceType.memory, memoryCount);
+        } else {
+            _resourceLimitMgr.decrementResourceCount(owner.getAccountId(), ResourceType.cpu, cpuCount);
+            _resourceLimitMgr.decrementResourceCount(owner.getAccountId(), ResourceType.memory, memoryCount);
+        }
+    }
+
+    /**
+     * Function to increment the VR resource count
+     * If the global setting resource.count.router is true then the VR
+     * resource count will be considered as well
+     * If the global setting resource.count.router.type is "all" then
+     * all VR resource count will be considered else the diff between
+     * the current VR service offering and the default offering will
+     * be considered.
+     * During router deployment/destroy, we increment the resource
+     * count only if resource.count.running.vms is false else
+     * we increment it during VR start/stop. Same applies for
+     * decrementing resource count.
+     *
+     * @param offering VR service offering
+     * @param owner account
+     * @param isDeployOrDestroy true if router is being deployed/destroyed
+     */
+    @Override
+    public void incrementVrResourceCount(ServiceOffering offering, Account owner, boolean isDeployOrDestroy) {
+        if (isDeployOrDestroy == Boolean.TRUE.equals(ResourceCountRunningVMsonly.value())) {
+            return;
+        }
+
+        final ServiceOffering defaultRouterOffering = getServiceOfferingByConfig("router.service.offering");

Review Comment:
   Actually here



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org