Class AbstractMastersListener

java.lang.Object
org.mariadb.jdbc.internal.failover.AbstractMastersListener
All Implemented Interfaces:
Listener
Direct Known Subclasses:
AbstractMastersReplicasListener, MastersFailoverListener

public abstract class AbstractMastersListener extends Object implements Listener
  • Field Details

    • blacklist

      private static final ConcurrentMap<HostAddress,Long> blacklist
      List the recent failedConnection.
    • connectionValidationLoop

      private static final ConnectionValidator connectionValidationLoop
    • logger

      private static final Logger logger
    • urlParser

      public final UrlParser urlParser
    • currentConnectionAttempts

      protected final AtomicInteger currentConnectionAttempts
    • explicitClosed

      protected final AtomicBoolean explicitClosed
    • globalInfo

      protected final GlobalStateInfo globalInfo
    • masterHostFail

      private final AtomicBoolean masterHostFail
    • currentReadOnlyAsked

      protected volatile boolean currentReadOnlyAsked
    • currentProtocol

      protected Protocol currentProtocol
    • proxy

      protected FailoverProxy proxy
    • lastRetry

      protected long lastRetry
    • lastQueryNanos

      protected long lastQueryNanos
    • masterHostFailNanos

      private volatile long masterHostFailNanos
  • Constructor Details

  • Method Details

    • clearBlacklist

      public static void clearBlacklist()
      Clear blacklist data.
    • initializeConnection

      public void initializeConnection() throws SQLException
      Initialize Listener. This listener will be added to the connection validation loop according to option value so the connection will be verified periodically. (Important for aurora, for other, connection pool often have this functionality)
      Specified by:
      initializeConnection in interface Listener
      Throws:
      SQLException - if any exception occur.
    • removeListenerFromSchedulers

      protected void removeListenerFromSchedulers()
    • preAutoReconnect

      protected void preAutoReconnect() throws SQLException
      Throws:
      SQLException
    • getProxy

      public FailoverProxy getProxy()
      Specified by:
      getProxy in interface Listener
    • setProxy

      public void setProxy(FailoverProxy proxy)
      Specified by:
      setProxy in interface Listener
    • getBlacklistKeys

      public Set<HostAddress> getBlacklistKeys()
      Specified by:
      getBlacklistKeys in interface Listener
    • handleFailover

      public HandleErrorResult handleFailover(SQLException qe, Method method, Object[] args, Protocol protocol, boolean isClosed) throws SQLException
      Call when a failover is detected on master connection. Will :
      1. set fail variable
      2. try to reconnect
      3. relaunch query if possible
      Specified by:
      handleFailover in interface Listener
      Parameters:
      method - called method
      args - methods parameters
      protocol - current protocol
      Returns:
      a HandleErrorResult object to indicate if query has been relaunched, and the exception if not
      Throws:
      SQLException - when method and parameters does not exist.
    • addToBlacklist

      public void addToBlacklist(HostAddress hostAddress)
      After a failover, put the hostAddress in a static list so the other connection will not take this host in account for a time.
      Specified by:
      addToBlacklist in interface Listener
      Parameters:
      hostAddress - the HostAddress to add to blacklist
    • removeFromBlacklist

      public void removeFromBlacklist(HostAddress hostAddress)
      After a successfull connection, permit to remove a hostAddress from blacklist.
      Specified by:
      removeFromBlacklist in interface Listener
      Parameters:
      hostAddress - the host address tho be remove of blacklist
    • resetOldsBlackListHosts

      public void resetOldsBlackListHosts()
      Permit to remove Host to blacklist after loadBalanceBlacklistTimeout seconds.
    • resetMasterFailoverData

      protected void resetMasterFailoverData()
    • setSessionReadOnly

      protected void setSessionReadOnly(boolean readOnly, Protocol protocol) throws SQLException
      Throws:
      SQLException
    • handleFailLoop

      public abstract void handleFailLoop()
    • getCurrentProtocol

      public Protocol getCurrentProtocol()
      Specified by:
      getCurrentProtocol in interface Listener
    • getMasterHostFailNanos

      public long getMasterHostFailNanos()
    • setMasterHostFail

      public boolean setMasterHostFail()
      Set master fail variables.
      Specified by:
      setMasterHostFail in interface Listener
      Returns:
      true if was already failed
    • isMasterHostFail

      public boolean isMasterHostFail()
      Specified by:
      isMasterHostFail in interface Listener
    • hasHostFail

      public boolean hasHostFail()
      Specified by:
      hasHostFail in interface Listener
    • getFilterForFailedHost

      public SearchFilter getFilterForFailedHost()
      Specified by:
      getFilterForFailedHost in interface Listener
    • relaunchOperation

      public HandleErrorResult relaunchOperation(Method method, Object[] args) throws SQLException
      After a failover that has bean done, relaunch the operation that was in progress. In case of special operation that crash server, doesn't relaunched it;
      Parameters:
      method - the method accessed
      args - the parameters
      Returns:
      An object that indicate the result or that the exception as to be thrown
      Throws:
      SQLException - if there is any error relaunching initial method
    • isQueryRelaunchable

      public boolean isQueryRelaunchable(Method method, Object[] args)
      Check if query can be re-executed.
      Parameters:
      method - invoke method
      args - invoke arguments
      Returns:
      true if can be re-executed
    • invoke

      public Object invoke(Method method, Object[] args, Protocol specificProtocol) throws Throwable
      Specified by:
      invoke in interface Listener
      Throws:
      Throwable
    • invoke

      public Object invoke(Method method, Object[] args) throws Throwable
      Specified by:
      invoke in interface Listener
      Throws:
      Throwable
    • syncConnection

      public void syncConnection(Protocol from, Protocol to) throws SQLException
      When switching between 2 connections, report existing connection parameter to the new used connection.
      Specified by:
      syncConnection in interface Listener
      Parameters:
      from - used connection
      to - will-be-current connection
      Throws:
      SQLException - if catalog cannot be set
    • versionGreaterOrEqual

      public boolean versionGreaterOrEqual(int major, int minor, int patch)
      Specified by:
      versionGreaterOrEqual in interface Listener
    • isServerMariaDb

      public boolean isServerMariaDb()
      Specified by:
      isServerMariaDb in interface Listener
    • sessionStateAware

      public boolean sessionStateAware()
      Specified by:
      sessionStateAware in interface Listener
    • noBackslashEscapes

      public boolean noBackslashEscapes()
      Specified by:
      noBackslashEscapes in interface Listener
    • getMajorServerVersion

      public int getMajorServerVersion()
      Specified by:
      getMajorServerVersion in interface Listener
    • isClosed

      public boolean isClosed()
      Specified by:
      isClosed in interface Listener
    • isValid

      public boolean isValid(int timeout) throws SQLException
      Specified by:
      isValid in interface Listener
      Throws:
      SQLException
    • isReadOnly

      public boolean isReadOnly()
      Specified by:
      isReadOnly in interface Listener
    • inTransaction

      public boolean inTransaction()
      Specified by:
      inTransaction in interface Listener
    • isMasterConnection

      public boolean isMasterConnection()
      Specified by:
      isMasterConnection in interface Listener
    • isExplicitClosed

      public boolean isExplicitClosed()
      Specified by:
      isExplicitClosed in interface Listener
    • getRetriesAllDown

      public int getRetriesAllDown()
      Specified by:
      getRetriesAllDown in interface Listener
    • isAutoReconnect

      public boolean isAutoReconnect()
      Specified by:
      isAutoReconnect in interface Listener
    • getUrlParser

      public UrlParser getUrlParser()
      Specified by:
      getUrlParser in interface Listener
    • preExecute

      public abstract void preExecute() throws SQLException
      Specified by:
      preExecute in interface Listener
      Throws:
      SQLException
    • preClose

      public abstract void preClose()
      Specified by:
      preClose in interface Listener
    • reconnectFailedConnection

      public abstract void reconnectFailedConnection(SearchFilter filter) throws SQLException
      Specified by:
      reconnectFailedConnection in interface Listener
      Throws:
      SQLException
    • switchReadOnlyConnection

      public abstract void switchReadOnlyConnection(Boolean readonly) throws SQLException
      Specified by:
      switchReadOnlyConnection in interface Listener
      Throws:
      SQLException
    • primaryFail

      public abstract HandleErrorResult primaryFail(Method method, Object[] args, boolean killCmd, boolean isClosed) throws SQLException
      Specified by:
      primaryFail in interface Listener
      Throws:
      SQLException
    • throwFailoverMessage

      public void throwFailoverMessage(HostAddress failHostAddress, boolean wasMaster, SQLException queryException, boolean reconnected) throws SQLException
      Throw a human readable message after a failoverException.
      Specified by:
      throwFailoverMessage in interface Listener
      Parameters:
      failHostAddress - failedHostAddress
      wasMaster - was failed connection master
      queryException - internal error
      reconnected - connection status
      Throws:
      SQLException - error with failover information
    • canRetryFailLoop

      public boolean canRetryFailLoop()
      Specified by:
      canRetryFailLoop in interface Listener
    • prolog

      public void prolog(long maxRows, MariaDbConnection connection, MariaDbStatement statement) throws SQLException
      Specified by:
      prolog in interface Listener
      Throws:
      SQLException
    • getCatalog

      public String getCatalog() throws SQLException
      Specified by:
      getCatalog in interface Listener
      Throws:
      SQLException
    • getTimeout

      public int getTimeout() throws SocketException
      Specified by:
      getTimeout in interface Listener
      Throws:
      SocketException
    • reconnect

      public abstract void reconnect() throws SQLException
      Specified by:
      reconnect in interface Listener
      Throws:
      SQLException
    • checkMasterStatus

      public abstract boolean checkMasterStatus(SearchFilter searchFilter)
      Specified by:
      checkMasterStatus in interface Listener
    • getLastQueryNanos

      public long getLastQueryNanos()
      Specified by:
      getLastQueryNanos in interface Listener
    • pingMasterProtocol

      protected boolean pingMasterProtocol(Protocol protocol)
    • closeConnection

      public void closeConnection(Protocol protocol)
      Utility to close existing connection.
      Parameters:
      protocol - connection to close.
    • abortConnection

      public void abortConnection(Protocol protocol)
      Utility to force close existing connection.
      Parameters:
      protocol - connection to close.