org.jboss.messaging.core.journal.impl
Class JournalImpl

java.lang.Object
  extended by org.jboss.messaging.core.journal.impl.JournalImpl
All Implemented Interfaces:
Journal, TestableJournal, MessagingComponent

public class JournalImpl
extends java.lang.Object
implements TestableJournal

A JournalImpl

WIKI Page: http://wiki.jboss.org/wiki/JBossMessaging2Journal

Look at load(LoaderCallback) for the file layout

Author:
Tim Fox, Clebert Suconic

Nested Class Summary
static class JournalImpl.ByteArrayEncoding
           
static class JournalImpl.JournalRecord
          This holds the relationship a record has with other files in regard to reference counting.
 
Field Summary
static byte ADD_RECORD
           
static byte ADD_RECORD_TX
           
static int BASIC_SIZE
           
static byte COMMIT_RECORD
           
static byte DELETE_RECORD
           
static byte DELETE_RECORD_TX
           
 java.lang.String fileExtension
           
 java.lang.String filePrefix
           
static byte FILL_CHARACTER
           
static int MIN_FILE_SIZE
           
static byte PREPARE_RECORD
           
static byte ROLLBACK_RECORD
           
static int SIZE_ADD_RECORD
           
static int SIZE_ADD_RECORD_TX
           
static int SIZE_COMMIT_RECORD
           
static int SIZE_COMPLETE_TRANSACTION_RECORD
           
static int SIZE_DELETE_RECORD
           
static int SIZE_DELETE_RECORD_TX
           
static int SIZE_HEADER
           
static int SIZE_PREPARE_RECORD
           
static int SIZE_ROLLBACK_RECORD
           
static byte SIZE_UPDATE_RECORD
           
static int SIZE_UPDATE_RECORD_TX
           
static byte UPDATE_RECORD
           
static byte UPDATE_RECORD_TX
           
 
Constructor Summary
JournalImpl(int fileSize, int minFiles, int compactMinFiles, int compactPercentage, SequentialFileFactory fileFactory, java.lang.String filePrefix, java.lang.String fileExtension, int maxAIO)
           
 
Method Summary
 void appendAddRecord(long id, byte recordType, byte[] record, boolean sync)
           
 void appendAddRecord(long id, byte recordType, EncodingSupport record, boolean sync)
           
 void appendAddRecordTransactional(long txID, long id, byte recordType, byte[] record)
           
 void appendAddRecordTransactional(long txID, long id, byte recordType, EncodingSupport record)
           
 void appendCommitRecord(long txID, boolean sync)
          A transaction record (Commit or Prepare), will hold the number of elements the transaction has on each file.
 void appendDeleteRecord(long id, boolean sync)
           
 void appendDeleteRecordTransactional(long txID, long id)
           
 void appendDeleteRecordTransactional(long txID, long id, byte[] record)
           
 void appendDeleteRecordTransactional(long txID, long id, EncodingSupport record)
           
 void appendPrepareRecord(long txID, EncodingSupport transactionData, boolean sync)
          If the system crashed after a prepare was called, it should store information that is required to bring the transaction back to a state it could be committed.
 void appendRollbackRecord(long txID, boolean sync)
           
 void appendUpdateRecord(long id, byte recordType, byte[] record, boolean sync)
           
 void appendUpdateRecord(long id, byte recordType, EncodingSupport record, boolean sync)
           
 void appendUpdateRecordTransactional(long txID, long id, byte recordType, byte[] record)
           
 void appendUpdateRecordTransactional(long txID, long id, byte recordType, EncodingSupport record)
           
 void checkAndReclaimFiles()
          This method is called automatically when a new file is opened
 void checkCompact()
          This method check for the need of compacting based on the minCompactPercentage This method is usually called automatically when new files are opened
 void compact()
          Note: This method can't be called from the main executor, as it will invoke other methods depending on it.
protected  SequentialFile createControlFile(java.util.List<JournalFile> files, java.util.List<JournalFile> newFiles)
           
 java.lang.String debug()
           
 void debugWait()
          Method for use on testcases.
protected  void deleteControlFile(SequentialFile controlFile)
           
 void forceMoveNextFile()
           
 int getAlignment()
           
 JournalCompactor getCompactor()
           
 JournalFile getCurrentFile()
           
 JournalFile[] getDataFiles()
           
 int getDataFilesCount()
           
 java.lang.String getFileExtension()
           
 java.lang.String getFilePrefix()
           
 int getFileSize()
           
 int getFreeFilesCount()
           
 int getIDMapSize()
           
 int getMaxAIO()
           
 int getMinFiles()
           
 int getOpenedFilesCount()
           
 java.util.Map<java.lang.Long,JournalImpl.JournalRecord> getRecords()
           
 boolean isAutoReclaim()
           
 boolean isStarted()
           
 long load(java.util.List<RecordInfo> committedRecords, java.util.List<PreparedTransactionInfo> preparedTransactions)
           
 long load(LoaderCallback loadManager)
          Load data accordingly to the record layouts
