Search in sources :

Example 1 with CapacityState

use of com.cloud.model.enumeration.CapacityState in project cosmic by MissionCriticalCloud.

the class CapacityManagerImpl method updateCapacityForHost.

@DB
@Override
public void updateCapacityForHost(final Host host) {
    // prepare the service offerings
    final List<ServiceOfferingVO> offerings = this._offeringsDao.listAllIncludingRemoved();
    final Map<Long, ServiceOfferingVO> offeringsMap = new HashMap<>();
    for (final ServiceOfferingVO offering : offerings) {
        offeringsMap.put(offering.getId(), offering);
    }
    long usedCpu = 0;
    long usedMemory = 0;
    long reservedMemory = 0;
    long reservedCpu = 0;
    final CapacityState capacityState = (host.getResourceState() == ResourceState.Enabled) ? CapacityState.Enabled : CapacityState.Disabled;
    final List<VMInstanceVO> vms = this._vmDao.listUpByHostId(host.getId());
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Found " + vms.size() + " VMs on host " + host.getId());
    }
    for (final VMInstanceVO vm : vms) {
        final ServiceOffering so = offeringsMap.get(vm.getServiceOfferingId());
        usedMemory += (so.getRamSize() * 1024L * 1024L);
        usedCpu += so.getCpu();
    }
    final List<VMInstanceVO> vmsByLastHostId = this._vmDao.listByLastHostId(host.getId());
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Found " + vmsByLastHostId.size() + " VM, not running on host " + host.getId());
    }
    for (final VMInstanceVO vm : vmsByLastHostId) {
        final long secondsSinceLastUpdate = (DateUtil.currentGMTTime().getTime() - vm.getUpdateTime().getTime()) / 1000;
        if (secondsSinceLastUpdate < this._vmCapacityReleaseInterval) {
            final ServiceOffering so = offeringsMap.get(vm.getServiceOfferingId());
            reservedMemory += (so.getRamSize() * 1024L * 1024L);
            reservedCpu += so.getCpu();
        } else {
            // signal if not done already, that the VM has been stopped for skip.counting.hours,
            // hence capacity will not be reserved anymore.
            final UserVmDetailVO messageSentFlag = this._userVmDetailsDao.findDetail(vm.getId(), MESSAGE_RESERVED_CAPACITY_FREED_FLAG);
            if (messageSentFlag == null || !Boolean.valueOf(messageSentFlag.getValue())) {
                this._messageBus.publish(this._name, "VM_ReservedCapacity_Free", PublishScope.LOCAL, vm);
                if (vm.getType() == VirtualMachineType.User) {
                    final UserVmVO userVM = this._userVMDao.findById(vm.getId());
                    this._userVMDao.loadDetails(userVM);
                    userVM.setDetail(MESSAGE_RESERVED_CAPACITY_FREED_FLAG, "true");
                    this._userVMDao.saveDetails(userVM);
                }
            }
        }
    }
    final CapacityVO cpuCap = this._capacityDao.findByHostIdType(host.getId(), Capacity.CAPACITY_TYPE_CPU);
    final CapacityVO memCap = this._capacityDao.findByHostIdType(host.getId(), Capacity.CAPACITY_TYPE_MEMORY);
    if (cpuCap != null && memCap != null) {
        if (host.getTotalMemory() != null) {
            memCap.setTotalCapacity(host.getTotalMemory());
        }
        final long hostTotalCpu = host.getCpus().longValue();
        if (cpuCap.getTotalCapacity() != hostTotalCpu) {
            s_logger.debug("Calibrate total cpu for host: " + host.getId() + " old total CPU:" + cpuCap.getTotalCapacity() + " new total CPU:" + hostTotalCpu);
            cpuCap.setTotalCapacity(hostTotalCpu);
        }
        // Set the capacity state as per the host allocation state.
        if (capacityState != cpuCap.getCapacityState()) {
            s_logger.debug("Calibrate cpu capacity state for host: " + host.getId() + " old capacity state:" + cpuCap.getTotalCapacity() + " new capacity state:" + hostTotalCpu);
            cpuCap.setCapacityState(capacityState);
        }
        memCap.setCapacityState(capacityState);
        if (cpuCap.getUsedCapacity() == usedCpu && cpuCap.getReservedCapacity() == reservedCpu) {
            s_logger.debug("No need to calibrate cpu capacity, host:" + host.getId() + " usedCpu: " + cpuCap.getUsedCapacity() + " reservedCpu: " + cpuCap.getReservedCapacity());
        } else {
            if (cpuCap.getReservedCapacity() != reservedCpu) {
                s_logger.debug("Calibrate reserved cpu for host: " + host.getId() + " old reservedCpu:" + cpuCap.getReservedCapacity() + " new reservedCpu:" + reservedCpu);
                cpuCap.setReservedCapacity(reservedCpu);
            }
            if (cpuCap.getUsedCapacity() != usedCpu) {
                s_logger.debug("Calibrate used cpu for host: " + host.getId() + " old usedCpu:" + cpuCap.getUsedCapacity() + " new usedCpu:" + usedCpu);
                cpuCap.setUsedCapacity(usedCpu);
            }
        }
        if (memCap.getTotalCapacity() != host.getTotalMemory()) {
            s_logger.debug("Calibrate total memory for host: " + host.getId() + " old total memory:" + memCap.getTotalCapacity() + " new total memory:" + host.getTotalMemory());
            memCap.setTotalCapacity(host.getTotalMemory());
        }
        // Set the capacity state as per the host allocation state.
        if (capacityState != memCap.getCapacityState()) {
            s_logger.debug("Calibrate memory capacity state for host: " + host.getId() + " old capacity state:" + memCap.getTotalCapacity() + " new capacity state:" + hostTotalCpu);
            memCap.setCapacityState(capacityState);
        }
        if (memCap.getUsedCapacity() == usedMemory && memCap.getReservedCapacity() == reservedMemory) {
            s_logger.debug("No need to calibrate memory capacity, host:" + host.getId() + " usedMem: " + memCap.getUsedCapacity() + " reservedMem: " + memCap.getReservedCapacity());
        } else {
            if (memCap.getReservedCapacity() != reservedMemory) {
                s_logger.debug("Calibrate reserved memory for host: " + host.getId() + " old reservedMem:" + memCap.getReservedCapacity() + " new reservedMem:" + reservedMemory);
                memCap.setReservedCapacity(reservedMemory);
            }
            if (memCap.getUsedCapacity() != usedMemory) {
                /*
                     * Didn't calibrate for used memory, because VMs can be in
                     * state(starting/migrating) that I don't know on which host
                     * they are allocated
                     */
                s_logger.debug("Calibrate used memory for host: " + host.getId() + " old usedMem: " + memCap.getUsedCapacity() + " new usedMem: " + usedMemory);
                memCap.setUsedCapacity(usedMemory);
            }
        }
        try {
            this._capacityDao.update(cpuCap.getId(), cpuCap);
            this._capacityDao.update(memCap.getId(), memCap);
        } catch (final Exception e) {
            s_logger.error("Caught exception while updating cpu/memory capacity for the host " + host.getId(), e);
        }
    } else {
        final long usedMemoryFinal = usedMemory;
        final long reservedMemoryFinal = reservedMemory;
        final long usedCpuFinal = usedCpu;
        final long reservedCpuFinal = reservedCpu;
        Transaction.execute(new TransactionCallbackNoReturn() {

            @Override
            public void doInTransactionWithoutResult(final TransactionStatus status) {
                CapacityVO capacity = new CapacityVO(host.getId(), host.getDataCenterId(), host.getPodId(), host.getClusterId(), usedMemoryFinal, host.getTotalMemory(), Capacity.CAPACITY_TYPE_MEMORY);
                capacity.setReservedCapacity(reservedMemoryFinal);
                capacity.setCapacityState(capacityState);
                CapacityManagerImpl.this._capacityDao.persist(capacity);
                capacity = new CapacityVO(host.getId(), host.getDataCenterId(), host.getPodId(), host.getClusterId(), usedCpuFinal, host.getCpus().longValue(), Capacity.CAPACITY_TYPE_CPU);
                capacity.setReservedCapacity(reservedCpuFinal);
                capacity.setCapacityState(capacityState);
                CapacityManagerImpl.this._capacityDao.persist(capacity);
            }
        });
    }
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) HashMap(java.util.HashMap) ServiceOffering(com.cloud.offering.ServiceOffering) VMInstanceVO(com.cloud.vm.VMInstanceVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) ServiceOfferingVO(com.cloud.service.ServiceOfferingVO) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) UserVmDetailVO(com.cloud.vm.UserVmDetailVO) CapacityState(com.cloud.model.enumeration.CapacityState) DB(com.cloud.utils.db.DB)

