Package com.clickhouse.client
Class ClickHouseNodes
- java.lang.Object
-
- com.clickhouse.client.ClickHouseNodes
-
- All Implemented Interfaces:
ClickHouseNodeManager,Serializable,Function<ClickHouseNodeSelector,ClickHouseNode>
- Direct Known Subclasses:
ClickHouseCluster
public class ClickHouseNodes extends Object implements ClickHouseNodeManager
A generic node manager for managing one or more nodes which may or may not belong to same cluster. It maintains two lists - one for healthy nodes and the other for faulty ones. Behind the scene, there's background thread(s) for discovering nodes and health check. Besides,ClickHouseLoadBalancingPolicyis used to pickup available node and moving node between lists according to its status.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected AtomicBooleancheckingFlag for exclusive health check.protected AtomicReference<ScheduledFuture<?>>discoveryFutureReference holding future of scheduled discovery.protected LinkedList<ClickHouseNode>faultyNodesList of faulty nodes.protected intgroupSizeMaximum number of nodes can be used for operation at a time.protected AtomicReference<ScheduledFuture<?>>healthCheckFutureReference holding future of scheduled health check.protected AtomicIntegerindexIndex for retrieving next node.protected ReentrantReadWriteLocklockLock for read and writenodesandfaultyNodes.protected LinkedList<ClickHouseNode>nodesList of healthy nodes.protected ClickHouseLoadBalancingPolicypolicyLoad balancing policy.protected ClickHouseNodeSelectorselectorLoad balancing tags for filtering out nodes.protected booleansingleNodeFlag indicating whether it's single node or not.protected ClickHouseNodetemplateTemplate node.
-
Constructor Summary
Constructors Modifier Constructor Description protectedClickHouseNodes(Collection<ClickHouseNode> nodes, ClickHouseNode template)Default constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ClickHouseNodeapply(ClickHouseNodeSelector t)static StringbuildCacheKey(String uri, Map<?,?> options)Build unique key according to the given base URI and options for caching.voidcheck()Checks (faulty) node status.voiddiscover()Discovers nodes in the same cluster by querying againstsystem.clusterstable.booleanequals(Object obj)protected ClickHouseNodeget()Gets next node available.List<ClickHouseNode>getFaultyNodes()Gets a copy of faulty nodes.List<ClickHouseNode>getFaultyNodes(ClickHouseNodeSelector selector, int groupSize)Gets a copy of filtered faulty nodes.List<ClickHouseNode>getNodes()Gets a copy of nodes, which in most cases are in healthy status.List<ClickHouseNode>getNodes(ClickHouseNodeSelector selector, int groupSize)Gets a copy of filtered nodes.ClickHouseNodeSelectorgetNodeSelector()Gets node selector for filtering out nodes.ClickHouseLoadBalancingPolicygetPolicy()Gets load balancing policy.ClickHouseNodegetTemplate()inthashCode()booleanisSingleNode()Checks whether it's single node or not.static ClickHouseNodesof(String endpoints)Gets or creates list of managedClickHouseNodefor load balancing and fail-over.static ClickHouseNodesof(String cacheKey, String endpoints, Map<?,?> options)Gets or creates list of managedClickHouseNodefor load balancing and fail-over.static ClickHouseNodesof(String endpoints, Map<?,?> options)Gets or creates list of managedClickHouseNodefor load balancing and fail-over.protected voidqueryClusterNodes(Collection<ClickHouseNode> seeds, Collection<ClickHouseNode> allNodes, Collection<ClickHouseNode> newHealthyNodes, Collection<ClickHouseNode> newFaultyNodes, Collection<ClickHouseNode> useless)Optional<ScheduledFuture<?>>scheduleDiscovery()Schedule node discovery task immediately.Optional<ScheduledFuture<?>>scheduleHealthCheck()Schedule node discovery task immediately.ClickHouseNodesuggestNode(ClickHouseNode server, Throwable failure)Suggests a different node in order to recover from a failure, which is usually a connection error.StringtoString()voidupdate(ClickHouseNode node, ClickHouseNode.Status status)Updates node status to one ofClickHouseNode.Status.
-
-
-
Field Detail
-
checking
protected final AtomicBoolean checking
Flag for exclusive health check.
-
index
protected final AtomicInteger index
Index for retrieving next node.
-
lock
protected final ReentrantReadWriteLock lock
Lock for read and writenodesandfaultyNodes.
-
groupSize
protected final int groupSize
Maximum number of nodes can be used for operation at a time.
-
nodes
protected final LinkedList<ClickHouseNode> nodes
List of healthy nodes.
-
faultyNodes
protected final LinkedList<ClickHouseNode> faultyNodes
List of faulty nodes.
-
discoveryFuture
protected final AtomicReference<ScheduledFuture<?>> discoveryFuture
Reference holding future of scheduled discovery.
-
healthCheckFuture
protected final AtomicReference<ScheduledFuture<?>> healthCheckFuture
Reference holding future of scheduled health check.
-
policy
protected final ClickHouseLoadBalancingPolicy policy
Load balancing policy.
-
selector
protected final ClickHouseNodeSelector selector
Load balancing tags for filtering out nodes.
-
singleNode
protected final boolean singleNode
Flag indicating whether it's single node or not.
-
template
protected final ClickHouseNode template
Template node.
-
-
Constructor Detail
-
ClickHouseNodes
protected ClickHouseNodes(Collection<ClickHouseNode> nodes, ClickHouseNode template)
Default constructor.- Parameters:
nodes- non-empty list of nodestemplate- non-null template node
-
-
Method Detail
-
buildCacheKey
public static String buildCacheKey(String uri, Map<?,?> options)
Build unique key according to the given base URI and options for caching.- Parameters:
uri- non-null URIoptions- options- Returns:
- non-empty unique key for caching
-
of
public static ClickHouseNodes of(String endpoints)
Gets or creates list of managedClickHouseNodefor load balancing and fail-over.- Parameters:
endpoints- non-empty URIs separated by comma- Returns:
- non-null list of nodes
-
of
public static ClickHouseNodes of(String endpoints, Map<?,?> options)
Gets or creates list of managedClickHouseNodefor load balancing and fail-over.- Parameters:
endpoints- non-empty URIs separated by commaoptions- default options- Returns:
- non-null list of nodes
-
of
public static ClickHouseNodes of(String cacheKey, String endpoints, Map<?,?> options)
Gets or creates list of managedClickHouseNodefor load balancing and fail-over. Since the list will be cached in aWeakHashMap, as long as you hold strong reference to thecacheKey, same combination ofendpointsandoptionswill be always mapped to the exact same list.- Parameters:
cacheKey- non-empty cache keyendpoints- non-empty URIs separated by commaoptions- default options- Returns:
- non-null list of nodes
-
queryClusterNodes
protected void queryClusterNodes(Collection<ClickHouseNode> seeds, Collection<ClickHouseNode> allNodes, Collection<ClickHouseNode> newHealthyNodes, Collection<ClickHouseNode> newFaultyNodes, Collection<ClickHouseNode> useless)
-
get
protected ClickHouseNode get()
Gets next node available.- Returns:
- non-null node
-
isSingleNode
public boolean isSingleNode()
Checks whether it's single node or not.- Returns:
- true if it's single node; false otherwise
-
apply
public ClickHouseNode apply(ClickHouseNodeSelector t)
- Specified by:
applyin interfaceFunction<ClickHouseNodeSelector,ClickHouseNode>
-
suggestNode
public ClickHouseNode suggestNode(ClickHouseNode server, Throwable failure)
Description copied from interface:ClickHouseNodeManagerSuggests a different node in order to recover from a failure, which is usually a connection error.- Specified by:
suggestNodein interfaceClickHouseNodeManager- Parameters:
server- node related to the failure(e.g. the node couldn't be connected)failure- recoverable failure- Returns:
- non-null node which may or may not be same as the given one
-
update
public void update(ClickHouseNode node, ClickHouseNode.Status status)
Description copied from interface:ClickHouseNodeManagerUpdates node status to one ofClickHouseNode.Status. It simply delegates the call togetPolicy().update(node, status)in a thread-safe manner.- Specified by:
updatein interfaceClickHouseNodeManager- Parameters:
node- non-null node to updatestatus- non-null status of the node
-
check
public void check()
Checks (faulty) node status.
-
discover
public void discover()
Discovers nodes in the same cluster by querying againstsystem.clusterstable.
-
getTemplate
public ClickHouseNode getTemplate()
-
getNodes
public final List<ClickHouseNode> getNodes()
Description copied from interface:ClickHouseNodeManagerGets a copy of nodes, which in most cases are in healthy status. However, this really depends on howClickHouseLoadBalancingPolicymanages node status. In first-alive policy, it's acutally a full list regardless node status.- Specified by:
getNodesin interfaceClickHouseNodeManager- Returns:
- non-null nodes
-
getNodes
public List<ClickHouseNode> getNodes(ClickHouseNodeSelector selector, int groupSize)
Description copied from interface:ClickHouseNodeManagerGets a copy of filtered nodes.- Specified by:
getNodesin interfaceClickHouseNodeManager- Parameters:
selector- node selector for filtering out nodes, null means no filtergroupSize- maximum number of nodes to get, zero or negative value means all- Returns:
- non-null nodes
-
getFaultyNodes
public final List<ClickHouseNode> getFaultyNodes()
Description copied from interface:ClickHouseNodeManagerGets a copy of faulty nodes.- Specified by:
getFaultyNodesin interfaceClickHouseNodeManager- Returns:
- non-null faulty nodes
-
getFaultyNodes
public List<ClickHouseNode> getFaultyNodes(ClickHouseNodeSelector selector, int groupSize)
Description copied from interface:ClickHouseNodeManagerGets a copy of filtered faulty nodes.- Specified by:
getFaultyNodesin interfaceClickHouseNodeManager- Parameters:
selector- node selector for filtering out nodes, null means no filtergroupSize- maximum number of nodes to get, zero or negative value means all- Returns:
- non-null faulty nodes
-
getPolicy
public ClickHouseLoadBalancingPolicy getPolicy()
Description copied from interface:ClickHouseNodeManagerGets load balancing policy.- Specified by:
getPolicyin interfaceClickHouseNodeManager- Returns:
- non-null load balancing policy
-
getNodeSelector
public ClickHouseNodeSelector getNodeSelector()
Description copied from interface:ClickHouseNodeManagerGets node selector for filtering out nodes.- Specified by:
getNodeSelectorin interfaceClickHouseNodeManager- Returns:
- non-null node selector
-
scheduleDiscovery
public Optional<ScheduledFuture<?>> scheduleDiscovery()
Description copied from interface:ClickHouseNodeManagerSchedule node discovery task immediately. Nothing will happen when task scheduler does not exist(e.g.getPolicy().getScheduler()returns null) or there's a task running for node discovery.- Specified by:
scheduleDiscoveryin interfaceClickHouseNodeManager- Returns:
- optional future for retrieving the running task
-
scheduleHealthCheck
public Optional<ScheduledFuture<?>> scheduleHealthCheck()
Description copied from interface:ClickHouseNodeManagerSchedule node discovery task immediately. Nothing will happen when task scheduler does not exist(e.g.getPolicy().getScheduler()returns null) or there's a task running for health check.- Specified by:
scheduleHealthCheckin interfaceClickHouseNodeManager- Returns:
- optional future for retrieving the running task
-
-