Dashboard > GridGain User Guide > Table Of Contents > Developers Guide > Grid Task Coding Guidelines
Grid Task Coding Guidelines
Added by architect, last edited by morpheus on Dec 23, 2008  (view change)
Labels: 
(None)


There are certain known patterns and anti-patterns to be aware of when developing grid task and jobs. This page will list all of them as we know them at the point and we will be adding more as we are learning about them:

Serialization and Deserialization

Jobs created by task are moved from one grid node to another (see [Creating Grid Tasks And Grid Jobs]). Before sending they are serialized into the byte stream and thus need to implement java.io.Serializable interface. On remote node every job is deserialized with a class loader that depends on deployment method (see Grid Deployment).

Every class member (including super classes) except for static members need to implement java.io.Serializable to have entire job to be serializable. Static class members will not be sent to remote node and should be initialized on remote node. Note also that task parameters passed into GridJob.execute() Javadoc method are sent to remote nodes and need to implement java.io.Serializable as well.

Inner and Anonymous Classes

Any kind of inner classes or anonymous classes are allowed. Write your code as you usually do and GridGain will distribute it. You can implement your job as anonymous class within grid task class and use task class members inside your job. Here is an example of anonymous job:

01 import java.io.*;
02 import java.util.*;
03 import org.gridgain.grid.*;
04
05 /**
06  * Test task with anonymous job which uses method scope variable. 
08  */
09 public class TestGridTask extends GridTaskSplitAdapter<String> {
10     /** Dummy multiplier. */
11     private int multiplier = 3;
12
13     /**
14      * This method is responsible for splitting a task into multiple jobs.
15      */
16     @Override
17     protected Collection<? extends GridJob> split(int gridSize, final String arg) throws GridException {
18         List<GridJobAdapter<String>> jobs = new ArrayList<GridJobAdapter<String>>(gridSize);
19        
20         for (int i = 0; i < gridSize; i++) {
21             jobs.add(new GridJobAdapter<String>() {
22                 /**
23                  * Every job simply multiplies number of characters in the argument by some multiplier.
24                  */
25                 public Serializable execute() throws GridException {
26                     return multiplier * arg.length();
27                 }
28             });
29         }
30        
31         return jobs;
32     }
33
34     /**
35      * Reduces multiple job results into one task result.
36      */
37     public Object reduce(List<GridJobResult> results) throws GridException {
38         int sum = 0;
39           
40         // For the sake of this example, let's sum all results.
41         for (GridJobResult res : results) {
42             sum += (Integer)res.getData();
43         }
44
45         return sum;
46     }
47 }

Here we have anonymous job class created at line 21 which uses method-scope variable arg of task class declared in method signature at line 17 and used in job at line 26 as well as task class member multiplier declared at line 11 and used at line 26.

Overriding Methods with Gridify Annotation

If you have following code:

public class A {
    @Gridify
    protected methodA() {
        ...
    }
};

public class B extends A {
    @Override
    protected methodA() {
        ...
        super.methodA();
        ...
    }
}

and use aspects you should get B.methodA() called twice, first on your local node and second time on remote node regardless of class or method modifiers. This is a feature of aspects implementation and we don't recommend to use @Gridify in parent classes.

Here is step by step explanation:

  1. You create object of class B.
  2. You make a call to B.methodA() and since this method does not have annotation in class B aspects will not work.
  3. Your B.methodA() executes and it calls super.methodA()
  4. A.methodA() has annotation and thus aspect will call GridGain and distribute your object of class B and method call to a grid node.
  5. On the grid node (local or remote) B.methodA() will be called (note that you have object of class B) again.
  6. Your B.methodA() executes and it calls super.methodA()
  7. Method A.methodA() has annotation but GridGain will catch this situation and it won't be distributed twice but instead will be just called.

As you can see we have 2 executions of B.methodA() and only one A.methodA().

Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.10 Build:#528 Nov 29, 2006) - Bug/feature request - Contact Administrators