Search in sources :

Example 1 with ManagedContextRunnable

use of com.cloud.common.managed.context.ManagedContextRunnable in project cosmic by MissionCriticalCloud.

the class AsyncJobManagerImpl method getHeartbeatTask.

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

        @Override
        protected void runInContext() {
            final GlobalLock scanLock = GlobalLock.getInternLock("AsyncJobManagerHeartbeat");
            try {
                if (scanLock.lock(ACQUIRE_GLOBAL_LOCK_TIMEOUT_FOR_COOPERATION)) {
                    try {
                        reallyRun();
                    } finally {
                        scanLock.unlock();
                    }
                }
            } finally {
                scanLock.releaseRef();
            }
        }

        protected void reallyRun() {
            try {
                final List<SyncQueueItemVO> l = _queueMgr.dequeueFromAny(getMsid(), MAX_ONETIME_SCHEDULE_SIZE);
                if (l != null && l.size() > 0) {
                    for (final SyncQueueItemVO item : l) {
                        if (s_logger.isDebugEnabled()) {
                            s_logger.debug("Execute sync-queue item: " + item.toString());
                        }
                        executeQueueItem(item, false);
                    }
                }
                final List<Long> standaloneWakeupJobs = wakeupScan();
                for (final Long jobId : standaloneWakeupJobs) {
                    // TODO, we assume that all jobs in this category is API job only
                    final AsyncJobVO job = _jobDao.findById(jobId);
                    if (job != null && (job.getPendingSignals() & AsyncJob.Constants.SIGNAL_MASK_WAKEUP) != 0) {
                        scheduleExecution(job, false);
                    }
                }
            } catch (final Throwable e) {
                s_logger.error("Unexpected exception when trying to execute queue item, ", e);
            }
        }
    };
}
Also used : GlobalLock(com.cloud.utils.db.GlobalLock) ManagedContextRunnable(com.cloud.common.managed.context.ManagedContextRunnable)

Example 2 with ManagedContextRunnable

use of com.cloud.common.managed.context.ManagedContextRunnable in project cosmic by MissionCriticalCloud.

the class ClusterManagerImpl method onNotifyingClusterPdu.

private void onNotifyingClusterPdu() {
    while (true) {
        final ClusterServicePdu pdu = popIncomingClusterPdu(1000);
        if (pdu == null) {
            continue;
        }
        _executor.execute(new ManagedContextRunnable() {

            @Override
            protected void runInContext() {
                if (pdu.getPduType() == ClusterServicePdu.PDU_TYPE_RESPONSE) {
                    final ClusterServiceRequestPdu requestPdu = popRequestPdu(pdu.getAckSequenceId());
                    if (requestPdu != null) {
                        requestPdu.setResponseResult(pdu.getJsonPackage());
                        synchronized (requestPdu) {
                            requestPdu.notifyAll();
                        }
                    } else {
                        s_logger.warn("Original request has already been cancelled. pdu: " + pdu.getJsonPackage());
                    }
                } else {
                    String result = _dispatcher.dispatch(pdu);
                    if (result == null) {
                        result = "";
                    }
                    if (pdu.getPduType() == ClusterServicePdu.PDU_TYPE_REQUEST) {
                        final ClusterServicePdu responsePdu = new ClusterServicePdu();
                        responsePdu.setPduType(ClusterServicePdu.PDU_TYPE_RESPONSE);
                        responsePdu.setSourcePeer(pdu.getDestPeer());
                        responsePdu.setDestPeer(pdu.getSourcePeer());
                        responsePdu.setAckSequenceId(pdu.getSequenceId());
                        responsePdu.setJsonPackage(result);
                        addOutgoingClusterPdu(responsePdu);
                    }
                }
            }
        });
    }
}
Also used : ManagedContextRunnable(com.cloud.common.managed.context.ManagedContextRunnable)

Example 3 with ManagedContextRunnable

use of com.cloud.common.managed.context.ManagedContextRunnable in project cosmic by MissionCriticalCloud.

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) {
                    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;
                    }
                }
                try {
                    Thread.sleep(1000);
                } catch (final InterruptedException e) {
                    s_logger.warn("Caught (previously ignored) interrupted exception", e);
                }
            }
        }
    };
}
Also used : ManagedContextRunnable(com.cloud.common.managed.context.ManagedContextRunnable) Profiler(com.cloud.utils.Profiler)

