Class ReplicationMessageReceive
- java.lang.Object
-
- org.apache.derby.impl.store.replication.net.ReplicationMessageReceive
-
public class ReplicationMessageReceive extends java.lang.ObjectThis class is the Receiver (viz. Socket server or listener) part of the network communication. It receives the message from the master and performs appropriate action depending on the type of the message.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classReplicationMessageReceive.SlavePingThreadThread that sends ping messages to the master on request to check if the replication network is working
-
Field Summary
Fields Modifier and Type Field Description private booleanconnectionConfirmedWhether or not the connection with the master is confirmed to be working.private static intDEFAULT_PING_TIMEOUTThe maximum number of millis to wait before giving up waiting for a ping responseprivate booleandoSendPingWhether or not the ping thread has been notified to check connection.private booleankillPingThreadUsed to terminate the ping thread.private java.lang.ThreadpingThreadThread used to send ping messages to master to check if the connection is working.private java.lang.ObjectreceivePongSemaphoreUsed for synchronization when waiting for a ping reply messageprivate java.lang.ObjectsendPingSemaphoreUsed for synchronization of the ping threadprivate java.net.ServerSocketserverSocketContains theServerSocketused to listen for connections from the replication master.private SlaveAddressslaveAddressContains the address (hostname and port number) of the slave to replicate to.private SocketConnectionsocketConnContains the methods used to read and write to the Object streams obtained from aSocketconnection.
-
Constructor Summary
Constructors Constructor Description ReplicationMessageReceive(SlaveAddress slaveAddress, java.lang.String dbname)Constructor initializes the slave address used in replication.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidcheckSocketConnection()Verifies if theSocketConnectionis valid.private java.net.ServerSocketcreateServerSocket()Used to create aServerSocketfor listening to connections from the master.private voidhandleUnexpectedMessage(java.lang.String dbname, java.lang.String expextedMsgId, java.lang.String receivedMsgId)Notify other replication peer that the message type was unexpected and throw a StandardExceptionvoidinitConnection(int timeout, long synchOnInstant, java.lang.String dbname)Used to create the server socket, listen on the socket for connections from the master and verify compatibility with the database version of the master.booleanisConnectedToMaster()Check if the repliation network is working.private voidparseAndAckInstant(ReplicationMessage initiatorMessage, long synchOnInstant, java.lang.String dbname)Used to parse the log instant initiator message from the master and check that the master and slave log files are in synch.private voidparseAndAckVersion(ReplicationMessage initiatorMessage, java.lang.String dbname)Used to parse the initiator message from the master and check if the slave is compatible with the master by comparing the UID of theReplicationMessageclass of the master, that is wrapped in the initiator message, with the UID of the same class in the slave.ReplicationMessagereadMessage()Used to read a replication message sent by the master.voidsendMessage(ReplicationMessage message)Used to send a replication message to the master.voidtearDown()Used to close theServerSocketand the resources associated with it.
-
-
-
Field Detail
-
slaveAddress
private final SlaveAddress slaveAddress
Contains the address (hostname and port number) of the slave to replicate to.
-
serverSocket
private java.net.ServerSocket serverSocket
Contains theServerSocketused to listen for connections from the replication master.
-
socketConn
private SocketConnection socketConn
Contains the methods used to read and write to the Object streams obtained from aSocketconnection.
-
DEFAULT_PING_TIMEOUT
private static final int DEFAULT_PING_TIMEOUT
The maximum number of millis to wait before giving up waiting for a ping response- See Also:
- Constant Field Values
-
pingThread
private java.lang.Thread pingThread
Thread used to send ping messages to master to check if the connection is working. The ping message must be sent from a separate thread because failed message shipping over TCP does not timeout for two minutes (not configurable).
-
killPingThread
private boolean killPingThread
Used to terminate the ping thread.
-
connectionConfirmed
private boolean connectionConfirmed
Whether or not the connection with the master is confirmed to be working. Set to false by isConnectedToMaster, set to true when a pong (i.e., a response to a ping) is received. Field protected by receivePongSemephore
-
sendPingSemaphore
private final java.lang.Object sendPingSemaphore
Used for synchronization of the ping thread
-
doSendPing
private boolean doSendPing
Whether or not the ping thread has been notified to check connection. Protected by sendPingSemaphore.
-
receivePongSemaphore
private final java.lang.Object receivePongSemaphore
Used for synchronization when waiting for a ping reply message
-
-
Constructor Detail
-
ReplicationMessageReceive
public ReplicationMessageReceive(SlaveAddress slaveAddress, java.lang.String dbname)
Constructor initializes the slave address used in replication. Accepts the host name and port number that constitute the slave address as parameters.- Parameters:
slaveAddress- the address (host name and port number) of the slave to connect to.dbname- the name of the database.
-
-
Method Detail
-
initConnection
public void initConnection(int timeout, long synchOnInstant, java.lang.String dbname) throws java.io.IOException, StandardException, java.lang.ClassNotFoundExceptionUsed to create the server socket, listen on the socket for connections from the master and verify compatibility with the database version of the master.- Parameters:
timeout- The amount of time, in milliseconds, this method will wait for a connection to be established. If no connection has been established before the timeout, a IOException is raised with cause java.net.SocketTimeoutExceptionsynchOnInstant- the slave log instant, used to check that the master and slave log files are in synch. If no chunks of log records have been received from the master yet, this is the end position in the current log file. If a chunk of log has been received, this is the instant of the log record received last. Note that there is a difference!dbname- the name of the replicated database- Throws:
java.io.IOException- if an exception occurs while trying to create theSocketConnectionclass or while trying to open a connection.java.lang.ClassNotFoundException- Class of a serialized object cannot be found.StandardException- if an incompatible database version is found.
-
createServerSocket
private java.net.ServerSocket createServerSocket() throws java.io.IOExceptionUsed to create aServerSocketfor listening to connections from the master.- Returns:
- an instance of the
ServerSocketclass. - Throws:
java.io.IOException- if an exception occurs while trying to open a connection.
-
tearDown
public void tearDown() throws java.io.IOExceptionUsed to close theServerSocketand the resources associated with it.- Throws:
java.io.IOException- If an exception occurs while trying to close the socket or the associated resources.
-
parseAndAckVersion
private void parseAndAckVersion(ReplicationMessage initiatorMessage, java.lang.String dbname) throws java.io.IOException, StandardException
Used to parse the initiator message from the master and check if the slave is compatible with the master by comparing the UID of theReplicationMessageclass of the master, that is wrapped in the initiator message, with the UID of the same class in the slave.- Parameters:
initiatorMessage- the object containing the UID.dbname- the name of the replicated database- Throws:
java.io.IOException- If an exception occurs while sending the acknowledgment.StandardException- If the UID's do not match.
-
parseAndAckInstant
private void parseAndAckInstant(ReplicationMessage initiatorMessage, long synchOnInstant, java.lang.String dbname) throws java.io.IOException, StandardException
Used to parse the log instant initiator message from the master and check that the master and slave log files are in synch.- Parameters:
initiatorMessage- the object containing the UID.synchOnInstant- the slave log instant, used to check that the master and slave log files are in synch. If no chunks of log records have been received from the master yet, this is the end position in the current log file. If a chunk of log has been received, this is the instant of the log record received last. Note that there is a difference!dbname- the name of the replicated database- Throws:
java.io.IOException- If an exception occurs while sending the acknowledgment.StandardException- If the log files are not in synch
-
handleUnexpectedMessage
private void handleUnexpectedMessage(java.lang.String dbname, java.lang.String expextedMsgId, java.lang.String receivedMsgId) throws StandardException, java.io.IOExceptionNotify other replication peer that the message type was unexpected and throw a StandardException- Parameters:
dbname- the name of the replicated databaseexpextedMsgId- the expected message typereceivedMsgId- the received message type- Throws:
StandardException- exception describing that an unexpected message was received is always thrownjava.io.IOException- thrown if an exception occurs while sending the error message
-
sendMessage
public void sendMessage(ReplicationMessage message) throws java.io.IOException
Used to send a replication message to the master.- Parameters:
message- aReplicationMessageobject that contains the message to be transmitted.- Throws:
java.io.IOException- 1) if an exception occurs while transmitting the message, 2) if the connection handle is invalid.
-
readMessage
public ReplicationMessage readMessage() throws java.lang.ClassNotFoundException, java.io.IOException
Used to read a replication message sent by the master. This method would wait on the connection from the master until a message is received or a connection failure occurs. Replication network layer specific messages (i.e. ping/pong messages) are handled internally and are not returned.- Returns:
- a
ReplicationMessageobject that contains the reply that is sent. - Throws:
java.lang.ClassNotFoundException- Class of a serialized object cannot be found.java.io.IOException- 1) if an exception occurs while reading from the stream, 2) if the connection handle is invalid.
-
checkSocketConnection
private void checkSocketConnection() throws java.io.IOExceptionVerifies if theSocketConnectionis valid.- Throws:
java.io.IOException- If the socket connection object is not valid (is null).
-
isConnectedToMaster
public boolean isConnectedToMaster()
Check if the repliation network is working. Tries to send a ping message to the master and returns the network status based on the success or failure of sending this message and receiving a pong reply. MT: Currently, only one thread is allowed to check the network status at any time to keep the code complexity down.- Returns:
- true if the pong message was received before timing out after DEFAULT_PING_TIMEOUT millis, false otherwise
- See Also:
DEFAULT_PING_TIMEOUT
-
-