Search in sources :

Example 6 with OperationTimedoutException

use of com.cloud.legacymodel.exceptions.OperationTimedoutException in project cosmic by MissionCriticalCloud.

the class VirtualMachineManagerImpl method orchestrateMigrateForScale.

private void orchestrateMigrateForScale(final String vmUuid, final long srcHostId, final DeployDestination dest, final Long oldSvcOfferingId) throws ResourceUnavailableException, ConcurrentOperationException {
    VMInstanceVO vm = _vmDao.findByUuid(vmUuid);
    s_logger.info("Migrating " + vm + " to " + dest);
    vm.getServiceOfferingId();
    final long dstHostId = dest.getHost().getId();
    final Host fromHost = _hostDao.findById(srcHostId);
    if (fromHost == null) {
        s_logger.info("Unable to find the host to migrate from: " + srcHostId);
        throw new CloudRuntimeException("Unable to find the host to migrate from: " + srcHostId);
    }
    if (fromHost.getClusterId().longValue() != dest.getCluster().getId()) {
        s_logger.info("Source and destination host are not in same cluster, unable to migrate to host: " + dest.getHost().getId());
        throw new CloudRuntimeException("Source and destination host are not in same cluster, unable to migrate to host: " + dest.getHost().getId());
    }
    final VirtualMachineGuru vmGuru = getVmGuru(vm);
    final long vmId = vm.getId();
    vm = _vmDao.findByUuid(vmUuid);
    if (vm == null) {
        s_logger.debug("Unable to find the vm " + vm);
        throw new CloudRuntimeException("Unable to find a virtual machine with id " + vmId);
    }
    if (vm.getState() != State.Running) {
        s_logger.debug("VM is not Running, unable to migrate the vm " + vm);
        throw new CloudRuntimeException("VM is not Running, unable to migrate the vm currently " + vm + " , current state: " + vm.getState().toString());
    }
    AlertManager.AlertType alertType = AlertManager.AlertType.ALERT_TYPE_USERVM_MIGRATE;
    if (VirtualMachineType.DomainRouter.equals(vm.getType())) {
        alertType = AlertManager.AlertType.ALERT_TYPE_DOMAIN_ROUTER_MIGRATE;
    } else if (VirtualMachineType.ConsoleProxy.equals(vm.getType())) {
        alertType = AlertManager.AlertType.ALERT_TYPE_CONSOLE_PROXY_MIGRATE;
    }
    final VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm);
    _networkMgr.prepareNicForMigration(profile, dest);
    volumeMgr.prepareForMigration(profile, dest);
    final VirtualMachineTO to = toVmTO(profile);
    final PrepareForMigrationCommand pfmc = new PrepareForMigrationCommand(to);
    ItWorkVO work = new ItWorkVO(UUID.randomUUID().toString(), _nodeId, State.Migrating, vm.getType(), vm.getId());
    work.setStep(Step.Prepare);
    work.setResourceType(ItWorkVO.ResourceType.Host);
    work.setResourceId(dstHostId);
    work = _workDao.persist(work);
    Answer pfma = null;
    try {
        pfma = _agentMgr.send(dstHostId, pfmc);
        if (pfma == null || !pfma.getResult()) {
            final String details = pfma != null ? pfma.getDetails() : "null answer returned";
            final String msg = "Unable to prepare for migration due to " + details;
            pfma = null;
            throw new AgentUnavailableException(msg, dstHostId);
        }
    } catch (final OperationTimedoutException e1) {
        throw new AgentUnavailableException("Operation timed out", dstHostId);
    } finally {
        if (pfma == null) {
            work.setStep(Step.Done);
            _workDao.update(work.getId(), work);
        }
    }
    vm.setLastHostId(srcHostId);
    try {
        if (vm == null || vm.getHostId() == null || vm.getHostId() != srcHostId || !changeState(vm, Event.MigrationRequested, dstHostId, work, Step.Migrating)) {
            s_logger.info("Migration cancelled because state has changed: " + vm);
            throw new ConcurrentOperationException("Migration cancelled because state has changed: " + vm);
        }
    } catch (final NoTransitionException e1) {
        s_logger.info("Migration cancelled because " + e1.getMessage());
        throw new ConcurrentOperationException("Migration cancelled because " + e1.getMessage());
    }
    boolean migrated = false;
    try {
        final boolean isWindows = _guestOsCategoryDao.findById(_guestOsDao.findById(vm.getGuestOSId()).getCategoryId()).getName().equalsIgnoreCase("Windows");
        final MigrateCommand mc = new MigrateCommand(vm.getInstanceName(), dest.getHost().getPrivateIpAddress(), isWindows, to, getExecuteInSequence(vm.getHypervisorType()));
        mc.setHostGuid(dest.getHost().getGuid());
        try {
            final Answer ma = _agentMgr.send(vm.getLastHostId(), mc);
            if (ma == null || !ma.getResult()) {
                final String details = ma != null ? ma.getDetails() : "null answer returned";
                final String msg = "Unable to migrate due to " + details;
                s_logger.error(msg);
                throw new CloudRuntimeException(msg);
            }
        } catch (final OperationTimedoutException e) {
            if (e.isActive()) {
                s_logger.warn("Active migration command so scheduling a restart for " + vm);
                _haMgr.scheduleRestart(vm, true);
            }
            throw new AgentUnavailableException("Operation timed out on migrating " + vm, dstHostId);
        }
        try {
            final long newServiceOfferingId = vm.getServiceOfferingId();
            // release capacity for the old service offering only
            vm.setServiceOfferingId(oldSvcOfferingId);
            if (!changeState(vm, VirtualMachine.Event.OperationSucceeded, dstHostId, work, Step.Started)) {
                throw new ConcurrentOperationException("Unable to change the state for " + vm);
            }
            vm.setServiceOfferingId(newServiceOfferingId);
        } catch (final NoTransitionException e1) {
            throw new ConcurrentOperationException("Unable to change state due to " + e1.getMessage());
        }
        try {
            if (!checkVmOnHost(vm, dstHostId)) {
                s_logger.error("Unable to complete migration for " + vm);
                try {
                    _agentMgr.send(srcHostId, new Commands(cleanup(vm.getInstanceName())), null);
                } catch (final AgentUnavailableException e) {
                    s_logger.error("AgentUnavailableException while cleanup on source host: " + srcHostId);
                }
                cleanup(vmGuru, new VirtualMachineProfileImpl(vm), work, Event.AgentReportStopped, true);
                throw new CloudRuntimeException("Unable to complete migration for " + vm);
            }
        } catch (final OperationTimedoutException e) {
            s_logger.debug("Error while checking the vm " + vm + " on host " + dstHostId, e);
        }
        migrated = true;
    } finally {
        if (!migrated) {
            s_logger.info("Migration was unsuccessful.  Cleaning up: " + vm);
            _alertMgr.sendAlert(alertType, fromHost.getDataCenterId(), fromHost.getPodId(), "Unable to migrate vm " + vm.getInstanceName() + " from host " + fromHost.getName() + " in zone " + dest.getZone().getName() + " and pod " + dest.getPod().getName(), "Migrate Command failed.  Please check logs.");
            try {
                _agentMgr.send(dstHostId, new Commands(cleanup(vm.getInstanceName())), null);
            } catch (final AgentUnavailableException ae) {
                s_logger.info("Looks like the destination Host is unavailable for cleanup");
            }
            try {
                stateTransitTo(vm, Event.OperationFailed, srcHostId);
            } catch (final NoTransitionException e) {
                s_logger.warn(e.getMessage());
            }
        }
        work.setStep(Step.Done);
        _workDao.update(work.getId(), work);
    }
}
Also used : AlertManager(com.cloud.alert.AlertManager) OperationTimedoutException(com.cloud.legacymodel.exceptions.OperationTimedoutException) Host(com.cloud.legacymodel.dc.Host) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) VirtualMachineTO(com.cloud.legacymodel.to.VirtualMachineTO) MigrateCommand(com.cloud.legacymodel.communication.command.MigrateCommand) UnPlugNicAnswer(com.cloud.legacymodel.communication.answer.UnPlugNicAnswer) AgentControlAnswer(com.cloud.legacymodel.communication.answer.AgentControlAnswer) ClusterVMMetaDataSyncAnswer(com.cloud.legacymodel.communication.answer.ClusterVMMetaDataSyncAnswer) RestoreVMSnapshotAnswer(com.cloud.legacymodel.communication.answer.RestoreVMSnapshotAnswer) RebootAnswer(com.cloud.legacymodel.communication.answer.RebootAnswer) StartAnswer(com.cloud.legacymodel.communication.answer.StartAnswer) PlugNicAnswer(com.cloud.legacymodel.communication.answer.PlugNicAnswer) CheckVirtualMachineAnswer(com.cloud.legacymodel.communication.answer.CheckVirtualMachineAnswer) StopAnswer(com.cloud.legacymodel.communication.answer.StopAnswer) Answer(com.cloud.legacymodel.communication.answer.Answer) PrepareForMigrationCommand(com.cloud.legacymodel.communication.command.PrepareForMigrationCommand) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) AgentUnavailableException(com.cloud.legacymodel.exceptions.AgentUnavailableException) NoTransitionException(com.cloud.legacymodel.exceptions.NoTransitionException) Commands(com.cloud.agent.manager.Commands)