protected  void onCompactDone()
          This is an interception point for testcases, when the compacted files are written, before replacing the data structures
 void perfBlast(int pages)
           
static int readJournalFile(SequentialFileFactory fileFactory, JournalFile file, JournalReaderCallback reader)
           
protected  void renameFiles(java.util.List<JournalFile> oldFiles, java.util.List<JournalFile> newFiles)
          being protected as testcases can override this method
 void setAutoReclaim(boolean autoReclaim)
           
 void start()
           
 void stop()
           
static void writeAddRecord(int fileId, long id, byte recordType, EncodingSupport record, int size, ChannelBuffer bb)
           
static void writeAddRecordTX(int fileID, long txID, long id, byte recordType, EncodingSupport record, int size, ChannelBuffer bb)
           
static void writeDeleteRecordTransactional(int fileID, long txID, long id, EncodingSupport record, int size, ChannelBuffer bb)
           
static void writeTransaction(int fileID, byte recordType, long txID, JournalTransaction tx, EncodingSupport transactionData, int size, int numberOfRecords, ChannelBuffer bb)
          A transaction record (Commit or Prepare), will hold the number of elements the transaction has on each file.
static void writeUpdateRecord(int fileId, long id, byte recordType, EncodingSupport record, int size, ChannelBuffer bb)
           
static void writeUpdateRecordTX(int fileID, long txID, long id, byte recordType, EncodingSupport record, int size, ChannelBuffer bb)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MIN_FILE_SIZE

public static final int MIN_FILE_SIZE
See Also:
Constant Field Values

SIZE_HEADER

public static final int SIZE_HEADER
See Also:
Constant Field Values

BASIC_SIZE

public static final int BASIC_SIZE
See Also:
Constant Field Values

SIZE_ADD_RECORD

public static final int SIZE_ADD_RECORD
See Also:
Constant Field Values

ADD_RECORD

public static final byte ADD_RECORD
See Also:
Constant Field Values

SIZE_UPDATE_RECORD

public static final byte SIZE_UPDATE_RECORD
See Also:
Constant Field Values

UPDATE_RECORD

public static final byte UPDATE_RECORD
See Also:
Constant Field Values

SIZE_ADD_RECORD_TX

public static final int SIZE_ADD_RECORD_TX
See Also:
Constant Field Values

ADD_RECORD_TX

public static final byte ADD_RECORD_TX
See Also:
Constant Field Values

SIZE_UPDATE_RECORD_TX

public static final int SIZE_UPDATE_RECORD_TX
See Also:
Constant Field Values

UPDATE_RECORD_TX

public static final byte UPDATE_RECORD_TX
See Also:
Constant Field Values

SIZE_DELETE_RECORD_TX

public static final int SIZE_DELETE_RECORD_TX
See Also:
Constant Field Values

DELETE_RECORD_TX

public static final byte DELETE_RECORD_TX
See Also:
Constant Field Values

SIZE_DELETE_RECORD

public static final int SIZE_DELETE_RECORD
See Also:
Constant Field Values

DELETE_RECORD

public static final byte DELETE_RECORD
See Also:
Constant Field Values

SIZE_COMPLETE_TRANSACTION_RECORD

public static final int SIZE_COMPLETE_TRANSACTION_RECORD
See Also:
Constant Field Values

SIZE_PREPARE_RECORD

public static final int SIZE_PREPARE_RECORD
See Also:
Constant Field Values

PREPARE_RECORD

public static final byte PREPARE_RECORD
See Also:
Constant Field Values

SIZE_COMMIT_RECORD

public static final int SIZE_COMMIT_RECORD
See Also:
Constant Field Values

COMMIT_RECORD

public static final byte COMMIT_RECORD
See Also:
Constant Field Values

SIZE_ROLLBACK_RECORD

public static final int SIZE_ROLLBACK_RECORD
See Also:
Constant Field Values

ROLLBACK_RECORD

public static final byte ROLLBACK_RECORD
See Also:
Constant Field Values

FILL_CHARACTER

public static final byte FILL_CHARACTER
See Also:
Constant Field Values

filePrefix

public final java.lang.String filePrefix

fileExtension

public final java.lang.String fileExtension
Constructor Detail

