APIdia Launch Partner
Top Description Inners Fields Constructors Methods
sun.rmi.log

public Class ReliableLog

extends Object
Class Inheritance
Imports
java.io.*, java.lang.reflect.Constructor

This class is a simple implementation of a reliable Log. The client of a ReliableLog must provide a set of callbacks (via a LogHandler) that enables a ReliableLog to read and write checkpoints and log records. This implementation ensures that the current value of the data stored (via a ReliableLog) is recoverable after a system crash.

The secondary storage strategy is to record values in files using a representation of the caller's choosing. Two sorts of files are kept: snapshots and logs. At any instant, one snapshot is current. The log consists of a sequence of updates that have occurred since the current snapshot was taken. The current stable state is the value of the snapshot, as modified by the sequence of updates in the log. From time to time, the client of a ReliableLog instructs the package to make a new snapshot and clear the log. A ReliableLog arranges disk writes such that updates are stable (as long as the changes are force-written to disk) and atomic : no update is lost, and each update either is recorded completely in the log or not at all. Making a new snapshot is also atomic.

Normal use for maintaining the recoverable store is as follows: The client maintains the relevant data structure in virtual memory. As updates happen to the structure, the client informs the ReliableLog (all it "log") by calling log.update. Periodically, the client calls log.snapshot to provide the current value of the data structure. On restart, the client calls log.recover to obtain the latest snapshot and the following sequences of updates; the client applies the updates to the snapshot to obtain the state that existed before the crash.

The current logfile format is:

  1. a format version number (two 4-octet integers, major and minor), followed by
  2. a sequence of log records. Each log record contains, in order,
    1. a 4-octet integer representing the length of the following log data,
    2. the log data (variable length).

Author
Ann Wollrath
See Also
LogHandler

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
ReliableLog.LogFile

ReliableLog's log file implementation.

Field Summary

Modifier and TypeField and Description
private boolean
private File
private static long
private LogHandler
private final byte[]
private static int
private long
private long
private ReliableLog.LogFile
private long
private static final Constructor<? extends ReliableLog.LogFile>
logClassConstructor

Constructor for the log file.

private int
private static String
private String
private int
private int
private static String
public static final int
public static final int
private long
private static String
private int
private static String

Constructor Summary

AccessConstructor and Description
public
ReliableLog(String
path to the stable storage directory
dirPath
,
LogHandler
the closure object containing callbacks for logging and recovery
handler
,
boolean
ignored
pad
)

Creates a ReliableLog to handle checkpoints and logging in a stable storage directory.

public
ReliableLog(String
path to the stable storage directory
dirPath
,
LogHandler
the closure object containing callbacks for logging and recovery
handler
)

Creates a ReliableLog to handle checkpoints and logging in a stable storage directory.

Method Summary

Modifier and TypeMethod and Description
public synchronized void
close()

Close the stable storage directory in an orderly manner.

private void
commitToNewVersion()

Commits (atomically) the new version.

private void
createFirstVersion()

Creates the initial version file

private void
deleteFile(String
the name of the file
name
)

Delete a file.

private void
deleteLogFile(int
the version to remove
ver
)

Removes the log file.

private void
deleteNewVersionFile()

Removes the new version number file.

private void
deleteSnapshot(int
the version to remove
ver
)

Removes the snapshot file.

private String
fName(String
the leaf name of the file
name
)

Generates a filename prepended with the stable storage directory path.

private static Constructor<? extends ReliableLog.LogFile>
getLogClassConstructor()

Returns the constructor for the log file if the system property sun.rmi.log.class is non-null and the class specified by the property a) can be loaded, b) is a subclass of LogFile, and c) has a public two-arg constructor (String, String); otherwise returns null.

private void
getVersion()

Sets the version.

private void
incrVersion()

Increments the directory version number.

private void
initializeLogFile()

Creates a new log file, truncated and initialized with the format version number preferred by this implementation.

public long
logSize()

Returns the size of the log file in bytes;

private void
openLogFile(boolean
if true and file exists, file is truncated to zero length
truncate
)

Opens the log file in read/write mode.

private int

Returns:

the version
readVersion
(String
the name of the version file
name
)

Reads version number from a file.

public synchronized Object
recover()

Returns an object which is the value recorded in the current snapshot.

private Object

Returns:

the resulting state of the object after all updates
recoverUpdates
(Object
the most recent snapshot
state
)

Applies outstanding updates to the snapshot.

public synchronized void
snapshot(Object
the object representing the new snapshot
value
)

Records this value as the current snapshot by invoking the client supplied "snapshot" callback and then empties the log.

public long
snapshotSize()

Returns the size of the snapshot file in bytes;

public synchronized void
update(Object
the object representing the update
value
)

Records this update in the log file (does not force update to disk).

public synchronized void
update(Object
the object representing the update
value
,
boolean
ignored; changes are always forced to disk
forceToDisk
)

Records this update in the log file.

private String
versionName(String
version file name
name
)

Generates a version 0 filename prepended with the stable storage directory path

private String
versionName(String prefix, int
a version number
ver
)

Generates a version filename prepended with the stable storage directory path with the version number as a suffix.

