Package com.clickhouse.client
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:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static StringFIRST_ALIVESimilar as the default policy, which always picks the first healthy node.static StringQUERY_GET_ALL_NODESstatic StringQUERY_GET_CLUSTER_NODESstatic StringQUERY_GET_OTHER_NODESstatic StringRANDOMPolicy to pick a healthy node randomly from the list.static StringROUND_ROBINPolicy to pick healthy node one after another based their order in the list.
-
Constructor Summary
Constructors Constructor Description ClickHouseLoadBalancingPolicy()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected ClickHouseNodeget(ClickHouseNodes manager)Gets next node available in the list.protected ClickHouseNodeget(ClickHouseNodes manager, ClickHouseNodeSelector t)Gets next node available in the list according to the given node selector.protected StringgetQueryForAllLocalNodes()Gets a SQL query for finding all local nodes regardless which cluster it belongs to.protected StringgetQueryForClusterLocalNodes()Gets a parameterized SQL query for finding all local nodes of a specific cluster.protected StringgetQueryForNonLocalNodes()Gets a parameterized SQL query for finding all non-local nodes in one or many clusters.protected ScheduledExecutorServicegetScheduler()Gets scheduled executor service for auto discovery and health check.static ClickHouseLoadBalancingPolicyof(String name)Gets or creates singleton load balancing policy.protected ScheduledFuture<?>schedule(ScheduledFuture<?> current, Runnable task, long interval)Schedules thetaskto run only whencurrentrun has been completed/cancelled or does not exist.protected ClickHouseNodesuggest(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.StringtoString()protected voidupdate(ClickHouseNodes manager, ClickHouseNode node, ClickHouseNode.Status status)Updates node status to one ofClickHouseNode.Status.
-
-
-
Field Detail
-
QUERY_GET_ALL_NODES
public static final String QUERY_GET_ALL_NODES
- See Also:
- Constant Field Values
-
QUERY_GET_CLUSTER_NODES
public static final String QUERY_GET_CLUSTER_NODES
- See Also:
- Constant Field Values
-
QUERY_GET_OTHER_NODES
public static final String QUERY_GET_OTHER_NODES
- See Also:
- Constant Field Values
-
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:
- Constant Field Values
-
RANDOM
public static final String RANDOM
Policy to pick a healthy node randomly from the list.- See Also:
- Constant Field Values
-
ROUND_ROBIN
public static final String ROUND_ROBIN
Policy to pick healthy node one after another based their order in the list.- See Also:
- Constant Field Values
-
-
Method Detail
-
of
public static ClickHouseLoadBalancingPolicy of(String name)
Gets or creates singleton load balancing policy.- Parameters:
name- policy name, one ofFIRST_ALIVE,RANDOMandROUND_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 toQUERY_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 toQUERY_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 toQUERY_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
protected ClickHouseNode get(ClickHouseNodes manager, ClickHouseNodeSelector t)
Gets next node available in the list according to the given node selector.- Parameters:
manager- managed nodest- 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 nodeserver- non-null server to replacefailure- 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 ofClickHouseNode.Status.- Parameters:
manager- non-null node managernode- non-null node to updatestatus- 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 thetaskto run only whencurrentrun has been completed/cancelled or does not exist.- Parameters:
current- current task, which could be null or in running statustask- scheduled task to runinterval- interval between each run- Returns:
- future object representing the scheduled task, could be null when no
scheduler available(
getScheduler()returns null)
-
-