Example 2 with CapacityState

use of com.cloud.model.enumeration.CapacityState in project cosmic by MissionCriticalCloud.

the class AlertManagerImpl method createOrUpdateIpCapacity.

public void createOrUpdateIpCapacity(final Long dcId, final Long podId, final short capacityType, final AllocationState capacityState) {
    SearchCriteria<CapacityVO> capacitySC = _capacityDao.createSearchCriteria();
    List<CapacityVO> capacities = _capacityDao.search(capacitySC, null);
    capacitySC = _capacityDao.createSearchCriteria();
    capacitySC.addAnd("podId", SearchCriteria.Op.EQ, podId);
    capacitySC.addAnd("dataCenterId", SearchCriteria.Op.EQ, dcId);
    capacitySC.addAnd("capacityType", SearchCriteria.Op.EQ, capacityType);
    final int totalIPs;
    final int allocatedIPs;
    capacities = _capacityDao.search(capacitySC, null);
    if (capacityType == Capacity.CAPACITY_TYPE_PRIVATE_IP) {
        totalIPs = _privateIPAddressDao.countIPs(podId, dcId, false);
        allocatedIPs = _privateIPAddressDao.countIPs(podId, dcId, true);
    } else if (capacityType == Capacity.CAPACITY_TYPE_VIRTUAL_NETWORK_PUBLIC_IP) {
        totalIPs = _publicIPAddressDao.countIPsForNetwork(dcId, false, VlanType.VirtualNetwork);
        allocatedIPs = _publicIPAddressDao.countIPsForNetwork(dcId, true, VlanType.VirtualNetwork);
    } else {
        totalIPs = _publicIPAddressDao.countIPsForNetwork(dcId, false, VlanType.DirectAttached);
        allocatedIPs = _publicIPAddressDao.countIPsForNetwork(dcId, true, VlanType.DirectAttached);
    }
    final CapacityState ipCapacityState = (capacityState == AllocationState.Disabled) ? CapacityState.Disabled : CapacityState.Enabled;
    if (capacities.size() == 0) {
        final CapacityVO newPublicIPCapacity = new CapacityVO(null, dcId, podId, null, allocatedIPs, totalIPs, capacityType);
        newPublicIPCapacity.setCapacityState(ipCapacityState);
        _capacityDao.persist(newPublicIPCapacity);
    } else if (!(capacities.get(0).getUsedCapacity() == allocatedIPs && capacities.get(0).getTotalCapacity() == totalIPs && capacities.get(0).getCapacityState() == ipCapacityState)) {
        final CapacityVO capacity = capacities.get(0);
        capacity.setUsedCapacity(allocatedIPs);
        capacity.setTotalCapacity(totalIPs);
        capacity.setCapacityState(ipCapacityState);
        _capacityDao.update(capacity.getId(), capacity);
    }
}
Also used : CapacityState(com.cloud.model.enumeration.CapacityState) CapacityVO(com.cloud.capacity.CapacityVO)

