You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "rreddy-22 (via GitHub)" <gi...@apache.org> on 2025/04/09 23:21:05 UTC

[PR] KAFKA-19082: [1/3] Add client config for enable2PC and overloaded initProducerId (KIP-939) [kafka]

rreddy-22 opened a new pull request, #19429:
URL: https://github.com/apache/kafka/pull/19429

   This is part of the client side changes required to enable 2PC for KIP-939
   
   **Producer Config:**
   transaction.two.phase.commit.enable The default would be ‘false’.  If set to ‘true’, the broker is informed that the client is participating in two phase commit protocol and transactions that this client starts never expire.
   
   **Overloaded InitProducerId method**
   If the value is 'true' then the corresponding field is set in the InitProducerIdRequest and the KafkaProducer object is set into a state which only allows calling  .commitTransaction, .abortTransaction, or .completeTransaction.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-19082: [1/4] Add client config for enable2PC and overloaded initProducerId (KIP-939) [kafka]

Posted by "artemlivshits (via GitHub)" <gi...@apache.org>.
artemlivshits commented on code in PR #19429:
URL: https://github.com/apache/kafka/pull/19429#discussion_r2051049933


##########
clients/src/main/java/org/apache/kafka/clients/producer/internals/TransactionManager.java:
##########
@@ -273,17 +276,29 @@ public TransactionManager(final LogContext logContext,
         this.retryBackoffMs = retryBackoffMs;
         this.txnPartitionMap = new TxnPartitionMap(logContext);
         this.apiVersions = apiVersions;
+        this.enable2PC = enable2PC;
     }
 
     void setPoisonStateOnInvalidTransition(boolean shouldPoisonState) {
         shouldPoisonStateOnInvalidTransition.set(shouldPoisonState);
     }
 
     public synchronized TransactionalRequestResult initializeTransactions() {
-        return initializeTransactions(ProducerIdAndEpoch.NONE);
+        return initializeTransactions(ProducerIdAndEpoch.NONE, false);

Review Comment:
   For an internal class do we need an overload or we could just call `initializeTransactions(false)` directly?



##########
clients/src/main/java/org/apache/kafka/clients/producer/KafkaProducer.java:
##########
@@ -654,6 +657,46 @@ public void initTransactions() {
         transactionManager.maybeUpdateTransactionV2Enabled(true);
     }
 
+    /**
+     * Initialize the transactional state for this producer, similar to {@link #initTransactions()} but
+     * with additional handling for two-phase commit (2PC). Must be called before any send operations

Review Comment:
   The way it's defined in the KIP, using `keepPreparedTxn` isn't coupled with 2PC -- while it's essential for 2PC support, it can also be called when 2PC isn't used.



##########
clients/src/main/java/org/apache/kafka/clients/producer/KafkaProducer.java:
##########
@@ -654,6 +657,46 @@ public void initTransactions() {
         transactionManager.maybeUpdateTransactionV2Enabled(true);
     }
 
+    /**
+     * Initialize the transactional state for this producer, similar to {@link #initTransactions()} but
+     * with additional handling for two-phase commit (2PC). Must be called before any send operations
+     * that require a {@code transactionalId}.
+     * <p>
+     * Unlike the standard {@link #initTransactions()}, when {@code keepPreparedTxn} is set to
+     * {@code true}, the producer does <em>not</em> automatically abort existing transactions
+     * in the “prepare” phase. Instead, it enters a recovery mode allowing only finalization
+     * of those previously prepared transactions. This behavior is crucial for 2PC scenarios,
+     * where transactions should remain intact until the external transaction manager decides
+     * whether to commit or abort.
+     * <p>
+     * When {@code keepPreparedTxn} is {@code false}, this behaves like the normal transactional
+     * initialization, aborting any unfinished transactions and resetting the producer for
+     * new writes.
+     *
+     * @param keepPreparedTxn true to retain any in-flight prepared transactions (necessary for 2PC
+     *                        recovery), false to abort existing transactions and behave like
+     *                        the standard initTransactions
+     *
+     * @throws IllegalStateException if no {@code transactional.id} is configured
+     * @throws org.apache.kafka.common.errors.UnsupportedVersionException if the broker does not
+     *         support transactions (i.e. if its version is lower than 0.11.0.0)
+     * @throws org.apache.kafka.common.errors.TransactionalIdAuthorizationException if the configured
+     *         {@code transactional.id} is unauthorized either for normal transaction writes or 2PC.
+     * @throws KafkaException if the producer encounters a fatal error or any other unexpected error
+     * @throws TimeoutException if the time taken for initialize the transaction has surpassed <code>max.block.ms</code>.
+     * @throws InterruptException if the thread is interrupted while blocked
+     */
+    public void initTransactions(boolean keepPreparedTxn) {

Review Comment:
   Should we change `initTransactions()` to just call `initTransactions(false)`?  Maybe even implement it as a `default` method in the `Producer` interface, so that we don't need to implement it in `MockProducer`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-19082: [1/4] Add client config for enable2PC and overloaded initProducerId (KIP-939) [kafka]

Posted by "rreddy-22 (via GitHub)" <gi...@apache.org>.
rreddy-22 commented on code in PR #19429:
URL: https://github.com/apache/kafka/pull/19429#discussion_r2051195451


##########
clients/src/main/java/org/apache/kafka/clients/producer/internals/TransactionManager.java:
##########
@@ -273,17 +276,29 @@ public TransactionManager(final LogContext logContext,
         this.retryBackoffMs = retryBackoffMs;
         this.txnPartitionMap = new TxnPartitionMap(logContext);
         this.apiVersions = apiVersions;
+        this.enable2PC = enable2PC;
     }
 
     void setPoisonStateOnInvalidTransition(boolean shouldPoisonState) {
         shouldPoisonStateOnInvalidTransition.set(shouldPoisonState);
     }
 
     public synchronized TransactionalRequestResult initializeTransactions() {
-        return initializeTransactions(ProducerIdAndEpoch.NONE);
+        return initializeTransactions(ProducerIdAndEpoch.NONE, false);

Review Comment:
   Yes, I was also unsure about this. The main reason I decided to keep the overload was that all the existing tests currently use it. I figured anyone who doesn’t particularly care about keepPrepared shouldn’t have to deal with the additional argument. 
   That said, I know that there is another school of thought that is not to have surprise defaults and to make callers set parameters explicitly, which I agree with. 
   I just didn't know how much change we wanted to make to the existing internal method so lmk whichever works
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-19082: [1/4] Add client config for enable2PC and overloaded initProducerId (KIP-939) [kafka]

Posted by "rreddy-22 (via GitHub)" <gi...@apache.org>.
rreddy-22 commented on code in PR #19429:
URL: https://github.com/apache/kafka/pull/19429#discussion_r2053117512


##########
clients/src/main/java/org/apache/kafka/clients/producer/KafkaProducer.java:
##########
@@ -654,6 +657,46 @@ public void initTransactions() {
         transactionManager.maybeUpdateTransactionV2Enabled(true);
     }
 
+    /**
+     * Initialize the transactional state for this producer, similar to {@link #initTransactions()} but
+     * with additional handling for two-phase commit (2PC). Must be called before any send operations
+     * that require a {@code transactionalId}.
+     * <p>
+     * Unlike the standard {@link #initTransactions()}, when {@code keepPreparedTxn} is set to
+     * {@code true}, the producer does <em>not</em> automatically abort existing transactions
+     * in the “prepare” phase. Instead, it enters a recovery mode allowing only finalization
+     * of those previously prepared transactions. This behavior is crucial for 2PC scenarios,
+     * where transactions should remain intact until the external transaction manager decides
+     * whether to commit or abort.
+     * <p>
+     * When {@code keepPreparedTxn} is {@code false}, this behaves like the normal transactional
+     * initialization, aborting any unfinished transactions and resetting the producer for
+     * new writes.
+     *
+     * @param keepPreparedTxn true to retain any in-flight prepared transactions (necessary for 2PC
+     *                        recovery), false to abort existing transactions and behave like
+     *                        the standard initTransactions
+     *
+     * @throws IllegalStateException if no {@code transactional.id} is configured
+     * @throws org.apache.kafka.common.errors.UnsupportedVersionException if the broker does not
+     *         support transactions (i.e. if its version is lower than 0.11.0.0)
+     * @throws org.apache.kafka.common.errors.TransactionalIdAuthorizationException if the configured
+     *         {@code transactional.id} is unauthorized either for normal transaction writes or 2PC.
+     * @throws KafkaException if the producer encounters a fatal error or any other unexpected error
+     * @throws TimeoutException if the time taken for initialize the transaction has surpassed <code>max.block.ms</code>.
+     * @throws InterruptException if the thread is interrupted while blocked
+     */
+    public void initTransactions(boolean keepPreparedTxn) {

Review Comment:
   Discussed offline, removed initTransactions() and merged the javadocs for both
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-19082: [1/4] Add client config for enable2PC and overloaded initProducerId (KIP-939) [kafka]

Posted by "rreddy-22 (via GitHub)" <gi...@apache.org>.
rreddy-22 commented on code in PR #19429:
URL: https://github.com/apache/kafka/pull/19429#discussion_r2051195451


##########
clients/src/main/java/org/apache/kafka/clients/producer/internals/TransactionManager.java:
##########
@@ -273,17 +276,29 @@ public TransactionManager(final LogContext logContext,
         this.retryBackoffMs = retryBackoffMs;
         this.txnPartitionMap = new TxnPartitionMap(logContext);
         this.apiVersions = apiVersions;
+        this.enable2PC = enable2PC;
     }
 
     void setPoisonStateOnInvalidTransition(boolean shouldPoisonState) {
         shouldPoisonStateOnInvalidTransition.set(shouldPoisonState);
     }
 
     public synchronized TransactionalRequestResult initializeTransactions() {
-        return initializeTransactions(ProducerIdAndEpoch.NONE);
+        return initializeTransactions(ProducerIdAndEpoch.NONE, false);

Review Comment:
   Yes, I was also unsure about this. The main reason I decided to keep the overload was that all the existing tests currently use it. I figured anyone who doesn’t particularly care about keepPrepared shouldn’t have to deal with the additional argument. 
   That said, I know that there is another school of thought that is not to have surprise defaults and to make callers set parameters explicitly which I agree with. 
   I just didn't know how much change we wanted to make to the existing internal method so lmk whichever works
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-19082: [1/4] Add client config for enable2PC and overloaded initProducerId (KIP-939) [kafka]

Posted by "rreddy-22 (via GitHub)" <gi...@apache.org>.
rreddy-22 commented on code in PR #19429:
URL: https://github.com/apache/kafka/pull/19429#discussion_r2051214889


##########
clients/src/main/java/org/apache/kafka/clients/producer/KafkaProducer.java:
##########
@@ -654,6 +657,46 @@ public void initTransactions() {
         transactionManager.maybeUpdateTransactionV2Enabled(true);
     }
 
+    /**
+     * Initialize the transactional state for this producer, similar to {@link #initTransactions()} but
+     * with additional handling for two-phase commit (2PC). Must be called before any send operations
+     * that require a {@code transactionalId}.
+     * <p>
+     * Unlike the standard {@link #initTransactions()}, when {@code keepPreparedTxn} is set to
+     * {@code true}, the producer does <em>not</em> automatically abort existing transactions
+     * in the “prepare” phase. Instead, it enters a recovery mode allowing only finalization
+     * of those previously prepared transactions. This behavior is crucial for 2PC scenarios,
+     * where transactions should remain intact until the external transaction manager decides
+     * whether to commit or abort.
+     * <p>
+     * When {@code keepPreparedTxn} is {@code false}, this behaves like the normal transactional
+     * initialization, aborting any unfinished transactions and resetting the producer for
+     * new writes.
+     *
+     * @param keepPreparedTxn true to retain any in-flight prepared transactions (necessary for 2PC
+     *                        recovery), false to abort existing transactions and behave like
+     *                        the standard initTransactions
+     *
+     * @throws IllegalStateException if no {@code transactional.id} is configured
+     * @throws org.apache.kafka.common.errors.UnsupportedVersionException if the broker does not
+     *         support transactions (i.e. if its version is lower than 0.11.0.0)
+     * @throws org.apache.kafka.common.errors.TransactionalIdAuthorizationException if the configured
+     *         {@code transactional.id} is unauthorized either for normal transaction writes or 2PC.
+     * @throws KafkaException if the producer encounters a fatal error or any other unexpected error
+     * @throws TimeoutException if the time taken for initialize the transaction has surpassed <code>max.block.ms</code>.
+     * @throws InterruptException if the thread is interrupted while blocked
+     */
+    public void initTransactions(boolean keepPreparedTxn) {

Review Comment:
   makes sense! I made the change, just had one question, do we still want to leave in initTransactions() in the KafkaProducer class for the javadoc? Or should I remove it here as well so it uses the default?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-19082: [1/4] Add client config for enable2PC and overloaded initProducerId (KIP-939) [kafka]

Posted by "rreddy-22 (via GitHub)" <gi...@apache.org>.
rreddy-22 commented on code in PR #19429:
URL: https://github.com/apache/kafka/pull/19429#discussion_r2053115299


##########
clients/src/main/java/org/apache/kafka/clients/producer/internals/TransactionManager.java:
##########
@@ -273,17 +276,29 @@ public TransactionManager(final LogContext logContext,
         this.retryBackoffMs = retryBackoffMs;
         this.txnPartitionMap = new TxnPartitionMap(logContext);
         this.apiVersions = apiVersions;
+        this.enable2PC = enable2PC;
     }
 
     void setPoisonStateOnInvalidTransition(boolean shouldPoisonState) {
         shouldPoisonStateOnInvalidTransition.set(shouldPoisonState);
     }
 
     public synchronized TransactionalRequestResult initializeTransactions() {
-        return initializeTransactions(ProducerIdAndEpoch.NONE);
+        return initializeTransactions(ProducerIdAndEpoch.NONE, false);

Review Comment:
   Discussed offline, we are going to remove initializeTransactions() and keep only initializeTransactions(boolean keepPreparedTxn)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-19082: [1/4] Add client config for enable2PC and overloaded initProducerId (KIP-939) [kafka]

Posted by "rreddy-22 (via GitHub)" <gi...@apache.org>.
rreddy-22 commented on code in PR #19429:
URL: https://github.com/apache/kafka/pull/19429#discussion_r2049313174


##########
clients/src/main/java/org/apache/kafka/clients/producer/KafkaProducer.java:
##########
@@ -654,6 +657,46 @@ public void initTransactions() {
         transactionManager.maybeUpdateTransactionV2Enabled(true);
     }
 
+    /**
+     * Initialize the transactional state for this producer, similar to {@link #initTransactions()} but
+     * with additional handling for two-phase commit (2PC). Must be called before any send operations
+     * that require a {@code transactionalId}.
+     * <p>
+     * Unlike the standard {@link #initTransactions()}, when {@code keepPreparedTxn} is set to
+     * {@code true}, the producer does <em>not</em> automatically abort existing transactions
+     * in the “prepare” phase. Instead, it enters a recovery mode allowing only finalization
+     * of those previously prepared transactions. This behavior is crucial for 2PC scenarios,

Review Comment:
   I could add the three methods we allow -> commit, abort, complete



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-19082: [1/4] Add client config for enable2PC and overloaded initProducerId (KIP-939) [kafka]

Posted by "rreddy-22 (via GitHub)" <gi...@apache.org>.
rreddy-22 commented on code in PR #19429:
URL: https://github.com/apache/kafka/pull/19429#discussion_r2051218013


##########
clients/src/main/java/org/apache/kafka/clients/producer/KafkaProducer.java:
##########
@@ -654,6 +657,46 @@ public void initTransactions() {
         transactionManager.maybeUpdateTransactionV2Enabled(true);
     }
 
+    /**
+     * Initialize the transactional state for this producer, similar to {@link #initTransactions()} but
+     * with additional handling for two-phase commit (2PC). Must be called before any send operations
+     * that require a {@code transactionalId}.
+     * <p>
+     * Unlike the standard {@link #initTransactions()}, when {@code keepPreparedTxn} is set to
+     * {@code true}, the producer does <em>not</em> automatically abort existing transactions
+     * in the “prepare” phase. Instead, it enters a recovery mode allowing only finalization
+     * of those previously prepared transactions. This behavior is crucial for 2PC scenarios,

Review Comment:
   makes sense, thanks!
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-19082: [1/4] Add client config for enable2PC and overloaded initProducerId (KIP-939) [kafka]

Posted by "rreddy-22 (via GitHub)" <gi...@apache.org>.
rreddy-22 commented on code in PR #19429:
URL: https://github.com/apache/kafka/pull/19429#discussion_r2051170796


##########
clients/src/main/java/org/apache/kafka/clients/producer/KafkaProducer.java:
##########
@@ -654,6 +657,46 @@ public void initTransactions() {
         transactionManager.maybeUpdateTransactionV2Enabled(true);
     }
 
+    /**
+     * Initialize the transactional state for this producer, similar to {@link #initTransactions()} but
+     * with additional handling for two-phase commit (2PC). Must be called before any send operations

Review Comment:
   Hmm, that's true, I can try to make the difference clear using keepPrepared instead of 2PC



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-19082: [1/4] Add client config for enable2PC and overloaded initProducerId (KIP-939) [kafka]

Posted by "jolshan (via GitHub)" <gi...@apache.org>.
jolshan commented on code in PR #19429:
URL: https://github.com/apache/kafka/pull/19429#discussion_r2051125842


##########
clients/src/main/java/org/apache/kafka/clients/producer/KafkaProducer.java:
##########
@@ -654,6 +657,46 @@ public void initTransactions() {
         transactionManager.maybeUpdateTransactionV2Enabled(true);
     }
 
+    /**
+     * Initialize the transactional state for this producer, similar to {@link #initTransactions()} but
+     * with additional handling for two-phase commit (2PC). Must be called before any send operations
+     * that require a {@code transactionalId}.
+     * <p>
+     * Unlike the standard {@link #initTransactions()}, when {@code keepPreparedTxn} is set to
+     * {@code true}, the producer does <em>not</em> automatically abort existing transactions
+     * in the “prepare” phase. Instead, it enters a recovery mode allowing only finalization
+     * of those previously prepared transactions. This behavior is crucial for 2PC scenarios,

Review Comment:
   I think the phrasing here is sufficient without the method specification. 
   
   >  in the “prepare” phase
   
   I think maybe we shouldn't use this phrasing since it is a little confusing. The transactions are in the prepare phase for external 2PC but from the Kafka point of view, they are in the ongoing phase. 
   
   Artem may have opinions here too, so happy to hear his thoughts and if he disagrees. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-19082: [1/4] Add client config for enable2PC and overloaded initProducerId (KIP-939) [kafka]

Posted by "jolshan (via GitHub)" <gi...@apache.org>.
jolshan merged PR #19429:
URL: https://github.com/apache/kafka/pull/19429


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org