artemlivshits commented on code in PR #21735:
URL: https://github.com/apache/kafka/pull/21735#discussion_r4553401440


##########
core/src/main/scala/kafka/coordinator/transaction/TransactionCoordinator.scala:
##########
@@ -797,149 +843,189 @@ class TransactionCoordinator(txnConfig: 
TransactionConfig,
           val coordinatorEpoch = epochAndTxnMetadata.coordinatorEpoch
 
           txnMetadata.inLock(() => {
-            producerIdCopy = txnMetadata.producerId
-            producerEpochCopy = txnMetadata.producerEpoch
-            // PrepareEpochFence has slightly different epoch bumping logic so 
don't include it here.
-            // Note that, it can only happen when the current state is Ongoing.
-            isEpochFence = txnMetadata.pendingState.filter(s => s == 
TransactionState.PREPARE_EPOCH_FENCE).isPresent
-            // True if the client retried a request that had overflowed the 
epoch, and a new producer ID is stored in the txnMetadata
-            val retryOnOverflow = !isEpochFence && txnMetadata.prevProducerId 
== producerId &&
-              producerEpoch == Short.MaxValue - 1 && txnMetadata.producerEpoch 
== 0
-            // True if the client retried an endTxn request, and the bumped 
producer epoch is stored in the txnMetadata.
-            val retryOnEpochBump = !isEpochFence && txnMetadata.producerEpoch 
== producerEpoch + 1
-
-            val isValidEpoch = {
-              if (!isEpochFence) {
-                // With transactions V2, state + same epoch is not sufficient 
to determine if a retry transition is valid. If the epoch is the
-                // same it actually indicates the next endTransaction call. 
Instead, we want to check the epoch matches with the epoch in the retry 
conditions.
-                // Return producer fenced even in the cases where the epoch is 
higher and could indicate an invalid state transition.
-                // Use the following criteria to determine if a v2 retry is 
valid:
-                txnMetadata.state match {
-                  case TransactionState.ONGOING | TransactionState.EMPTY | 
TransactionState.DEAD | TransactionState.PREPARE_EPOCH_FENCE =>
-                    producerEpoch == txnMetadata.producerEpoch
-                  case TransactionState.PREPARE_COMMIT | 
TransactionState.PREPARE_ABORT =>
-                    retryOnEpochBump
-                  case TransactionState.COMPLETE_COMMIT | 
TransactionState.COMPLETE_ABORT =>
-                    retryOnEpochBump || retryOnOverflow || producerEpoch == 
txnMetadata.producerEpoch
+            // These checks are performed first so that we don't have to deal 
with intermediate states
+            // (including PrepareCommit and PrepareAbort) in already complex 
state machine.
+            if (txnMetadata.pendingTransitionInProgress && 
!txnMetadata.pendingState.filter(s => s == 
TransactionState.PREPARE_EPOCH_FENCE).isPresent) {
+              Left(Errors.CONCURRENT_TRANSACTIONS)
+            } else if (txnMetadata.state == TransactionState.PREPARE_COMMIT || 
txnMetadata.state == TransactionState.PREPARE_ABORT) {
+              Left(Errors.CONCURRENT_TRANSACTIONS)
+            } else {
+              // Copy the data under lock, this is going to be used if we want 
to return success
+              // on retrying commit / abort.
+              producerIdCopy = txnMetadata.producerId
+              producerEpochCopy = txnMetadata.producerEpoch
+
+              // PrepareEpochFence has slightly different epoch bumping logic 
so don't include it here.
+              // Note that, it can only happen when the current state is 
Ongoing.
+              isEpochFence = txnMetadata.pendingState.filter(s => s == 
TransactionState.PREPARE_EPOCH_FENCE).isPresent
+
+              // Calculate retry conditions.  A retry is only possible in 
CompleteCommit / CompleteAbort states (the
+              // endTxn request that is being retried bumped the epoch), so 
the retry conditions only apply to these
+              // states.  In these states the client-facing producerId / epoch 
are the same as producerId / epoch.
+              // Note that the retry-on-overflow condition happens only when 
the producer id is rotated during the
+              // last commit / abort, rotations during 
InitProducerId(keepPreparedTxn=true) go through the
+              // retry-on-epoch-bump path.  Example:
+              //  1. State ONGOING, {pid, epoch} = {42, 10}, {nextPid, 
nextEpoch} = {73, 85} -- the client-facing producer id was rotated
+              //  2. Commit transitions to {pid, epoch} = {73, 86} and the 
client retries with {73, 85} -- retryOnEpochBump
+              // Another example where the producer id is rotated during 
commit after it was rotated with keepPreparedTxn = true
+              //  1. State ONGOING, {pid, epoch} = {42, 10}, {nextPid, 
nextEpoch} = {73, 32766}
+              //  2. Commit transitions to {pid, epoch} = {85, 0}, prevPid = 
73 and the client retries with {73, 32766} -- retryOnOverflow
+              val isCompleteState = txnMetadata.state == 
TransactionState.COMPLETE_COMMIT ||
+                txnMetadata.state == TransactionState.COMPLETE_ABORT
+
+              // True if the client retried an endTxn request that rotated the 
producer id on epoch overflow, the new
+              // producer id is stored in the txnMetadata and the client's 
producer id in prevProducerId.
+              val retryOnOverflow = isCompleteState && !isEpochFence && 
producerId != RecordBatch.NO_PRODUCER_ID &&

Review Comment:
   Agreed, it's not a valid request, the check is just to make sure it isn't 
treated as a retry.



##########
storage/src/test/java/org/apache/kafka/tiered/storage/integration/TransactionsWith2PCTest.java:
##########
@@ -0,0 +1,647 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.tiered.storage.integration;

Review Comment:
   It doesn't use tiered storage. TransactionsTest was moved there when it was 
rewritten in Java (KAFKA-19042) so that the tiered store variant could share 
TransactionsTestHelper, and this test uses the same helper, so I put it next to 
TransactionsTest. Happy to move it if there's a better place.



##########
core/src/main/scala/kafka/coordinator/transaction/TransactionCoordinator.scala:
##########
@@ -263,7 +277,7 @@ class TransactionCoordinator(txnConfig: TransactionConfig,
           val transitMetadataResult =
             // If the epoch is exhausted and the expected epoch (if provided) 
matches it, generate a new producer ID
             try {
-              if (txnMetadata.isProducerEpochExhausted &&
+              if (txnMetadata.isClientFacingEpochExhausted &&

Review Comment:
   Yes, they're the same in Empty / CompleteCommit / CompleteAbort, but it's 
more consistent to use the client-facing epoch here. Done in 70bc6620ee.



##########
transaction-coordinator/src/main/java/org/apache/kafka/coordinator/transaction/TransactionMetadata.java:
##########
@@ -257,8 +342,15 @@ public TxnTransitMetadata 
prepareAbortOrCommit(TransactionState newState,
         // start time is uncertain but it is still required. So we can use the 
update time as the transaction start time.
         data.txnStartTimestamp = noPartitionAdded ? updateTimestamp : 
txnStartTimestamp;
         data.nextProducerId = nextProducerId;
+        data.nextProducerEpoch = nextProducerEpoch;
         data.txnLastUpdateTimestamp = updateTimestamp;
         data.clientTransactionVersion = clientTransactionVersion;
+        // If the transaction was kept with 
InitProducerId(keepPreparedTxn=true), the client used the client-facing
+        // producer id, which may be different from the frozen producerId.  
Keep track of it as the previous
+        // producer id, so that the client's retry can be recognized if the 
producer id is rotated on completion.
+        if (hasNextProducerEpoch())
+            data.prevProducerId = clientProducerId();

Review Comment:
   Yes, with the CONCURRENT_TRANSACTIONS check moved first, endTransaction 
never has to interpret prevProducerId in PrepareCommit / PrepareAbort, so 
changing what it records there only affects the retry checks in CompleteCommit 
/ CompleteAbort.



##########
core/src/main/scala/kafka/coordinator/transaction/TransactionCoordinator.scala:
##########
@@ -442,6 +477,12 @@ class TransactionCoordinator(txnConfig: TransactionConfig,
               // With TV2, we may be transitioning over a producer epoch 
overflow, and the producer may be using the
               // new producer ID that is still only in pending state.
               Left(Errors.CONCURRENT_TRANSACTIONS)
+            } else if (txnMetadata.hasNextProducerEpoch) {
+              // InitProducerId(keepPreparedTxn=true) kept the ongoing 
transaction, it can only be committed
+              // or aborted, so adding partitions is not allowed regardless of 
the identity in the request
+              // (including the frozen producerId / producerEpoch used for the 
markers).  A well-behaving
+              // client never adds partitions in this state.
+              Left(Errors.INVALID_TXN_STATE)

Review Comment:
   Yes, a stale request could trigger it: e.g. a produce request from the 
producer instance that started the transaction (a zombie at this point, most 
likely dead) arriving after InitProducerId(keepPreparedTxn=true) -- with TV2 
the partition leader would add the partition with the frozen identity. The 
error is fatal either way, but PRODUCER_FENCED is a more accurate signal for 
that, so changed it to PRODUCER_FENCED in 70bc6620ee (the partition leader maps 
it to INVALID_PRODUCER_EPOCH for produce, as for other zombies).



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to