Example 3 with CapacityState

use of com.cloud.model.enumeration.CapacityState in project cosmic by MissionCriticalCloud.

the class ResourceManagerImpl method resourceStateTransitTo.

@Override
public boolean resourceStateTransitTo(final Host host, final ResourceState.Event event, final long msId) throws NoTransitionException {
    final ResourceState currentState = host.getResourceState();
    final ResourceState nextState = currentState.getNextState(event);
    if (nextState == null) {
        throw new NoTransitionException("No next resource state found for current state = " + currentState + " event = " + event);
    }
    // TO DO - Make it more granular and have better conversion into capacity type
    if (host.getType() == HostType.Routing) {
        final CapacityState capacityState = nextState == ResourceState.Enabled ? CapacityState.Enabled : CapacityState.Disabled;
        final short[] capacityTypes = { Capacity.CAPACITY_TYPE_CPU, Capacity.CAPACITY_TYPE_MEMORY };
        this._capacityDao.updateCapacityState(null, null, null, host.getId(), capacityState.toString(), capacityTypes);
    }
    return this._hostDao.updateResourceState(currentState, event, nextState, host);
}
Also used : CapacityState(com.cloud.model.enumeration.CapacityState) NoTransitionException(com.cloud.legacymodel.exceptions.NoTransitionException) ResourceState(com.cloud.legacymodel.resource.ResourceState)

