Search in sources :

Example 1 with Cluster

use of com.cloud.org.Cluster in project cloudstack by apache.

the class ResourceManagerImpl method updateCluster.

@Override
@DB
public Cluster updateCluster(final Cluster clusterToUpdate, final String clusterType, final String hypervisor, final String allocationState, final String managedstate) {
    final ClusterVO cluster = (ClusterVO) clusterToUpdate;
    // Verify cluster information and update the cluster if needed
    boolean doUpdate = false;
    if (hypervisor != null && !hypervisor.isEmpty()) {
        final Hypervisor.HypervisorType hypervisorType = Hypervisor.HypervisorType.getType(hypervisor);
        if (hypervisorType == null) {
            s_logger.error("Unable to resolve " + hypervisor + " to a valid supported hypervisor type");
            throw new InvalidParameterValueException("Unable to resolve " + hypervisor + " to a supported type");
        } else {
            cluster.setHypervisorType(hypervisor);
            doUpdate = true;
        }
    }
    Cluster.ClusterType newClusterType = null;
    if (clusterType != null && !clusterType.isEmpty()) {
        try {
            newClusterType = Cluster.ClusterType.valueOf(clusterType);
        } catch (final IllegalArgumentException ex) {
            throw new InvalidParameterValueException("Unable to resolve " + clusterType + " to a supported type");
        }
        if (newClusterType == null) {
            s_logger.error("Unable to resolve " + clusterType + " to a valid supported cluster type");
            throw new InvalidParameterValueException("Unable to resolve " + clusterType + " to a supported type");
        } else {
            cluster.setClusterType(newClusterType);
            doUpdate = true;
        }
    }
    Grouping.AllocationState newAllocationState = null;
    if (allocationState != null && !allocationState.isEmpty()) {
        try {
            newAllocationState = Grouping.AllocationState.valueOf(allocationState);
        } catch (final IllegalArgumentException ex) {
            throw new InvalidParameterValueException("Unable to resolve Allocation State '" + allocationState + "' to a supported state");
        }
        if (newAllocationState == null) {
            s_logger.error("Unable to resolve " + allocationState + " to a valid supported allocation State");
            throw new InvalidParameterValueException("Unable to resolve " + allocationState + " to a supported state");
        } else {
            cluster.setAllocationState(newAllocationState);
            doUpdate = true;
        }
    }
    Managed.ManagedState newManagedState = null;
    final Managed.ManagedState oldManagedState = cluster.getManagedState();
    if (managedstate != null && !managedstate.isEmpty()) {
        try {
            newManagedState = Managed.ManagedState.valueOf(managedstate);
        } catch (final IllegalArgumentException ex) {
            throw new InvalidParameterValueException("Unable to resolve Managed State '" + managedstate + "' to a supported state");
        }
        if (newManagedState == null) {
            s_logger.error("Unable to resolve Managed State '" + managedstate + "' to a supported state");
            throw new InvalidParameterValueException("Unable to resolve Managed State '" + managedstate + "' to a supported state");
        } else {
            doUpdate = true;
        }
    }
    if (doUpdate) {
        _clusterDao.update(cluster.getId(), cluster);
    }
    if (newManagedState != null && !newManagedState.equals(oldManagedState)) {
        if (newManagedState.equals(Managed.ManagedState.Unmanaged)) {
            boolean success = false;
            try {
                cluster.setManagedState(Managed.ManagedState.PrepareUnmanaged);
                _clusterDao.update(cluster.getId(), cluster);
                List<HostVO> hosts = listAllUpAndEnabledHosts(Host.Type.Routing, cluster.getId(), cluster.getPodId(), cluster.getDataCenterId());
                for (final HostVO host : hosts) {
                    if (host.getType().equals(Host.Type.Routing) && !host.getStatus().equals(Status.Down) && !host.getStatus().equals(Status.Disconnected) && !host.getStatus().equals(Status.Up) && !host.getStatus().equals(Status.Alert)) {
                        final String msg = "host " + host.getPrivateIpAddress() + " should not be in " + host.getStatus().toString() + " status";
                        throw new CloudRuntimeException("PrepareUnmanaged Failed due to " + msg);
                    }
                }
                for (final HostVO host : hosts) {
                    if (host.getStatus().equals(Status.Up)) {
                        umanageHost(host.getId());
                    }
                }
                final int retry = 40;
                boolean lsuccess = true;
                for (int i = 0; i < retry; i++) {
                    lsuccess = true;
                    try {
                        Thread.sleep(5 * 1000);
                    } catch (final Exception e) {
                    }
                    hosts = listAllUpAndEnabledHosts(Host.Type.Routing, cluster.getId(), cluster.getPodId(), cluster.getDataCenterId());
                    for (final HostVO host : hosts) {
                        if (!host.getStatus().equals(Status.Down) && !host.getStatus().equals(Status.Disconnected) && !host.getStatus().equals(Status.Alert)) {
                            lsuccess = false;
                            break;
                        }
                    }
                    if (lsuccess == true) {
                        success = true;
                        break;
                    }
                }
                if (success == false) {
                    throw new CloudRuntimeException("PrepareUnmanaged Failed due to some hosts are still in UP status after 5 Minutes, please try later ");
                }
            } finally {
                cluster.setManagedState(success ? Managed.ManagedState.Unmanaged : Managed.ManagedState.PrepareUnmanagedError);
                _clusterDao.update(cluster.getId(), cluster);
            }
        } else if (newManagedState.equals(Managed.ManagedState.Managed)) {
            cluster.setManagedState(Managed.ManagedState.Managed);
            _clusterDao.update(cluster.getId(), cluster);
        }
    }
    return cluster;
}
Also used : ClusterVO(com.cloud.dc.ClusterVO) Hypervisor(com.cloud.hypervisor.Hypervisor) PodCluster(com.cloud.dc.PodCluster) Cluster(com.cloud.org.Cluster) Grouping(com.cloud.org.Grouping) StoragePoolHostVO(com.cloud.storage.StoragePoolHostVO) HostVO(com.cloud.host.HostVO) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResourceInUseException(com.cloud.exception.ResourceInUseException) URISyntaxException(java.net.URISyntaxException) DiscoveryException(com.cloud.exception.DiscoveryException) SshException(com.cloud.utils.ssh.SshException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) Managed(com.cloud.org.Managed) DB(com.cloud.utils.db.DB)

