Class 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 (see setNodeWeight(int) configuration property). By default all nodes get equal weight defined by DFLT_NODE_WEIGHT (value is 10).

    Coding Example

    If you are using 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();
        }
     }
     

    Configuration

    In order to use this load balancer, you should configure your grid instance to use WeightedRandomLoadBalancingSpi 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: Below is Java configuration example:
     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

    • Field Detail

      • DFLT_NODE_WEIGHT

        public static final int DFLT_NODE_WEIGHT
        Default weight assigned to every node if explicit one is not provided (value is 10).
        See Also:
        Constant Field Values
    • Constructor Detail

      • WeightedRandomLoadBalancingSpi

        public WeightedRandomLoadBalancingSpi()
    • 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 is false which means that node weights are disregarded for load balancing logic.
        Parameters:
        isUseWeights - If true then random load is distributed according to node weights.
        Returns:
        this for chaining.
      • getNodeWeight

        public int getNodeWeight()
        Returns:
        Maximum sparsity.
      • 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 interface IgniteSpi
        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 interface IgniteSpi
        Throws:
        IgniteSpiException - Thrown in case of any error during SPI stop.
      • onContextDestroyed0

        protected void onContextDestroyed0()
        Method to be called in the beginning of onContextDestroyed() method.
        Overrides:
        onContextDestroyed0 in class IgniteSpiAdapter
      • 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 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.
      • 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 class IgniteSpiAdapter
        Returns:
        List or attribute names.