diff --git a/engine/schema/src/com/cloud/storage/dao/VolumeDao.java b/engine/schema/src/com/cloud/storage/dao/VolumeDao.java index 1f5083a8e14..24ade51c3b4 100755 --- a/engine/schema/src/com/cloud/storage/dao/VolumeDao.java +++ b/engine/schema/src/com/cloud/storage/dao/VolumeDao.java @@ -37,6 +37,8 @@ public interface VolumeDao extends GenericDao, StateDao getNonDestroyedCountAndTotalByPool(long poolId); + long getVMSnapshotSizeByPool(long poolId); + List findByInstance(long id); List findByInstanceAndType(long id, Volume.Type vType); diff --git a/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java b/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java index 54b6465642e..2fb9dbae07d 100755 --- a/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java +++ b/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java @@ -59,6 +59,7 @@ public class VolumeDaoImpl extends GenericDaoBase implements Vol protected final SearchBuilder DetachedAccountIdSearch; protected final SearchBuilder TemplateZoneSearch; protected final GenericSearchBuilder TotalSizeByPoolSearch; + protected final GenericSearchBuilder TotalVMSnapshotSizeByPoolSearch; protected final GenericSearchBuilder ActiveTemplateSearch; protected final SearchBuilder InstanceStatesSearch; protected final SearchBuilder AllFieldsSearch; @@ -316,6 +317,15 @@ public class VolumeDaoImpl extends GenericDaoBase implements Vol TotalSizeByPoolSearch.and("state", TotalSizeByPoolSearch.entity().getState(), Op.NEQ); TotalSizeByPoolSearch.done(); + TotalVMSnapshotSizeByPoolSearch = createSearchBuilder(SumCount.class); + TotalVMSnapshotSizeByPoolSearch.select("sum", Func.SUM, TotalVMSnapshotSizeByPoolSearch.entity().getVmSnapshotChainSize()); + TotalVMSnapshotSizeByPoolSearch.and("poolId", TotalVMSnapshotSizeByPoolSearch.entity().getPoolId(), Op.EQ); + TotalVMSnapshotSizeByPoolSearch.and("removed", TotalVMSnapshotSizeByPoolSearch.entity().getRemoved(), Op.NULL); + TotalVMSnapshotSizeByPoolSearch.and("state", TotalVMSnapshotSizeByPoolSearch.entity().getState(), Op.NEQ); + TotalVMSnapshotSizeByPoolSearch.and("vType", TotalVMSnapshotSizeByPoolSearch.entity().getVolumeType(), Op.EQ); + TotalVMSnapshotSizeByPoolSearch.and("instanceId", TotalVMSnapshotSizeByPoolSearch.entity().getInstanceId(), Op.NNULL); + TotalVMSnapshotSizeByPoolSearch.done(); + ActiveTemplateSearch = createSearchBuilder(Long.class); ActiveTemplateSearch.and("pool", ActiveTemplateSearch.entity().getPoolId(), Op.EQ); ActiveTemplateSearch.and("template", ActiveTemplateSearch.entity().getTemplateId(), Op.EQ); @@ -516,6 +526,20 @@ public class VolumeDaoImpl extends GenericDaoBase implements Vol return new Pair(sumCount.count, sumCount.sum); } + @Override + public long getVMSnapshotSizeByPool(long poolId) { + SearchCriteria sc = TotalVMSnapshotSizeByPoolSearch.create(); + sc.setParameters("poolId", poolId); + sc.setParameters("state", State.Destroy); + sc.setParameters("vType", Volume.Type.ROOT.toString()); + List results = customSearch(sc, null); + if (results != null) { + return results.get(0).sum; + } else { + return 0; + } + } + @Override @DB public boolean remove(Long id) { diff --git a/server/src/com/cloud/capacity/CapacityManagerImpl.java b/server/src/com/cloud/capacity/CapacityManagerImpl.java index a9b9ae410d6..70491bc2ade 100755 --- a/server/src/com/cloud/capacity/CapacityManagerImpl.java +++ b/server/src/com/cloud/capacity/CapacityManagerImpl.java @@ -28,6 +28,7 @@ import javax.inject.Inject; import javax.naming.ConfigurationException; import org.apache.log4j.Logger; + import org.apache.cloudstack.framework.config.ConfigDepot; import org.apache.cloudstack.framework.config.ConfigKey; import org.apache.cloudstack.framework.config.Configurable; @@ -69,7 +70,6 @@ import com.cloud.service.dao.ServiceOfferingDao; import com.cloud.storage.StorageManager; import com.cloud.storage.VMTemplateStoragePoolVO; import com.cloud.storage.VMTemplateVO; -import com.cloud.storage.VolumeVO; import com.cloud.storage.dao.VMTemplatePoolDao; import com.cloud.storage.dao.VolumeDao; import com.cloud.utils.DateUtil; @@ -474,19 +474,6 @@ public class CapacityManagerImpl extends ManagerBase implements CapacityManager, } - private long getVMSnapshotAllocatedCapacity(StoragePoolVO pool){ - List volumes = _volumeDao.findByPoolId(pool.getId()); - long totalSize = 0; - for (VolumeVO volume : volumes) { - if(volume.getInstanceId() == null) - continue; - Long chainSize = volume.getVmSnapshotChainSize(); - if(chainSize != null) - totalSize += chainSize; - } - return totalSize; - } - @Override public long getAllocatedPoolCapacity(StoragePoolVO pool, VMTemplateVO templateForVmCreation){ @@ -495,7 +482,7 @@ public class CapacityManagerImpl extends ManagerBase implements CapacityManager, long totalAllocatedSize = sizes.second() + sizes.first() * _extraBytesPerVolume; // Get size for VM Snapshots - totalAllocatedSize = totalAllocatedSize + getVMSnapshotAllocatedCapacity(pool); + totalAllocatedSize = totalAllocatedSize + _volumeDao.getVMSnapshotSizeByPool(pool.getId()); // Iterate through all templates on this storage pool boolean tmpinstalled = false;