Class ClickHouseLoadBalancingPolicy

java.lang.Object
com.clickhouse.client.ClickHouseLoadBalancingPolicy
All Implemented Interfaces:
Serializable

public abstract class ClickHouseLoadBalancingPolicy extends Object implements Serializable
Load balancing policy. In general, a policy is responsible for 3 things: 1) get node from a managed list; 2) managing node's status; and 3) optionally schedule background tasks like node discovery and health check.
See Also:
  • Field Details

    • QUERY_GET_ALL_NODES

      public static final String QUERY_GET_ALL_NODES
      See Also:
    • QUERY_GET_CLUSTER_NODES

      public static final String QUERY_GET_CLUSTER_NODES
      See Also:
    • QUERY_GET_OTHER_NODES

      public static final String QUERY_GET_OTHER_NODES
      See Also:
    • FIRST_ALIVE

      public static final String FIRST_ALIVE
      Similar as the default policy, which always picks the first healthy node. Besides, it provides scheduler for node discovery and health check.
      See Also:
    • RANDOM

      public static final String RANDOM
      Policy to pick a healthy node randomly from the list.
      See Also:
    • ROUND_ROBIN

      public static final String ROUND_ROBIN
      Policy to pick healthy node one after another based their order in the list.
      See Also:
  • Constructor Details

    • ClickHouseLoadBalancingPolicy

      public ClickHouseLoadBalancingPolicy()
  • Method Details

    • of

      public static ClickHouseLoadBalancingPolicy of(String name)
      Gets or creates singleton load balancing policy.
      Parameters:
      name - policy name, one of FIRST_ALIVE,RANDOM and ROUND_ROBIN, or a fully qualified class name
      Returns:
      non-null load balancing policy
    • getQueryForAllLocalNodes

      protected String getQueryForAllLocalNodes()
      Gets a SQL query for finding all local nodes regardless which cluster it belongs to. Defaults to QUERY_GET_ALL_NODES.
      Returns:
      non-null SQL query
    • getQueryForClusterLocalNodes

      protected String getQueryForClusterLocalNodes()
      Gets a parameterized SQL query for finding all local nodes of a specific cluster. Defaults to QUERY_GET_CLUSTER_NODES.
      Returns:
      non-null SQL query
    • getQueryForNonLocalNodes

      protected String getQueryForNonLocalNodes()
      Gets a parameterized SQL query for finding all non-local nodes in one or many clusters. Defaults to QUERY_GET_OTHER_NODES.
      Returns:
      non-null SQL query
    • get

      protected final ClickHouseNode get(ClickHouseNodes manager)
      Gets next node available in the list.
      Parameters:
      manager - managed nodes
      Returns:
      next node
    • get

      Gets next node available in the list according to the given node selector.
      Parameters:
      manager - managed nodes
      t - node selector
      Returns:
      next node
      Throws:
      IllegalArgumentException - when no node available to use
    • suggest

      protected ClickHouseNode suggest(ClickHouseNodes manager, ClickHouseNode server, Throwable failure)
      Sugguests a new node as replacement of the given one in order to recover from the failure, which is usually a connection error.
      Parameters:
      manager - non-null manager for finding suitable node
      server - non-null server to replace
      failure - non-null recoverable exception
      Returns:
      new server, or the exact same server from input when running out of options
    • update

      protected void update(ClickHouseNodes manager, ClickHouseNode node, ClickHouseNode.Status status)
      Updates node status to one of ClickHouseNode.Status.
      Parameters:
      manager - non-null node manager
      node - non-null node to update
      status - non-null status of the node
    • getScheduler

      protected ScheduledExecutorService getScheduler()
      Gets scheduled executor service for auto discovery and health check.
      Returns:
      scheduled executor service, null will turn off auto discovery and health check
    • schedule

      protected ScheduledFuture<?> schedule(ScheduledFuture<?> current, Runnable task, long interval)
      Schedules the task to run only when current run has been completed/cancelled or does not exist.
      Parameters:
      current - current task, which could be null or in running status
      task - scheduled task to run
      interval - interval between each run
      Returns:
      future object representing the scheduled task, could be null when no scheduler available(getScheduler() returns null)
    • toString

      public String toString()
      Overrides:
      toString in class Object