Example 2 with Cluster

use of com.cloud.org.Cluster in project cloudstack by apache.

the class AddClusterCmdTest method testExecuteForResult.

@Test
public void testExecuteForResult() throws Exception {
    resourceService = Mockito.mock(ResourceService.class);
    responseGenerator = Mockito.mock(ResponseGenerator.class);
    addClusterCmd._resourceService = resourceService;
    addClusterCmd._responseGenerator = responseGenerator;
    Cluster cluster = Mockito.mock(Cluster.class);
    Cluster[] clusterArray = new Cluster[] { cluster };
    Mockito.doReturn(Arrays.asList(clusterArray)).when(resourceService).discoverCluster(addClusterCmd);
    addClusterCmd.execute();
}
Also used : ResponseGenerator(org.apache.cloudstack.api.ResponseGenerator) ResourceService(com.cloud.resource.ResourceService) Cluster(com.cloud.org.Cluster) Test(org.junit.Test)

Example 3 with Cluster

use of com.cloud.org.Cluster in project cloudstack by apache.

the class StorageSystemSnapshotStrategy method getHost.

private Optional<HostVO> getHost(long zoneId, boolean computeClusterMustSupportResign) {
    List<? extends Cluster> clusters = mgr.searchForClusters(zoneId, 0L, Long.MAX_VALUE, HypervisorType.XenServer.toString());
    if (clusters == null) {
        clusters = new ArrayList<>();
    }
    Collections.shuffle(clusters, new Random(System.nanoTime()));
    clusters: for (Cluster cluster : clusters) {
        if (cluster.getAllocationState() == AllocationState.Enabled) {
            List<HostVO> hosts = hostDao.findByClusterId(cluster.getId());
            if (hosts != null) {
                Collections.shuffle(hosts, new Random(System.nanoTime()));
                for (HostVO host : hosts) {
                    if (host.getResourceState() == ResourceState.Enabled) {
                        if (computeClusterMustSupportResign) {
                            if (clusterDao.getSupportsResigning(cluster.getId())) {
                                return Optional.of(host);
                            } else {
                                // no other host in the cluster in question should be able to satisfy our requirements here, so move on to the next cluster
                                continue clusters;
                            }
                        } else {
                            return Optional.of(host);
                        }
                    }
                }
            }
        }
    }
    return Optional.absent();
}
Also used : Random(java.util.Random) Cluster(com.cloud.org.Cluster) List(java.util.List) ArrayList(java.util.ArrayList) HostVO(com.cloud.host.HostVO)

