From 33857e21e3c594a0685da90f2654898fe289c2b0 Mon Sep 17 00:00:00 2001 From: Nitin Mehta Date: Tue, 31 Jan 2012 12:31:18 +0530 Subject: [PATCH] Bug 11789: Created a function for calculation of allocated primary storage. The dashboard calculation would go through this. Reviewed-By: Kishan --- .../src/com/cloud/alert/AlertManagerImpl.java | 6 +- .../com/cloud/capacity/CapacityManager.java | 10 +++ .../cloud/capacity/CapacityManagerImpl.java | 66 +++++++++++++++++++ 3 files changed, 78 insertions(+), 4 deletions(-) diff --git a/server/src/com/cloud/alert/AlertManagerImpl.java b/server/src/com/cloud/alert/AlertManagerImpl.java index eb38c695f50..4dc7f8279dd 100755 --- a/server/src/com/cloud/alert/AlertManagerImpl.java +++ b/server/src/com/cloud/alert/AlertManagerImpl.java @@ -300,9 +300,7 @@ public class AlertManagerImpl implements AlertManager { // Calculate storage pool capacity List storagePools = _storagePoolDao.listAll(); for (StoragePoolVO pool : storagePools) { - long disk = 0l; - Pair sizes = _volumeDao.getCountAndTotalByPool(pool.getId()); - disk = sizes.second(); + long disk = _capacityMgr.getAllocatedPoolCapacity(pool, null); if (pool.isShared()){ _storageMgr.createCapacityEntry(pool, Capacity.CAPACITY_TYPE_STORAGE_ALLOCATED, disk); }else { @@ -312,7 +310,7 @@ public class AlertManagerImpl implements AlertManager { if (s_logger.isDebugEnabled()) { s_logger.debug("Done executing storage capacity update"); - s_logger.debug("Executing capacity updates public ip and Vlans"); + s_logger.debug("Executing capacity updates for public ip and Vlans"); } List datacenters = _dcDao.listAll(); diff --git a/server/src/com/cloud/capacity/CapacityManager.java b/server/src/com/cloud/capacity/CapacityManager.java index 5edbd77d25b..6eca36027ee 100755 --- a/server/src/com/cloud/capacity/CapacityManager.java +++ b/server/src/com/cloud/capacity/CapacityManager.java @@ -19,6 +19,8 @@ package com.cloud.capacity; import com.cloud.host.HostVO; +import com.cloud.storage.StoragePoolVO; +import com.cloud.storage.VMTemplateVO; import com.cloud.utils.component.Manager; import com.cloud.vm.VirtualMachine; @@ -43,4 +45,12 @@ public interface CapacityManager extends Manager { boolean checkIfHostHasCapacity(long hostId, Integer cpu, long ram, boolean checkFromReservedCapacity, float cpuOverprovisioningFactor, boolean considerReservedCapacity); void updateCapacityForHost(HostVO host); + + /** + * Returns the allocated capacity for the storage pool. If template is passed in it will include its size if its not already present on the pool + * @param pool storage pool + * @param templateForVmCreation template that will be used for vm creation + * @return total allocated capacity for the storage pool + */ + long getAllocatedPoolCapacity(StoragePoolVO pool, VMTemplateVO templateForVmCreation); } diff --git a/server/src/com/cloud/capacity/CapacityManagerImpl.java b/server/src/com/cloud/capacity/CapacityManagerImpl.java index f13503c7ce7..7563a0ebfa9 100755 --- a/server/src/com/cloud/capacity/CapacityManagerImpl.java +++ b/server/src/com/cloud/capacity/CapacityManagerImpl.java @@ -50,6 +50,15 @@ import com.cloud.resource.ResourceManager; import com.cloud.resource.ServerResource; import com.cloud.service.ServiceOfferingVO; import com.cloud.service.dao.ServiceOfferingDao; +import com.cloud.storage.StorageManager; +import com.cloud.storage.StoragePoolVO; +import com.cloud.storage.VMTemplateHostVO; +import com.cloud.storage.VMTemplateStoragePoolVO; +import com.cloud.storage.VMTemplateSwiftVO; +import com.cloud.storage.VMTemplateVO; +import com.cloud.storage.dao.VMTemplatePoolDao; +import com.cloud.storage.dao.VolumeDao; +import com.cloud.storage.swift.SwiftManager; import com.cloud.utils.DateUtil; import com.cloud.utils.NumbersUtil; import com.cloud.utils.Pair; @@ -79,14 +88,23 @@ public class CapacityManagerImpl implements CapacityManager, StateListener sizes = _volumeDao.getCountAndTotalByPool(pool.getId()); + long totalAllocatedSize = sizes.second() + sizes.first() * _extraBytesPerVolume; + + // Iterate through all templates on this storage pool + boolean tmpinstalled = false; + List templatePoolVOs; + templatePoolVOs = _templatePoolDao.listByPoolId(pool.getId()); + + for (VMTemplateStoragePoolVO templatePoolVO : templatePoolVOs) { + if ((templateForVmCreation != null) && !tmpinstalled && (templatePoolVO.getTemplateId() == templateForVmCreation.getId())) { + tmpinstalled = true; + } + long templateSize = templatePoolVO.getTemplateSize(); + totalAllocatedSize += templateSize + _extraBytesPerVolume; + } + + // Add the size for the templateForVmCreation if its not already present + if ((templateForVmCreation != null) && !tmpinstalled) { + // If the template that was passed into this allocator is not installed in the storage pool, + // add 3 * (template size on secondary storage) to the running total + VMTemplateHostVO templateHostVO = _storageMgr.findVmTemplateHost(templateForVmCreation.getId(), pool); + + if (templateHostVO == null) { + VMTemplateSwiftVO templateSwiftVO = _swiftMgr.findByTmpltId(templateForVmCreation.getId()); + if (templateSwiftVO != null) { + long templateSize = templateSwiftVO.getPhysicalSize(); + if (templateSize == 0) { + templateSize = templateSwiftVO.getSize(); + } + totalAllocatedSize += (templateSize + _extraBytesPerVolume); + } + } else { + long templateSize = templateHostVO.getPhysicalSize(); + if ( templateSize == 0 ){ + templateSize = templateHostVO.getSize(); + } + totalAllocatedSize += (templateSize + _extraBytesPerVolume); + } + } + + return totalAllocatedSize; + } + + @DB @Override public void updateCapacityForHost(HostVO host){