JournalImpl

public JournalImpl(int fileSize,
                   int minFiles,
                   int compactMinFiles,
                   int compactPercentage,
                   SequentialFileFactory fileFactory,
                   java.lang.String filePrefix,
                   java.lang.String fileExtension,
                   int maxAIO)
Method Detail

writeTransaction

public static void writeTransaction(int fileID,
                                    byte recordType,
                                    long txID,
                                    JournalTransaction tx,
                                    EncodingSupport transactionData,
                                    int size,
                                    int numberOfRecords,
                                    ChannelBuffer bb)
                             throws java.lang.Exception

A transaction record (Commit or Prepare), will hold the number of elements the transaction has on each file.

For example, a transaction was spread along 3 journal files with 10 pendingTransactions on each file. (What could happen if there are too many pendingTransactions, or if an user event delayed pendingTransactions to come in time to a single file).

The element-summary will then have

FileID1, 10

FileID2, 10

FileID3, 10


During the load, the transaction needs to have 30 pendingTransactions spread across the files as originally written.

If for any reason there are missing pendingTransactions, that means the transaction was not completed and we should ignore the whole transaction

We can't just use a global counter as reclaiming could delete files after the transaction was successfully committed. That also means not having a whole file on journal-reload doesn't mean we have to invalidate the transaction

Parameters:
recordType -
txID -
tx -
transactionData -
Throws:
java.lang.Exception

writeUpdateRecordTX

public static void writeUpdateRecordTX(int fileID,
                                       long txID,
                                       long id,
                                       byte recordType,
                                       EncodingSupport record,
                                       int size,
                                       ChannelBuffer bb)
Parameters:
txID -
id -
recordType -
record -
size -
bb -

writeUpdateRecord

public static void writeUpdateRecord(int fileId,
                                     long id,
                                     byte recordType,
                                     EncodingSupport record,
                                     int size,
                                     ChannelBuffer bb)
Parameters:
id -
recordType -
record -
size -
bb -

writeAddRecord

public static void writeAddRecord(int fileId,
                                  long id,
                                  byte recordType,
                                  EncodingSupport record,
                                  int size,
                                  ChannelBuffer bb)
Parameters:
id -
recordType -
record -
size -
bb -

writeDeleteRecordTransactional

public static void writeDeleteRecordTransactional(int fileID,
                                                  long txID,
                                                  long id,
                                                  EncodingSupport record,
                                                  int size,
                                                  ChannelBuffer bb)
Parameters:
txID -
id -
record -
size -
bb -

writeAddRecordTX

public static void writeAddRecordTX(int fileID,
                                    long txID,
                                    long id,
                                    byte recordType,
                                    EncodingSupport record,
                                    int size,
                                    ChannelBuffer bb)
Parameters:
txID -
id -
recordType -
record -
recordLength -
size -
bb -

getRecords

public java.util.Map<java.lang.Long,JournalImpl.JournalRecord> getRecords()

getCurrentFile

public JournalFile getCurrentFile()

getCompactor

public JournalCompactor getCompactor()

readJournalFile

public static int readJournalFile(SequentialFileFactory fileFactory,
                                  JournalFile file,
                                  JournalReaderCallback reader)
                           throws java.lang.Exception
Throws:
java.lang.Exception

appendAddRecord

public void appendAddRecord(long id,
                            byte recordType,
                            byte[] record,
                            boolean sync)
                     throws java.lang.Exception
Specified by:
appendAddRecord in interface Journal
Throws:
java.lang.Exception

appendAddRecord

public void appendAddRecord(long id,
                            byte recordType,
                            EncodingSupport record,
                            boolean sync)
                     throws java.lang.Exception
Specified by:
appendAddRecord in interface Journal
Throws:
java.lang.Exception

appendUpdateRecord

public void appendUpdateRecord(long id,
                               byte recordType,
                               byte[] record,
                               boolean sync)
                        throws java.lang.Exception
Specified by:
appendUpdateRecord in interface Journal
Throws:
java.lang.Exception

appendUpdateRecord

public void appendUpdateRecord(long id,
                               byte recordType,
                               EncodingSupport record,
                               boolean sync)
                        throws java.lang.Exception
Specified by:
appendUpdateRecord in interface Journal
Throws:
java.lang.Exception

appendDeleteRecord

public void appendDeleteRecord(long id,
                               boolean sync)
                        throws java.lang.Exception
Specified by:
appendDeleteRecord in interface Journal
Throws:
java.lang.Exception

appendAddRecordTransactional

public void appendAddRecordTransactional(long txID,
                                         long id,
                                         byte recordType,
                                         byte[] record)
                                  throws java.lang.Exception
