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
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,
ClickHouseLoadBalancingPolicy is used to pickup available node and
moving node between lists according to its status.- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final AtomicBooleanFlag for exclusive health check.protected final AtomicReference<ScheduledFuture<?>>Reference holding future of scheduled discovery.protected final LinkedList<ClickHouseNode>List of faulty nodes.protected final intMaximum number of nodes can be used for operation at a time.protected final AtomicReference<ScheduledFuture<?>>Reference holding future of scheduled health check.protected final AtomicIntegerIndex for retrieving next node.protected final ReentrantReadWriteLockLock for read and writenodesandfaultyNodes.protected final LinkedList<ClickHouseNode>List of healthy nodes.protected final ClickHouseLoadBalancingPolicyLoad balancing policy.protected final ClickHouseNodeSelectorLoad balancing tags for filtering out nodes.protected final booleanFlag indicating whether it's single node or not.protected final ClickHouseNodeTemplate node. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedClickHouseNodes(Collection<ClickHouseNode> nodes, ClickHouseNode template) Default constructor. -
Method Summary
Modifier and TypeMethodDescriptionstatic 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.booleanprotected ClickHouseNodeget()Gets next node available.final List<ClickHouseNode>Gets a copy of faulty nodes.getFaultyNodes(ClickHouseNodeSelector selector, int groupSize) Gets a copy of filtered faulty nodes.final List<ClickHouseNode>getNodes()Gets a copy of nodes, which in most cases are in healthy status.getNodes(ClickHouseNodeSelector selector, int groupSize) Gets a copy of filtered nodes.Gets node selector for filtering out nodes.Gets load balancing policy.inthashCode()booleanChecks whether it's single node or not.static ClickHouseNodesGets or creates list of managedClickHouseNodefor load balancing and fail-over.static ClickHouseNodesGets or creates list of managedClickHouseNodefor load balancing and fail-over.static ClickHouseNodesGets 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) Schedule node discovery task immediately.Schedule node discovery task immediately.voidshutdown()Shuts down scheduled tasks if any.suggestNode(ClickHouseNode server, Throwable failure) Suggests a different node in order to recover from a failure, which is usually a connection error.toString()voidupdate(ClickHouseNode node, ClickHouseNode.Status status) Updates node status to one ofClickHouseNode.Status.
-
Field Details
-
checking
Flag for exclusive health check. -
index
Index for retrieving next node. -
lock
Lock for read and writenodesandfaultyNodes. -
groupSize
protected final int groupSizeMaximum number of nodes can be used for operation at a time. -
nodes
List of healthy nodes. -
faultyNodes
List of faulty nodes. -
discoveryFuture
Reference holding future of scheduled discovery. -
healthCheckFuture
Reference holding future of scheduled health check. -
policy
Load balancing policy. -
selector
Load balancing tags for filtering out nodes. -
singleNode
protected final boolean singleNodeFlag indicating whether it's single node or not. -
template
Template node.
-
-
Constructor Details
-
ClickHouseNodes
Default constructor.- Parameters:
nodes- non-empty list of nodestemplate- non-null template node
-
-
Method Details
-
buildCacheKey
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
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
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
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
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
- Specified by:
applyin interfaceFunction<ClickHouseNodeSelector,ClickHouseNode>
-
suggestNode
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
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
-
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
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
Description copied from interface:ClickHouseNodeManagerGets a copy of faulty nodes.- Specified by:
getFaultyNodesin interfaceClickHouseNodeManager- Returns:
- non-null faulty nodes
-
getFaultyNodes
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
Description copied from interface:ClickHouseNodeManagerGets load balancing policy.- Specified by:
getPolicyin interfaceClickHouseNodeManager- Returns:
- non-null load balancing policy
-
getNodeSelector
Description copied from interface:ClickHouseNodeManagerGets node selector for filtering out nodes.- Specified by:
getNodeSelectorin interfaceClickHouseNodeManager- Returns:
- non-null node selector
-
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
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
-
shutdown
public void shutdown()Description copied from interface:ClickHouseNodeManagerShuts down scheduled tasks if any.- Specified by:
shutdownin interfaceClickHouseNodeManager
-
hashCode
public int hashCode() -
equals
-
toString
-