Class WeightedRandomLoadBalancingSpi
- java.lang.Object
-
- org.apache.ignite.spi.IgniteSpiAdapter
-
- org.apache.ignite.spi.loadbalancing.weightedrandom.WeightedRandomLoadBalancingSpi
-
- All Implemented Interfaces:
IgniteSpi
,LoadBalancingSpi
@IgniteSpiMultipleInstancesSupport(true) @IgniteSpiConsistencyChecked(optional=true) public class WeightedRandomLoadBalancingSpi extends IgniteSpiAdapter implements LoadBalancingSpi
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 (seesetNodeWeight(int)
configuration property). By default all nodes get equal weight defined byDFLT_NODE_WEIGHT
(value is10
).Coding Example
If you are usingComputeTaskSplitAdapter
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 ComputeTaskSplitAdapter<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 useComputeTaskAdapter
. 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 ComputeTaskAdapter<String, String> { // Inject load balancer. @LoadBalancerResource ComputeLoadBalancer balancer; // Map jobs to grid nodes. public Map<? extends ComputeJob, ClusterNode> map(List<ClusterNode> subgrid, String arg) throws IgniteCheckedException { Map<MyFooBarJob, ClusterNode> jobs = new HashMap<MyFooBarJob, ClusterNode>(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(); } }
Configuration
In order to use this load balancer, you should configure your grid instance to useWeightedRandomLoadBalancingSpi
either from Spring XML file or directly. The following configuration parameters are supported:Mandatory
This SPI has no mandatory configuration parameters.Optional
The following configuration parameters are optional:-
Flag that indicates whether to use weight policy or simple random policy
(see
setUseWeights(boolean)
) -
Weight of this node (see
setNodeWeight(int)
). This parameter is ignored ifsetUseWeights(boolean)
is set tofalse
.
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 configureWeightedRandomLoadBalancingSpi
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
-
-
Field Summary
Fields Modifier and Type Field Description static int
DFLT_NODE_WEIGHT
Default weight assigned to every node if explicit one is not provided (value is10
).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"
).-
Fields inherited from class org.apache.ignite.spi.IgniteSpiAdapter
ignite, igniteInstanceName
-
-
Constructor Summary
Constructors Constructor Description WeightedRandomLoadBalancingSpi()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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 methodIgniteSpi.spiStart(String)
is called).int
getNodeWeight()
SeesetNodeWeight(int)
.boolean
isUseWeights()
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.WeightedRandomLoadBalancingSpi
setName(String name)
Sets SPI name.WeightedRandomLoadBalancingSpi
setNodeWeight(int nodeWeight)
Sets weight of this node.WeightedRandomLoadBalancingSpi
setUseWeights(boolean isUseWeights)
Sets a flag to indicate whether node weights should be checked when doing random load balancing.void
spiStart(@Nullable String igniteInstanceName)
This method is called to start SPI.void
spiStop()
This method is called to stop SPI.String
toString()
-
Methods inherited from class org.apache.ignite.spi.IgniteSpiAdapter
addTimeoutObject, assertParameter, checkConfigurationConsistency0, clientFailureDetectionTimeout, configInfo, createSpiAttributeName, failureDetectionTimeout, failureDetectionTimeoutEnabled, failureDetectionTimeoutEnabled, getExceptionRegistry, getLocalNode, getName, getSpiContext, ignite, initFailureDetectionTimeout, injectables, injectResources, isNodeStopping, onBeforeStart, onClientDisconnected, onClientReconnected, onContextDestroyed, onContextInitialized, registerMBean, removeTimeoutObject, started, startInfo, startStopwatch, stopInfo, unregisterMBean
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.ignite.spi.IgniteSpi
getName, onClientDisconnected, onClientReconnected, onContextDestroyed, onContextInitialized
-
-
-
-
Field Detail
-
NODE_WEIGHT_ATTR_NAME
public static final String NODE_WEIGHT_ATTR_NAME
Name of node attribute used to indicate load weight of a node (value is"ignite.node.weight.attr.name"
).- See Also:
ClusterNode.attributes()
, Constant Field Values
-
DFLT_NODE_WEIGHT
public static final int DFLT_NODE_WEIGHT
Default weight assigned to every node if explicit one is not provided (value is10
).- See Also:
- Constant Field Values
-
-
Method Detail
-
setUseWeights
@IgniteSpiConfiguration(optional=true) public WeightedRandomLoadBalancingSpi setUseWeights(boolean isUseWeights)
Sets a flag to indicate whether node weights should be checked when doing random load balancing. Default value isfalse
which means that node weights are disregarded for load balancing logic.- Parameters:
isUseWeights
- Iftrue
then random load is distributed according to node weights.- Returns:
this
for chaining.
-
isUseWeights
public boolean isUseWeights()
- Returns:
- Maximum sparsity.
-
setNodeWeight
@IgniteSpiConfiguration(optional=true) public WeightedRandomLoadBalancingSpi setNodeWeight(int nodeWeight)
Sets weight of this node. Nodes with more processing capacity should be assigned proportionally larger weight. Default value isDFLT_NODE_WEIGHT
and is equal for all nodes.- Parameters:
nodeWeight
- Weight of this node.- Returns:
this
for chaining.
-
getNodeWeight
public int getNodeWeight()
SeesetNodeWeight(int)
.- Returns:
- Maximum sparsity.
-
getNodeAttributes
public Map<String,Object> getNodeAttributes() throws IgniteSpiException
This method is called before SPI starts (before methodIgniteSpi.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.- Specified by:
getNodeAttributes
in interfaceIgniteSpi
- Overrides:
getNodeAttributes
in classIgniteSpiAdapter
- Returns:
- Map of local node attributes this SPI wants to add.
- Throws:
IgniteSpiException
- Throws in case of any error.
-
spiStart
public void spiStart(@Nullable @Nullable String igniteInstanceName) throws IgniteSpiException
This method is called to start SPI. After this method returns successfully kernel assumes that SPI is fully operational.- Specified by:
spiStart
in interfaceIgniteSpi
- Parameters:
igniteInstanceName
- Name of Ignite instance this SPI is being started for (null
for default Ignite instance).- Throws:
IgniteSpiException
- Throws in case of any error during SPI start.
-
spiStop
public void spiStop() throws IgniteSpiException
This method is called to stop SPI. After this method returns kernel assumes that this SPI is finished and all resources acquired by it are released.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.
- Specified by:
spiStop
in interfaceIgniteSpi
- Throws:
IgniteSpiException
- Thrown in case of any error during SPI stop.
-
onContextInitialized0
protected void onContextInitialized0(IgniteSpiContext spiCtx) throws IgniteSpiException
Method to be called in the end of onContextInitialized method.- Overrides:
onContextInitialized0
in classIgniteSpiAdapter
- Parameters:
spiCtx
- SPI context.- Throws:
IgniteSpiException
- In case of errors.
-
onContextDestroyed0
protected void onContextDestroyed0()
Method to be called in the beginning of onContextDestroyed() method.- Overrides:
onContextDestroyed0
in classIgniteSpiAdapter
-
getBalancedNode
public ClusterNode getBalancedNode(ComputeTaskSession ses, List<ClusterNode> top, ComputeJob job)
Gets balanced node for specified job within given task session.- Specified by:
getBalancedNode
in interfaceLoadBalancingSpi
- Parameters:
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.- Returns:
- Best balanced node for the given job within given task session.
-
getConsistentAttributeNames
protected List<String> getConsistentAttributeNames()
Returns back a list of attributes that should be consistent for this SPI. Consistency means that remote node has to have the same attribute with the same value.- Overrides:
getConsistentAttributeNames
in classIgniteSpiAdapter
- Returns:
- List or attribute names.
-
setName
public WeightedRandomLoadBalancingSpi setName(String name)
Sets SPI name.- Overrides:
setName
in classIgniteSpiAdapter
- Parameters:
name
- SPI name.- Returns:
this
for chaining.
-
-