T
- Type of the task argument that is passed into map(List, Object)
method.R
- Type of the task result returning from reduce(List)
method.public interface ComputeTask<T,R> extends Serializable
IgniteCompute.localDeployTask(Class, ClassLoader)
method), however if task does not specify
its name explicitly via @ComputeTaskName
annotation, it
will be auto-deployed first time it gets executed.
ComputeTaskSession
).
org.apache.ignite.resources
package for the list of injectable resources.
map(List, Object)
. This
method is responsible for splitting business logic of grid task into
multiple grid jobs (units of execution) and mapping them to
grid nodes. Method map(List, Object)
returns
a map of with grid jobs as keys and grid node as values.
CollisionSpi
) which will determine how a job will be executed
on the remote node (immediately, buffered or canceled).
result(ComputeJobResult, List)
will be called for each received job result. The policy returned by this method will
determine the way task reacts to every job result:
ComputeJobResultPolicy.WAIT
policy is returned, task will continue to wait
for other job results. If this result is the last job result, then
reduce(List)
method will be called.
ComputeJobResultPolicy.REDUCE
policy is returned, then method
reduce(List)
will be called right away without waiting for
other jobs' completion (all remaining jobs will receive a cancel request).
ComputeJobResultPolicy.FAILOVER
policy is returned, then job will
be failed over to another node for execution. The node to which job will get
failed over is decided by FailoverSpi
SPI implementation.
Note that if you use ComputeTaskAdapter
adapter for ComputeTask
implementation, then it will automatically fail jobs to another node for 2
known failure cases:
ComputeJobResult.getException()
method will return an instance of ClusterTopologyException
exception.
ComputeJobResult.getException()
method will return an instance of
ComputeExecutionRejectedException
exception.
result(ComputeJobResult, List)
method returned ComputeJobResultPolicy.REDUCE
policy, method reduce(List)
is called to aggregate received results into one final result. Once this method is finished the
execution of the grid task is complete. This result will be returned to the user through
ComputeTaskFuture.get()
method.
map(List, Object)
step,
use ComputeTaskContinuousMapper
to continuously stream jobs from task even after map(...)
step is complete. Usually with continuous mapper the number of jobs within task
may grow too large - in this case it may make sense to use it in combination with
@ComputeTaskNoResultCache
annotation.
@ComputeTaskNoResultCache
annotation to task class, and
processing all results as they come in result(ComputeJobResult, List)
method.
When Ignite sees this annotation it will disable tracking of job results and
list of all job results passed into result(ComputeJobResult, List)
or
reduce(List)
methods will always be empty. Note that list of
job siblings on ComputeTaskSession
will also be empty to prevent number
of job siblings from growing as well.
TaskSessionResource
IgniteInstanceResource
LoggerResource
SpringApplicationContextResource
SpringResource
ComputeTask
comes with several convenience adapters to make the usage easier:
ComputeTaskAdapter
provides default implementation for result(ComputeJobResult, List)
method which provides automatic fail-over to another node if remote job has failed
due to node crash (detected by ClusterTopologyException
exception) or due to job
execution rejection (detected by ComputeExecutionRejectedException
exception).
Here is an example of how a you would implement your task using ComputeTaskAdapter
:
public class MyFooBarTask extends ComputeTaskAdapter<String, String> { // Inject load balancer. @LoadBalancerResource ComputeLoadBalancer balancer; // Map jobs to grid nodes. public Map<? extends ComputeJob, ClusterNode> map(List<ClusterNode> subgrid, String arg) throws IgniteCheckedException { Map<MyFooBarJob, ClusterNode> jobs = new HashMap<MyFooBarJob, ClusterNode>(subgrid.size()); // In more complex cases, you can actually do // more complicated assignments of jobs to nodes. for (int i = 0; i < subgrid.size(); i++) { // Pick the next best balanced node for the job. jobs.put(new MyFooBarJob(arg), balancer.getBalancedNode()) } return jobs; } // Aggregate results into one compound result. public String reduce(List<ComputeJobResult> results) throws IgniteCheckedException { // For the purpose of this example we simply // concatenate string representation of every // job result StringBuilder buf = new StringBuilder(); for (ComputeJobResult res : results) { // Append string representation of result // returned by every job. buf.append(res.getData().string()); } return buf.string(); } }
ComputeTaskSplitAdapter
hides the job-to-node mapping logic from
user and provides convenient ComputeTaskSplitAdapter.split(int, Object)
method for splitting task into sub-jobs in homogeneous environments.
Here is an example of how you would implement your task using ComputeTaskSplitAdapter
:
public class MyFooBarTask extends ComputeTaskSplitAdapter<Object, String> { @Override protected Collection<? extends ComputeJob> split(int gridSize, Object arg) throws IgniteCheckedException { List<MyFooBarJob> jobs = new ArrayList<MyFooBarJob>(gridSize); for (int i = 0; i < gridSize; i++) { jobs.add(new MyFooBarJob(arg)); } // Node assignment via load balancer // happens automatically. return jobs; } // Aggregate results into one compound result. public String reduce(List<ComputeJobResult> results) throws IgniteCheckedException { // For the purpose of this example we simply // concatenate string representation of every // job result StringBuilder buf = new StringBuilder(); for (ComputeJobResult res : results) { // Append string representation of result // returned by every job. buf.append(res.getData().string()); } return buf.string(); } }
Modifier and Type | Method and Description |
---|---|
Map<? extends ComputeJob,ClusterNode> |
map(List<ClusterNode> subgrid,
T arg)
This method is called to map or split grid task into multiple grid jobs.
|
R |
reduce(List<ComputeJobResult> results)
Reduces (or aggregates) results received so far into one compound result to be returned to
caller via
ComputeTaskFuture.get() method. |
ComputeJobResultPolicy |
result(ComputeJobResult res,
List<ComputeJobResult> rcvd)
Asynchronous callback invoked every time a result from remote execution is
received.
|
Map<? extends ComputeJob,ClusterNode> map(List<ClusterNode> subgrid, T arg) throws IgniteException
arg
- Task execution argument. Can be null
. This is the same argument
as the one passed into Grid#execute(...)
methods.subgrid
- Nodes available for this task execution. Note that order of nodes is
guaranteed to be randomized by container. This ensures that every time
you simply iterate through grid nodes, the order of nodes will be random which
over time should result into all nodes being used equally.ComputeTaskContinuousMapper
is
injected into task, if null
or empty map is returned, exception will be thrown to the user.IgniteException
- If mapping could not complete successfully. This exception will be
thrown out of ComputeTaskFuture.get()
method.ComputeJobResultPolicy result(ComputeJobResult res, List<ComputeJobResult> rcvd) throws IgniteException
ComputeJobResultPolicy
for more information about result policies.res
- Received remote grid executable result.rcvd
- All previously received results. Note that if task class has
ComputeTaskNoResultCache
annotation, then this list will be empty.IgniteException
- If handling a job result caused an error. This exception will
be thrown out of ComputeTaskFuture.get()
method.R reduce(List<ComputeJobResult> results) throws IgniteException
ComputeTaskFuture.get()
method.
Note, that if some jobs did not succeed and could not be failed over then the list of results passed into this method will include the failed results. Otherwise, failed results will not be in the list.
results
- Received results of broadcasted remote executions. Note that if task class has
ComputeTaskNoResultCache
annotation, then this list will be empty.IgniteException
- If reduction or results caused an error. This exception will
be thrown out of ComputeTaskFuture.get()
method.
GridGain In-Memory Computing Platform : ver. 8.9.14 Release Date : November 5 2024