private void
writeInt(DataOutput
output stream
out
,
int
int value
val
)

Write an int value in single write operation.

private void
writeVersionFile(boolean
if true, writes to a new version file
newVersion
)

Writes out version number to file.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

Debugback to summary
private boolean Debug
dirback to summary
private File dir
diskPageSizeback to summary
private static long diskPageSize
handlerback to summary
private LogHandler handler
intBufback to summary
private final byte[] intBuf
intBytesback to summary
private static int intBytes
lastLogback to summary
private long lastLog
lastSnapshotback to summary
private long lastSnapshot
logback to summary
private ReliableLog.LogFile log
logBytesback to summary
private long logBytes
logClassConstructorback to summary
private static final Constructor<? extends ReliableLog.LogFile> logClassConstructor

Constructor for the log file. If the system property sun.rmi.log.class is non-null and the class specified by this property a) can be loaded, b) is a subclass of LogFile, and c) has a public two-arg constructor (String, String), ReliableLog uses the constructor to construct the LogFile.

logEntriesback to summary
private int logEntries
logfilePrefixback to summary
private static String logfilePrefix
logNameback to summary
private String logName
majorFormatVersionback to summary
private int majorFormatVersion
minorFormatVersionback to summary
private int minorFormatVersion
newVersionFileback to summary
private static String newVersionFile
PreferredMajorVersionback to summary
public static final int PreferredMajorVersion
PreferredMinorVersionback to summary
public static final int PreferredMinorVersion
snapshotBytesback to summary
private long snapshotBytes
snapshotPrefixback to summary
private static String snapshotPrefix
versionback to summary
private int version
versionFileback to summary
private static String versionFile

Constructor Detail

ReliableLogback to summary
public ReliableLog(String dirPath, LogHandler handler, boolean pad) throws IOException

Creates a ReliableLog to handle checkpoints and logging in a stable storage directory.

Parameters
dirPath:String

path to the stable storage directory

handler:LogHandler

the closure object containing callbacks for logging and recovery

pad:boolean

ignored

Exceptions
IOException:
If a directory creation error has occurred or if initialSnapshot callback raises an exception or if an exception occurs during invocation of the handler's snapshot method or if other IOException occurs.
ReliableLogback to summary
public ReliableLog(String dirPath, LogHandler handler) throws IOException

Creates a ReliableLog to handle checkpoints and logging in a stable storage directory.

Parameters
dirPath:String

path to the stable storage directory

handler:LogHandler

the closure object containing callbacks for logging and recovery

Exceptions
IOException:
If a directory creation error has occurred or if initialSnapshot callback raises an exception

Method Detail

closeback to summary
public synchronized void close() throws IOException

Close the stable storage directory in an orderly manner.

Exceptions
IOException:
If an I/O error occurs when the log is closed
commitToNewVersionback to summary
private void commitToNewVersion() throws IOException

Commits (atomically) the new version.

Exceptions
IOException:
If an I/O error has occurred.
createFirstVersionback to summary
private void createFirstVersion() throws IOException

Creates the initial version file

Exceptions
IOException:
If an I/O error has occurred.
deleteFileback to summary
private void deleteFile(String name) throws IOException

Delete a file.

Parameters
name:String

the name of the file

Exceptions
IOException:
If new version file couldn't be removed
deleteLogFileback to summary
private void deleteLogFile(int ver) throws IOException

Removes the log file.

Parameters
ver:int

the version to remove

Exceptions
IOException:
If an I/O error has occurred.
deleteNewVersionFileback to summary
private void deleteNewVersionFile() throws IOException

Removes the new version number file.

Exceptions
IOException:
If an I/O error has occurred.
deleteSnapshotback to summary
private void deleteSnapshot(int ver) throws IOException

Removes the snapshot file.

Parameters
ver:int

the version to remove

Exceptions
IOException:
If an I/O error has occurred.
fNameback to summary
private String fName(String name)

Generates a filename prepended with the stable storage directory path.

Parameters
name:String

the leaf name of the file

getLogClassConstructorback to summary
private static Constructor<? extends ReliableLog.LogFile> getLogClassConstructor()

Returns the constructor for the log file if the system property sun.rmi.log.class is non-null and the class specified by the property a) can be loaded, b) is a subclass of LogFile, and c) has a public two-arg constructor (String, String); otherwise returns null.

getVersionback to summary
private void getVersion() throws IOException

Sets the version. If version file does not exist, the initial version file is created.

Exceptions
IOException:
If an I/O error has occurred.
incrVersionback to summary
private void incrVersion()

Increments the directory version number.

initializeLogFileback to summary
private void initializeLogFile() throws IOException

Creates a new log file, truncated and initialized with the format version number preferred by this implementation.

Environment: inited, synchronized

Precondition

valid: log, log contains nothing useful

Postcondition: if successful, log is initialised with the format version number (Preferred{Major,Minor}Version), and logBytes is set to the resulting size of the updatelog, and logEntries is set to zero. Otherwise, log is in an indeterminate state, and logBytes is unchanged, and logEntries is unchanged.

Exceptions
IOException:
If an I/O error has occurred.
logSizeback to summary
public long logSize()

