GridGain™ 3.6.0e
Enterprise Edition

org.gridgain.grid.spi.loadbalancing
Interface GridLoadBalancingSpi

All Superinterfaces:
GridSpi, GridSpiJsonConfigurable
All Known Implementing Classes:
GridAdaptiveLoadBalancingSpi, GridRoundRobinLoadBalancingSpi, GridWeightedRandomLoadBalancingSpi

public interface GridLoadBalancingSpi
extends GridSpi, GridSpiJsonConfigurable

Load balancing SPI provides the next best balanced node for job execution. This SPI is used either implicitly or explicitly whenever a job gets mapped to a node during GridTask.map(List, Object) invocation.

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 could look:
 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 and use, for example, affinity load balancing for some other jobs within task, then you should use GridTaskAdapter. Here is an example of how your task could 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.
            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().string());
        }

        return buf.string();
    }
 }
 

GridGain comes with the following load balancing SPI implementations out of the box:

NOTE: this SPI (i.e. methods in this interface) should never be used directly. SPIs provide internal view on the subsystem and is used internally by GridGain kernal. In rare use cases when access to a specific implementation of this SPI is required - an instance of this SPI can be obtained via Grid.configuration() method to check its configuration properties or call other non-SPI methods. Note again that calling methods from this interface on the obtained instance can lead to undefined behavior and explicitly not supported.

Wiki & Forum:


Wiki
Forum

 

Method Summary
 GridNode getBalancedNode(GridTaskSession ses, List<GridNode> top, GridJob job)
          Gets balanced node for specified job within given task session.
 
Methods inherited from interface org.gridgain.grid.spi.GridSpi
getName, getNodeAttributes, onContextDestroyed, onContextInitialized, spiStart, spiStop
 
Methods inherited from interface org.gridgain.grid.spi.GridSpiJsonConfigurable
setJson
 

Method Detail

getBalancedNode

GridNode getBalancedNode(GridTaskSession ses,
                         List<GridNode> top,
                         GridJob job)
                         throws GridException
Gets balanced node for specified job within given task session.

Throws:
GridException - If failed to get next balanced node.
Parameters:
ses - Grid task session for currently executing task.
top - Topology of task nodes from which to pick the best balanced node for given job.
job - Job for which to pick the best balanced node.
Returns:
Best balanced node for the given job within given task session.

GridGain™ 3.6.0e
Enterprise Edition

GridGain - Real Time Big Data
Enterprise Edition, ver. 3.6.0e.13012012
2012 Copyright © GridGain Systems
Follow us:   Follow GridGain on Github Follow GridGain on Facebook Join GridGain User Group Follow GridGain on Twitter Follow GridGain on YouTube