Class MasterController
- java.lang.Object
-
- org.apache.derby.impl.store.replication.master.MasterController
-
- All Implemented Interfaces:
ModuleControl
,ModuleSupportable
,MasterFactory
public class MasterController extends java.lang.Object implements MasterFactory, ModuleControl, ModuleSupportable
This is an implementation of the replication master controller service. The service is booted when this instance of Derby will have the replication master role for this database.
Note: The current version of the class is far from complete. Code to control the replication master behavior will be added as more parts of the replication functionality is added to Derby.
- See Also:
MasterFactory
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
active
private DataFactory
dataFactory
private java.lang.String
dbname
private static int
DEFAULT_LOG_BUFFER_SIZE
private static int
LOG_BUFFER_SIZE_MAX
private static int
LOG_BUFFER_SIZE_MIN
private ReplicationLogBuffer
logBuffer
private int
logBufferSize
private LogFactory
logFactory
private AsynchronousLogShipper
logShipper
private RawStoreFactory
rawStoreFactory
private java.lang.String
replicationMode
private ReplicationLogger
repLogger
private static int
SLAVE_CONNECTION_ATTEMPT_TIMEOUT
private SlaveAddress
slaveAddr
private ReplicationMessageTransmit
transmitter
-
Fields inherited from interface org.apache.derby.iapi.store.replication.master.MasterFactory
ASYNCHRONOUS_MODE, MODULE, REPLICATION_MODE
-
-
Constructor Summary
Constructors Constructor Description MasterController()
Empty constructor required by Monitor.bootServiceModule
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
appendLog(long greatestInstant, byte[] log, int logOffset, int logLength)
Append a chunk of log records to the log buffer.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.void
flushedTo(long instant)
Used by the LogFactory to notify the replication master controller that the log records up to this instant have been flushed to disk.(package private) java.lang.String
getDbName()
Returns a name of a database associated with this master controller.private java.lang.String
getHostName()
Used to return the host name of the slave being connected to.private void
getMasterProperties()
Load relevant system property: replication log buffer sizeprivate int
getPortNumber()
Used to return the port number of the slave being connected to.(package private) ReplicationMessageTransmit
handleExceptions(java.lang.Exception exception)
Used to handle the exceptions (IOException and StandardException) from the log shipper.private void
handleFailoverFailure(java.lang.Throwable t)
used to handle the case when an attempt to failover the database fails.private void
printStackAndStopMaster(java.lang.Exception e)
used to print the error stack for the given exception and stop the master.private void
setupConnection()
Connects to the slave being replicated to.void
startFailover()
Will perform all work needed to failovervoid
startMaster(RawStoreFactory rawStore, DataFactory dataFac, LogFactory logFac, java.lang.String slavehost, int slaveport, java.lang.String dbname)
Will perform all the work that is needed to set up replication.void
stop()
Will stop the replication master service.void
stopMaster()
Will perform all work that is needed to shut down replication.private void
teardownNetwork()
Stop log shipping, notify slave that replication is stopped and tear down network connection with slave.void
workToDo()
Used to notify the log shipper that a log buffer element is full.
-
-
-
Field Detail
-
DEFAULT_LOG_BUFFER_SIZE
private static final int DEFAULT_LOG_BUFFER_SIZE
- See Also:
- Constant Field Values
-
LOG_BUFFER_SIZE_MIN
private static final int LOG_BUFFER_SIZE_MIN
- See Also:
- Constant Field Values
-
LOG_BUFFER_SIZE_MAX
private static final int LOG_BUFFER_SIZE_MAX
- See Also:
- Constant Field Values
-
rawStoreFactory
private RawStoreFactory rawStoreFactory
-
dataFactory
private DataFactory dataFactory
-
logFactory
private LogFactory logFactory
-
logBuffer
private ReplicationLogBuffer logBuffer
-
logShipper
private AsynchronousLogShipper logShipper
-
transmitter
private ReplicationMessageTransmit transmitter
-
repLogger
private ReplicationLogger repLogger
-
replicationMode
private java.lang.String replicationMode
-
slaveAddr
private SlaveAddress slaveAddr
-
dbname
private java.lang.String dbname
-
logBufferSize
private int logBufferSize
-
active
private boolean active
-
SLAVE_CONNECTION_ATTEMPT_TIMEOUT
private static final int SLAVE_CONNECTION_ATTEMPT_TIMEOUT
- See Also:
- Constant Field Values
-
-
Method Detail
-
boot
public void boot(boolean create, java.util.Properties properties) throws StandardException
Used by Monitor.bootServiceModule to start the service. Currently only used to set up the replication mode.- Specified by:
boot
in interfaceModuleControl
- Parameters:
create
- Currently ignoredproperties
- Properties used to start the service in the correct mode. Currently initializes only the replicationMode property.- Throws:
StandardException
- Standard Derby exception policy, thrown on error.- 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 asynchronous replication is specified in startParams by checking that a property with key MasterFactory.REPLICATION_MODE has the value MasterFactory.ASYNCHRONOUS_MODE.- Specified by:
canSupport
in interfaceModuleSupportable
- Parameters:
startParams
- The properties used to boot replication- Returns:
- true if asynchronous replication is requested, meaning that this MasterController is a suitable implementation for the MasterFactory service. False otherwise
- See Also:
ModuleSupportable.canSupport(java.util.Properties)
-
stop
public void stop()
Will stop the replication master service.- Specified by:
stop
in interfaceModuleControl
- See Also:
Monitor
,ModuleFactory
-
startMaster
public void startMaster(RawStoreFactory rawStore, DataFactory dataFac, LogFactory logFac, java.lang.String slavehost, int slaveport, java.lang.String dbname) throws StandardException
Will perform all the work that is needed to set up replication.- Specified by:
startMaster
in interfaceMasterFactory
- Parameters:
rawStore
- The RawStoreFactory for the databasedataFac
- The DataFactory for this databaselogFac
- The LogFactory ensuring recoverability for this databaseslavehost
- The hostname of the slaveslaveport
- The port the slave is listening ondbname
- The master database that is being replicated.- Throws:
StandardException
- Standard Derby exception policy, 1) thrown on replication startup error 2) thrown if the master has already been booted. 3) thrown if the specified replication mode is not supported.
-
stopMaster
public void stopMaster() throws StandardException
Will perform all work that is needed to shut down replication.- Specified by:
stopMaster
in interfaceMasterFactory
- Throws:
StandardException
- If the replication master has been stopped already.
-
startFailover
public void startFailover() throws StandardException
Description copied from interface:MasterFactory
Will perform all work needed to failover- Specified by:
startFailover
in interfaceMasterFactory
- Throws:
StandardException
- 1) If the failover succeeds, an exception is thrown to indicate that the master database was shutdown after a successful failover 2) If a failure occurs during network communication with slave.- See Also:
MasterFactory.startFailover()
-
getMasterProperties
private void getMasterProperties()
Load relevant system property: replication log buffer size
-
handleFailoverFailure
private void handleFailoverFailure(java.lang.Throwable t) throws StandardException
used to handle the case when an attempt to failover the database fails.- Parameters:
t
- The throwable which resulted in the aborted failover attempt.- Throws:
StandardException
- Indicating the reason for the aborted failover attempt.
-
appendLog
public void appendLog(long greatestInstant, byte[] log, int logOffset, int logLength)
Append a chunk of log records to the log buffer. The method is not threadsafe; only one thread should access this method at a time.- Specified by:
appendLog
in interfaceMasterFactory
- Parameters:
greatestInstant
- the instant of the log record that was added last to this chunk of loglog
- the chunk of log recordslogOffset
- offset in log to start copy fromlogLength
- number of bytes to copy, starting from logOffset
-
flushedTo
public void flushedTo(long instant)
Used by the LogFactory to notify the replication master controller that the log records up to this instant have been flushed to disk. The master controller takes action according to the current replication strategy when this method is called. When the asynchronous replication strategy is used, the method does not force log shipping to the slave; the log records may be shipped now or later at the MasterController's discretion. However, if another strategy like 2-safe replication is implemented in the future, a call to this method may force log shipment before returning control to the caller. Currently, only asynchronous replication is supported. Not implemented yet- Specified by:
flushedTo
in interfaceMasterFactory
- Parameters:
instant
- The highest log instant that has been flushed to disk- See Also:
MasterFactory.flushedTo(long)
,LogFactory.flush(org.apache.derby.iapi.store.raw.log.LogInstant)
-
setupConnection
private void setupConnection() throws StandardException
Connects to the slave being replicated to.- Throws:
StandardException
- If a failure occurs while trying to open the connection to the slave.
-
handleExceptions
ReplicationMessageTransmit handleExceptions(java.lang.Exception exception)
Used to handle the exceptions (IOException and StandardException) from the log shipper.- Parameters:
exception
- the exception which caused the log shipper to terminate in an unexcepted manner.- Returns:
- an instance of the transmitter used to transmit messages to the slave.
-
printStackAndStopMaster
private void printStackAndStopMaster(java.lang.Exception e)
used to print the error stack for the given exception and stop the master.- Parameters:
e
- the exception that needs to be handled.
-
workToDo
public void workToDo()
Used to notify the log shipper that a log buffer element is full.- Specified by:
workToDo
in interfaceMasterFactory
-
teardownNetwork
private void teardownNetwork()
Stop log shipping, notify slave that replication is stopped and tear down network connection with slave.
-
getDbName
java.lang.String getDbName()
Returns a name of a database associated with this master controller.
Note: The only purpose of the method as of now is to give a meaningful name to a log shipper thread. The log shipper thread name should contain a name of a corresponding master database, and this method is used to access it.
- Returns:
- a master database name
-
getHostName
private java.lang.String getHostName()
Used to return the host name of the slave being connected to.- Returns:
- a String containing the host name of the slave being connected to.
-
getPortNumber
private int getPortNumber()
Used to return the port number of the slave being connected to.- Returns:
- an Integer that represents the port number of the slave being connected to.
-
-