Example 7 with OperationTimedoutException

use of com.cloud.legacymodel.exceptions.OperationTimedoutException in project cosmic by MissionCriticalCloud.

the class VMSnapshotStrategyTest method testDeleteVMSnapshot.

@Test
public void testDeleteVMSnapshot() throws AgentUnavailableException, OperationTimedoutException {
    final Long hostId = 1L;
    final Long vmId = 1L;
    final Long guestOsId = 1L;
    final HypervisorType hypervisorType = HypervisorType.Any;
    final String hypervisorVersion = "default";
    final String guestOsName = "Other";
    final List<VolumeObjectTO> volumeObjectTOs = new ArrayList<>();
    final VMSnapshotVO vmSnapshot = Mockito.mock(VMSnapshotVO.class);
    final UserVmVO userVmVO = Mockito.mock(UserVmVO.class);
    Mockito.when(userVmVO.getGuestOSId()).thenReturn(guestOsId);
    Mockito.when(vmSnapshot.getVmId()).thenReturn(vmId);
    Mockito.when(vmSnapshotHelper.pickRunningHost(Matchers.anyLong())).thenReturn(hostId);
    Mockito.when(vmSnapshotHelper.getVolumeTOList(Matchers.anyLong())).thenReturn(volumeObjectTOs);
    Mockito.when(userVmDao.findById(Matchers.anyLong())).thenReturn(userVmVO);
    final GuestOSVO guestOSVO = Mockito.mock(GuestOSVO.class);
    Mockito.when(guestOSDao.findById(Matchers.anyLong())).thenReturn(guestOSVO);
    final GuestOSHypervisorVO guestOSHypervisorVO = Mockito.mock(GuestOSHypervisorVO.class);
    Mockito.when(guestOSHypervisorVO.getGuestOsName()).thenReturn(guestOsName);
    Mockito.when(guestOsHypervisorDao.findById(Matchers.anyLong())).thenReturn(guestOSHypervisorVO);
    Mockito.when(guestOsHypervisorDao.findByOsIdAndHypervisor(Matchers.anyLong(), Matchers.anyString(), Matchers.anyString())).thenReturn(guestOSHypervisorVO);
    final VMSnapshotTO vmSnapshotTO = Mockito.mock(VMSnapshotTO.class);
    Mockito.when(vmSnapshotHelper.getSnapshotWithParents(Matchers.any(VMSnapshotVO.class))).thenReturn(vmSnapshotTO);
    Mockito.when(vmSnapshotDao.findById(Matchers.anyLong())).thenReturn(vmSnapshot);
    Mockito.when(vmSnapshot.getId()).thenReturn(1L);
    Mockito.when(vmSnapshot.getCreated()).thenReturn(new Date());
    Mockito.when(agentMgr.send(Matchers.anyLong(), Matchers.any(Command.class))).thenReturn(null);
    final HostVO hostVO = Mockito.mock(HostVO.class);
    Mockito.when(hostDao.findById(Matchers.anyLong())).thenReturn(hostVO);
    Mockito.when(hostVO.getHypervisorType()).thenReturn(hypervisorType);
    Mockito.when(hostVO.getHypervisorVersion()).thenReturn(hypervisorVersion);
    Exception e = null;
    try {
        vmSnapshotStrategy.deleteVMSnapshot(vmSnapshot);
    } catch (final CloudRuntimeException e1) {
        e = e1;
    }
    assertNotNull(e);
    final DeleteVMSnapshotAnswer answer = Mockito.mock(DeleteVMSnapshotAnswer.class);
    Mockito.when(answer.getResult()).thenReturn(true);
    Mockito.when(agentMgr.send(Matchers.anyLong(), Matchers.any(Command.class))).thenReturn(answer);
    final boolean result = vmSnapshotStrategy.deleteVMSnapshot(vmSnapshot);
    assertTrue(result);
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) ArrayList(java.util.ArrayList) GuestOSVO(com.cloud.storage.GuestOSVO) Date(java.util.Date) HostVO(com.cloud.host.HostVO) AgentUnavailableException(com.cloud.legacymodel.exceptions.AgentUnavailableException) OperationTimedoutException(com.cloud.legacymodel.exceptions.OperationTimedoutException) IOException(java.io.IOException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) HypervisorType(com.cloud.model.enumeration.HypervisorType) GuestOSHypervisorVO(com.cloud.storage.GuestOSHypervisorVO) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) VMSnapshotTO(com.cloud.legacymodel.to.VMSnapshotTO) Command(com.cloud.legacymodel.communication.command.Command) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) DeleteVMSnapshotAnswer(com.cloud.legacymodel.communication.answer.DeleteVMSnapshotAnswer) Test(org.junit.Test)

