Class TransactionTable

  • All Implemented Interfaces:
    java.io.Externalizable, java.io.Serializable, Formatable, TypedFormat

    public class TransactionTable
    extends java.lang.Object
    implements Formatable
    The transaction table is used by the transaction factory to keep track of all transactions that are in the system.
    The transction table serves the following purposes:
    1. checkpoint - when a checkpoint log record is written out, it writes out also all transactions that have updated the database. RESOLVE: this is actually not used right now - rather, the transaction table is reconstructed during the redo phase by traversing from the undo LWM. It is a goal to use this transaction table (and traversing from the redoLWM) instead of rebuilding it to speed up recovery.
    2. Quiesce State - when a system enters the quiesce state, it needs to account for all transactions in the system, even those which are just started and are in their IDLE state.
    3. TransactionTable VTI - we need to get a snapshot of all transactions in the system for diagnostic purposes.
    In order to speed up the time it takes to look up a transaction from the transaction table, each transaction must have a unique transaction Id. This means newly coined transaction must also have a transaction Id.

    During recovery, there is only one real xact object doing all the recovery work, but there could be many outstanding transactions that are gleamed from the log. Each of these "recovery transactions" have its on entry into the transaction table but they all share the same Xact object.

    Multithreading considerations:
    TransactionTable must be MT-safe it is called upon by many threads simultaneously (except during recovery)

    Methods that are only called during recovery don't need to take MT considerations, and can safely use iterators with no additional synchronization.

    See Also:
    Serialized Form
    • Constructor Detail

      • TransactionTable

        public TransactionTable()
        MT - not needed for constructor
    • Method Detail

      • findTransactionEntry

        private TransactionTableEntry findTransactionEntry​(TransactionId id)
        generic methods called by all clients of transaction table Must be MT -safe
      • visitEntries

        void visitEntries​(TransactionTable.EntryVisitor visitor)

        Visit all the entries in the transaction table.

        MT - MT safe

        Entries that are added to or removed from the transaction table while it's being traversed, may or may not be visited. All the entries that are present in the map when this method is called, and have not been removed when the method returns, will have been visited exactly once (except if the visit() method returns false before all entries have been visited, in which case the traversal of the map will stop earlier).

        Note however that this method does not guarantee that a single TransactionTableEntry is not accessed concurrently by multiple threads. If the visitor accesses some of the entry's mutable state, the caller must ensure that appropriate synchronization protection is in place. For example, if accessing the update state of the entry, the caller must synchronize on "this" (the TransactionTable instance).

        Parameters:
        visitor - the visitor to apply on each transaction table entry
      • add

        void add​(Xact xact,
                 boolean exclude)
      • remove

        boolean remove​(TransactionId id)
        remove the transaction Id an return false iff the transaction is found in the table and it doesn't need exclusion during quiesce state
      • addUpdateTransaction

        public void addUpdateTransaction​(TransactionId tid,
                                         RawTransaction tran,
                                         int transactionStatus)
        Change a transaction to update or add an update transaction to this table.
        Parameters:
        tid - the transaction id
        tran - the transaction to be added
        transactionStatus - the transaction status that is stored in the BeginXact log record
      • removeUpdateTransaction

        void removeUpdateTransaction​(TransactionId id)
        Change update transaction to non-update

        MT - MT safe, since vector is MT-safe.

        Parameters:
        id - the transaction Id
      • prepareTransaction

        void prepareTransaction​(TransactionId id)
        Change transaction to prepared.

        MT - unsafe, caller is recovery, which is single threaded.

        Parameters:
        id - the transaction Id
      • findTransactionContextByGlobalId

        public ContextManager findTransactionContextByGlobalId​(GlobalXactId global_id)
        Find a transaction in the table by Global transaction id.

        This routine use to be only called during offline recovery so performance was not critical. Since that time more calls have been made, including one in startGlobalTransaction() so a linear search may no longer be appropriate. See DERBY-828.

        Parameters:
        global_id - The global transaction we are searching for.
        Returns:
        The ContextManager of the transaction being searched for.
      • hasActiveUpdateTransaction

        boolean hasActiveUpdateTransaction()
        Return true if there is no transaction actively updating the database. New transaction may be started or old transaction committed right afterward, the caller of this routine must have other ways to stop transactions from starting or ending.

        MT - safe

      • getTypeFormatId

        public int getTypeFormatId()
        Return my format identifier.
        Specified by:
        getTypeFormatId in interface TypedFormat
        Returns:
        The identifier. (A UUID stuffed in an array of 16 bytes).
      • writeExternal

        public void writeExternal​(java.io.ObjectOutput out)
                           throws java.io.IOException
        Specified by:
        writeExternal in interface java.io.Externalizable
        Throws:
        java.io.IOException - problem reading the transaction table
      • readExternal

        public void readExternal​(java.io.ObjectInput in)
                          throws java.io.IOException,
                                 java.lang.ClassNotFoundException
        Specified by:
        readExternal in interface java.io.Externalizable
        Throws:
        java.io.IOException - problem reading the transaction table
        java.lang.ClassNotFoundException - problem reading the transaction table
      • largestUpdateXactId

        public TransactionId largestUpdateXactId()
        Return the largest update transactionId I have seen so far.

        MT - unsafe, caller is recovery, which is single threaded.

      • hasRollbackFirstTransaction

        public boolean hasRollbackFirstTransaction()
        Is there an active internal transaction in the transaction table.

        MT - unsafe, caller is recovery, which is single threaded.

      • hasPreparedRecoveredXact

        public boolean hasPreparedRecoveredXact()
        Is there a prepared transaction that are recovered durring the recovery in the transaction table.

        MT - unsafe, caller is recovery, which is single threaded.

      • hasPreparedXact

        public boolean hasPreparedXact()
        Is there a prepared transaction in the transaction table.

        MT - unsafe, called during boot, which is single threaded.

      • hasPreparedXact

        private boolean hasPreparedXact​(boolean recovered)
        Is there a prepared transaction in the transaction table.

        MT - unsafe, caller is recovery/at boot, which is single threaded.

        Parameters:
        recovered - true to search for transaction that are in prepared during recovery. recovered tranaction. false > to search for just prepared transactons.
        Returns:
        true if there is a prepared transaction and recovered when recovered argument is true
      • getMostRecentRollbackFirstTransaction

        public boolean getMostRecentRollbackFirstTransaction​(RawTransaction tran)
        Get the most recently added transaction that says it needs to be rolled back first (an InternalXact) from the transaction table and make the passed in transaction assume its identity. Should only be used in recovery undo !! RESOLVE: (sku)I don't think even these internal transactions need to be rolled back in the reverse order, because they are physical in nature. But it won't hurt.

        MT - unsafe, caller is recovery, which is single threaded.

      • getMostRecentTransactionForRollback

        public boolean getMostRecentTransactionForRollback​(RawTransaction tran)
        Get the most recently non-prepared added transaction from the transaction table and make the passed in transaction assume its identity. Prepared transactions will not be undone. RESOLVE: (sku) I don't think normal user transactions needs to be rolled back in order, but it won't hurt. Should only be used in recovery undo !!

        MT - unsafe, caller is recovery, which is single threaded.

      • getMostRecentPreparedRecoveredXact

        public boolean getMostRecentPreparedRecoveredXact​(RawTransaction tran)
        Get the most recent recovered prepared transaction.

        Get the most recently added transaction that says it is prepared during recovery the transaction table and make the passed in transaction assume its identity.

        This routine, unlike the redo and rollback getMostRecent*() routines expects a brand new transaction to be passed in. If a candidate transaction is found, then upon return the transaction table will be altered such that the old entry no longer exists, and a new entry will exist pointing to the transaction passed in. The new entry will look the same as if the prepared transaction had been created during runtime rather than recovery. Should only be used in recovery handle prepare after undo !!

        MT - unsafe, caller is recovery, which is single threaded.

        Parameters:
        tran - Newly allocated transaction to add to link to a entry.
        Returns:
        true if a candidate transaction has been found. false if no prepared/recovery transactions found in the table.
      • getFirstLogInstant

        public LogInstant getFirstLogInstant()
        Get the least recently added (oldest) transaction
        Returns:
        the RawTransaction's first log instant

        MT - safe, caller can be recovery or checkpoint

      • findAndAssumeTransaction

        boolean findAndAssumeTransaction​(TransactionId id,
                                         RawTransaction tran)
        Find a transaction using the transaction id, and make the passed in transaction assume the identity and properties of that transaction.

        MT - unsafe, caller is recovery, which is single threaded.

        Parameters:
        id - transaction Id
        tran - the transaction that was made to assume the transactionID and all other relevant information stored in the transaction table
        Returns:
        true if transaction can be found, false otherwise
      • getTransactionInfo

        public TransactionInfo[] getTransactionInfo()
        Get a printable version of the transaction table
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object