use of com.cloud.common.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();
}
};
}
use of com.cloud.common.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);
}
}
};
}
use of com.cloud.common.managed.context.ManagedContextRunnable in project cosmic by MissionCriticalCloud.
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();
}
final String related = job.getRelated();
String logContext = job.getShortUuid();
if (related != null && !related.isEmpty()) {
MDC.put("job", " (job: " + related + "/" + "job: " + job.getId() + ")");
final AsyncJob relatedJob = _jobDao.findByIdIncludingRemoved(Long.parseLong(related));
if (relatedJob != null) {
logContext = relatedJob.getShortUuid();
}
} else {
MDC.put("job", " (job: " + job.getId() + ")");
}
MDC.put("logcontextid", " (logid: " + logContext + ")");
try {
super.run();
} finally {
MDC.remove("job");
}
}
@Override
protected void runInContext() {
final long runNumber = getJobRunNumber();
try {
//
try {
JmxUtil.registerMBean("AsyncJobManager", "Active Job " + job.getId(), new AsyncJobMBeanImpl(job));
} catch (final 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));
final String related = job.getRelated();
String logContext = job.getShortUuid();
if (related != null && !related.isEmpty()) {
final AsyncJob relatedJob = _jobDao.findByIdIncludingRemoved(Long.parseLong(related));
if (relatedJob != null) {
logContext = relatedJob.getShortUuid();
}
}
MDC.put("logcontextid", " (logid: " + 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) {
final AsyncJobDispatcher jobDispatcher = getWakeupDispatcher(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 {
final AsyncJobDispatcher jobDispatcher = getDispatcher(job.getDispatcher());
if (jobDispatcher != null) {
jobDispatcher.runJob(job);
} else {
s_logger.error("Unable to find job dispatcher, job will be cancelled");
final ExceptionResponse response = new ExceptionResponse();
response.setErrorCode(ApiErrorCode.INTERNAL_ERROR.getHttpCode());
response.setErrorText("Unable to find job dispatcher, job will be cancelled");
completeAsyncJob(job.getId(), JobInfo.Status.FAILED, ApiErrorCode.INTERNAL_ERROR.getHttpCode(), JobSerializerHelper.toSerializedString(response));
}
}
if (s_logger.isDebugEnabled()) {
s_logger.debug("Done executing " + job.getCmd() + " for job-" + job.getId());
}
} catch (final Throwable e) {
s_logger.error("Unexpected exception", e);
final ExceptionResponse response = new ExceptionResponse();
response.setErrorCode(ApiErrorCode.INTERNAL_ERROR.getHttpCode());
response.setErrorText(ExceptionUtils.getRootCauseMessage(e));
completeAsyncJob(job.getId(), JobInfo.Status.FAILED, ApiErrorCode.INTERNAL_ERROR.getHttpCode(), JobSerializerHelper.toSerializedString(response));
} 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 (final 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 (final Throwable e) {
s_logger.error("Double exception", e);
}
}
}
};
}
use of com.cloud.common.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());
this._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) TemplateManagerImpl.this._dataStoreMgr.getPrimaryDataStore(pool.getId());
prepareTemplateForCreate(template, pol);
s_logger.info("End of preloading template " + template.getId() + " into primary storage " + pool.getId());
}
});
}
use of com.cloud.common.managed.context.ManagedContextRunnable in project cosmic by MissionCriticalCloud.
the class ClusteredAgentManagerImpl method getTransferScanTask.
private Runnable getTransferScanTask() {
return new ManagedContextRunnable() {
@Override
protected void runInContext() {
if (s_logger.isTraceEnabled()) {
s_logger.trace("Clustered agent transfer scan check, management server id:" + ClusteredAgentManagerImpl.this._nodeId);
}
synchronized (ClusteredAgentManagerImpl.this._agentToTransferIds) {
if (ClusteredAgentManagerImpl.this._agentToTransferIds.size() > 0) {
s_logger.debug("Found " + ClusteredAgentManagerImpl.this._agentToTransferIds.size() + " agents to transfer");
// for (Long hostId : _agentToTransferIds) {
for (final Iterator<Long> iterator = ClusteredAgentManagerImpl.this._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 = ClusteredAgentManagerImpl.this._hostTransferDao.findActiveHostTransferMapByHostId(hostId, new Date(cutTime.getTime() - ClusteredAgentManagerImpl.this.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();
ClusteredAgentManagerImpl.this._hostTransferDao.completeAgentTransfer(hostId);
continue;
}
if (transferMap.getInitialOwner() != ClusteredAgentManagerImpl.this._nodeId || attache == null || attache.forForward()) {
s_logger.debug("Management server " + ClusteredAgentManagerImpl.this._nodeId + " doesn't own host id=" + hostId + " any more, skipping rebalance for the host");
iterator.remove();
ClusteredAgentManagerImpl.this._hostTransferDao.completeAgentTransfer(hostId);
continue;
}
final ManagementServerHostVO futureOwner = ClusteredAgentManagerImpl.this._mshostDao.findByMsid(transferMap.getFutureOwner());
final ManagementServerHostVO initialOwner = ClusteredAgentManagerImpl.this._mshostDao.findByMsid(transferMap.getInitialOwner());
if (futureOwner != null && futureOwner.getState() != ManagementServerHost.State.Up) {
s_logger.debug("Can't transfer host " + hostId + " as it's future owner is not in UP state: " + futureOwner + ", skipping rebalance");
iterator.remove();
ClusteredAgentManagerImpl.this._hostTransferDao.completeAgentTransfer(hostId);
continue;
}
if (attache.getQueueSize() == 0 && attache.getNonRecurringListenersSize() == 0) {
iterator.remove();
try {
s_logger.debug("Transferring agent for host " + hostId + " from management server " + initialOwner + " " + "to " + futureOwner);
ClusteredAgentManagerImpl.this._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 transferred 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 transferee by the management server " + ClusteredAgentManagerImpl.this._nodeId);
}
}
}
}
};
}
Aggregations