Top Description Fields Constructors Methods
org.apache.derby.impl.store.raw.log

public final Class LogToFile

extends Object
implements LogFactory, ModuleControl, ModuleSupportable, Serviceable
Class Inheritance
All Implemented Interfaces
org.apache.derby.iapi.services.daemon.Serviceable, org.apache.derby.iapi.services.monitor.ModuleSupportable, org.apache.derby.iapi.services.monitor.ModuleControl, org.apache.derby.iapi.store.raw.log.LogFactory, org.apache.derby.iapi.store.raw.Corruptable
Imports
org.apache.derby.iapi.services.diag.Performance, org.apache.derby.shared.common.info.ProductVersionHolder, org.apache.derby.shared.common.reference.MessageId, .Property, .SQLState, .Attribute, org.apache.derby.iapi.services.daemon.DaemonService, .Serviceable, org.apache.derby.iapi.services.context.ContextManager, .ContextService, org.apache.derby.iapi.services.monitor.Monitor, .ModuleControl, .ModuleFactory, .ModuleSupportable, .PersistentService, org.apache.derby.shared.common.sanity.SanityManager, org.apache.derby.iapi.services.io.Formatable, .StoredFormatIds, .ArrayInputStream, .FileUtil, org.apache.derby.shared.common.error.ErrorStringBuilder, .ShutdownException, .StandardException, org.apache.derby.shared.common.i18n.MessageService, org.apache.derby.iapi.store.access.AccessFactory, .AccessFactoryGlobals, .TransactionController, .DatabaseInstant, org.apache.derby.iapi.store.raw.Loggable, .RawStoreFactory, .ScanHandle, org.apache.derby.iapi.store.raw.log.LogFactory, .Logger, .LogInstant, .LogScan, org.apache.derby.iapi.store.raw.xact.RawTransaction, .TransactionFactory, org.apache.derby.iapi.store.raw.data.DataFactory, org.apache.derby.iapi.services.property.PersistentSet, .PropertyUtil, org.apache.derby.iapi.store.replication.master.MasterFactory, org.apache.derby.iapi.store.replication.slave.SlaveFactory, org.apache.derby.io.WritableStorageFactory, .StorageFile, .StorageRandomAccessFile, org.apache.derby.iapi.util.InterruptStatus, java.io.File, .IOException, .OutputStreamWriter, .SyncFailedException, .ByteArrayOutputStream, .DataOutputStream, .ByteArrayInputStream, .DataInputStream, .FileNotFoundException, java.net.MalformedURLException, .URISyntaxException, .URI, .URL, java.util.Properties, java.util.zip.CRC32

This is an implementation of the log using a non-circular file system file. No support for incremental log backup or media recovery. Only crash recovery is supported.

The 'log' is a stream of log records. The 'log' is implemented as a series of numbered log files. These numbered log files are logically continuous so a transaction can have log records that span multiple log files. A single log record cannot span more then one log file. The log file number is monotonically increasing.

The log belongs to a log factory of a RawStore. In the current implementation, each RawStore only has one log factory, so each RawStore only has one log (which composed of multiple log files). At any given time, a log factory only writes new log records to one log file, this log file is called the 'current log file'.

A log file is named loglogNumber.dat

Everytime a checkpoint is taken, a new log file is created and all subsequent log records will go to the new log file. After a checkpoint is taken, old and useless log files will be deleted.

RawStore exposes a checkpoint method which clients can call, or a checkpoint is taken automatically by the RawStore when

  1. the log file grows beyond a certain size (configurable, default 100K bytes)
  2. RawStore is shutdown and a checkpoint hasn't been done "for a while"
  3. RawStore is recovered and a checkpoint hasn't been done "for a while"

This LogFactory is responsible for the formats of 2 kinds of file: the log file and the log control file. And it is responsible for the format of the log record wrapper.


Format of log control file

Field Summary

