Package org.mariadb.jdbc.client.impl
Class MultiPrimaryClient
java.lang.Object
org.mariadb.jdbc.client.impl.MultiPrimaryClient
- All Implemented Interfaces:
AutoCloseable
,Client
- Direct Known Subclasses:
MultiPrimaryReplicaClient
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 Summary
FieldsModifier and TypeFieldDescriptionprotected boolean
is connections explicitly closedprotected final Configuration
configurationprotected Client
current clientprotected final long
denied timeoutprotected static final ConcurrentMap
<HostAddress, Long> temporary blacklisted hostsprotected final ClosableLock
thread lockerprivate static final Logger
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Abort current connectionprivate void
addToDenyList
(HostAddress host) private String
buildTransactionIsolationQuery
(int isolationLevel) void
close()
Close clientvoid
closePrepare
(Prepare prepare) Close prepare commandprotected Client
connectHost
(boolean readOnly, boolean failFast) Trying connecting server.private Client
createClient
(HostAddress host) execute
(ClientMessage message, boolean canRedo) Send client message and read resultexecute
(ClientMessage message, Statement stmt, boolean canRedo) Send client message and read resultexecute
(ClientMessage message, Statement stmt, int fetchSize, long maxRows, int resultSetConcurrency, int resultSetType, boolean closeOnCompletion, boolean canRedo) Send client message and read resultexecutePipeline
(ClientMessage[] messages, Statement stmt, int fetchSize, long maxRows, int resultSetConcurrency, int resultSetType, boolean closeOnCompletion, boolean canRedo) Send client messages pipelining and read resultprotected void
executeTransactionReplay
(Client oldCli) Execute transaction replayprivate Optional
<HostAddress> findHostWithLowestDenyTimeout
(boolean readOnly) Get connection contextGet connection exception factoryGet connection hostGet current socket IP or null (for Pipe / unix socket)int
get socket timeoutprivate boolean
isAutoCommitSyncRequired
(Context oldCtx) boolean
isClosed()
Is client closedprivate boolean
isDatabaseSyncRequired
(Context oldCtx) boolean
is current client writer or read-onlyprivate boolean
isReadOnlySyncRequired
(Context oldCtx) private boolean
void
readStreamingResults
(List<Completion> completions, int fetchSize, long maxRows, int resultSetConcurrency, int resultSetType, boolean closeOnCompletion) Read resultsprotected Client
Connection loopprotected void
replayIfPossible
(Client oldClient, boolean canRedo) Execute transaction replay if in transaction and configured for it, throw an exception if notvoid
reset()
Reset connectionvoid
setReadOnly
(boolean readOnly) Switch to a writer/read-only connection, no effet on mono-connectionvoid
setSocketTimeout
(int milliseconds) Set socket timeoutprivate void
private void
syncAutoCommit
(Context oldCtx) private void
syncDatabase
(Context oldCtx) private void
syncExceptionFactory
(Client oldCli) private void
syncNetworkTimeout
(Context oldCtx, Client oldCli) void
syncNewState
(Client oldCli) Synchronizes client states between previous and new clients.private void
syncReadOnlyState
(Context oldCtx) private void
syncTransactionIsolation
(Context oldCtx) private Client
tryConnectToAvailableHost
(boolean readOnly, int retriesLeft) private Client
tryConnectToDeniedHost
(boolean readOnly, int retriesLeft) private void
validateHostConfiguration
(boolean readOnly)
-
Field Details
-
denyList
temporary blacklisted hosts -
logger
-
deniedListTimeout
protected final long deniedListTimeoutdenied timeout -
conf
configuration -
lock
thread locker -
closed
protected boolean closedis connections explicitly closed -
currentClient
current client
-
-
Constructor Details
-
MultiPrimaryClient
Constructor- Parameters:
conf
- configurationlock
- thread locker- Throws:
SQLException
- if fail to connect
-
-
Method Details
-
connectHost
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 / primaryfailFast
- must try only not denied server- Returns:
- a valid connection client
- Throws:
SQLException
- if not succeed to create a connection.
-
tryConnectToAvailableHost
- Throws:
SQLException
-
tryConnectToDeniedHost
- Throws:
SQLException
-
findHostWithLowestDenyTimeout
-
validateHostConfiguration
-
createClient
- Throws:
SQLException
-
addToDenyList
-
sleepBeforeRetry
private void sleepBeforeRetry() -
reConnect
Connection loop- Returns:
- client connection
- Throws:
SQLException
- if fail to connect
-
replayIfPossible
Execute transaction replay if in transaction and configured for it, throw an exception if not- Parameters:
oldClient
- previous clientcanRedo
- if command can be redo even if not in transaction- Throws:
SQLException
- if not able to replay
-
executeTransactionReplay
Execute transaction replay- Parameters:
oldCli
- previous client- Throws:
SQLException
- if not able to replay
-
syncNewState
Synchronizes client states between previous and new clients.- Parameters:
oldCli
- previous client instance- Throws:
SQLException
- if synchronization error occurs
-
syncExceptionFactory
-
syncAutoCommit
- Throws:
SQLException
-
isAutoCommitSyncRequired
-
syncDatabase
- Throws:
SQLException
-
isDatabaseSyncRequired
-
syncNetworkTimeout
- Throws:
SQLException
-
syncReadOnlyState
- Throws:
SQLException
-
isReadOnlySyncRequired
-
syncTransactionIsolation
- Throws:
SQLException
-
isTransactionIsolationSyncRequired
-
buildTransactionIsolationQuery
-
execute
Description copied from interface:Client
Send client message and read result- Specified by:
execute
in interfaceClient
- Parameters:
message
- client messagecanRedo
- 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 interfaceClient
- Parameters:
message
- client messagestmt
- statementcanRedo
- 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 interfaceClient
- Parameters:
message
- client messagestmt
- statementfetchSize
- fetch sizemaxRows
- maximum number of rows. 0 = allresultSetConcurrency
- concurrencyresultSetType
- result-set typecloseOnCompletion
- close statement on completioncanRedo
- 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 interfaceClient
- Parameters:
messages
- client messagestmt
- statementfetchSize
- fetch sizemaxRows
- maximum number of rows. 0 = allresultSetConcurrency
- concurrencyresultSetType
- result-set typecloseOnCompletion
- close statement on completioncanRedo
- 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 interfaceClient
- Parameters:
completions
- List that will have the new resultsfetchSize
- fetch sizemaxRows
- maximum number of rows. 0 = allresultSetConcurrency
- concurrencyresultSetType
- result-set typecloseOnCompletion
- close statement on completion- Throws:
SQLException
- if any error occurs
-
closePrepare
Description copied from interface:Client
Close prepare command- Specified by:
closePrepare
in interfaceClient
- Parameters:
prepare
- prepare command- Throws:
SQLException
- if any error occurs
-
abort
Description copied from interface:Client
Abort current connection- Specified by:
abort
in interfaceClient
- Parameters:
executor
- executor- Throws:
SQLException
- if any error occurs
-
close
Description copied from interface:Client
Close client- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceClient
- Throws:
SQLException
- if any error occurs
-
setReadOnly
Description copied from interface:Client
Switch to a writer/read-only connection, no effet on mono-connection- Specified by:
setReadOnly
in interfaceClient
- 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 interfaceClient
- Returns:
- socket timeout
-
setSocketTimeout
Description copied from interface:Client
Set socket timeout- Specified by:
setSocketTimeout
in interfaceClient
- Parameters:
milliseconds
- timeout- Throws:
SQLException
- if any error occurs
-
isClosed
public boolean isClosed()Description copied from interface:Client
Is client closed -
getContext
Description copied from interface:Client
Get connection context- Specified by:
getContext
in interfaceClient
- Returns:
- connection context
-
getExceptionFactory
Description copied from interface:Client
Get connection exception factory- Specified by:
getExceptionFactory
in interfaceClient
- Returns:
- connection exception factory
-
getHostAddress
Description copied from interface:Client
Get connection host- Specified by:
getHostAddress
in interfaceClient
- Returns:
- connection host
-
getSocketIp
Description copied from interface:Client
Get current socket IP or null (for Pipe / unix socket)- Specified by:
getSocketIp
in interfaceClient
- Returns:
- Socket current IP
-
isPrimary
public boolean isPrimary()Description copied from interface:Client
is current client writer or read-only -
reset
public void reset()Description copied from interface:Client
Reset connection
-