com.sleepycat.je.log
Class FileManager

java.lang.Object
  extended by com.sleepycat.je.log.FileManager

public class FileManager
extends Object

The FileManager presents the abstraction of one contiguous file. It doles out LSNs.


Nested Class Summary
static class FileManager.FileMode
           
(package private)  class FileManager.LogEndFileDescriptor
          The LogEndFileDescriptor is used to write and fsync the end of the log.
 
Field Summary
static String BAD_SUFFIX
           
static String CIF_SUFFIX
           
static String DEL_SUFFIX
           
(package private) static String[] DEL_SUFFIXES
           
(package private) static boolean IO_EXCEPTION_TESTING
           
static String JE_SUFFIX
           
(package private) static String[] JE_SUFFIXES
           
static String LOCK_SUFFIX
           
(package private) static boolean RUNRECOVERY_EXCEPTION_TESTING
           
 
Constructor Summary
FileManager(EnvironmentImpl envImpl, File dbEnvHome, boolean readOnly)
          Set up the file cache and initialize the file manager to point to the beginning of the log.
 
Method Summary
(package private)  boolean bumpLsn(long size)
          Increase the current log position by "size" bytes.
 void clear()
          Close all file handles and empty the cache.
 void close()
          Clear the file lock.
 void deleteFile(long fileNum)
          Delete log file NNNNNNNN.
 boolean filesExist()
           
static int firstLogEntryOffset()
           
(package private)  void forceNewLogFile()
          Set the flag that causes a new file to be written before the next write.
 Long[] getAllFileNumbers()
          Get all JE file numbers.
(package private)  Set getCacheKeys()
           
 long getCurrentFileNum()
           
(package private)  FileHandle getFileHandle(long fileNum)
          Return a read only file handle that corresponds the this file number.
(package private)  long getFileHeaderPrevOffset(long fileNum)
           
static String getFileName(long fileNum, String suffix)
           
 Long getFirstFileNum()
          public for cleaner.
 Long getFollowingFileNum(long currentFileNum, boolean forward)
          Get the next file number before/after currentFileNum.
 String getFullFileName(long fileNum, String suffix)
           
(package private)  String[] getFullFileNames(long fileNum)
           
 Long getLastFileNum()
           
 long getLastUsedLsn()
          Return the last allocated LSN in the log.
 long getNextLsn()
          Return the next available LSN in the log.
 long getNFSyncRequests()
           
 long getNFSyncs()
           
 long getNFSyncTimeouts()
           
(package private)  long getPrevEntryOffset()
           
 boolean getReadOnly()
           
(package private)  void groupSync()
          Flush a file using the group sync mechanism, trying to amortize off other syncs.
static String[] listFiles(File envDirFile, String[] suffixes)
          Find je files, flavor for unit test support.
(package private)  String[] listFiles(String[] suffixes)
          Find je files.
(package private)  void loadStats(StatsConfig config, EnvironmentStats stats)
           
 boolean lockEnvironment(boolean readOnly, boolean exclusive)
          Lock the environment.
(package private)  void readFromFile(RandomAccessFile file, ByteBuffer readBuffer, long offset)
          Read a buffer from a file at a given offset, using NIO if so configured.
 void releaseExclusiveLock()
           
 void renameFile(long fileNum, String newSuffix)
          Rename this file to NNNNNNNN.suffix.
(package private)  void restoreLastPosition()
           
(package private)  void saveLastPosition()
           
 void setIncludeDeletedFiles(boolean includeDeletedFiles)
           
 void setLastPosition(long nextAvailableLsn, long lastUsedLsn, long prevOffset)
          Set the file manager's "end of log".
 void setSyncAtFileEnd(boolean sync)
          May be used to disable sync at file end to speed unit tests.
(package private)  void syncLogEnd()
          FSync the end of the log.
(package private)  void syncLogEndAndFinishFile()
          Sync the end of the log, close off this log file.
 void truncateLog(long fileNum, long offset)
          Truncate a log at this position.
(package private)  void writeLogBuffer(LogBuffer fullBuffer)
          Write out a log buffer to the file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

IO_EXCEPTION_TESTING

static boolean IO_EXCEPTION_TESTING

JE_SUFFIX

public static final String JE_SUFFIX
See Also:
Constant Field Values

CIF_SUFFIX

public static final String CIF_SUFFIX
See Also:
Constant Field Values

DEL_SUFFIX

public static final String DEL_SUFFIX
See Also:
Constant Field Values

BAD_SUFFIX

public static final String BAD_SUFFIX
See Also:
Constant Field Values

