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


##########
core/src/main/scala/kafka/coordinator/transaction/TransactionCoordinator.scala:
##########
@@ -779,134 +808,165 @@ 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, note that they are going to be 
used only
+              // in CompleteCommit / CompleteAbort states, so we can use the 
semantics
+              // of the producerId / epoch that's applicable in those states.
+              // Also note that the retry-on-overflow condition happens only 
when
+              // overflow happens during last commit / abort, overflows during
+              // InitProducerId(keepPreparedTxn=true) go through 
retry-on-epoch-bump
+              // path.  Example:
+              //  1. State ONGOING, {pid, epoch} = {42, 10}, {nextPid, 
nextEpoch} = {73, 85} -- the client-facing epoch has overflown
+              //  2. Commit transitions to {pid, epoch} = {73, 86} and the 
client retries with {73, 85} -- retryOnEpochBump
+              // Another example where we overflow during commit after going 
through overflow 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
+
+              // 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.clientProducerEpoch
+                    case TransactionState.PREPARE_COMMIT | 
TransactionState.PREPARE_ABORT =>
+                      retryOnEpochBump
+                    case TransactionState.COMPLETE_COMMIT | 
TransactionState.COMPLETE_ABORT =>
+                      retryOnEpochBump || retryOnOverflow || producerEpoch == 
txnMetadata.producerEpoch

Review Comment:
   I didn't quite understand what Jun was proposing here. Just trying to 
understand if there was a case he was meaning to cover. I agree though that the 
proposed change wouldn't work since we fill the prevProducerId field. 



-- 
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