Example 4 with CapacityState

use of com.cloud.model.enumeration.CapacityState in project cosmic by MissionCriticalCloud.

the class AlertManagerImpl method createOrUpdateVlanCapacity.

private void createOrUpdateVlanCapacity(final long dcId, final AllocationState capacityState) {
    SearchCriteria<CapacityVO> capacitySC = _capacityDao.createSearchCriteria();
    List<CapacityVO> capacities = _capacityDao.search(capacitySC, null);
    capacitySC = _capacityDao.createSearchCriteria();
    capacitySC.addAnd("dataCenterId", SearchCriteria.Op.EQ, dcId);
    capacitySC.addAnd("capacityType", SearchCriteria.Op.EQ, Capacity.CAPACITY_TYPE_VLAN);
    capacities = _capacityDao.search(capacitySC, null);
    final int totalVlans = _dcDao.countZoneVlans(dcId, false);
    final int allocatedVlans = _dcDao.countZoneVlans(dcId, true);
    final CapacityState vlanCapacityState = (capacityState == AllocationState.Disabled) ? CapacityState.Disabled : CapacityState.Enabled;
    if (capacities.size() == 0) {
        final CapacityVO newVlanCapacity = new CapacityVO(null, dcId, null, null, allocatedVlans, totalVlans, Capacity.CAPACITY_TYPE_VLAN);
        newVlanCapacity.setCapacityState(vlanCapacityState);
        _capacityDao.persist(newVlanCapacity);
    } else if (!(capacities.get(0).getUsedCapacity() == allocatedVlans && capacities.get(0).getTotalCapacity() == totalVlans && capacities.get(0).getCapacityState() == vlanCapacityState)) {
        final CapacityVO capacity = capacities.get(0);
        capacity.setUsedCapacity(allocatedVlans);
        capacity.setTotalCapacity(totalVlans);
        capacity.setCapacityState(vlanCapacityState);
        _capacityDao.update(capacity.getId(), capacity);
    }
}
Also used : CapacityState(com.cloud.model.enumeration.CapacityState) CapacityVO(com.cloud.capacity.CapacityVO)

Example 5 with CapacityState

use of com.cloud.model.enumeration.CapacityState in project cosmic by MissionCriticalCloud.

the class StorageManagerImpl method createCapacityEntry.

