Search in sources :

Example 1 with ReadyCommand

use of com.cloud.legacymodel.communication.command.ReadyCommand 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 ReadyCommand

use of com.cloud.legacymodel.communication.command.ReadyCommand in project cosmic by MissionCriticalCloud.

the class Agent method processReadyCommand.

public void processReadyCommand(final Command cmd) {
    final ReadyCommand ready = (ReadyCommand) cmd;
    logger.info("Process agent ready command, agent id = " + ready.getHostId());
    if (ready.getHostId() != null) {
        setId(ready.getHostId());
    }
    logger.info("Ready command is processed: agent id = " + getId());
}
Also used : ReadyCommand(com.cloud.legacymodel.communication.command.ReadyCommand)

Example 3 with ReadyCommand

use of com.cloud.legacymodel.communication.command.ReadyCommand in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testReadyCommand.

@Test
public void testReadyCommand() {
    final ReadyCommand command = new ReadyCommand(1l);
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
    assertTrue(answer.getResult());
}
Also used : Answer(com.cloud.legacymodel.communication.answer.Answer) CheckRouterAnswer(com.cloud.legacymodel.communication.answer.CheckRouterAnswer) AttachAnswer(com.cloud.legacymodel.communication.answer.AttachAnswer) LibvirtRequestWrapper(com.cloud.agent.resource.kvm.wrapper.LibvirtRequestWrapper) ReadyCommand(com.cloud.legacymodel.communication.command.ReadyCommand) Test(org.junit.Test)

Example 4 with ReadyCommand

use of com.cloud.legacymodel.communication.command.ReadyCommand in project cosmic by MissionCriticalCloud.

the class AgentManagerImpl method notifyMonitorsOfConnection.

protected AgentAttache notifyMonitorsOfConnection(final AgentAttache attache, final StartupCommand[] cmd, final boolean forRebalance) throws ConnectionException {
    final long hostId = attache.getId();
    final HostVO host = this._hostDao.findById(hostId);
    for (final Pair<Integer, Listener> monitor : this._hostMonitors) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Sending Connect to listener: " + monitor.second().getClass().getSimpleName());
        }
        try {
            monitor.second().processConnect(host, cmd, forRebalance);
        } catch (final Exception e) {
            if (e instanceof ConnectionException) {
                final ConnectionException ce = (ConnectionException) e;
                if (ce.isSetupError()) {
                    s_logger.warn("Monitor " + monitor.second().getClass().getSimpleName() + " says there is an error in the connect process for " + hostId + " due to " + e.getMessage());
                    handleDisconnectWithoutInvestigation(attache, Event.AgentDisconnected, true, true);
                    throw ce;
                } else {
                    s_logger.info("Monitor " + monitor.second().getClass().getSimpleName() + " says not to continue the connect process for " + hostId + " due to " + e.getMessage());
                    handleDisconnectWithoutInvestigation(attache, Event.ShutdownRequested, true, true);
                    return attache;
                }
            } else if (e instanceof HypervisorVersionChangedException) {
                handleDisconnectWithoutInvestigation(attache, Event.ShutdownRequested, true, true);
                throw new CloudRuntimeException("Unable to connect " + attache.getId(), e);
            } else {
                s_logger.error("Monitor " + monitor.second().getClass().getSimpleName() + " says there is an error in the connect process for " + hostId + " due to " + e.getMessage(), e);
                handleDisconnectWithoutInvestigation(attache, Event.AgentDisconnected, true, true);
                throw new CloudRuntimeException("Unable to connect " + attache.getId(), e);
            }
        }
    }
    final Long dcId = host.getDataCenterId();
    final ReadyCommand ready = new ReadyCommand(dcId, host.getId());
    final Answer answer = easySend(hostId, ready);
    if (answer == null || !answer.getResult()) {
        // this is tricky part for secondary storage
        // make it as disconnected, wait for secondary storage VM to be up
        // return the attache instead of null, even it is disconnectede
        handleDisconnectWithoutInvestigation(attache, Event.AgentDisconnected, true, true);
    }
    agentStatusTransitTo(host, Event.Ready, this._nodeId);
    attache.ready();
    return attache;
}
Also used : HypervisorVersionChangedException(com.cloud.legacymodel.exceptions.HypervisorVersionChangedException) AgentControlAnswer(com.cloud.legacymodel.communication.answer.AgentControlAnswer) StartupAnswer(com.cloud.legacymodel.communication.answer.StartupAnswer) PingAnswer(com.cloud.legacymodel.communication.answer.PingAnswer) ReadyAnswer(com.cloud.legacymodel.communication.answer.ReadyAnswer) Answer(com.cloud.legacymodel.communication.answer.Answer) UnsupportedAnswer(com.cloud.legacymodel.communication.answer.UnsupportedAnswer) Listener(com.cloud.agent.Listener) ReadyCommand(com.cloud.legacymodel.communication.command.ReadyCommand) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) HostVO(com.cloud.host.HostVO) ConnectionException(com.cloud.legacymodel.exceptions.ConnectionException) UnsupportedVersionException(com.cloud.legacymodel.exceptions.UnsupportedVersionException) HypervisorVersionChangedException(com.cloud.legacymodel.exceptions.HypervisorVersionChangedException) InvocationTargetException(java.lang.reflect.InvocationTargetException) AgentUnavailableException(com.cloud.legacymodel.exceptions.AgentUnavailableException) ConfigurationException(javax.naming.ConfigurationException) OperationTimedoutException(com.cloud.legacymodel.exceptions.OperationTimedoutException) TaskExecutionException(com.cloud.legacymodel.exceptions.TaskExecutionException) ClosedChannelException(java.nio.channels.ClosedChannelException) NoTransitionException(com.cloud.legacymodel.exceptions.NoTransitionException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) NioConnectionException(com.cloud.legacymodel.exceptions.NioConnectionException) ConnectionException(com.cloud.legacymodel.exceptions.ConnectionException) NioConnectionException(com.cloud.legacymodel.exceptions.NioConnectionException)

