Top Description Inners Fields Constructors Methods
org.apache.derby.impl.store.replication.slave

public Class SlaveController

extends Object
implements SlaveFactory, ModuleControl, ModuleSupportable
Class Inheritance
All Implemented Interfaces
org.apache.derby.iapi.services.monitor.ModuleSupportable, org.apache.derby.iapi.services.monitor.ModuleControl, org.apache.derby.iapi.store.replication.slave.SlaveFactory
Imports
org.apache.derby.shared.common.error.StandardException, org.apache.derby.shared.common.reference.Attribute, .MessageId, .SQLState, org.apache.derby.iapi.services.monitor.ModuleControl, .ModuleSupportable, .Monitor, org.apache.derby.iapi.store.raw.RawStoreFactory, org.apache.derby.impl.store.raw.log.LogCounter, .LogToFile, org.apache.derby.iapi.store.raw.log.LogFactory, org.apache.derby.impl.store.replication.net.SlaveAddress, .ReplicationMessage, .ReplicationMessageReceive, org.apache.derby.impl.store.replication.ReplicationLogger, org.apache.derby.iapi.store.replication.slave.SlaveFactory, java.io.EOFException, .IOException, java.net.SocketTimeoutException, .UnknownHostException, java.util.Properties

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 and Inner Type Summary

Modifier and TypeClass and 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

Modifier and TypeField and Description
private String
private static final int
private volatile long
highestLogInstant

The instant of the latest log record received from the master and processed so far.

private volatile boolean
inReplicationSlaveMode

Whether or not replication slave mode is still on.

private SlaveController.SlaveLogReceiverThread
private ReplicationLogScan
private LogToFile
private RawStoreFactory
private ReplicationMessageReceive
private ReplicationLogger
private SlaveAddress
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.

Constructor Summary

AccessConstructor and Description
public
SlaveController()

Empty constructor required by Monitor.bootServiceModule

Method Summary

Modifier and TypeMethod and Description
public void
boot(boolean
Currently ignored
create
,
Properties
Properties used to start the service in the correct mode
properties
)

Implements org.apache.derby.iapi.services.monitor.ModuleControl.boot.

Used by Monitor.bootServiceModule to start the service.

public boolean

Returns:

true if slave repliation is specified, meaning that this MasterController is a suitable implementation for the SlaveFactory service. False otherwise.
canSupport
(Properties
The properties used to start replication
startParams
)

Implements org.apache.derby.iapi.services.monitor.ModuleSupportable.canSupport.

Used by Monitor.bootServiceModule to check if this class is usable for replication.

private void
doFailover()

Performs failover on this database.

public void
failover()

Implements org.apache.derby.iapi.store.replication.slave.SlaveFactory.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 String

Returns:

a String containing the host name of the slave.
getHostName
()

Used to return the host name of the slave.

private int

Returns:

an Integer that represents the port number of the slave.
getPortNumber
()

Used to return the port number of the slave.

private void
handleDisconnect(Exception
The reason the connection to the master was lost
e
)

Write the reason for the lost connection to the log (derby.log) and reconnect with the master.

private void
handleFatalException(Exception
The fatal exception that is the reason for calling this method
e
)

Handles fatal errors for slave replication functionality.

private boolean

Returns:

true if the network connection is working, false otherwise
isConnectedToMaster
()

Check if the repliation network connection to the master is working

public boolean
isStarted()

Implements org.apache.derby.iapi.store.replication.slave.SlaveFactory.isStarted.

Check whether or not slave replication mode has been successfully started.

private boolean

Returns:

true if a connection has been set up with the master, false if the connection attempt timed out.
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.

public void
startSlave(RawStoreFactory
The RawStoreFactory for the database
rawStore
,
LogFactory
The LogFactory ensuring recoverability for this database
logFac
)

Implements org.apache.derby.iapi.store.replication.slave.SlaveFactory.startSlave.

Start slave replication.

public void
stop()

Implements org.apache.derby.iapi.services.monitor.ModuleControl.stop.

Will tear down the replication slave service.

private void
stopSlave()

Will perform all work that is needed to stop replication

public void
stopSlave(boolean
Determines whether or not an exception should be thrown when this method is called while the network connection to the master is up.
forcedStop
)

Implements org.apache.derby.iapi.store.replication.slave.SlaveFactory.stopSlave.

Stop replication slave mode.

private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

dbnameback to summary
private String dbname
DEFAULT_SOCKET_TIMEOUTback to summary
private static final int DEFAULT_SOCKET_TIMEOUT
highestLogInstantback to summary
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

inReplicationSlaveModeback to summary
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.

logReceiverThreadback to summary
private SlaveController.SlaveLogReceiverThread logReceiverThread
logScanback to summary
private ReplicationLogScan logScan
logToFileback to summary
private LogToFile logToFile
rawStoreFactoryback to summary
private RawStoreFactory rawStoreFactory
receiverback to summary
private ReplicationMessageReceive receiver
repLoggerback to summary
private ReplicationLogger repLogger
slaveAddrback to summary
private SlaveAddress slaveAddr
startupSuccessfulback to summary
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

Constructor Detail

SlaveControllerback to summary
public SlaveController()

Empty constructor required by Monitor.bootServiceModule

Method Detail

bootback to summary
public void boot(boolean create, Properties properties) throws StandardException

Implements org.apache.derby.iapi.services.monitor.ModuleControl.boot.

Used by Monitor.bootServiceModule to start the service. It will set up basic variables

Parameters
create:boolean

Currently ignored

properties:Properties

Properties used to start the service in the correct mode

Exceptions
StandardException:
Standard Derby exception policy, thrown on error.
canSupportback to summary
public boolean canSupport(Properties startParams)