Example 4 with ManagedContextRunnable

use of com.cloud.common.managed.context.ManagedContextRunnable in project cosmic by MissionCriticalCloud.

the class ConsoleProxyResource method launchConsoleProxy.

private void launchConsoleProxy(final byte[] ksBits, final String ksPassword, final String encryptorPassword, final String ksAuthenticationKey) {
    final Object resource = this;
    if (this._consoleProxyMain == null) {
        this._consoleProxyMain = new Thread(new ManagedContextRunnable() {

            @Override
            protected void runInContext() {
                try {
                    final Class<?> consoleProxyClazz = Class.forName("com.cloud.agent.resource.consoleproxy.ConsoleProxy");
                    try {
                        s_logger.info("Invoke setEncryptorPassword(), encryptorPassword: " + encryptorPassword);
                        final Method methodSetup = consoleProxyClazz.getMethod("setEncryptorPassword", String.class);
                        methodSetup.invoke(null, encryptorPassword);
                        s_logger.info("Invoke startWithContext()");
                        final Method method = consoleProxyClazz.getMethod("startWithContext", Properties.class, Object.class, byte[].class, String.class, String.class);
                        method.invoke(null, ConsoleProxyResource.this._properties, resource, ksBits, ksPassword, ksAuthenticationKey);
                    } catch (final SecurityException e) {
                        s_logger.error("Unable to launch console proxy due to SecurityException", e);
                        System.exit(ExitStatus.Error.value());
                    } catch (final NoSuchMethodException e) {
                        s_logger.error("Unable to launch console proxy due to NoSuchMethodException", e);
                        System.exit(ExitStatus.Error.value());
                    } catch (final IllegalArgumentException e) {
                        s_logger.error("Unable to launch console proxy due to IllegalArgumentException", e);
                        System.exit(ExitStatus.Error.value());
                    } catch (final IllegalAccessException e) {
                        s_logger.error("Unable to launch console proxy due to IllegalAccessException", e);
                        System.exit(ExitStatus.Error.value());
                    } catch (final InvocationTargetException e) {
                        s_logger.error("Unable to launch console proxy due to InvocationTargetException " + e.getTargetException().toString(), e);
                        System.exit(ExitStatus.Error.value());
                    }
                } catch (final ClassNotFoundException e) {
                    s_logger.error("Unable to launch console proxy due to ClassNotFoundException");
                    System.exit(ExitStatus.Error.value());
                }
            }
        }, "Console-Proxy-Main");
        this._consoleProxyMain.setDaemon(true);
        this._consoleProxyMain.start();
    } else {
        s_logger.info("com.cloud.consoleproxy.ConsoleProxy is already running");
        try {
            final Class<?> consoleProxyClazz = Class.forName("com.cloud.agent.resource.consoleproxy.ConsoleProxy");
            final Method methodSetup = consoleProxyClazz.getMethod("setEncryptorPassword", String.class);
            methodSetup.invoke(null, encryptorPassword);
        } catch (final SecurityException e) {
            s_logger.error("Unable to launch console proxy due to SecurityException", e);
            System.exit(ExitStatus.Error.value());
        } catch (final NoSuchMethodException e) {
            s_logger.error("Unable to launch console proxy due to NoSuchMethodException", e);
            System.exit(ExitStatus.Error.value());
        } catch (final IllegalArgumentException e) {
            s_logger.error("Unable to launch console proxy due to IllegalArgumentException", e);
            System.exit(ExitStatus.Error.value());
        } catch (final IllegalAccessException e) {
            s_logger.error("Unable to launch console proxy due to IllegalAccessException", e);
            System.exit(ExitStatus.Error.value());
        } catch (final InvocationTargetException e) {
            s_logger.error("Unable to launch console proxy due to InvocationTargetException " + e.getTargetException().toString(), e);
            System.exit(ExitStatus.Error.value());
        } catch (final ClassNotFoundException e) {
            s_logger.error("Unable to launch console proxy due to ClassNotFoundException", e);
            System.exit(ExitStatus.Error.value());
        }
    }
}
Also used : ManagedContextRunnable(com.cloud.common.managed.context.ManagedContextRunnable) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 5 with ManagedContextRunnable

