Search in sources :

Example 1 with MaintainAnswer

use of com.cloud.legacymodel.communication.answer.MaintainAnswer in project cosmic by MissionCriticalCloud.

the class Agent method processRequest.

protected void processRequest(final Request request, final Link link) {
    boolean requestLogged = false;
    Response response = null;
    try {
        final Command[] cmds = request.getCommands();
        final Answer[] answers = new Answer[cmds.length];
        for (int i = 0; i < cmds.length; i++) {
            final Command cmd = cmds[i];
            Answer answer;
            if (cmd.getContextParam("logid") != null) {
                MDC.put("logcontextid", cmd.getContextParam("logid"));
            }
            if (// ensures request is logged only once per method call
            !requestLogged) {
                final String requestMsg = request.toString();
                if (requestMsg != null) {
                    logger.debug("Request:" + requestMsg);
                }
                requestLogged = true;
            }
            logger.debug("Processing command: " + cmd.toString());
            if (cmd instanceof CronCommand) {
                final CronCommand watch = (CronCommand) cmd;
                scheduleWatch(link, request, (long) watch.getInterval() * 1000, watch.getInterval() * 1000);
                answer = new Answer(cmd, true, null);
            } else if (cmd instanceof ShutdownCommand) {
                final ShutdownCommand shutdown = (ShutdownCommand) cmd;
                logger.debug("Received shutdownCommand, due to: " + shutdown.getReason());
                cancelTasks();
                this._reconnectAllowed = false;
                answer = new Answer(cmd, true, null);
            } else if (cmd instanceof ReadyCommand && ((ReadyCommand) cmd).getDetails() != null) {
                logger.debug("Not ready to connect to mgt server: " + ((ReadyCommand) cmd).getDetails());
                System.exit(1);
                return;
            } else if (cmd instanceof MaintainCommand) {
                logger.debug("Received maintainCommand");
                cancelTasks();
                this._reconnectAllowed = false;
                answer = new MaintainAnswer((MaintainCommand) cmd);
            } else if (cmd instanceof AgentControlCommand) {
                answer = null;
                synchronized (this._controlListeners) {
                    for (final IAgentControlListener listener : this._controlListeners) {
                        answer = listener.processControlRequest(request, (AgentControlCommand) cmd);
                        if (answer != null) {
                            break;
                        }
                    }
                }
                if (answer == null) {
                    logger.warn("No handler found to process cmd: " + cmd.toString());
                    answer = new AgentControlAnswer(cmd);
                }
            } else {
                if (cmd instanceof ReadyCommand) {
                    processReadyCommand(cmd);
                }
                this._inProgress.incrementAndGet();
                try {
                    answer = this.resource.executeRequest(cmd);
                } finally {
                    this._inProgress.decrementAndGet();
                }
                if (answer == null) {
                    logger.debug("Response: unsupported command" + cmd.toString());
                    answer = Answer.createUnsupportedCommandAnswer(cmd);
                }
            }
            answers[i] = answer;
            if (!answer.getResult() && request.stopOnError()) {
                for (i++; i < cmds.length; i++) {
                    answers[i] = new Answer(cmds[i], false, "Stopped by previous failure");
                }
                break;
            }
        }
        response = new Response(request, answers);
    } catch (final RuntimeException e) {
        logger.error("Error while handling request: " + e.getMessage());
        logger.error(ExceptionUtils.getRootCauseMessage(e));
    } finally {
        if (logger.isDebugEnabled()) {
            final String responseMsg = response.toString();
            if (responseMsg != null) {
                logger.debug(response.toString());
            }
        }
        if (response != null) {
            try {
                link.send(response.toBytes());
            } catch (final ClosedChannelException e) {
                logger.warn("Unable to send response: " + response.toString());
            }
        }
    }
}
Also used : MaintainCommand(com.cloud.legacymodel.communication.command.MaintainCommand) ClosedChannelException(java.nio.channels.ClosedChannelException) MaintainAnswer(com.cloud.legacymodel.communication.answer.MaintainAnswer) CronCommand(com.cloud.legacymodel.communication.command.CronCommand) AgentControlAnswer(com.cloud.legacymodel.communication.answer.AgentControlAnswer) ShutdownCommand(com.cloud.legacymodel.communication.command.ShutdownCommand) Response(com.cloud.common.transport.Response) MaintainAnswer(com.cloud.legacymodel.communication.answer.MaintainAnswer) AgentControlAnswer(com.cloud.legacymodel.communication.answer.AgentControlAnswer) StartupAnswer(com.cloud.legacymodel.communication.answer.StartupAnswer) Answer(com.cloud.legacymodel.communication.answer.Answer) AgentControlCommand(com.cloud.legacymodel.communication.command.agentcontrol.AgentControlCommand) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) MaintainCommand(com.cloud.legacymodel.communication.command.MaintainCommand) ReadyCommand(com.cloud.legacymodel.communication.command.ReadyCommand) CronCommand(com.cloud.legacymodel.communication.command.CronCommand) PingCommand(com.cloud.legacymodel.communication.command.PingCommand) StartupCommand(com.cloud.legacymodel.communication.command.startup.StartupCommand) Command(com.cloud.legacymodel.communication.command.Command) AgentControlCommand(com.cloud.legacymodel.communication.command.agentcontrol.AgentControlCommand) ShutdownCommand(com.cloud.legacymodel.communication.command.ShutdownCommand) ReadyCommand(com.cloud.legacymodel.communication.command.ReadyCommand) IAgentControlListener(com.cloud.common.agent.IAgentControlListener)

Example 2 with MaintainAnswer

use of com.cloud.legacymodel.communication.answer.MaintainAnswer in project cosmic by MissionCriticalCloud.

the class CitrixMaintainCommandWrapper method execute.