Example 8 with OperationTimedoutException

use of com.cloud.legacymodel.exceptions.OperationTimedoutException in project cosmic by MissionCriticalCloud.

the class LibvirtServerDiscoverer method deleteHost.

@Override
public DeleteHostAnswer deleteHost(final HostVO host, final boolean isForced, final boolean isForceDeleteStorage) throws UnableDeleteHostException {
    if (host.getType() != HostType.Routing || host.getHypervisorType() != HypervisorType.KVM) {
        return null;
    }
    this._resourceMgr.deleteRoutingHost(host, isForced, isForceDeleteStorage);
    try {
        final ShutdownCommand cmd = new ShutdownCommand(ShutdownCommand.DeleteHost, null);
        this._agentMgr.send(host.getId(), cmd);
    } catch (final AgentUnavailableException e) {
        s_logger.warn("Sending ShutdownCommand failed: ", e);
    } catch (final OperationTimedoutException e) {
        s_logger.warn("Sending ShutdownCommand failed: ", e);
    }
    return new DeleteHostAnswer(true);
}
Also used : OperationTimedoutException(com.cloud.legacymodel.exceptions.OperationTimedoutException) AgentUnavailableException(com.cloud.legacymodel.exceptions.AgentUnavailableException) ShutdownCommand(com.cloud.legacymodel.communication.command.ShutdownCommand)

