Package com.clickhouse.client
Class AbstractClient<T>
- java.lang.Object
-
- com.clickhouse.client.AbstractClient<T>
-
- All Implemented Interfaces:
ClickHouseClient,AutoCloseable
public abstract class AbstractClient<T> extends Object implements ClickHouseClient
Base class for implementing a thread-safe ClickHouse client. It usesReadWriteLockto manage access to underlying connection.
-
-
Field Summary
Fields Modifier and Type Field Description protected ReadWriteLocklock
-
Constructor Summary
Constructors Constructor Description AbstractClient()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description booleanaccept(ClickHouseProtocol protocol)Tests whether the given protocol is supported or not.protected booleancheckConnection(T connection, ClickHouseNode requestServer, ClickHouseNode currentServer, ClickHouseRequest<?> request)Checks if the underlying connection can be reused.protected abstract booleancheckHealth(ClickHouseNode server, int timeout)voidclose()protected abstract voidcloseConnection(T connection, boolean force)Closes a connection.ClickHouseRequest<?>connect(ClickHouseNode node)Connects to a ClickHouse server.ClickHouseRequest<?>connect(ClickHouseNodes nodes)Connects to a list of managed ClickHouse servers.ClickHouseRequest<?>connect(Function<ClickHouseNodeSelector,ClickHouseNode> nodeFunc)Connects to a ClickHouse server defined by the givenFunction.CompletableFuture<ClickHouseResponse>execute(ClickHouseRequest<?> request)Creates an immutable copy of the request if it's not sealed, and sends it to a node hold by the request(e.g.protected CompletableFuture<ClickHouseResponse>failedResponse(Throwable ex)protected Object[]getAsyncExecArguments(ClickHouseRequest<?> sealedRequest)Gets arguments required for async execution.ClickHouseConfiggetConfig()Gets the immutable configuration associated with this client.protected TgetConnection(ClickHouseRequest<?> request)Gets a connection according to the given request.protected ExecutorServicegetExecutor()Gets executor service for this client.protected ClickHouseNodegetServer()Gets current server.protected abstract Collection<ClickHouseProtocol>getSupportedProtocols()Gets list of supported protocols.voidinit(ClickHouseConfig config)Initializes the client using immutable configuration extracted from the builder usingClickHouseClientBuilder.getConfig().protected abstract TnewConnection(T connection, ClickHouseNode server, ClickHouseRequest<?> request)Creates a new connection and optionally close existing connection.booleanping(ClickHouseNode server, int timeout)Tests if the given server is alive or not.protected abstract ClickHouseResponsesend(ClickHouseRequest<?> sealedRequest)Sends the request to server in a current thread.protected ClickHouseResponsesendAsync(ClickHouseRequest<?> sealedRequest, Object... args)Sends the request to server in a separate thread.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.clickhouse.client.ClickHouseClient
connect, executeAndWait, getOptionClass
-
-
-
-
Field Detail
-
lock
protected final ReadWriteLock lock
-
-
Method Detail
-
checkHealth
protected abstract boolean checkHealth(ClickHouseNode server, int timeout)
-
failedResponse
protected CompletableFuture<ClickHouseResponse> failedResponse(Throwable ex)
-
getExecutor
protected final ExecutorService getExecutor()
Gets executor service for this client.- Returns:
- executor service
- Throws:
IllegalStateException- when the client is either closed or not initialized
-
getSupportedProtocols
protected abstract Collection<ClickHouseProtocol> getSupportedProtocols()
Gets list of supported protocols.- Returns:
- non-null list of supported protocols
-
getServer
protected final ClickHouseNode getServer()
Gets current server.- Returns:
- current server
- Throws:
IllegalStateException- when the client is either closed or not initialized
-
checkConnection
protected boolean checkConnection(T connection, ClickHouseNode requestServer, ClickHouseNode currentServer, ClickHouseRequest<?> request)
Checks if the underlying connection can be reused. In general, new connection will be created whenconnectionisnullorrequestServeris different fromcurrentServer- the existing connection will be closed in the later case.- Parameters:
connection- existing connection which may or may not be nullrequestServer- non-null requested server, returned from previous call ofrequest.getServer()currentServer- current server, same asgetServer()request- non-null request- Returns:
- true if the connection should NOT be changed(e.g. requestServer is same as currentServer); false otherwise
-
newConnection
protected abstract T newConnection(T connection, ClickHouseNode server, ClickHouseRequest<?> request)
Creates a new connection and optionally close existing connection. This method will be called fromgetConnection(ClickHouseRequest)as needed.- Parameters:
connection- existing connection which may or may not be nullserver- non-null requested server, returned from previous call ofrequest.getServer()request- non-null request- Returns:
- new connection
- Throws:
CompletionException- when error occured
-
closeConnection
protected abstract void closeConnection(T connection, boolean force)
Closes a connection. This method will be called fromclose().- Parameters:
connection- connection to closeforce- whether force to close the connection or not
-
getAsyncExecArguments
protected Object[] getAsyncExecArguments(ClickHouseRequest<?> sealedRequest)
Gets arguments required for async execution. This method will be executed in current thread right beforesendAsync(ClickHouseRequest, Object...).- Parameters:
sealedRequest- non-null sealed request- Returns:
- arguments required for async execution
-
sendAsync
protected ClickHouseResponse sendAsync(ClickHouseRequest<?> sealedRequest, Object... args) throws ClickHouseException, IOException
Sends the request to server in a separate thread.- Parameters:
sealedRequest- non-null sealed requestargs- arguments required for sending out the request- Returns:
- non-null response
- Throws:
ClickHouseException- when error server failed to process the requestIOException- when error occurred sending the request
-
send
protected abstract ClickHouseResponse send(ClickHouseRequest<?> sealedRequest) throws ClickHouseException, IOException
Sends the request to server in a current thread.- Parameters:
sealedRequest- non-null sealed request- Returns:
- non-null response
- Throws:
ClickHouseException- when error server failed to process the requestIOException- when error occurred sending the request
-
getConnection
protected final T getConnection(ClickHouseRequest<?> request)
Gets a connection according to the given request.- Parameters:
request- non-null request- Returns:
- non-null connection
- Throws:
CompletionException- when error occured
-
accept
public boolean accept(ClickHouseProtocol protocol)
Description copied from interface:ClickHouseClientTests whether the given protocol is supported or not. An advanced client can support as many protocols as needed.- Specified by:
acceptin interfaceClickHouseClient- Parameters:
protocol- protocol to test, null is treated asClickHouseProtocol.ANY- Returns:
- true if the given protocol is
ClickHouseProtocol.ANYor supported by this client; false otherwise
-
connect
public ClickHouseRequest<?> connect(ClickHouseNode node)
Description copied from interface:ClickHouseClientConnects to a ClickHouse server.- Specified by:
connectin interfaceClickHouseClient- Parameters:
node- non-null server to connect to- Returns:
- non-null request object holding references to this client and node provider
-
connect
public ClickHouseRequest<?> connect(ClickHouseNodes nodes)
Description copied from interface:ClickHouseClientConnects to a list of managed ClickHouse servers.- Specified by:
connectin interfaceClickHouseClient- Parameters:
nodes- non-null list of servers to connect to- Returns:
- non-null request object holding references to this client and node provider
-
connect
public ClickHouseRequest<?> connect(Function<ClickHouseNodeSelector,ClickHouseNode> nodeFunc)
Description copied from interface:ClickHouseClientConnects to a ClickHouse server defined by the givenFunction. You can pass eitherClickHouseCluster,ClickHouseNodesorClickHouseNodehere, as all of them implemented the same interface.Please be aware that this is nothing but an intention, so no network communication happens until
ClickHouseClient.execute(ClickHouseRequest)is invoked(usually triggered byrequest.execute()).- Specified by:
connectin interfaceClickHouseClient- Parameters:
nodeFunc- function to get aClickHouseNodeto connect to- Returns:
- non-null request object holding references to this client and node provider
-
getConfig
public final ClickHouseConfig getConfig()
Description copied from interface:ClickHouseClientGets the immutable configuration associated with this client. In most cases it's the exact same one passed toClickHouseClient.init(ClickHouseConfig)method for initialization.- Specified by:
getConfigin interfaceClickHouseClient- Returns:
- non-null configuration associated with this client
-
init
public void init(ClickHouseConfig config)
Description copied from interface:ClickHouseClientInitializes the client using immutable configuration extracted from the builder usingClickHouseClientBuilder.getConfig(). In general, it'sClickHouseClientBuilder's responsiblity to call this method to initialize the client at the end ofClickHouseClientBuilder.build(). However, sometimes, you may want to call this method explicitly in order to (re)initialize the client based on certain needs. If that's the case, please consider the environment when calling this method to avoid concurrent modification, and keep in mind that 1) ClickHouseConfig is immutable but ClickHouseClient is NOT; and 2) no guarantee that this method is thread-safe.- Specified by:
initin interfaceClickHouseClient- Parameters:
config- immutable configuration extracted from the builder
-
execute
public CompletableFuture<ClickHouseResponse> execute(ClickHouseRequest<?> request)
Description copied from interface:ClickHouseClientCreates an immutable copy of the request if it's not sealed, and sends it to a node hold by the request(e.g.ClickHouseNodereturned fromrequest.getServer()). Connection will be made for the first-time invocation, and then it will be reused in subsequential calls to the extract sameClickHouseNodeuntilClickHouseClient.close()is invoked.- Specified by:
executein interfaceClickHouseClient- Parameters:
request- request object which will be sealed(immutable copy) upon execution, meaning you're free to make any change to this object(e.g. prepare for next call using different SQL statement) without impacting the execution- Returns:
- non-null future object to get result
-
close
public final void close()
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceClickHouseClient
-
ping
public boolean ping(ClickHouseNode server, int timeout)
Description copied from interface:ClickHouseClientTests if the given server is alive or not. It always returnsfalsewhen server isnullor timeout is less than one. Pay attention that it's a synchronous call with minimum overhead(e.g. tiny buffer, no compression and no deserialization etc).- Specified by:
pingin interfaceClickHouseClient- Parameters:
server- non-null server to testtimeout- timeout in millisecond, should be greater than zero- Returns:
- true if the server is alive; false otherwise
-
-