use of org.apache.ignite.internal.GridTaskSessionImpl in project ignite by apache.
the class GridJobProcessor method processJobExecuteRequest.
/**
* @param node Node.
* @param req Request.
*/
@SuppressWarnings("TooBroadScope")
public void processJobExecuteRequest(ClusterNode node, final GridJobExecuteRequest req) {
if (log.isDebugEnabled())
log.debug("Received job request message [req=" + req + ", nodeId=" + node.id() + ']');
PartitionsReservation partsReservation = null;
if (req.getCacheIds() != null) {
assert req.getPartition() >= 0 : req;
assert !F.isEmpty(req.getCacheIds()) : req;
partsReservation = new PartitionsReservation(req.getCacheIds(), req.getPartition(), req.getTopVer());
}
GridJobWorker job = null;
if (!rwLock.tryReadLock()) {
if (log.isDebugEnabled())
log.debug("Received job execution request while stopping this node (will ignore): " + req);
return;
}
try {
long endTime = req.getCreateTime() + req.getTimeout();
// Account for overflow.
if (endTime < 0)
endTime = Long.MAX_VALUE;
GridDeployment tmpDep = req.isForceLocalDeployment() ? ctx.deploy().getLocalDeployment(req.getTaskClassName()) : ctx.deploy().getGlobalDeployment(req.getDeploymentMode(), req.getTaskName(), req.getTaskClassName(), req.getUserVersion(), node.id(), req.getClassLoaderId(), req.getLoaderParticipants(), null);
if (tmpDep == null) {
if (log.isDebugEnabled())
log.debug("Checking local tasks...");
// Check local tasks.
for (Map.Entry<String, GridDeployment> d : ctx.task().getUsedDeploymentMap().entrySet()) {
if (d.getValue().classLoaderId().equals(req.getClassLoaderId())) {
assert d.getValue().local();
tmpDep = d.getValue();
break;
}
}
}
final GridDeployment dep = tmpDep;
if (log.isDebugEnabled())
log.debug("Deployment: " + dep);
boolean releaseDep = true;
try {
if (dep != null && dep.acquire()) {
GridJobSessionImpl jobSes;
GridJobContextImpl jobCtx;
try {
List<ComputeJobSibling> siblings = null;
if (!req.isDynamicSiblings()) {
Collection<ComputeJobSibling> siblings0 = req.getSiblings();
if (siblings0 == null) {
assert req.getSiblingsBytes() != null;
siblings0 = U.unmarshal(marsh, req.getSiblingsBytes(), U.resolveClassLoader(ctx.config()));
}
siblings = new ArrayList<>(siblings0);
}
Map<Object, Object> sesAttrs = null;
if (req.isSessionFullSupport()) {
sesAttrs = req.getSessionAttributes();
if (sesAttrs == null)
sesAttrs = U.unmarshal(marsh, req.getSessionAttributesBytes(), U.resolveClassLoader(dep.classLoader(), ctx.config()));
}
IgnitePredicate<ClusterNode> topologyPred = req.getTopologyPredicate();
if (topologyPred == null && req.getTopologyPredicateBytes() != null) {
topologyPred = U.unmarshal(marsh, req.getTopologyPredicateBytes(), U.resolveClassLoader(dep.classLoader(), ctx.config()));
}
// Note that we unmarshal session/job attributes here with proper class loader.
GridTaskSessionImpl taskSes = ctx.session().createTaskSession(req.getSessionId(), node.id(), req.getTaskName(), dep, req.getTaskClassName(), req.topology(), topologyPred, req.getStartTaskTime(), endTime, siblings, sesAttrs, req.isSessionFullSupport(), req.isInternal(), req.getSubjectId(), req.executorName());
taskSes.setCheckpointSpi(req.getCheckpointSpi());
taskSes.setClassLoader(dep.classLoader());
jobSes = new GridJobSessionImpl(ctx, taskSes, req.getJobId());
Map<? extends Serializable, ? extends Serializable> jobAttrs = req.getJobAttributes();
if (jobAttrs == null)
jobAttrs = U.unmarshal(marsh, req.getJobAttributesBytes(), U.resolveClassLoader(dep.classLoader(), ctx.config()));
jobCtx = new GridJobContextImpl(ctx, req.getJobId(), jobAttrs);
} catch (IgniteCheckedException e) {
IgniteException ex = new IgniteException("Failed to deserialize task attributes " + "[taskName=" + req.getTaskName() + ", taskClsName=" + req.getTaskClassName() + ", codeVer=" + req.getUserVersion() + ", taskClsLdr=" + dep.classLoader() + ']', e);
U.error(log, ex.getMessage(), e);
handleException(node, req, ex, endTime);
return;
}
job = new GridJobWorker(ctx, dep, req.getCreateTime(), jobSes, jobCtx, req.getJobBytes(), req.getJob(), node, req.isInternal(), evtLsnr, holdLsnr, partsReservation, req.getTopVer(), req.executorName());
jobCtx.job(job);
// If exception occurs on job initialization, deployment is released in job listener.
releaseDep = false;
if (job.initialize(dep, dep.deployedClass(req.getTaskClassName()))) {
// Internal jobs will always be executed synchronously.
if (job.isInternal()) {
// This is an internal job and can be executed inside busy lock
// since job is expected to be short.
// This is essential for proper stop without races.
job.run();
// No execution outside lock.
job = null;
} else if (jobAlwaysActivate) {
if (onBeforeActivateJob(job)) {
if (ctx.localNodeId().equals(node.id())) {
// Always execute in another thread for local node.
executeAsync(job);
// No sync execution.
job = null;
} else if (metricsUpdateFreq > -1L)
// Job will be executed synchronously.
startedJobsCnt.increment();
} else
// Job has been cancelled.
// Set to null, to avoid sync execution.
job = null;
} else {
GridJobWorker old = passiveJobs.putIfAbsent(job.getJobId(), job);
if (old == null)
handleCollisions();
else
U.error(log, "Received computation request with duplicate job ID (could be " + "network malfunction, source node may hang if task timeout was not set) " + "[srcNode=" + node.id() + ", jobId=" + req.getJobId() + ", sesId=" + req.getSessionId() + ", locNodeId=" + ctx.localNodeId() + ']');
// No sync execution.
job = null;
}
} else
// Job was not initialized, no execution.
job = null;
} else {
// Deployment is null.
IgniteException ex = new IgniteDeploymentException("Task was not deployed or was redeployed since " + "task execution [taskName=" + req.getTaskName() + ", taskClsName=" + req.getTaskClassName() + ", codeVer=" + req.getUserVersion() + ", clsLdrId=" + req.getClassLoaderId() + ", seqNum=" + req.getClassLoaderId().localId() + ", depMode=" + req.getDeploymentMode() + ", dep=" + dep + ']');
U.error(log, ex.getMessage(), ex);
handleException(node, req, ex, endTime);
}
} finally {
if (dep != null && releaseDep)
release(dep);
}
} finally {
rwLock.readUnlock();
}
if (job != null)
job.run();
}
use of org.apache.ignite.internal.GridTaskSessionImpl in project ignite by apache.
the class GridTaskProcessor method getUsedDeploymentMap.
/**
* Gets currently used deployments mapped by task name or aliases.
*
* @return Currently used deployments.
*/
public Map<String, GridDeployment> getUsedDeploymentMap() {
Map<String, GridDeployment> deps = new HashMap<>();
for (GridTaskWorker w : tasks.values()) {
GridTaskSessionImpl ses = w.getSession();
deps.put(ses.getTaskClassName(), w.getDeployment());
if (ses.getTaskName() != null && ses.getTaskClassName().equals(ses.getTaskName()))
deps.put(ses.getTaskName(), w.getDeployment());
}
return deps;
}
use of org.apache.ignite.internal.GridTaskSessionImpl in project ignite by apache.
the class GridTaskProcessor method processTaskSessionRequest.
/**
* @param nodeId Node ID.
* @param msg Task session request.
*/
@SuppressWarnings({ "unchecked" })
private void processTaskSessionRequest(UUID nodeId, GridTaskSessionRequest msg) {
assert nodeId != null;
assert msg != null;
lock.readLock();
try {
if (stopping && !waiting) {
U.warn(log, "Received task session request while stopping grid (will ignore): " + msg);
return;
}
GridTaskWorker<?, ?> task = tasks.get(msg.getSessionId());
if (task == null) {
if (log.isDebugEnabled())
log.debug("Received task session request for unknown task (was task already reduced?): " + msg);
return;
}
boolean loc = ctx.localNodeId().equals(nodeId) && !ctx.config().isMarshalLocalJobs();
Map<?, ?> attrs = loc ? msg.getAttributes() : U.<Map<?, ?>>unmarshal(marsh, msg.getAttributesBytes(), U.resolveClassLoader(task.getTask().getClass().getClassLoader(), ctx.config()));
GridTaskSessionImpl ses = task.getSession();
sendSessionAttributes(attrs, ses);
} catch (IgniteCheckedException e) {
U.error(log, "Failed to deserialize session request: " + msg, e);
} finally {
lock.readUnlock();
}
}
use of org.apache.ignite.internal.GridTaskSessionImpl in project ignite by apache.
the class GridJobProcessor method processTaskSessionRequest.
/**
* @param nodeId Node ID.
* @param req Request.
*/
@SuppressWarnings({ "SynchronizationOnLocalVariableOrMethodParameter", "RedundantCast" })
private void processTaskSessionRequest(UUID nodeId, GridTaskSessionRequest req) {
if (!rwLock.tryReadLock()) {
if (log.isDebugEnabled())
log.debug("Received job session request while stopping grid (will ignore): " + req);
return;
}
try {
GridTaskSessionImpl ses = ctx.session().getSession(req.getSessionId());
if (ses == null) {
if (log.isDebugEnabled())
log.debug("Received job session request for non-existing session: " + req);
return;
}
boolean loc = ctx.localNodeId().equals(nodeId) && !ctx.config().isMarshalLocalJobs();
Map<?, ?> attrs = loc ? req.getAttributes() : (Map<?, ?>) U.unmarshal(marsh, req.getAttributesBytes(), U.resolveClassLoader(ses.getClassLoader(), ctx.config()));
if (ctx.event().isRecordable(EVT_TASK_SESSION_ATTR_SET)) {
Event evt = new TaskEvent(ctx.discovery().localNode(), "Changed attributes: " + attrs, EVT_TASK_SESSION_ATTR_SET, ses.getId(), ses.getTaskName(), ses.getTaskClassName(), false, null);
ctx.event().record(evt);
}
synchronized (ses) {
ses.setInternal(attrs);
}
} catch (IgniteCheckedException e) {
U.error(log, "Failed to deserialize session attributes.", e);
} finally {
rwLock.readUnlock();
}
}
use of org.apache.ignite.internal.GridTaskSessionImpl in project ignite by apache.
the class GridTaskProcessor method startTask.
/**
* @param taskName Task name.
* @param taskCls Task class.
* @param task Task.
* @param sesId Task session ID.
* @param arg Optional task argument.
* @param sys If {@code true}, then system pool will be used.
* @param execName Name of the custom executor.
* @return Task future.
*/
@SuppressWarnings("unchecked")
private <T, R> ComputeTaskInternalFuture<R> startTask(@Nullable String taskName, @Nullable Class<?> taskCls, @Nullable ComputeTask<T, R> task, IgniteUuid sesId, @Nullable T arg, boolean sys, @Nullable String execName) {
assert sesId != null;
String taskClsName;
if (task != null)
taskClsName = task.getClass().getName();
else
taskClsName = taskCls != null ? taskCls.getName() : taskName;
// Get values from thread-local context.
Map<GridTaskThreadContextKey, Object> map = thCtx.get();
if (map == null)
map = EMPTY_ENUM_MAP;
else
// Reset thread-local context.
thCtx.set(null);
if (map.get(TC_SKIP_AUTH) == null)
ctx.security().authorize(taskClsName, SecurityPermission.TASK_EXECUTE, null);
Long timeout = (Long) map.get(TC_TIMEOUT);
long timeout0 = timeout == null || timeout == 0 ? Long.MAX_VALUE : timeout;
long startTime = U.currentTimeMillis();
long endTime = timeout0 + startTime;
// Account for overflow.
if (endTime < 0)
endTime = Long.MAX_VALUE;
IgniteCheckedException deployEx = null;
GridDeployment dep = null;
// User provided task name.
if (taskName != null) {
assert taskCls == null;
assert task == null;
try {
dep = ctx.deploy().getDeployment(taskName);
if (dep == null)
throw new IgniteDeploymentCheckedException("Unknown task name or failed to auto-deploy " + "task (was task (re|un)deployed?): " + taskName);
taskCls = dep.deployedClass(taskName);
if (taskCls == null)
throw new IgniteDeploymentCheckedException("Unknown task name or failed to auto-deploy " + "task (was task (re|un)deployed?) [taskName=" + taskName + ", dep=" + dep + ']');
if (!ComputeTask.class.isAssignableFrom(taskCls))
throw new IgniteCheckedException("Failed to auto-deploy task (deployed class is not a task) " + "[taskName=" + taskName + ", depCls=" + taskCls + ']');
} catch (IgniteCheckedException e) {
deployEx = e;
}
} else // Deploy user task class.
if (taskCls != null) {
assert task == null;
try {
// Implicit deploy.
dep = ctx.deploy().deploy(taskCls, U.detectClassLoader(taskCls));
if (dep == null)
throw new IgniteDeploymentCheckedException("Failed to auto-deploy task " + "(was task (re|un)deployed?): " + taskCls);
taskName = taskName(dep, taskCls, map);
} catch (IgniteCheckedException e) {
taskName = taskCls.getName();
deployEx = e;
}
} else // Deploy user task.
if (task != null) {
try {
ClassLoader ldr;
Class<?> cls;
if (task instanceof GridPeerDeployAware) {
GridPeerDeployAware depAware = (GridPeerDeployAware) task;
cls = depAware.deployClass();
ldr = depAware.classLoader();
// Set proper class name to make peer-loading possible.
taskCls = cls;
} else {
taskCls = task.getClass();
assert ComputeTask.class.isAssignableFrom(taskCls);
cls = task.getClass();
ldr = U.detectClassLoader(cls);
}
// Explicit deploy.
dep = ctx.deploy().deploy(cls, ldr);
if (dep == null)
throw new IgniteDeploymentCheckedException("Failed to auto-deploy task " + "(was task (re|un)deployed?): " + cls);
taskName = taskName(dep, taskCls, map);
} catch (IgniteCheckedException e) {
taskName = task.getClass().getName();
deployEx = e;
}
}
assert taskName != null;
if (log.isDebugEnabled())
log.debug("Task deployment: " + dep);
boolean fullSup = dep != null && taskCls != null && dep.annotation(taskCls, ComputeTaskSessionFullSupport.class) != null;
Collection<UUID> top = null;
final IgnitePredicate<ClusterNode> topPred = (IgnitePredicate<ClusterNode>) map.get(TC_SUBGRID_PREDICATE);
if (topPred == null) {
final Collection<ClusterNode> nodes = (Collection<ClusterNode>) map.get(TC_SUBGRID);
top = nodes != null ? F.nodeIds(nodes) : null;
}
UUID subjId = getThreadContext(TC_SUBJ_ID);
if (subjId == null)
subjId = ctx.localNodeId();
boolean internal = false;
if (dep == null || taskCls == null)
assert deployEx != null;
else
internal = dep.internalTask(task, taskCls);
// Creates task session with task name and task version.
GridTaskSessionImpl ses = ctx.session().createTaskSession(sesId, ctx.localNodeId(), taskName, dep, taskCls == null ? null : taskCls.getName(), top, topPred, startTime, endTime, Collections.<ComputeJobSibling>emptyList(), Collections.emptyMap(), fullSup, internal, subjId, execName);
ComputeTaskInternalFuture<R> fut = new ComputeTaskInternalFuture<>(ses, ctx);
IgniteCheckedException securityEx = null;
if (ctx.security().enabled() && deployEx == null && !dep.internalTask(task, taskCls)) {
try {
saveTaskMetadata(taskName);
} catch (IgniteCheckedException e) {
securityEx = e;
}
}
if (deployEx == null && securityEx == null) {
if (dep == null || !dep.acquire())
handleException(new IgniteDeploymentCheckedException("Task not deployed: " + ses.getTaskName()), fut);
else {
GridTaskWorker<?, ?> taskWorker = new GridTaskWorker<>(ctx, arg, ses, fut, taskCls, task, dep, new TaskEventListener(), map, subjId);
GridTaskWorker<?, ?> taskWorker0 = tasks.putIfAbsent(sesId, taskWorker);
assert taskWorker0 == null : "Session ID is not unique: " + sesId;
if (!ctx.clientDisconnected()) {
if (dep.annotation(taskCls, ComputeTaskMapAsync.class) != null) {
try {
// Start task execution in another thread.
if (sys)
ctx.getSystemExecutorService().execute(taskWorker);
else
ctx.getExecutorService().execute(taskWorker);
} catch (RejectedExecutionException e) {
tasks.remove(sesId);
release(dep);
handleException(new ComputeExecutionRejectedException("Failed to execute task " + "due to thread pool execution rejection: " + taskName, e), fut);
}
} else
taskWorker.run();
} else
taskWorker.finishTask(null, disconnectedError(null));
}
} else {
if (deployEx != null)
handleException(deployEx, fut);
else
handleException(securityEx, fut);
}
return fut;
}
Aggregations