Class SlaveController
- java.lang.Object
-
- org.apache.derby.impl.store.replication.slave.SlaveController
-
- All Implemented Interfaces:
ModuleControl
,ModuleSupportable
,SlaveFactory
public class SlaveController extends java.lang.Object implements SlaveFactory, ModuleControl, ModuleSupportable
This is an implementation of the replication slave controller service. The service is booted when this instance of Derby will have the replication slave role for this database.
Note: The current version of the class is far from complete. Code to control the replication slave behavior will be added as more parts of the replication functionality is added to Derby.
- See Also:
SlaveFactory
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
SlaveController.SlaveLogReceiverThread
Thread that listens for incoming messages from the master and applies chunks of log records to the local log files.
-
Field Summary
Fields Modifier and Type Field Description private java.lang.String
dbname
private static int
DEFAULT_SOCKET_TIMEOUT
private long
highestLogInstant
The instant of the latest log record received from the master and processed so far.private boolean
inReplicationSlaveMode
Whether or not replication slave mode is still on.private SlaveController.SlaveLogReceiverThread
logReceiverThread
private ReplicationLogScan
logScan
private LogToFile
logToFile
private RawStoreFactory
rawStoreFactory
private ReplicationMessageReceive
receiver
private ReplicationLogger
repLogger
private SlaveAddress
slaveAddr
private boolean
startupSuccessful
Whether or not this SlaveController has been successfully started, including setting up a connection with the master and starting the log receiver thread.-
Fields inherited from interface org.apache.derby.iapi.store.replication.slave.SlaveFactory
MODULE, REPLICATION_MODE, SLAVE_DB, SLAVE_MODE, SLAVE_PRE_MODE
-
-
Constructor Summary
Constructors Constructor Description SlaveController()
Empty constructor required by Monitor.bootServiceModule
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
boot(boolean create, java.util.Properties properties)
Used by Monitor.bootServiceModule to start the service.boolean
canSupport(java.util.Properties startParams)
Used by Monitor.bootServiceModule to check if this class is usable for replication.private void
doFailover()
Performs failover on this database.void
failover()
Used to turn this slave instance of the database into a normal instance that clients can connect to, assuming that the connection with the master is down.private java.lang.String
getHostName()
Used to return the host name of the slave.private int
getPortNumber()
Used to return the port number of the slave.private void
handleDisconnect(java.lang.Exception e)
Write the reason for the lost connection to the log (derby.log) and reconnect with the master.private void
handleFatalException(java.lang.Exception e)
Handles fatal errors for slave replication functionality.private boolean
isConnectedToMaster()
Check if the repliation network connection to the master is workingboolean
isStarted()
Check whether or not slave replication mode has been successfully started.private boolean
setupConnection()
Establish a connection with the replication master.private void
startLogReceiverThread()
Starts the LogReceiverThread that will listen for chunks of log records from the master and apply the log records to the local log file.void
startSlave(RawStoreFactory rawStore, LogFactory logFac)
Start slave replication.void
stop()
Will tear down the replication slave service.private void
stopSlave()
Will perform all work that is needed to stop replicationvoid
stopSlave(boolean forcedStop)
Stop replication slave mode.private void
teardownNetwork()
-
-
-
Field Detail
-
DEFAULT_SOCKET_TIMEOUT
private static final int DEFAULT_SOCKET_TIMEOUT
- See Also:
- Constant Field Values
-
rawStoreFactory
private RawStoreFactory rawStoreFactory
-
logToFile
private LogToFile logToFile
-
receiver
private ReplicationMessageReceive receiver
-
repLogger
private ReplicationLogger repLogger
-
slaveAddr
private SlaveAddress slaveAddr
-
dbname
private java.lang.String dbname
-
highestLogInstant
private volatile long highestLogInstant
The instant of the latest log record received from the master and processed so far. Used to check that master and slave log files are in synch
-
inReplicationSlaveMode
private volatile boolean inReplicationSlaveMode
Whether or not replication slave mode is still on. Will be set to false when slave replication is shut down. The value of this variable is checked after every timeout when trying to set up a connection to the master, and by the thread that applies log chunks received from the master.
-
startupSuccessful
private volatile boolean startupSuccessful
Whether or not this SlaveController has been successfully started, including setting up a connection with the master and starting the log receiver thread. The client connection that initiated slave replication mode on this database will not report that slave mode was successfully started (i.e., it will hang) until startupSuccessful has been set to true
-
logScan
private ReplicationLogScan logScan
-
logReceiverThread
private SlaveController.SlaveLogReceiverThread logReceiverThread
-
-
Method Detail
-
boot
public void boot(boolean create, java.util.Properties properties) throws StandardException
Used by Monitor.bootServiceModule to start the service. It will set up basic variables- Specified by:
boot
in interfaceModuleControl
- Parameters:
create
- Currently ignoredproperties
- Properties used to start the service in the correct mode- Throws:
StandardException
- Standard Derby exception policy, thrown on error.- See Also:
Monitor
,ModuleFactory
-
stop
public void stop()
Will tear down the replication slave service.- Specified by:
stop
in interfaceModuleControl
- See Also:
Monitor
,ModuleFactory
-
canSupport
public boolean canSupport(java.util.Properties startParams)
Used by Monitor.bootServiceModule to check if this class is usable for replication. To be usable, we require that slave replication mode is specified in startParams by checking that a property with key SlaveFactory.REPLICATION_MODE has the value SlaveFactory.SLAVE_MODE.- Specified by:
canSupport
in interfaceModuleSupportable
- Parameters:
startParams
- The properties used to start replication- Returns:
- true if slave repliation is specified, meaning that this MasterController is a suitable implementation for the SlaveFactory service. False otherwise.
- See Also:
ModuleSupportable.canSupport(java.util.Properties)
-
startSlave
public void startSlave(RawStoreFactory rawStore, LogFactory logFac) throws StandardException
Start slave replication. This method establishes a network connection with the associated replication master and starts a thread that applies operations received from the master (in the form of log records) to the local slave database.- Specified by:
startSlave
in interfaceSlaveFactory
- Parameters:
rawStore
- The RawStoreFactory for the databaselogFac
- The LogFactory ensuring recoverability for this database- Throws:
StandardException
- Thrown if the slave could not be started.
-
stopSlave
private void stopSlave() throws StandardException
Will perform all work that is needed to stop replication- Throws:
StandardException
-
stopSlave
public void stopSlave(boolean forcedStop) throws StandardException
Description copied from interface:SlaveFactory
Stop replication slave mode. Causes the database to abort the boot process, and should only be used when shutting down this database. If forcedStop is false, the method will fail with an exception if connected with the master. If forcedStop is true, the slave will be shut down even if connected to the master. A forcedStop value of true should only be used by system shutdown.- Specified by:
stopSlave
in interfaceSlaveFactory
- Parameters:
forcedStop
- Determines whether or not an exception should be thrown when this method is called while the network connection to the master is up.- Throws:
StandardException
- Thrown if slave is connected with master and forcedStop is false.- See Also:
SlaveFactory.stopSlave(boolean)
-
failover
public void failover() throws StandardException
Description copied from interface:SlaveFactory
Used to turn this slave instance of the database into a normal instance that clients can connect to, assuming that the connection with the master is down. This is typically done in cases where a fatal error has happened on the master instance of the database, or when the master database is unreachable due to network problems.
By calling failover, this slave instance of the database will be recovered so that all committed operations that have been received from the master are reflected here. On the other hand, operations from transactions where the commit log record has not been received from the master will not be reflected.
Note that even though an operation has been executed (and even committed) on the master, it is not neccessarily reflected in the slave instance of the database. This depends on the replication strategy used by the MasterFactory.
- Specified by:
failover
in interfaceSlaveFactory
- Throws:
StandardException
- Thrown if slave is connected with master- See Also:
MasterFactory
,MasterController.flushedTo(long)
-
doFailover
private void doFailover()
Performs failover on this database. May be called because a failover command has been received from the master, or because a client has requested a failover after the network connection with the master has been lost.- See Also:
SlaveFactory.failover()
-
isStarted
public boolean isStarted()
Description copied from interface:SlaveFactory
Check whether or not slave replication mode has been successfully started.- Specified by:
isStarted
in interfaceSlaveFactory
- Returns:
- true if slave replication mode has started successfully, false if slave mode startup is not yet confirmed
- See Also:
SlaveFactory.isStarted()
-
setupConnection
private boolean setupConnection() throws StandardException
Establish a connection with the replication master. Listens for a connection on the slavehost/port for DEFAULT_SOCKET_TIMEOUT milliseconds.- Returns:
- true if a connection has been set up with the master, false if the connection attempt timed out.
- Throws:
StandardException
- if an unexpected exception occured that prevented a connection with the master.
-
handleDisconnect
private void handleDisconnect(java.lang.Exception e)
Write the reason for the lost connection to the log (derby.log) and reconnect with the master. Once the network is up and running, a new LogReceiverThread is started. The method returns without doing anything if inReplicationSlaveMode=false, which means that stopSlave() has been called by another thread.- Parameters:
e
- The reason the connection to the master was lost
-
isConnectedToMaster
private boolean isConnectedToMaster()
Check if the repliation network connection to the master is working- Returns:
- true if the network connection is working, false otherwise
-
startLogReceiverThread
private void startLogReceiverThread()
Starts the LogReceiverThread that will listen for chunks of log records from the master and apply the log records to the local log file.
-
handleFatalException
private void handleFatalException(java.lang.Exception e)
Handles fatal errors for slave replication functionality. These are errors that requires us to stop replication. Calling this method has the following effects: 1) Debug messages are written to the log file (usually derby.log) if ReplicationLogger#LOG_REPLICATION_MESSAGES is true. 2) If the network connection is up, the master is notified of the problem. 3) All slave replication functionality is stopped, and the database is then shut down without being booted. The method will return without doing anything if inReplicationSlaveMode=false, meaning that stopSlave has been called.- Parameters:
e
- The fatal exception that is the reason for calling this method
-
teardownNetwork
private void teardownNetwork()
-
getHostName
private java.lang.String getHostName()
Used to return the host name of the slave.- Returns:
- a String containing the host name of the slave.
-
getPortNumber
private int getPortNumber()
Used to return the port number of the slave.- Returns:
- an Integer that represents the port number of the slave.
-
-