public class LogRecord extends Object
A log is a sequence of log records. Each log record is a sequence of bytes.
Log records are written sequentially into a stream, and will be assigned with
an unique system generated sequence number DLSN (distributedlog sequence
number). Besides DLSN, application can assign its own sequence number
while constructing log records. The application defined sequence number is called
TransactionID (txid). Either DLSN or TransactionId
could be used to position readers to start from specific log records.
LogRecord(long, byte[]) by applications and appended to
logs by writers. And they would be deserialized from bytes by the readers and return
to applications.
They are named as 'Control Records' for controlling visibility of application written records.
The transaction id of 'Control Records' are assigned by distributedlog by inheriting from last written user records. So we could indicate what user records that a control record is committing by looking at its transaction id.
EoS(EndOfStream) is a special control record that would be written by a writer
to seal a log. After a EoS record is written to a log, no writers could append any record
after that and readers will get EndOfStreamException
when they reach EoS.
TransactionID of EoS is Long.MAX_VALUE.
LogRecord structure:
-------------------
Bytes 0 - 7 : Metadata (Long)
Bytes 8 - 15 : TxId (Long)
Bytes 16 - 19 : Payload length (Integer)
Bytes 20 - 20+payload.length-1 : Payload (Byte[])
Metadata: 8 Bytes (Long)
--------
0x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
|_____________| |_____|
| |
position flags
Flags: 2 Bytes (least significant)
-----
Bit 0 : If set, control record, else record with payload.
Bit 1 : If set, end of stream.
Bits 2 - 15 : Unused
LogRecordWithDLSN for more details.LogRecordWithDLSN| Modifier and Type | Class and Description |
|---|---|
static class |
LogRecord.Reader
This class is a package private class for reading log records
from the persistent
|
static class |
LogRecord.Writer
Class for writing log records
|
| Modifier and Type | Field and Description |
|---|---|
static int |
MAX_LOGRECORD_SIZE |
static int |
MAX_LOGRECORDSET_SIZE |
| Modifier | Constructor and Description |
|---|---|
protected |
LogRecord()
Construct an uninitialized log record.
|
|
LogRecord(long txid,
byte[] payload)
Construct a log record with TransactionId and payload.
|
| Modifier and Type | Method and Description |
|---|---|
protected long |
getMetadata() |
byte[] |
getPayload()
Return the payload of this log record.
|
InputStream |
getPayLoadInputStream()
Return the payload as an
InputStream. |
int |
getPositionWithinLogSegment()
The position in the log segment means how many records (inclusive) added to the log segment so far.
|
long |
getTransactionId()
Return application defined transaction id.
|
boolean |
isControl()
Check if the record is a control record.
|
static boolean |
isControl(long flags)
Check flags to see if it indicates a control record.
|
boolean |
isRecordSet()
Check if the record represents a set of records.
|
static boolean |
isRecordSet(long metadata) |
protected void |
readPayload(DataInputStream in) |
void |
setControl() |
protected void |
setMetadata(long metadata) |
void |
setRecordSet()
Set the record to represent a set of records.
|
protected void |
setTransactionId(long txid)
Set application defined transaction id.
|
public static final int MAX_LOGRECORD_SIZE
public static final int MAX_LOGRECORDSET_SIZE
protected LogRecord()
NOTE: only deserializer should call this constructor.
public LogRecord(long txid,
byte[] payload)
Usually writer would construct the log record for writing.
txid - application defined transaction id.payload - record datapublic long getTransactionId()
protected void setTransactionId(long txid)
txid - application defined transaction id.public byte[] getPayload()
public InputStream getPayLoadInputStream()
InputStream.protected void setMetadata(long metadata)
protected long getMetadata()
public int getPositionWithinLogSegment()
public void setRecordSet()
The bytes in this record is the serialized format of LogRecordSet.
public boolean isRecordSet()
setRecordSet()public static boolean isRecordSet(long metadata)
public void setControl()
public boolean isControl()
public static boolean isControl(long flags)
flags - record flagsprotected void readPayload(DataInputStream in) throws IOException
IOExceptionCopyright © 2016. All Rights Reserved.