Package org.apache.ignite.compute
Interface ComputeJob
-
- All Superinterfaces:
Serializable
- All Known Implementing Classes:
ComputeJobAdapter
,ComputeJobContinuationAdapter
public interface ComputeJob extends Serializable
Defines executable unit forComputeTask
.Description
Grid job is an executable unit ofComputeTask
. Grid task gets split into jobs whenComputeTask.map(List, Object)
method is called. This method returns all jobs for the task mapped to their corresponding grid nodes for execution. Grid will then serialize this jobs and send them to requested nodes for execution. When a node receives a request to execute a job, the following sequence of events takes place:-
If collision SPI is defined, then job gets put on waiting list which is passed to underlying
CollisionSpi
SPI. Otherwise job will be submitted to the executor service responsible for job execution immediately upon arrival. -
If collision SPI is configured, then it will decide one of the following scheduling policies:
- Job will be kept on waiting list. In this case, job will not get a chance to execute until next time the Collision SPI is called.
- Job will be moved to active list. In this case system will proceed with job execution.
-
Job will be rejected. In this case the
ComputeJobResult
passed intoComputeTask.result(ComputeJobResult, List)
method will containComputeExecutionRejectedException
exception. If you are using any of the task adapters shipped with Ignite, then job will be failed over automatically for execution on another node.
-
For activated jobs, an instance of distributed task session (see
ComputeTaskSession
) will be injected. -
System will execute the job by calling
execute()
method. -
If job gets cancelled while executing then
cancel()
method will be called. Note that just like withThread.interrupt()
method, grid job cancellation serves as a hint that a job should stop executing or exhibit some other user defined behavior. Generally it is up to a job to decide whether it wants to react to cancellation or ignore it. Job cancellation can happen for several reasons:- Collision SPI cancelled an active job.
- Parent task has completed without waiting for this job's result.
- User cancelled task by calling
IgniteFuture.cancel()
method.
-
Once job execution is complete, the return value will be sent back to parent
task and will be passed into
ComputeTask.result(ComputeJobResult, List)
method viaComputeJobResult
instance. If job execution resulted in a checked exception, thenComputeJobResult.getException()
method will contain that exception. If job execution threw a runtime exception or error, then it will be wrapped intoComputeUserUndeclaredException
exception.
Resource Injection
Grid job implementation can be injected using IoC (dependency injection) with ignite resources. Both, field and method based injection are supported. The following ignite resources can be injected:TaskSessionResource
JobContextResource
IgniteInstanceResource
LoggerResource
SpringApplicationContextResource
SpringResource
ComputeJobAdapter
Ignite comes with convenienceComputeJobAdapter
adapter that provides default empty implementation forcancel()
method and also allows user to set and get job argument, if there is one.Distributed Session Attributes
Jobs can communicate with parent task and with other job siblings from the same task by setting session attributes (seeComputeTaskSession
). Other jobs can wait for an attribute to be set either synchronously or asynchronously. Such functionality allows jobs to synchronize their execution with other jobs at any point and can be useful when other jobs within task need to be made aware of certain event or state change that occurred during job execution.Distributed task session can be injected into
ComputeJob
implementation using@TaskSessionResource
annotation. Both, field and method based injections are supported. Refer toComputeTaskSession
documentation for more information on session functionality.Saving Checkpoints
Long running jobs may wish to save intermediate checkpoints to protect themselves from failures. There are three checkpoint management methods:ComputeTaskSession.saveCheckpoint(String, Object, ComputeTaskSessionScope, long)
ComputeTaskSession.loadCheckpoint(String)
ComputeTaskSession.removeCheckpoint(String)
non-null
value is returned, then job can continue from where it failed last time, otherwise it would start from scratch. Throughout it's execution job should periodically save its intermediate state to avoid starting from scratch in case of a failure.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
cancel()
This method is called when system detects that completion of this job can no longer alter the overall outcome (for example, when parent task has already reduced the results).Object
execute()
Executes this job.
-
-
-
Method Detail
-
cancel
void cancel()
This method is called when system detects that completion of this job can no longer alter the overall outcome (for example, when parent task has already reduced the results). Job is also cancelled whenIgniteFuture.cancel()
is called.Note that job cancellation is only a hint, and just like with
Thread.interrupt()
method, it is really up to the actual job instance to gracefully finish execution and exit.
-
execute
Object execute() throws IgniteException
Executes this job.- Returns:
- Job execution result (possibly
null
). This result will be returned inComputeJobResult.getData()
method passed intoComputeTask.result(ComputeJobResult, List)
task method on caller node. - Throws:
IgniteException
- If job execution caused an exception. This exception will be returned inComputeJobResult.getException()
method passed intoComputeTask.result(ComputeJobResult, List)
task method on caller node. If execution produces aRuntimeException
orError
, then it will be wrapped intoIgniteCheckedException
.
-
-