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

public Class MasterController

extends Object
implements MasterFactory, 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.master.MasterFactory
Imports
java.io.IOException, java.net.SocketTimeoutException, .UnknownHostException, org.apache.derby.shared.common.error.StandardException, org.apache.derby.shared.common.reference.MessageId, .SQLState, .Property, org.apache.derby.iapi.services.monitor.ModuleControl, .ModuleSupportable, .Monitor, org.apache.derby.iapi.services.property.PropertyUtil, org.apache.derby.shared.common.sanity.SanityManager, org.apache.derby.iapi.store.raw.RawStoreFactory, org.apache.derby.iapi.store.raw.log.LogFactory, org.apache.derby.iapi.store.raw.data.DataFactory, org.apache.derby.iapi.store.replication.master.MasterFactory, org.apache.derby.impl.store.replication.net.ReplicationMessage, .ReplicationMessageTransmit, .SlaveAddress, org.apache.derby.impl.store.replication.ReplicationLogger, org.apache.derby.impl.store.replication.buffer.ReplicationLogBuffer, .LogBufferFullException, java.util.Properties

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

Modifier and TypeField and Description
private boolean
private DataFactory
private String
private static final int
private static final int
private static final int
private ReplicationLogBuffer
private int
private LogFactory
private AsynchronousLogShipper
private RawStoreFactory
private String
private ReplicationLogger
private static final int
private SlaveAddress
private ReplicationMessageTransmit

Constructor Summary

AccessConstructor and Description
public
MasterController()

Empty constructor required by Monitor.bootServiceModule

Method Summary

Modifier and TypeMethod and Description
public void
appendLog(long
the instant of the log record that was added last to this chunk of log
greatestInstant
,
byte[]
the chunk of log records
log
,
int
offset in log to start copy from
logOffset
,
int
number of bytes to copy, starting from logOffset
logLength
)

Implements org.apache.derby.iapi.store.replication.master.MasterFactory.appendLog.

Append a chunk of log records to the log buffer.

public void
boot(boolean
Currently ignored
create
,
Properties
Properties used to start the service in the correct mode. Currently initializes only the replicationMode property.
properties
)

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

Used by Monitor.bootServiceModule to start the service.

public boolean

Returns:

true if asynchronous replication is requested, meaning that this MasterController is a suitable implementation for the MasterFactory service. False otherwise
canSupport
(Properties
The properties used to boot replication
startParams
)

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

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

public void
flushedTo(long
The highest log instant that has been flushed to disk
instant
)

Implements org.apache.derby.iapi.store.replication.master.MasterFactory.flushedTo.

Used by the LogFactory to notify the replication master controller that the log records up to this instant have been flushed to disk.

pack-priv String

Returns:

a master database name
getDbName
()

Returns a name of a database associated with this master controller.

private String

Returns:

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

Used to return the host name of the slave being connected to.

private void
getMasterProperties()

Load relevant system property: replication log buffer size

private int

Returns:

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

Used to return the port number of the slave being connected to.

pack-priv ReplicationMessageTransmit

Returns:

an instance of the transmitter used to transmit messages to the slave.
handleExceptions
(Exception
the exception which caused the log shipper to terminate in an unexcepted manner.
exception
)

Used to handle the exceptions (IOException and StandardException) from the log shipper.

private void
handleFailoverFailure(Throwable
The throwable which resulted in the aborted failover attempt.
t
)

used to handle the case when an attempt to failover the database fails.