Modifier and TypeField and Description
private int
private StorageFile
private String
private long
private volatile boolean
pack-priv long
private static final int
private static final int
private DaemonService
private volatile boolean
pack-priv long
private int
private CRC32
protected volatile StandardException
corrupt

If not null then something is corrupt in the raw store and this represents the original error.

private CheckpointOperation
private boolean
pack-priv String
protected DataFactory
public static final String
private static final int
private static final String
private static final int
private static final int
public static final String
public static final String
protected long
private static int
private StorageRandomAccessFile
pack-priv long
private boolean
private boolean
private boolean
private boolean
private boolean
private boolean
inReplicationSlavePreMode

True if the database has been booted in replication slave pre mode, effectively turning off writes to the log file.

private static final long
private static final byte
private static final byte
IS_DURABILITY_TESTMODE_NO_SYNC_FLAG

When the derby.system.durability property is set to 'test', the store system will not force sync calls in the following cases - for the log file at each commit - for the log file before data page is forced to disk - for page allocation when file is grown - for data writes during checkpoint This means it is possible that the recovery system may not work properly, committed transactions may be lost, and/or database may not be in a consistent state.

private boolean
isFrozen

If frozen, don't allow anything on disk to change.

private boolean
isWriteSynced

Note

Why logging system support file sync and write sync ?

pack-priv ProductVersionHolder
jbmsVersion

Product Version information.

private boolean
jvmSyncErrorChecked

Status for whether the check on the sync error on some JVMs has been done or not.

private boolean
pack-priv long
private static final int
private static final int
protected static final int
public static final int
public static final int
private static final int
private static final int
protected static final String
private volatile boolean
private boolean
private int
pack-priv String
pack-priv long
private volatile long
private boolean
protected LogAccessFile
private WritableStorageFactory
private int
private boolean
private long
private MasterFactory
private long
private int
private boolean
private int
private int
private int
private int
private static final int
private boolean
private int
onDiskMajorVersion

On disk database version information.

private int
private RawStoreFactory
protected boolean
private boolean
private volatile StandardException
replicationSlaveException

If this exception is set while in replication slave mode, the exception will be thrown by the thread doing recovery will.

private Object
private boolean
public static final String
TEST_LOG_FULL

Set to true if we want to simulate a log full condition

public static final String
TEST_LOG_INCOMPLETE_LOG_WRITE

Set to true if we want the upcoming log record to be only partially written.

public static final String
TEST_LOG_PARTIAL_LOG_WRITE_NUM_BYTES

Set to the number of bytes we want the next log record to actually write out, only used when TEST_LOG_INCOMPLETE_LOG_WRITE is on.

public static final String
TEST_LOG_SWITCH_LOG

Set to true if we want the checkpoint to only switch the log but not actually do the checkpoint

pack-priv int
test_logWritten

DEBUG test only

public static final String
TEST_MAX_LOGFILE_NUMBER

Set to true if we want to simulate max possible log file number is being used.

pack-priv int
public static final String
TEST_RECORD_TO_FILL_LOG

Set to the number of log record we want to write before the log is simulated to be full.

public static final String
TEST_SWITCH_LOG_FAIL1

Set to true if we want to simulate a log full condition while switching log

public static final String
private File
private static boolean
wasDBInDurabilityTestModeNoSync

keeps track of if the database was booted previously at any time with derby.system.durability=test

Constructor Summary

AccessConstructor and Description
public
LogToFile()

MT- not needed for constructor

Method Summary

Modifier and TypeMethod and Description
public void
public long
appendLogRecord(byte[] data, int offset, int length, byte[] optionalData, int optionalDataOffset, int optionalDataLength)

Append length bytes of data to the log prepended by a long log instant and followed by 4 bytes of length information.

private void
backupLogFiles(File toDir, long lastLogFileToBackup)

public void
boot(boolean
True if the database should be created
create
,
Properties
Configuration properties
startParams
)

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

Boot up the log factory.

public boolean
canSupport(Properties startParams)

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

See if this implementation can support any attributes that are listed in properties.

