|
25 | 25 | import org.apache.kafka.clients.consumer.ConsumerRecords; |
26 | 26 | import org.apache.kafka.clients.consumer.KafkaConsumer; |
27 | 27 | import org.apache.kafka.clients.consumer.OffsetAndMetadata; |
| 28 | +import org.apache.kafka.clients.consumer.OffsetCommitCallback; |
28 | 29 | import org.apache.kafka.clients.producer.internals.BufferPool; |
29 | 30 | import org.apache.kafka.clients.producer.internals.BuiltInPartitioner; |
30 | 31 | import org.apache.kafka.clients.producer.internals.KafkaProducerMetrics; |
@@ -659,34 +660,33 @@ public void initTransactions() { |
659 | 660 | } |
660 | 661 |
|
661 | 662 | /** |
662 | | - * Performs initialization of transactions functionality in this producer instance. This method bootstraps |
663 | | - * the producer with a {@code producerId} and also resets the internal state of the producer following a previous |
664 | | - * fatal error. Additionally, it allows setting the {@code keepPreparedTxn} flag which, if set to true, puts the producer |
665 | | - * into a restricted state that only allows transaction completion operations. |
666 | | - * |
| 663 | + * Initialize the transactional state for this producer, similar to {@link #initTransactions()} but |
| 664 | + * with additional handling for two-phase commit (2PC). Must be called before any send operations |
| 665 | + * that require a {@code transactionalId}. |
667 | 666 | * <p> |
668 | | - * When {@code keepPreparedTxn} is set to {@code true}, the producer will be able to complete in-flight prepared |
669 | | - * transactions, but will only allow calling {@link #commitTransaction()}, {@link #abortTransaction()}, or |
670 | | - * the to-be-added {@code completeTransaction()} methods. This is to support recovery of prepared transactions |
671 | | - * after a producer restart. |
672 | | - * |
| 667 | + * Unlike the standard {@link #initTransactions()}, when {@code keepPreparedTxn} is set to |
| 668 | + * {@code true}, the producer does <em>not</em> automatically abort existing transactions |
| 669 | + * in the “prepare” phase. Instead, it enters a recovery mode allowing only finalization |
| 670 | + * of those previously prepared transactions. This behavior is crucial for 2PC scenarios, |
| 671 | + * where transactions should remain intact until the external transaction manager decides |
| 672 | + * whether to commit or abort. |
673 | 673 | * <p> |
674 | | - * Note that this method should only be called once during the lifetime of a producer instance, and must be |
675 | | - * called before any other methods which require a {@code transactionalId} to be specified. |
| 674 | + * When {@code keepPreparedTxn} is {@code false}, this behaves like the normal transactional |
| 675 | + * initialization, aborting any unfinished transactions and resetting the producer for |
| 676 | + * new writes. |
676 | 677 | * |
677 | | - * @param keepPreparedTxn whether to keep prepared transactions, restricting the producer to only support completion of |
678 | | - * prepared transactions. When set to true, the producer will only allow transaction completion |
679 | | - * operations after initialization. |
| 678 | + * @param keepPreparedTxn true to retain any in-flight prepared transactions (necessary for 2PC |
| 679 | + * recovery), false to abort existing transactions and behave like |
| 680 | + * the standard initTransactions |
680 | 681 | * |
681 | | - * @throws IllegalStateException if no {@code transactional.id} has been configured for the producer |
682 | | - * @throws org.apache.kafka.common.errors.UnsupportedVersionException fatal error indicating that the broker |
683 | | - * does not support transactions (i.e. if its version is lower than 0.11.0.0). If this is encountered, |
684 | | - * the producer cannot be used for transactional messaging. |
685 | | - * @throws org.apache.kafka.common.errors.AuthorizationException fatal error indicating that the configured |
686 | | - * {@code transactional.id} is not authorized. If this is encountered, the producer cannot be used for |
687 | | - * transactional messaging. |
688 | | - * @throws KafkaException if the producer has encountered a previous fatal error or for any other unexpected error |
689 | | - * @see #initTransactions() |
| 682 | + * @throws IllegalStateException if no {@code transactional.id} is configured |
| 683 | + * @throws org.apache.kafka.common.errors.UnsupportedVersionException if the broker does not |
| 684 | + * support transactions (broker version < 0.11.0.0) |
| 685 | + * @throws org.apache.kafka.common.errors.TransactionalIdAuthorizationException if the configured |
| 686 | + * {@code transactional.id} is unauthorized either for normal transaction writes or 2PC. |
| 687 | + * @throws KafkaException if the producer encounters a fatal error or any other unexpected error |
| 688 | + * @throws TimeoutException if the time taken for initialize the transaction has surpassed <code>max.block.ms</code>. |
| 689 | + * @throws InterruptException if the thread is interrupted while blocked |
690 | 690 | */ |
691 | 691 | public void initTransactions(boolean keepPreparedTxn) { |
692 | 692 | throwIfNoTransactionManager(); |
@@ -746,7 +746,7 @@ public void beginTransaction() throws ProducerFencedException { |
746 | 746 | * <p> |
747 | 747 | * Note, that the consumer should have {@code enable.auto.commit=false} and should |
748 | 748 | * also not commit offsets manually (via {@link KafkaConsumer#commitSync(Map) sync} or |
749 | | - * {@link KafkaConsumer#commitAsync()} (Map, OffsetCommitCallback) async} commits). |
| 749 | + * {@link KafkaConsumer#commitAsync(Map, OffsetCommitCallback) async} commits). |
750 | 750 | * This method will raise {@link TimeoutException} if the producer cannot send offsets before expiration of {@code max.block.ms}. |
751 | 751 | * Additionally, it will raise {@link InterruptException} if interrupted. |
752 | 752 | * |
|
0 commit comments