Class ProducerBatch
java.lang.Object
org.apache.kafka.clients.producer.internals.ProducerBatch
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 Summary
ConstructorsConstructorDescriptionProducerBatch(org.apache.kafka.common.TopicPartition tp, org.apache.kafka.common.record.MemoryRecordsBuilder recordsBuilder, long createdMs) ProducerBatch(org.apache.kafka.common.TopicPartition tp, org.apache.kafka.common.record.MemoryRecordsBuilder recordsBuilder, long createdMs, boolean isSplitBatch) -
Method Summary
Modifier and TypeMethodDescriptionvoidabort(RuntimeException exception) Abort the batch and complete the future and callbacks.voidAbort the record builder and reset the state of the underlying buffer.intbuffer()voidclose()voidRelease resources required for record appends (e.g.booleancomplete(long baseOffset, long logAppendTime) Complete the batch successfully.booleancompleteExceptionally(RuntimeException topLevelException, Function<Integer, RuntimeException> recordExceptions) Complete the batch exceptionally.doubleintorg.apache.kafka.clients.producer.internals.ProducerBatch.FinalStatebooleanintbooleaninRetry()Returns if the batch is been retried for sending to kafkabooleanisClosed()booleanbooleanisDone()Check if the batch has been completed (either successfully or exceptionally).booleanisFull()booleanbooleanintbytemagic()shortlongorg.apache.kafka.common.record.MemoryRecordsrecords()voidresetProducerState(org.apache.kafka.common.utils.ProducerIdAndEpoch producerIdAndEpoch, int baseSequence) booleanvoidsetProducerState(org.apache.kafka.common.utils.ProducerIdAndEpoch producerIdAndEpoch, int baseSequence, boolean isTransactional) split(int splitBatchSize) toString()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
-
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
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 serverlogAppendTime- 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 errorrecordExceptions- 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
-
isCompressed
public boolean isCompressed() -
toString
-
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 withabort(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 inRecordAccumulator). -
isClosed
public boolean isClosed() -
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()
-