Class MastersReplicasListener
- java.lang.Object
-
- org.mariadb.jdbc.internal.failover.AbstractMastersListener
-
- org.mariadb.jdbc.internal.failover.AbstractMastersReplicasListener
-
- org.mariadb.jdbc.internal.failover.impl.MastersReplicasListener
-
- All Implemented Interfaces:
Listener
- Direct Known Subclasses:
AuroraListener
public class MastersReplicasListener extends AbstractMastersReplicasListener
this class handle the operation when multiple hosts.
-
-
Field Summary
Fields Modifier and Type Field Description private static DynamicSizedSchedulerInterfacedynamicSizedSchedulerprotected java.util.List<HostAddress>hostAddressesprivate static java.util.concurrent.atomic.AtomicIntegerlistenerCountprivate static Loggerloggerprotected ProtocolmasterProtocolprotected ProtocolsecondaryProtocol-
Fields inherited from class org.mariadb.jdbc.internal.failover.AbstractMastersReplicasListener
waitNewMasterProtocol, waitNewSecondaryProtocol
-
Fields inherited from class org.mariadb.jdbc.internal.failover.AbstractMastersListener
currentConnectionAttempts, currentProtocol, currentReadOnlyAsked, explicitClosed, globalInfo, lastQueryNanos, lastRetry, proxy, urlParser
-
-
Constructor Summary
Constructors Constructor Description MastersReplicasListener(UrlParser urlParser, GlobalStateInfo globalInfo)Initialisation.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidblackListAndCloseConnection(Protocol protocol)protected voidcheckInitialConnection(java.sql.SQLException queryException)booleancheckMasterStatus(SearchFilter searchFilter)Check master status.voidcheckWaitingConnection()Verify that there is waiting connection that have to replace failing one.java.util.List<HostAddress>connectedHosts()List current connected HostAddress.voidfoundActiveMaster(Protocol newMasterProtocol)Method called when a new Master connection is found after a fallback.voidfoundActiveSecondary(Protocol newSecondaryProtocol)Method called when a new secondary connection is found after a fallback.java.lang.StringgetCatalog()intgetMajorServerVersion()longgetServerThreadId()Get current connection server id.intgetTimeout()Get timeout (master connection possibly down).voidhandleFailLoop()voidinitializeConnection()Initialize connections.booleaninTransaction()Indicate if connection has an active transaction.java.lang.Objectinvoke(java.lang.reflect.Method method, java.lang.Object[] args)booleanisClosed()booleanisMasterConnected()booleanisMasterConnection()booleanisServerMariaDb()booleanisValid(int timeout)private static voidloadScheduler()voidlockAndSwitchMaster(Protocol newMasterProtocol)Use the parameter newMasterProtocol as new current master connection.voidlockAndSwitchSecondary(Protocol newSecondaryProtocol)Use the parameter newSecondaryProtocol as new current secondary connection.booleannoBackslashEscapes()private booleanpingSecondaryProtocol(Protocol protocol)Ping secondary protocol.voidpreAbort()voidpreClose()Called after a call on Connection.close().voidpreExecute()HandleErrorResultprimaryFail(java.lang.reflect.Method method, java.lang.Object[] args, boolean killCmd, boolean isClosed)To handle the newly detected failover on the master connection.voidprolog(long maxRows, MariaDbConnection connection, MariaDbStatement statement)voidreconnect()Reconnect failed connection.voidreconnectFailedConnection(SearchFilter searchFilter)Loop to connect.protected voidremoveListenerFromSchedulers()voidrePrepareOnReplica(ServerPrepareResult oldServerPrepareResult, boolean mustBeOnMaster)voidreset()Reset state of master and replica connection.HandleErrorResultsecondaryFail(java.lang.reflect.Method method, java.lang.Object[] args, boolean killCmd)To handle the newly detected failover on the secondary connection.booleansessionStateAware()voidswitchReadOnlyConnection(java.lang.Boolean mustBeReadOnly)Switch to a read-only(secondary) or read and write connection(master).booleanversionGreaterOrEqual(int major, int minor, int patch)-
Methods inherited from class org.mariadb.jdbc.internal.failover.AbstractMastersReplicasListener
getFilterForFailedHost, getSecondaryHostFailNanos, handleFailover, hasHostFail, isMasterHostFailReconnect, isSecondaryHostFail, isSecondaryHostFailReconnect, resetMasterFailoverData, resetSecondaryFailoverData, setSecondaryHostFail
-
Methods inherited from class org.mariadb.jdbc.internal.failover.AbstractMastersListener
abortConnection, addToBlacklist, canRetryFailLoop, clearBlacklist, closeConnection, getBlacklistKeys, getCurrentProtocol, getLastQueryNanos, getMasterHostFailNanos, getProxy, getRetriesAllDown, getUrlParser, invoke, isAutoReconnect, isExplicitClosed, isMasterHostFail, isQueryRelaunchable, isReadOnly, pingMasterProtocol, preAutoReconnect, relaunchOperation, removeFromBlacklist, resetOldsBlackListHosts, setMasterHostFail, setProxy, setSessionReadOnly, syncConnection, throwFailoverMessage
-
-
-
-
Field Detail
-
listenerCount
private static final java.util.concurrent.atomic.AtomicInteger listenerCount
-
logger
private static final Logger logger
-
dynamicSizedScheduler
private static DynamicSizedSchedulerInterface dynamicSizedScheduler
-
masterProtocol
protected Protocol masterProtocol
-
secondaryProtocol
protected Protocol secondaryProtocol
-
hostAddresses
protected java.util.List<HostAddress> hostAddresses
-
-
Constructor Detail
-
MastersReplicasListener
public MastersReplicasListener(UrlParser urlParser, GlobalStateInfo globalInfo)
Initialisation.- Parameters:
urlParser- connection string object.globalInfo- server global variables information
-
-
Method Detail
-
loadScheduler
private static void loadScheduler()
-
removeListenerFromSchedulers
protected void removeListenerFromSchedulers()
- Overrides:
removeListenerFromSchedulersin classAbstractMastersListener
-
initializeConnection
public void initializeConnection() throws java.sql.SQLExceptionInitialize connections.- Specified by:
initializeConnectionin interfaceListener- Overrides:
initializeConnectionin classAbstractMastersListener- Throws:
java.sql.SQLException- if a connection error append.
-
isClosed
public boolean isClosed()
- Specified by:
isClosedin interfaceListener- Overrides:
isClosedin classAbstractMastersListener
-
invoke
public java.lang.Object invoke(java.lang.reflect.Method method, java.lang.Object[] args) throws java.lang.Throwable- Specified by:
invokein interfaceListener- Overrides:
invokein classAbstractMastersListener- Throws:
java.lang.Throwable
-
versionGreaterOrEqual
public boolean versionGreaterOrEqual(int major, int minor, int patch)- Specified by:
versionGreaterOrEqualin interfaceListener- Overrides:
versionGreaterOrEqualin classAbstractMastersListener
-
isServerMariaDb
public boolean isServerMariaDb()
- Specified by:
isServerMariaDbin interfaceListener- Overrides:
isServerMariaDbin classAbstractMastersListener
-
sessionStateAware
public boolean sessionStateAware()
- Specified by:
sessionStateAwarein interfaceListener- Overrides:
sessionStateAwarein classAbstractMastersListener
-
getCatalog
public java.lang.String getCatalog() throws java.sql.SQLException- Specified by:
getCatalogin interfaceListener- Overrides:
getCatalogin classAbstractMastersListener- Throws:
java.sql.SQLException
-
getMajorServerVersion
public int getMajorServerVersion()
- Specified by:
getMajorServerVersionin interfaceListener- Overrides:
getMajorServerVersionin classAbstractMastersListener
-
isMasterConnection
public boolean isMasterConnection()
- Specified by:
isMasterConnectionin interfaceListener- Overrides:
isMasterConnectionin classAbstractMastersListener
-
getTimeout
public int getTimeout() throws java.net.SocketExceptionGet timeout (master connection possibly down).- Specified by:
getTimeoutin interfaceListener- Overrides:
getTimeoutin classAbstractMastersListener- Returns:
- socket timeout in ms
- Throws:
java.net.SocketException- if socket exception
-
prolog
public void prolog(long maxRows, MariaDbConnection connection, MariaDbStatement statement) throws java.sql.SQLException- Specified by:
prologin interfaceListener- Overrides:
prologin classAbstractMastersListener- Throws:
java.sql.SQLException
-
noBackslashEscapes
public boolean noBackslashEscapes()
- Specified by:
noBackslashEscapesin interfaceListener- Overrides:
noBackslashEscapesin classAbstractMastersListener
-
getServerThreadId
public long getServerThreadId()
Get current connection server id.- Returns:
- server id.
-
checkInitialConnection
protected void checkInitialConnection(java.sql.SQLException queryException) throws java.sql.SQLException- Throws:
java.sql.SQLException
-
preClose
public void preClose()
Called after a call on Connection.close(). Will explicitly closed all connections.- Specified by:
preClosein interfaceListener- Specified by:
preClosein classAbstractMastersListener
-
preAbort
public void preAbort()
-
preExecute
public void preExecute() throws java.sql.SQLException- Specified by:
preExecutein interfaceListener- Specified by:
preExecutein classAbstractMastersListener- Throws:
java.sql.SQLException
-
isValid
public boolean isValid(int timeout) throws java.sql.SQLException- Specified by:
isValidin interfaceListener- Overrides:
isValidin classAbstractMastersListener- Throws:
java.sql.SQLException
-
checkWaitingConnection
public void checkWaitingConnection() throws java.sql.SQLExceptionVerify that there is waiting connection that have to replace failing one. If there is replace failed connection with new one.- Throws:
java.sql.SQLException- if error occur
-
reconnectFailedConnection
public void reconnectFailedConnection(SearchFilter searchFilter) throws java.sql.SQLException
Loop to connect.- Specified by:
reconnectFailedConnectionin interfaceListener- Specified by:
reconnectFailedConnectionin classAbstractMastersListener- Throws:
java.sql.SQLException- if there is any error during reconnection
-
foundActiveMaster
public void foundActiveMaster(Protocol newMasterProtocol)
Method called when a new Master connection is found after a fallback.- Parameters:
newMasterProtocol- the new active connection
-
lockAndSwitchMaster
public void lockAndSwitchMaster(Protocol newMasterProtocol) throws ReconnectDuringTransactionException
Use the parameter newMasterProtocol as new current master connection.Lock must be set
- Parameters:
newMasterProtocol- new master connection- Throws:
ReconnectDuringTransactionException- if there was an active transaction.
-
foundActiveSecondary
public void foundActiveSecondary(Protocol newSecondaryProtocol) throws java.sql.SQLException
Method called when a new secondary connection is found after a fallback.- Specified by:
foundActiveSecondaryin classAbstractMastersReplicasListener- Parameters:
newSecondaryProtocol- the new active connection- Throws:
java.sql.SQLException- if switch failed
-
lockAndSwitchSecondary
public void lockAndSwitchSecondary(Protocol newSecondaryProtocol) throws java.sql.SQLException
Use the parameter newSecondaryProtocol as new current secondary connection.- Parameters:
newSecondaryProtocol- new secondary connection- Throws:
java.sql.SQLException- if an error occur during setting session read-only
-
switchReadOnlyConnection
public void switchReadOnlyConnection(java.lang.Boolean mustBeReadOnly) throws java.sql.SQLExceptionSwitch to a read-only(secondary) or read and write connection(master).- Specified by:
switchReadOnlyConnectionin interfaceListener- Specified by:
switchReadOnlyConnectionin classAbstractMastersListener- Parameters:
mustBeReadOnly- the read-only status asked- Throws:
java.sql.SQLException- if operation hasn't change protocol
-
primaryFail
public HandleErrorResult primaryFail(java.lang.reflect.Method method, java.lang.Object[] args, boolean killCmd, boolean isClosed)
To handle the newly detected failover on the master connection.- Specified by:
primaryFailin interfaceListener- Specified by:
primaryFailin classAbstractMastersListener- Parameters:
method- the initial called methodargs- the initial argskillCmd- is the fail due to a KILL cmd- Returns:
- an object to indicate if the previous Exception must be thrown, or the object resulting if a failover worked
-
blackListAndCloseConnection
private void blackListAndCloseConnection(Protocol protocol)
-
reconnect
public void reconnect() throws java.sql.SQLExceptionReconnect failed connection.- Specified by:
reconnectin interfaceListener- Specified by:
reconnectin classAbstractMastersListener- Throws:
java.sql.SQLException- if reconnection has failed
-
pingSecondaryProtocol
private boolean pingSecondaryProtocol(Protocol protocol)
Ping secondary protocol. ! lock must be set !- Parameters:
protocol- socket to ping- Returns:
- true if ping is valid.
-
secondaryFail
public HandleErrorResult secondaryFail(java.lang.reflect.Method method, java.lang.Object[] args, boolean killCmd) throws java.sql.SQLException
To handle the newly detected failover on the secondary connection.- Specified by:
secondaryFailin classAbstractMastersReplicasListener- Parameters:
method- the initial called methodargs- the initial argskillCmd- is fail due to a KILL command- Returns:
- an object to indicate if the previous Exception must be thrown, or the object resulting if a failover worked
- Throws:
java.sql.SQLException- if relaunch operation fails
-
handleFailLoop
public void handleFailLoop()
- Specified by:
handleFailLoopin classAbstractMastersListener
-
isMasterConnected
public boolean isMasterConnected()
-
inTransaction
public boolean inTransaction()
Indicate if connection has an active transaction.- Specified by:
inTransactionin interfaceListener- Overrides:
inTransactionin classAbstractMastersListener- Returns:
- boolean
-
checkMasterStatus
public boolean checkMasterStatus(SearchFilter searchFilter)
Check master status.- Specified by:
checkMasterStatusin interfaceListener- Specified by:
checkMasterStatusin classAbstractMastersListener- Parameters:
searchFilter- search filter- Returns:
- has some status changed
-
rePrepareOnReplica
public void rePrepareOnReplica(ServerPrepareResult oldServerPrepareResult, boolean mustBeOnMaster) throws java.sql.SQLException
- Throws:
java.sql.SQLException
-
connectedHosts
public java.util.List<HostAddress> connectedHosts()
List current connected HostAddress.- Returns:
- hostAddress List.
-
reset
public void reset() throws java.sql.SQLExceptionReset state of master and replica connection.- Throws:
java.sql.SQLException- if command fail.
-
-