Class ReplicationLogScan
Scan a chunk of log received from the master. The log chunk (byte[] logToScan) is assumed to always contain (an unknown) number of complete log records. If the last log record is incomplete, something is wrong. This will raise a StandardException, and will probably mean that replication has to be aborted. That decision is not made here, though.
When a new chunk of log records is to be scanned, ReplicationLogScan is initialized by calling init(...). Every time next() is called after that, ReplicationLogScan reads a new log record from logToScan. If next() returns true, the next log record was successfully read. The information in this last read log record either indicates that a log file switch has taken place on the master (isLogSwitch() = true) or it is a normal log record which information can be retrieved by using the get-methods (if isLogRecord() = true).
Threads: The class does not provide thread synchronization since it is assumed that only one thread will be receiving and applying log per replicated database when in slave mode. Since a ReplicationLogScan object belongs to only one slave database, this means that only one thread will access the object.
The format of the log chunk byte[] is defined in org.apache.derby.impl.store.raw.log.LogAccessFile
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate byte[]
private int
private long
private int
private boolean
hasInfo = true when the scan will return meaningful information, either in the form of a log record (in which case the above variables will be set), or when it has found a log record indicating that a log file switch has taken place on the master (isLogSwitch = true). hasInfo = false before next() is called for the first time, after next() has reached the end of logToScan and if an error occured when parsing logToScan (i.e.private boolean
true if the last read log record indicates a log switch, false if it is a normal log record private boolean isLogSwitch;private byte[]
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected byte[]
getData()
Method to get the data byte[] read by the last call to next().protected int
protected long
protected boolean
Used to determine whether or not the last call to next() was successful.protected void
init
(byte[] logToScan) Set all variables to default values, and makes logToScan the byte[] that will be scanned for log records.protected boolean
Used to determine whether the last call to next() found a log file switchprotected boolean
Used to determine whether the last call to next() read a log recordprotected boolean
next()
Read the next log record in logToScan.private void
retrieveBytes
(byte[] readInto, int length) Copy length number of bytes from logToScan into readInto.private int
Read an int from logToScan.private long
Read a long from logToScan.
-
Field Details
-
logToScan
private byte[] logToScan -
currentPosition
private int currentPosition -
currentInstant
private long currentInstant -
currentDataOffset
private int currentDataOffset -
currentData
private byte[] currentData -
hasInfo
private boolean hasInfohasInfo = true when the scan will return meaningful information, either in the form of a log record (in which case the above variables will be set), or when it has found a log record indicating that a log file switch has taken place on the master (isLogSwitch = true). hasInfo = false before next() is called for the first time, after next() has reached the end of logToScan and if an error occured when parsing logToScan (i.e. when next() has thrown a StandardException) -
isLogSwitch
private boolean isLogSwitchtrue if the last read log record indicates a log switch, false if it is a normal log record private boolean isLogSwitch;
-
-
Constructor Details
-
ReplicationLogScan
protected ReplicationLogScan()
-
-
Method Details
-
init
protected void init(byte[] logToScan) Set all variables to default values, and makes logToScan the byte[] that will be scanned for log records.- Parameters:
logToScan
- A chunk of log records received from the master
-
next
Read the next log record in logToScan. The information in this log record can be read by using the getXXX methods.
Side effects:
On a successful read (return true): either...
... the scan read a log record indicating that a log file switch has taken place on the master, in which case isLogFileSwitch() returns true. In this case, getXXX will not contain valid data. Asserts handle calls to these methods when in sane mode. currentPosition is updated to point to the byte immediately following this log file switch log record.
... or the scan read a normal log record, in which case isLogRecord() returns true. Also sets currentInstant and currentData, and updates currentPosition to point to the byte immediatly following the log record. In this case, getXXX will return meaningful information about the log record.If there are no more log records in logToScan (returns false) or a problem occurs (throws StandardException): setting hasInfo = false
- Returns:
- true if a log record was successfully read, false if end of logToScan has been reached.
- Throws:
StandardException
- if logToScan is found to be corrupted.
-
getInstant
- Returns:
- The instant of the log record read by the last call to next(). Only returns meaningful information if isLogRecord() returns true.
- Throws:
NoSuchElementException
- if next() has not been called or if there are no more log records in this chunk of log. Should never be thrown unless ReplicationLogScan is used in a wrong way.
-
getDataLength
- Returns:
- The number of bytes in the byte[] returned by getData() for the log record read by the last call to next(). Only returns meaningful information if isLogRecord() returns true.
- Throws:
NoSuchElementException
- if next() has not been called or if there are no more log records in this chunk of log. Should never be thrown unless ReplicationLogScan is used in a wrong way.
-
getData
Method to get the data byte[] read by the last call to next(). Note that this byte[] contains both byte[] data and byte[] optional_data from LogAccessFile. To split this byte[] into data and optional_data, we would need to create a Loggable object from it because the log does not provide information on where to split. There is no need to split since this byte[] will only be written to the slave log anyway. If it was split, LogAccessFile would simply merge them when writing to file.- Returns:
- The byte[] containing data+optional_data of the log record read by the last call to next(). Only returns meaningful information if isLogRecord() returns true.
- Throws:
NoSuchElementException
- if next() has not been called or if there are no more log records in this chunk of log. Should never be thrown unless ReplicationLogScan is used in a wrong way.
-
hasValidInformation
protected boolean hasValidInformation()Used to determine whether or not the last call to next() was successful.- Returns:
- true if next() has been called and the end of the log chunk has not yet been reached. Returns the same answer as the last call to next() did. Use isLogFileSwitch() and isLogRecord() to find out if the current log record indicates a log file switch or is a normal log record.
-
isLogRecord
Used to determine whether the last call to next() read a log record- Returns:
- true if the last call to next() found a normal log record.
- Throws:
NoSuchElementException
- if next() has not been called or if there are no more log records in this chunk of log. Should never be thrown unless ReplicationLogScan is used in a wrong way.
-
isLogFileSwitch
Used to determine whether the last call to next() found a log file switch- Returns:
- true if the last call to next() found a log record indicating a log file switch has taken place on the master.
- Throws:
NoSuchElementException
- if next() has not been called or if there are no more log records in this chunk of log. Should never be thrown unless ReplicationLogScan is used in a wrong way.
-
retrieveBytes
Copy length number of bytes from logToScan into readInto. Starts to copy from currentPosition. Also increments currentPosition by length.- Parameters:
readInto
- The byte[] copied tolength
- The number of bytes copied from logToScan to readInto- Throws:
StandardException
- if there are less then length bytes left to read in logToScan, meaning that the chunk of log is corrupt.
-
retrieveInt
Read an int from logToScan. Also increments currentPosition by 4 (the number of bytes in an int).- Returns:
- an int read from logToScan
- Throws:
StandardException
- if there are less then 4 bytes left to read in logToScan, meaning that the chunk of log is corrupt.
-
retrieveLong
Read a long from logToScan. Also increments currentPosition by 8 (the number of bytes in a long).- Returns:
- a long read from logToScan
- Throws:
StandardException
- if there are less then 8 bytes left to read in logToScan, meaning that the chunk of log is corrupt.
-