public class LogAccessFile
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
private long |
checksumInstant |
private int |
checksumLength |
private ChecksumOperation |
checksumLogOperation |
private LogRecord |
checksumLogRecord |
private int |
checksumLogRecordSize |
private LogAccessFileBuffer |
currentBuffer |
private boolean |
databaseEncrypted |
private java.util.LinkedList<LogAccessFileBuffer> |
dirtyBuffers |
private boolean |
flushInProgress |
private java.util.LinkedList<LogAccessFileBuffer> |
freeBuffers |
(package private) boolean |
inReplicationMasterMode |
(package private) boolean |
inReplicationSlaveMode |
private StorageRandomAccessFile |
log |
private static int |
LOG_NUMBER_LOG_BUFFERS |
private static int |
LOG_RECORD_FIXED_OVERHEAD_SIZE
The fixed size of a log record is 16 bytes:
int length : 4 bytes
long instant : 8 bytes
int trailing length : 4 bytes
|
private static int |
LOG_RECORD_HEADER_SIZE |
private static int |
LOG_RECORD_TRAILER_SIZE |
private LogToFile |
logFactory |
private java.lang.Object |
logFileSemaphore |
private FormatIdOutputStream |
logicalOut |
private ArrayOutputStream |
logOutputBuffer |
(package private) MasterFactory |
masterFac |
(package private) static int |
mon_numBytesToLog |
(package private) static int |
mon_numWritesToLog |
private boolean |
writeChecksum |
Constructor and Description |
---|
LogAccessFile(LogToFile logFactory,
StorageRandomAccessFile log,
int bufferSize) |
Modifier and Type | Method and Description |
---|---|
private int |
appendLogRecordToBuffer(byte[] buff,
int pos,
int length,
long instant,
byte[] data,
int data_offset,
byte[] optional_data,
int optional_data_offset,
int optional_data_length)
Append a log record to a byte[].
|
void |
close() |
void |
corrupt()
The database is being marked corrupted, get rid of file pointer without
writing out anything more.
|
protected void |
flushDirtyBuffers()
Write data from all dirty buffers into the log file.
|
void |
flushLogAccessFile() |
int |
getChecksumLogRecordSize()
Return the length of a checksum record
|
protected long |
reserveSpaceForChecksum(int length,
long logFileNumber,
long currentPosition)
reserve the space for the checksum log record in the log file.
|
protected void |
setReplicationMasterRole(MasterFactory masterFac)
Make this LogAccessFile pass chunks of log records (byte[]) to
the MasterFactory when the chunks are written to disk.
|
protected void |
setReplicationSlaveRole()
Method to put this LogAccessFile object in replication slave
mode, effectively disabling checksum writes.
|
protected void |
stopReplicationMasterRole()
Stop this LogAccessFile from passing chunks of log records to
the MasterFactory.
|
void |
switchLogBuffer()
Appends the current Buffer to the dirty Buffer list and assigns a free
buffer to be the currrent active buffer .
|
void |
syncLogAccessFile()
Guarantee all writes up to the last call to flushLogAccessFile on disk.
|
void |
write(byte[] b,
int off,
int len) |
void |
write(int b) |
private void |
writeChecksumLogRecord(byte[] buffer)
Generate the checkum log record and write it into the log
buffer.
|
protected void |
writeEndMarker(int marker) |
void |
writeInt(int i) |
private int |
writeInt(int i,
byte[] b,
int p) |
void |
writeLogRecord(int length,
long instant,
byte[] data,
int data_offset,
byte[] optional_data,
int optional_data_offset,
int optional_data_length)
Write a single log record to the stream.
|
void |
writeLong(long l) |
private int |
writeLong(long l,
byte[] b,
int p) |
private void |
writeToLog(byte[] b,
int off,
int len,
long highestInstant) |
private static final int LOG_RECORD_FIXED_OVERHEAD_SIZE
private static final int LOG_RECORD_HEADER_SIZE
private static final int LOG_RECORD_TRAILER_SIZE
private static final int LOG_NUMBER_LOG_BUFFERS
private java.util.LinkedList<LogAccessFileBuffer> freeBuffers
private java.util.LinkedList<LogAccessFileBuffer> dirtyBuffers
private LogAccessFileBuffer currentBuffer
private boolean flushInProgress
private final StorageRandomAccessFile log
private final java.lang.Object logFileSemaphore
static int mon_numWritesToLog
static int mon_numBytesToLog
MasterFactory masterFac
boolean inReplicationMasterMode
boolean inReplicationSlaveMode
private ArrayOutputStream logOutputBuffer
private FormatIdOutputStream logicalOut
private long checksumInstant
private int checksumLength
private int checksumLogRecordSize
private boolean writeChecksum
private ChecksumOperation checksumLogOperation
private LogRecord checksumLogRecord
private LogToFile logFactory
private boolean databaseEncrypted
public LogAccessFile(LogToFile logFactory, StorageRandomAccessFile log, int bufferSize)
public void writeLogRecord(int length, long instant, byte[] data, int data_offset, byte[] optional_data, int optional_data_offset, int optional_data_length) throws StandardException, java.io.IOException
For performance pass all parameters rather into a specialized routine rather than maintaining the writeInt, writeLong, and write interfaces that this class provides as a standard OutputStream. It will make it harder to use other OutputStream implementations, but makes for less function calls and allows optimizations knowing when to switch buffers.
This routine handles all log records which are smaller than one log buffer. If a log record is bigger than a log buffer it calls writeUnbufferedLogRecord().
The log record written will always look the same as if the following code had been executed: writeInt(length) writeLong(instant) write(data, data_offset, (length - optional_data_length) ) if (optional_data_length != 0) write(optional_data, optional_data_offset, optional_data_length) writeInt(length)
length
- (data + optional_data) length bytes to writeinstant
- the log address of this log record.data
- "from" array to copy "data" portion of recdata_offset
- offset in "data" to start copying from.optional_data
- "from" array to copy "optional data" fromoptional_data_offset
- offset in "optional_data" to start copy fromoptional_data_length
- length of optional data to copy.StandardException
- Standard exception policy.java.io.IOException
private int appendLogRecordToBuffer(byte[] buff, int pos, int length, long instant, byte[] data, int data_offset, byte[] optional_data, int optional_data_offset, int optional_data_length)
buff
- The byte[] the log record is appended topos
- The position in buff where the method will start to
append tolength
- (data + optional_data) length bytes to writeinstant
- the log address of this log record.data
- "from" array to copy "data" portion of recdata_offset
- offset in "data" to start copying from.optional_data
- "from" array to copy "optional data" fromoptional_data_offset
- offset in "optional_data" to start copy fromoptional_data_length
- length of optional data to copy.writeLogRecord(int, long, byte[], int, byte[], int, int)
private final int writeInt(int i, byte[] b, int p)
private final int writeLong(long l, byte[] b, int p)
public void writeInt(int i)
public void writeLong(long l)
public void write(int b)
public void write(byte[] b, int off, int len)
protected void flushDirtyBuffers() throws java.io.IOException
A call for clients of LogAccessFile to insure that all privately buffered data has been writen to the file - so that reads on the file using one of the various scan classes will see all the data which has been writen to this point.
Note that this routine only "writes" the data to the file, this does not mean that the data has been synced to disk unless file was opened in WRITE SYNC mode(rws/rwd). The only way to insure that is by calling is to call syncLogAccessFile() after this call in Non-WRITE sync mode(rw)
MT-Safe : parallel thereads can call this function, only one threads does the flush and the other threads waits for the one that is doing the flush to finish. Currently there are two possible threads that can call this function in parallel 1) A Thread that is doing the commit 2) A Thread that is writing to the log and log buffers are full or a log records does not fit in a buffer. (Log Buffers full(switchLogBuffer() or a log record size that is greater than logbuffer size has to be writtern through writeToLog call directlty) Note: writeToLog() is not synchronized on the semaphore that is used to do buffer management to allow writes to the free buffers when flush is in progress.
java.io.IOException
public void flushLogAccessFile() throws java.io.IOException, StandardException
java.io.IOException
StandardException
public void switchLogBuffer() throws java.io.IOException, StandardException
java.io.IOException
StandardException
public void syncLogAccessFile() throws java.io.IOException, StandardException
A call for clients of LogAccessFile to insure that all data written up to the last call to flushLogAccessFile() are written to disk. This call will not return until those writes have hit disk.
Note that this routine may block waiting for I/O to complete so callers should limit the number of resource held locked while this operation is called. It is expected that the caller Note that this routine only "writes" the data to the file, this does not mean that the data has been synced to disk. The only way to insure that is to first call switchLogBuffer() and then follow by a call of sync().
java.io.IOException
StandardException
public void corrupt() throws java.io.IOException
java.io.IOException
public void close() throws java.io.IOException, StandardException
java.io.IOException
StandardException
protected void setReplicationMasterRole(MasterFactory masterFac)
masterFac
- The MasterFactory service responsible for
controlling the master side replication behaviour.protected void stopReplicationMasterRole()
protected void setReplicationSlaveRole()
private void writeToLog(byte[] b, int off, int len, long highestInstant) throws java.io.IOException
java.io.IOException
protected long reserveSpaceForChecksum(int length, long logFileNumber, long currentPosition) throws StandardException, java.io.IOException
length
- the length of the log record to be writtenlogFileNumber
- current log file numbercurrentPosition
- current position in the log file.StandardException
java.io.IOException
private void writeChecksumLogRecord(byte[] buffer) throws java.io.IOException, StandardException
buffer
- The byte[] the checksum is written to. The
checksum is always written at the beginning of buffer.java.io.IOException
StandardException
public int getChecksumLogRecordSize()
protected void writeEndMarker(int marker) throws java.io.IOException, StandardException
java.io.IOException
StandardException
Apache Derby V10.13 Internals - Copyright © 2004,2016 The Apache Software Foundation. All Rights Reserved.