Package org.apache.ignite.compute

## Interface ComputeLoadBalancer

-     public interface ComputeLoadBalancer

  Load balancer is used for finding the best balanced node according to load balancing policy. Internally load balancer will query the `LoadBalancingSpi` to get the balanced node.

  Load balancer can be used explicitly from inside `ComputeTask.map(List, Object)` method when you implement `ComputeTask` interface directly or use `ComputeTaskAdapter`. If you use `ComputeTaskSplitAdapter` then load balancer is accessed implicitly by the adapter so you don't have to use it directly in your logic.

  # Coding Examples

  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<String> {
           @Override
           protected Collection<? extends ComputeJob> split(int gridSize, String 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.
                   ComputeJob myJob = new MyFooBarJob(arg);

                   jobs.put(myJob, balancer.getBalancedNode(myJob, null));
               }

               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().toString());
               }

               return buf.toString();
           }
       }

- - ### Method Summary

    All Methods Instance Methods Abstract Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `@Nullable ClusterNode` | `getBalancedNode​(ComputeJob job, @Nullable Collection<ClusterNode> exclNodes)` | Gets the next balanced node according to the underlying load balancing policy.

- - ### Method Detail

    - #### getBalancedNode

          @Nullable
          @Nullable ClusterNode getBalancedNode​(ComputeJob job,
                                                @Nullable
                                                @Nullable Collection<ClusterNode> exclNodes)
                                         throws IgniteException

      Gets the next balanced node according to the underlying load balancing policy.
      Parameters:
        `job` - Job to get the balanced node for.
        `exclNodes` - Optional collection of nodes that should be excluded from balanced nodes. If collection is `null` or empty - no nodes will be excluded.
      Returns:
        Next balanced node.
      Throws:
        `IgniteException` - If any error occurred when finding next balanced node.