Search in sources :

Example 1 with ManagedContextRunnable

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

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 {
                    Class<?> consoleProxyClazz = Class.forName("com.cloud.consoleproxy.ConsoleProxy");
                    try {
                        s_logger.info("Invoke setEncryptorPassword(), ecnryptorPassword: " + encryptorPassword);
                        Method methodSetup = consoleProxyClazz.getMethod("setEncryptorPassword", String.class);
                        methodSetup.invoke(null, encryptorPassword);
                        s_logger.info("Invoke startWithContext()");
                        Method method = consoleProxyClazz.getMethod("startWithContext", Properties.class, Object.class, byte[].class, String.class);
                        method.invoke(null, _properties, resource, ksBits, ksPassword);
                    } catch (SecurityException e) {
                        s_logger.error("Unable to launch console proxy due to SecurityException", e);
                        System.exit(ExitStatus.Error.value());
                    } catch (NoSuchMethodException e) {
                        s_logger.error("Unable to launch console proxy due to NoSuchMethodException", e);
                        System.exit(ExitStatus.Error.value());
                    } catch (IllegalArgumentException e) {
                        s_logger.error("Unable to launch console proxy due to IllegalArgumentException", e);
                        System.exit(ExitStatus.Error.value());
                    } catch (IllegalAccessException e) {
                        s_logger.error("Unable to launch console proxy due to IllegalAccessException", e);
                        System.exit(ExitStatus.Error.value());
                    } catch (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 {
            Class<?> consoleProxyClazz = Class.forName("com.cloud.consoleproxy.ConsoleProxy");
            Method methodSetup = consoleProxyClazz.getMethod("setEncryptorPassword", String.class);
            methodSetup.invoke(null, encryptorPassword);
        } catch (SecurityException e) {
            s_logger.error("Unable to launch console proxy due to SecurityException", e);
            System.exit(ExitStatus.Error.value());
        } catch (NoSuchMethodException e) {
            s_logger.error("Unable to launch console proxy due to NoSuchMethodException", e);
            System.exit(ExitStatus.Error.value());
        } catch (IllegalArgumentException e) {
            s_logger.error("Unable to launch console proxy due to IllegalArgumentException", e);
            System.exit(ExitStatus.Error.value());
        } catch (IllegalAccessException e) {
            s_logger.error("Unable to launch console proxy due to IllegalAccessException", e);
            System.exit(ExitStatus.Error.value());
        } catch (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(org.apache.cloudstack.managed.context.ManagedContextRunnable) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with ManagedContextRunnable

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

the class AsyncJobManagerImpl method getHeartbeatTask.

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

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

Example 3 with ManagedContextRunnable

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

the class AsyncJobManagerImpl method getExecutorRunnable.

private Runnable getExecutorRunnable(final AsyncJob job) {
    return new ManagedContextRunnable() {

        @Override
        public void run() {
            // register place-holder context to avoid installing system account call context
            if (CallContext.current() == null)
                CallContext.registerPlaceHolderContext();
            String related = job.getRelated();
            String logContext = job.getShortUuid();
            if (related != null && !related.isEmpty()) {
                NDC.push("job-" + related + "/" + "job-" + job.getId());
                AsyncJob relatedJob = _jobDao.findByIdIncludingRemoved(Long.parseLong(related));
                if (relatedJob != null) {
                    logContext = relatedJob.getShortUuid();
                }
            } else {
                NDC.push("job-" + job.getId());
            }
            MDC.put("logcontextid", logContext);
            try {
                super.run();
            } finally {
                NDC.pop();
            }
        }

        @Override
        protected void runInContext() {
            long runNumber = getJobRunNumber();
            try {
                // 
                try {
                    JmxUtil.registerMBean("AsyncJobManager", "Active Job " + job.getId(), new AsyncJobMBeanImpl(job));
                } catch (Exception e) {
                    // is expected to fail under situations
                    if (s_logger.isTraceEnabled())
                        s_logger.trace("Unable to register active job " + job.getId() + " to JMX monitoring due to exception " + ExceptionUtil.toString(e));
                }
                _jobMonitor.registerActiveTask(runNumber, job.getId());
                AsyncJobExecutionContext.setCurrentExecutionContext(new AsyncJobExecutionContext(job));
                String related = job.getRelated();
                String logContext = job.getShortUuid();
                if (related != null && !related.isEmpty()) {
                    AsyncJob relatedJob = _jobDao.findByIdIncludingRemoved(Long.parseLong(related));
                    if (relatedJob != null) {
                        logContext = relatedJob.getShortUuid();
                    }
                }
                MDC.put("logcontextid", logContext);
                // execute the job
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("Executing " + StringUtils.cleanString(job.toString()));
                }
                if ((getAndResetPendingSignals(job) & AsyncJob.Constants.SIGNAL_MASK_WAKEUP) != 0) {
                    AsyncJobDispatcher jobDispatcher = findWakeupDispatcher(job);
                    if (jobDispatcher != null) {
                        jobDispatcher.runJob(job);
                    } else {
                        // TODO, job wakeup is not in use yet
                        if (s_logger.isTraceEnabled())
                            s_logger.trace("Unable to find a wakeup dispatcher from the joined job: " + job);
                    }
                } else {
                    AsyncJobDispatcher jobDispatcher = getDispatcher(job.getDispatcher());
                    if (jobDispatcher != null) {
                        jobDispatcher.runJob(job);
                    } else {
                        s_logger.error("Unable to find job dispatcher, job will be cancelled");
                        completeAsyncJob(job.getId(), JobInfo.Status.FAILED, ApiErrorCode.INTERNAL_ERROR.getHttpCode(), null);
                    }
                }
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("Done executing " + job.getCmd() + " for job-" + job.getId());
                }
            } catch (Throwable e) {
                s_logger.error("Unexpected exception", e);
                completeAsyncJob(job.getId(), JobInfo.Status.FAILED, ApiErrorCode.INTERNAL_ERROR.getHttpCode(), null);
            } finally {
                // guard final clause as well
                try {
                    if (job.getSyncSource() != null) {
                        // here check queue item one more time to double make sure that queue item is removed in case of any uncaught exception
                        _queueMgr.purgeItem(job.getSyncSource().getId());
                    }
                    try {
                        JmxUtil.unregisterMBean("AsyncJobManager", "Active Job " + job.getId());
                    } catch (Exception e) {
                        // is expected to fail under situations
                        if (s_logger.isTraceEnabled())
                            s_logger.trace("Unable to unregister job " + job.getId() + " to JMX monitoring due to exception " + ExceptionUtil.toString(e));
                    }
                    // 
                    // clean execution environment
                    // 
                    AsyncJobExecutionContext.unregister();
                    _jobMonitor.unregisterActiveTask(runNumber);
                } catch (Throwable e) {
                    s_logger.error("Double exception", e);
                }
            }
        }
    };
}
Also used : ManagedContextRunnable(org.apache.cloudstack.managed.context.ManagedContextRunnable) AsyncJobExecutionContext(org.apache.cloudstack.framework.jobs.AsyncJobExecutionContext) AsyncJob(org.apache.cloudstack.framework.jobs.AsyncJob) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) AsyncJobDispatcher(org.apache.cloudstack.framework.jobs.AsyncJobDispatcher)

