Dashboard > GridGain User Guide > Table Of Contents > Developers Guide > Load Balancing SPI
Load Balancing SPI
Added by morpheus, last edited by morpheus on Nov 12, 2008  (view change)
Labels: 
(None)


Click on Javadoc link to open Javadoc documentation.

Package

org.gridgain.grid.spi.loadbalancing Javadoc

Available starting with GridGain

Built-in Implementations

GridGain comes with following implementations for load balancing that cover most popular strategies:

Description

GridLoadBalancingSpi Javadoc 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) Javadoc invocation. By providing different load balancing SPI you can enable different load balancing algorithms for your tasks.

Coding Examples

If you are using GridTaskSplitAdapter Javadoc 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 <i>and</i> use, for example, affinity load balancing for some other jobs within task, then you should use GridTaskAdapter Javadoc . Here is an example of how your task could look. Note that in this case we manually inject load balancer (using GridLoadBalancerResource Javadoc 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().toString());
        }

        return buf.toString();
    }
}

Configuration

GridLoadBalancingSpi Javadoc is provided in Configuring GridGain passed into GridFactory Javadoc at startup. You can configure a different load balancing SPI implementation as follows

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 Javadoc interface is just a bean and can also be configured using spring XML configuration.


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.

GridAdaptiveLoadBalancingSpi (GridGain User Guide)
GridAffinityLoadBalancingSpi (GridGain User Guide)
GridCoherenceLoadBalancingSpi (GridGain User Guide)
GridRoundRobinLoadBalancingSpi (GridGain User Guide)
GridWeightedRandomLoadBalancingSpi (GridGain User Guide)

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