Top Description Inners Fields Constructors Methods
org.apache.derby.impl.store.replication.net

public Class ReplicationMessageReceive

extends Object
Class Inheritance
Imports
java.io.IOException, java.net.ServerSocket, .Socket, javax.net.ServerSocketFactory, org.apache.derby.shared.common.error.StandardException, org.apache.derby.shared.common.reference.MessageId, .SQLState, org.apache.derby.iapi.services.monitor.Monitor, org.apache.derby.impl.store.raw.log.LogCounter, org.apache.derby.iapi.util.InterruptStatus

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 and Inner Type Summary

Modifier and TypeClass and 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

Modifier and TypeField and Description
private boolean
connectionConfirmed

Whether or not the connection with the master is confirmed to be working.

private static final 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 Thread
pingThread

Thread used to send ping messages to master to check if the connection is working.

private final Object
receivePongSemaphore

Used for synchronization when waiting for a ping reply message

private final Object
sendPingSemaphore

Used for synchronization of the ping thread

private ServerSocket
serverSocket

Contains the ServerSocket used to listen for connections from the replication master.

private final 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

AccessConstructor and Description
public
ReplicationMessageReceive(SlaveAddress
the address (host name and port number) of the slave to connect to.
slaveAddress
,
String
the name of the database.
dbname
)

Constructor initializes the slave address used in replication.

Method Summary

Modifier and TypeMethod and Description
private void
checkSocketConnection()

Verifies if the SocketConnection is valid.

private ServerSocket

Returns:

an instance of the ServerSocket class.
createServerSocket
()

Used to create a ServerSocket for listening to connections from the master.

private void
handleUnexpectedMessage(String
the name of the replicated database
dbname
,
String
the expected message type
expextedMsgId
,
String
the received message type
receivedMsgId
)

Notify other replication peer that the message type was unexpected and throw a StandardException

public void
initConnection(int
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
timeout
,
long
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!
synchOnInstant
,
String
the name of the replicated database
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.

public synchronized boolean

Returns:

true if the pong message was received before timing out after DEFAULT_PING_TIMEOUT millis, false otherwise
isConnectedToMaster
()

Check if the repliation network is working.

private void
parseAndAckInstant(ReplicationMessage
the object containing the UID.
initiatorMessage
,
long
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!
synchOnInstant
,
String
the name of the replicated database
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
the object containing the UID.
initiatorMessage
,
String
the name of the replicated database
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.

public ReplicationMessage

Returns:

a ReplicationMessage object that contains the reply that is sent.
readMessage
()

Used to read a replication message sent by the master.

public void
sendMessage(ReplicationMessage
a ReplicationMessage object that contains the message to be transmitted.
message
)

Used to send a replication message to the master.

public void
tearDown()

Used to close the ServerSocket and the resources associated with it.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

connectionConfirmedback to summary
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

DEFAULT_PING_TIMEOUTback to summary
private static final int DEFAULT_PING_TIMEOUT

The maximum number of millis to wait before giving up waiting for a ping response

doSendPingback to summary
private boolean doSendPing

Whether or not the ping thread has been notified to check connection. Protected by sendPingSemaphore.

killPingThreadback to summary
private boolean killPingThread

Used to terminate the ping thread.

pingThreadback to summary
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).

receivePongSemaphoreback to summary
private final Object receivePongSemaphore

Used for synchronization when waiting for a ping reply message

sendPingSemaphoreback to summary
private final Object sendPingSemaphore

Used for synchronization of the ping thread

serverSocketback to summary
private ServerSocket serverSocket

Contains the ServerSocket used to listen for connections from the replication master.

slaveAddressback to summary
private final SlaveAddress slaveAddress

Contains the address (hostname and port number) of the slave to replicate to.

socketConnback to summary
private SocketConnection socketConn

Contains the methods used to read and write to the Object streams obtained from a Socket connection.

Constructor Detail

ReplicationMessageReceiveback to summary
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:SlaveAddress

the address (host name and port number) of the slave to connect to.

dbname:String

the name of the database.

Method Detail

checkSocketConnectionback to summary
private void checkSocketConnection() throws IOException

Verifies if the SocketConnection is valid.

Exceptions
IOException:
If the socket connection object is not valid (is null).
createServerSocketback to summary
private ServerSocket createServerSocket() throws IOException

Used to create a ServerSocket for listening to connections from the master.

Returns:ServerSocket

an instance of the ServerSocket class.

Exceptions
IOException:
if an exception occurs while trying to open a connection.
handleUnexpectedMessageback to summary
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:String

the name of the replicated database

expextedMsgId:String

the expected message type

receivedMsgId:String

the received message type

Exceptions
StandardException:
exception describing that an unexpected message was received is always thrown
IOException:
thrown if an exception occurs while sending the error message
initConnectionback to summary
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:int

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:long

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:String

the name of the replicated database

Exceptions
IOException:
if an exception occurs while trying to create the SocketConnection class or while trying to open a connection.
StandardException:
if an incompatible database version is found.
ClassNotFoundException:
Class of a serialized object cannot be found.
isConnectedToMasterback to summary
public synchronized 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:boolean

true if the pong message was received before timing out after DEFAULT_PING_TIMEOUT millis, false otherwise

See Also
DEFAULT_PING_TIMEOUT
parseAndAckInstantback to summary
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:ReplicationMessage

the object containing the UID.

synchOnInstant:long

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:String

the name of the replicated database

Exceptions
IOException:
If an exception occurs while sending the acknowledgment.
StandardException:
If the log files are not in synch
parseAndAckVersionback to summary
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:ReplicationMessage

the object containing the UID.

dbname:String

the name of the replicated database

Exceptions
IOException:
If an exception occurs while sending the acknowledgment.
StandardException:
If the UID's do not match.
readMessageback to summary
public ReplicationMessage readMessage() throws ClassNotFoundException, 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:ReplicationMessage

a ReplicationMessage object that contains the reply that is sent.

Exceptions
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.
sendMessageback to summary
public void sendMessage(ReplicationMessage message) throws IOException

Used to send a replication message to the master.

Parameters
message:ReplicationMessage

a ReplicationMessage object that contains the message to be transmitted.

Exceptions
IOException:
1) if an exception occurs while transmitting the message, 2) if the connection handle is invalid.
tearDownback to summary
public void tearDown() throws IOException