Example 4 with Cluster

use of com.cloud.org.Cluster in project cloudstack by apache.

the class MetricsServiceImpl method listZoneMetrics.

@Override
public List<ZoneMetricsResponse> listZoneMetrics(List<ZoneResponse> zoneResponses) {
    final List<ZoneMetricsResponse> metricsResponses = new ArrayList<>();
    for (final ZoneResponse zoneResponse : zoneResponses) {
        ZoneMetricsResponse metricsResponse = new ZoneMetricsResponse();
        try {
            BeanUtils.copyProperties(metricsResponse, zoneResponse);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to generate zone metrics response");
        }
        final DataCenter zone = dataCenterDao.findByUuid(zoneResponse.getId());
        if (zone == null) {
            continue;
        }
        final Long zoneId = zone.getId();
        // Thresholds
        final Double cpuThreshold = AlertManager.CPUCapacityThreshold.value();
        final Double memoryThreshold = AlertManager.MemoryCapacityThreshold.value();
        final Float cpuDisableThreshold = DeploymentClusterPlanner.ClusterCPUCapacityDisableThreshold.value();
        final Float memoryDisableThreshold = DeploymentClusterPlanner.ClusterMemoryCapacityDisableThreshold.value();
        // CPU and memory capacities
        final CapacityDaoImpl.SummedCapacity cpuCapacity = getCapacity((int) Capacity.CAPACITY_TYPE_CPU, zoneId, null);
        final CapacityDaoImpl.SummedCapacity memoryCapacity = getCapacity((int) Capacity.CAPACITY_TYPE_MEMORY, zoneId, null);
        final Metrics metrics = new Metrics(cpuCapacity, memoryCapacity);
        for (final Cluster cluster : clusterDao.listClustersByDcId(zoneId)) {
            metrics.incrTotalResources();
            if (cluster.getAllocationState() == Grouping.AllocationState.Enabled && cluster.getManagedState() == Managed.ManagedState.Managed) {
                metrics.incrUpResources();
            }
            for (final HostJoinVO host : hostJoinDao.findByClusterId(cluster.getId(), Host.Type.Routing)) {
                updateHostMetrics(metrics, host);
            }
        }
        metricsResponse.setState(zoneResponse.getAllocationState());
        metricsResponse.setResource(metrics.getUpResources(), metrics.getTotalResources());
        // CPU
        metricsResponse.setCpuTotal(metrics.getTotalCpu());
        metricsResponse.setCpuAllocated(metrics.getCpuAllocated(), metrics.getTotalCpu());
        if (metrics.getCpuUsedPercentage() > 0L) {
            metricsResponse.setCpuUsed(metrics.getCpuUsedPercentage(), metrics.getTotalHosts());
            metricsResponse.setCpuMaxDeviation(metrics.getMaximumCpuUsage(), metrics.getCpuUsedPercentage(), metrics.getTotalHosts());
        }
        // Memory
        metricsResponse.setMemTotal(metrics.getTotalMemory());
        metricsResponse.setMemAllocated(metrics.getMemoryAllocated(), metrics.getTotalMemory());
        if (metrics.getMemoryUsed() > 0L) {
            metricsResponse.setMemUsed(metrics.getMemoryUsed(), metrics.getTotalMemory());
            metricsResponse.setMemMaxDeviation(metrics.getMaximumMemoryUsage(), metrics.getMemoryUsed(), metrics.getTotalHosts());
        }
        // CPU thresholds
        metricsResponse.setCpuUsageThreshold(metrics.getCpuUsedPercentage(), metrics.getTotalHosts(), cpuThreshold);
        metricsResponse.setCpuUsageDisableThreshold(metrics.getCpuUsedPercentage(), metrics.getTotalHosts(), cpuDisableThreshold);
        metricsResponse.setCpuAllocatedThreshold(metrics.getCpuAllocated(), metrics.getTotalCpu(), cpuThreshold);
        metricsResponse.setCpuAllocatedDisableThreshold(metrics.getCpuAllocated(), metrics.getTotalCpu(), cpuDisableThreshold);
        // Memory thresholds
        metricsResponse.setMemoryUsageThreshold(metrics.getMemoryUsed(), metrics.getTotalMemory(), memoryThreshold);
        metricsResponse.setMemoryUsageDisableThreshold(metrics.getMemoryUsed(), metrics.getTotalMemory(), memoryDisableThreshold);
        metricsResponse.setMemoryAllocatedThreshold(metrics.getMemoryAllocated(), metrics.getTotalMemory(), memoryThreshold);
        metricsResponse.setMemoryAllocatedDisableThreshold(metrics.getMemoryAllocated(), metrics.getTotalMemory(), memoryDisableThreshold);
        metricsResponses.add(metricsResponse);
    }
    return metricsResponses;
}
Also used : ArrayList(java.util.ArrayList) Cluster(com.cloud.org.Cluster) HostJoinVO(com.cloud.api.query.vo.HostJoinVO) InvocationTargetException(java.lang.reflect.InvocationTargetException) ZoneResponse(org.apache.cloudstack.api.response.ZoneResponse) DataCenter(com.cloud.dc.DataCenter) ServerApiException(org.apache.cloudstack.api.ServerApiException) ZoneMetricsResponse(org.apache.cloudstack.response.ZoneMetricsResponse) CapacityDaoImpl(com.cloud.capacity.dao.CapacityDaoImpl)

