Cloud Zone is brought to you in partnership with:

Turik Campbell is an enterprise software engineer and technology evangelist. He is passionate about utilizing technology to solve real world problems. His many interests include server-side Java technologies, distributed systems, portals and more. He is also a Sun Certified Java Programmer. Turik has posted 1 posts at DZone. View Full User Profile

OCLOUD SDK for Gridgain: Cloud App Development Made Easy

11.14.2011
| 3583 views |
  • submit to reddit

Abstract:

Gridgain is software middleware that unifies compute and data grid capabilities to provide a framework for building high performance cloud applications.  OCLOUD SDK Express is an Eclipse Java Plugin which leverages the power of Gridgain to provide a wizard style interface to building Gridgain centric applications. This article will provide an introduction to developing applications with OCLOUD SDK Express for Gridgain 3.2.1

Compute Grids

For the purpose of this article, our focus will be Gridgain's compute grid functionality. Compute Grids are concerned with parallelizing business logic. Gridgain implements a well known parallel design pattern called MapReduce for new and existing applications.

MapReduce

The following steps further describe the MapReduce design pattern:

  1. Tasks (GridTask) are split into subtasks (GridJobs)

  2. GridJobs are mapped and shipped to various nodes for parallel processing

  3. Upon completion, results of subtasks are returned.

  4. All results from subtasks are aggregated by GridTask into a final result.

Flexible Program Model

Gridgain's program model is very flexible as it offers multiple approaches to developing a distributive applications. The various approaches include

     1. Direct Grid Task and Grid Jobs approach

     2. AOP base grid enabling with @Gridify notation

     3. Utilization of Gridgain Functional API's

For this article, we will elect to utilize the direct GridTask/Grid Jobs approach as this provides the most control over our programming logic.

GridTask and GridJob

GridTask and GridJob objects are the major abstractions utilized in Gridgain's compute grid architecure. The GridTask is responsible for overall map/reduce logic processing, while the GridJob represents the unit of work to be mapped/shipped to various nodes for execution.

Prerequisites:

Before you get started you will have to go to Gridgain and NetMillennium websites to download

the latest release of Gridgain and OCLOUD SDK respectively. The OCLOUD SDK Express plugin supports Gridgain 3.x and above. Fortunately, both installations are fairly trivial.

Gridgain Installation

     1. Download GridGain ZIP archive from http://www.gridgain.com/downloads.shtml    

     2. Extract .zip file into an installation folder in your system

     3. Set GRIDGAIN_HOME environment variable to point to installation folder

 OCLOUD SDK Express Installation

Go to download the Netmillennium site to download OCLOUD SDK Express

  1. Close Eclipse if already running.

  2. Copy OCLOUDSDK_1.0.0.<build>.jar plugin to Eclipse dropins folder

  3. Restart Eclipse

Configure OCLOUD SDK Express for Gridgain

  1. Configure the Gridgain installation path.
    a. Go to Window..Preferences
    b. Go to OCLOUDSDK..Settings.
    c. Select Use default GRIDGAIN_HOME system variable if it is properly set or simply select a Gridgain Home installation path .

  2. Click Apply. Click OK


 

HelloWorld Application using OCLOUD SDK for Gridgain

Ok, Now we are ready to begin developing with Gridgain and OCLOUD SDK Express. We will create an application that simply prints a message on multiple computers.  Follow the directions in the sections below to create your application. 

How to create a Gridgain Project

  1. Create a Standard Java Project.  For this example we named our project ''mygg321proj".

  2. Select OCLOUD SDK..Add/Remove Gridgain Capabilities menu item from menu bar.

  3. Next, select the Browse button to select a corresponding project.

  4. Select Finish.

  5. All required .jar files will be added to the project build path.







How to create a GridTask
  1. Select OCLOUD SDK...New GridTask menu item from main menu

  2. Select a project source folder where the GridTask will reside.

  3. Fill in the required fields:
    a. Name: Enter 'HelloWorldGridTaskSplitAdapter'
    b. Superclass: Enter 'GridTaskSplitAdapter'

  4. Fill in the optional fields for Task SPI's and Injectable types.  

           Select GridInstanceResource and GridLoggerResource types.

  5. Select the Generate checkbox to create a Java Main class that will be responsible for  

         executing GridTask

  6. Select Finish. A minimal GridTask class will be generated as seen in Listing 1a,

         as well as a 'starter' class (Listing 1b) which is responsible for executing the GridTask.

 

Listing 1a: HelloWorldGridTaskSplitAdapter.java

package tasks;

import java.util.Collection;
import java.util.List;

import org.gridgain.grid.Grid;
import org.gridgain.grid.GridException;
import org.gridgain.grid.GridJobResult;
import org.gridgain.grid.GridTaskSplitAdapter;
import org.gridgain.grid.logger.GridLogger;
import org.gridgain.grid.resources.GridInstanceResource;
import org.gridgain.grid.resources.GridLoggerResource;

	public class HelloWorldGridTaskSplitAdapter extends
			GridTaskSplitAdapter<Object, Object> {

		/**
		 * @GridInstanceResource is a Grid instance for executing and deploying tasks, sending messages.
		 **/
		@GridInstanceResource
		private Grid grid;

		/**
		 * @GridLoggerResource is a GridLogger which provides logging functionality.
		 **/
		@GridLoggerResource
		private GridLogger gridLogger;

		public Object reduce(List<GridJobResult> arg0) throws GridException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		protected Collection split(int gridSize, Object arg1)
				throws GridException {
			// TODO Auto-generated method stub
			return null;
		}

	}

