Search in sources :

Example 1 with Response

use of com.cloud.common.transport.Response 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 Response

use of com.cloud.common.transport.Response in project cosmic by MissionCriticalCloud.

the class AgentManagerImpl method connectAgent.

protected void connectAgent(final Link link, final Command[] cmds, final Request request) {
    // send startupanswer to agent in the very beginning, so agent can move on without waiting for the answer for an undetermined time, if we put this logic into another
    // thread pool.
    final StartupAnswer[] answers = new StartupAnswer[cmds.length];
    Command cmd;
    for (int i = 0; i < cmds.length; i++) {
        cmd = cmds[i];
        if (cmd instanceof StartupRoutingCommand || cmd instanceof StartupProxyCommand || cmd instanceof StartupSecondaryStorageCommand || cmd instanceof StartupStorageCommand) {
            answers[i] = new StartupAnswer((StartupCommand) cmds[i], 0, getPingInterval());
            break;
        }
    }
    Response response = null;
    response = new Response(request, answers[0], this._nodeId, -1);
    try {
        link.send(response.toBytes());
    } catch (final ClosedChannelException e) {
        s_logger.debug("Failed to send startupanswer: " + e.toString());
    }
    this._connectExecutor.execute(new HandleAgentConnectTask(link, cmds, request));
}
Also used : StartupAnswer(com.cloud.legacymodel.communication.answer.StartupAnswer) StartupProxyCommand(com.cloud.legacymodel.communication.command.startup.StartupProxyCommand) StartupCommand(com.cloud.legacymodel.communication.command.startup.StartupCommand) Response(com.cloud.common.transport.Response) ClosedChannelException(java.nio.channels.ClosedChannelException) ReadyCommand(com.cloud.legacymodel.communication.command.ReadyCommand) 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) StartupStorageCommand(com.cloud.legacymodel.communication.command.startup.StartupStorageCommand) StartupSecondaryStorageCommand(com.cloud.legacymodel.communication.command.startup.StartupSecondaryStorageCommand) StartupRoutingCommand(com.cloud.legacymodel.communication.command.startup.StartupRoutingCommand) CheckHealthCommand(com.cloud.legacymodel.communication.command.CheckHealthCommand) PingRoutingCommand(com.cloud.legacymodel.communication.command.PingRoutingCommand) ShutdownCommand(com.cloud.legacymodel.communication.command.ShutdownCommand) StartupProxyCommand(com.cloud.legacymodel.communication.command.startup.StartupProxyCommand) StartupSecondaryStorageCommand(com.cloud.legacymodel.communication.command.startup.StartupSecondaryStorageCommand) StartupStorageCommand(com.cloud.legacymodel.communication.command.startup.StartupStorageCommand) StartupRoutingCommand(com.cloud.legacymodel.communication.command.startup.StartupRoutingCommand)

Example 3 with Response

use of com.cloud.common.transport.Response in project cosmic by MissionCriticalCloud.

the class DirectAgentAttache method send.

@Override
public void send(final Request req) throws AgentUnavailableException {
    req.logD("Executing: ", true);
    if (req instanceof Response) {
        final Response resp = (Response) req;
        final Answer[] answers = resp.getAnswers();
        if (answers != null && answers[0] instanceof StartupAnswer) {
            final StartupAnswer startup = (StartupAnswer) answers[0];
            final int interval = startup.getPingInterval();
            this._futures.add(this._agentMgr.getCronJobPool().scheduleAtFixedRate(new PingTask(), interval, interval, TimeUnit.SECONDS));
        }
    } else {
        final Command[] cmds = req.getCommands();
        if (cmds.length > 0 && !(cmds[0] instanceof CronCommand)) {
            queueTask(new Task(req));
            scheduleFromQueue();
        } else {
            final CronCommand cmd = (CronCommand) cmds[0];
            this._futures.add(this._agentMgr.getCronJobPool().scheduleAtFixedRate(new CronTask(req), cmd.getInterval(), cmd.getInterval(), TimeUnit.SECONDS));
        }
    }
}
Also used : Response(com.cloud.common.transport.Response) StartupAnswer(com.cloud.legacymodel.communication.answer.StartupAnswer) StartupAnswer(com.cloud.legacymodel.communication.answer.StartupAnswer) Answer(com.cloud.legacymodel.communication.answer.Answer) Command(com.cloud.legacymodel.communication.command.Command) CronCommand(com.cloud.legacymodel.communication.command.CronCommand) PingCommand(com.cloud.legacymodel.communication.command.PingCommand) CronCommand(com.cloud.legacymodel.communication.command.CronCommand)

Example 4 with Response

use of com.cloud.common.transport.Response in project cosmic by MissionCriticalCloud.

the class Agent method processOtherTask.