Example 5 with Cluster

use of com.cloud.org.Cluster in project cloudstack by apache.

the class FirstFitAllocator method allocateTo.

protected List<Host> allocateTo(DeploymentPlan plan, ServiceOffering offering, VMTemplateVO template, ExcludeList avoid, List<? extends Host> hosts, int returnUpTo, boolean considerReservedCapacity, Account account) {
    if (_allocationAlgorithm.equals("random") || _allocationAlgorithm.equals("userconcentratedpod_random")) {
        // Shuffle this so that we don't check the hosts in the same order.
        Collections.shuffle(hosts);
    } else if (_allocationAlgorithm.equals("userdispersing")) {
        hosts = reorderHostsByNumberOfVms(plan, hosts, account);
    } else if (_allocationAlgorithm.equals("firstfitleastconsumed")) {
        hosts = reorderHostsByCapacity(plan, hosts);
    }
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("FirstFitAllocator has " + hosts.size() + " hosts to check for allocation: " + hosts);
    }
    // We will try to reorder the host lists such that we give priority to hosts that have
    // the minimums to support a VM's requirements
    hosts = prioritizeHosts(template, offering, hosts);
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Found " + hosts.size() + " hosts for allocation after prioritization: " + hosts);
    }
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Looking for speed=" + (offering.getCpu() * offering.getSpeed()) + "Mhz, Ram=" + offering.getRamSize());
    }
    long serviceOfferingId = offering.getId();
    List<Host> suitableHosts = new ArrayList<Host>();
    ServiceOfferingDetailsVO offeringDetails = null;
    for (Host host : hosts) {
        if (suitableHosts.size() == returnUpTo) {
            break;
        }
        if (avoid.shouldAvoid(host)) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Host name: " + host.getName() + ", hostId: " + host.getId() + " is in avoid set, skipping this and trying other available hosts");
            }
            continue;
        }
        //find number of guest VMs occupying capacity on this host.
        if (_capacityMgr.checkIfHostReachMaxGuestLimit(host)) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Host name: " + host.getName() + ", hostId: " + host.getId() + " already has max Running VMs(count includes system VMs), skipping this and trying other available hosts");
            }
            avoid.addHost(host.getId());
            continue;
        }
        // Check if GPU device is required by offering and host has the availability
        if ((offeringDetails = _serviceOfferingDetailsDao.findDetail(serviceOfferingId, GPU.Keys.vgpuType.toString())) != null) {
            ServiceOfferingDetailsVO groupName = _serviceOfferingDetailsDao.findDetail(serviceOfferingId, GPU.Keys.pciDevice.toString());
            if (!_resourceMgr.isGPUDeviceAvailable(host.getId(), groupName.getValue(), offeringDetails.getValue())) {
                s_logger.info("Host name: " + host.getName() + ", hostId: " + host.getId() + " does not have required GPU devices available");
                avoid.addHost(host.getId());
                continue;
            }
        }
        int cpu_requested = offering.getCpu() * offering.getSpeed();
        long ram_requested = offering.getRamSize() * 1024L * 1024L;
        Cluster cluster = _clusterDao.findById(host.getClusterId());
        ClusterDetailsVO clusterDetailsCpuOvercommit = _clusterDetailsDao.findDetail(cluster.getId(), "cpuOvercommitRatio");
        ClusterDetailsVO clusterDetailsRamOvercommmt = _clusterDetailsDao.findDetail(cluster.getId(), "memoryOvercommitRatio");
        Float cpuOvercommitRatio = Float.parseFloat(clusterDetailsCpuOvercommit.getValue());
        Float memoryOvercommitRatio = Float.parseFloat(clusterDetailsRamOvercommmt.getValue());
        boolean hostHasCpuCapability = _capacityMgr.checkIfHostHasCpuCapability(host.getId(), offering.getCpu(), offering.getSpeed());
        boolean hostHasCapacity = _capacityMgr.checkIfHostHasCapacity(host.getId(), cpu_requested, ram_requested, false, cpuOvercommitRatio, memoryOvercommitRatio, considerReservedCapacity);
        if (hostHasCpuCapability && hostHasCapacity) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Found a suitable host, adding to list: " + host.getId());
            }
            suitableHosts.add(host);
        } else {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Not using host " + host.getId() + "; host has cpu capability? " + hostHasCpuCapability + ", host has capacity?" + hostHasCapacity);
            }
            avoid.addHost(host.getId());
        }
    }
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Host Allocator returning " + suitableHosts.size() + " suitable hosts");
    }
    return suitableHosts;
}
Also used : ArrayList(java.util.ArrayList) ServiceOfferingDetailsVO(com.cloud.service.ServiceOfferingDetailsVO) Cluster(com.cloud.org.Cluster) Host(com.cloud.host.Host) ClusterDetailsVO(com.cloud.dc.ClusterDetailsVO)

Aggregations

Cluster (com.cloud.org.Cluster)26 ArrayList (java.util.ArrayList)13 HostVO (com.cloud.host.HostVO)8 ServerApiException (org.apache.cloudstack.api.ServerApiException)7 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)6 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)6 DataCenter (com.cloud.dc.DataCenter)5 Pod (com.cloud.dc.Pod)5 DeployDestination (com.cloud.deploy.DeployDestination)5 DataCenterVO (com.cloud.dc.DataCenterVO)4 DiscoveryException (com.cloud.exception.DiscoveryException)4 ResourceInUseException (com.cloud.exception.ResourceInUseException)4 Host (com.cloud.host.Host)4 StoragePool (com.cloud.storage.StoragePool)4 ClusterResponse (org.apache.cloudstack.api.response.ClusterResponse)4 ClusterResponse (com.cloud.api.response.ClusterResponse)3 ClusterDetailsVO (com.cloud.dc.ClusterDetailsVO)3 ClusterVO (com.cloud.dc.ClusterVO)3 List (java.util.List)3 ServerApiException (com.cloud.api.ServerApiException)2