Class FileTxnLog

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, TxnLog

    public class FileTxnLog
    extends java.lang.Object
    implements TxnLog, java.io.Closeable
    This class implements the TxnLog interface. It provides api's to access the txnlogs and add entries to it.

    The format of a Transactional log is as follows:

     LogFile:
         FileHeader TxnList ZeroPad
    
     FileHeader: {
         magic 4bytes (ZKLG)
         version 4bytes
         dbid 8bytes
       }
    
     TxnList:
         Txn || Txn TxnList
    
     Txn:
         checksum Txnlen TxnHeader Record 0x42
    
     checksum: 8bytes Adler32 is currently used
       calculated across payload -- Txnlen, TxnHeader, Record and 0x42
    
     Txnlen:
         len 4bytes
    
     TxnHeader: {
         sessionid 8bytes
         cxid 4bytes
         zxid 8bytes
         time 8bytes
         type 4bytes
       }
    
     Record:
         See Jute definition file for details on the various record types
    
     ZeroPad:
         0 padded to EOF (filled during preallocation stage)
     
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  FileTxnLog.FileTxnIterator
      this class implements the txnlog iterator interface which is used for reading the transaction logs
    • Constructor Summary

      Constructors 
      Constructor Description
      FileTxnLog​(java.io.File logDir)
      constructor for FileTxnLog.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean append​(TxnHeader hdr, Record txn)
      append an entry to the transaction log
      boolean append​(TxnHeader hdr, Record txn, TxnDigest digest)
      Append a request to the transaction log with a digset
      void close()
      close all the open file handles
      void commit()
      commit the logs.
      long getCurrentLogSize()
      Return the current on-disk size of log size.
      long getDbId()
      the dbid of this transaction database
      long getLastLoggedZxid()
      get the last zxid that was logged in the transaction logs
      static java.io.File[] getLogFiles​(java.io.File[] logDirList, long snapshotZxid)
      Find the log file that starts at, or just before, the snapshot.
      long getTotalLogSize()
      Gets the total size of all log files
      long getTxnLogSyncElapsedTime()  
      boolean isForceSync()
      the forceSync value.
      protected java.util.zip.Checksum makeChecksumAlgorithm()
      creates a checksum algorithm to be used
      TxnLog.TxnIterator read​(long zxid)
      start reading all the transactions from the given zxid
      TxnLog.TxnIterator read​(long zxid, boolean fastForward)
      start reading all the transactions from the given zxid.
      void rollLog()
      rollover the current log file to a new one.
      static void setPreallocSize​(long size)
      method to allow setting preallocate size of log file to pad the file.
      void setServerStats​(ServerStats serverStats)
      Setter for ServerStats to monitor fsync threshold exceed
      void setTotalLogSize​(long size)
      Sets the total size of all log files
      static void setTxnLogSizeLimit​(long size)
      Set log size limit
      boolean truncate​(long zxid)
      truncate the current transaction logs
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • TXNLOG_MAGIC

        public static final int TXNLOG_MAGIC
    • Constructor Detail

      • FileTxnLog

        public FileTxnLog​(java.io.File logDir)
        constructor for FileTxnLog. Take the directory where the txnlogs are stored
        Parameters:
        logDir - the directory where the txnlogs are stored
    • Method Detail

      • setPreallocSize

        public static void setPreallocSize​(long size)
        method to allow setting preallocate size of log file to pad the file.
        Parameters:
        size - the size to set to in bytes
      • setServerStats

        public void setServerStats​(ServerStats serverStats)
        Setter for ServerStats to monitor fsync threshold exceed
        Specified by:
        setServerStats in interface TxnLog
        Parameters:
        serverStats - used to update fsyncThresholdExceedCount
      • setTxnLogSizeLimit

        public static void setTxnLogSizeLimit​(long size)
        Set log size limit
      • getCurrentLogSize

        public long getCurrentLogSize()
        Return the current on-disk size of log size. This will be accurate only after commit() is called. Otherwise, unflushed txns may not be included.
      • setTotalLogSize

        public void setTotalLogSize​(long size)
        Description copied from interface: TxnLog
        Sets the total size of all log files
        Specified by:
        setTotalLogSize in interface TxnLog
      • getTotalLogSize

        public long getTotalLogSize()
        Description copied from interface: TxnLog
        Gets the total size of all log files
        Specified by:
        getTotalLogSize in interface TxnLog
      • makeChecksumAlgorithm

        protected java.util.zip.Checksum makeChecksumAlgorithm()
        creates a checksum algorithm to be used
        Returns:
        the checksum used for this txnlog
      • rollLog

        public void rollLog()
                     throws java.io.IOException
        rollover the current log file to a new one.
        Specified by:
        rollLog in interface TxnLog
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        close all the open file handles
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in interface TxnLog
        Throws:
        java.io.IOException
      • append

        public boolean append​(TxnHeader hdr,
                              Record txn)
                       throws java.io.IOException
        append an entry to the transaction log
        Specified by:
        append in interface TxnLog
        Parameters:
        hdr - the header of the transaction
        txn - the transaction part of the entry returns true iff something appended, otw false
        Returns:
        true iff something appended, otw false
        Throws:
        java.io.IOException
      • append

        public boolean append​(TxnHeader hdr,
                              Record txn,
                              TxnDigest digest)
                       throws java.io.IOException
        Description copied from interface: TxnLog
        Append a request to the transaction log with a digset
        Specified by:
        append in interface TxnLog
        Parameters:
        hdr - the transaction header
        txn - the transaction itself
        digest - transaction digest returns true iff something appended, otw false
        Throws:
        java.io.IOException
      • getLogFiles

        public static java.io.File[] getLogFiles​(java.io.File[] logDirList,
                                                 long snapshotZxid)
        Find the log file that starts at, or just before, the snapshot. Return this and all subsequent logs. Results are ordered by zxid of file, ascending order.
        Parameters:
        logDirList - array of files
        snapshotZxid - return files at, or before this zxid
        Returns:
        log files that starts at, or just before, the snapshot and subsequent ones
      • getLastLoggedZxid

        public long getLastLoggedZxid()
        get the last zxid that was logged in the transaction logs
        Specified by:
        getLastLoggedZxid in interface TxnLog
        Returns:
        the last zxid logged in the transaction logs
      • commit

        public void commit()
                    throws java.io.IOException
        commit the logs. make sure that everything hits the disk
        Specified by:
        commit in interface TxnLog
        Throws:
        java.io.IOException
      • getTxnLogSyncElapsedTime

        public long getTxnLogSyncElapsedTime()
        Specified by:
        getTxnLogSyncElapsedTime in interface TxnLog
        Returns:
        elapsed sync time of transaction log in milliseconds
      • read

        public TxnLog.TxnIterator read​(long zxid)
                                throws java.io.IOException
        start reading all the transactions from the given zxid
        Specified by:
        read in interface TxnLog
        Parameters:
        zxid - the zxid to start reading transactions from
        Returns:
        returns an iterator to iterate through the transaction logs
        Throws:
        java.io.IOException
      • read

        public TxnLog.TxnIterator read​(long zxid,
                                       boolean fastForward)
                                throws java.io.IOException
        start reading all the transactions from the given zxid.
        Parameters:
        zxid - the zxid to start reading transactions from
        fastForward - true if the iterator should be fast forwarded to point to the txn of a given zxid, else the iterator will point to the starting txn of a txnlog that may contain txn of a given zxid
        Returns:
        returns an iterator to iterate through the transaction logs
        Throws:
        java.io.IOException
      • truncate

        public boolean truncate​(long zxid)
                         throws java.io.IOException
        truncate the current transaction logs
        Specified by:
        truncate in interface TxnLog
        Parameters:
        zxid - the zxid to truncate the logs to
        Returns:
        true if successful false if not
        Throws:
        java.io.IOException
      • getDbId

        public long getDbId()
                     throws java.io.IOException
        the dbid of this transaction database
        Specified by:
        getDbId in interface TxnLog
        Returns:
        the dbid of this database
        Throws:
        java.io.IOException
      • isForceSync

        public boolean isForceSync()
        the forceSync value. true if forceSync is enabled, false otherwise.
        Returns:
        the forceSync value