Search in sources :

Example 16 with Request

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

the class ClusteredAgentManagerImpl method finishRebalance.

protected void finishRebalance(final long hostId, final long futureOwnerId, final Event event) {
    final boolean success = event == Event.RebalanceCompleted ? true : false;
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Finishing rebalancing for the agent " + hostId + " with event " + event);
    }
    final AgentAttache attache = findAttache(hostId);
    if (attache == null || !(attache instanceof ClusteredAgentAttache)) {
        s_logger.debug("Unable to find forward attache for the host id=" + hostId + ", assuming that the agent disconnected already");
        _hostTransferDao.completeAgentTransfer(hostId);
        return;
    }
    final ClusteredAgentAttache forwardAttache = (ClusteredAgentAttache) attache;
    if (success) {
        // 1) Set transfer mode to false - so the agent can start processing requests normally
        forwardAttache.setTransferMode(false);
        // 2) Get all transfer requests and route them to peer
        Request requestToTransfer = forwardAttache.getRequestToTransfer();
        while (requestToTransfer != null) {
            s_logger.debug("Forwarding request " + requestToTransfer.getSequence() + " held in transfer attache " + hostId + " from the management server " + _nodeId + " to " + futureOwnerId);
            final boolean routeResult = routeToPeer(Long.toString(futureOwnerId), requestToTransfer.getBytes());
            if (!routeResult) {
                logD(requestToTransfer.getBytes(), "Failed to route request to peer");
            }
            requestToTransfer = forwardAttache.getRequestToTransfer();
        }
        s_logger.debug("Management server " + _nodeId + " completed agent " + hostId + " rebalance to " + futureOwnerId);
    } else {
        failRebalance(hostId);
    }
    s_logger.debug("Management server " + _nodeId + " completed agent " + hostId + " rebalance");
    _hostTransferDao.completeAgentTransfer(hostId);
}
Also used : Request(com.cloud.agent.transport.Request)

Example 17 with Request

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

the class ClusteredAgentManagerImpl method cancel.

public void cancel(final String peerName, final long hostId, final long sequence, final String reason) {
    final CancelCommand cancel = new CancelCommand(sequence, reason);
    final Request req = new Request(hostId, _nodeId, cancel, true);
    req.setControl(true);
    routeToPeer(peerName, req.getBytes());
}
Also used : CancelCommand(com.cloud.agent.api.CancelCommand) Request(com.cloud.agent.transport.Request)

Example 18 with Request

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

the class ResourceManagerImpl method createHostAndAgentDeferred.

private Host createHostAndAgentDeferred(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 deferAgentCreation = true;
    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
                hostExists = true;
                // again
                return null;
            }
        }
        host = null;
        boolean newHost = false;
        final GlobalLock addHostLock = GlobalLock.getInternLock("AddHostLock");
        try {
            if (addHostLock.lock(ACQUIRE_GLOBAL_LOCK_TIMEOUT_FOR_COOPERATION)) {
                // to safely determine first host in cluster in multi-MS scenario
                try {
                    // find out if the host we want to connect to is new (so we can send an event)
                    newHost = getNewHost(cmds) == null;
                    host = createHostVO(cmds, resource, details, hostTags, ResourceStateAdapter.Event.CREATE_HOST_VO_FOR_DIRECT_CONNECT);
                    if (host != null) {
                        // if first host in cluster no need to defer agent creation
                        deferAgentCreation = !isFirstHostInCluster(host);
                    }
                } finally {
                    addHostLock.unlock();
                }
            }
        } finally {
            addHostLock.releaseRef();
        }
        if (host != null) {
            if (!deferAgentCreation) {
                // if first host in cluster then
                created = _agentMgr.handleDirectConnectAgent(host, cmds, resource, forRebalance, newHost);
                // reload
                host = _hostDao.findById(host.getId());
            } else {
                // reload
                host = _hostDao.findById(host.getId());
                // force host status to 'Alert' so that it is loaded for
                // connection during next scan task
                _agentMgr.agentStatusTransitTo(host, Status.Event.AgentDisconnected, _nodeId);
                // reload
                host = _hostDao.findById(host.getId());
                // so that scan task can pick it up
                host.setLastPinged(0);
                _hostDao.update(host.getId(), host);
            }
        }
    } catch (final Exception e) {
        s_logger.warn("Unable to connect due to ", e);
    } finally {
        if (hostExists) {
            if (cmds != null) {
                resource.disconnected();
            }
        } else {
            if (!deferAgentCreation && !created) {
                if (cmds != null) {
                    resource.disconnected();
                }
                markHostAsDisconnected(host, cmds);
            }
        }
    }
    return host;
}
Also used : StartupCommand(com.cloud.agent.api.StartupCommand) GlobalLock(com.cloud.utils.db.GlobalLock) 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)

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