public class ReplicationMessageReceive
extends java.lang.Object
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
|
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 | Description |
---|---|
ReplicationMessageReceive(SlaveAddress slaveAddress,
java.lang.String dbname) |
Constructor initializes the slave address used in replication.
|
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. |
private final SlaveAddress slaveAddress
private java.net.ServerSocket serverSocket
ServerSocket
used to listen for
connections from the replication master.private SocketConnection socketConn
Socket
connection.private static final int DEFAULT_PING_TIMEOUT
private java.lang.Thread pingThread
private boolean killPingThread
private boolean connectionConfirmed
private final java.lang.Object sendPingSemaphore
private boolean doSendPing
private final java.lang.Object receivePongSemaphore
public ReplicationMessageReceive(SlaveAddress slaveAddress, java.lang.String dbname)
slaveAddress
- the address (host name and port number) of the slave
to connect to.dbname
- the name of the database.public void initConnection(int timeout, long synchOnInstant, java.lang.String dbname) throws java.io.IOException, StandardException, java.lang.ClassNotFoundException
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 databasejava.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.private java.net.ServerSocket createServerSocket() throws java.io.IOException
ServerSocket
for listening to connections
from the master.ServerSocket
class.java.io.IOException
- if an exception occurs while trying
to open a connection.public void tearDown() throws java.io.IOException
ServerSocket
and the resources
associated with it.java.io.IOException
- If an exception occurs while trying to
close the socket or the associated resources.private void parseAndAckVersion(ReplicationMessage initiatorMessage, java.lang.String dbname) throws java.io.IOException, StandardException
ReplicationMessage
class of the master, that is wrapped
in the initiator message, with the UID of the same class in the slave.initiatorMessage
- the object containing the UID.dbname
- the name of the replicated databasejava.io.IOException
- If an exception occurs while sending the
acknowledgment.StandardException
- If the UID's do not match.private void parseAndAckInstant(ReplicationMessage initiatorMessage, long synchOnInstant, java.lang.String dbname) throws java.io.IOException, StandardException
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 databasejava.io.IOException
- If an exception occurs while sending the
acknowledgment.StandardException
- If the log files are not in synchprivate void handleUnexpectedMessage(java.lang.String dbname, java.lang.String expextedMsgId, java.lang.String receivedMsgId) throws StandardException, java.io.IOException
dbname
- the name of the replicated databaseexpextedMsgId
- the expected message typereceivedMsgId
- the received message typeStandardException
- exception describing that an unexpected
message was received is always thrownjava.io.IOException
- thrown if an exception occurs while sending
the error messagepublic void sendMessage(ReplicationMessage message) throws java.io.IOException
message
- a ReplicationMessage
object that contains
the message to be transmitted.java.io.IOException
- 1) if an exception occurs while transmitting
the message,
2) if the connection handle is invalid.public ReplicationMessage readMessage() throws java.lang.ClassNotFoundException, java.io.IOException
ReplicationMessage
object that contains
the reply that is sent.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.private void checkSocketConnection() throws java.io.IOException
SocketConnection
is valid.java.io.IOException
- If the socket connection object is not
valid (is null).public boolean isConnectedToMaster()
DEFAULT_PING_TIMEOUT
Apache Derby V10.14 Internals - Copyright © 2004,2018 The Apache Software Foundation. All Rights Reserved.