diff --git a/api/src/org/apache/cloudstack/context/CallContext.java b/api/src/org/apache/cloudstack/context/CallContext.java index d0eaa25d6db..bc8ad00b105 100644 --- a/api/src/org/apache/cloudstack/context/CallContext.java +++ b/api/src/org/apache/cloudstack/context/CallContext.java @@ -155,10 +155,14 @@ public class CallContext { s_logger.debug("Context removed " + context); String sessionId = context.getSessionId(); if (sessionId != null) { - while ((sessionId = NDC.pop()) != null) { - if (context.getSessionId().equals(sessionId)) { + String sessionIdOnStack = null; + while ((sessionIdOnStack = NDC.pop()) != null) { + if (sessionId.equals(sessionIdOnStack)) { break; } + if (s_logger.isTraceEnabled()) { + s_logger.trace("Popping from NDC: " + sessionId); + } } } return context; diff --git a/client/tomcatconf/applicationContext.xml.in b/client/tomcatconf/applicationContext.xml.in index bc443bcf811..1c98346e1db 100644 --- a/client/tomcatconf/applicationContext.xml.in +++ b/client/tomcatconf/applicationContext.xml.in @@ -808,9 +808,7 @@ - - - + diff --git a/server/src/com/cloud/api/ApiAsyncJobDispatcher.java b/server/src/com/cloud/api/ApiAsyncJobDispatcher.java index 8fee43f77d5..081bcc139ab 100644 --- a/server/src/com/cloud/api/ApiAsyncJobDispatcher.java +++ b/server/src/com/cloud/api/ApiAsyncJobDispatcher.java @@ -90,7 +90,7 @@ public class ApiAsyncJobDispatcher extends AdapterBase implements AsyncJobDispat CallContext.register(userId, accountObject, "job-" + job.getShortUuid(), false); try { // dispatch could ultimately queue the job - _dispatcher.dispatch(cmdObj, params); + _dispatcher.dispatch(cmdObj, params, true); // serialize this to the async job table _asyncJobMgr.completeAsyncJob(job.getId(), AsyncJobConstants.STATUS_SUCCEEDED, 0, cmdObj.getResponseObject()); diff --git a/server/src/com/cloud/api/ApiDispatcher.java b/server/src/com/cloud/api/ApiDispatcher.java index e9536e22774..ff709d07ca5 100755 --- a/server/src/com/cloud/api/ApiDispatcher.java +++ b/server/src/com/cloud/api/ApiDispatcher.java @@ -58,7 +58,6 @@ import org.apache.cloudstack.context.CallContext; import org.apache.cloudstack.framework.jobs.AsyncJob; import org.apache.cloudstack.framework.jobs.AsyncJobManager; -import com.cloud.async.AsyncJobExecutionContext; import com.cloud.dao.EntityManager; import com.cloud.exception.InvalidParameterValueException; import com.cloud.user.Account; @@ -122,7 +121,7 @@ public class ApiDispatcher { } } - public void dispatch(BaseCmd cmd, Map params) throws Exception { + public void dispatch(BaseCmd cmd, Map params, boolean execute) throws Exception { processParameters(cmd, params); CallContext ctx = CallContext.current(); @@ -142,7 +141,7 @@ public class ApiDispatcher { } if (queueSizeLimit != null) { - if(AsyncJobExecutionContext.getCurrentExecutionContext() == null) { + if (!execute) { // if we are not within async-execution context, enqueue the command _asyncMgr.syncAsyncJobExecution((AsyncJob)asyncCmd.getJob(), asyncCmd.getSyncObjType(), asyncCmd.getSyncObjId().longValue(), queueSizeLimit); return; diff --git a/server/src/com/cloud/api/ApiServer.java b/server/src/com/cloud/api/ApiServer.java index 2dfdaf0e057..175c8b823c9 100755 --- a/server/src/com/cloud/api/ApiServer.java +++ b/server/src/com/cloud/api/ApiServer.java @@ -170,6 +170,9 @@ public class ApiServer extends ManagerBase implements HttpRequestHandler, ApiSer @Inject List _pluggableServices; @Inject List _apiAccessCheckers; + @Inject + ApiAsyncJobDispatcher _asyncDispatcher; + @Inject private EntityManager _entityMgr; @@ -520,6 +523,7 @@ public class ApiServer extends ManagerBase implements HttpRequestHandler, ApiSer AsyncJobVO job = new AsyncJobVO(callerUserId, caller.getId(), cmdObj.getClass().getName(), ApiGsonHelper.getBuilder().create().toJson(params), instanceId, asyncCmd.getInstanceType() != null ? asyncCmd.getInstanceType().toString() : null); + job.setDispatcher(_asyncDispatcher.getName()); long jobId = _asyncMgr.submitAsyncJob(job); @@ -537,7 +541,7 @@ public class ApiServer extends ManagerBase implements HttpRequestHandler, ApiSer return getBaseAsyncResponse(jobId, asyncCmd); } } else { - _dispatcher.dispatch(cmdObj, params); + _dispatcher.dispatch(cmdObj, params, false); // if the command is of the listXXXCommand, we will need to also return the // the job id and status if possible diff --git a/server/src/com/cloud/async/AsyncJobManagerImpl.java b/server/src/com/cloud/async/AsyncJobManagerImpl.java index bedeb434777..42cbae86853 100644 --- a/server/src/com/cloud/async/AsyncJobManagerImpl.java +++ b/server/src/com/cloud/async/AsyncJobManagerImpl.java @@ -108,15 +108,6 @@ public class AsyncJobManagerImpl extends ManagerBase implements AsyncJobManager, @Inject private MessageBus _messageBus; @Inject private AsyncJobMonitor _jobMonitor; - // property - private String defaultDispatcher; - public String getDefaultDispatcher() { - return defaultDispatcher; - } - public void setDefaultDispatcher(String defaultDispatcher) { - this.defaultDispatcher = defaultDispatcher; - } - private long _jobExpireSeconds = 86400; // 1 day private long _jobCancelThresholdSeconds = 3600; // 1 hour (for cancelling the jobs blocking other jobs) @@ -491,16 +482,14 @@ public class AsyncJobManagerImpl extends ManagerBase implements AsyncJobManager, } private AsyncJobDispatcher getDispatcher(String dispatcherName) { - if(dispatcherName == null || dispatcherName.isEmpty()) - dispatcherName = defaultDispatcher; + assert (dispatcherName != null && !dispatcherName.isEmpty()) : "Who's not setting the dispatcher when submitting a job? Who am I suppose to call if you do that!"; - if(_jobDispatchers != null) { - for(AsyncJobDispatcher dispatcher : _jobDispatchers) { - if(dispatcherName.equals(dispatcher.getName())) - return dispatcher; - } - } - return null; + for (AsyncJobDispatcher dispatcher : _jobDispatchers) { + if (dispatcherName.equals(dispatcher.getName())) + return dispatcher; + } + + throw new CloudRuntimeException("Unable to find dispatcher name: " + dispatcherName); } private AsyncJobDispatcher getWakeupDispatcher(AsyncJob job) {