Class ChangeEventQueue<T extends Sizeable>

java.lang.Object
io.debezium.connector.base.ChangeEventQueue<T>
Type Parameters:
T - the type of events in this queue. Usually Sizeable is used, but in cases where additional metadata must be passed from producers to the consumer, a custom type extending source records may be used.
All Implemented Interfaces:
ChangeEventQueueMetrics

@ThreadSafe public class ChangeEventQueue<T extends Sizeable> extends Object implements ChangeEventQueueMetrics
A queue which serves as handover point between producer threads (e.g. MySQL's binlog reader thread) and the Kafka Connect polling loop.

The queue is configurable in different aspects, e.g. its maximum size and the time to sleep (block) between two subsequent poll calls. See the ChangeEventQueue.Builder for the different options. The queue applies back-pressure semantics, i.e. if it holds the maximum number of elements, subsequent calls to enqueue(T) will block until elements have been removed from the queue.

If an exception occurs on the producer side, the producer should make that exception known by calling producerException(RuntimeException) before stopping its operation. Upon the next call to poll(), that exception will be raised, causing Kafka Connect to stop the connector and mark it as FAILED.

Author:
Gunnar Morling
  • Field Details

    • LOGGER

      private static final org.slf4j.Logger LOGGER
    • pollInterval

      private final Duration pollInterval
    • maxBatchSize

      private final int maxBatchSize
    • maxQueueSize

      private final int maxQueueSize
    • maxQueueSizeInBytes

      private final long maxQueueSizeInBytes
    • lock

      private final Lock lock
    • isFull

      private final Condition isFull
    • isNotFull

      private final Condition isNotFull
    • queue

      private final Queue<T extends Sizeable> queue
    • loggingContextSupplier

      private final Supplier<LoggingContext.PreviousContext> loggingContextSupplier
    • sizeInBytesQueue

      private final Queue<Long> sizeInBytesQueue
    • currentQueueSizeInBytes

      private long currentQueueSizeInBytes
    • buffering

      private boolean buffering
    • bufferedEvent

      private final AtomicReference<T extends Sizeable> bufferedEvent
    • producerException

      private volatile RuntimeException producerException
  • Constructor Details

  • Method Details

    • enqueue

      public void enqueue(T record) throws InterruptedException
      Enqueues a record so that it can be obtained via poll(). This method will block if the queue is full.
      Parameters:
      record - the record to be enqueued
      Throws:
      InterruptedException - if this thread has been interrupted
    • flushBuffer

      public void flushBuffer(Function<T,T> recordModifier) throws InterruptedException
      Applies a function to the event and the buffer and adds it to the queue. Buffer is emptied.
      Parameters:
      recordModifier -
      Throws:
      InterruptedException
    • disableBuffering

      public void disableBuffering()
      Disable buffering for the queue
    • enableBuffering

      public void enableBuffering()
      Enable buffering for the queue
    • doEnqueue

      protected void doEnqueue(T record) throws InterruptedException
      Throws:
      InterruptedException
    • poll

      public List<T> poll() throws InterruptedException
      Returns the next batch of elements from this queue. May be empty in case no elements have arrived in the maximum waiting time.
      Throws:
      InterruptedException - if this thread has been interrupted while waiting for more elements to arrive
    • drainRecords

      private long drainRecords(List<T> records, int maxElements)
    • producerException

      public void producerException(RuntimeException producerException)
    • throwProducerExceptionIfPresent

      private void throwProducerExceptionIfPresent()
    • totalCapacity

      public int totalCapacity()
      Specified by:
      totalCapacity in interface ChangeEventQueueMetrics
    • remainingCapacity

      public int remainingCapacity()
      Specified by:
      remainingCapacity in interface ChangeEventQueueMetrics
    • maxQueueSizeInBytes

      public long maxQueueSizeInBytes()
      Specified by:
      maxQueueSizeInBytes in interface ChangeEventQueueMetrics
    • currentQueueSizeInBytes

      public long currentQueueSizeInBytes()
      Specified by:
      currentQueueSizeInBytes in interface ChangeEventQueueMetrics
    • isBuffered

      public boolean isBuffered()