Class MasterController

java.lang.Object
org.apache.derby.impl.store.replication.master.MasterController
All Implemented Interfaces:
ModuleControl, ModuleSupportable, MasterFactory

public class MasterController extends 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:
  • Field Details

  • Constructor Details

    • MasterController

      public MasterController()
      Empty constructor required by Monitor.bootServiceModule
  • Method Details

    • boot

      public void boot(boolean create, 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 interface ModuleControl
      Parameters:
      create - Currently ignored
      properties - 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:
    • canSupport

      public boolean canSupport(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 interface ModuleSupportable
      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:
    • stop

      public void stop()
      Will stop the replication master service.
      Specified by:
      stop in interface ModuleControl
      See Also:
    • startMaster

      public void startMaster(RawStoreFactory rawStore, DataFactory dataFac, LogFactory logFac, String slavehost, int slaveport, String dbname) throws StandardException
      Will perform all the work that is needed to set up replication.
      Specified by:
      startMaster in interface MasterFactory
      Parameters:
      rawStore - The RawStoreFactory for the database
      dataFac - The DataFactory for this database
      logFac - The LogFactory ensuring recoverability for this database
      slavehost - The hostname of the slave
      slaveport - The port the slave is listening on
      dbname - 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 interface MasterFactory
      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 interface MasterFactory
      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:
    • getMasterProperties

      private void getMasterProperties()
      Load relevant system property: replication log buffer size
    • handleFailoverFailure

      private void handleFailoverFailure(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 interface MasterFactory
      Parameters:
      greatestInstant - the instant of the log record that was added last to this chunk of log
      log - the chunk of log records
      logOffset - offset in log to start copy from
      logLength - 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 interface MasterFactory
      Parameters:
      instant - The highest log instant that has been flushed to disk
      See Also:
    • 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(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(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 interface MasterFactory
    • teardownNetwork

      private void teardownNetwork()
      Stop log shipping, notify slave that replication is stopped and tear down network connection with slave.
    • getDbName

      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 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.