use of org.apache.ignite.compute.ComputeTask in project ignite by apache.
the class GridP2PUndeploySelfTest method processTestUndeployP2PTasks.
/**
* @param depMode deployment mode.
* @throws Exception If failed.
*/
@SuppressWarnings("unchecked")
private void processTestUndeployP2PTasks(DeploymentMode depMode) throws Exception {
try {
this.depMode = depMode;
Ignite ignite1 = startGrid(1);
Ignite ignite2 = startGrid(2);
ClassLoader ldr = new URLClassLoader(new URL[] { new URL(GridTestProperties.getProperty("p2p.uri.cls")) }, GridP2PSameClassLoaderSelfTest.class.getClassLoader());
Class<? extends ComputeTask<?, ?>> task1 = (Class<? extends ComputeTask<?, ?>>) ldr.loadClass(TEST_TASK_NAME);
ignite1.compute().localDeployTask(task1, ldr);
ignite1.compute().execute(task1.getName(), ignite2.cluster().localNode().id());
LocalDeploymentSpi spi1 = spis.get(ignite1.name());
LocalDeploymentSpi spi2 = spis.get(ignite2.name());
assert spi1.findResource(task1.getName()) != null;
assert ignite1.compute().localTasks().containsKey(task1.getName());
// P2P deployment will not deploy task into the SPI.
assert spi2.findResource(task1.getName()) == null;
ignite1.compute().undeployTask(task1.getName());
// Wait for undeploy.
Thread.sleep(1000);
assert spi1.findResource(task1.getName()) == null;
assert spi2.findResource(task1.getName()) == null;
assert !ignite1.compute().localTasks().containsKey(task1.getName());
assert !ignite2.compute().localTasks().containsKey(task1.getName());
spis = null;
} finally {
stopGrid(2);
stopGrid(1);
}
}
use of org.apache.ignite.compute.ComputeTask in project ignite by apache.
the class GridP2PUndeploySelfTest method processTestUndeployLocalTasks.
/**
* @param depMode deployment mode.
* @throws Exception If failed.
*/
@SuppressWarnings("unchecked")
private void processTestUndeployLocalTasks(DeploymentMode depMode) throws Exception {
try {
this.depMode = depMode;
Ignite ignite1 = startGrid(1);
Ignite ignite2 = startGrid(2);
ClassLoader tstClsLdr = new GridTestClassLoader(GridP2PTestTask.class.getName(), GridP2PTestJob.class.getName());
Class<? extends ComputeTask<?, ?>> task1 = (Class<? extends ComputeTask<?, ?>>) tstClsLdr.loadClass(GridP2PTestTask.class.getName());
ignite1.compute().localDeployTask(task1, tstClsLdr);
ignite1.compute().execute(task1.getName(), 1);
ignite2.compute().localDeployTask(task1, tstClsLdr);
ignite2.compute().execute(task1.getName(), 2);
LocalDeploymentSpi spi1 = spis.get(ignite1.name());
LocalDeploymentSpi spi2 = spis.get(ignite2.name());
assert spi1.findResource(task1.getName()) != null;
assert spi2.findResource(task1.getName()) != null;
assert ignite1.compute().localTasks().containsKey(task1.getName());
assert ignite2.compute().localTasks().containsKey(task1.getName());
ignite2.compute().undeployTask(task1.getName());
// Wait for undeploy.
Thread.sleep(1000);
assert spi1.findResource(task1.getName()) == null;
assert spi2.findResource(task1.getName()) == null;
assert !ignite1.compute().localTasks().containsKey(task1.getName());
assert !ignite2.compute().localTasks().containsKey(task1.getName());
} finally {
stopGrid(2);
stopGrid(1);
}
}
use of org.apache.ignite.compute.ComputeTask in project ignite by apache.
the class GridUriDeploymentFileProcessor method processWithDescriptorFile.
/**
* Processes given GAR file and returns back all tasks which are in
* descriptor.
*
* @param doc GAR file descriptor.
* @param file GAR file.
* @param uri GAR file deployment URI.
* @param log Logger.
* @throws org.apache.ignite.spi.IgniteSpiException Thrown if it's impossible to open file.
* @return List of tasks from descriptor.
*/
@SuppressWarnings({ "ClassLoader2Instantiation" })
private static GridUriDeploymentFileProcessorResult processWithDescriptorFile(GridUriDeploymentSpringDocument doc, File file, String uri, IgniteLogger log) throws IgniteSpiException {
ClassLoader clsLdr = GridUriDeploymentClassLoaderFactory.create(U.gridClassLoader(), file, log);
List<Class<? extends ComputeTask<?, ?>>> tasks = doc.getTasks(clsLdr);
List<Class<? extends ComputeTask<?, ?>>> validTasks = null;
if (!F.isEmpty(tasks)) {
validTasks = new ArrayList<>();
for (Class<? extends ComputeTask<?, ?>> task : tasks) {
if (!isAllowedTaskClass(task)) {
U.warn(log, "Failed to load task. Task should be public none-abstract class " + "(might be inner static one) that implements ComputeTask interface [taskCls=" + task + ']');
} else {
if (log.isDebugEnabled())
log.debug("Found grid deployment task: " + task.getName());
validTasks.add(task);
}
}
}
GridUriDeploymentFileProcessorResult res = new GridUriDeploymentFileProcessorResult();
res.setFile(file);
res.setClassLoader(clsLdr);
if (!F.isEmpty(validTasks))
res.setTaskClasses(validTasks);
else if (log.isDebugEnabled())
log.debug("No tasks loaded from file [file=" + file.getAbsolutePath() + ", uri=" + U.hidePassword(uri) + ']');
return res;
}
use of org.apache.ignite.compute.ComputeTask in project ignite by apache.
the class GridifySpringAspect method invoke.
/**
* Aspect implementation which executes grid-enabled methods on remote
* nodes.
*
* {@inheritDoc}
*/
@SuppressWarnings({ "ProhibitedExceptionDeclared", "ProhibitedExceptionThrown", "CatchGenericClass", "unchecked" })
@Override
public Object invoke(MethodInvocation invoc) throws Throwable {
Method mtd = invoc.getMethod();
Gridify ann = mtd.getAnnotation(Gridify.class);
assert ann != null : "Intercepted method does not have gridify annotation.";
// Since annotations in Java don't allow 'null' as default value
// we have accept an empty string and convert it here.
// NOTE: there's unintended behavior when user specifies an empty
// string as intended Ignite instance name.
// NOTE: the 'ann.igniteInstanceName() == null' check is added to mitigate
// annotation bugs in some scripting languages (e.g. Groovy).
String igniteInstanceName = F.isEmpty(ann.igniteInstanceName()) ? ann.gridName() : ann.igniteInstanceName();
if (F.isEmpty(igniteInstanceName))
igniteInstanceName = null;
if (G.state(igniteInstanceName) != STARTED)
throw new IgniteCheckedException("Grid is not locally started: " + igniteInstanceName);
// Initialize defaults.
GridifyArgument arg = new GridifyArgumentAdapter(mtd.getDeclaringClass(), mtd.getName(), mtd.getParameterTypes(), invoc.getArguments(), invoc.getThis());
if (!ann.interceptor().equals(GridifyInterceptor.class)) {
// Check interceptor first.
if (!ann.interceptor().newInstance().isGridify(ann, arg))
return invoc.proceed();
}
if (!ann.taskClass().equals(GridifyDefaultTask.class) && !ann.taskName().isEmpty())
throw new IgniteCheckedException("Gridify annotation must specify either Gridify.taskName() or " + "Gridify.taskClass(), but not both: " + ann);
try {
Ignite ignite = G.ignite(igniteInstanceName);
if (!ann.taskClass().equals(GridifyDefaultTask.class))
return ignite.compute().withTimeout(ann.timeout()).execute((Class<? extends ComputeTask<GridifyArgument, Object>>) ann.taskClass(), arg);
// If task name was not specified.
if (ann.taskName().isEmpty())
return ignite.compute().withTimeout(ann.timeout()).execute(new GridifyDefaultTask(invoc.getMethod().getDeclaringClass()), arg);
// If task name was specified.
return ignite.compute().withTimeout(ann.timeout()).execute(ann.taskName(), arg);
} catch (Exception e) {
for (Class<?> ex : invoc.getMethod().getExceptionTypes()) {
// Descend all levels down.
Throwable cause = e.getCause();
while (cause != null) {
if (ex.isAssignableFrom(cause.getClass()))
throw cause;
cause = cause.getCause();
}
if (ex.isAssignableFrom(e.getClass()))
throw e;
}
throw new GridifyRuntimeException("Undeclared exception thrown: " + e.getMessage(), e);
}
}
use of org.apache.ignite.compute.ComputeTask 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