|
GridGain 2.0.3
Java API Specification |
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.gridgain.grid.spi.GridSpiAdapter
org.gridgain.grid.spi.loadbalancing.weightedrandom.GridWeightedRandomLoadBalancingSpi
@GridSpiInfo(author="GridGain Systems",
url="www.gridgain.org",
email="support@gridgain.com",
version="x.x")
@GridSpiMultipleInstancesSupport(value=true)
public class GridWeightedRandomLoadBalancingSpi
Load balancing SPI that picks a random node for job execution. Note that you can
optionally assign weights to nodes, so nodes with larger weights will end up getting
proportionally more jobs routed to them (see GridWeightedRandomLoadBalancingSpi.setNodeWeight(int)
configuration property). By default all nodes get equal weight defined by
GridWeightedRandomLoadBalancingSpi.DFLT_NODE_WEIGHT (value is 10).
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 affinity load balancing for some other jobs within task, then you should use
GridTaskAdapter. Here is an example of how your task will 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().toString());
}
return buf.toString();
}
}
GridWeightedRandomLoadBalancingSpi.setUseWeights(boolean))
GridWeightedRandomLoadBalancingSpi.setNodeWeight(int)). This parameter is ignored
if GridWeightedRandomLoadBalancingSpi.setUseWeights(boolean) is set to false.
GridWeightedRandomLoadBalancingSpi = new GridWeightedLoadBalancingSpi(); // Configure SPI to used weighted // random load balancing. spi.setUseWeights(true); // Set weight for the local node. spi.setWeight( *); GridConfigurationAdapter cfg = new GridConfigurationAdapter(); // Override default load balancing SPI. cfg.setLoadBalancingSpi(spi); // Start grid. GridFactory.start(cfg);Here is how you can configure GridRandomLoadBalancingSpi using Spring XML configuration:
<property name="loadBalancingSpi">
<bean class="org.gridgain.grid.spi.loadbalancing.weightedrandom.GridWeightedRandomLoadBalancingSpi">
<property name="useWeights" value="true"/>
<property name="nodeWeight" value="10"/>
</bean>
</property>
For information about Spring framework visit www.springframework.org
See Also:
Documentation
Email Support
Online Forums
Issue Tracking
Author: 2005-2008 Copyright © GridGain Systems. All Rights Reserved. ver. 2.0.3
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
| Field Summary | |
|---|---|
static int |
DFLT_NODE_WEIGHT
Default weight assigned to every node if explicit one is not provided (value is 10). |
static String |
NODE_WEIGHT_ATTR_NAME
Name of node attribute used to indicate load weight of a node (value is "gridgain.node.weight.attr.name"). |
| Constructor Summary | |
|---|---|
GridWeightedRandomLoadBalancingSpi()
|
|
| Method Summary | |
|---|---|
GridNode |
getBalancedNode(GridTaskSession ses,
List<GridNode> top,
GridJob job)
Gets balanced node for specified job within given task session. |
Map<String,Serializable> |
getNodeAttributes()
This method is called before SPI starts (before method GridSpi.spiStart(String)
is called). It allows SPI implementation to add attributes to a local
node. Kernel collects these attributes from all SPI implementations
loaded up and then passes it to discovery SPI so that they can be
exchanged with other nodes. |
int |
getNodeWeight()
Gets weight of this node. |
boolean |
isUseWeights()
Checks whether node weights are considered when doing random load balancing. |
void |
onContextDestroyed()
Callback invoked prior to stopping grid before SPI context is destroyed. Once this method is complete, grid will begin shutdown sequence. Use this callback for de-initialization logic that may involve SPI context. Note that invoking SPI context after this callback is complete is considered illegal and may produce unknown results.
If |
void |
onContextInitialized(GridSpiContext spiCtx)
Callback invoked when SPI context is initialized. SPI implementation may store SPI context for future access.
This method is invoked after |
void |
setNodeWeight(int nodeWeight)
Sets weight of this node. |
void |
setUseWeights(boolean isUseWeights)
Sets a flag to indicate whether node weights should be checked when doing random load balancing. |
void |
spiStart(String gridName)
This method is called to start SPI. |
void |
spiStop()
This method is called to stop SPI. |
String |
toString()
|
| Methods inherited from class org.gridgain.grid.spi.GridSpiAdapter |
|---|
assertParameter, checkConfigurationConsistency, configInfo, getAuthor, getGridGainHome, getLocalNodeId, getSpiContext, getStartTimestamp, getStartTimestampFormatted, getUpTime, getUpTimeFormatted, getVendorEmail, getVendorUrl, getVersion, registerMBean, setSpiContext, startInfo, startStopwatch, stopInfo, unregisterMBean, warnSpi, warnSpiParameter |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Methods inherited from interface org.gridgain.grid.spi.GridSpiManagementMBean |
|---|
getAuthor, getGridGainHome, getLocalNodeId, getStartTimestamp, getStartTimestampFormatted, getUpTime, getUpTimeFormatted, getVendorEmail, getVendorUrl, getVersion |
| Field Detail |
|---|
public static final String NODE_WEIGHT_ATTR_NAME
GridNode.getAttributes(),
Constant Field Valuespublic static final int DFLT_NODE_WEIGHT
| Constructor Detail |
|---|
public GridWeightedRandomLoadBalancingSpi()
| Method Detail |
|---|
@GridSpiConfiguration(optional=true) public void setUseWeights(boolean isUseWeights)
isUseWeights - If true then random load is distributed according
to node weights.public boolean isUseWeights()
isUseWeights in interface GridWeightedRandomLoadBalancingSpiMBean@GridSpiConfiguration(optional=true) public void setNodeWeight(int nodeWeight)
GridWeightedRandomLoadBalancingSpi.DFLT_NODE_WEIGHT and is equal for all nodes.
nodeWeight - Weight of this node.public int getNodeWeight()
getNodeWeight in interface GridWeightedRandomLoadBalancingSpiMBean
public Map<String,Serializable> getNodeAttributes()
throws GridSpiException
GridSpi.spiStart(String)
is called). It allows SPI implementation to add attributes to a local
node. Kernel collects these attributes from all SPI implementations
loaded up and then passes it to discovery SPI so that they can be
exchanged with other nodes.
getNodeAttributes in interface GridSpigetNodeAttributes in class GridSpiAdapterGridSpiException - Throws in case of any error.
public void spiStart(@Nullable
String gridName)
throws GridSpiException
spiStart in interface GridSpiGridSpiException - Throws in case of any error during SPI start.gridName - Name of grid instance this SPI is being started for
(null for default grid).
public void spiStop()
throws GridSpiException
spiStop in interface GridSpiGridSpiException - Thrown in case of any error during SPI stop.
public void onContextInitialized(GridSpiContext spiCtx)
throws GridSpiException
This method is invoked after GridSpi.spiStart(String) method is
completed, so SPI should be fully functional at this point. Use this
method for post-start initialization, such as subscribing a discovery
listener, sending a message to remote node, etc...
onContextInitialized in interface GridSpionContextInitialized in class GridSpiAdapterGridSpiException - If context initialization failed (grid will be stopped).spiCtx - Spi context.public void onContextDestroyed()
If GridSpiAdapter is used for SPI implementation, then it will
replace actual context with dummy no-op context which is usually good-enough
since grid is about to shut down.
onContextDestroyed in interface GridSpionContextDestroyed in class GridSpiAdapter
public GridNode getBalancedNode(GridTaskSession ses,
List<GridNode> top,
GridJob job)
getBalancedNode in interface GridLoadBalancingSpises - 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.
public String toString()
toString in class Object
|
GridGain 2.0.3
Java API Specification |
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
|
GridGain - Grid Computing Made Simple, ver. 2.0.3.20052008
2005-2008 Copyright © GridGain Systems. All Rights Reserved. |
|