LOCK_SUFFIX

public static final String LOCK_SUFFIX
See Also:
Constant Field Values

DEL_SUFFIXES

static final String[] DEL_SUFFIXES

JE_SUFFIXES

static final String[] JE_SUFFIXES

RUNRECOVERY_EXCEPTION_TESTING

static boolean RUNRECOVERY_EXCEPTION_TESTING
Constructor Detail

FileManager

public FileManager(EnvironmentImpl envImpl,
                   File dbEnvHome,
                   boolean readOnly)
            throws DatabaseException
Set up the file cache and initialize the file manager to point to the beginning of the log.

Parameters:
configManager -
dbEnvHome - environment home directory
Throws:
DatabaseException
Method Detail

setLastPosition

public void setLastPosition(long nextAvailableLsn,
                            long lastUsedLsn,
                            long prevOffset)
Set the file manager's "end of log".

Parameters:
nextAvailableLsn - LSN to be used for the next log entry
lastUsedLsn - last LSN to have a valid entry, may be null
prevOffset - value to use for the prevOffset of the next entry. If the beginning of the file, this is 0.

saveLastPosition

void saveLastPosition()

restoreLastPosition

void restoreLastPosition()

setSyncAtFileEnd

public void setSyncAtFileEnd(boolean sync)
May be used to disable sync at file end to speed unit tests. Must only be used for unit testing, since log corruption may result.


getFirstFileNum

public Long getFirstFileNum()
public for cleaner.

Returns:
the number of the first file in this environment.

getReadOnly

public boolean getReadOnly()

getLastFileNum

public Long getLastFileNum()
Returns:
the number of the last file in this environment.

getCurrentFileNum

public long getCurrentFileNum()

setIncludeDeletedFiles

public void setIncludeDeletedFiles(boolean includeDeletedFiles)

getAllFileNumbers

public Long[] getAllFileNumbers()
Get all JE file numbers.

Returns:
an array of all JE file numbers.

getFollowingFileNum

public Long getFollowingFileNum(long currentFileNum,
                                boolean forward)
Get the next file number before/after currentFileNum.

Parameters:
currentFileNum - the file we're at right now. Note that it may not exist, if it's been cleaned and renamed.
forward - if true, we want the next larger file, if false we want the previous file
Returns:
null if there is no following file, or if filenum doesn't exist

filesExist

public boolean filesExist()
Returns:
true if there are any files at all.

listFiles

String[] listFiles(String[] suffixes)
Find je files. Return names sorted in ascending fashion.

Parameters:
suffix - which type of file we're looking for
Returns:
array of file names

listFiles

public static String[] listFiles(File envDirFile,
                                 String[] suffixes)
Find je files, flavor for unit test support.

Parameters:
suffix - which type of file we're looking for
Returns:
array of file names

getFullFileNames

String[] getFullFileNames(long fileNum)
Returns:
the full file name and path for the nth je file.

getFullFileName

public String getFullFileName(long fileNum,
                              String suffix)
Returns:
the full file name and path for the given file number and suffix.

getFileName

public static String getFileName(long fileNum,
                                 String suffix)
Returns:
the file name for the nth file.

renameFile

public void renameFile(long fileNum,
                       String newSuffix)
                throws DatabaseException,
                       IOException
Rename this file to NNNNNNNN.suffix. If that file already exists, try NNNNNNNN.suffix.1, etc. Used for deleting files or moving corrupt files aside.

Parameters:
fileNum - the file we want to move
newSuffix - the new file suffix
Throws:
DatabaseException
IOException

deleteFile

public void deleteFile(long fileNum)
                throws DatabaseException,
                       IOException
Delete log file NNNNNNNN.

Parameters:
fileNum - the file we want to move
Throws:
DatabaseException
IOException

getFileHandle

FileHandle getFileHandle(long fileNum)
                   throws LogException,
                          DatabaseException
Return a read only file handle that corresponds the this file number. Retrieve it from the cache or open it anew and validate the file header. This method takes a latch on this file, so that the file descriptor will be held in the cache as long as it's in use. When the user is done with the file, the latch must be released.

Parameters:
fileNum - which file
Returns:
the file handle for the existing or newly created file
Throws:
LogException
DatabaseException

getFileHeaderPrevOffset

long getFileHeaderPrevOffset(long fileNum)
                       throws IOException,
                              DatabaseException
Returns:
the prevOffset field stored in the file header.
Throws:
IOException
DatabaseException

getPrevEntryOffset

