Search in sources :

Example 6 with Response

use of com.cloud.agent.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], _nodeId, -1);
    try {
        link.send(response.toBytes());
    } catch (final ClosedChannelException e) {
        s_logger.debug("Failed to send startupanswer: " + e.toString());
    }
    _connectExecutor.execute(new HandleAgentConnectTask(link, cmds, request));
}
Also used : StartupAnswer(com.cloud.agent.api.StartupAnswer) StartupProxyCommand(com.cloud.agent.api.StartupProxyCommand) StartupCommand(com.cloud.agent.api.StartupCommand) Response(com.cloud.agent.transport.Response) ClosedChannelException(java.nio.channels.ClosedChannelException) StartupStorageCommand(com.cloud.agent.api.StartupStorageCommand) StartupCommand(com.cloud.agent.api.StartupCommand) AgentControlCommand(com.cloud.agent.api.AgentControlCommand) PingCommand(com.cloud.agent.api.PingCommand) PingRoutingCommand(com.cloud.agent.api.PingRoutingCommand) StartupSecondaryStorageCommand(com.cloud.agent.api.StartupSecondaryStorageCommand) StartupRoutingCommand(com.cloud.agent.api.StartupRoutingCommand) ShutdownCommand(com.cloud.agent.api.ShutdownCommand) StartupProxyCommand(com.cloud.agent.api.StartupProxyCommand) CheckHealthCommand(com.cloud.agent.api.CheckHealthCommand) Command(com.cloud.agent.api.Command) ReadyCommand(com.cloud.agent.api.ReadyCommand) StartupSecondaryStorageCommand(com.cloud.agent.api.StartupSecondaryStorageCommand) StartupStorageCommand(com.cloud.agent.api.StartupStorageCommand) StartupRoutingCommand(com.cloud.agent.api.StartupRoutingCommand)

Example 7 with Response

use of com.cloud.agent.transport.Response in project CloudStack-archive by CloudStack-extras.

the class Agent method processOtherTask.

public void processOtherTask(Task task) {
    final Object obj = task.get();
    if (obj instanceof Response) {
        if ((System.currentTimeMillis() - _lastPingResponseTime) > _pingInterval * _shell.getPingRetries()) {
            s_logger.error("Ping Interval has gone past " + _pingInterval * _shell.getPingRetries() + ".  Attempting to reconnect.");
            final Link link = task.getLink();
            reconnect(link);
            return;
        }
        final PingCommand ping = _resource.getCurrentStatus(getId());
        final Request request = new Request(_id, -1, ping, false);
        request.setSequence(getNextSequence());
        if (s_logger.isDebugEnabled()) {
            s_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) {
            s_logger.warn("Unable to send request: " + request.toString());
        }
    } else if (obj instanceof Request) {
        final Request req = (Request) obj;
        final Command command = req.getCommand();
        Answer answer = null;
        _inProgress.incrementAndGet();
        try {
            answer = _resource.executeRequest(command);
        } finally {
            _inProgress.decrementAndGet();
        }
        if (answer != null) {
            final Response response = new Response(req, answer);
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Watch Sent: " + response.toString());
            }
            try {
                task.getLink().send(response.toBytes());
            } catch (final ClosedChannelException e) {
                s_logger.warn("Unable to send response: " + response.toString());
            }
        }
    } else {
        s_logger.warn("Ignoring an unknown task");
    }
}
Also used : Response(com.cloud.agent.transport.Response) ClosedChannelException(java.nio.channels.ClosedChannelException) UpgradeAnswer(com.cloud.agent.api.UpgradeAnswer) StartupAnswer(com.cloud.agent.api.StartupAnswer) AgentControlAnswer(com.cloud.agent.api.AgentControlAnswer) Answer(com.cloud.agent.api.Answer) MaintainAnswer(com.cloud.agent.api.MaintainAnswer) MaintainCommand(com.cloud.agent.api.MaintainCommand) StartupCommand(com.cloud.agent.api.StartupCommand) AgentControlCommand(com.cloud.agent.api.AgentControlCommand) ShutdownCommand(com.cloud.agent.api.ShutdownCommand) Command(com.cloud.agent.api.Command) PingCommand(com.cloud.agent.api.PingCommand) ModifySshKeysCommand(com.cloud.agent.api.ModifySshKeysCommand) UpgradeCommand(com.cloud.agent.api.UpgradeCommand) CronCommand(com.cloud.agent.api.CronCommand) Request(com.cloud.agent.transport.Request) Link(com.cloud.utils.nio.Link) PingCommand(com.cloud.agent.api.PingCommand)

Example 8 with Response

use of com.cloud.agent.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.agent.transport.Response) Answer(com.cloud.agent.api.Answer) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException)

Example 9 with Response

use of com.cloud.agent.transport.Response in project cloudstack by apache.

the class Agent method processOtherTask.