@Override
public Answer execute(final MaintainCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    try {
        final XsHost xsHost = citrixResourceBase.getHost();
        final String uuid = xsHost.getUuid();
        final Host host = Host.getByUuid(conn, uuid);
        // remove all tags cloud stack
        final Host.Record hr = host.getRecord(conn);
        // Adding this check because could not get the mock to work. Will push the code and fix it afterwards.
        if (hr == null) {
            s_logger.warn("Host.Record is null.");
            return new MaintainAnswer(command, false, "Host.Record is null");
        }
        final Iterator<String> it = hr.tags.iterator();
        while (it.hasNext()) {
            final String tag = it.next();
            if (tag.contains("cloud")) {
                it.remove();
            }
        }
        host.setTags(conn, hr.tags);
        return new MaintainAnswer(command);
    } catch (final XenAPIException e) {
        s_logger.warn("Unable to put server in maintainence mode", e);
        return new MaintainAnswer(command, false, e.getMessage());
    } catch (final XmlRpcException e) {
        s_logger.warn("Unable to put server in maintainence mode", e);
        return new MaintainAnswer(command, false, e.getMessage());
    }
}
Also used : XsHost(com.cloud.hypervisor.xenserver.resource.XsHost) MaintainAnswer(com.cloud.legacymodel.communication.answer.MaintainAnswer) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) Host(com.xensource.xenapi.Host) XsHost(com.cloud.hypervisor.xenserver.resource.XsHost) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Example 3 with MaintainAnswer

use of com.cloud.legacymodel.communication.answer.MaintainAnswer in project cosmic by MissionCriticalCloud.

the class ResourceManagerImpl method doMaintain.

private boolean doMaintain(final long hostId) {
    final HostVO host = this._hostDao.findById(hostId);
    final MaintainAnswer answer = (MaintainAnswer) this._agentMgr.easySend(hostId, new MaintainCommand());
    if (answer == null || !answer.getResult()) {
        s_logger.warn("Unable to send MaintainCommand to host: " + hostId);
        return false;
    }
    try {
        resourceStateTransitTo(host, ResourceState.Event.AdminAskMaintenace, this._nodeId);
    } catch (final NoTransitionException e) {
        final String err = "Cannot transmit resource state of host " + host.getId() + " to " + ResourceState.Maintenance;
        s_logger.debug(err, e);
        throw new CloudRuntimeException(err + e.getMessage());
    }
    ActionEventUtils.onStartedActionEvent(CallContext.current().getCallingUserId(), CallContext.current().getCallingAccountId(), EventTypes.EVENT_MAINTENANCE_PREPARE, "starting maintenance for host " + hostId, true, 0);
    this._agentMgr.pullAgentToMaintenance(hostId);
    /* TODO: move below to listener */
    if (host.getType() == HostType.Routing) {
        final List<VMInstanceVO> vms = this._vmDao.listByHostId(hostId);
        if (vms.size() == 0) {
            return true;
        }
        final List<HostVO> hosts = listAllUpAndEnabledHosts(HostType.Routing, host.getClusterId(), host.getPodId(), host.getDataCenterId());
        for (final VMInstanceVO vm : vms) {
            if (hosts == null || hosts.isEmpty() || !answer.getMigrate() || this._serviceOfferingDetailsDao.findDetail(vm.getServiceOfferingId(), GPU.Keys.vgpuType.toString()) != null) {
                // Migration is not supported for VGPU Vms so stop them.
                // for the last host in this cluster, stop all the VMs
                this._haMgr.scheduleStop(vm, hostId, HaWork.HaWorkType.ForceStop);
            } else {
                this._haMgr.scheduleMigration(vm);
            }
        }
    }
    return true;
}
Also used : MaintainCommand(com.cloud.legacymodel.communication.command.MaintainCommand) MaintainAnswer(com.cloud.legacymodel.communication.answer.MaintainAnswer) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) NoTransitionException(com.cloud.legacymodel.exceptions.NoTransitionException) VMInstanceVO(com.cloud.vm.VMInstanceVO) StoragePoolHostVO(com.cloud.storage.StoragePoolHostVO) HostVO(com.cloud.host.HostVO)

Aggregations

MaintainAnswer (com.cloud.legacymodel.communication.answer.MaintainAnswer)3 MaintainCommand (com.cloud.legacymodel.communication.command.MaintainCommand)2 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)2 IAgentControlListener (com.cloud.common.agent.IAgentControlListener)1 Response (com.cloud.common.transport.Response)1 HostVO (com.cloud.host.HostVO)1 XsHost (com.cloud.hypervisor.xenserver.resource.XsHost)1 AgentControlAnswer (com.cloud.legacymodel.communication.answer.AgentControlAnswer)1 Answer (com.cloud.legacymodel.communication.answer.Answer)1 StartupAnswer (com.cloud.legacymodel.communication.answer.StartupAnswer)1 Command (com.cloud.legacymodel.communication.command.Command)1 CronCommand (com.cloud.legacymodel.communication.command.CronCommand)1 PingCommand (com.cloud.legacymodel.communication.command.PingCommand)1 ReadyCommand (com.cloud.legacymodel.communication.command.ReadyCommand)1 ShutdownCommand (com.cloud.legacymodel.communication.command.ShutdownCommand)1 AgentControlCommand (com.cloud.legacymodel.communication.command.agentcontrol.AgentControlCommand)1 StartupCommand (com.cloud.legacymodel.communication.command.startup.StartupCommand)1 NoTransitionException (com.cloud.legacymodel.exceptions.NoTransitionException)1 StoragePoolHostVO (com.cloud.storage.StoragePoolHostVO)1 VMInstanceVO (com.cloud.vm.VMInstanceVO)1