Example 9 with OperationTimedoutException

use of com.cloud.legacymodel.exceptions.OperationTimedoutException in project cosmic by MissionCriticalCloud.

the class XcpServerDiscoverer method processConnect.

@Override
public void processConnect(final com.cloud.legacymodel.dc.Host agent, final StartupCommand[] startupCommands, final boolean forRebalance) throws ConnectionException {
    for (final StartupCommand startupCommand : startupCommands) {
        if (!(startupCommand instanceof StartupRoutingCommand)) {
            return;
        }
        final long agentId = agent.getId();
        final StartupRoutingCommand startup = (StartupRoutingCommand) startupCommand;
        if (startup.getHypervisorType() != HypervisorType.XenServer) {
            s_logger.debug("Not XenServer so moving on.");
            return;
        }
        final HostVO host = this._hostDao.findById(agentId);
        final ClusterVO cluster = this._clusterDao.findById(host.getClusterId());
        if (cluster.getGuid() == null) {
            cluster.setGuid(startup.getPool());
            this._clusterDao.update(cluster.getId(), cluster);
        } else if (!cluster.getGuid().equals(startup.getPool())) {
            final String msg = "pool uuid for cluster " + cluster.getId() + " changed from " + cluster.getGuid() + " to " + startup.getPool();
            s_logger.warn(msg);
            throw new CloudRuntimeException(msg);
        }
        final Map<String, String> details = startup.getHostDetails();
        final String prodBrand = details.get("product_brand").trim();
        final String prodVersion = details.get("product_version").trim();
        final String hotfix = details.get(XenserverConfigs.XS620HotFix);
        final String prodVersionTextShort = details.get("product_version_text_short");
        final String resource = createServerResource(prodBrand, prodVersion, prodVersionTextShort, hotfix).getClass().getName();
        if (!resource.equals(host.getResource())) {
            final String msg = "host " + host.getPrivateIpAddress() + " changed from " + host.getResource() + " to " + resource;
            s_logger.debug(msg);
            host.setResource(resource);
            host.setSetup(false);
            this._hostDao.update(agentId, host);
            throw new HypervisorVersionChangedException(msg);
        }
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Setting up host " + agentId);
        }
        final HostEnvironment env = new HostEnvironment();
        final SetupCommand setup = new SetupCommand(env);
        if (this._setupMultipath) {
            setup.setMultipathOn();
        }
        if (!host.isSetup()) {
            setup.setNeedSetup(true);
        }
        try {
            final Answer answer = this._agentMgr.send(agentId, setup);
            if (answer != null && answer.getResult() && answer instanceof SetupAnswer) {
                host.setSetup(true);
                host.setLastPinged((System.currentTimeMillis() >> 10) - 5 * 60);
                host.setHypervisorVersion(prodVersion);
                this._hostDao.update(host.getId(), host);
                if (((SetupAnswer) answer).needReconnect()) {
                    throw new ConnectionException(false, "Reinitialize agent after setup.");
                }
                return;
            } else {
                s_logger.warn("Unable to setup agent " + agentId + " due to " + ((answer != null) ? answer.getDetails() : "return null"));
            }
        } catch (final AgentUnavailableException e) {
            s_logger.warn("Unable to setup agent " + agentId + " because it became unavailable.", e);
        } catch (final OperationTimedoutException e) {
            s_logger.warn("Unable to setup agent " + agentId + " because it timed out", e);
        }
        throw new ConnectionException(true, "Reinitialize agent after setup.");
    }
}
Also used : OperationTimedoutException(com.cloud.legacymodel.exceptions.OperationTimedoutException) ClusterVO(com.cloud.dc.ClusterVO) HostEnvironment(com.cloud.legacymodel.dc.HostEnvironment) SetupCommand(com.cloud.legacymodel.communication.command.SetupCommand) HostVO(com.cloud.host.HostVO) SetupAnswer(com.cloud.legacymodel.communication.answer.SetupAnswer) StartupCommand(com.cloud.legacymodel.communication.command.startup.StartupCommand) HypervisorVersionChangedException(com.cloud.legacymodel.exceptions.HypervisorVersionChangedException) AgentControlAnswer(com.cloud.legacymodel.communication.answer.AgentControlAnswer) Answer(com.cloud.legacymodel.communication.answer.Answer) SetupAnswer(com.cloud.legacymodel.communication.answer.SetupAnswer) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) AgentUnavailableException(com.cloud.legacymodel.exceptions.AgentUnavailableException) StartupRoutingCommand(com.cloud.legacymodel.communication.command.startup.StartupRoutingCommand) ConnectionException(com.cloud.legacymodel.exceptions.ConnectionException)