Implements org.apache.derby.iapi.services.monitor.ModuleSupportable.canSupport.

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.

Parameters
startParams:Properties

The properties used to start replication

Returns:boolean

true if slave repliation is specified, meaning that this MasterController is a suitable implementation for the SlaveFactory service. False otherwise.

See Also
ModuleSupportable#canSupport
doFailoverback to summary
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
failoverback to summary
public void failover() throws StandardException

Implements org.apache.derby.iapi.store.replication.slave.SlaveFactory.failover.

Doc from org.apache.derby.iapi.store.replication.slave.SlaveFactory.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. 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.

Exceptions
StandardException:
Thrown if slave is connected with master
getHostNameback to summary
private String getHostName()

Used to return the host name of the slave.

Returns:String

a String containing the host name of the slave.

getPortNumberback to summary
private int getPortNumber()

Used to return the port number of the slave.

Returns:int

an Integer that represents the port number of the slave.

handleDisconnectback to summary
private void handleDisconnect(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:Exception

The reason the connection to the master was lost

handleFatalExceptionback to summary
private void handleFatalException(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:Exception

The fatal exception that is the reason for calling this method

isConnectedToMasterback to summary
private boolean isConnectedToMaster()

Check if the repliation network connection to the master is working

Returns:boolean

true if the network connection is working, false otherwise

isStartedback to summary
public boolean isStarted()

Implements org.apache.derby.iapi.store.replication.slave.SlaveFactory.isStarted.

Doc from org.apache.derby.iapi.store.replication.slave.SlaveFactory.isStarted.

Check whether or not slave replication mode has been successfully started.

Returns:boolean

true if slave replication mode has started successfully, false if slave mode startup is not yet confirmed

See Also
SlaveFactory#isStarted
setupConnectionback to summary
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:boolean

true if a connection has been set up with the master, false if the connection attempt timed out.

Exceptions
StandardException:
if an unexpected exception occured that prevented a connection with the master.
startLogReceiverThreadback to summary
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.

startSlaveback to summary
public void startSlave(RawStoreFactory rawStore, LogFactory logFac) throws StandardException

Implements org.apache.derby.iapi.store.replication.slave.SlaveFactory.startSlave.

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.

Parameters
rawStore:RawStoreFactory

The RawStoreFactory for the database

logFac:LogFactory

The LogFactory ensuring recoverability for this database

Exceptions
StandardException:
Thrown if the slave could not be started.
stopback to summary
public void stop()

Implements org.apache.derby.iapi.services.monitor.ModuleControl.stop.

Will tear down the replication slave service.

stopSlaveback to summary
private void stopSlave() throws StandardException

Will perform all work that is needed to stop replication

stopSlaveback to summary
public void stopSlave(boolean forcedStop) throws StandardException

Implements org.apache.derby.iapi.store.replication.slave.SlaveFactory.stopSlave.

Doc from org.apache.derby.iapi.store.replication.slave.SlaveFactory.stopSlave.

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.

Parameters
forcedStop:boolean

Determines whether or not an exception should be thrown when this method is called while the network connection to the master is up.

Exceptions
StandardException:
Thrown if slave is connected with master and forcedStop is false.
See Also
SlaveFactory#stopSlave
teardownNetworkback to summary
private void teardownNetwork()
org.apache.derby.impl.store.replication.slave back to summary

private Class SlaveController.SlaveLogReceiverThread

extends Thread
Class Inheritance

Thread that listens for incoming messages from the master and applies chunks of log records to the local log files.

Field Summary

Inherited from java.lang.Thread:
MAX_PRIORITYMIN_PRIORITYNORM_PRIORITY

Constructor Summary

AccessConstructor and Description
pack-priv
SlaveLogReceiverThread()

Creates a new instance of SlaveLogReceiverThread with a debugging-friendly thread name.

Method Summary

Modifier and TypeMethod and Description
private void
handleLogChunk(byte[]
A chunk of log records received from the master
logChunk
)

Parses a chunk of log received from the master, and applies the individual log records to the local log file.

public void
run()

Overrides java.lang.Thread.run.

Implements java.lang.Runnable.run.

This method is run by the thread when it executes.

Inherited from java.lang.Thread:
activeCountcheckAccessclonecurrentThreaddumpStackenumerategetAllStackTracesgetContextClassLoadergetDefaultUncaughtExceptionHandlergetIdgetNamegetPrioritygetStackTracegetStategetThreadGroupgetUncaughtExceptionHandlerholdsLockinterruptinterruptedisAliveisDaemonisInterruptedisVirtualjoinjoinjoinjoinofPlatformofVirtualonSpinWaitsetContextClassLoadersetDaemonsetDefaultUncaughtExceptionHandlersetNamesetPrioritysetUncaughtExceptionHandlersleepsleepsleepstartstartVirtualThreadstopthreadIdtoStringyield

Constructor Detail

SlaveLogReceiverThreadback to summary
pack-priv SlaveLogReceiverThread()

Creates a new instance of SlaveLogReceiverThread with a debugging-friendly thread name.

Method Detail

handleLogChunkback to summary
private void handleLogChunk(byte[] logChunk) throws StandardException

Parses a chunk of log received from the master, and applies the individual log records to the local log file.

Parameters
logChunk:byte[]

A chunk of log records received from the master

Exceptions
StandardException:
If the chunk of log records could not be parsed or the local log file is out of synch with the master log file.
runback to summary
public void run()

Overrides java.lang.Thread.run.

Implements java.lang.Runnable.run.

Doc from java.lang.Thread.run.

This method is run by the thread when it executes. Subclasses of Thread may override this method.

This method is not intended to be invoked directly. If this thread is a platform thread created with a Runnable task then invoking this method will invoke the task's run method. If this thread is a virtual thread then invoking this method directly does nothing.