public void processOtherTask(final Task task) {
    final Object obj = task.get();
    if (obj instanceof Response) {
        if (System.currentTimeMillis() - _lastPingResponseTime > _pingInterval * _shell.getPingRetries()) {
            s_logger.error("Ping Interval has gone past " + _pingInterval * _shell.getPingRetries() + ". Won't reconnect to mgt server, as connection is still alive");
            return;
        }
        final PingCommand ping = _resource.getCurrentStatus(getId());
        final Request request = new Request(_id, -1, ping, false);
        request.setSequence(getNextSequence());
        if (s_logger.isDebugEnabled()) {
            s_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) {
            s_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;
        _inProgress.incrementAndGet();
        try {
            answer = _resource.executeRequest(command);
        } finally {
            _inProgress.decrementAndGet();
        }
        if (answer != null) {
            final Response response = new Response(req, answer);
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Watch Sent: " + response.toString());
            }
            try {
                task.getLink().send(response.toBytes());
            } catch (final ClosedChannelException e) {
                s_logger.warn("Unable to send response: " + response.toString());
            }
        }
    } else {
        s_logger.warn("Ignoring an unknown task");
    }
}
Also used : Response(com.cloud.agent.transport.Response) ClosedChannelException(java.nio.channels.ClosedChannelException) SetupKeystoreAnswer(org.apache.cloudstack.ca.SetupKeystoreAnswer) AgentControlAnswer(com.cloud.agent.api.AgentControlAnswer) Answer(com.cloud.agent.api.Answer) MaintainAnswer(com.cloud.agent.api.MaintainAnswer) SetupMSListAnswer(org.apache.cloudstack.agent.lb.SetupMSListAnswer) StartupAnswer(com.cloud.agent.api.StartupAnswer) SetupCertificateAnswer(org.apache.cloudstack.ca.SetupCertificateAnswer) StartupCommand(com.cloud.agent.api.StartupCommand) AgentControlCommand(com.cloud.agent.api.AgentControlCommand) SetupCertificateCommand(org.apache.cloudstack.ca.SetupCertificateCommand) SetupKeyStoreCommand(org.apache.cloudstack.ca.SetupKeyStoreCommand) PingCommand(com.cloud.agent.api.PingCommand) PostCertificateRenewalCommand(org.apache.cloudstack.ca.PostCertificateRenewalCommand) MaintainCommand(com.cloud.agent.api.MaintainCommand) SetupMSListCommand(org.apache.cloudstack.agent.lb.SetupMSListCommand) ShutdownCommand(com.cloud.agent.api.ShutdownCommand) Command(com.cloud.agent.api.Command) ReadyCommand(com.cloud.agent.api.ReadyCommand) CronCommand(com.cloud.agent.api.CronCommand) Request(com.cloud.agent.transport.Request) PingCommand(com.cloud.agent.api.PingCommand)

Example 10 with Response

