Class ReplicationLogBuffer

java.lang.Object
org.apache.derby.impl.store.replication.buffer.ReplicationLogBuffer

public class ReplicationLogBuffer extends Object
Used for the replication master role only. When a Derby instance has the replication master role for a database 'x', all log records that are written to the local log file are also appended to this log buffer. The replication master service will consume chunks of log from this buffer and send it to the Derby instance with the slave role for 'x'. ReplicationLogBuffer consists of a number of LogBufferElements. Elements that are not in use are in the freeBuffers list, while elements that contains dirty log are in dirtyBuffers. Chunks of log records are appended to the buffer element in currentDirtyBuffer. Hence, the life cycle of buffer elements is: freeBuffers -> currentDirtyBuffer -> dirtyBuffers -> freeBuffers To append chunks of log records to the buffer, use appendLog(...) To consume chunks of log records, use next() followed by getData(), getLastInstant() and getSize(). These get-methods throw NoSuchElementException if next() returned false, meaning that there were no dirty log at the time next() was called. Threads: ReplicationLogBuffer is threadsafe. It can be used by a logger (LogToFile) and a log consumer (LogShipping service) concurrently without further synchronization. Important: If methods in this class calls methods outside this package (e.g. MasterFactory#workToDo), make sure that deadlocks are not introduced. If possible, a call to any method in another package should be done without holding latches in this class.
  • Field Details

    • DEFAULT_NUMBER_LOG_BUFFERS

      public static final int DEFAULT_NUMBER_LOG_BUFFERS
      See Also:
    • dirtyBuffers

      private final LinkedList<LogBufferElement> dirtyBuffers
    • freeBuffers

      private final LinkedList<LogBufferElement> freeBuffers
    • currentDirtyBuffer

      private LogBufferElement currentDirtyBuffer
    • validOutBuffer

      private boolean validOutBuffer
    • outBufferData

      private byte[] outBufferData
    • outBufferStored

      private int outBufferStored
    • outBufferLastInstant

      private long outBufferLastInstant
    • listLatch

      private final Object listLatch
    • outputLatch

      private final Object outputLatch
    • defaultBufferSize

      private int defaultBufferSize
    • mf

      private final MasterFactory mf
  • Constructor Details

    • ReplicationLogBuffer

      public ReplicationLogBuffer(int bufferSize, MasterFactory mf)
      Class constructor specifies the number of buffer elements and the master controller that creates this replication log buffer.
      Parameters:
      bufferSize - the default number of buffer elements
      mf - Used to notify the master controller that a log buffer element is full and work needs to be done.
  • Method Details

    • appendLog

      public void appendLog(long greatestInstant, byte[] log, int logOffset, int logLength) throws LogBufferFullException
      Append a chunk of log records to the log buffer.
      Parameters:
      greatestInstant - the instant of the log record that was added last to this chunk of log
      log - the chunk of log records
      logOffset - offset in log to start copy from
      logLength - number of bytes to copy, starting from logOffset
      Throws:
      LogBufferFullException - - thrown if there is not enough free space in the buffer to store the chunk of log.
    • next

      public boolean next()
      Sets the output data to that of the next (oldest) buffer element in dirtyBuffers so that getData(), getLastInstant() and getSize() return values from the next oldest chunk of log. Used by the log consumer (the LogShipping service) to move to the next chunk of log in the buffer.
      Returns:
      true if there is log in the buffer, resulting in valid data for the get-methods
    • getData

      public byte[] getData() throws NoSuchElementException
      Returns a byte[] containing a chunk of serialized log records. Always returns the log that was oldest at the time next() was called last time. Use next() to move to the next chunk of log records.
      Returns:
      A copy of the current byte[], which is a chunk of log
      Throws:
      NoSuchElementException - if there was no log in the buffer the last time next() was called.
    • validData

      public boolean validData()
      Method to determine whether or not the buffer had any log records the last time next() was called.
      Returns:
      true if the buffer contained log records the last time next() was called. False if not, or if next() has not been called yet.
    • getSize

      public int getSize() throws NoSuchElementException
      Returns:
      The number of bytes returned by getData
      Throws:
      NoSuchElementException - if there was no log in the buffer the last time next() was called.
    • getLastInstant

      public long getLastInstant() throws NoSuchElementException
      Can be used so that only the necessary log records are sent when a flush(LogInstant flush_to_this) is called in the log factory. Returns the highest log instant in the chunk of log that can be read with getData().
      Returns:
      The highest log instant in the chunk of log returned by getData().
      Throws:
      NoSuchElementException - if there was no log in the buffer the last time next() was called.
    • switchDirtyBuffer

      private void switchDirtyBuffer() throws LogBufferFullException
      Appends the currentDirtyBuffer to dirtyBuffers, and makes a fresh buffer element from freeBuffers the currentDirtyBuffer. Note: this method is not synchronized since all uses of it is inside synchronized(listLatch) code blocks.
      Throws:
      LogBufferFullException - if the freeBuffers list is empty
    • getFillInformation

      public int getFillInformation()
      Used to calculate the Fill Information. The fill information is a indicator of how full the buffer is at any point of time fill information = (full buffers/Total Buffers)*100. The Fill information ranges between 0-100 (both 0 and 100 inclusive).
      Returns:
      an integer value between 0-100 representing the fill information.