com.continuent.tungsten.replicator.thl.log
Class LogFile

java.lang.Object
  extended by com.continuent.tungsten.replicator.thl.log.LogFile

public class LogFile
extends java.lang.Object

This class manages I/O on a physical log file. It handles streams to read or write from the underlying file.

Version:
1.0
Author:
Stephane Giron, Robert Hodges

Field Summary
static int HEADER_LENGTH
           
static int NO_WAIT
          Return immediately from write when there are no data.
 
Constructor Summary
LogFile(java.io.File file)
          Creates a log file from a simple file.
LogFile(java.io.File parentDirectory, java.lang.String fileName)
          Creates a file from a parent directory and child filename.
 
Method Summary
protected  void assertAnyMode()
          Returns only if log file is in read or write mode.
protected  void assertReadMode()
          Returns only if log file is in read mode.
protected  void assertWriteMode()
          Returns only if log file is in write mode.
 void close()
          Flush and close file.
 void create(long seqno)
          Create a new log file.
 void flush()
          Synchronizes file writes using flush with optional fsync.
 long getBaseSeqno()
          Returns the base sequence number from the file header.
 int getBufferSize()
           
 java.io.File getFile()
          Returns the log file.
 long getFlushIntervalMillis()
           
 long getLength()
          Returns the length of the file, including any unbuffered writes.
 LogFlushTask getLogFlushTask()
          Returns the current log flush task, if any.
 long getOffset()
          Returns the current position in the log file.
 boolean isFsyncOnFlush()
           
 void openRead()
          Open the log file for reading.
 void openWrite()
          Prepare the log file for writing.
protected  int readInt()
          Read a single integer.
protected  long readLong()
          Reads a single long.
 LogRecord readRecord(int waitMillis)
          Reads a record from the file into a byte array.
protected  short readShort()
          Reads a single short.
 void seekOffset(long offset)
          Seeks to a particular offset in the file.
 void setBufferSize(int bufferSize)
           
 void setFlushIntervalMillis(long flushIntervalMillis)
           
 void setFsyncOnFlush(boolean fsyncOnFlush)
           
 void setLength(long length)
          Truncate the file to the provided length.
 void setLogSyncTask(LogFlushTask logFlushTask)
          Sets the log flush task.
 java.lang.String toString()
          Returns a nicely formatting description of the file.
protected  void write(int myInt)
           
protected  void write(long seqno)
           
protected  void write(short myShort)
           
 boolean writeRecord(LogRecord record, int logFileSize)
          Writes a buffer to the log file and returns true if we have exceeded the log file size.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

HEADER_LENGTH

public static final int HEADER_LENGTH
See Also:
Constant Field Values

NO_WAIT

public static final int NO_WAIT
Return immediately from write when there are no data.

See Also:
Constant Field Values
Constructor Detail

LogFile

public LogFile(java.io.File parentDirectory,
               java.lang.String fileName)
Creates a file from a parent directory and child filename. The file must exist.

Parameters:
parentDirectory - Log file directory
fileName - Log file name

LogFile

public LogFile(java.io.File file)
Creates a log file from a simple file. The file must exist.

Parameters:
file - Log file specification
Method Detail

getFile

public java.io.File getFile()
Returns the log file.


getLogFlushTask

public LogFlushTask getLogFlushTask()
Returns the current log flush task, if any.


setLogSyncTask

public void setLogSyncTask(LogFlushTask logFlushTask)
Sets the log flush task. This enables delayed flush/fsync using flushIntervalMillis.


getFlushIntervalMillis

public long getFlushIntervalMillis()

setFlushIntervalMillis

public void setFlushIntervalMillis(long flushIntervalMillis)

isFsyncOnFlush

public boolean isFsyncOnFlush()

setFsyncOnFlush

public void setFsyncOnFlush(boolean fsyncOnFlush)

getBufferSize

public int getBufferSize()

setBufferSize

public void setBufferSize(int bufferSize)

openRead

public void openRead()
              throws ReplicatorException,
                     java.lang.InterruptedException
Open the log file for reading. The log cannot be written.

Throws:
ReplicatorException - Thrown if file cannot be opened
java.lang.InterruptedException - Thrown if thread is interrupted

openWrite

public void openWrite()
               throws ReplicatorException,
                      java.lang.InterruptedException
Prepare the log file for writing. The write offset is automatically set to the end of the file.

Throws:
ReplicatorException - Thrown if file cannot be opened
java.lang.InterruptedException - Thrown if we are interrupted

create

public void create(long seqno)
            throws ReplicatorException,
                   java.lang.InterruptedException
Create a new log file. We must write a header with a base sequence number. NOTE: The file offset is positioned after the header.

