org.mariadb.jdbc.internal.failover
Class AbstractMastersListener

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

public abstract class AbstractMastersListener
extends Object
implements Listener


Field Summary
protected  AtomicInteger currentConnectionAttempts
           
protected  Protocol currentProtocol
           
protected  boolean currentReadOnlyAsked
           
protected  AtomicBoolean explicitClosed
           
protected  long lastQueryNanos
           
protected  long lastRetry
           
protected  FailoverProxy proxy
           
 UrlParser urlParser
           
 
Constructor Summary
protected AbstractMastersListener(UrlParser urlParser)
           
 
Method Summary
 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.
 boolean canRetryFailLoop()
           
abstract  boolean checkMasterStatus(SearchFilter searchFilter)
           
static void clearBlacklist()
          Clear blacklist data.
 void closeConnection(Protocol protocol)
          Utility to close existing connection.
 Set<HostAddress> getBlacklistKeys()
           
 Protocol getCurrentProtocol()
           
 SearchFilter getFilterForFailedHost()
           
 long getLastQueryNanos()
           
 long getMasterHostFailNanos()
           
 FailoverProxy getProxy()
           
 int getRetriesAllDown()
           
 UrlParser getUrlParser()
           
abstract  void handleFailLoop()
           
 HandleErrorResult handleFailover(SQLException qe, Method method, Object[] args, Protocol protocol)
          Call when a failover is detected on master connection.
 boolean hasHostFail()
           
 void initializeConnection()
          Initialize Listener.
 Object invoke(Method method, Object[] args)
           
 Object invoke(Method method, Object[] args, Protocol specificProtocol)
           
 boolean isAutoReconnect()
           
 boolean isClosed()
           
 boolean isExplicitClosed()
           
 boolean isMasterHostFail()
           
 boolean isQueryRelaunchable(Method method, Object[] args)
          Check if query can be re-executed.
 boolean isReadOnly()
           
protected  boolean pingMasterProtocol(Protocol protocol)
           
protected  void preAutoReconnect()
           
abstract  void preClose()
           
abstract  void preExecute()
           
abstract  HandleErrorResult primaryFail(Method method, Object[] args)
           
abstract  void reconnect()
           
abstract  void reconnectFailedConnection(SearchFilter filter)
           
 HandleErrorResult relaunchOperation(Method method, Object[] args)
          After a failover that has bean done, relaunch the operation that was in progress.
 void removeFromBlacklist(HostAddress hostAddress)
          After a successfull connection, permit to remove a hostAddress from blacklist.
protected  void removeListenerFromSchedulers()
           
protected  void resetMasterFailoverData()
           
 void resetOldsBlackListHosts()
          Permit to remove Host to blacklist after loadBalanceBlacklistTimeout seconds.
 boolean setMasterHostFail()
          Set master fail variables.
 void setProxy(FailoverProxy proxy)
           
protected  void setSessionReadOnly(boolean readOnly, Protocol protocol)
           
abstract  void switchReadOnlyConnection(Boolean readonly)
           
 void syncConnection(Protocol from, Protocol to)
          When switching between 2 connections, report existing connection parameter to the new used connection.
 void throwFailoverMessage(HostAddress failHostAddress, boolean wasMaster, SQLException queryException, boolean reconnected)
          Throw a human readable message after a failoverException.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.mariadb.jdbc.internal.failover.Listener
foundActiveMaster, isMasterConnected, rePrepareOnSlave
 

Field Detail

urlParser

public final UrlParser urlParser

currentConnectionAttempts

protected AtomicInteger currentConnectionAttempts

currentReadOnlyAsked

protected volatile boolean currentReadOnlyAsked

currentProtocol

protected Protocol currentProtocol

proxy

protected FailoverProxy proxy

lastRetry

protected long lastRetry

explicitClosed

protected AtomicBoolean explicitClosed

lastQueryNanos

protected long lastQueryNanos
Constructor Detail

AbstractMastersListener

protected AbstractMastersListener(UrlParser urlParser)
Method Detail

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)
                                 throws Throwable
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:
Throwable - 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 IllegalAccessException,
                                           InvocationTargetException
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 methode accessed
args - the parameters
Returns:
An object that indicate the result or that the exception as to be thrown
Throws:
IllegalAccessException - if the initial call is not permit
InvocationTargetException - 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

isClosed

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

isReadOnly

public boolean isReadOnly()
Specified by:
isReadOnly 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()
                       throws SQLException
Specified by:
preClose in interface Listener
Throws:
SQLException

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)
                                       throws Throwable
Specified by:
primaryFail in interface Listener
Throws:
Throwable

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

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.


Copyright © 2017. All rights reserved.