org.apache.ignite.spi.loadbalancing.adaptive

## Class AdaptiveLoadBalancingSpi

- java.lang.Object
- - org.apache.ignite.spi.IgniteSpiAdapter
  - - org.apache.ignite.spi.loadbalancing.adaptive.AdaptiveLoadBalancingSpi

- All Implemented Interfaces:
    IgniteSpi, LoadBalancingSpi

      @IgniteSpiMultipleInstancesSupport(value=true)
      public class AdaptiveLoadBalancingSpi
      extends IgniteSpiAdapter
      implements LoadBalancingSpi

  Load balancing SPI that adapts to overall node performance. It proportionally distributes more jobs to more performant nodes based on a pluggable and dynamic node load probing.

  # Adaptive Node Probe

  This SPI comes with pluggable algorithm to calculate a node load at any given point of time. The algorithm is defined by `AdaptiveLoadProbe` interface and user is free to provide custom implementations. By default `AdaptiveCpuLoadProbe` implementation is used which distributes jobs to nodes based on average CPU load on every node.

  The following load probes are available with the product:

  - `AdaptiveCpuLoadProbe` - default
  - `AdaptiveProcessingTimeLoadProbe`
  - `AdaptiveJobCountLoadProbe`
  Note that if `AdaptiveLoadProbe.getLoad(org.apache.ignite.cluster.ClusterNode, int)` returns a value of `0`, then implementation will assume that load value is simply not available and will try to calculate an average of load values for other nodes. If such average cannot be obtained (all node load values are `0`), then a value of `1` will be used.

  When working with node metrics, take into account that all averages are calculated over metrics history size defined by `IgniteConfiguration.getMetricsExpireTime()` and `IgniteConfiguration.getMetricsHistorySize()` grid configuration parameters. Generally the larger these configuration parameter values are, the more precise the metrics are. You should tune these values based on the level of accuracy needed vs. the additional memory that would be required for storing metrics.

  You should also keep in mind that metrics for remote nodes are delayed (usually by the metrics update frequency). So if it is acceptable in your environment, set the metrics update frequency to be more inline with job execution time. Generally, the more often metrics update between nodes are exchanged, the more precise the metrics are. However, you should keep in mind that if metrics update are exchanged too often then it may create unnecessary traffic in the network. Metrics update frequency can be configured via underlying `IgniteConfiguration` used in your grid.

  Here is an example of how probing can be implemented to use number of active and waiting jobs as probing mechanism:

       public class FooBarLoadProbe implements GridAdaptiveLoadProbe {
           // Flag indicating whether to use average value or current.
           private int useAvg = true;

           public FooBarLoadProbe(boolean useAvg) {
               this.useAvg = useAvg;
           }

           // Calculate load based on number of active and waiting jobs.
           public double getLoad(ClusterNode node, int jobsSentSinceLastUpdate) {
               GridNodeMetrics metrics = node.getMetrics();

               if (useAvg) {
                   double load = metrics.getAverageActiveJobs() + metrics.getAverageWaitingJobs();

                   if (load > 0) {
                       return load;
                   }
               }

               return metrics.getCurrentActiveJobs() + metrics.getCurrentWaitingJobs();
           }
       }

  # Which Node Probe To Use

  There is no correct answer here. Every single node probe will work better or worse in different environments. CPU load probe (default option) is the safest approach to start with as it simply attempts to utilize every CPU on the grid to the maximum. However, you should experiment with other probes by executing load tests in your environment and observing which probe gives you best performance and load balancing.

  # Task 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 will 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 `JobsLoadBalancingSpi` either from Spring XML file or directly. The following configuration parameters are supported:

  ## Mandatory

  This SPI has no mandatory configuration parameters.

  ## Optional

  This SPI has the following optional configuration parameters:
  - Adaptive node load probing implementation (see `setLoadProbe(AdaptiveLoadProbe)`). This configuration parameter supplies a custom algorithm for probing a node's load. By default, `AdaptiveCpuLoadProbe` implementation is used which takes every node's CPU load and tries to send proportionally more jobs to less loaded nodes.

  Below is Java configuration example:

       AdaptiveLoadBalancingSpi spi = new AdaptiveLoadBalancingSpi();

       // Configure probe to use latest job execution time vs. average.
       AdaptiveProcessingTimeLoadProbe probe = new AdaptiveProcessingTimeLoadProbe(false);

       spi.setLoadProbe(probe);

       IgniteConfiguration cfg = new IgniteConfiguration();

       // Override default load balancing SPI.
       cfg.setLoadBalancingSpi(spi);

       // Starts grid.
       G.start(cfg);

  Here is how you can configure `GridJobsLoadBalancingSpi` using Spring XML configuration:

       <property name="loadBalancingSpi">
           <bean class="org.apache.ignite.spi.loadBalancing.adaptive.AdaptiveLoadBalancingSpi">
               <property name="loadProbe">
                   <bean class="org.apache.ignite.spi.loadBalancing.adaptive.AdaptiveProcessingTimeLoadProbe">
                       <constructor-arg value="false"/>
                   </bean>
               </property>
           </bean>
       </property>

  For information about Spring framework visit www.springframework.org

- - ### Field Summary

    - ### Fields inherited from class org.apache.ignite.spi.IgniteSpiAdapter

      `ignite, igniteInstanceName`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `AdaptiveLoadBalancingSpi()`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    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.
    `String` | `getLoadProbeFormatted()` Gets text description of current load probing implementation used.
    `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.
    `AdaptiveLoadBalancingSpi` | `setLoadProbe(AdaptiveLoadProbe probe)` Sets implementation of node load probe.
    `AdaptiveLoadBalancingSpi` | `setName(String name)` Sets SPI name.
    `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, getConsistentAttributeNames, getExceptionRegistry, getLocalNode, getName, getNodeAttributes, 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, getNodeAttributes, onClientDisconnected, onClientReconnected, onContextDestroyed, onContextInitialized`

- - ### Constructor Detail

    - #### AdaptiveLoadBalancingSpi

          public AdaptiveLoadBalancingSpi()

  - ### Method Detail

    - #### getLoadProbeFormatted

          public String getLoadProbeFormatted()

      Gets text description of current load probing implementation used.
      Returns:
        Text description of current load probing implementation used.

    - #### setLoadProbe

          @IgniteSpiConfiguration(optional=true)
          public AdaptiveLoadBalancingSpi setLoadProbe(AdaptiveLoadProbe probe)

      Sets implementation of node load probe. By default `AdaptiveProcessingTimeLoadProbe` is used which proportionally distributes load based on the average job execution time on every node.
      Parameters:
        `probe` - Implementation of node load probe
      Returns:
        `this` for chaining.

    - #### 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.

    - #### onContextInitialized0

          protected void onContextInitialized0(IgniteSpiContext spiCtx)
                                        throws IgniteSpiException

      Method to be called in the end of onContextInitialized method.
      Overrides:
        `onContextInitialized0` in class `IgniteSpiAdapter`
      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 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.

    - #### setName

          public AdaptiveLoadBalancingSpi setName(String name)

      Sets SPI name.
      Overrides:
        `setName` in class `IgniteSpiAdapter`
      Parameters:
        `name` - SPI name.
      Returns:
        `this` for chaining.

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`

2022 Copyright © Apache Software Foundation
| Follow @ApacheIgnite
Ignite Database and Caching Platform | : ver. 2.12.0
Release Date | : January 10 2022