Class ProducerPoolImpl
- java.lang.Object
-
- org.apache.flink.connector.kafka.sink.internal.ProducerPoolImpl
-
- All Implemented Interfaces:
AutoCloseable,ProducerPool
@NotThreadSafe @Internal public class ProducerPoolImpl extends Object implements ProducerPool
Manages a pool ofFlinkKafkaInternalProducerinstances for reuse in theKafkaWriterand keeps track of the used transactional ids.Reusing the producers is important for performance reasons. The producer initialization includes a few requests to the broker (e.g., ApiVersion), which can be avoided with reuse.
Tracking the transactional ids in use can be a tricky because the
KafkaCommitteris ultimately finishing the transactions. There are two major cases:- The committer is chained to the writer (common case): The
KafkaCommittablecontains the producer (in-memory transfer) and the producer is only returned to the producer pool upon completion by the committer. Thus, none of the producers in the pool have active transactions. - The committer is not chained: The
KafkaCommittableSerializerwill return the producer to this pool, but it still has an ongoing transaction. The producer will be "cloned" in the committer by using producer id and epoch. In this case, we rely onProducerFencedExceptionto test later if a producer in the pool is still in the transaction or not.
This pool is not thread-safe and is only intended to be accessed from the writer, which owns it.
-
-
Constructor Summary
Constructors Constructor Description ProducerPoolImpl(Properties kafkaProducerConfig, Consumer<FlinkKafkaInternalProducer<byte[],byte[]>> producerInit, Collection<CheckpointTransaction> precommittedTransactions)Creates a newProducerPoolImpl.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Collection<CheckpointTransaction>getOngoingTransactions()Returns a snapshot of all ongoing transactions.Collection<FlinkKafkaInternalProducer<byte[],byte[]>>getProducers()FlinkKafkaInternalProducer<byte[],byte[]>getTransactionalProducer(String transactionalId, long checkpointId)Get a producer for the given transactional id and checkpoint id.voidrecycle(FlinkKafkaInternalProducer<byte[],byte[]> producer)Explicitly recycle a producer.voidrecycleByTransactionId(String transactionalId, boolean success)Notify the pool that a transaction has finished.
-
-
-
Constructor Detail
-
ProducerPoolImpl
public ProducerPoolImpl(Properties kafkaProducerConfig, Consumer<FlinkKafkaInternalProducer<byte[],byte[]>> producerInit, Collection<CheckpointTransaction> precommittedTransactions)
Creates a newProducerPoolImpl.
-
-
Method Detail
-
recycleByTransactionId
public void recycleByTransactionId(String transactionalId, boolean success)
Description copied from interface:ProducerPoolNotify the pool that a transaction has finished. The producer with the given transactional id can be recycled.- Specified by:
recycleByTransactionIdin interfaceProducerPool
-
recycle
public void recycle(FlinkKafkaInternalProducer<byte[],byte[]> producer)
Description copied from interface:ProducerPoolExplicitly recycle a producer. This is useful when the producer has not been passed to the committer.- Specified by:
recyclein interfaceProducerPool
-
getTransactionalProducer
public FlinkKafkaInternalProducer<byte[],byte[]> getTransactionalProducer(String transactionalId, long checkpointId)
Description copied from interface:ProducerPoolGet a producer for the given transactional id and checkpoint id. The producer is not recycled until it is passed to the committer, the committer commits the transaction, andProducerPool.recycleByTransactionId(String, boolean)is called. Alternatively, the producer can be recycled byProducerPool.recycle(FlinkKafkaInternalProducer).- Specified by:
getTransactionalProducerin interfaceProducerPool
-
getOngoingTransactions
public Collection<CheckpointTransaction> getOngoingTransactions()
Description copied from interface:ProducerPoolReturns a snapshot of all ongoing transactions.- Specified by:
getOngoingTransactionsin interfaceProducerPool
-
getProducers
@VisibleForTesting public Collection<FlinkKafkaInternalProducer<byte[],byte[]>> getProducers()
-
close
public void close() throws Exception- Specified by:
closein interfaceAutoCloseable- Throws:
Exception
-
-