Returns the size of the log file in bytes;

openLogFileback to summary
private void openLogFile(boolean truncate) throws IOException

Opens the log file in read/write mode. If file does not exist, it is created.

Parameters
truncate:boolean

if true and file exists, file is truncated to zero length

Exceptions
IOException:
If an I/O error has occurred.
readVersionback to summary
private int readVersion(String name) throws IOException

Reads version number from a file.

Parameters
name:String

the name of the version file

Returns:int

the version

Exceptions
IOException:
If an I/O error has occurred.
recoverback to summary
public synchronized Object recover() throws IOException

Returns an object which is the value recorded in the current snapshot. This snapshot is recovered by calling the client supplied callback "recover" and then subsequently invoking the "readUpdate" callback to apply any logged updates to the state.

Exceptions
IOException:
If recovery fails due to serious log corruption, read update failure, or if an exception occurs during the recover callback
recoverUpdatesback to summary
private Object recoverUpdates(Object state) throws IOException

Applies outstanding updates to the snapshot.

Parameters
state:Object

the most recent snapshot

Returns:Object

the resulting state of the object after all updates

Exceptions
IOException:
If serious log corruption is detected or if an exception occurred during a readUpdate callback or if other I/O error has occurred.
snapshotback to summary
public synchronized void snapshot(Object value) throws IOException

Records this value as the current snapshot by invoking the client supplied "snapshot" callback and then empties the log.

Parameters
value:Object

the object representing the new snapshot

Exceptions
IOException:
If an exception occurred during the snapshot callback or if other I/O error has occurred during the snapshot process
snapshotSizeback to summary
public long snapshotSize()

Returns the size of the snapshot file in bytes;

updateback to summary
public synchronized void update(Object value) throws IOException

Records this update in the log file (does not force update to disk). The update is recorded by calling the client's "writeUpdate" callback. This method must not be called until this log's recover method has been invoked (and completed).

Parameters
value:Object

the object representing the update

Exceptions
IOException:
If an exception occurred during a writeUpdate callback or if other I/O error has occurred.
updateback to summary
public synchronized void update(Object value, boolean forceToDisk) throws IOException

Records this update in the log file. The update is recorded by calling the client's writeUpdate callback. This method must not be called until this log's recover method has been invoked (and completed).

Parameters
value:Object

the object representing the update

forceToDisk:boolean

ignored; changes are always forced to disk

Exceptions
IOException:
If force-write to log failed or an exception occurred during the writeUpdate callback or if other I/O error occurs while updating the log.
versionNameback to summary
private String versionName(String name)

Generates a version 0 filename prepended with the stable storage directory path

Parameters
name:String

version file name

versionNameback to summary
private String versionName(String prefix, int ver)

Generates a version filename prepended with the stable storage directory path with the version number as a suffix.

Parameters
ver:int

a version number

writeIntback to summary
private void writeInt(DataOutput out, int val) throws IOException

Write an int value in single write operation. This method assumes that the caller is synchronized on the log file.

Parameters
out:DataOutput

output stream

val:int

int value

Exceptions
IOException:
if any other I/O error occurs
writeVersionFileback to summary
private void writeVersionFile(boolean newVersion) throws IOException

Writes out version number to file.

Parameters
newVersion:boolean

if true, writes to a new version file

Exceptions
IOException:
If an I/O error has occurred.
sun.rmi.log back to summary

public Class ReliableLog.LogFile

extends RandomAccessFile
Class Inheritance

ReliableLog's log file implementation. This implementation is subclassable for testing purposes.

Field Summary

Modifier and TypeField and Description
private final FileDescriptor

Constructor Summary

AccessConstructor and Description
public
LogFile(String name, String mode)

Constructs a LogFile and initializes the file descriptor.

Method Summary

Modifier and TypeMethod and Description
protected boolean
checkSpansBoundary(long fp)

Returns true if writing 4 bytes starting at the specified file position, would span a 512 byte sector boundary; otherwise returns false.

protected void
sync()

Invokes sync on the file descriptor for this log file.

Inherited from java.io.RandomAccessFile:
closegetChannelgetFDgetFilePointerlengthreadreadreadreadBooleanreadBytereadCharreadDoublereadFloatreadFullyreadFullyreadIntreadLinereadLongreadShortreadUnsignedBytereadUnsignedShortreadUTFseeksetLengthskipByteswritewritewritewriteBooleanwriteBytewriteByteswriteCharwriteCharswriteDoublewriteFloatwriteIntwriteLongwriteShortwriteUTF

Field Detail

fdback to summary
private final FileDescriptor fd

Hides java.io.RandomAccessFile.fd.

Constructor Detail

LogFileback to summary
public LogFile(String name, String mode) throws FileNotFoundException, IOException

Constructs a LogFile and initializes the file descriptor.

Method Detail

checkSpansBoundaryback to summary
protected boolean checkSpansBoundary(long fp)

Returns true if writing 4 bytes starting at the specified file position, would span a 512 byte sector boundary; otherwise returns false.

syncback to summary
protected void sync() throws IOException

Invokes sync on the file descriptor for this log file.