private void
protected void
checkForReplication(LogAccessFile
The LogAccessFile that will take the replication master role iff this database is master.
log
)

Used by LogAccessFile to check if it should take the replication master role, and thereby send log records to the MasterFactory.

private boolean

Returns:

true if a JVM error is detected, false otherwise
checkJvmSyncError
(StorageFile
information about the log file to be opened
logFile
)

In Java 1.4.2 and newer rws and rwd modes for RandomAccessFile are supported.

public boolean
checkpoint(RawStoreFactory
The RawStoreFactory to use to do the checkpoint.
rsf
,
DataFactory
The DataFactory to use to do the checkpoint.
df
,
TransactionFactory
The TransactionFactory to use to do the checkpoint.
tf
,
boolean
If an existing checkpoint is in progress, then if wait=true then this routine will wait for the checkpoint to complete and the do another checkpoint and wait for it to finish before returning.
wait
)

Implements org.apache.derby.iapi.store.raw.log.LogFactory.checkpoint.

Checkpoint the rawStore.

public void
checkpointInRFR(LogInstant
The LogInstant of the checkpoint
cinstant
,
long
Redo Low Water Mark in the check point record
redoLWM
,
long
Undo Low Water Mark in the checkpoint
undoLWM
,
DataFactory
- the data factory
df
)

Implements org.apache.derby.iapi.store.raw.log.LogFactory.checkpointInRFR.

redo a checkpoint during rollforward recovery

private boolean
checkpointWithTran(RawTransaction cptran, RawStoreFactory
The RawStoreFactory to use to do the checkpoint.
rsf
,
DataFactory
The DataFactory to use to do the checkpoint.
df
,
TransactionFactory
The TransactionFactory to use to do the checkpoint.
tf
,
boolean
If an existing checkpoint is in progress, then if wait=true then this routine will wait for the checkpoint to complete and the do another checkpoint and wait for it to finish before returning.
wait
)

checkpoint with pre-start transaction

pack-priv boolean

Returns:

True if the database has been upgraded to the required level, false otherwise.
checkVersion
(int
required database Engine major version
requiredMajorVersion
,
int
required database Engine minor version
requiredMinorVersion
)

Check to see if a database has been upgraded to the required level in order to use a store feature.

public boolean

Returns:

true if the database has been upgraded to the required level, false otherwise.
checkVersion
(int
required database Engine major version
requiredMajorVersion
,
int
required database Engine minor version
requiredMinorVersion
,
String
Non-null to throw an exception, null to return the state of the version match.
feature
)

Implements org.apache.derby.iapi.store.raw.log.LogFactory.checkVersion.

Check to see if a database has been upgraded to the required level in order to use a store feature.

public void
createDataWarningFile()

Implements org.apache.derby.iapi.store.raw.log.LogFactory.createDataWarningFile.

Create readme file in log directory warning users against touching any files in the directory

private void
createLogDirectory()

Create the directory where transaction log should go.

protected synchronized long
currentInstant()

Get the current log instant - this is the log instant of the Next log record to be written out

MT - This method is synchronized to ensure that it always points to the end of a log record, not the middle of one.

public final boolean
public int
decrypt(byte[] ciphertext, int offset, int length, byte[] cleartext, int outputOffset)

public void
private void
public void
public void
public void
public int
encrypt(byte[] cleartext, int offset, int length, byte[] ciphertext, int outputOffset)

public void
protected synchronized long
public void
failoverSlave()

Used to make the slave stop appending log records, complete recovery and boot the database.

private CheckpointOperation
findCheckpoint(long checkpointInstant, FileLogger logger)

private static Object
findServiceModule(final Object serviceModule, final String factoryInterface)

Must be private so that user code can't call this entry point.

private synchronized long
firstLogInstant()

Get the first valid log instant - this is the beginning of the first log file

MT- synchronized on this

public void
flush(LogInstant
flush log up to here
where
)

Implements org.apache.derby.iapi.store.raw.log.LogFactory.flush.