Example 10 with OperationTimedoutException

use of com.cloud.legacymodel.exceptions.OperationTimedoutException in project cosmic by MissionCriticalCloud.

the class CheckOnAgentInvestigator method isVmAlive.

@Override
public boolean isVmAlive(final VirtualMachine vm, final Host host) throws UnknownVM {
    final CheckVirtualMachineCommand cmd = new CheckVirtualMachineCommand(vm.getInstanceName());
    try {
        final CheckVirtualMachineAnswer answer = (CheckVirtualMachineAnswer) _agentMgr.send(vm.getHostId(), cmd);
        if (!answer.getResult()) {
            s_logger.debug("Unable to get vm state on " + vm.toString());
            throw new UnknownVM();
        }
        s_logger.debug("Agent responded with state " + answer.getState().toString());
        return answer.getState() == PowerState.PowerOn;
    } catch (final AgentUnavailableException e) {
        s_logger.debug("Unable to reach the agent for " + vm.toString() + ": " + e.getMessage());
        throw new UnknownVM();
    } catch (final OperationTimedoutException e) {
        s_logger.debug("Operation timed out for " + vm.toString() + ": " + e.getMessage());
        throw new UnknownVM();
    }
}
Also used : CheckVirtualMachineAnswer(com.cloud.legacymodel.communication.answer.CheckVirtualMachineAnswer) OperationTimedoutException(com.cloud.legacymodel.exceptions.OperationTimedoutException) AgentUnavailableException(com.cloud.legacymodel.exceptions.AgentUnavailableException) CheckVirtualMachineCommand(com.cloud.legacymodel.communication.command.CheckVirtualMachineCommand)

