Search in sources :

Example 6 with ManagedContextRunnable

use of org.apache.cloudstack.managed.context.ManagedContextRunnable in project cloudstack by apache.

the class ClusterManagerImpl method getNotificationTask.

private Runnable getNotificationTask() {
    return new ManagedContextRunnable() {

        @Override
        protected void runInContext() {
            while (true) {
                synchronized (_notificationMsgs) {
                    try {
                        _notificationMsgs.wait(1000);
                    } catch (final InterruptedException e) {
                    }
                }
                ClusterManagerMessage msg = null;
                while ((msg = getNextNotificationMessage()) != null) {
                    try {
                        switch(msg.getMessageType()) {
                            case nodeAdded:
                                if (msg.getNodes() != null && msg.getNodes().size() > 0) {
                                    final Profiler profiler = new Profiler();
                                    profiler.start();
                                    notifyNodeJoined(msg.getNodes());
                                    profiler.stop();
                                    if (profiler.getDurationInMillis() > 1000) {
                                        if (s_logger.isDebugEnabled()) {
                                            s_logger.debug("Notifying management server join event took " + profiler.getDurationInMillis() + " ms");
                                        }
                                    } else {
                                        s_logger.warn("Notifying management server join event took " + profiler.getDurationInMillis() + " ms");
                                    }
                                }
                                break;
                            case nodeRemoved:
                                if (msg.getNodes() != null && msg.getNodes().size() > 0) {
                                    final Profiler profiler = new Profiler();
                                    profiler.start();
                                    notifyNodeLeft(msg.getNodes());
                                    profiler.stop();
                                    if (profiler.getDurationInMillis() > 1000) {
                                        if (s_logger.isDebugEnabled()) {
                                            s_logger.debug("Notifying management server leave event took " + profiler.getDurationInMillis() + " ms");
                                        }
                                    } else {
                                        s_logger.warn("Notifying management server leave event took " + profiler.getDurationInMillis() + " ms");
                                    }
                                }
                                break;
                            case nodeIsolated:
                                notifyNodeIsolated();
                                break;
                            default:
                                assert false;
                                break;
                        }
                    } catch (final Throwable e) {
                        s_logger.warn("Unexpected exception during cluster notification. ", e);
                    }
                }
                try {
                    Thread.sleep(1000);
                } catch (final InterruptedException e) {
                }
            }
        }
    };
}
Also used : ManagedContextRunnable(org.apache.cloudstack.managed.context.ManagedContextRunnable) Profiler(com.cloud.utils.Profiler)

Example 7 with ManagedContextRunnable

use of org.apache.cloudstack.managed.context.ManagedContextRunnable in project cloudstack by apache.

the class ClientTransportProvider method initialize.

public void initialize(String serverAddress, int serverPort) {
    _serverAddress = serverAddress;
    _serverPort = serverPort;
    _executor = Executors.newFixedThreadPool(_poolSize, new NamedThreadFactory("Transport-Worker"));
    _connection = new ClientTransportConnection(this);
    _executor.execute(new ManagedContextRunnable() {

        @Override
        protected void runInContext() {
            try {
                _connection.connect(_serverAddress, _serverPort);
            } catch (Throwable e) {
                s_logger.info("[ignored]" + "error during ipc client initialization: " + e.getLocalizedMessage());
            }
        }
    });
}
Also used : ManagedContextRunnable(org.apache.cloudstack.managed.context.ManagedContextRunnable) NamedThreadFactory(com.cloud.utils.concurrency.NamedThreadFactory)

Example 8 with ManagedContextRunnable

use of org.apache.cloudstack.managed.context.ManagedContextRunnable in project cloudstack by apache.

the class ClusteredAgentManagerImpl method getTransferScanTask.

