public interface GridLoadBalancingSpi extends GridSpi
GridComputeTask.map(List, Object)
invocation.
GridComputeTaskSplitAdapter 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 GridComputeTaskSplitAdapter<Object,Object> {
@Override
protected Collection<? extends GridComputeJob> 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
GridComputeTaskAdapter. 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 GridComputeTaskAdapter<String,String> {
// Inject load balancer.
@GridLoadBalancerResource
GridComputeLoadBalancer balancer;
// Map jobs to grid nodes.
public Map<? extends GridComputeJob, 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.
GridComputeJob myJob = new MyFooBarJob(arg);
jobs.put(myJob, balancer.getBalancedNode(myJob, null));
}
return jobs;
}
// Aggregate results into one compound result.
public String reduce(List<GridComputeJobResult> results) throws GridException {
// For the purpose of this example we simply
// concatenate string representation of every
// job result
StringBuilder buf = new StringBuilder();
for (GridComputeJobResult res : results) {
// Append string representation of result
// returned by every job.
buf.append(res.getData().toString());
}
return buf.toString();
}
}
GridGain comes with the following load balancing SPI implementations out of the box:
GridRoundRobinLoadBalancingSpi - defaultGridAdaptiveLoadBalancingSpiGridWeightedRandomLoadBalancingSpiGrid.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.| Modifier and Type | Method and Description |
|---|---|
GridNode |
getBalancedNode(GridComputeTaskSession ses,
List<GridNode> top,
GridComputeJob job)
Gets balanced node for specified job within given task session.
|
getName, getNodeAttributes, onContextDestroyed, onContextInitialized, spiStart, spiStopGridNode getBalancedNode(GridComputeTaskSession ses, List<GridNode> top, GridComputeJob job) throws GridException
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.GridException - If failed to get next balanced node.Copyright © 2014. All rights reserved.