Search in sources :

Example 91 with VirtualMachine

use of com.cloud.vm.VirtualMachine in project cosmic by MissionCriticalCloud.

the class KVMFencerTest method testWithHosts.

@Test
public void testWithHosts() throws AgentUnavailableException, OperationTimedoutException {
    final HostVO host = Mockito.mock(HostVO.class);
    Mockito.when(host.getClusterId()).thenReturn(1l);
    Mockito.when(host.getHypervisorType()).thenReturn(HypervisorType.KVM);
    Mockito.when(host.getStatus()).thenReturn(Status.Up);
    Mockito.when(host.getDataCenterId()).thenReturn(1l);
    Mockito.when(host.getPodId()).thenReturn(1l);
    Mockito.when(host.getId()).thenReturn(1l);
    final HostVO secondHost = Mockito.mock(HostVO.class);
    Mockito.when(secondHost.getClusterId()).thenReturn(1l);
    Mockito.when(secondHost.getHypervisorType()).thenReturn(HypervisorType.KVM);
    Mockito.when(secondHost.getStatus()).thenReturn(Status.Up);
    Mockito.when(secondHost.getDataCenterId()).thenReturn(1l);
    Mockito.when(secondHost.getPodId()).thenReturn(1l);
    Mockito.when(host.getId()).thenReturn(2l);
    final VirtualMachine virtualMachine = Mockito.mock(VirtualMachine.class);
    Mockito.when(resourceManager.listAllHostsInCluster(1l)).thenReturn(Arrays.asList(host, secondHost));
    final FenceAnswer answer = new FenceAnswer(null, true, "ok");
    Mockito.when(agentManager.send(Matchers.anyLong(), Matchers.any(FenceCommand.class))).thenReturn(answer);
    Assert.assertTrue(fencer.fenceOff(virtualMachine, host));
}
Also used : FenceCommand(com.cloud.agent.api.FenceCommand) FenceAnswer(com.cloud.agent.api.FenceAnswer) HostVO(com.cloud.host.HostVO) VirtualMachine(com.cloud.vm.VirtualMachine) Test(org.junit.Test)

Example 92 with VirtualMachine

use of com.cloud.vm.VirtualMachine in project cosmic by MissionCriticalCloud.

the class NetworkOrchestrator method createNicForVm.

@Override
public NicProfile createNicForVm(final Network network, final NicProfile requested, final ReservationContext context, final VirtualMachineProfile vmProfile, final boolean prepare) throws ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException {
    final VirtualMachine vm = vmProfile.getVirtualMachine();
    final Zone zone = _zoneRepository.findOne(network.getDataCenterId());
    final Host host = _hostDao.findById(vm.getHostId());
    final DeployDestination dest = new DeployDestination(zone, null, null, host);
    NicProfile nic = getNicProfileForVm(network, requested, vm);
    // 1) allocate nic (if needed) Always allocate if it is a user vm
    if (nic == null || vmProfile.getType() == VirtualMachine.Type.User) {
        nic = allocateNic(requested, network, false, vmProfile);
        if (nic == null) {
            throw new CloudRuntimeException("Failed to allocate nic for vm " + vm + " in network " + network);
        }
        // Update vm_network_map table
        if (vmProfile.getType() == VirtualMachine.Type.User) {
            final VMNetworkMapVO vno = new VMNetworkMapVO(vm.getId(), network.getId());
            _vmNetworkMapDao.persist(vno);
        }
        s_logger.debug("Nic is allocated successfully for vm " + vm + " in network " + network);
    }
    // 2) prepare nic
    if (prepare) {
        final Pair<NetworkGuru, NetworkVO> implemented = implementNetwork(nic.getNetworkId(), dest, context, vmProfile.getVirtualMachine().getType() == Type.DomainRouter);
        if (implemented == null || implemented.first() == null) {
            s_logger.warn("Failed to implement network id=" + nic.getNetworkId() + " as a part of preparing nic id=" + nic.getId());
            throw new CloudRuntimeException("Failed to implement network id=" + nic.getNetworkId() + " as a part preparing nic id=" + nic.getId());
        }
        nic = prepareNic(vmProfile, dest, context, nic.getId(), implemented.second());
        s_logger.debug("Nic is prepared successfully for vm " + vm + " in network " + network);
    }
    return nic;
}
Also used : PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) Zone(com.cloud.db.model.Zone) DeployDestination(com.cloud.deploy.DeployDestination) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) NetworkGuru(com.cloud.network.guru.NetworkGuru) VMNetworkMapVO(com.cloud.engine.cloud.entity.api.db.VMNetworkMapVO) Host(com.cloud.host.Host) NicProfile(com.cloud.vm.NicProfile) VirtualMachine(com.cloud.vm.VirtualMachine)

Example 93 with VirtualMachine

use of com.cloud.vm.VirtualMachine in project cosmic by MissionCriticalCloud.

the class DefaultEndPointSelector method select.

