Search in sources :

Example 1 with Request

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

the class Agent method sendRequest.

@Override
public AgentControlAnswer sendRequest(AgentControlCommand cmd, int timeoutInMilliseconds) throws AgentControlChannelException {
    Request request = new Request(this.getId(), -1, new Command[] { cmd }, true, false);
    request.setSequence(getNextSequence());
    AgentControlListener listener = new AgentControlListener(request);
    registerControlListener(listener);
    try {
        postRequest(request);
        synchronized (listener) {
            try {
                listener.wait(timeoutInMilliseconds);
            } catch (InterruptedException e) {
                s_logger.warn("sendRequest is interrupted, exit waiting");
            }
        }
        return listener.getAnswer();
    } finally {
        unregisterControlListener(listener);
    }
}
Also used : Request(com.cloud.agent.transport.Request)

Example 2 with Request

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

the class Agent method postRequest.

@Override
public void postRequest(AgentControlCommand cmd) throws AgentControlChannelException {
    Request request = new Request(this.getId(), -1, new Command[] { cmd }, true, false);
    request.setSequence(getNextSequence());
    postRequest(request);
}
Also used : Request(com.cloud.agent.transport.Request)

Example 3 with Request

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

the class ResourceManagerImpl method createHostAndAgent.

private Host createHostAndAgent(final ServerResource resource, final Map<String, String> details, final boolean old, final List<String> hostTags, final boolean forRebalance) {
    HostVO host = null;
    StartupCommand[] cmds = null;
    boolean hostExists = false;
    boolean created = false;
    try {
        cmds = resource.initialize();
        if (cmds == null) {
            s_logger.info("Unable to fully initialize the agent because no StartupCommands are returned");
            return null;
        }
        /* Generate a random version in a dev setup situation */
        if (this.getClass().getPackage().getImplementationVersion() == null) {
            for (final StartupCommand cmd : cmds) {
                if (cmd.getVersion() == null) {
                    cmd.setVersion(Long.toString(System.currentTimeMillis()));
                }
            }
        }
        if (s_logger.isDebugEnabled()) {
            new Request(-1l, -1l, cmds, true, false).logD("Startup request from directly connected host: ", true);
        }
        if (old) {
            final StartupCommand firstCmd = cmds[0];
            host = findHostByGuid(firstCmd.getGuid());
            if (host == null) {
                host = findHostByGuid(firstCmd.getGuidWithoutResource());
            }
            if (host != null && host.getRemoved() == null) {
                // host already added, no need to add again
                s_logger.debug("Found the host " + host.getId() + " by guid: " + firstCmd.getGuid() + ", old host reconnected as new");
                // ensures that host status is left unchanged in case of adding same one again
                hostExists = true;
                return null;
            }
        }
        // find out if the host we want to connect to is new (so we can send an event)
        boolean newHost = getNewHost(cmds) == null;
        host = createHostVO(cmds, resource, details, hostTags, ResourceStateAdapter.Event.CREATE_HOST_VO_FOR_DIRECT_CONNECT);
        if (host != null) {
            created = _agentMgr.handleDirectConnectAgent(host, cmds, resource, forRebalance, newHost);
            /* reload myself from database */
            host = _hostDao.findById(host.getId());
        }
    } catch (final Exception e) {
        s_logger.warn("Unable to connect due to ", e);
    } finally {
        if (hostExists) {
            if (cmds != null) {
                resource.disconnected();
            }
        } else {
            if (!created) {
                if (cmds != null) {
                    resource.disconnected();
                }
                markHostAsDisconnected(host, cmds);
            }
        }
    }
    return host;
}
Also used : StartupCommand(com.cloud.agent.api.StartupCommand) Request(com.cloud.agent.transport.Request) StoragePoolHostVO(com.cloud.storage.StoragePoolHostVO) HostVO(com.cloud.host.HostVO) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResourceInUseException(com.cloud.exception.ResourceInUseException) URISyntaxException(java.net.URISyntaxException) DiscoveryException(com.cloud.exception.DiscoveryException) SshException(com.cloud.utils.ssh.SshException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException)

Example 4 with Request

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

the class Agent method stop.

public void stop(final String reason, final String detail) {
    s_logger.info("Stopping the agent: Reason = " + reason + (detail != null ? ": Detail = " + detail : ""));
    if (_connection != null) {
        final ShutdownCommand cmd = new ShutdownCommand(reason, detail);
        try {
            if (_link != null) {
                final Request req = new Request(_id != null ? _id : -1, -1, cmd, false);
                _link.send(req.toBytes());
            }
        } catch (final ClosedChannelException e) {
            s_logger.warn("Unable to send: " + cmd.toString());
        } catch (final Exception e) {
            s_logger.warn("Unable to send: " + cmd.toString() + " due to exception: ", e);
        }
        s_logger.debug("Sending shutdown to management server");
        try {
            Thread.sleep(1000);
        } catch (final InterruptedException e) {
            s_logger.debug("Who the heck interrupted me here?");
        }
        _connection.stop();
        _connection = null;
    }
    if (_resource != null) {
        _resource.stop();
        _resource = null;
    }
    _ugentTaskPool.shutdownNow();
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) Request(com.cloud.agent.transport.Request) ShutdownCommand(com.cloud.agent.api.ShutdownCommand) TaskExecutionException(com.cloud.utils.exception.TaskExecutionException) AgentControlChannelException(com.cloud.exception.AgentControlChannelException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) NioConnectionException(com.cloud.utils.exception.NioConnectionException)

Example 5 with Request

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

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 = _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.agent.api.ReadyCommand) Request(com.cloud.agent.transport.Request) HostVO(com.cloud.host.HostVO) ConnectionException(com.cloud.exception.ConnectionException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) TaskExecutionException(com.cloud.utils.exception.TaskExecutionException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ClosedChannelException(java.nio.channels.ClosedChannelException) HypervisorVersionChangedException(com.cloud.utils.exception.HypervisorVersionChangedException) NioConnectionException(com.cloud.utils.exception.NioConnectionException) UnsupportedVersionException(com.cloud.exception.UnsupportedVersionException)

Aggregations

Request (com.cloud.agent.transport.Request)18 ShutdownCommand (com.cloud.agent.api.ShutdownCommand)8 StartupCommand (com.cloud.agent.api.StartupCommand)8 ClosedChannelException (java.nio.channels.ClosedChannelException)7 AgentControlCommand (com.cloud.agent.api.AgentControlCommand)6 Command (com.cloud.agent.api.Command)6 PingCommand (com.cloud.agent.api.PingCommand)6 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)6 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)6 ReadyCommand (com.cloud.agent.api.ReadyCommand)5 ConfigurationException (javax.naming.ConfigurationException)5 CronCommand (com.cloud.agent.api.CronCommand)4 MaintainCommand (com.cloud.agent.api.MaintainCommand)4 AgentControlAnswer (com.cloud.agent.api.AgentControlAnswer)3 Answer (com.cloud.agent.api.Answer)3 StartupAnswer (com.cloud.agent.api.StartupAnswer)3 HostVO (com.cloud.host.HostVO)3 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)3 CheckHealthCommand (com.cloud.agent.api.CheckHealthCommand)2 MaintainAnswer (com.cloud.agent.api.MaintainAnswer)2