Search in sources :

Example 1 with AgentLoadBalancerPlanner

use of com.cloud.cluster.agentlb.AgentLoadBalancerPlanner in project cosmic by MissionCriticalCloud.

the class ClusteredAgentManagerImpl method startRebalanceAgents.

public void startRebalanceAgents() {
    s_logger.debug("Management server " + _nodeId + " is asking other peers to rebalance their agents");
    final List<ManagementServerHostVO> allMS = _mshostDao.listBy(ManagementServerHost.State.Up);
    final QueryBuilder<HostVO> sc = QueryBuilder.create(HostVO.class);
    sc.and(sc.entity().getManagementServerId(), Op.NNULL);
    sc.and(sc.entity().getType(), Op.EQ, Host.Type.Routing);
    final List<HostVO> allManagedAgents = sc.list();
    int avLoad = 0;
    if (!allManagedAgents.isEmpty() && !allMS.isEmpty()) {
        avLoad = allManagedAgents.size() / allMS.size();
    } else {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("There are no hosts to rebalance in the system. Current number of active management server nodes in the system is " + allMS.size() + "; number of managed agents is " + allManagedAgents.size());
        }
        return;
    }
    if (avLoad == 0L) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("As calculated average load is less than 1, rounding it to 1");
        }
        avLoad = 1;
    }
    for (final ManagementServerHostVO node : allMS) {
        if (node.getMsid() != _nodeId) {
            List<HostVO> hostsToRebalance = new ArrayList<>();
            for (final AgentLoadBalancerPlanner lbPlanner : _lbPlanners) {
                hostsToRebalance = lbPlanner.getHostsToRebalance(node.getMsid(), avLoad);
                if (hostsToRebalance != null && !hostsToRebalance.isEmpty()) {
                    break;
                } else {
                    s_logger.debug("Agent load balancer planner " + lbPlanner.getName() + " found no hosts to be rebalanced from management server " + node.getMsid());
                }
            }
            if (hostsToRebalance != null && !hostsToRebalance.isEmpty()) {
                s_logger.debug("Found " + hostsToRebalance.size() + " hosts to rebalance from management server " + node.getMsid());
                for (final HostVO host : hostsToRebalance) {
                    final long hostId = host.getId();
                    s_logger.debug("Asking management server " + node.getMsid() + " to give away host id=" + hostId);
                    boolean result = true;
                    if (_hostTransferDao.findById(hostId) != null) {
                        s_logger.warn("Somebody else is already rebalancing host id: " + hostId);
                        continue;
                    }
                    HostTransferMapVO transfer = null;
                    try {
                        transfer = _hostTransferDao.startAgentTransfering(hostId, node.getMsid(), _nodeId);
                        final Answer[] answer = sendRebalanceCommand(node.getMsid(), hostId, node.getMsid(), _nodeId, Event.RequestAgentRebalance);
                        if (answer == null) {
                            s_logger.warn("Failed to get host id=" + hostId + " from management server " + node.getMsid());
                            result = false;
                        }
                    } catch (final Exception ex) {
                        s_logger.warn("Failed to get host id=" + hostId + " from management server " + node.getMsid(), ex);
                        result = false;
                    } finally {
                        if (transfer != null) {
                            final HostTransferMapVO transferState = _hostTransferDao.findByIdAndFutureOwnerId(transfer.getId(), _nodeId);
                            if (!result && transferState != null && transferState.getState() == HostTransferState.TransferRequested) {
                                if (s_logger.isDebugEnabled()) {
                                    s_logger.debug("Removing mapping from op_host_transfer as it failed to be set to transfer mode");
                                }
                                // just remove the mapping (if exists) as nothing was done on the peer management
                                // server yet
                                _hostTransferDao.remove(transfer.getId());
                            }
                        }
                    }
                }
            } else {
                s_logger.debug("Found no hosts to rebalance from the management server " + node.getMsid());
            }
        }
    }
}
Also used : HostTransferMapVO(com.cloud.cluster.agentlb.HostTransferMapVO) ArrayList(java.util.ArrayList) HostVO(com.cloud.host.HostVO) ManagementServerHostVO(com.cloud.cluster.ManagementServerHostVO) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) TaskExecutionException(com.cloud.utils.exception.TaskExecutionException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) JsonSyntaxException(com.google.gson.JsonSyntaxException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) UnsupportedVersionException(com.cloud.exception.UnsupportedVersionException) ChangeAgentAnswer(com.cloud.agent.api.ChangeAgentAnswer) Answer(com.cloud.agent.api.Answer) ManagementServerHostVO(com.cloud.cluster.ManagementServerHostVO) AgentLoadBalancerPlanner(com.cloud.cluster.agentlb.AgentLoadBalancerPlanner)

Example 2 with AgentLoadBalancerPlanner

use of com.cloud.cluster.agentlb.AgentLoadBalancerPlanner in project cloudstack by apache.

the class ClusteredAgentManagerImpl method startRebalanceAgents.

