Click on Javadoc link to open Javadoc documentation.
Package
org.gridgain.grid.spi.loadbalancing ![]()
Available starting with GridGain 
Built-in Implementations
GridGain comes with following implementations for load balancing that cover most popular strategies:
- GridAdaptiveLoadBalancingSpi
- GridAffinityLoadBalancingSpi
- GridCoherenceLoadBalancingSpi
- GridRoundRobinLoadBalancingSpi
- GridWeightedRandomLoadBalancingSpi
Description
Starting with GridGain 2.1 you can provide multiple instances of Load Balancing SPIs and then specify which one to use on per-task level via @GridTaskSpis ![]()
GridLoadBalancingSpi ![]()
![]()
Coding Examples
If you are using GridTaskSplitAdapter ![]()
public class MyFooBarTask extends GridTaskSplitAdapter<Object,Object> { @Override protected Collection<? extends GridJob> split(int gridSize, Object 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 <i>and</i> use, for example, affinity load balancing for some other jobs within task, then you should use GridTaskAdapter ![]()
![]()
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. jobs.put(new MyFooBarJob(arg), balancer.getBalancedNode()) } 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(); } }
Configuration
GridLoadBalancingSpi ![]()
![]()
GridAffinityLoadBalancingSpi spi = new GridAffnityLoadBalancingSpi(); // Change number of virtual nodes. spi.setVirtualNodeCount(1500); GridConfigurationAdapter cfg = new GridConfigurationAdapter(); // Override default load balancing SPI. cfg.setLoadBalancingSpi(spi); // Start grid. GridFactory.start(cfg);
Note that GridConfiguration ![]()

For more information about using Spring framework for configuration click here.
Default Implementation
If no collision SPI is provided in configuration by default GridRoundRobinLoadBalancingSpi is used.


