Search in sources :

Example 6 with ManagedContextRunnable

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

the class AsyncJobManagerImpl method getGCTask.

@DB
private Runnable getGCTask() {
    return new ManagedContextRunnable() {

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

        public void reallyRun() {
            try {
                s_logger.info("Begin cleanup expired async-jobs");
                // forcefully cancel blocking queue items if they've been staying there for too long
                final List<SyncQueueItemVO> blockItems = _queueMgr.getBlockedQueueItems(JobCancelThresholdMinutes.value() * 60000, false);
                if (blockItems != null && blockItems.size() > 0) {
                    for (final SyncQueueItemVO item : blockItems) {
                        try {
                            if (item.getContentType().equalsIgnoreCase(SyncQueueItem.AsyncJobContentType)) {
                                s_logger.info("Remove Job-" + item.getContentId() + " from Queue-" + item.getId() + " since it has been blocked for too long");
                                completeAsyncJob(item.getContentId(), JobInfo.Status.FAILED, 0, "Job is cancelled as it has been blocking others for too long");
                                _jobMonitor.unregisterByJobId(item.getContentId());
                            }
                            // purge the item and resume queue processing
                            _queueMgr.purgeItem(item.getId());
                        } catch (final Throwable e) {
                            s_logger.error("Unexpected exception when trying to remove job from sync queue, ", e);
                        }
                    }
                }
                final Date cutTime = new Date(DateUtil.currentGMTTime().getTime() - JobExpireMinutes.value() * 60000);
                // limit to 100 jobs per turn, this gives cleanup throughput as 600 jobs per minute
                // hopefully this will be fast enough to balance potential growth of job table
                // 1) Expire unfinished jobs that weren't processed yet
                final List<AsyncJobVO> unfinishedJobs = _jobDao.getExpiredUnfinishedJobs(cutTime, 100);
                for (final AsyncJobVO job : unfinishedJobs) {
                    try {
                        s_logger.info("Expunging unfinished job-" + job.getId());
                        _jobMonitor.unregisterByJobId(job.getId());
                        expungeAsyncJob(job);
                    } catch (final Throwable e) {
                        s_logger.error("Unexpected exception when trying to expunge job-" + job.getId(), e);
                    }
                }
                // 2) Expunge finished jobs
                final List<AsyncJobVO> completedJobs = _jobDao.getExpiredCompletedJobs(cutTime, 100);
                for (final AsyncJobVO job : completedJobs) {
                    try {
                        s_logger.info("Expunging completed job-" + job.getId());
                        expungeAsyncJob(job);
                    } catch (final Throwable e) {
                        s_logger.error("Unexpected exception when trying to expunge job-" + job.getId(), e);
                    }
                }
                s_logger.info("End cleanup expired async-jobs");
            } 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.managed.context.ManagedContextRunnable) Date(java.util.Date) DB(com.cloud.utils.db.DB)

Example 7 with ManagedContextRunnable

use of com.cloud.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.managed.context.ManagedContextRunnable) Profiler(com.cloud.utils.Profiler) SQLException(java.sql.SQLException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 8 with ManagedContextRunnable

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

the class SystemVmLoadScanner method getCapacityScanTask.

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

        @Override
        protected void runInContext() {
            final CallContext callContext = CallContext.current();
            AsyncJobExecutionContext.registerPseudoExecutionContext(callContext.getCallingAccountId(), callContext.getCallingUserId());
            reallyRun();
            AsyncJobExecutionContext.unregister();
        }

        private void reallyRun() {
            loadScan();
        }
    };
}
Also used : ManagedContextRunnable(com.cloud.managed.context.ManagedContextRunnable) CallContext(com.cloud.context.CallContext)

Example 9 with ManagedContextRunnable

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

the class TemplateManagerImpl method prepareTemplateInOneStoragePool.

private void prepareTemplateInOneStoragePool(final VMTemplateVO template, final StoragePoolVO pool) {
    s_logger.info("Schedule to preload template " + template.getId() + " into primary storage " + pool.getId());
    _preloadExecutor.execute(new ManagedContextRunnable() {

        @Override
        protected void runInContext() {
            reallyRun();
        }

        private void reallyRun() {
            s_logger.info("Start to preload template " + template.getId() + " into primary storage " + pool.getId());
            final StoragePool pol = (StoragePool) _dataStoreMgr.getPrimaryDataStore(pool.getId());
            prepareTemplateForCreate(template, pol);
            s_logger.info("End of preloading template " + template.getId() + " into primary storage " + pool.getId());
        }
    });
}
Also used : ManagedContextRunnable(com.cloud.managed.context.ManagedContextRunnable) StoragePool(com.cloud.storage.StoragePool)

Example 10 with ManagedContextRunnable

use of com.cloud.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 Object resource = this;
    if (_consoleProxyMain == null) {
        _consoleProxyMain = new Thread(new ManagedContextRunnable() {

            @Override
            protected void runInContext() {
                try {
                    final Class<?> consoleProxyClazz = Class.forName("com.cloud.consoleproxy.ConsoleProxy");
                    try {
                        s_logger.info("Invoke setEncryptorPassword(), ecnryptorPassword: " + 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);
                        method.invoke(null, _properties, resource, ksBits, ksPassword);
                    } 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");
        _consoleProxyMain.setDaemon(true);
        _consoleProxyMain.start();
    } else {
        s_logger.info("com.cloud.consoleproxy.ConsoleProxy is already running");
        try {
            final Class<?> consoleProxyClazz = Class.forName("com.cloud.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.managed.context.ManagedContextRunnable) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

ManagedContextRunnable (com.cloud.managed.context.ManagedContextRunnable)10 Profiler (com.cloud.utils.Profiler)2 GlobalLock (com.cloud.utils.db.GlobalLock)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)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 StoragePool (com.cloud.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 ConfigurationException (javax.naming.ConfigurationException)1