public void startRebalanceAgents() {
    s_logger.debug("Management server " + _nodeId + " is asking other peers to rebalance their agents");
    final List<ManagementServerHostVO> allMS = _mshostDao.listBy(ManagementServerHost.State.Up);
    final QueryBuilder<HostVO> sc = QueryBuilder.create(HostVO.class);
    sc.and(sc.entity().getManagementServerId(), Op.NNULL);
    sc.and(sc.entity().getType(), Op.EQ, Host.Type.Routing);
    final List<HostVO> allManagedAgents = sc.list();
    int avLoad = 0;
    if (!allManagedAgents.isEmpty() && !allMS.isEmpty()) {
        avLoad = allManagedAgents.size() / allMS.size();
    } else {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("There are no hosts to rebalance in the system. Current number of active management server nodes in the system is " + allMS.size() + "; number of managed agents is " + allManagedAgents.size());
        }
        return;
    }
    if (avLoad == 0L) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("As calculated average load is less than 1, rounding it to 1");
        }
        avLoad = 1;
    }
    for (final ManagementServerHostVO node : allMS) {
        if (node.getMsid() != _nodeId) {
            List<HostVO> hostsToRebalance = new ArrayList<HostVO>();
            for (final AgentLoadBalancerPlanner lbPlanner : _lbPlanners) {
                hostsToRebalance = lbPlanner.getHostsToRebalance(node.getMsid(), avLoad);
                if (hostsToRebalance != null && !hostsToRebalance.isEmpty()) {
                    break;
                } else {
                    s_logger.debug("Agent load balancer planner " + lbPlanner.getName() + " found no hosts to be rebalanced from management server " + node.getMsid());
                }
            }
            if (hostsToRebalance != null && !hostsToRebalance.isEmpty()) {
                s_logger.debug("Found " + hostsToRebalance.size() + " hosts to rebalance from management server " + node.getMsid());
                for (final HostVO host : hostsToRebalance) {
                    final long hostId = host.getId();
                    s_logger.debug("Asking management server " + node.getMsid() + " to give away host id=" + hostId);
                    boolean result = true;
                    if (_hostTransferDao.findById(hostId) != null) {
                        s_logger.warn("Somebody else is already rebalancing host id: " + hostId);
                        continue;
                    }
                    HostTransferMapVO transfer = null;
                    try {
                        transfer = _hostTransferDao.startAgentTransfering(hostId, node.getMsid(), _nodeId);
                        final Answer[] answer = sendRebalanceCommand(node.getMsid(), hostId, node.getMsid(), _nodeId, Event.RequestAgentRebalance);
                        if (answer == null) {
                            s_logger.warn("Failed to get host id=" + hostId + " from management server " + node.getMsid());
                            result = false;
                        }
                    } catch (final Exception ex) {
                        s_logger.warn("Failed to get host id=" + hostId + " from management server " + node.getMsid(), ex);
                        result = false;
                    } finally {
                        if (transfer != null) {
                            final HostTransferMapVO transferState = _hostTransferDao.findByIdAndFutureOwnerId(transfer.getId(), _nodeId);
                            if (!result && transferState != null && transferState.getState() == HostTransferState.TransferRequested) {
                                if (s_logger.isDebugEnabled()) {
                                    s_logger.debug("Removing mapping from op_host_transfer as it failed to be set to transfer mode");
                                }
                                // just remove the mapping (if exists) as nothing was done on the peer management
                                // server yet
                                _hostTransferDao.remove(transfer.getId());
                            }
                        }
                    }
                }
            } else {
                s_logger.debug("Found no hosts to rebalance from the management server " + node.getMsid());
            }
        }
    }
}
Also used : HostTransferMapVO(com.cloud.cluster.agentlb.HostTransferMapVO) ArrayList(java.util.ArrayList) HostVO(com.cloud.host.HostVO) ManagementServerHostVO(com.cloud.cluster.ManagementServerHostVO) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) TaskExecutionException(com.cloud.utils.exception.TaskExecutionException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) UnsupportedVersionException(com.cloud.exception.UnsupportedVersionException) ChangeAgentAnswer(com.cloud.agent.api.ChangeAgentAnswer) Answer(com.cloud.agent.api.Answer) ManagementServerHostVO(com.cloud.cluster.ManagementServerHostVO) AgentLoadBalancerPlanner(com.cloud.cluster.agentlb.AgentLoadBalancerPlanner)

Aggregations

Answer (com.cloud.agent.api.Answer)2 ChangeAgentAnswer (com.cloud.agent.api.ChangeAgentAnswer)2 ManagementServerHostVO (com.cloud.cluster.ManagementServerHostVO)2 AgentLoadBalancerPlanner (com.cloud.cluster.agentlb.AgentLoadBalancerPlanner)2 HostTransferMapVO (com.cloud.cluster.agentlb.HostTransferMapVO)2 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)2 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)2 UnsupportedVersionException (com.cloud.exception.UnsupportedVersionException)2 HostVO (com.cloud.host.HostVO)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 TaskExecutionException (com.cloud.utils.exception.TaskExecutionException)2 IOException (java.io.IOException)2 UnknownHostException (java.net.UnknownHostException)2 ArrayList (java.util.ArrayList)2 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 ConfigurationException (javax.naming.ConfigurationException)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)1