Example 4 with ManagedContextRunnable

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

the class ConsoleProxyResource method launchConsoleProxy.

private void launchConsoleProxy(final byte[] ksBits, final String ksPassword, final String encryptorPassword, final Boolean isSourceIpCheckEnabled) {
    final Object resource = this;
    s_logger.info("Building class loader for com.cloud.consoleproxy.ConsoleProxy");
    if (_consoleProxyMain == null) {
        s_logger.info("Running com.cloud.consoleproxy.ConsoleProxy with encryptor password=" + encryptorPassword);
        _consoleProxyMain = new Thread(new ManagedContextRunnable() {

            @Override
            protected void runInContext() {
                try {
                    Class<?> consoleProxyClazz = Class.forName("com.cloud.consoleproxy.ConsoleProxy");
                    try {
                        s_logger.info("Invoke startWithContext()");
                        Method method = consoleProxyClazz.getMethod("startWithContext", Properties.class, Object.class, byte[].class, String.class, String.class, Boolean.class);
                        method.invoke(null, _properties, resource, ksBits, ksPassword, encryptorPassword, isSourceIpCheckEnabled);
                    } catch (SecurityException e) {
                        s_logger.error("Unable to launch console proxy due to SecurityException", e);
                        System.exit(ExitStatus.Error.value());
                    } catch (NoSuchMethodException e) {
                        s_logger.error("Unable to launch console proxy due to NoSuchMethodException", e);
                        System.exit(ExitStatus.Error.value());
                    } catch (IllegalArgumentException e) {
                        s_logger.error("Unable to launch console proxy due to IllegalArgumentException", e);
                        System.exit(ExitStatus.Error.value());
                    } catch (IllegalAccessException e) {
                        s_logger.error("Unable to launch console proxy due to IllegalAccessException", e);
                        System.exit(ExitStatus.Error.value());
                    } catch (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 {
            Class<?> consoleProxyClazz = Class.forName("com.cloud.consoleproxy.ConsoleProxy");
            Method methodSetup = consoleProxyClazz.getMethod("setEncryptorPassword", String.class);
            methodSetup.invoke(null, encryptorPassword);
            methodSetup = consoleProxyClazz.getMethod("setIsSourceIpCheckEnabled", Boolean.class);
            methodSetup.invoke(null, isSourceIpCheckEnabled);
        } catch (SecurityException e) {
            s_logger.error("Unable to launch console proxy due to SecurityException", e);
            System.exit(ExitStatus.Error.value());
        } catch (NoSuchMethodException e) {
            s_logger.error("Unable to launch console proxy due to NoSuchMethodException", e);
            System.exit(ExitStatus.Error.value());
        } catch (IllegalArgumentException e) {
            s_logger.error("Unable to launch console proxy due to IllegalArgumentException", e);
            System.exit(ExitStatus.Error.value());
        } catch (IllegalAccessException e) {
            s_logger.error("Unable to launch console proxy due to IllegalAccessException", e);
            System.exit(ExitStatus.Error.value());
        } catch (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(org.apache.cloudstack.managed.context.ManagedContextRunnable) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 5 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)

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