private Runnable getTransferScanTask() {
    return new ManagedContextRunnable() {

        @Override
        protected void runInContext() {
            try {
                if (s_logger.isTraceEnabled()) {
                    s_logger.trace("Clustered agent transfer scan check, management server id:" + _nodeId);
                }
                synchronized (_agentToTransferIds) {
                    if (_agentToTransferIds.size() > 0) {
                        s_logger.debug("Found " + _agentToTransferIds.size() + " agents to transfer");
                        // for (Long hostId : _agentToTransferIds) {
                        for (final Iterator<Long> iterator = _agentToTransferIds.iterator(); iterator.hasNext(); ) {
                            final Long hostId = iterator.next();
                            final AgentAttache attache = findAttache(hostId);
                            // if the thread:
                            // 1) timed out waiting for the host to reconnect
                            // 2) recipient management server is not active any more
                            // 3) if the management server doesn't own the host any more
                            // remove the host from re-balance list and delete from op_host_transfer DB
                            // no need to do anything with the real attache as we haven't modified it yet
                            final Date cutTime = DateUtil.currentGMTTime();
                            final HostTransferMapVO transferMap = _hostTransferDao.findActiveHostTransferMapByHostId(hostId, new Date(cutTime.getTime() - rebalanceTimeOut));
                            if (transferMap == null) {
                                s_logger.debug("Timed out waiting for the host id=" + hostId + " to be ready to transfer, skipping rebalance for the host");
                                iterator.remove();
                                _hostTransferDao.completeAgentTransfer(hostId);
                                continue;
                            }
                            if (transferMap.getInitialOwner() != _nodeId || attache == null || attache.forForward()) {
                                s_logger.debug("Management server " + _nodeId + " doesn't own host id=" + hostId + " any more, skipping rebalance for the host");
                                iterator.remove();
                                _hostTransferDao.completeAgentTransfer(hostId);
                                continue;
                            }
                            final ManagementServerHostVO ms = _mshostDao.findByMsid(transferMap.getFutureOwner());
                            if (ms != null && ms.getState() != ManagementServerHost.State.Up) {
                                s_logger.debug("Can't transfer host " + hostId + " as it's future owner is not in UP state: " + ms + ", skipping rebalance for the host");
                                iterator.remove();
                                _hostTransferDao.completeAgentTransfer(hostId);
                                continue;
                            }
                            if (attache.getQueueSize() == 0 && attache.getNonRecurringListenersSize() == 0) {
                                iterator.remove();
                                try {
                                    _executor.execute(new RebalanceTask(hostId, transferMap.getInitialOwner(), transferMap.getFutureOwner()));
                                } catch (final RejectedExecutionException ex) {
                                    s_logger.warn("Failed to submit rebalance task for host id=" + hostId + "; postponing the execution");
                                    continue;
                                }
                            } else {
                                s_logger.debug("Agent " + hostId + " can't be transfered yet as its request queue size is " + attache.getQueueSize() + " and listener queue size is " + attache.getNonRecurringListenersSize());
                            }
                        }
                    } else {
                        if (s_logger.isTraceEnabled()) {
                            s_logger.trace("Found no agents to be transfered by the management server " + _nodeId);
                        }
                    }
                }
            } catch (final Throwable e) {
                s_logger.error("Problem with the clustered agent transfer scan check!", e);
            }
        }
    };
}
Also used : ManagedContextRunnable(org.apache.cloudstack.managed.context.ManagedContextRunnable) ManagementServerHostVO(com.cloud.cluster.ManagementServerHostVO) HostTransferMapVO(com.cloud.cluster.agentlb.HostTransferMapVO) Date(java.util.Date) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 9 with ManagedContextRunnable

use of org.apache.cloudstack.managed.context.ManagedContextRunnable in project cloudstack by apache.

the class AgentResourceBase method executeRequest.

@Override
public final Answer executeRequest(final Command cmd) {
    final AtomicReference<Answer> result = new AtomicReference<Answer>();
    new ManagedContextRunnable() {

        @Override
        protected void runInContext() {
            result.set(executeRequestInContext(cmd));
        }
    }.run();
    return result.get();
}
Also used : Answer(com.cloud.agent.api.Answer) ManagedContextRunnable(org.apache.cloudstack.managed.context.ManagedContextRunnable) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 10 with ManagedContextRunnable

use of org.apache.cloudstack.managed.context.ManagedContextRunnable in project cloudstack by apache.

the class SystemVmLoadScanner method getCapacityScanTask.

private Runnable getCapacityScanTask() {
    return new ManagedContextRunnable() {

        @Override
        protected void runInContext() {
            try {
                CallContext callContext = CallContext.current();
                assert (callContext != null);
                AsyncJobExecutionContext.registerPseudoExecutionContext(callContext.getCallingAccountId(), callContext.getCallingUserId());
                reallyRun();
                AsyncJobExecutionContext.unregister();
            } catch (Throwable e) {
                s_logger.warn("Unexpected exception " + e.getMessage(), e);
            }
        }

        private void reallyRun() {
            loadScan();
        }
    };
}
Also used : ManagedContextRunnable(org.apache.cloudstack.managed.context.ManagedContextRunnable) CallContext(org.apache.cloudstack.context.CallContext)

Aggregations

ManagedContextRunnable (org.apache.cloudstack.managed.context.ManagedContextRunnable)12 Profiler (com.cloud.utils.Profiler)2 GlobalLock (com.cloud.utils.db.GlobalLock)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 Date (java.util.Date)2 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 Answer (com.cloud.agent.api.Answer)1 ManagementServerHostVO (com.cloud.cluster.ManagementServerHostVO)1 HostTransferMapVO (com.cloud.cluster.agentlb.HostTransferMapVO)1 NamedThreadFactory (com.cloud.utils.concurrency.NamedThreadFactory)1 DB (com.cloud.utils.db.DB)1 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ConfigurationException (javax.naming.ConfigurationException)1 CallContext (org.apache.cloudstack.context.CallContext)1 AsyncJob (org.apache.cloudstack.framework.jobs.AsyncJob)1 AsyncJobDispatcher (org.apache.cloudstack.framework.jobs.AsyncJobDispatcher)1 AsyncJobExecutionContext (org.apache.cloudstack.framework.jobs.AsyncJobExecutionContext)1