public void processOtherTask(final Task task) {
    final Object obj = task.get();
    if (obj instanceof Response) {
        if (System.currentTimeMillis() - this._lastPingResponseTime > this._pingInterval * this.agentConfiguration.getPingRetries()) {
            logger.error("Ping Interval has gone past " + this._pingInterval * this.agentConfiguration.getPingRetries() + ". Won't reconnect to mgt server, as connection is still alive");
            return;
        }
        final PingCommand ping = this.resource.getCurrentStatus(getId());
        final Request request = new Request(this._id, -1, ping, false);
        request.setSequence(getNextSequence());
        if (logger.isDebugEnabled()) {
            logger.debug("Sending ping: " + request.toString());
        }
        try {
            task.getLink().send(request.toBytes());
            // if i can send pingcommand out, means the link is ok
            setLastPingResponseTime();
        } catch (final ClosedChannelException e) {
            logger.warn("Unable to send request: " + request.toString());
        }
    } else if (obj instanceof Request) {
        final Request req = (Request) obj;
        final Command command = req.getCommand();
        if (command.getContextParam("logid") != null) {
            MDC.put("logcontextid", command.getContextParam("logid"));
        }
        Answer answer = null;
        this._inProgress.incrementAndGet();
        try {
            answer = this.resource.executeRequest(command);
        } finally {
            this._inProgress.decrementAndGet();
        }
        if (answer != null) {
            final Response response = new Response(req, answer);
            if (logger.isDebugEnabled()) {
                logger.debug("Watch Sent: " + response.toString());
            }
            try {
                task.getLink().send(response.toBytes());
            } catch (final ClosedChannelException e) {
                logger.warn("Unable to send response: " + response.toString());
            }
        }
    } else {
        logger.warn("Ignoring an unknown task");
    }
}
Also used : Response(com.cloud.common.transport.Response) ClosedChannelException(java.nio.channels.ClosedChannelException) 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) 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) Request(com.cloud.common.transport.Request) PingCommand(com.cloud.legacymodel.communication.command.PingCommand)

Example 5 with Response

use of com.cloud.common.transport.Response in project cosmic by MissionCriticalCloud.

the class AgentAttache method send.

public Answer[] send(final Request req, final int wait) throws AgentUnavailableException, OperationTimedoutException {
    final SynchronousListener sl = new SynchronousListener(null);
    final long seq = req.getSequence();
    send(req, sl);
    try {
        for (int i = 0; i < 2; i++) {
            Answer[] answers = null;
            try {
                answers = sl.waitFor(wait);
            } catch (final InterruptedException e) {
                s_logger.debug(log(seq, "Interrupted"));
            }
            if (answers != null) {
                if (s_logger.isDebugEnabled()) {
                    new Response(req, answers).logD("Received: ", false);
                }
                return answers;
            }
            // Try it again.
            answers = sl.getAnswers();
            if (answers != null) {
                if (s_logger.isDebugEnabled()) {
                    new Response(req, answers).logD("Received after timeout: ", true);
                }
                _agentMgr.notifyAnswersToMonitors(_id, seq, answers);
                return answers;
            }
            final Long current = _currentSequence;
            if (current != null && seq != current) {
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug(log(seq, "Waited too long."));
                }
                throw new OperationTimedoutException(req.getCommands(), _id, seq, wait, false);
            }
            if (s_logger.isDebugEnabled()) {
                s_logger.debug(log(seq, "Waiting some more time because this is the current command"));
            }
        }
        throw new OperationTimedoutException(req.getCommands(), _id, seq, wait * 2, true);
    } catch (final OperationTimedoutException e) {
        s_logger.warn(log(seq, "Timed out on " + req.toString()));
        cancel(seq);
        final Long current = _currentSequence;
        if (req.executeInSequence() && (current != null && current == seq)) {
            sendNext(seq);
        }
        throw e;
    } catch (final Exception e) {
        s_logger.warn(log(seq, "Exception while waiting for answer"), e);
        cancel(seq);
        final Long current = _currentSequence;
        if (req.executeInSequence() && (current != null && current == seq)) {
            sendNext(seq);
        }
        throw new OperationTimedoutException(req.getCommands(), _id, seq, wait, false);
    } finally {
        unregisterListener(seq);
    }
}
Also used : Response(com.cloud.common.transport.Response) Answer(com.cloud.legacymodel.communication.answer.Answer) OperationTimedoutException(com.cloud.legacymodel.exceptions.OperationTimedoutException) OperationTimedoutException(com.cloud.legacymodel.exceptions.OperationTimedoutException) AgentUnavailableException(com.cloud.legacymodel.exceptions.AgentUnavailableException)

Aggregations

Response (com.cloud.common.transport.Response)5 Answer (com.cloud.legacymodel.communication.answer.Answer)4 StartupAnswer (com.cloud.legacymodel.communication.answer.StartupAnswer)4 Command (com.cloud.legacymodel.communication.command.Command)4 PingCommand (com.cloud.legacymodel.communication.command.PingCommand)4 CronCommand (com.cloud.legacymodel.communication.command.CronCommand)3 ReadyCommand (com.cloud.legacymodel.communication.command.ReadyCommand)3 ShutdownCommand (com.cloud.legacymodel.communication.command.ShutdownCommand)3 AgentControlCommand (com.cloud.legacymodel.communication.command.agentcontrol.AgentControlCommand)3 StartupCommand (com.cloud.legacymodel.communication.command.startup.StartupCommand)3 ClosedChannelException (java.nio.channels.ClosedChannelException)3 AgentControlAnswer (com.cloud.legacymodel.communication.answer.AgentControlAnswer)2 MaintainAnswer (com.cloud.legacymodel.communication.answer.MaintainAnswer)2 MaintainCommand (com.cloud.legacymodel.communication.command.MaintainCommand)2 IAgentControlListener (com.cloud.common.agent.IAgentControlListener)1 Request (com.cloud.common.transport.Request)1 CheckHealthCommand (com.cloud.legacymodel.communication.command.CheckHealthCommand)1 PingRoutingCommand (com.cloud.legacymodel.communication.command.PingRoutingCommand)1 StartupProxyCommand (com.cloud.legacymodel.communication.command.startup.StartupProxyCommand)1 StartupRoutingCommand (com.cloud.legacymodel.communication.command.startup.StartupRoutingCommand)1