Used to close the ServerSocket and the resources associated with it.

Exceptions
IOException:
If an exception occurs while trying to close the socket or the associated resources.
org.apache.derby.impl.store.replication.net back to summary

private Class ReplicationMessageReceive.SlavePingThread

extends Thread
Class Inheritance

Thread that sends ping messages to the master on request to check if the replication network is working

Field Summary

Modifier and TypeField and Description
private final ReplicationMessage
Inherited from java.lang.Thread:
MAX_PRIORITYMIN_PRIORITYNORM_PRIORITY

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public void
run()

Overrides java.lang.Thread.run.

Implements java.lang.Runnable.run.

This method is run by the thread when it executes.

Inherited from java.lang.Thread:
activeCountcheckAccessclonecurrentThreaddumpStackenumerategetAllStackTracesgetContextClassLoadergetDefaultUncaughtExceptionHandlergetIdgetNamegetPrioritygetStackTracegetStategetThreadGroupgetUncaughtExceptionHandlerholdsLockinterruptinterruptedisAliveisDaemonisInterruptedisVirtualjoinjoinjoinjoinofPlatformofVirtualonSpinWaitsetContextClassLoadersetDaemonsetDefaultUncaughtExceptionHandlersetNamesetPrioritysetUncaughtExceptionHandlersleepsleepsleepstartstartVirtualThreadstopthreadIdtoStringyield

Field Detail

pingMsgback to summary
private final ReplicationMessage pingMsg

Constructor Detail

SlavePingThreadback to summary
pack-priv SlavePingThread(String dbname)

Method Detail

runback to summary
public void run()

Overrides java.lang.Thread.run.

Implements java.lang.Runnable.run.

Doc from java.lang.Thread.run.

This method is run by the thread when it executes. Subclasses of Thread may override this method.

This method is not intended to be invoked directly. If this thread is a platform thread created with a Runnable task then invoking this method will invoke the task's run method. If this thread is a virtual thread then invoking this method directly does nothing.