Flush all unwritten log record up to the log instance indicated to disk and sync.

protected void
flush(long fileNumber, long wherePosition)

Flush the log such that the log record written with the instant wherePosition is guaranteed to be on disk.

public void
flushAll()

Flush all unwritten log record to disk and sync.

private void
flushBuffer(long fileNumber, long
flush log up to here
wherePosition
)

Flush all unwritten log record up to the log instance indicated to disk without syncing.

public void
freezePersistentStore()

Implements org.apache.derby.iapi.store.raw.log.LogFactory.freezePersistentStore.

Backup restore - stop sending log record to the log stream

public String
getCanonicalLogPath()

Implements org.apache.derby.iapi.store.raw.log.LogFactory.getCanonicalLogPath.

Return the canonical directory of the PARENT of the log directory.

private static ContextService
getContextService()

Privileged lookup of the ContextService.

private StorageFile
getControlFileName()

Return the control file name

MT- read only

public int
getEncryptedDataLength(int length)

returns the length that will make the data to be multiple of encryption block size based on the given length.

public int
getEncryptionBlockSize()

return the encryption block size used during encrypted db creation

private long
getFirstLogNeeded(CheckpointOperation checkpoint)

Return the "oldest" log file still needed by recovery.

public synchronized LogInstant
getFirstUnflushedInstant()

Implements org.apache.derby.iapi.store.raw.log.LogFactory.getFirstUnflushedInstant.

Get the instant of the first record which was not flushed.

public synchronized long
getFirstUnflushedInstantAsLong()

Implements org.apache.derby.iapi.store.raw.log.LogFactory.getFirstUnflushedInstantAsLong.

Get the log instant long value of the first log record that has not been flushed.

public StorageFile
getLogDirectory()

Implements org.apache.derby.iapi.store.raw.log.LogFactory.getLogDirectory.

Return the location of the log directory.

private String
public void
protected StorageRandomAccessFile
getLogFileAtBeginning(long filenumber)

Open a log file and position the file at the beginning.

protected StorageRandomAccessFile

Returns:

null if file does not exist or of the wrong format
getLogFileAtPosition
(long logInstant)

Get a read-only handle to the log file positioned at the stated position

MT- read only

private StorageFile
getLogFileName(long filenumber)

Given a log file number, return its file name

MT- read only

private synchronized long
getLogFileNumber()

Return the current log file number.

public StorageRandomAccessFile
getLogFileToSimulateCorruption(long filenum)

Get the log file to Simulate a log corruption FOR UNIT TESTING USAGE ONLY

public Logger
private void
private StorageFile
getMirrorControlFileName()

Return the mirror control file name

MT- read only

private static ModuleFactory
getMonitor()

Must be private so that user code can't call this entry point.

private static Object
getServiceModule(final Object serviceModule, final String factoryInterface)

Must be private so that user code can't call this entry point.

public int
getTypeFormatId()

Return my format identifier.

public void
initializeReplicationSlaveRole()

Initializes logOut so that log received from the replication master can be appended to the log file.

private boolean

Returns:

true if the log file is empty, else false.
initLogFile
(StorageRandomAccessFile
the new log file to be initialized
newlog
,
long
the log file number
number
,
long
the end position of the previous log record
prevLogRecordEndInstant
)

Initialize the log to the correct format with the given version and log file number.

public boolean

Returns:

true If the master replication mode is turned on and the unlogged operations need to be logged. false If the master replication mode is turned off and the unlogged operations need not be logged.
inReplicationMasterMode
()

Implements org.apache.derby.iapi.store.raw.log.LogFactory.inReplicationMasterMode.

Used to determine if the replication master mode has been started, and the logging for unlogged operations needs to be enabled.

public boolean
public boolean
private static boolean
isFullUpgrade(final Properties startParams, final String oldVersionInfo)

Must be private so that user code can't call this entry point.

public boolean
logArchived()

