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


Click on Javadoc link to open Javadoc documentation.

Package

org.gridgain.grid.spi.loadbalancing.affinity Javadoc

Available starting with GridGain

Description

GridCoherenceLoadBalancingSpi Javadoc data affinity for routing jobs to remote nodes. It provides ability to collocate computations with data. Coherence Cache provides partitioned cache feature which allows you to segment your cached data across cluster. This SPI delegates to Coherence Cache to find out which node is responsible for caching data and routes a job to it.

Usage

Note, that instead of regular GridJob Javadoc , this SPI expects GridCoherenceAffinityJob Javadoc which allows user to specify affinity key and cache name.

Coding Example

To use load balancers for your job routing, in your GridTask.map(List, Object) Javadoc implementation use load balancer to find out the node this job should be routed to (see GridLoadBalancerResource Javadoc documentation for information on how a load balancer can be injected into your task). However, the preferred way here is to use GridTaskSplitAdapter Javadoc , as it will handle affinity assignment of jobs to nodes automatically. Node that when working with affinity load balancing, your task's map(..) or split(..) methods should return GridCoherenceAffinityJob Javadoc instances instead of GridJob Javadoc ones. GridCoherenceAffinityJob adds two additional methods to grid job: GridCoherenceAffinityJob.getAffinityKey() and GridCoherenceAffinityJob.getCacheName() which will allow GridGain to delegate routing to Coherence Cache, so jobs for the same cache with the same key will be always routed to the same node. In case if regular GridJob instance is returned, not the GridCoherenceAffinityJob it will be routed to a randomly picked node.

Here is an example of a grid task that uses affinity load balancing. Note how load balancing jobs is absolutely transparent to the user and is simply a matter of proper grid configuration.

Here is an example of a grid task that uses affinity load balancing. Note how load balancing jobs is absolutely transparent to the user and is simply a matter of proper grid configuration.

public class MyFooBarCoherenceAffinityTask extends GridTaskSplitAdapter<List<Integer>,Object> {
    // For this example we receive a list of cache keys and for every key
    // create a job that accesses it.
    @Override
    protected Collection<? extends GridJob> split(int gridSize, List<Integer> cacheKeys) throws GridException {
        List<MyGridAffinityJob> jobs = new ArrayList<MyGridAffinityJob>(gridSize);
 
        for (Integer cacheKey : cacheKeys) {
            jobs.add(new MyFooBarCoherenceAffinityJob(cacheKey));
        }

        // Node assignment via load balancer 
        // happens automatically.
        return jobs;
    }
    ...
}

Here is the example of grid jobs created by the task above:

public class MyFooBarCoherenceAffinityJob extends GridCoherenceAffinityJobAdapter<Integer, Serializable> {
    ...                                                                      
    private static final String CACHE_NAME = "myDistributedCache";

    public MyFooBarCoherenceAffinityJob(Integer cacheKey) {
        super(CACHE_NAME, cacheKey);
    }
                                                                             
    public Serializable execute() throws GridException {                     
        ...                                                                  
        // Access data by the same key returned in 'getAffinityKey()' method 
        // and for cache with name returned in 'getCacheName()'.             
        NamedCache mycache = CacheFactory.getCache(getCacheName);              
                                                                             
        mycache.get(getAffinityKey());                                                  
        ...                                                                  
    }                                                                        
}

Configuration

This SPI has no configuration parameters.

Examples

As any GridGain SPI, GridCoherenceLoadBalancingSpi Javadoc SPI can be configured either directly from code or from Spring configuration file. Here is an example of GridCoherenceLoadBalancingSpi configuration from code:

GridCoherenceLoadBalancingSpi spi = new GridCoherenceLoadBalancingSpi();

GridConfigurationAdapter cfg = new GridConfigurationAdapter();

// Override default load balancing SPI.
cfg.setLoadBalancingSpi(spi);

// Start grid.
GridFactory.start(cfg);

or from Spring configuration file

<bean id="grid.custom.cfg" class="org.gridgain.grid.GridConfigurationAdapter" singleton="true">
    ...
    <property name="loadBalancingSpi">
        <bean class="org.gridgain.grid.spi.loadbalancing.coherence.GridCoherenceLoadBalancingSpi"/>
    </property>
    ...
</bean>


For more information about using Spring framework for configuration click here.

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