Search in sources :

Example 11 with ManagedContextRunnable

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

the class AsyncJobManagerImpl method getGCTask.

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

        @Override
        protected void runInContext() {
            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.trace("Begin cleanup expired async-jobs");
                // forcefully cancel blocking queue items if they've been staying there for too long
                List<SyncQueueItemVO> blockItems = _queueMgr.getBlockedQueueItems(JobCancelThresholdMinutes.value() * 60000, false);
                if (blockItems != null && blockItems.size() > 0) {
                    for (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 (Throwable e) {
                            s_logger.error("Unexpected exception when trying to remove job from sync queue, ", e);
                        }
                    }
                }
                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
                List<AsyncJobVO> unfinishedJobs = _jobDao.getExpiredUnfinishedJobs(cutTime, 100);
                for (AsyncJobVO job : unfinishedJobs) {
                    try {
                        s_logger.info("Expunging unfinished job-" + job.getId());
                        _jobMonitor.unregisterByJobId(job.getId());
                        expungeAsyncJob(job);
                    } catch (Throwable e) {
                        s_logger.error("Unexpected exception when trying to expunge job-" + job.getId(), e);
                    }
                }
                // 2) Expunge finished jobs
                List<AsyncJobVO> completedJobs = _jobDao.getExpiredCompletedJobs(cutTime, 100);
                for (AsyncJobVO job : completedJobs) {
                    try {
                        s_logger.info("Expunging completed job-" + job.getId());
                        expungeAsyncJob(job);
                    } catch (Throwable e) {
                        s_logger.error("Unexpected exception when trying to expunge job-" + job.getId(), e);
                    }
                }
                s_logger.trace("End cleanup expired async-jobs");
            } 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) Date(java.util.Date) DB(com.cloud.utils.db.DB)

Example 12 with ManagedContextRunnable

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

the class ClusterManagerImpl method onNotifyingClusterPdu.

private void onNotifyingClusterPdu() {
    while (true) {
        try {
            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);
                        }
                    }
                }
            });
        } catch (final Throwable e) {
            s_logger.error("Unexcpeted exception: ", e);
        }
    }
}
Also used : ManagedContextRunnable(org.apache.cloudstack.managed.context.ManagedContextRunnable)

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