Implements org.apache.derby.iapi.store.raw.log.LogFactory.logArchived.

Backup restore - is the log being archived to some directory?

protected void
logErrMsg(String msg)

Print error message to user about the log MT - not needed, informational only

protected void
logErrMsg(Throwable t)

Print error message to user about the log MT - not needed, informational only

private void
logErrMsgForDurabilityTestModeNoSync()

In case of boot errors, and if database is either booted with derby.system.durability=test or was previously at any time booted in this mode, mention in the error message that the error is probably because the derby.system.durability was set.

private long
logtest_appendPartialLogRecord(byte[] data, int offset, int length, byte[] optionalData, int optionalDataOffset, int optionalDataLength)

Writes out a partial log record - takes the appendLogRecord.

public StandardException
markCorrupt(StandardException originalError)

Implements org.apache.derby.iapi.store.raw.Corruptable.markCorrupt.

Once the log factory is marked as corrupt then the raw store will shut down.

protected LogScan
openBackwardsScan(long startAt, LogInstant stopAt)

Scan backward from start position.

protected LogScan
openBackwardsScan(LogInstant stopAt)

Scan backward from end of log.

public ScanHandle
openFlushedScan(DatabaseInstant
- the LogInstant where we start our scan. null means start at the beginning of the log. This function raises an error if startAt is a LogInstant which is not in the log.
start
,
int
- log record groups the scanner wants.
groupsIWant
)

Implements org.apache.derby.iapi.store.raw.log.LogFactory.openFlushedScan.

Get a ScanHandle to scan flushed records from the log.

public LogScan
openForwardsFlushedScan(LogInstant
- the LogInstant where we start our scan. null means start at the beginning of the log. This function raises an error if startAt is a LogInstant which is not in the log.
startAt
)

Implements org.apache.derby.iapi.store.raw.log.LogFactory.openForwardsFlushedScan.

Open a forward scan of the transaction log.

protected LogScan
openForwardsScan(long
- if startAt == INVALID_LOG_INSTANT, start from the beginning of the log. Otherwise, start scan from startAt.
startAt
,
LogInstant
- if not null, stop at this log instant (inclusive). Otherwise, stop at the end of the log
stopAt
)

Scan Forward from start position.

public LogScan
openForwardsScan(LogInstant
- the LogInstant where we start our scan. null means start at the beginning of the log. This function raises an error if startAt is a LogInstant which is not in the log.
startAt
,
LogInstant
- the LogInstant where we stop our scan. null means stop at the end of the log. This function raises an error if stopAt is a LogInstant which is not in the log.
stopAt
)

Implements org.apache.derby.iapi.store.raw.log.LogFactory.openForwardsScan.

Get a forwards scan

private StorageRandomAccessFile
openLogFileInWriteMode(StorageFile logFile)

open the given log file name for writes; if file can not be be opened in write sync mode then disable the write sync mode and open the file in "rw" mode.

public int
performWork(ContextManager
the contextManager set up by the DaemonService. There may or may not be the necessary context on it, depending on which other Serviceable object it has done work for.
context
)

Implements org.apache.derby.iapi.services.daemon.Serviceable.performWork.

Do whatever it is that you want the daemon to do for you.

private void
private void
printErrorStack(Throwable
trace starts from this error
t
)

print stack trace from the Throwable including its nested exceptions

protected boolean
private synchronized boolean
private synchronized boolean
protected boolean
protected boolean
private synchronized OutputStreamWriter
private synchronized String[]
privList(File file)

private synchronized String[]
protected boolean
private synchronized StorageRandomAccessFile
private boolean
private long
readControlFile(StorageFile logControlFileName, Properties startParams)

public void
recover(DataFactory
- the data factory
df
,
TransactionFactory
- the transaction factory
tf
)

Implements org.apache.derby.iapi.store.raw.log.LogFactory.recover.

Recover the rawStore to a consistent state using the log.

private boolean
restoreLogs(Properties properties)