long getPrevEntryOffset()
Returns:
the file offset of the last LSN that was used. For constructing the headers of log entries. If the last LSN that was used was in a previous file, or this is the very first LSN of the whole system, return 0.

bumpLsn

boolean bumpLsn(long size)
Increase the current log position by "size" bytes. Move the prevOffset pointer along.

Parameters:
size - is an unsigned int
Returns:
true if we flipped to the next log file.

writeLogBuffer

void writeLogBuffer(LogBuffer fullBuffer)
              throws DatabaseException
Write out a log buffer to the file.

Parameters:
fullBuffer - buffer to write
Throws:
DatabaseException

readFromFile

void readFromFile(RandomAccessFile file,
                  ByteBuffer readBuffer,
                  long offset)
            throws IOException
Read a buffer from a file at a given offset, using NIO if so configured.

Throws:
IOException

syncLogEnd

void syncLogEnd()
          throws DatabaseException
FSync the end of the log.

Throws:
DatabaseException

syncLogEndAndFinishFile

void syncLogEndAndFinishFile()
                       throws DatabaseException,
                              IOException
Sync the end of the log, close off this log file. Should only be called under the log write latch.

Throws:
DatabaseException
IOException

groupSync

void groupSync()
         throws DatabaseException
Flush a file using the group sync mechanism, trying to amortize off other syncs.

Throws:
DatabaseException

clear

public void clear()
           throws IOException,
                  DatabaseException
Close all file handles and empty the cache.

Throws:
IOException
DatabaseException

close

public void close()
           throws IOException,
                  DatabaseException
Clear the file lock.

Throws:
IOException
DatabaseException

lockEnvironment

public boolean lockEnvironment(boolean readOnly,
                               boolean exclusive)
                        throws DatabaseException
Lock the environment. Return true if the lock was acquired. If exclusive is false, then this implements a single writer, multiple reader lock. If exclusive is true, then implement an exclusive lock. There is a lock file and there are two regions of the lock file: byte 0, and byte 1. Byte 0 is the exclusive writer process area of the lock file. If an environment is opened for write, then it attempts to take an exclusive write lock on byte 0. Byte 1 is the shared reader process area of the lock file. If an environment is opened for read-only, then it attempts to take a shared lock on byte 1. This is how we implement single writer, multi reader semantics. The cleaner, each time it is invoked, attempts to take an exclusive lock on byte 1. The owning process already either has an exclusive lock on byte 0, or a shared lock on byte 1. This will necessarily conflict with any shared locks on byte 1, even if it's in the same process and there are no other holders of that shared lock. So if there is only one read-only process, it will have byte 1 for shared access, and the cleaner can not run in it because it will attempt to get an exclusive lock on byte 1 (which is already locked for shared access by itself). If a write process comes along and tries to run the cleaner, it will attempt to get an exclusive lock on byte 1. If there are no other reader processes (with shared locks on byte 1), and no other writers (which are running cleaners on with exclusive locks on byte 1), then the cleaner will run.

Throws:
DatabaseException

releaseExclusiveLock

public void releaseExclusiveLock()
                          throws DatabaseException
Throws:
DatabaseException

truncateLog

public void truncateLog(long fileNum,
                        long offset)
                 throws IOException,
                        DatabaseException
Truncate a log at this position. Used by recovery to a timestamp utilities and by recovery to set the end-of-log position.

This method forces a new log file to be written next, if the last file (the file truncated to) has an old version in its header. This ensures that when the log is opened by an old version of JE, a version incompatibility will be detected. [#11243]

Throws:
IOException
DatabaseException

forceNewLogFile

void forceNewLogFile()
Set the flag that causes a new file to be written before the next write.


firstLogEntryOffset

public static int firstLogEntryOffset()
Returns:
the size in bytes of the file header log entry.

getNextLsn

public long getNextLsn()
Return the next available LSN in the log. Note that this is unsynchronized, so is only valid as an approximation of log size.


getLastUsedLsn

public long getLastUsedLsn()
Return the last allocated LSN in the log. Note that this is unsynchronized, so if it is called outside the log write latch it is only valid as an approximation of log size.


getNFSyncs

public long getNFSyncs()

getNFSyncRequests

public long getNFSyncRequests()

getNFSyncTimeouts

public long getNFSyncTimeouts()

loadStats

void loadStats(StatsConfig config,
               EnvironmentStats stats)
         throws DatabaseException
Throws:
DatabaseException

getCacheKeys

Set getCacheKeys()


Copyright 2004-2005 Sleepycat, Inc. All Rights Reserved.