Show / Hide Table of Contents

# Interface IComputeTask<TArg, TJobRes, TRes>

Ignite task interface defines a task that can be executed on the grid. Ignite task is responsible for splitting business logic into multiple Ignite jobs, receiving results from individual Ignite jobs executing on remote nodes, and reducing (aggregating) received jobs' results into final Ignite task result.

Upon request to execute a task, the system will do the following:

- Inject annotated resources into task instance.
- Apply Map(IList<IClusterNode>, TArg). This method is responsible for splitting business logic into multiple jobs (units of execution) and mapping them to Ignite nodes.
- System will send mapped Ignite jobs to their respective nodes.
- Once job execution results become available, OnResult(IComputeJobResult<TJobRes>, IList<IComputeJobResult<TJobRes>>) method will be called for ech received job result. The policy returned by this method will determine the way task reacts to every job result.

  If Wait is returned, task will continue to wait for other job results. If this result is the last job result, then reduce phase will be started.

  If Reduce is returned, reduce phase will be started right away without waiting for other jobs completion (all remaining jobs will receive cancel request).

  If Failover is returned, job will be failed over to another node for execution. Note that if you use ComputeTaskAdapter<TArg, TJobRes, TTaskRes>, it will automatically fail jobs to another node for 2 well-known failure cases: 1) job has failed to due to node crash (in this case Exception will return ClusterTopologyException); 2) job execution was rejected, i.e. remote node has cancelled job before it got a chance to execute, while it still was on the waiting list. (in this case Exception will return ComputeExecutionRejectedException).

- Once all results are received or OnResult(IComputeJobResult<TJobRes>, IList<IComputeJobResult<TJobRes>>) method returned Reduce policy, method Reduce(IList<IComputeJobResult<TJobRes>>) is called to aggregate received results into one final result. Once this method is finished the execution of the Ignite task is complete. This result will be returned to the user through future.

###### Namespace: Apache.Ignite.Core.Compute

###### Assembly: Apache.Ignite.Core.dll

##### Syntax

    public interface IComputeTask<in TArg, TJobRes, out TRes>

##### Type Parameters

Name | Description
--- | ---
TArg | Argument type.
TJobRes | Type of job result.
TRes | Type of final task result after reduce.

### Methods

#### Map(IList<IClusterNode>, TArg)

This method is called to map or split Ignite task into multiple Ignite jobs. This is the first method that gets called when task execution starts.

##### Declaration

    IDictionary<IComputeJob<TJobRes>, IClusterNode> Map(IList<IClusterNode> subgrid, TArg arg)

##### Parameters

Type | Name | Description
--- | --- | ---
System.Collections.Generic.IList<IClusterNode> | 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 Ignite nodes, the order of nodes will be random which over time should result into all nodes being used equally.
TArg | arg | Task execution argument. Can be `null`. This is the same argument as the one passed into `ICompute.Execute()` methods.

##### Returns

Type | Description
--- | ---
System.Collections.Generic.IDictionary<IComputeJob<TJobRes>, IClusterNode> | Map of Ignite jobs assigned to subgrid node. If `null` or empty map is returned, exception will be thrown.

#### OnResult(IComputeJobResult<TJobRes>, IList<IComputeJobResult<TJobRes>>)

Asynchronous callback invoked every time a result from remote execution is received. It is ultimately upto this method to return a policy based on which the system will either wait for more results, reduce results received so far, or failover this job to another node. See ComputeJobResultPolicy for more information.

##### Declaration

    ComputeJobResultPolicy OnResult(IComputeJobResult<TJobRes> res, IList<IComputeJobResult<TJobRes>> rcvd)

##### Parameters

Type | Name | Description
--- | --- | ---
IComputeJobResult<TJobRes> | res | Received remote Ignite executable result.
System.Collections.Generic.IList<IComputeJobResult<TJobRes>> | rcvd | All previously received results. Note that if task class has ComputeTaskNoResultCacheAttribute attribute, then this list will be empty.

##### Returns

Type | Description
--- | ---
ComputeJobResultPolicy | Result policy that dictates how to process further upcoming job results.

#### Reduce(IList<IComputeJobResult<TJobRes>>)

Reduces (or aggregates) results received so far into one compound result to be returned to caller via future.

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.

##### Declaration

    TRes Reduce(IList<IComputeJobResult<TJobRes>> results)

##### Parameters

Type | Name | Description
--- | --- | ---
System.Collections.Generic.IList<IComputeJobResult<TJobRes>> | results | Received job results. Note that if task class has ComputeTaskNoResultCacheAttribute attribute, then this list will be empty.

##### Returns

Type | Description
--- | ---
TRes | Task result constructed from results of remote executions.