Specified by:
appendAddRecordTransactional in interface Journal
Throws:
java.lang.Exception

appendAddRecordTransactional

public void appendAddRecordTransactional(long txID,
                                         long id,
                                         byte recordType,
                                         EncodingSupport record)
                                  throws java.lang.Exception
Specified by:
appendAddRecordTransactional in interface Journal
Throws:
java.lang.Exception

appendUpdateRecordTransactional

public void appendUpdateRecordTransactional(long txID,
                                            long id,
                                            byte recordType,
                                            byte[] record)
                                     throws java.lang.Exception
Specified by:
appendUpdateRecordTransactional in interface Journal
Throws:
java.lang.Exception

appendUpdateRecordTransactional

public void appendUpdateRecordTransactional(long txID,
                                            long id,
                                            byte recordType,
                                            EncodingSupport record)
                                     throws java.lang.Exception
Specified by:
appendUpdateRecordTransactional in interface Journal
Throws:
java.lang.Exception

appendDeleteRecordTransactional

public void appendDeleteRecordTransactional(long txID,
                                            long id,
                                            byte[] record)
                                     throws java.lang.Exception
Specified by:
appendDeleteRecordTransactional in interface Journal
Throws:
java.lang.Exception

appendDeleteRecordTransactional

public void appendDeleteRecordTransactional(long txID,
                                            long id,
                                            EncodingSupport record)
                                     throws java.lang.Exception
Specified by:
appendDeleteRecordTransactional in interface Journal
Throws:
java.lang.Exception

appendDeleteRecordTransactional

public void appendDeleteRecordTransactional(long txID,
                                            long id)
                                     throws java.lang.Exception
Specified by:
appendDeleteRecordTransactional in interface Journal
Throws:
java.lang.Exception

appendPrepareRecord

public void appendPrepareRecord(long txID,
                                EncodingSupport transactionData,
                                boolean sync)
                         throws java.lang.Exception

If the system crashed after a prepare was called, it should store information that is required to bring the transaction back to a state it could be committed.

transactionData allows you to store any other supporting user-data related to the transaction

This method also uses the same logic applied on JournalImpl#appendCommitRecord(long)

Specified by:
appendPrepareRecord in interface Journal
Parameters:
txID -
transactionData - - extra user data for the prepare
Throws:
java.lang.Exception

appendCommitRecord

public void appendCommitRecord(long txID,
                               boolean sync)
                        throws java.lang.Exception

A transaction record (Commit or Prepare), will hold the number of elements the transaction has on each file.

For example, a transaction was spread along 3 journal files with 10 pendingTransactions on each file. (What could happen if there are too many pendingTransactions, or if an user event delayed pendingTransactions to come in time to a single file).

The element-summary will then have

FileID1, 10

FileID2, 10

FileID3, 10


During the load, the transaction needs to have 30 pendingTransactions spread across the files as originally written.

If for any reason there are missing pendingTransactions, that means the transaction was not completed and we should ignore the whole transaction

We can't just use a global counter as reclaiming could delete files after the transaction was successfully committed. That also means not having a whole file on journal-reload doesn't mean we have to invalidate the transaction

Specified by:
appendCommitRecord in interface Journal
Throws:
java.lang.Exception
See Also:
JournalImpl#writeTransaction(byte, long, org.jboss.messaging.core.journal.impl.JournalImpl.JournalTransaction, EncodingSupport)

appendRollbackRecord

public void appendRollbackRecord(long txID,
                                 boolean sync)
                          throws java.lang.Exception
Specified by:
appendRollbackRecord in interface Journal
Throws:
java.lang.Exception

getAlignment

public int getAlignment()
                 throws java.lang.Exception
Specified by:
getAlignment in interface Journal
Throws:
java.lang.Exception

load

public long load(java.util.List<RecordInfo> committedRecords,
                 java.util.List<PreparedTransactionInfo> preparedTransactions)
          throws java.lang.Exception
Specified by:
load in interface Journal
Throws:
java.lang.Exception
See Also:
load(LoaderCallback)

compact

public void compact()
             throws java.lang.Exception
Note: This method can't be called from the main executor, as it will invoke other methods depending on it.

Specified by:
compact in interface Journal
Throws:
java.lang.Exception

load

public long load(LoaderCallback loadManager)
          throws java.lang.Exception

Load data accordingly to the record layouts

Basic record layout:

Field NameSize
RecordTypeByte (1)
FileIDInteger (4 bytes)
TransactionID (if record is transactional)Long (8 bytes)
RecordIDLong (8 bytes)
BodySize(Add, update and delete)Integer (4 bytes)
UserDefinedRecordType (If add/update only)Byte (1)
RecordBodyByte Array (size=BodySize)
Check SizeInteger (4 bytes)