Aggregations

OperationTimedoutException (com.cloud.legacymodel.exceptions.OperationTimedoutException)42 AgentUnavailableException (com.cloud.legacymodel.exceptions.AgentUnavailableException)32 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)24 Answer (com.cloud.legacymodel.communication.answer.Answer)16 HostVO (com.cloud.host.HostVO)14 ResourceUnavailableException (com.cloud.legacymodel.exceptions.ResourceUnavailableException)12 AgentControlAnswer (com.cloud.legacymodel.communication.answer.AgentControlAnswer)10 UnPlugNicAnswer (com.cloud.legacymodel.communication.answer.UnPlugNicAnswer)9 ConcurrentOperationException (com.cloud.legacymodel.exceptions.ConcurrentOperationException)9 NoTransitionException (com.cloud.legacymodel.exceptions.NoTransitionException)9 CheckVirtualMachineAnswer (com.cloud.legacymodel.communication.answer.CheckVirtualMachineAnswer)8 PlugNicAnswer (com.cloud.legacymodel.communication.answer.PlugNicAnswer)8 RestoreVMSnapshotAnswer (com.cloud.legacymodel.communication.answer.RestoreVMSnapshotAnswer)8 StartAnswer (com.cloud.legacymodel.communication.answer.StartAnswer)8 Commands (com.cloud.agent.manager.Commands)7 ClusterVMMetaDataSyncAnswer (com.cloud.legacymodel.communication.answer.ClusterVMMetaDataSyncAnswer)7 RebootAnswer (com.cloud.legacymodel.communication.answer.RebootAnswer)7 StopAnswer (com.cloud.legacymodel.communication.answer.StopAnswer)7 Host (com.cloud.legacymodel.dc.Host)6 VolumeObjectTO (com.cloud.legacymodel.to.VolumeObjectTO)6