Class ReplicationMessageReceive


  • public class ReplicationMessageReceive
    extends java.lang.Object
    This 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 class  ReplicationMessageReceive.SlavePingThread
      Thread 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 boolean connectionConfirmed
      Whether or not the connection with the master is confirmed to be working.
      private static int DEFAULT_PING_TIMEOUT
      The maximum number of millis to wait before giving up waiting for a ping response
      private boolean doSendPing
      Whether or not the ping thread has been notified to check connection.
      private boolean killPingThread
      Used to terminate the ping thread.
      private java.lang.Thread pingThread
      Thread used to send ping messages to master to check if the connection is working.
      private java.lang.Object receivePongSemaphore
      Used for synchronization when waiting for a ping reply message
      private java.lang.Object sendPingSemaphore
      Used for synchronization of the ping thread
      private java.net.ServerSocket serverSocket
      Contains the ServerSocket used to listen for connections from the replication master.
      private SlaveAddress slaveAddress
      Contains the address (hostname and port number) of the slave to replicate to.
      private SocketConnection socketConn
      Contains the methods used to read and write to the Object streams obtained from a Socket connection.
    • 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 void checkSocketConnection()
      Verifies if the SocketConnection is valid.
      private java.net.ServerSocket createServerSocket()
      Used to create a ServerSocket for listening to connections from the master.
      private void handleUnexpectedMessage​(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 StandardException
      void initConnection​(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.
      boolean isConnectedToMaster()
      Check if the repliation network is working.
      private void parseAndAckInstant​(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 void parseAndAckVersion​(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 the ReplicationMessage class of the master, that is wrapped in the initiator message, with the UID of the same class in the slave.
      ReplicationMessage readMessage()
      Used to read a replication message sent by the master.
      void sendMessage​(ReplicationMessage message)
      Used to send a replication message to the master.
      void tearDown()
      Used to close the ServerSocket and the resources associated with it.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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 the ServerSocket used 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 a Socket connection.
      • 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.ClassNotFoundException
        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.
        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.SocketTimeoutException
        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 trying to create the SocketConnection class 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.IOException
        Used to create a ServerSocket for listening to connections from the master.
        Returns:
        an instance of the ServerSocket class.
        Throws:
        java.io.IOException - if an exception occurs while trying to open a connection.
      • tearDown

        public void tearDown()
                      throws java.io.IOException
        Used to close the ServerSocket and 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 the ReplicationMessage class 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.IOException
        Notify other replication peer that the message type was unexpected and throw a StandardException
        Parameters:
        dbname - the name of the replicated database
        expextedMsgId - the expected message type
        receivedMsgId - the received message type
        Throws:
        StandardException - exception describing that an unexpected message was received is always thrown
        java.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 - a ReplicationMessage object 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 ReplicationMessage object 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.IOException
        Verifies if the SocketConnection is 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