use of com.cloud.agent.transport.Response in project cloudstack by apache.

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;
            try {
                if (cmd.getContextParam("logid") != null) {
                    MDC.put("logcontextid", cmd.getContextParam("logid"));
                }
                if (s_logger.isDebugEnabled()) {
                    if (// ensures request is logged only once per method call
                    !requestLogged) {
                        final String requestMsg = request.toString();
                        if (requestMsg != null) {
                            s_logger.debug("Request:" + requestMsg);
                        }
                        requestLogged = true;
                    }
                    s_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;
                    s_logger.debug("Received shutdownCommand, due to: " + shutdown.getReason());
                    cancelTasks();
                    if (shutdown.isRemoveHost()) {
                        cleanupAgentZoneProperties();
                    }
                    _reconnectAllowed = false;
                    answer = new Answer(cmd, true, null);
                } else if (cmd instanceof ReadyCommand && ((ReadyCommand) cmd).getDetails() != null) {
                    s_logger.debug("Not ready to connect to mgt server: " + ((ReadyCommand) cmd).getDetails());
                    System.exit(1);
                    return;
                } else if (cmd instanceof MaintainCommand) {
                    s_logger.debug("Received maintainCommand, do not cancel current tasks");
                    answer = new MaintainAnswer((MaintainCommand) cmd);
                } else if (cmd instanceof AgentControlCommand) {
                    answer = null;
                    synchronized (_controlListeners) {
                        for (final IAgentControlListener listener : _controlListeners) {
                            answer = listener.processControlRequest(request, (AgentControlCommand) cmd);
                            if (answer != null) {
                                break;
                            }
                        }
                    }
                    if (answer == null) {
                        s_logger.warn("No handler found to process cmd: " + cmd.toString());
                        answer = new AgentControlAnswer(cmd);
                    }
                } else if (cmd instanceof SetupKeyStoreCommand && ((SetupKeyStoreCommand) cmd).isHandleByAgent()) {
                    answer = setupAgentKeystore((SetupKeyStoreCommand) cmd);
                } else if (cmd instanceof SetupCertificateCommand && ((SetupCertificateCommand) cmd).isHandleByAgent()) {
                    answer = setupAgentCertificate((SetupCertificateCommand) cmd);
                    if (Host.Type.Routing.equals(_resource.getType())) {
                        scheduleServicesRestartTask();
                    }
                } else if (cmd instanceof SetupMSListCommand) {
                    answer = setupManagementServerList((SetupMSListCommand) cmd);
                } else {
                    if (cmd instanceof ReadyCommand) {
                        processReadyCommand(cmd);
                    }
                    _inProgress.incrementAndGet();
                    try {
                        answer = _resource.executeRequest(cmd);
                    } finally {
                        _inProgress.decrementAndGet();
                    }
                    if (answer == null) {
                        s_logger.debug("Response: unsupported command" + cmd.toString());
                        answer = Answer.createUnsupportedCommandAnswer(cmd);
                    }
                }
            } catch (final Throwable th) {
                s_logger.warn("Caught: ", th);
                final StringWriter writer = new StringWriter();
                th.printStackTrace(new PrintWriter(writer));
                answer = new Answer(cmd, false, writer.toString());
            }
            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);
    } finally {
        if (s_logger.isDebugEnabled()) {
            final String responseMsg = response.toString();
            if (responseMsg != null) {
                s_logger.debug(response.toString());
            }
        }
        if (response != null) {
            try {
                link.send(response.toBytes());
            } catch (final ClosedChannelException e) {
                s_logger.warn("Unable to send response: " + response.toString());
            }
        }
    }
}
Also used : MaintainCommand(com.cloud.agent.api.MaintainCommand) ClosedChannelException(java.nio.channels.ClosedChannelException) MaintainAnswer(com.cloud.agent.api.MaintainAnswer) CronCommand(com.cloud.agent.api.CronCommand) AgentControlAnswer(com.cloud.agent.api.AgentControlAnswer) SetupMSListCommand(org.apache.cloudstack.agent.lb.SetupMSListCommand) ShutdownCommand(com.cloud.agent.api.ShutdownCommand) Response(com.cloud.agent.transport.Response) SetupCertificateCommand(org.apache.cloudstack.ca.SetupCertificateCommand) SetupKeystoreAnswer(org.apache.cloudstack.ca.SetupKeystoreAnswer) AgentControlAnswer(com.cloud.agent.api.AgentControlAnswer) Answer(com.cloud.agent.api.Answer) MaintainAnswer(com.cloud.agent.api.MaintainAnswer) SetupMSListAnswer(org.apache.cloudstack.agent.lb.SetupMSListAnswer) StartupAnswer(com.cloud.agent.api.StartupAnswer) SetupCertificateAnswer(org.apache.cloudstack.ca.SetupCertificateAnswer) AgentControlCommand(com.cloud.agent.api.AgentControlCommand) StringWriter(java.io.StringWriter) StartupCommand(com.cloud.agent.api.StartupCommand) AgentControlCommand(com.cloud.agent.api.AgentControlCommand) SetupCertificateCommand(org.apache.cloudstack.ca.SetupCertificateCommand) SetupKeyStoreCommand(org.apache.cloudstack.ca.SetupKeyStoreCommand) PingCommand(com.cloud.agent.api.PingCommand) PostCertificateRenewalCommand(org.apache.cloudstack.ca.PostCertificateRenewalCommand) MaintainCommand(com.cloud.agent.api.MaintainCommand) SetupMSListCommand(org.apache.cloudstack.agent.lb.SetupMSListCommand) ShutdownCommand(com.cloud.agent.api.ShutdownCommand) Command(com.cloud.agent.api.Command) ReadyCommand(com.cloud.agent.api.ReadyCommand) CronCommand(com.cloud.agent.api.CronCommand) ReadyCommand(com.cloud.agent.api.ReadyCommand) SetupKeyStoreCommand(org.apache.cloudstack.ca.SetupKeyStoreCommand) PrintWriter(java.io.PrintWriter)

Aggregations

Response (com.cloud.agent.transport.Response)12 Answer (com.cloud.agent.api.Answer)10 Command (com.cloud.agent.api.Command)10 PingCommand (com.cloud.agent.api.PingCommand)10 StartupAnswer (com.cloud.agent.api.StartupAnswer)10 AgentControlCommand (com.cloud.agent.api.AgentControlCommand)8 CronCommand (com.cloud.agent.api.CronCommand)8 ShutdownCommand (com.cloud.agent.api.ShutdownCommand)8 StartupCommand (com.cloud.agent.api.StartupCommand)8 ClosedChannelException (java.nio.channels.ClosedChannelException)8 AgentControlAnswer (com.cloud.agent.api.AgentControlAnswer)6 MaintainAnswer (com.cloud.agent.api.MaintainAnswer)6 MaintainCommand (com.cloud.agent.api.MaintainCommand)6 ReadyCommand (com.cloud.agent.api.ReadyCommand)6 Request (com.cloud.agent.transport.Request)3 CheckHealthCommand (com.cloud.agent.api.CheckHealthCommand)2 ModifySshKeysCommand (com.cloud.agent.api.ModifySshKeysCommand)2 PingRoutingCommand (com.cloud.agent.api.PingRoutingCommand)2 StartupProxyCommand (com.cloud.agent.api.StartupProxyCommand)2 StartupRoutingCommand (com.cloud.agent.api.StartupRoutingCommand)2