@IgniteSpiMultipleInstancesSupport(value=true) @IgniteSpiConsistencyChecked(optional=true) public class WeightedRandomLoadBalancingSpi extends IgniteSpiAdapter implements LoadBalancingSpi, WeightedRandomLoadBalancingSpiMBean
setNodeWeight(int)
configuration property). By default all nodes get equal weight defined by
DFLT_NODE_WEIGHT (value is 10).
ComputeTaskSplitAdapter 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 ComputeJob> split(int gridSize, Object arg) throws IgniteCheckedException {
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
ComputeTaskAdapter. 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 GridComputeTaskAdapter<String, String> {
// Inject load balancer.
@LoadBalancerResource
ComputeLoadBalancer balancer;
// Map jobs to grid nodes.
public Map<? extends ComputeJob, GridNode> map(List<GridNode> subgrid, String arg) throws IgniteCheckedException {
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<ComputeJobResult> results) throws IgniteCheckedException {
// For the purpose of this example we simply
// concatenate string representation of every
// job result
StringBuilder buf = new StringBuilder();
for (ComputeJobResult res : results) {
// Append string representation of result
// returned by every job.
buf.append(res.getData().string());
}
return buf.string();
}
}
WeightedRandomLoadBalancingSpi either from Spring XML file or
directly. The following configuration parameters are supported:
setUseWeights(boolean))
setNodeWeight(int)). This parameter is ignored
if setUseWeights(boolean) is set to false.
WeightedRandomLoadBalancingSpi spi = new WeightedRandomLoadBalancingSpi(); // Configure SPI to used weighted // random load balancing. spi.setUseWeights(true); // Set weight for the local node. spi.setWeight( *); IgniteConfiguration cfg = new IgniteConfiguration(); // Override default load balancing SPI. cfg.setLoadBalancingSpi(spi); // Starts grid. G.start(cfg);Here is how you can configure
WeightedRandomLoadBalancingSpi using Spring XML configuration:
<property name="loadBalancingSpi">
<bean class="org.apache.ignite.spi.loadBalancing.weightedrandom.WeightedRandomLoadBalancingSpi">
<property name="useWeights" value="true"/>
<property name="nodeWeight" value="10"/>
</bean>
</property>
For information about Spring framework visit www.springframework.org
| Modifier and Type | Field and Description |
|---|---|
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
"ignite.node.weight.attr.name"). |
gridName, ignite| Constructor and Description |
|---|
WeightedRandomLoadBalancingSpi() |
| Modifier and Type | Method and Description |
|---|---|
ClusterNode |
getBalancedNode(ComputeTaskSession ses,
List<ClusterNode> top,
ComputeJob job)
Gets balanced node for specified job within given task session.
|
protected List<String> |
getConsistentAttributeNames()
Returns back a list of attributes that should be consistent
for this SPI.
|
Map<String,Object> |
getNodeAttributes()
This method is called before SPI starts (before method
IgniteSpi.spiStart(String)
is called). |
int |
getNodeWeight()
Gets weight of this node.
|
boolean |
isUseWeights()
Checks whether node weights are considered when doing
random load balancing.
|
protected void |
onContextDestroyed0()
Method to be called in the beginning of onContextDestroyed() method.
|
protected void |
onContextInitialized0(IgniteSpiContext spiCtx)
Method to be called in the end of onContextInitialized method.
|
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() |
addTimeoutObject, assertParameter, checkConfigurationConsistency0, configInfo, createSpiAttributeName, failureDetectionTimeout, failureDetectionTimeoutEnabled, failureDetectionTimeoutEnabled, getExceptionRegistry, getIgniteHome, getLocalNode, getLocalNodeId, getName, getSpiContext, getStartTimestamp, getStartTimestampFormatted, getUpTime, getUpTimeFormatted, initFailureDetectionTimeout, injectables, injectResources, isNodeStopping, onClientDisconnected, onClientReconnected, onContextDestroyed, onContextInitialized, registerMBean, removeTimeoutObject, setName, startInfo, startStopwatch, stopInfo, unregisterMBeanclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitgetName, onClientDisconnected, onClientReconnected, onContextDestroyed, onContextInitializedgetIgniteHome, getLocalNodeId, getName, getStartTimestamp, getStartTimestampFormatted, getUpTime, getUpTimeFormattedpublic static final String NODE_WEIGHT_ATTR_NAME
"ignite.node.weight.attr.name").public static final int DFLT_NODE_WEIGHT
10).@IgniteSpiConfiguration(optional=true) public void setUseWeights(boolean isUseWeights)
false which
means that node weights are disregarded for load balancing logic.isUseWeights - If true then random load is distributed according
to node weights.public boolean isUseWeights()
isUseWeights in interface WeightedRandomLoadBalancingSpiMBeantrue then random load is distributed according
to node weights.@IgniteSpiConfiguration(optional=true) public void setNodeWeight(int nodeWeight)
DFLT_NODE_WEIGHT and is equal for all nodes.nodeWeight - Weight of this node.public int getNodeWeight()
getNodeWeight in interface WeightedRandomLoadBalancingSpiMBeanpublic Map<String,Object> getNodeAttributes() throws IgniteSpiException
IgniteSpi.spiStart(String)
is called). It allows SPI implementation to add attributes to a local
node. Kernal 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 IgniteSpigetNodeAttributes in class IgniteSpiAdapterIgniteSpiException - Throws in case of any error.public void spiStart(@Nullable
String gridName)
throws IgniteSpiException
spiStart in interface IgniteSpigridName - Name of grid instance this SPI is being started for
(null for default grid).IgniteSpiException - Throws in case of any error during SPI start.public void spiStop()
throws IgniteSpiException
Note that this method can be called at any point including during recovery of failed start. It should make no assumptions on what state SPI will be in when this method is called.
spiStop in interface IgniteSpiIgniteSpiException - Thrown in case of any error during SPI stop.protected void onContextInitialized0(IgniteSpiContext spiCtx) throws IgniteSpiException
onContextInitialized0 in class IgniteSpiAdapterspiCtx - SPI context.IgniteSpiException - In case of errors.protected void onContextDestroyed0()
onContextDestroyed0 in class IgniteSpiAdapterpublic ClusterNode getBalancedNode(ComputeTaskSession ses, List<ClusterNode> top, ComputeJob job)
getBalancedNode in interface LoadBalancingSpises - 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.protected List<String> getConsistentAttributeNames()
getConsistentAttributeNames in class IgniteSpiAdapter
Follow @ApacheIgnite
Ignite Fabric : ver. 1.5.11 Release Date : April 8 2016