private void
printStackAndStopMaster(Exception
the exception that needs to be handled.
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.

public void
public void
startMaster(RawStoreFactory
The RawStoreFactory for the database
rawStore
,
DataFactory
The DataFactory for this database
dataFac
,
LogFactory
The LogFactory ensuring recoverability for this database
logFac
,
String
The hostname of the slave
slavehost
,
int
The port the slave is listening on
slaveport
,
String
The master database that is being replicated.
dbname
)

Implements org.apache.derby.iapi.store.replication.master.MasterFactory.startMaster.

Will perform all the work that is needed to set up replication.

public void
stop()

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

Will stop the replication master service.

public void
stopMaster()

Implements org.apache.derby.iapi.store.replication.master.MasterFactory.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.

public void
workToDo()

Implements org.apache.derby.iapi.store.replication.master.MasterFactory.workToDo.

Used to notify the log shipper that a log buffer element is full.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

activeback to summary
private boolean active
dataFactoryback to summary
private DataFactory dataFactory
dbnameback to summary
private String dbname
DEFAULT_LOG_BUFFER_SIZEback to summary
private static final int DEFAULT_LOG_BUFFER_SIZE
LOG_BUFFER_SIZE_MAXback to summary
private static final int LOG_BUFFER_SIZE_MAX
LOG_BUFFER_SIZE_MINback to summary
private static final int LOG_BUFFER_SIZE_MIN
logBufferback to summary
private ReplicationLogBuffer logBuffer
logBufferSizeback to summary
private int logBufferSize
logFactoryback to summary
private LogFactory logFactory
logShipperback to summary
private AsynchronousLogShipper logShipper
rawStoreFactoryback to summary
private RawStoreFactory rawStoreFactory
replicationModeback to summary
private String replicationMode
repLoggerback to summary
private ReplicationLogger repLogger
SLAVE_CONNECTION_ATTEMPT_TIMEOUTback to summary
private static final int SLAVE_CONNECTION_ATTEMPT_TIMEOUT
slaveAddrback to summary
private SlaveAddress slaveAddr
transmitterback to summary
private ReplicationMessageTransmit transmitter

Constructor Detail

MasterControllerback to summary
public MasterController()

Empty constructor required by Monitor.bootServiceModule

Method Detail

appendLogback to summary
public void appendLog(long greatestInstant, byte[] log, int logOffset, int logLength)

Implements org.apache.derby.iapi.store.replication.master.MasterFactory.appendLog.

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.

Parameters
greatestInstant:long

the instant of the log record that was added last to this chunk of log

log:byte[]

the chunk of log records

logOffset:int

offset in log to start copy from

logLength:int

number of bytes to copy, starting from logOffset

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. Currently only used to set up the replication mode.

Parameters
create:boolean

Currently ignored

properties:Properties

Properties used to start the service in the correct mode. Currently initializes only the replicationMode property.

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 asynchronous replication is specified in startParams by checking that a property with key MasterFactory.REPLICATION_MODE has the value MasterFactory.ASYNCHRONOUS_MODE.

Parameters
startParams:Properties

The properties used to boot replication

Returns:boolean

true if asynchronous replication is requested, meaning that this MasterController is a suitable implementation for the MasterFactory service. False otherwise

See Also
ModuleSupportable#canSupport
flushedToback to summary
public void flushedTo(long instant)

Implements org.apache.derby.iapi.store.replication.master.MasterFactory.flushedTo.

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

Parameters
instant:long

The highest log instant that has been flushed to disk

See Also
MasterFactory#flushedTo, LogFactory#flush
getDbNameback to summary
pack-priv 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:String

a master database name

getHostNameback to summary
private String getHostName()

Used to return the host name of the slave being connected to.

Returns:String

a String containing the host name of the slave being connected to.

getMasterPropertiesback to summary
private void getMasterProperties()

Load relevant system property: replication log buffer size

getPortNumberback to summary
private int getPortNumber()

Used to return the port number of the slave being connected to.

Returns:int

an Integer that represents the port number of the slave being connected to.

handleExceptionsback to summary
pack-priv ReplicationMessageTransmit handleExceptions(Exception exception)

Used to handle the exceptions (IOException and StandardException) from the log shipper.

Parameters
exception:Exception

the exception which caused the log shipper to terminate in an unexcepted manner.

Returns:ReplicationMessageTransmit

an instance of the transmitter used to transmit messages to the slave.

handleFailoverFailureback to summary
private void handleFailoverFailure(Throwable t) throws StandardException

used to handle the case when an attempt to failover the database fails.

Parameters
t:Throwable

The throwable which resulted in the aborted failover attempt.

Exceptions
StandardException:
Indicating the reason for the aborted failover attempt.
printStackAndStopMasterback to summary
private void printStackAndStopMaster(Exception e)

used to print the error stack for the given exception and stop the master.

Parameters
e:Exception

the exception that needs to be handled.

setupConnectionback to summary
private void setupConnection() throws StandardException

Connects to the slave being replicated to.

Exceptions
StandardException:
If a failure occurs while trying to open the connection to the slave.
startFailoverback to summary
public void startFailover() throws StandardException

Implements org.apache.derby.iapi.store.replication.master.MasterFactory.startFailover.

Doc from org.apache.derby.iapi.store.replication.master.MasterFactory.startFailover.

Will perform all work needed to failover

Exceptions
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()
startMasterback to summary
public void startMaster(RawStoreFactory rawStore, DataFactory dataFac, LogFactory logFac, String slavehost, int slaveport, String dbname) throws StandardException

Implements org.apache.derby.iapi.store.replication.master.MasterFactory.startMaster.

Will perform all the work that is needed to set up replication.

Parameters
rawStore:RawStoreFactory

The RawStoreFactory for the database

dataFac:DataFactory

The DataFactory for this database

logFac:LogFactory

The LogFactory ensuring recoverability for this database

slavehost:String

The hostname of the slave

slaveport:int

The port the slave is listening on

dbname:String

The master database that is being replicated.

Exceptions
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.
stopback to summary
public void stop()

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

Will stop the replication master service.

stopMasterback to summary
public void stopMaster() throws StandardException

Implements org.apache.derby.iapi.store.replication.master.MasterFactory.stopMaster.

Will perform all work that is needed to shut down replication.

Exceptions
StandardException:
If the replication master has been stopped already.
teardownNetworkback to summary
private void teardownNetwork()

Stop log shipping, notify slave that replication is stopped and tear down network connection with slave.

workToDoback to summary
public void workToDo()

Implements org.apache.derby.iapi.store.replication.master.MasterFactory.workToDo.

Used to notify the log shipper that a log buffer element is full.