Parameters:
seqno - Base sequence number of this file (written to header)
Throws:
ReplicatorException
java.lang.InterruptedException

close

public void close()
Flush and close file. It should be called after all other methods as part of a clean shutdown.


assertAnyMode

protected void assertAnyMode()
                      throws ReplicatorException
Returns only if log file is in read or write mode.

Throws:
ReplicatorException

assertWriteMode

protected void assertWriteMode()
                        throws ReplicatorException,
                               java.lang.InterruptedException
Returns only if log file is in write mode. If we are in read mode, this will switch over to write mode and position at the end of the file.

Throws:
ReplicatorException
java.lang.InterruptedException

assertReadMode

protected void assertReadMode()
                       throws ReplicatorException,
                              java.lang.InterruptedException
Returns only if log file is in read mode.

Throws:
ReplicatorException
java.lang.InterruptedException

getBaseSeqno

public long getBaseSeqno()
Returns the base sequence number from the file header.


getLength

public long getLength()
               throws ReplicatorException
Returns the length of the file, including any unbuffered writes.

Throws:
ReplicatorException

getOffset

public long getOffset()
               throws ReplicatorException
Returns the current position in the log file.

Throws:
ReplicatorException

seekOffset

public void seekOffset(long offset)
                throws java.io.IOException,
                       ReplicatorException,
                       java.lang.InterruptedException
Seeks to a particular offset in the file.

Throws:
java.io.IOException - If positioning results in an error.
ReplicatorException
java.lang.InterruptedException

readRecord

public LogRecord readRecord(int waitMillis)
                     throws java.io.IOException,
                            java.lang.InterruptedException,
                            LogTimeoutException,
                            ReplicatorException
Reads a record from the file into a byte array. We may encounter a number of unpredictable conditions at this point that we need to report accurately to layers above us that will decide whether it represents a problem.

Parameters:
waitMillis - Number of milliseconds to wait for data to be available. 0 means do not wait.
Returns:
A log record if we can read one before timing out
Throws:
java.io.IOException - Thrown if there is an I/O error
java.lang.InterruptedException - Thrown if we are interrupted
LogTimeoutException - Thrown if we timeout while waiting for data to appear
ReplicatorException

readShort

protected short readShort()
                   throws java.io.IOException,
                          ReplicatorException,
                          java.lang.InterruptedException
Reads a single short.

Throws:
java.io.IOException
ReplicatorException
java.lang.InterruptedException

readInt

protected int readInt()
               throws java.io.IOException,
                      ReplicatorException,
                      java.lang.InterruptedException
Read a single integer.

Throws:
java.io.IOException
ReplicatorException
java.lang.InterruptedException

readLong

protected long readLong()
                 throws java.io.IOException,
                        ReplicatorException,
                        java.lang.InterruptedException
Reads a single long.

Throws:
java.io.IOException
ReplicatorException
java.lang.InterruptedException

setLength

public void setLength(long length)
               throws ReplicatorException,
                      java.lang.InterruptedException
Truncate the file to the provided length. Performs an automatic fsync.

Parameters:
length - New file length
Throws:
ReplicatorException
java.lang.InterruptedException

write

protected void write(int myInt)
              throws java.io.IOException,
                     ReplicatorException,
                     java.lang.InterruptedException
Throws:
java.io.IOException
ReplicatorException
java.lang.InterruptedException

write

protected void write(long seqno)
              throws java.io.IOException,
                     ReplicatorException,
                     java.lang.InterruptedException
Throws:
java.io.IOException
ReplicatorException
java.lang.InterruptedException

write

protected void write(short myShort)
              throws java.io.IOException,
                     ReplicatorException,
                     java.lang.InterruptedException
Throws:
java.io.IOException
ReplicatorException
java.lang.InterruptedException

writeRecord

public boolean writeRecord(LogRecord record,
                           int logFileSize)
                    throws java.io.IOException,
                           java.lang.InterruptedException,
                           ReplicatorException
Writes a buffer to the log file and returns true if we have exceeded the log file size.

Parameters:
record - Log record to write
logFileSize - Maximum log file size
Returns:
true if log file size exceeded
Throws:
java.io.IOException
java.lang.InterruptedException
ReplicatorException

flush

public void flush()
           throws java.io.IOException,
                  ReplicatorException,
                  java.lang.InterruptedException
Synchronizes file writes using flush with optional fsync. You must call this method to commit data.

Throws:
java.io.IOException
ReplicatorException
java.lang.InterruptedException

toString

public java.lang.String toString()
Returns a nicely formatting description of the file.

Overrides:
toString in class java.lang.Object