@IgniteSpiMultipleInstancesSupport(value=true) @IgniteSpiConsistencyChecked(optional=true) public class WeightedRandomLoadBalancingSpi extends IgniteSpiAdapter implements LoadBalancingSpi
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 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 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 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(); } }
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" ). |
ignite, igniteInstanceName
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()
See
setNodeWeight(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() |
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
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
getName, onClientDisconnected, onClientReconnected, onContextDestroyed, onContextInitialized
public static final String NODE_WEIGHT_ATTR_NAME
"ignite.node.weight.attr.name"
).ClusterNode.attributes()
,
Constant Field Valuespublic static final int DFLT_NODE_WEIGHT
10
).@IgniteSpiConfiguration(optional=true) public WeightedRandomLoadBalancingSpi 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.this
for chaining.public boolean isUseWeights()
@IgniteSpiConfiguration(optional=true) public WeightedRandomLoadBalancingSpi setNodeWeight(int nodeWeight)
DFLT_NODE_WEIGHT
and is equal for all nodes.nodeWeight
- Weight of this node.this
for chaining.public int getNodeWeight()
setNodeWeight(int)
.public 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 IgniteSpi
getNodeAttributes
in class IgniteSpiAdapter
IgniteSpiException
- Throws in case of any error.public void spiStart(@Nullable @Nullable String igniteInstanceName) throws IgniteSpiException
spiStart
in interface IgniteSpi
igniteInstanceName
- Name of Ignite instance this SPI is being started for
(null
for default Ignite instance).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 IgniteSpi
IgniteSpiException
- Thrown in case of any error during SPI stop.protected void onContextInitialized0(IgniteSpiContext spiCtx) throws IgniteSpiException
onContextInitialized0
in class IgniteSpiAdapter
spiCtx
- SPI context.IgniteSpiException
- In case of errors.protected void onContextDestroyed0()
onContextDestroyed0
in class IgniteSpiAdapter
public ClusterNode getBalancedNode(ComputeTaskSession ses, List<ClusterNode> top, ComputeJob job)
getBalancedNode
in interface LoadBalancingSpi
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.protected List<String> getConsistentAttributeNames()
getConsistentAttributeNames
in class IgniteSpiAdapter
public WeightedRandomLoadBalancingSpi setName(String name)
setName
in class IgniteSpiAdapter
name
- SPI name.this
for chaining.
Follow @ApacheIgnite
Ignite Database and Caching Platform : ver. 2.16.0 Release Date : December 15 2023