This function restores logs based on the following attributes are specified on connection URL: Attribute.CREATE_FROM (Create database from backup if it does not exist) Attribute.RESTORE_FROM (Delete the whole database if it exists and then restore it from backup) Attribute.ROLL_FORWARD_RECOVERY_FROM:(Perform Rollforward Recovery; except for the log directory everything else is replaced by the copy from backup.

public final Object
run()

private synchronized boolean
runBooleanAction(int action, StorageFile file)

public boolean
serviceASAP()

Implements org.apache.derby.iapi.services.daemon.Serviceable.serviceASAP.

If this work should be done as soon as possible, then return true.

public boolean
serviceImmediately()

Implements org.apache.derby.iapi.services.daemon.Serviceable.serviceImmediately.

If this work should be done immediately on the user thread then return true.

public void
setDatabaseEncrypted(boolean
true if the database is encrypted, false if not
isEncrypted
,
boolean
true if log needs to be flushed, false otherwise
flushLog
)

Implements org.apache.derby.iapi.store.raw.log.LogFactory.setDatabaseEncrypted.

Sets whether the database is encrypted, all the transaction log has to be encrypted, and flush the log if requested.

private void
setEndPosition(long newPosition)

set the endPosition of the log and make sure the new position won't spill off the end of the log

public void
setRawStoreFactory(RawStoreFactory rsf)

Implements org.apache.derby.iapi.store.raw.log.LogFactory.setRawStoreFactory.

Make log factory aware of which raw store factory it belongs to

public void
public void
public void
startReplicationMasterRole(MasterFactory
The MasterFactory service responsible for controlling the master side replication behaviour.
masterFactory
)

Implements org.apache.derby.iapi.store.raw.log.LogFactory.startReplicationMasterRole.

Make this LogFactory pass log records to the MasterFactory every time a log record is appended to the log on disk, and notify the MasterFactory when a log disk flush has taken place.

public void
stop()

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

Stop the log factory

MT- caller provide synchronization (RESOLVE: this should be called AFTER dataFactory and transFactory are stopped)

public void
stopReplicationMasterRole()

Implements org.apache.derby.iapi.store.raw.log.LogFactory.stopReplicationMasterRole.

Stop this LogFactory from passing log records to the MasterFactory and from notifying the MasterFactory when a log disk flush has taken place.

public void
stopReplicationSlaveRole()

Stop the slave functionality for this LogFactory.

public void
switchLogFile()

Switch to the next log file if possible.

private void
syncFile(StorageRandomAccessFile raf)

Utility routine to call sync() on the input file descriptor.

protected void
testLogFull()

Simulate a log full condition if TEST_LOG_FULL is set to true, then the property TEST_RECORD_TO_FILL_LOG indicates the number of times this function is call before an IOException simulating a log full condition is raised.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

private void
truncateLog(CheckpointOperation checkpoint)

Get rid of old and unnecessary log files

MT- only one truncate log is allowed to be taking place at any given time.

private void
truncateLog(long
The log file number of the oldest log file needed for recovery.
firstLogNeeded
)

Get rid of old and unnecessary log files

public void
unfreezePersistentStore()

Implements org.apache.derby.iapi.store.raw.log.LogFactory.unfreezePersistentStore.

Backup restore - start sending log record to the log stream

private boolean

Returns:

true if the log file is of the current version and of the correct format
verifyLogFormat
(StorageFile
the name of the log file
logFileName
,
long
the log file number
number
)

Verify that we the log file is of the right format and of the right version and log file number.

private boolean

Returns:

true if the log file is of the current version and of the correct format
verifyLogFormat
(StorageRandomAccessFile
the log file
log
,
long
the log file number
number
)

Verify that we the log file is of the right format and of the right version and log file number.

private IOException
wrapSE(Exception se)

This method compensates for the fact that callers of run() have to handle the StandardException raised by that method.

pack-priv boolean
writeControlFile(StorageFile logControlFileName, long value)

Carefully write out this value to the control file.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait