Class AbstractClient<T>

    • Constructor Detail

      • AbstractClient

        public AbstractClient()
    • Method Detail

      • checkHealth

        protected abstract boolean checkHealth​(ClickHouseNode server,
                                               int timeout)
      • 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 when connection is null or requestServer is different from currentServer - the existing connection will be closed in the later case.
        Parameters:
        connection - existing connection which may or may not be null
        requestServer - non-null requested server, returned from previous call of request.getServer()
        currentServer - current server, same as getServer()
        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 from getConnection(ClickHouseRequest) as needed.
        Parameters:
        connection - existing connection which may or may not be null
        server - non-null requested server, returned from previous call of request.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 from close().
        Parameters:
        connection - connection to close
        force - 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 before sendAsync(ClickHouseRequest, Object...).
        Parameters:
        sealedRequest - non-null sealed request
        Returns:
        arguments required for async execution
      • 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
      • connect

        public ClickHouseRequest<?> connect​(ClickHouseNodes nodes)
        Description copied from interface: ClickHouseClient
        Connects to a list of managed ClickHouse servers.
        Specified by:
        connect in interface ClickHouseClient
        Parameters:
        nodes - non-null list of servers to connect to
        Returns:
        non-null request object holding references to this client and node provider
      • init

        public void init​(ClickHouseConfig config)
        Description copied from interface: ClickHouseClient
        Initializes the client using immutable configuration extracted from the builder using ClickHouseClientBuilder.getConfig(). In general, it's ClickHouseClientBuilder's responsiblity to call this method to initialize the client at the end of ClickHouseClientBuilder.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:
        init in interface ClickHouseClient
        Parameters:
        config - immutable configuration extracted from the builder
      • execute

        public CompletableFuture<ClickHouseResponse> execute​(ClickHouseRequest<?> request)
        Description copied from interface: ClickHouseClient
        Creates an immutable copy of the request if it's not sealed, and sends it to a node hold by the request(e.g. ClickHouseNode returned from request.getServer()). Connection will be made for the first-time invocation, and then it will be reused in subsequential calls to the extract same ClickHouseNode until ClickHouseClient.close() is invoked.
        Specified by:
        execute in interface ClickHouseClient
        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
      • ping

        public boolean ping​(ClickHouseNode server,
                            int timeout)
        Description copied from interface: ClickHouseClient
        Tests if the given server is alive or not. It always returns false when server is null or 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:
        ping in interface ClickHouseClient
        Parameters:
        server - non-null server to test
        timeout - timeout in millisecond, should be greater than zero
        Returns:
        true if the server is alive; false otherwise