The check-size is used to validate if the record is valid and complete

Commit/Prepare record layout:

Field NameSize
RecordTypeByte (1)
FileIDInteger (4 bytes)
TransactionID (if record is transactional)Long (8 bytes)
ExtraDataLength (Prepares only)Integer (4 bytes)
Number Of Files (N)Integer (4 bytes)
ExtraDataBytesBytes (sized by ExtraDataLength)
* FileID(n)Integer (4 bytes)
* NumberOfElements(n)Integer (4 bytes)
CheckSizeInteger (4 bytes)

* FileID and NumberOfElements are the transaction summary, and they will be repeated (N)umberOfFiles times

Specified by:
load in interface TestableJournal
Throws:
java.lang.Exception

checkAndReclaimFiles

public void checkAndReclaimFiles()
                          throws java.lang.Exception
Description copied from interface: Journal
This method is called automatically when a new file is opened

Specified by:
checkAndReclaimFiles in interface Journal
Throws:
java.lang.Exception

checkCompact

public void checkCompact()
                  throws java.lang.Exception
Description copied from interface: Journal
This method check for the need of compacting based on the minCompactPercentage This method is usually called automatically when new files are opened

Specified by:
checkCompact in interface Journal
Throws:
java.lang.Exception

setAutoReclaim

public void setAutoReclaim(boolean autoReclaim)
Specified by:
setAutoReclaim in interface TestableJournal

isAutoReclaim

public boolean isAutoReclaim()
Specified by:
isAutoReclaim in interface TestableJournal

debug

public java.lang.String debug()
                       throws java.lang.Exception
Specified by:
debug in interface TestableJournal
Throws:
java.lang.Exception

debugWait

public void debugWait()
               throws java.lang.Exception
Method for use on testcases. It will call waitComplete on every transaction, so any assertions on the file system will be correct after this

Specified by:
debugWait in interface TestableJournal
Throws:
java.lang.Exception

getDataFilesCount

public int getDataFilesCount()
Specified by:
getDataFilesCount in interface TestableJournal

getDataFiles

public JournalFile[] getDataFiles()
Specified by:
getDataFiles in interface Journal

getFreeFilesCount

public int getFreeFilesCount()
Specified by:
getFreeFilesCount in interface TestableJournal

getOpenedFilesCount

public int getOpenedFilesCount()
Specified by:
getOpenedFilesCount in interface TestableJournal

getIDMapSize

public int getIDMapSize()
Specified by:
getIDMapSize in interface TestableJournal

getFileSize

public int getFileSize()
Specified by:
getFileSize in interface TestableJournal

getMinFiles

public int getMinFiles()
Specified by:
getMinFiles in interface TestableJournal

getFilePrefix

public java.lang.String getFilePrefix()
Specified by:
getFilePrefix in interface TestableJournal

getFileExtension

public java.lang.String getFileExtension()
Specified by:
getFileExtension in interface TestableJournal

getMaxAIO

public int getMaxAIO()
Specified by:
getMaxAIO in interface TestableJournal

forceMoveNextFile

public void forceMoveNextFile()
                       throws java.lang.Exception
Specified by:
forceMoveNextFile in interface TestableJournal
Throws:
java.lang.Exception

perfBlast

public void perfBlast(int pages)
               throws java.lang.Exception
Specified by:
perfBlast in interface Journal
Throws:
java.lang.Exception

isStarted

public boolean isStarted()
Specified by:
isStarted in interface MessagingComponent

start

public void start()
Specified by:
start in interface MessagingComponent

stop

public void stop()
          throws java.lang.Exception
Specified by:
stop in interface MessagingComponent
Throws:
java.lang.Exception

deleteControlFile

protected void deleteControlFile(SequentialFile controlFile)
                          throws java.lang.Exception
Throws:
java.lang.Exception

renameFiles

protected void renameFiles(java.util.List<JournalFile> oldFiles,
                           java.util.List<JournalFile> newFiles)
                    throws java.lang.Exception
being protected as testcases can override this method

Throws:
java.lang.Exception

onCompactDone

protected void onCompactDone()
This is an interception point for testcases, when the compacted files are written, before replacing the data structures


createControlFile

protected SequentialFile createControlFile(java.util.List<JournalFile> files,
                                           java.util.List<JournalFile> newFiles)
                                    throws java.lang.Exception
Throws:
java.lang.Exception


Copyright © 2006 JBoss Inc. All Rights Reserved.