Class StandardClient

  • All Implemented Interfaces:
    java.lang.AutoCloseable, Client
    Direct Known Subclasses:
    ReplayClient

    public class StandardClient
    extends java.lang.Object
    implements Client, java.lang.AutoCloseable
    Connection client
    • Field Detail

      • logger

        private static final Logger logger
      • exceptionFactory

        protected final ExceptionFactory exceptionFactory
        connection exception factory
      • REDIRECT_PATTERN

        private static final java.util.regex.Pattern REDIRECT_PATTERN
      • socket

        private java.net.Socket socket
      • compressionSequence

        private final MutableByte compressionSequence
      • disablePipeline

        private final boolean disablePipeline
      • context

        protected Context context
        connection context
      • writer

        protected Writer writer
        packet writer
      • closed

        private boolean closed
      • reader

        private Reader reader
      • certFingerprint

        private byte[] certFingerprint
      • socketTimeout

        private int socketTimeout
      • redirectConsumer

        private final java.util.function.Consumer<java.lang.String> redirectConsumer
    • Constructor Detail

      • StandardClient

        public StandardClient​(Configuration conf,
                              HostAddress hostAddress,
                              ClosableLock lock,
                              boolean skipPostCommands)
                       throws java.sql.SQLException
        Constructor
        Parameters:
        conf - configuration
        hostAddress - host
        lock - thread locker
        skipPostCommands - must connection post command be skipped
        Throws:
        java.sql.SQLException - if connection fails
    • Method Detail

      • authenticationHandler

        public void authenticationHandler​(Credential credential,
                                          HostAddress hostAddress)
                                   throws java.io.IOException,
                                          java.sql.SQLException
        Parameters:
        credential - credential
        hostAddress - host address
        Throws:
        java.io.IOException - if any socket error occurs
        java.sql.SQLException - if any other kind of issue occurs
      • validateFingerPrint

        private static boolean validateFingerPrint​(AuthenticationPlugin authPlugin,
                                                   byte[] validationHash,
                                                   byte[] fingerPrint,
                                                   Credential credential,
                                                   byte[] seed)
      • redirect

        public void redirect​(java.lang.String redirectUrl)
      • sslWrapper

        public javax.net.ssl.SSLSocket sslWrapper​(HostAddress hostAddress,
                                                  java.net.Socket socket,
                                                  long clientCapabilities,
                                                  byte exchangeCharset,
                                                  Context context,
                                                  Writer writer)
                                           throws java.io.IOException,
                                                  java.sql.SQLException
        Create SSL wrapper
        Parameters:
        hostAddress - host
        socket - socket
        clientCapabilities - client capabilities
        exchangeCharset - connection charset
        context - connection context
        writer - socket writer
        Returns:
        SSLsocket
        Throws:
        java.io.IOException - if any socket error occurs
        java.sql.SQLException - for any other kind of error
      • assignStream

        private void assignStream​(java.io.OutputStream out,
                                  java.io.InputStream in,
                                  Configuration conf,
                                  java.lang.Long threadId)
      • destroySocket

        protected void destroySocket()
        Closing socket in case of Connection error after socket creation.
      • handleTimezone

        private void handleTimezone()
                             throws java.sql.SQLException
        load server timezone and ensure this corresponds to client timezone
        Throws:
        java.sql.SQLException - if any socket error.
      • postConnectionQueries

        private void postConnectionQueries()
                                    throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • createSessionVariableQuery

        public java.lang.String createSessionVariableQuery​(Context context)
        Create session variable if configuration requires additional commands.
        Parameters:
        context - context
        Returns:
        sql setting session command
      • setReadOnly

        public void setReadOnly​(boolean readOnly)
                         throws java.sql.SQLException
        Description copied from interface: Client
        Switch to a writer/read-only connection, no effet on mono-connection
        Specified by:
        setReadOnly in interface Client
        Parameters:
        readOnly - must use read-only connection
        Throws:
        java.sql.SQLException - if any error occurs
      • sendQuery

        public int sendQuery​(ClientMessage message)
                      throws java.sql.SQLException
        Send client message to server
        Parameters:
        message - client message
        Returns:
        number of command send
        Throws:
        java.sql.SQLException - if socket error occurs
      • execute

        public java.util.List<Completion> execute​(ClientMessage message,
                                                  boolean canRedo)
                                           throws java.sql.SQLException
        Description copied from interface: Client
        Send client message and read result
        Specified by:
        execute in interface Client
        Parameters:
        message - client message
        canRedo - can client message be redone in case of failover
        Returns:
        results
        Throws:
        java.sql.SQLException - if execution fails
      • execute

        public java.util.List<Completion> execute​(ClientMessage message,
                                                  Statement stmt,
                                                  boolean canRedo)
                                           throws java.sql.SQLException
        Description copied from interface: Client
        Send client message and read result
        Specified by:
        execute in interface Client
        Parameters:
        message - client message
        stmt - statement
        canRedo - can client message be redone in case of failover
        Returns:
        results
        Throws:
        java.sql.SQLException - if execution fails
      • executePipeline

        public java.util.List<Completion> executePipeline​(ClientMessage[] messages,
                                                          Statement stmt,
                                                          int fetchSize,
                                                          long maxRows,
                                                          int resultSetConcurrency,
                                                          int resultSetType,
                                                          boolean closeOnCompletion,
                                                          boolean canRedo)
                                                   throws java.sql.SQLException
        Description copied from interface: Client
        Send client messages pipelining and read result
        Specified by:
        executePipeline in interface Client
        Parameters:
        messages - client message
        stmt - statement
        fetchSize - fetch size
        maxRows - maximum number of rows. 0 = all
        resultSetConcurrency - concurrency
        resultSetType - result-set type
        closeOnCompletion - close statement on completion
        canRedo - can client message be redone in case of failover
        Returns:
        results
        Throws:
        java.sql.SQLException - if any error occurs
      • execute

        public java.util.List<Completion> execute​(ClientMessage message,
                                                  Statement stmt,
                                                  int fetchSize,
                                                  long maxRows,
                                                  int resultSetConcurrency,
                                                  int resultSetType,
                                                  boolean closeOnCompletion,
                                                  boolean canRedo)
                                           throws java.sql.SQLException
        Description copied from interface: Client
        Send client message and read result
        Specified by:
        execute in interface Client
        Parameters:
        message - client message
        stmt - statement
        fetchSize - fetch size
        maxRows - maximum number of rows. 0 = all
        resultSetConcurrency - concurrency
        resultSetType - result-set type
        closeOnCompletion - close statement on completion
        canRedo - can client message be redone in case of failover
        Returns:
        results
        Throws:
        java.sql.SQLException - if any error occurs
      • readResponse

        public java.util.List<Completion> readResponse​(Statement stmt,
                                                       ClientMessage message,
                                                       int fetchSize,
                                                       long maxRows,
                                                       int resultSetConcurrency,
                                                       int resultSetType,
                                                       boolean closeOnCompletion)
                                                throws java.sql.SQLException
        Read server responses for a client message
        Parameters:
        stmt - statement that issue the message
        message - client message sent
        fetchSize - fetch size
        maxRows - maximum number of rows
        resultSetConcurrency - concurrency
        resultSetType - result-set type
        closeOnCompletion - close statement on resultset completion
        Returns:
        list of result
        Throws:
        java.sql.SQLException - if any error occurs
      • readResponse

        public void readResponse​(ClientMessage message)
                          throws java.sql.SQLException
        Read server response
        Parameters:
        message - client message that was sent
        Throws:
        java.sql.SQLException - if any error occurs
      • closePrepare

        public void closePrepare​(Prepare prepare)
                          throws java.sql.SQLException
        Description copied from interface: Client
        Close prepare command
        Specified by:
        closePrepare in interface Client
        Parameters:
        prepare - prepare command
        Throws:
        java.sql.SQLException - if any error occurs
      • readStreamingResults

        public void readStreamingResults​(java.util.List<Completion> completions,
                                         int fetchSize,
                                         long maxRows,
                                         int resultSetConcurrency,
                                         int resultSetType,
                                         boolean closeOnCompletion)
                                  throws java.sql.SQLException
        Description copied from interface: Client
        Read results
        Specified by:
        readStreamingResults in interface Client
        Parameters:
        completions - List that will have the new results
        fetchSize - fetch size
        maxRows - maximum number of rows. 0 = all
        resultSetConcurrency - concurrency
        resultSetType - result-set type
        closeOnCompletion - close statement on completion
        Throws:
        java.sql.SQLException - if any error occurs
      • readResults

        private void readResults​(Statement stmt,
                                 ClientMessage message,
                                 java.util.List<Completion> completions,
                                 int fetchSize,
                                 long maxRows,
                                 int resultSetConcurrency,
                                 int resultSetType,
                                 boolean closeOnCompletion)
                          throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • readPacket

        public Completion readPacket​(ClientMessage message)
                              throws java.sql.SQLException
        Read a MySQL packet from socket
        Parameters:
        message - client message issuing the result
        Returns:
        a mysql result
        Throws:
        java.sql.SQLException - if any error occurs
      • readPacket

        public Completion readPacket​(Statement stmt,
                                     ClientMessage message,
                                     int fetchSize,
                                     long maxRows,
                                     int resultSetConcurrency,
                                     int resultSetType,
                                     boolean closeOnCompletion)
                              throws java.sql.SQLException
        Read server response packet.
        Parameters:
        stmt - current statement (null if internal)
        message - current message
        fetchSize - default fetch size
        maxRows - maximum row number
        resultSetConcurrency - concurrency
        resultSetType - type
        closeOnCompletion - must resultset close statement on completion
        Returns:
        Completion
        Throws:
        java.sql.SQLException - if any exception
        See Also:
        server response packets
      • checkNotClosed

        protected void checkNotClosed()
                               throws java.sql.SQLException
        Throw an exception if client is closed
        Throws:
        java.sql.SQLException - if closed
      • closeSocket

        private void closeSocket()
      • isClosed

        public boolean isClosed()
        Description copied from interface: Client
        Is client closed
        Specified by:
        isClosed in interface Client
        Returns:
        close flag
      • getContext

        public Context getContext()
        Description copied from interface: Client
        Get connection context
        Specified by:
        getContext in interface Client
        Returns:
        connection context
      • abort

        public void abort​(java.util.concurrent.Executor executor)
                   throws java.sql.SQLException
        Description copied from interface: Client
        Abort current connection
        Specified by:
        abort in interface Client
        Parameters:
        executor - executor
        Throws:
        java.sql.SQLException - if any error occurs
      • getSocketTimeout

        public int getSocketTimeout()
        Description copied from interface: Client
        get socket timeout
        Specified by:
        getSocketTimeout in interface Client
        Returns:
        socket timeout
      • setSocketTimeout

        public void setSocketTimeout​(int milliseconds)
                              throws java.sql.SQLException
        Description copied from interface: Client
        Set socket timeout
        Specified by:
        setSocketTimeout in interface Client
        Parameters:
        milliseconds - timeout
        Throws:
        java.sql.SQLException - if any error occurs
      • close

        public void close()
        Description copied from interface: Client
        Close client
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface Client
      • getSocketIp

        public java.lang.String getSocketIp()
        Description copied from interface: Client
        Get current socket IP or null (for Pipe / unix socket)
        Specified by:
        getSocketIp in interface Client
        Returns:
        Socket current IP
      • isPrimary

        public boolean isPrimary()
        Description copied from interface: Client
        is current client writer or read-only
        Specified by:
        isPrimary in interface Client
        Returns:
        is primary
      • getExceptionFactory

        public ExceptionFactory getExceptionFactory()
        Description copied from interface: Client
        Get connection exception factory
        Specified by:
        getExceptionFactory in interface Client
        Returns:
        connection exception factory
      • getHostAddress

        public HostAddress getHostAddress()
        Description copied from interface: Client
        Get connection host
        Specified by:
        getHostAddress in interface Client
        Returns:
        connection host
      • reset

        public void reset()
        Description copied from interface: Client
        Reset connection
        Specified by:
        reset in interface Client