Class ReplicationMessageReceive

java.lang.Object
org.apache.derby.impl.store.replication.net.ReplicationMessageReceive

public class ReplicationMessageReceive extends 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 
    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
    Whether or not the connection with the master is confirmed to be working.
    private static final int
    The maximum number of millis to wait before giving up waiting for a ping response
    private boolean
    Whether or not the ping thread has been notified to check connection.
    private boolean
    Used to terminate the ping thread.
    private Thread
    Thread used to send ping messages to master to check if the connection is working.
    private final Object
    Used for synchronization when waiting for a ping reply message
    private final Object
    Used for synchronization of the ping thread
    private ServerSocket
    Contains the ServerSocket used to listen for connections from the replication master.
    private final SlaveAddress
    Contains the address (hostname and port number) of the slave to replicate to.
    Contains the methods used to read and write to the Object streams obtained from a Socket connection.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor initializes the slave address used in replication.
  • Method Summary

    Modifier and Type
    Method
    Description
    private void
    Verifies if the SocketConnection is valid.
    private ServerSocket
    Used to create a ServerSocket for listening to connections from the master.
    private void
    handleUnexpectedMessage(String dbname, String expextedMsgId, String receivedMsgId)
    Notify other replication peer that the message type was unexpected and throw a StandardException
    void
    initConnection(int timeout, long synchOnInstant, 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
    Check if the repliation network is working.
    private void
    parseAndAckInstant(ReplicationMessage initiatorMessage, long synchOnInstant, 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, 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.
    Used to read a replication message sent by the master.
    void
    Used to send a replication message to the master.
    void
    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 Details

    • slaveAddress

      private final SlaveAddress slaveAddress
      Contains the address (hostname and port number) of the slave to replicate to.
    • serverSocket

      private 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:
    • pingThread

      private 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 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 Object receivePongSemaphore
      Used for synchronization when waiting for a ping reply message
  • Constructor Details

    • ReplicationMessageReceive

      public ReplicationMessageReceive(SlaveAddress slaveAddress, 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 Details

    • initConnection

      public void initConnection(int timeout, long synchOnInstant, String dbname) throws IOException, StandardException, 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:
      IOException - if an exception occurs while trying to create the SocketConnection class or while trying to open a connection.
      ClassNotFoundException - Class of a serialized object cannot be found.
      StandardException - if an incompatible database version is found.
    • createServerSocket

      private ServerSocket createServerSocket() throws IOException
      Used to create a ServerSocket for listening to connections from the master.
      Returns:
      an instance of the ServerSocket class.
      Throws:
      IOException - if an exception occurs while trying to open a connection.
    • tearDown

      public void tearDown() throws IOException
      Used to close the ServerSocket and the resources associated with it.
      Throws:
      IOException - If an exception occurs while trying to close the socket or the associated resources.
    • parseAndAckVersion

      private void parseAndAckVersion(ReplicationMessage initiatorMessage, String dbname) throws 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:
      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, String dbname) throws 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:
      IOException - If an exception occurs while sending the acknowledgment.
      StandardException - If the log files are not in synch
    • handleUnexpectedMessage

      private void handleUnexpectedMessage(String dbname, String expextedMsgId, String receivedMsgId) throws StandardException, 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
      IOException - thrown if an exception occurs while sending the error message
    • sendMessage

      public void sendMessage(ReplicationMessage message) throws IOException
      Used to send a replication message to the master.
      Parameters:
      message - a ReplicationMessage object that contains the message to be transmitted.
      Throws:
      IOException - 1) if an exception occurs while transmitting the message, 2) if the connection handle is invalid.
    • readMessage

      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:
      ClassNotFoundException - Class of a serialized object cannot be found.
      IOException - 1) if an exception occurs while reading from the stream, 2) if the connection handle is invalid.
    • checkSocketConnection

      private void checkSocketConnection() throws IOException
      Verifies if the SocketConnection is valid.
      Throws:
      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: