Defines a distributed session for particular task execution.
Description
This interface defines a distributed session that exists for particular task
execution. Task session is distributed across the parent task and all grid
jobs spawned by it, so attributes set on a task or on a job can be viewed on
other jobs. Correspondingly attributes set on any of the jobs can also be
viewed on a task.
Session has 2 main features:
attribute and
checkpointmanagement. Both attributes and checkpoints can be used from task itself and
from the jobs belonging to this task. Session attributes and checkpoints can
be set from any task or job methods. Session attribute and checkpoint consistency
is fault tolerant and is preserved whenever a job gets failed over to
another node for execution. Whenever task execution ends, all checkpoints
saved within session with
GridComputeTaskSessionScope#SESSION_SCOPE scope
will be removed from checkpoint storage. Checkpoints saved with
GridComputeTaskSessionScope#GLOBAL_SCOPE will outlive the session and
can be viewed by other tasks.
The sequence in which session attributes are set is consistent across
the task and all job siblings within it. There will never be a
case when one job sees attribute A before attribute B, and another job sees
attribute B before A. Attribute order is identical across all session
participants. Attribute order is also fault tolerant and is preserved
whenever a job gets failed over to another node.
Connected Tasks
Note that apart from setting and getting session attributes, tasks or
jobs can choose to wait for a certain attribute to be set using any of
the
waitForAttribute(...) methods. Tasks and jobs can also
receive asynchronous notifications about a certain attribute being set
through
GridComputeTaskSessionAttributeListener listener. Such feature
allows grid jobs and tasks remain
connected in order
to synchronize their execution with each other and opens a solution for a
whole new range of problems.
Imagine for example that you need to compress a very large file (let's say
terabytes in size). To do that in grid environment you would split such
file into multiple sections and assign every section to a remote job for
execution. Every job would have to scan its section to look for repetition
patterns. Once this scan is done by all jobs in parallel, jobs would need to
synchronize their results with their siblings so compression would happen
consistently across the whole file. This can be achieved by setting
repetition patterns discovered by every job into the session. Once all
patterns are synchronized, all jobs can proceed with compressing their
designated file sections in parallel, taking into account repetition patterns
found by all the jobs in the split. Grid task would then reduce (aggregate)
all compressed sections into one compressed file. Without session attribute
synchronization step this problem would be much harder to solve.
Session Injection
Session can be injected into a task or a job using IoC (dependency
injection) by attaching
GridTaskSessionResourceannotation to a field or a setter method inside of
GridComputeTask or
GridComputeJob implementations as follows:
...
// This field will be injected with distributed task session.
@GridTaskSessionResource
private GridComputeTaskSession ses;
...
or from a setter method:
// This setter method will be automatically called by the system
// to set grid task session.
@GridTaskSessionResource
void setSession(GridComputeTaskSession ses) {
this.ses = ses;
}
Example
To see example on how to use
GridComputeTaskSession refer to
HelloWorld Distributed Task Session Example
on GitHub.