artemlivshits commented on code in PR #21735:
URL: https://github.com/apache/kafka/pull/21735#discussion_r4529072134
##########
core/src/main/scala/kafka/coordinator/transaction/TransactionCoordinator.scala:
##########
@@ -927,9 +987,14 @@ class TransactionCoordinator(txnConfig: TransactionConfig,
if (epochAndMetadata.coordinatorEpoch == coordinatorEpoch) {
val txnMetadata = epochAndMetadata.transactionMetadata
txnMetadata.inLock(() => {
- if (txnMetadata.producerId != producerId)
+ // Note that if we did a few
InitProducerId(keepPreparedTxn) the client's producer id
+ // may be different from the producerId (we track it in
the nextProducerId), but at
+ // this point even nextProducerId could be different
from the client's producer id
+ // if we generated a new nextProducerId due to
nextProducerEpoch overflow. So we
+ // check with the copy of the txnMetadata.producerId.
Ditto for the epoch.
+ if (txnMetadata.producerId != producerIdCopy)
Left(Errors.INVALID_PRODUCER_ID_MAPPING)
- else if (txnMetadata.producerEpoch != producerEpoch &&
txnMetadata.producerEpoch != producerEpoch + 1)
+ else if (txnMetadata.producerEpoch != producerEpochCopy
&& txnMetadata.producerEpoch != producerEpochCopy + 1)
Review Comment:
Yes, the epoch is not bumped only when aborting with the epoch already at
Short.MaxValue (see prepareAbortOrCommit), otherwise PrepareCommit /
PrepareAbort bump it by 1. Clarified the comment in 314ff0da14.
##########
clients/src/main/java/org/apache/kafka/clients/producer/internals/TransactionManager.java:
##########
@@ -340,7 +340,10 @@ synchronized TransactionalRequestResult
initializeTransactions(
.setEnable2Pc(enable2PC)
.setKeepPreparedTxn(keepPreparedTxn);
- InitProducerIdHandler handler = new InitProducerIdHandler(new
InitProducerIdRequest.Builder(requestData),
+ // Use unstable API version when 2PC features are enabled
(requires InitProducerId v6)
+ boolean enableUnstableLastVersion = enable2PC || keepPreparedTxn;
+ InitProducerIdHandler handler = new InitProducerIdHandler(
Review Comment:
Yes, once InitProducerId v6 is marked stable this opt-in won't be needed and
can be removed.
##########
core/src/main/scala/kafka/coordinator/transaction/TransactionStateManager.scala:
##########
@@ -143,7 +143,7 @@ class TransactionStateManager(brokerId: Int,
}
}
}.map { case (txnId, txnMetadata) =>
- new TransactionalIdAndProducerIdEpoch(txnId, txnMetadata.producerId,
txnMetadata.producerEpoch)
+ new TransactionalIdAndProducerIdEpoch(txnId,
txnMetadata.clientProducerId, txnMetadata.clientProducerEpoch)
Review Comment:
Yes, the timeout abort has to fence the client, so it bumps the
client-facing epoch (the one the client actually uses). The frozen producerId /
epoch are only used for the markers of the kept transaction and are bumped
separately by the abort.
##########
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 think the intent was to distinguish a retry from the next EndTxn at the
same epoch. With TV2 the epoch does that by itself: a retry comes with the
epoch before the bump (retryOnEpochBump / retryOnOverflow), and the next EndTxn
comes with the current epoch, which is the 'Current' column of the state table.
One correction to my earlier reply: after KAFKA-21181 (merged from trunk),
CompleteCommit + Commit at the current epoch commits an empty transaction (EB)
rather than returning INVALID_TXN_STATE. @junrao please let me know if you had
a different case in mind.
##########
core/src/main/scala/kafka/coordinator/transaction/TransactionCoordinator.scala:
##########
@@ -927,9 +987,14 @@ class TransactionCoordinator(txnConfig: TransactionConfig,
if (epochAndMetadata.coordinatorEpoch == coordinatorEpoch) {
val txnMetadata = epochAndMetadata.transactionMetadata
txnMetadata.inLock(() => {
- if (txnMetadata.producerId != producerId)
+ // Note that if we did a few
InitProducerId(keepPreparedTxn) the client's producer id
+ // may be different from the producerId (we track it in
the nextProducerId), but at
+ // this point even nextProducerId could be different
from the client's producer id
+ // if we generated a new nextProducerId due to
nextProducerEpoch overflow. So we
+ // check with the copy of the txnMetadata.producerId.
Ditto for the epoch.
+ if (txnMetadata.producerId != producerIdCopy)
Review Comment:
Yes, it's a sanity check that's not expected to fail, kept as a safeguard
against sending markers with a wrong producer id / epoch. Made that explicit in
the comment in 314ff0da14.
##########
transaction-coordinator/src/main/java/org/apache/kafka/coordinator/transaction/TransactionMetadata.java:
##########
@@ -276,29 +350,42 @@ public TxnTransitMetadata prepareComplete(long
updateTimestamp) {
// but lastProducerEpoch remains MAX-1 to maintain consistency with
what the client last saw.
if (clientTransactionVersion.supportsEpochBump() && nextProducerId !=
RecordBatch.NO_PRODUCER_ID) {
data.producerId = nextProducerId;
- data.producerEpoch = 0;
+ data.producerEpoch = hasNextProducerEpoch() ? nextProducerEpoch :
0;
Review Comment:
Done in 314ff0da14.
##########
transaction-coordinator/src/main/java/org/apache/kafka/coordinator/transaction/TxnTransitMetadata.java:
##########
@@ -38,4 +40,36 @@ public record TxnTransitMetadata(
long txnStartTimestamp,
long txnLastUpdateTimestamp,
TransactionVersion clientTransactionVersion
-) { }
+) {
+ private boolean hasNextProducerEpoch() {
+ return nextProducerEpoch != RecordBatch.NO_PRODUCER_EPOCH;
+ }
+
+ // When InitProducerId keeps an Ongoing transaction (which is "prepared"
from
+ // the 2PC protocol perspective) it needs to preserve the producerId /
epoch of
+ // the transaction (that will be used for sending markers), but update the
+ // producerId / epoch that's going to be used by client (so that we could
fence
+ // stale client requests). For backward compatibility (in case of server
+ // downgrade) we keep the producerId / epoch of the transaction in the
+ // producer* fields and the other pair in the nextProducer* fields, so
+ // we have to do a little bit of switcheroo:
Review Comment:
Yes, that's right: the producer* fields always represent the ongoing
transaction, and on downgrade we only lose the client-facing identity used for
fencing stale clients. Reworded the comment in 314ff0da14.
##########
transaction-coordinator/src/main/java/org/apache/kafka/coordinator/transaction/TransactionMetadata.java:
##########
@@ -332,8 +419,8 @@ private TxnTransitMetadata
prepareTransitionTo(TransitionData data) {
// check that the new state transition is valid and update the pending
state if necessary
if (data.state.validPreviousStates().contains(this.state)) {
TxnTransitMetadata transitMetadata = new TxnTransitMetadata(
- data.producerId, this.producerId, data.nextProducerId,
data.producerEpoch, data.lastProducerEpoch,
- data.txnTimeoutMs, data.state, data.topicPartitions,
+ data.producerId, data.prevProducerId, data.nextProducerId,
data.producerEpoch, data.lastProducerEpoch,
Review Comment:
It's required: for a kept transaction the client uses the client-facing
producer id (nextProducerId), so that's the previous producer id that must be
recorded on PrepareCommit / PrepareAbort and CompleteCommit / CompleteAbort,
otherwise EndTxn / InitProducerId retries after the producer id rotation aren't
recognized (5060ebcdac). TransitionData.prevProducerId defaults to
this.producerId, so the other transitions are unchanged.
##########
core/src/main/scala/kafka/coordinator/transaction/TransactionCoordinator.scala:
##########
@@ -241,9 +248,14 @@ class TransactionCoordinator(txnConfig: TransactionConfig,
// increment it)
// 3. The expected producer ID matches the previous one and the
expected epoch is exhausted, in which case this
// could be a retry after a valid epoch bump that the producer
never received the response for
- txnMetadata.producerEpoch == RecordBatch.NO_PRODUCER_EPOCH ||
- producerIdAndEpoch.producerId == txnMetadata.producerId ||
- (producerIdAndEpoch.producerId == txnMetadata.prevProducerId &&
TransactionMetadata.isEpochExhausted(producerIdAndEpoch.epoch))
+ // The checks are done against the client-facing identity: if
InitProducerId(keepPreparedTxn=true) was used, the
+ // producerId / producerEpoch are frozen to be used for the transaction
markers. In that case prevProducerId
+ // refers to the frozen producerId rather than to a previous
client-facing producer ID, so check 3 doesn't apply
Review Comment:
Right, it actually never goes through these checks: keepPreparedTxn with the
expected producer id / epoch is rejected with INVALID_REQUEST, so they only
apply to a subsequent InitProducerId with keepPreparedTxn=false. Clarified the
comment in 314ff0da14.
##########
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:
This can only happen if the client violates the protocol: a transaction is
kept only after it was prepared, and after InitProducerId(keepPreparedTxn=true)
the producer is in the prepared state where send / sendOffsetsToTransaction /
beginTransaction throw, so the application can only commit or abort. Added this
to the comment in 314ff0da14.
##########
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:
It's defensive: a CompleteCommit / CompleteAbort record written before
PreviousProducerId was persisted loads with prevProducerId = -1, so an EndTxn
with producerId = -1 and epoch MAX-1 would have matched the retry-on-overflow
condition. shouldRejectEndTxnOverflowRetryWithoutPrevProducerId covers it.
##########
transaction-coordinator/src/main/java/org/apache/kafka/coordinator/transaction/TransactionMetadata.java:
##########
@@ -108,6 +112,27 @@ public TransactionMetadata(String transactionalId,
this.lock = new ReentrantLock();
}
+ public boolean hasNextProducerEpoch() {
+ return nextProducerEpoch != RecordBatch.NO_PRODUCER_EPOCH;
+ }
+
+ // When InitProducerId keeps an Ongoing transaction (which is "prepared"
from
+ // the 2PC protocol perspective) it needs to preserve the producerId /
epoch of
+ // the transaction (that will be used for sending markers), but update the
+ // producerId / epoch that's going to be used by client (so that we could
fence
+ // stale client requests). For backward compatibility (in case of server
+ // downgrade) we keep the producerId / epoch of the transaction in the
+ // producer* fields and the other pair in the nextProducer* fields.
+ // Note that we check hasNextProducerEpoch because nextProducerId could
Review Comment:
Added guards in 314ff0da14: prepareAbortOrCommit rejects nextProducerEpoch
if the transaction wasn't kept with InitProducerId(keepPreparedTxn=true), and
prepareTransitionTo rejects carrying it into Empty / CompleteCommit /
CompleteAbort / Dead. Also expanded the comment on the field. Both are covered
in TransactionMetadataTest.
##########
core/src/main/scala/kafka/coordinator/transaction/TransactionCoordinator.scala:
##########
@@ -140,9 +141,12 @@ class TransactionCoordinator(txnConfig: TransactionConfig,
// 2PC functionality is disabled, clients that attempt to use this
functionality
// would receive an authorization failed error.
responseCallback(initTransactionError(Errors.TRANSACTIONAL_ID_AUTHORIZATION_FAILED))
- } else if (keepPreparedTxn) {
- // if the request is to keep the prepared transaction, then return an
- // unsupported version error since the feature hasn't been implemented
yet.
+ } else if (keepPreparedTxn && expectedProducerIdAndEpoch.nonEmpty) {
+ // keepPreparedTxn can only be specified in the initial call of
InitProducerId, so
+ // no expected producerId / epoch should be set
+ responseCallback(initTransactionError(Errors.INVALID_REQUEST))
+ } else if (keepPreparedTxn &&
!txnManager.transactionVersionLevel().supportsEpochBump()) {
Review Comment:
Yes, we'll gate it on TV3 when it's added. For now it's gated by
InitProducerId v6 being unstable (KeepPreparedTxn is only in v6+, so brokers
reject it unless unstable.api.versions.enable=true), and keepPreparedTxn also
requires TV2.
--
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]