use of com.cloud.common.managed.context.ManagedContextRunnable in project cosmic by MissionCriticalCloud.

the class ClusterManagerImpl method getHeartbeatTask.

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

        @Override
        protected void runInContext() {
            final TransactionLegacy txn = TransactionLegacy.open("ClusterHeartbeat");
            try {
                final Profiler profiler = new Profiler();
                final Profiler profilerHeartbeatUpdate = new Profiler();
                final Profiler profilerPeerScan = new Profiler();
                try {
                    profiler.start();
                    profilerHeartbeatUpdate.start();
                    txn.transitToUserManagedConnection(getHeartbeatConnection());
                    if (s_logger.isTraceEnabled()) {
                        s_logger.trace("Cluster manager heartbeat update, id:" + _mshostId);
                    }
                    _mshostDao.update(_mshostId, _runId, DateUtil.currentGMTTime());
                    profilerHeartbeatUpdate.stop();
                    profilerPeerScan.start();
                    if (s_logger.isTraceEnabled()) {
                        s_logger.trace("Cluster manager peer-scan, id:" + _mshostId);
                    }
                    if (!_peerScanInited) {
                        _peerScanInited = true;
                        initPeerScan();
                    }
                    peerScan();
                    profilerPeerScan.stop();
                } catch (final SQLException e) {
                    s_logger.error("Unexpected exception in cluster heartbeat", e);
                    if (isRootCauseConnectionRelated(e.getCause())) {
                        invalidHeartbeatConnection();
                    }
                } finally {
                    profiler.stop();
                    if (profiler.getDurationInMillis() >= HeartbeatInterval.value()) {
                        if (s_logger.isDebugEnabled()) {
                            s_logger.debug("Management server heartbeat takes too long to finish. profiler: " + profiler.toString() + ", profilerHeartbeatUpdate: " + profilerHeartbeatUpdate.toString() + ", profilerPeerScan: " + profilerPeerScan.toString());
                        }
                    }
                }
            } catch (final CloudRuntimeException e) {
                s_logger.error("Runtime DB exception ", e.getCause());
                if (e.getCause() instanceof ClusterInvalidSessionException) {
                    s_logger.error("Invalid cluster session found, fence it");
                    queueNotification(new ClusterManagerMessage(ClusterManagerMessage.MessageType.nodeIsolated));
                }
                if (isRootCauseConnectionRelated(e.getCause())) {
                    invalidHeartbeatConnection();
                }
            } catch (final ActiveFencingException e) {
                queueNotification(new ClusterManagerMessage(ClusterManagerMessage.MessageType.nodeIsolated));
            } finally {
                txn.transitToAutoManagedConnection(TransactionLegacy.CLOUD_DB);
                txn.close("ClusterHeartbeat");
            }
        }
    };
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) ManagedContextRunnable(com.cloud.common.managed.context.ManagedContextRunnable) Profiler(com.cloud.utils.Profiler) SQLException(java.sql.SQLException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) ActiveFencingException(com.cloud.legacymodel.exceptions.ActiveFencingException)

Aggregations

ManagedContextRunnable (com.cloud.common.managed.context.ManagedContextRunnable)10 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)2 Profiler (com.cloud.utils.Profiler)2 GlobalLock (com.cloud.utils.db.GlobalLock)2 Date (java.util.Date)2 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 ExceptionResponse (com.cloud.api.response.ExceptionResponse)1 ManagementServerHostVO (com.cloud.cluster.ManagementServerHostVO)1 HostTransferMapVO (com.cloud.cluster.agentlb.HostTransferMapVO)1 CallContext (com.cloud.context.CallContext)1 AsyncJob (com.cloud.framework.jobs.AsyncJob)1 AsyncJobDispatcher (com.cloud.framework.jobs.AsyncJobDispatcher)1 AsyncJobExecutionContext (com.cloud.framework.jobs.AsyncJobExecutionContext)1 ActiveFencingException (com.cloud.legacymodel.exceptions.ActiveFencingException)1 StoragePool (com.cloud.legacymodel.storage.StoragePool)1 DB (com.cloud.utils.db.DB)1 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 SQLException (java.sql.SQLException)1