Example 5 with ReadyCommand

use of com.cloud.legacymodel.communication.command.ReadyCommand in project cosmic by MissionCriticalCloud.

the class AgentManagerImpl method handleConnectedAgent.

private AgentAttache handleConnectedAgent(final Link link, final StartupCommand[] startup, final Request request) {
    AgentAttache attache = null;
    ReadyCommand ready = null;
    try {
        final HostVO host = this._resourceMgr.createHostVOForConnectedAgent(startup);
        if (host != null) {
            ready = new ReadyCommand(host.getDataCenterId(), host.getId());
            attache = createAttacheForConnect(host, link);
            attache = notifyMonitorsOfConnection(attache, startup, false);
        }
    } catch (final Exception e) {
        s_logger.debug("Failed to handle host connection: " + e.toString());
        ready = new ReadyCommand(null);
        ready.setDetails(e.toString());
    } finally {
        if (ready == null) {
            ready = new ReadyCommand(null);
        }
    }
    try {
        if (attache == null) {
            final Request readyRequest = new Request(-1, -1, ready, false);
            link.send(readyRequest.getBytes());
        } else {
            easySend(attache.getId(), ready);
        }
    } catch (final Exception e) {
        s_logger.debug("Failed to send ready command:" + e.toString());
    }
    return attache;
}
Also used : ReadyCommand(com.cloud.legacymodel.communication.command.ReadyCommand) Request(com.cloud.common.transport.Request) HostVO(com.cloud.host.HostVO) ConnectionException(com.cloud.legacymodel.exceptions.ConnectionException) UnsupportedVersionException(com.cloud.legacymodel.exceptions.UnsupportedVersionException) HypervisorVersionChangedException(com.cloud.legacymodel.exceptions.HypervisorVersionChangedException) InvocationTargetException(java.lang.reflect.InvocationTargetException) AgentUnavailableException(com.cloud.legacymodel.exceptions.AgentUnavailableException) ConfigurationException(javax.naming.ConfigurationException) OperationTimedoutException(com.cloud.legacymodel.exceptions.OperationTimedoutException) TaskExecutionException(com.cloud.legacymodel.exceptions.TaskExecutionException) ClosedChannelException(java.nio.channels.ClosedChannelException) NoTransitionException(com.cloud.legacymodel.exceptions.NoTransitionException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) NioConnectionException(com.cloud.legacymodel.exceptions.NioConnectionException)

Aggregations

ReadyCommand (com.cloud.legacymodel.communication.command.ReadyCommand)7 Answer (com.cloud.legacymodel.communication.answer.Answer)5 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)3 ClosedChannelException (java.nio.channels.ClosedChannelException)3 Test (org.junit.Test)3 HostVO (com.cloud.host.HostVO)2 AgentControlAnswer (com.cloud.legacymodel.communication.answer.AgentControlAnswer)2 AttachAnswer (com.cloud.legacymodel.communication.answer.AttachAnswer)2 StartupAnswer (com.cloud.legacymodel.communication.answer.StartupAnswer)2 AgentUnavailableException (com.cloud.legacymodel.exceptions.AgentUnavailableException)2 ConnectionException (com.cloud.legacymodel.exceptions.ConnectionException)2 HypervisorVersionChangedException (com.cloud.legacymodel.exceptions.HypervisorVersionChangedException)2 NioConnectionException (com.cloud.legacymodel.exceptions.NioConnectionException)2 NoTransitionException (com.cloud.legacymodel.exceptions.NoTransitionException)2 OperationTimedoutException (com.cloud.legacymodel.exceptions.OperationTimedoutException)2 TaskExecutionException (com.cloud.legacymodel.exceptions.TaskExecutionException)2 UnsupportedVersionException (com.cloud.legacymodel.exceptions.UnsupportedVersionException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ConfigurationException (javax.naming.ConfigurationException)2 Listener (com.cloud.agent.Listener)1