Package org.mariadb.jdbc.client.impl
Class MultiPrimaryClient
- java.lang.Object
-
- org.mariadb.jdbc.client.impl.MultiPrimaryClient
-
- All Implemented Interfaces:
java.lang.AutoCloseable,Client
- Direct Known Subclasses:
MultiPrimaryReplicaClient
public class MultiPrimaryClient extends java.lang.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 Summary
Fields Modifier and Type Field Description protected booleanclosedis connections explicitly closedprotected Configurationconfconfigurationprotected ClientcurrentClientcurrent clientprotected longdeniedListTimeoutdenied timeoutprotected static java.util.concurrent.ConcurrentMap<HostAddress,java.lang.Long>denyListtemporary blacklisted hostsprotected ClosableLocklockthread lockerprivate static Loggerlogger
-
Constructor Summary
Constructors Constructor Description MultiPrimaryClient(Configuration conf, ClosableLock lock)Constructor
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidabort(java.util.concurrent.Executor executor)Abort current connectionvoidclose()Close clientvoidclosePrepare(Prepare prepare)Close prepare commandprotected ClientconnectHost(boolean readOnly, boolean failFast)Trying connecting server.java.util.List<Completion>execute(ClientMessage message, boolean canRedo)Send client message and read resultjava.util.List<Completion>execute(ClientMessage message, Statement stmt, boolean canRedo)Send client message and read resultjava.util.List<Completion>execute(ClientMessage message, Statement stmt, int fetchSize, long maxRows, int resultSetConcurrency, int resultSetType, boolean closeOnCompletion, boolean canRedo)Send client message and read resultjava.util.List<Completion>executePipeline(ClientMessage[] messages, Statement stmt, int fetchSize, long maxRows, int resultSetConcurrency, int resultSetType, boolean closeOnCompletion, boolean canRedo)Send client messages pipelining and read resultprotected voidexecuteTransactionReplay(Client oldCli)Execute transaction replayContextgetContext()Get connection contextExceptionFactorygetExceptionFactory()Get connection exception factoryHostAddressgetHostAddress()Get connection hostjava.lang.StringgetSocketIp()Get current socket IP or null (for Pipe / unix socket)intgetSocketTimeout()get socket timeoutbooleanisClosed()Is client closedbooleanisPrimary()is current client writer or read-onlyvoidreadStreamingResults(java.util.List<Completion> completions, int fetchSize, long maxRows, int resultSetConcurrency, int resultSetType, boolean closeOnCompletion)Read resultsprotected ClientreConnect()Connection loopprotected voidreplayIfPossible(Client oldClient, boolean canRedo)Execute transaction replay if in transaction and configured for it, throw an exception if notvoidreset()Reset connectionvoidsetReadOnly(boolean readOnly)Switch to a writer/read-only connection, no effet on mono-connectionvoidsetSocketTimeout(int milliseconds)Set socket timeoutvoidsyncNewState(Client oldCli)Synchronized previous and new client states.
-
-
-
Field Detail
-
denyList
protected static final java.util.concurrent.ConcurrentMap<HostAddress,java.lang.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 Detail
-
MultiPrimaryClient
public MultiPrimaryClient(Configuration conf, ClosableLock lock) throws java.sql.SQLException
Constructor- Parameters:
conf- configurationlock- thread locker- Throws:
java.sql.SQLException- if fail to connect
-
-
Method Detail
-
connectHost
protected Client connectHost(boolean readOnly, boolean failFast) throws java.sql.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 / primaryfailFast- must try only not denied server- Returns:
- a valid connection client
- Throws:
java.sql.SQLException- if not succeed to create a connection.
-
reConnect
protected Client reConnect() throws java.sql.SQLException
Connection loop- Returns:
- client connection
- Throws:
java.sql.SQLException- if fail to connect
-
replayIfPossible
protected void replayIfPossible(Client oldClient, boolean canRedo) throws java.sql.SQLException
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:
java.sql.SQLException- if not able to replay
-
executeTransactionReplay
protected void executeTransactionReplay(Client oldCli) throws java.sql.SQLException
Execute transaction replay- Parameters:
oldCli- previous client- Throws:
java.sql.SQLException- if not able to replay
-
syncNewState
public void syncNewState(Client oldCli) throws java.sql.SQLException
Synchronized previous and new client states.- Parameters:
oldCli- previous client- Throws:
java.sql.SQLException- if error occurs
-
execute
public java.util.List<Completion> execute(ClientMessage message, boolean canRedo) throws java.sql.SQLException
Description copied from interface:ClientSend client message and read result
-
execute
public java.util.List<Completion> execute(ClientMessage message, Statement stmt, boolean canRedo) throws java.sql.SQLException
Description copied from interface:ClientSend client message and read result
-
execute
public java.util.List<Completion> execute(ClientMessage message, Statement stmt, int fetchSize, long maxRows, int resultSetConcurrency, int resultSetType, boolean closeOnCompletion, boolean canRedo) throws java.sql.SQLException
Description copied from interface:ClientSend client message and read result- Specified by:
executein 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:
java.sql.SQLException- if any error occurs
-
executePipeline
public java.util.List<Completion> executePipeline(ClientMessage[] messages, Statement stmt, int fetchSize, long maxRows, int resultSetConcurrency, int resultSetType, boolean closeOnCompletion, boolean canRedo) throws java.sql.SQLException
Description copied from interface:ClientSend client messages pipelining and read result- Specified by:
executePipelinein 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:
java.sql.SQLException- if any error occurs
-
readStreamingResults
public void readStreamingResults(java.util.List<Completion> completions, int fetchSize, long maxRows, int resultSetConcurrency, int resultSetType, boolean closeOnCompletion) throws java.sql.SQLException
Description copied from interface:ClientRead results- Specified by:
readStreamingResultsin 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:
java.sql.SQLException- if any error occurs
-
closePrepare
public void closePrepare(Prepare prepare) throws java.sql.SQLException
Description copied from interface:ClientClose prepare command- Specified by:
closePreparein interfaceClient- Parameters:
prepare- prepare command- Throws:
java.sql.SQLException- if any error occurs
-
abort
public void abort(java.util.concurrent.Executor executor) throws java.sql.SQLExceptionDescription copied from interface:ClientAbort current connection
-
close
public void close() throws java.sql.SQLExceptionDescription copied from interface:ClientClose client
-
setReadOnly
public void setReadOnly(boolean readOnly) throws java.sql.SQLExceptionDescription copied from interface:ClientSwitch to a writer/read-only connection, no effet on mono-connection- Specified by:
setReadOnlyin interfaceClient- Parameters:
readOnly- must use read-only connection- Throws:
java.sql.SQLException- if any error occurs
-
getSocketTimeout
public int getSocketTimeout()
Description copied from interface:Clientget socket timeout- Specified by:
getSocketTimeoutin interfaceClient- Returns:
- socket timeout
-
setSocketTimeout
public void setSocketTimeout(int milliseconds) throws java.sql.SQLExceptionDescription copied from interface:ClientSet socket timeout- Specified by:
setSocketTimeoutin interfaceClient- Parameters:
milliseconds- timeout- Throws:
java.sql.SQLException- if any error occurs
-
isClosed
public boolean isClosed()
Description copied from interface:ClientIs client closed
-
getContext
public Context getContext()
Description copied from interface:ClientGet connection context- Specified by:
getContextin interfaceClient- Returns:
- connection context
-
getExceptionFactory
public ExceptionFactory getExceptionFactory()
Description copied from interface:ClientGet connection exception factory- Specified by:
getExceptionFactoryin interfaceClient- Returns:
- connection exception factory
-
getHostAddress
public HostAddress getHostAddress()
Description copied from interface:ClientGet connection host- Specified by:
getHostAddressin interfaceClient- Returns:
- connection host
-
getSocketIp
public java.lang.String getSocketIp()
Description copied from interface:ClientGet current socket IP or null (for Pipe / unix socket)- Specified by:
getSocketIpin interfaceClient- Returns:
- Socket current IP
-
isPrimary
public boolean isPrimary()
Description copied from interface:Clientis current client writer or read-only
-
-