Class MultiPrimaryClient

java.lang.Object
org.mariadb.jdbc.client.impl.MultiPrimaryClient
All Implemented Interfaces:
AutoCloseable, Client
Direct Known Subclasses:
MultiPrimaryReplicaClient

public class MultiPrimaryClient extends Object implements Client
Handling connection failing automatic reconnection transparently when possible for multi-master Topology.

remark: would have been better using proxy, but for AOT compilation, avoiding to using not supported proxy class.

  • Field Details

    • denyList

      protected static final ConcurrentMap<HostAddress,Long> denyList
      temporary blacklisted hosts
    • logger

      private static final Logger logger
    • deniedListTimeout

      protected final long deniedListTimeout
      denied timeout
    • conf

      protected final Configuration conf
      configuration
    • lock

      protected final ClosableLock lock
      thread locker
    • closed

      protected boolean closed
      is connections explicitly closed
    • currentClient

      protected Client currentClient
      current client
  • Constructor Details

  • Method Details

    • connectHost

      protected Client connectHost(boolean readOnly, boolean failFast) throws SQLException
      Trying connecting server.

      searching each connecting primary / replica connection not temporary denied until found one. searching in temporary denied host if not succeed, until reaching `retriesAllDown` attempts.

      Parameters:
      readOnly - must connect a replica / primary
      failFast - must try only not denied server
      Returns:
      a valid connection client
      Throws:
      SQLException - if not succeed to create a connection.
    • tryConnectToAvailableHost

      private Client tryConnectToAvailableHost(boolean readOnly, int retriesLeft) throws SQLException
      Throws:
      SQLException
    • tryConnectToDeniedHost

      private Client tryConnectToDeniedHost(boolean readOnly, int retriesLeft) throws SQLException
      Throws:
      SQLException
    • findHostWithLowestDenyTimeout

      private Optional<HostAddress> findHostWithLowestDenyTimeout(boolean readOnly)
    • validateHostConfiguration

      private void validateHostConfiguration(boolean readOnly) throws SQLNonTransientConnectionException
      Throws:
      SQLNonTransientConnectionException
    • createClient

      private Client createClient(HostAddress host) throws SQLException
      Throws:
      SQLException
    • addToDenyList

      private void addToDenyList(HostAddress host)
    • sleepBeforeRetry

      private void sleepBeforeRetry()
    • reConnect

      protected Client reConnect() throws SQLException
      Connection loop
      Returns:
      client connection
      Throws:
      SQLException - if fail to connect
    • replayIfPossible

      protected void replayIfPossible(Client oldClient, boolean canRedo) throws SQLException
      Execute transaction replay if in transaction and configured for it, throw an exception if not
      Parameters:
      oldClient - previous client
      canRedo - if command can be redo even if not in transaction
      Throws:
      SQLException - if not able to replay
    • executeTransactionReplay

      protected void executeTransactionReplay(Client oldCli) throws SQLException
      Execute transaction replay
      Parameters:
      oldCli - previous client
      Throws:
      SQLException - if not able to replay
    • syncNewState

      public void syncNewState(Client oldCli) throws SQLException
      Synchronizes client states between previous and new clients.
      Parameters:
      oldCli - previous client instance
      Throws:
      SQLException - if synchronization error occurs
    • syncExceptionFactory

      private void syncExceptionFactory(Client oldCli)
    • syncAutoCommit

      private void syncAutoCommit(Context oldCtx) throws SQLException
      Throws:
      SQLException
    • isAutoCommitSyncRequired

      private boolean isAutoCommitSyncRequired(Context oldCtx)
    • syncDatabase

      private void syncDatabase(Context oldCtx) throws SQLException
      Throws:
      SQLException
    • isDatabaseSyncRequired

      private boolean isDatabaseSyncRequired(Context oldCtx)
    • syncNetworkTimeout

      private void syncNetworkTimeout(Context oldCtx, Client oldCli) throws SQLException
      Throws:
      SQLException
    • syncReadOnlyState

      private void syncReadOnlyState(Context oldCtx) throws SQLException
      Throws:
      SQLException
    • isReadOnlySyncRequired

      private boolean isReadOnlySyncRequired(Context oldCtx)
    • syncTransactionIsolation

      private void syncTransactionIsolation(Context oldCtx) throws SQLException
      Throws:
      SQLException
    • isTransactionIsolationSyncRequired

      private boolean isTransactionIsolationSyncRequired(Context oldCtx)
    • buildTransactionIsolationQuery

      private String buildTransactionIsolationQuery(int isolationLevel)
    • execute

      public List<Completion> execute(ClientMessage message, boolean canRedo) throws 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:
      SQLException - if execution fails
    • execute

      public List<Completion> execute(ClientMessage message, Statement stmt, boolean canRedo) throws 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:
      SQLException - if execution fails
    • execute

      public List<Completion> execute(ClientMessage message, Statement stmt, int fetchSize, long maxRows, int resultSetConcurrency, int resultSetType, boolean closeOnCompletion, boolean canRedo) throws 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:
      SQLException - if any error occurs
    • executePipeline

      public List<Completion> executePipeline(ClientMessage[] messages, Statement stmt, int fetchSize, long maxRows, int resultSetConcurrency, int resultSetType, boolean closeOnCompletion, boolean canRedo) throws 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:
      SQLException - if any error occurs
    • readStreamingResults

      public void readStreamingResults(List<Completion> completions, int fetchSize, long maxRows, int resultSetConcurrency, int resultSetType, boolean closeOnCompletion) throws 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:
      SQLException - if any error occurs
    • closePrepare

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

      public void abort(Executor executor) throws SQLException
      Description copied from interface: Client
      Abort current connection
      Specified by:
      abort in interface Client
      Parameters:
      executor - executor
      Throws:
      SQLException - if any error occurs
    • close

      public void close() throws SQLException
      Description copied from interface: Client
      Close client
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Client
      Throws:
      SQLException - if any error occurs
    • setReadOnly

      public void setReadOnly(boolean readOnly) throws 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:
      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 SQLException
      Description copied from interface: Client
      Set socket timeout
      Specified by:
      setSocketTimeout in interface Client
      Parameters:
      milliseconds - timeout
      Throws:
      SQLException - if any error occurs
    • 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
    • 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
    • getSocketIp

      public 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
    • reset

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