Gridify With Checkpoint
This example contains HelloWorld example that is using Gridify ![]()
String 'Hello World' is passed as an argument to GridifyHelloWorldCheckpointExample.sayIt(String) method. Since this method is annotated with @Gridify ![]()
- Save checkpoint with key 'fail' and value 'true'.
- Pass the passed in string as an argument into remote job for execution.
- The job will check the value of checkpoint with key 'fail'. If it is true, then it will set it to false and throw exception to simulate a failure. If it is false, then it will execute the grid-enabled method.
Note that when job throws an exception it will be treated as a failure by GridifyHelloWorldCheckpointTask.result(GridJobResult, List) method which will return GridJobResultPolicy.FAILOVER ![]()
The possible outcome will look as following:
| Node 1 (failure occured on this node) Exception: |
| Node 2 (job was failed over to this node) [15:15:57,549][INFO ][GridFailoverManager] Resolving failover [jobRes=org.gridgain.grid.kernal.GridJobResultImpl ... ] |
Package:
org.gridgain.examples.helloworld.gridify.checkpoint
There are two classes implemented for this example:
AspectJ AOP Configuration
We will use AspectJ AOP for this example. To use other AOP implementations (such as JBoss AOP, or Spring AOP), refer to AOP Configuration documentation.
The following configuration needs to be applied to enable AspectJ byte code weaving.
- JVM configuration should include: -javaagent:[GRIDGAIN_HOME]/libs/aspectjweaver-1.5.3.jar
- Classpath should contain the [GRIDGAIN_HOME]/config/aop/aspectj folder.
Running Grid Node
This example will need one remote node to be running. Note that you don't need another machine for it - you can start remote node on the same machine you are running example on.
To start a remote node open the terminal window on Linux/Mac OS X or Command Prompt on Windows, change directory to ${GRIDGAIN_HOME}/bin and run the gridgain.{sh|bat} script. It takes 2-3 seconds for grid node to start and if everything worked fine you should see starting log ending with successful start acknowledgment.
GridifyHelloWorldCheckpointExample.java
1. Import GridGain classes.
import org.gridgain.grid.*; import org.gridgain.grid.gridify.*;
2. Add Grid Start and Stop.
GridFactory.start();
try {
...
}
finally {
GridFactory.stop(true);
}
finally clause allows for graceful grid shutdown in case of the exceptions.
3. Add Gridify Annotation.
Method grid-enabled with @Gridify ![]()
@Gridify(taskClass = GridifyHelloWorldCheckpointTask.class, timeout = 60000) public static int sayIt(String phrase) { // Simply print out the argument. System.out.println(">>>"); System.out.println(">>> Printing '" + phrase + "' on this node from grid-enabled method."); System.out.println(">>>"); return phrase.length(); }
Full Source Code
package org.gridgain.examples.helloworld.gridify.checkpoint; import org.gridgain.grid.*; import org.gridgain.grid.gridify.*; import org.gridgain.grid.gridify.aop.spring.*; public final class GridifyHelloWorldCheckpointExample { /** * Enforces singleton. */ private GridifyHelloWorldCheckpointExample() { // No-op. } /** * Method grid-enabled with {@link Gridify} annotation. Simply prints * out the argument passed in. * * @param arg String to print. */ @Gridify(taskClass = GridifyHelloWorldCheckpointTask.class, timeout = 60000) public static int sayIt(String phrase) { // Simply print out the argument. System.out.println(">>>"); System.out.println(">>> Printing '" + phrase + "' on this node from grid-enabled method."); System.out.println(">>>"); return phrase.length(); } /** * Execute <tt>HelloWorld</tt> example grid-enabled with <tt>Gridify</tt> annotation. * * @param args Command line arguments, none required but user may * set configuration file path as the only parameter. For GigaSpaces * checkpoint SPI user should pass <tt>"examples/config/gigaspaces.xml"</tt> * as VM configuration parameter. * @throws GridException If example execution failed. */ public static void main(String[] args) throws GridException { if (args.length == 0) { GridFactory.start(); } else { GridFactory.start(args[0]); } try { // This method will be executed on a remote grid node. int phraseLen = sayIt("Hello World"); System.out.println(">>>"); System.out.println(">>> Finished executing Gridify \"Hello World\" example with checkpoints."); System.out.println(">>> Total number of characters in the phrase is '" + phraseLen + "'."); System.out.println(">>> You should see exception stack trace from failed job on one node."); System.out.println(">>> Failed job will be failed over to another node."); System.out.println(">>> You should see print out of 'Hello World' on another node."); System.out.println(">>> Check all nodes for output (this node is also part of the grid)."); System.out.println(">>>"); } finally { GridFactory.stop(true); } } }
GridifyHelloWorldCheckpointTask.java
1. Import GridGain classes.
import org.gridgain.grid.gridify.*; import org.gridgain.grid.*; import org.gridgain.grid.resources.*;
2. Split Logic.
This is a grid task implementation that is responsible for split and aggregate (a.k.a map/reduce) logic. Note that this implementation uses GridifyTaskSplitAdapter ![]()
- Save checkpoint with key 'fail' and value 'true'.
- Pass the passed in string as an argument into remote job for execution.
- The job will check the value of checkpoint with key 'fail'. If it is true, then it will set it to false and throw exception to simulate a failure. If it is false, then it will execute the grid-enabled method.
3. Result Logic.
Method GridTask.result(GridJobResult, List) is invoked every time a result from remote job execution is received (see GridTask ![]()
![]()
![]()
Full Source Code
package org.gridgain.examples.helloworld.gridify.checkpoint; import org.gridgain.grid.*; import org.gridgain.grid.resources.*; import org.gridgain.grid.gridify.*; import java.util.*; import java.io.*; /** * This grid task demonstrates some basic usage of task session checkpoints and failover. * It does the following: * <ol> * <li>Save checkpoint with key '<tt>fail</tt>' and value '<tt>true</tt>'.</li> * <li>Pass the passed in string as an argument into remote job for execution.</li> * <li> * The job will check the value of checkpoint with key '<tt>fail</tt>'. If it * is <tt>true</tt>, then it will set it to <tt>false</tt> and throw * exception to simulate a failure. If it is <tt>false</tt>, then * it will execute the grid-enabled method. * </li> * </ol> * Note that when job throws an exception it will be treated as a failure * by {@link #result(GridJobResult,List)} method which will return * {@link GridJobResultPolicy#FAILOVER} policy. This will cause the job to * automatically failover to another node for execution. The new job will * simply print out the argument passed in. */ public class GridifyHelloWorldCheckpointTask extends GridifyTaskSplitAdapter<Integer> { /** Injected task session. */ @GridTaskSessionResource private GridTaskSession taskSes = null; /** * {@inheritDoc} */ @Override public Collection<? extends GridJob> split(int gridSize, GridifyArgument arg) throws GridException { // Make reasonably unique checkpoint key. final String cpKey = getClass().getName() + arg; taskSes.saveCheckpoint(cpKey, true); String phrase = ((String)arg.getMethodParameters()[0]); return Collections.singletonList(new GridJobAdapter<String>(phrase) { /** Injected distributed task session. */ @GridTaskSessionResource private GridTaskSession jobSes = null; /** * The job will check the checkpoint with key '<tt>fail</tt>' and if * it's <tt>true</tt> it will throw exception to simulate a failure. * Otherwise, it will execute the grid-enabled method. */ public Serializable execute() throws GridException { boolean fail = (Boolean)jobSes.loadCheckpoint(cpKey); if (fail == true) { jobSes.saveCheckpoint(cpKey, false); throw new GridException("Example job exception."); } // Execute gridified method. // Note that since we are calling this method from within the grid job // AOP-based grid enabling will not cross-cut it and method will just // execute normally. return GridifyHelloWorldCheckpointExample.sayIt(getArgument()); } }); } /** * To facilitate example's logic, returns {@link GridJobResultPolicy#FAILOVER} * policy in case of any exception. * * @param result Job result. * @param received All previously received results. * @throws GridException {@inheritDoc} * @return {@inheritDoc} */ @Override public GridJobResultPolicy result(GridJobResult result, List<GridJobResult> received) throws GridException { return result.getException() != null ? GridJobResultPolicy.FAILOVER : GridJobResultPolicy.WAIT; } /** * Sums up all characters from all jobs and returns a * total number of characters in the initial phrase. * * @param results Job results. * @return Number of letters for the phrase passed into * {@link GridifyHelloWorldCheckpointExample#sayIt(String)} method. * @throws GridException If reduce failed. */ public Integer reduce(List<GridJobResult> results) throws GridException { // We only had one job in the split. Therefore, // we only have one result. Integer charCnt = results.get(0).getData(); // Total number of characters in the phrase // passed into task execution. return charCnt; } }