@Override
public void createCapacityEntry(final StoragePoolVO storagePool, final short capacityType, final long allocated) {
    final SearchCriteria<CapacityVO> capacitySC = this._capacityDao.createSearchCriteria();
    capacitySC.addAnd("hostOrPoolId", SearchCriteria.Op.EQ, storagePool.getId());
    capacitySC.addAnd("dataCenterId", SearchCriteria.Op.EQ, storagePool.getDataCenterId());
    capacitySC.addAnd("capacityType", SearchCriteria.Op.EQ, capacityType);
    final List<CapacityVO> capacities = this._capacityDao.search(capacitySC, null);
    final long totalOverProvCapacity;
    if (storagePool.getPoolType() == StoragePoolType.NetworkFilesystem) {
        // All this is for the inaccuracy of floats for big number multiplication.
        final BigDecimal overProvFactor = getStorageOverProvisioningFactor(storagePool.getId());
        totalOverProvCapacity = overProvFactor.multiply(new BigDecimal(storagePool.getCapacityBytes())).longValue();
        s_logger.debug("Found storage pool " + storagePool.getName() + " of type " + storagePool.getPoolType().toString() + " with overprovisioning factor " + overProvFactor.toString());
        s_logger.debug("Total over provisioned capacity calculated is " + overProvFactor + " * " + storagePool.getCapacityBytes());
    } else {
        s_logger.debug("Found storage pool " + storagePool.getName() + " of type " + storagePool.getPoolType().toString());
        totalOverProvCapacity = storagePool.getCapacityBytes();
    }
    s_logger.debug("Total over provisioned capacity of the pool " + storagePool.getName() + " id: " + storagePool.getId() + " is " + totalOverProvCapacity);
    CapacityState capacityState = CapacityState.Enabled;
    if (storagePool.getScope() == ScopeType.ZONE) {
        final DataCenterVO dc = ApiDBUtils.findZoneById(storagePool.getDataCenterId());
        final AllocationState allocationState = dc.getAllocationState();
        capacityState = allocationState == AllocationState.Disabled ? CapacityState.Disabled : CapacityState.Enabled;
    } else {
        if (storagePool.getClusterId() != null) {
            final ClusterVO cluster = ApiDBUtils.findClusterById(storagePool.getClusterId());
            if (cluster != null) {
                final AllocationState allocationState = this._configMgr.findClusterAllocationState(cluster);
                capacityState = allocationState == AllocationState.Disabled ? CapacityState.Disabled : CapacityState.Enabled;
            }
        }
    }
    if (capacities.size() == 0) {
        final CapacityVO capacity = new CapacityVO(storagePool.getId(), storagePool.getDataCenterId(), storagePool.getPodId(), storagePool.getClusterId(), allocated, totalOverProvCapacity, capacityType);
        capacity.setCapacityState(capacityState);
        this._capacityDao.persist(capacity);
    } else {
        final CapacityVO capacity = capacities.get(0);
        if (capacity.getTotalCapacity() != totalOverProvCapacity || allocated != capacity.getUsedCapacity() || capacity.getCapacityState() != capacityState) {
            capacity.setTotalCapacity(totalOverProvCapacity);
            capacity.setUsedCapacity(allocated);
            capacity.setCapacityState(capacityState);
            this._capacityDao.update(capacity.getId(), capacity);
        }
    }
    s_logger.debug("Successfully set Capacity - " + totalOverProvCapacity + " for capacity type - " + capacityType + " , DataCenterId - " + storagePool.getDataCenterId() + ", HostOrPoolId - " + storagePool.getId() + ", PodId " + storagePool.getPodId());
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) ClusterVO(com.cloud.dc.ClusterVO) CapacityState(com.cloud.model.enumeration.CapacityState) AllocationState(com.cloud.model.enumeration.AllocationState) CapacityVO(com.cloud.capacity.CapacityVO) BigDecimal(java.math.BigDecimal)

Aggregations

CapacityState (com.cloud.model.enumeration.CapacityState)5 CapacityVO (com.cloud.capacity.CapacityVO)3 ClusterVO (com.cloud.dc.ClusterVO)1 DataCenterVO (com.cloud.dc.DataCenterVO)1 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)1 NoTransitionException (com.cloud.legacymodel.exceptions.NoTransitionException)1 ResourceState (com.cloud.legacymodel.resource.ResourceState)1 AllocationState (com.cloud.model.enumeration.AllocationState)1 ServiceOffering (com.cloud.offering.ServiceOffering)1 ServiceOfferingVO (com.cloud.service.ServiceOfferingVO)1 DB (com.cloud.utils.db.DB)1 TransactionCallbackNoReturn (com.cloud.utils.db.TransactionCallbackNoReturn)1 TransactionStatus (com.cloud.utils.db.TransactionStatus)1 UserVmDetailVO (com.cloud.vm.UserVmDetailVO)1 UserVmVO (com.cloud.vm.UserVmVO)1 VMInstanceVO (com.cloud.vm.VMInstanceVO)1 BigDecimal (java.math.BigDecimal)1 HashMap (java.util.HashMap)1 ConfigurationException (javax.naming.ConfigurationException)1