Line 20

   @GridInstanceResource represents a grid node where the GridJob will execute.

Line 26

   @GridLoggerResource represents a Logger object to capture important information.

 

Listing 1b: HelloGridTaskSplitAdapterExample.java

package tasks;

import org.gridgain.grid.Grid;
import org.gridgain.grid.GridException;
import org.gridgain.grid.typedef.G;

	public class HelloGridTaskSplitAdapterExample {

		public static void main(String[] args) throws GridException {
			if (args.length == 0) {
				G.start();
			} else {
				G.start(args[0]);
			}

			try {
				Grid grid = G.grid();
				//TO DO: Execute GridTask here

			} finally {
				G.stop(true);
			}
		}

	}

Lines 10-14

 These lines are responsible for starting the Gridgain runtime.

Lines 17-22

 These lines are responsible for stopping the Gridgain runtime.

 

Listing 2a: HelloWorldGridTaskSplitAdapter.java (Completed Program)

package tasks;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;

import org.gridgain.grid.GridException;
import org.gridgain.grid.GridJob;
import org.gridgain.grid.GridJobAdapterEx;
import org.gridgain.grid.GridJobResult;
import org.gridgain.grid.GridTaskSplitAdapter;
import org.gridgain.grid.logger.GridLogger;
import org.gridgain.grid.resources.GridHomeResource;
import org.gridgain.grid.resources.GridLocalNodeIdResource;
import org.gridgain.grid.resources.GridLoggerResource;

	public class HelloWorldGridTaskSplitAdapter extends
			GridTaskSplitAdapter<Object, Object> {

		/**
		 * @GridHomeResource represents installation path of the current grid executing this task.
		 **/
		@GridHomeResource
		private String gridHomeResource;

		/**
		 * @GridLoggerResource is a GridLogger which provides logging functionality.
		 **/
		@GridLoggerResource
		private GridLogger log;

		/**
		 * @GridLocalNodeIdResource annotates a field or a setter method for injection of local node UUID resource. Node UUID    
		 * is a globally unique node identifier and is provided to grid via GridConfiguration.
		 **/
		@GridLocalNodeIdResource
		private UUID gridLocalNodeId;

		public Integer reduce(List<GridJobResult> arg0) throws GridException {
			// TODO Auto-generated method stub
			 return null;

		}

		@Override
		protected Collection split(int gridSize, Object arg1)
				throws GridException {
			// TODO Auto-generated method stub
			
			// Split the passed sentence into multiple words delimited by spaces.
	        String[] words = ((String)arg1).split(" ");

	        List<GridJob> jobs = new ArrayList<GridJob>(words.length);

	        for (String word : words) {
	            // Every job gets its own word as an input.
	            jobs.add(new GridJobAdapterEx(word) {
	                /*
	                 * Prints out the nodeId and word passed into respective job.
	                 * 
	                 */
	                public Serializable execute() {
	                    String word = argument(0);

	                    if (log.isInfoEnabled() == true) {
	                        log.info("***");
	                        log.info("*** Printing '" + word + "' on nodeId " + gridLocalNodeId + " from grid job.");
	                        log.info("***");
	                    }

	                    // Return number of letters in the word.
	                    return word.length();
	                }
	            });
	        }

	        return jobs;
			
		}

	}

Lines 48-79

 The split method is responsible for creating a GridJob for each one of the input parameters.

 Each GridJob will print out a word from the sentence.

 

Listing 2b: HelloWorldGridTaskSplitAdapterExample.java (Completed Program)

package tasks;

import org.gridgain.grid.Grid;
import org.gridgain.grid.GridException;
import org.gridgain.grid.GridTaskFuture;
import org.gridgain.grid.typedef.G;

	public class HelloWorldGridTaskSplitAdapterExample {

		public static void main(String[] args) throws GridException {
			if (args.length == 0) {
				G.start();
			} else {
				G.start(args[0]);
			}

			try {
				Grid grid = G.grid();
				 // Execute task.
		        GridTaskFuture<Object> future = grid.execute(HelloWorldGridTaskSplitAdapter.class, "OCLOUD SDK Express for Gridgain is easy to use!");
		        
		        // Wait for task completion.
		        Object  result = future.get();

			} finally {
				G.stop(true);
			}
		}

	}

 Lines 20-23

  These lines are necessary to execute GridTask.

To execute this example, simply startup a few stand alone GridGain nodes by executing GRIDGAIN_HOME/bin/ggstart  script.  Next run the HelloWorldGridTaskSplitAdapterExample main class.  You will see various words from the phrase printed out on each Gridgain node.

 

Published at DZone with permission of its author, Turik Campbell.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)