@Override
public EndPoint select(final DataObject object, final StorageAction action) {
    if (StorageAction.TAKESNAPSHOT.equals(action)) {
        final SnapshotInfo snapshotInfo = (SnapshotInfo) object;
        if (Hypervisor.HypervisorType.KVM.equals(snapshotInfo.getHypervisorType())) {
            final VolumeInfo volumeInfo = snapshotInfo.getBaseVolume();
            final VirtualMachine vm = volumeInfo.getAttachedVM();
            if (vm != null && VirtualMachine.State.Running.equals(vm.getState())) {
                final Long hostId = vm.getHostId();
                return getEndPointFromHostId(hostId);
            }
        }
    } else if (StorageAction.MIGRATEVOLUME.equals(action)) {
        final VolumeInfo volumeInfo = (VolumeInfo) object;
        if (Hypervisor.HypervisorType.KVM.equals(volumeInfo.getHypervisorType())) {
            final VirtualMachine vm = volumeInfo.getAttachedVM();
            if (vm != null && VirtualMachine.State.Running.equals(vm.getState()) && volumeInfo.isToBeLiveMigrated()) {
                final Long hostId = vm.getHostId();
                return getEndPointFromHostId(hostId);
            }
        }
    }
    return select(object);
}
Also used : SnapshotInfo(com.cloud.engine.subsystem.api.storage.SnapshotInfo) VolumeInfo(com.cloud.engine.subsystem.api.storage.VolumeInfo) VirtualMachine(com.cloud.vm.VirtualMachine)

Example 94 with VirtualMachine

use of com.cloud.vm.VirtualMachine in project cosmic by MissionCriticalCloud.

the class HostAntiAffinityProcessor method check.

@Override
public boolean check(final VirtualMachineProfile vmProfile, final DeployDestination plannedDestination) throws AffinityConflictException {
    if (plannedDestination.getHost() == null) {
        return true;
    }
    final long plannedHostId = plannedDestination.getHost().getId();
    final VirtualMachine vm = vmProfile.getVirtualMachine();
    final List<AffinityGroupVMMapVO> vmGroupMappings = _affinityGroupVMMapDao.findByVmIdType(vm.getId(), getType());
    for (final AffinityGroupVMMapVO vmGroupMapping : vmGroupMappings) {
        // if more than 1 VM's are present in the group then check for
        // conflict due to parallel deployment
        final List<Long> groupVMIds = _affinityGroupVMMapDao.listVmIdsByAffinityGroup(vmGroupMapping.getAffinityGroupId());
        groupVMIds.remove(vm.getId());
        for (final Long groupVMId : groupVMIds) {
            final VMReservationVO vmReservation = _reservationDao.findByVmId(groupVMId);
            if (vmReservation != null && vmReservation.getHostId() != null && vmReservation.getHostId().equals(plannedHostId)) {
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("Planned destination for VM " + vm.getId() + " conflicts with an existing VM " + vmReservation.getVmId() + " reserved on the same host " + plannedHostId);
                }
                return false;
            }
        }
    }
    return true;
}
Also used : VMReservationVO(com.cloud.engine.cloud.entity.api.db.VMReservationVO) VirtualMachine(com.cloud.vm.VirtualMachine)

Example 95 with VirtualMachine

use of com.cloud.vm.VirtualMachine in project cosmic by MissionCriticalCloud.

the class HostAntiAffinityProcessor method process.

@Override
public void process(final VirtualMachineProfile vmProfile, final DeploymentPlan plan, final ExcludeList avoid) throws AffinityConflictException {
    final VirtualMachine vm = vmProfile.getVirtualMachine();
    final List<AffinityGroupVMMapVO> vmGroupMappings = _affinityGroupVMMapDao.findByVmIdType(vm.getId(), getType());
    for (final AffinityGroupVMMapVO vmGroupMapping : vmGroupMappings) {
        if (vmGroupMapping != null) {
            final AffinityGroupVO group = _affinityGroupDao.findById(vmGroupMapping.getAffinityGroupId());
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Processing affinity group " + group.getName() + " for VM Id: " + vm.getId());
            }
            final List<Long> groupVMIds = _affinityGroupVMMapDao.listVmIdsByAffinityGroup(group.getId());
            groupVMIds.remove(vm.getId());
            for (final Long groupVMId : groupVMIds) {
                final VMInstanceVO groupVM = _vmInstanceDao.findById(groupVMId);
                if (groupVM != null && !groupVM.isRemoved()) {
                    if (groupVM.getHostId() != null) {
                        avoid.addHost(groupVM.getHostId());
                        if (s_logger.isDebugEnabled()) {
                            s_logger.debug("Added host " + groupVM.getHostId() + " to avoid set, since VM " + groupVM.getId() + " is present on the host");
                        }
                    } else if (VirtualMachine.State.Stopped.equals(groupVM.getState()) && groupVM.getLastHostId() != null) {
                        final long secondsSinceLastUpdate = (DateUtil.currentGMTTime().getTime() - groupVM.getUpdateTime().getTime()) / 1000;
                        if (secondsSinceLastUpdate < _vmCapacityReleaseInterval) {
                            avoid.addHost(groupVM.getLastHostId());
                            if (s_logger.isDebugEnabled()) {
                                s_logger.debug("Added host " + groupVM.getLastHostId() + " to avoid set, since VM " + groupVM.getId() + " is present on the host, in Stopped state but has reserved capacity");
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : VMInstanceVO(com.cloud.vm.VMInstanceVO) VirtualMachine(com.cloud.vm.VirtualMachine)

Aggregations

VirtualMachine (com.cloud.vm.VirtualMachine)141 HostVO (com.cloud.host.HostVO)38 ArrayList (java.util.ArrayList)35 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)26 HashMap (java.util.HashMap)25 List (java.util.List)23 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)21 VMInstanceVO (com.cloud.vm.VMInstanceVO)20 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)19 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)19 DataCenter (com.cloud.dc.DataCenter)17 Host (com.cloud.host.Host)17 ServiceOffering (com.cloud.offering.ServiceOffering)17 Test (org.junit.Test)17 ServerApiException (com.cloud.api.ServerApiException)16 SystemVmResponse (com.cloud.api.response.SystemVmResponse)14 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)14 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)13 Account (com.cloud.user.Account)13 UserVm (com.cloud.uservm.UserVm)13