GridGain™ 2.1.0
Java API Specification

org.gridgain.grid
Interface GridLoadBalancer


@Apache20LicenseCompatible
public interface GridLoadBalancer

Load balancer is used for finding the best balanced node according to load balancing policy. Internally load balancer will query the GridLoadBalancingSpi to get the balanced node.

Load balancer can be used explicitly from inside GridTask.map(java.util.List, Object) method when you implement GridTask interface directly or use GridTaskAdapter. If you use GridTaskSplitAdapter then load balancer is accessed implicitly by the adapter so you don't have to use it directly in your logic.

Coding Examples

If you are using GridTaskSplitAdapter then load balancing logic is transparent to your code and is handled automatically by the adapter. Here is an example of how your task will look:
 public class MyFooBarTask extends GridTaskSplitAdapter<String> {
     @Override
     protected Collection<? extends GridJob> split(int gridSize, String arg) throws GridException {
         List<MyFooBarJob> jobs = new ArrayList<MyFooBarJob>(gridSize);
 
         for (int i = 0; i < gridSize; i++) {
             jobs.add(new MyFooBarJob(arg));
         }
  
         // Node assignment via load balancer 
         // happens automatically.
         return jobs;
     }
     ...
 } 
 
If you need more fine-grained control over how some jobs within task get mapped to a node and use affinity load balancing for some other jobs within task, then you should use GridTaskAdapter. Here is an example of how your task will look. Note that in this case we manually inject load balancer and use it to pick the best node. Doing it in such way would allow user to map some jobs manually and for others use load balancer.
 public class MyFooBarTask extends GridTaskAdapter<String, String> {
     // Inject load balancer.
     @GridLoadBalancerResource
     GridLoadBalancer balancer;
 
     // Map jobs to grid nodes.
     public Map<? extends GridJob, GridNode> map(List<GridNode> subgrid, String arg) throws GridException {
         Map<MyFooBarJob, GridNode> jobs = new HashMap<MyFooBarJob, GridNode>(subgrid.size());
 
         // In more complex cases, you can actually do
         // more complicated assignments of jobs to nodes.
         for (int i = 0; i < subgrid.size(); i++) {
             // Pick the next best balanced node for the job.
             GridJob myJob = new MyFooBarJob(arg);
             
             jobs.put(myJob, balancer.getBalancedNode(myJob));
         }
 
         return jobs;
     }
 
     // Aggregate results into one compound result.
     public String reduce(List<GridJobResult> results) throws GridException {
         // For the purpose of this example we simply
         // concatenate string representation of every 
         // job result
         StringBuilder buf = new StringBuilder();
 
         for (GridJobResult res : results) {
             // Append string representation of result
             // returned by every job.
             buf.append(res.getData().toString());
         }
 
         return buf.toString();
     }
 } 
 




See Also:

  Documentation
  Email Support
  Online Forums
  Issue Tracking

Author:   2005-2008 Copyright © GridGain Systems. All Rights Reserved. ver. 2.1.0

 

Method Summary
 GridNode getBalancedNode(GridJob job, GridNode... excludeNodes)
          Gets the next balanced node according to the underlying load balancing policy.
 

Method Detail

getBalancedNode

GridNode getBalancedNode(GridJob job,
                         GridNode... excludeNodes)
                         throws GridException
Gets the next balanced node according to the underlying load balancing policy.

Throws:
GridException - If any error occurred when finding next balanced node.
Parameters:
job - Job to get the balanced node for.
excludeNodes - List of nodes that should be excluded from balanced nodes.
Returns:
Next balanced node.

GridGain™ 2.1.0
Java API Specification

GridGain™ - Grid Computing Made Simple, ver. 2.1.0.19122008
2005-2008 Copyright © GridGain Systems. All Rights Reserved.