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 ExampleIf you are usingComputeTaskSplitAdapterthen 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(); } }ConfigurationIn order to use this load balancer, you should configure your grid instance to useWeightedRandomLoadBalancingSpieither from Spring XML file or directly. The following configuration parameters are supported:MandatoryThis SPI has no mandatory configuration parameters.OptionalThe 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 configureWeightedRandomLoadBalancingSpiusing 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 SummaryFields Modifier and Type Field Description static intDFLT_NODE_WEIGHTDefault weight assigned to every node if explicit one is not provided (value is10).static StringNODE_WEIGHT_ATTR_NAMEName 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.IgniteSpiAdapterignite, igniteInstanceName
 
- 
 - 
Constructor SummaryConstructors Constructor Description WeightedRandomLoadBalancingSpi()
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description ClusterNodegetBalancedNode(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).intgetNodeWeight()SeesetNodeWeight(int).booleanisUseWeights()protected voidonContextDestroyed0()Method to be called in the beginning of onContextDestroyed() method.protected voidonContextInitialized0(IgniteSpiContext spiCtx)Method to be called in the end of onContextInitialized method.WeightedRandomLoadBalancingSpisetName(String name)Sets SPI name.WeightedRandomLoadBalancingSpisetNodeWeight(int nodeWeight)Sets weight of this node.WeightedRandomLoadBalancingSpisetUseWeights(boolean isUseWeights)Sets a flag to indicate whether node weights should be checked when doing random load balancing.voidspiStart(@Nullable String igniteInstanceName)This method is called to start SPI.voidspiStop()This method is called to stop SPI.StringtoString()- 
Methods inherited from class org.apache.ignite.spi.IgniteSpiAdapteraddTimeoutObject, 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.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 - 
Methods inherited from interface org.apache.ignite.spi.IgniteSpigetName, onClientDisconnected, onClientReconnected, onContextDestroyed, onContextInitialized
 
- 
 
- 
- 
- 
Field Detail- 
NODE_WEIGHT_ATTR_NAMEpublic 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_WEIGHTpublic 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 isfalsewhich means that node weights are disregarded for load balancing logic.- Parameters:
- isUseWeights- If- truethen random load is distributed according to node weights.
- Returns:
- thisfor chaining.
 
 - 
isUseWeightspublic 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_WEIGHTand is equal for all nodes.- Parameters:
- nodeWeight- Weight of this node.
- Returns:
- thisfor chaining.
 
 - 
getNodeWeightpublic int getNodeWeight() SeesetNodeWeight(int).- Returns:
- Maximum sparsity.
 
 - 
getNodeAttributespublic 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:
- getNodeAttributesin interface- IgniteSpi
- Overrides:
- getNodeAttributesin class- IgniteSpiAdapter
- Returns:
- Map of local node attributes this SPI wants to add.
- Throws:
- IgniteSpiException- Throws in case of any error.
 
 - 
spiStartpublic void spiStart(@Nullable @Nullable String igniteInstanceName) throws IgniteSpiExceptionThis method is called to start SPI. After this method returns successfully kernel assumes that SPI is fully operational.- Specified by:
- spiStartin interface- IgniteSpi
- Parameters:
- igniteInstanceName- Name of Ignite instance this SPI is being started for (- nullfor default Ignite instance).
- Throws:
- IgniteSpiException- Throws in case of any error during SPI start.
 
 - 
spiStoppublic void spiStop() throws IgniteSpiExceptionThis 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:
- spiStopin interface- IgniteSpi
- Throws:
- IgniteSpiException- Thrown in case of any error during SPI stop.
 
 - 
onContextInitialized0protected void onContextInitialized0(IgniteSpiContext spiCtx) throws IgniteSpiException Method to be called in the end of onContextInitialized method.- Overrides:
- onContextInitialized0in class- IgniteSpiAdapter
- Parameters:
- spiCtx- SPI context.
- Throws:
- IgniteSpiException- In case of errors.
 
 - 
onContextDestroyed0protected void onContextDestroyed0() Method to be called in the beginning of onContextDestroyed() method.- Overrides:
- onContextDestroyed0in class- IgniteSpiAdapter
 
 - 
getBalancedNodepublic ClusterNode getBalancedNode(ComputeTaskSession ses, List<ClusterNode> top, ComputeJob job) Gets balanced node for specified job within given task session.- Specified by:
- getBalancedNodein interface- LoadBalancingSpi
- 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.
 
 - 
getConsistentAttributeNamesprotected 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:
- getConsistentAttributeNamesin class- IgniteSpiAdapter
- Returns:
- List or attribute names.
 
 - 
setNamepublic WeightedRandomLoadBalancingSpi setName(String name) Sets SPI name.- Overrides:
- setNamein class- IgniteSpiAdapter
- Parameters:
- name- SPI name.
- Returns:
- thisfor chaining.
 
 
- 
 
-