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


##########
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:
   did we hit a case where producerId == RecordBatch.NO_PRODUCER_ID was a 
problem?



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