org.apache.derby.impl.store.replication.slave
Class SlaveController

java.lang.Object
  extended by 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
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
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
SlaveController()
          Empty constructor required by Monitor.bootServiceModule
 
Method Summary
 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 working
 boolean 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 replication
 void stopSlave(boolean forcedStop)
          Stop replication slave mode.
private  void teardownNetwork()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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
Constructor Detail

SlaveController

public SlaveController()
Empty constructor required by Monitor.bootServiceModule

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 interface ModuleControl
Parameters:
create - Currently ignored
properties - 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 interface ModuleControl
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 interface ModuleSupportable
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 interface SlaveFactory
Parameters:
rawStore - The RawStoreFactory for the database
logFac - 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 interface SlaveFactory
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 interface SlaveFactory
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 interface SlaveFactory
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.

Built on Thu 2012-03-29 21:53:33+0000, from revision ???

Apache Derby V10.6 Internals - Copyright © 2004,2007 The Apache Software Foundation. All Rights Reserved.