Class ProducerBatch

java.lang.Object
org.apache.kafka.clients.producer.internals.ProducerBatch

public final class ProducerBatch extends Object
A batch of records that is or will be sent. This class is not thread safe and external synchronization must be used when modifying it
  • Constructor Details

    • ProducerBatch

      public ProducerBatch(org.apache.kafka.common.TopicPartition tp, org.apache.kafka.common.record.MemoryRecordsBuilder recordsBuilder, long createdMs)
    • ProducerBatch

      public ProducerBatch(org.apache.kafka.common.TopicPartition tp, org.apache.kafka.common.record.MemoryRecordsBuilder recordsBuilder, long createdMs, boolean isSplitBatch)
  • Method Details

    • tryAppend

      public FutureRecordMetadata tryAppend(long timestamp, byte[] key, byte[] value, org.apache.kafka.common.header.Header[] headers, Callback callback, long now)
      Append the record to the current record set and return the relative offset within that record set
      Returns:
      The RecordSend corresponding to this record or null if there isn't sufficient room.
    • abort

      public void abort(RuntimeException exception)
      Abort the batch and complete the future and callbacks.
      Parameters:
      exception - The exception to use to complete the future and awaiting callbacks.
    • isDone

      public boolean isDone()
      Check if the batch has been completed (either successfully or exceptionally).
      Returns:
      `true` if the batch has been completed, `false` otherwise.
    • complete

      public boolean complete(long baseOffset, long logAppendTime)
      Complete the batch successfully.
      Parameters:
      baseOffset - The base offset of the messages assigned by the server
      logAppendTime - The log append time or -1 if CreateTime is being used
      Returns:
      true if the batch was completed as a result of this call, and false if it had been completed previously
    • completeExceptionally

      public boolean completeExceptionally(RuntimeException topLevelException, Function<Integer,RuntimeException> recordExceptions)
      Complete the batch exceptionally. The provided top-level exception will be used for each record future contained in the batch.
      Parameters:
      topLevelException - top-level partition error
      recordExceptions - Record exception function mapping batchIndex to the respective record exception
      Returns:
      true if the batch was completed as a result of this call, and false if it had been completed previously
    • split

      public Deque<ProducerBatch> split(int splitBatchSize)
    • isCompressed

      public boolean isCompressed()
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • finalState

      public org.apache.kafka.clients.producer.internals.ProducerBatch.FinalState finalState()
    • inRetry

      public boolean inRetry()
      Returns if the batch is been retried for sending to kafka
    • records

      public org.apache.kafka.common.record.MemoryRecords records()
    • estimatedSizeInBytes

      public int estimatedSizeInBytes()
    • compressionRatio

      public double compressionRatio()
    • isFull

      public boolean isFull()
    • setProducerState

      public void setProducerState(org.apache.kafka.common.utils.ProducerIdAndEpoch producerIdAndEpoch, int baseSequence, boolean isTransactional)
    • resetProducerState

      public void resetProducerState(org.apache.kafka.common.utils.ProducerIdAndEpoch producerIdAndEpoch, int baseSequence)
    • closeForRecordAppends

      public void closeForRecordAppends()
      Release resources required for record appends (e.g. compression buffers). Once this method is called, it's only possible to update the RecordBatch header.
    • close

      public void close()
    • abortRecordAppends

      public void abortRecordAppends()
      Abort the record builder and reset the state of the underlying buffer. This is used prior to aborting the batch with abort(RuntimeException) and ensures that no record previously appended can be read. This is used in scenarios where we want to ensure a batch ultimately gets aborted, but in which it is not safe to invoke the completion callbacks (e.g. because we are holding a lock, such as when aborting batches in RecordAccumulator).
    • isClosed

      public boolean isClosed()
    • buffer

      public ByteBuffer buffer()
    • initialCapacity

      public int initialCapacity()
    • isWritable

      public boolean isWritable()
    • magic

      public byte magic()
    • producerId

      public long producerId()
    • producerEpoch

      public short producerEpoch()
    • baseSequence

      public int baseSequence()
    • lastSequence

      public int lastSequence()
    • hasSequence

      public boolean hasSequence()
    • isTransactional

      public boolean isTransactional()
    • sequenceHasBeenReset

      public boolean sequenceHasBeenReset()