Skip to content

Commit 2debbb9

Browse files
dengzimingdengziming
and
dengziming
authored
KAFKA-12772: Move all transaction state transition rules into their states (#10667)
Co-authored-by: dengziming <[email protected]>
1 parent 7ef3879 commit 2debbb9

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

core/src/main/scala/kafka/coordinator/transaction/TransactionMetadata.scala

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ private[transaction] sealed trait TransactionState {
6464
* Get the name of this state. This is exposed through the `DescribeTransactions` API.
6565
*/
6666
def name: String
67+
68+
def validPreviousStates: Set[TransactionState]
6769
}
6870

6971
/**
@@ -75,6 +77,7 @@ private[transaction] sealed trait TransactionState {
7577
private[transaction] case object Empty extends TransactionState {
7678
val id: Byte = 0
7779
val name: String = "Empty"
80+
val validPreviousStates: Set[TransactionState] = Set(Empty, CompleteCommit, CompleteAbort)
7881
}
7982

8083
/**
@@ -88,6 +91,7 @@ private[transaction] case object Empty extends TransactionState {
8891
private[transaction] case object Ongoing extends TransactionState {
8992
val id: Byte = 1
9093
val name: String = "Ongoing"
94+
val validPreviousStates: Set[TransactionState] = Set(Ongoing, Empty, CompleteCommit, CompleteAbort)
9195
}
9296

9397
/**
@@ -98,6 +102,7 @@ private[transaction] case object Ongoing extends TransactionState {
98102
private[transaction] case object PrepareCommit extends TransactionState {
99103
val id: Byte = 2
100104
val name: String = "PrepareCommit"
105+
val validPreviousStates: Set[TransactionState] = Set(Ongoing)
101106
}
102107

103108
/**
@@ -108,6 +113,7 @@ private[transaction] case object PrepareCommit extends TransactionState {
108113
private[transaction] case object PrepareAbort extends TransactionState {
109114
val id: Byte = 3
110115
val name: String = "PrepareAbort"
116+
val validPreviousStates: Set[TransactionState] = Set(Ongoing, PrepareEpochFence)
111117
}
112118

113119
/**
@@ -118,6 +124,7 @@ private[transaction] case object PrepareAbort extends TransactionState {
118124
private[transaction] case object CompleteCommit extends TransactionState {
119125
val id: Byte = 4
120126
val name: String = "CompleteCommit"
127+
val validPreviousStates: Set[TransactionState] = Set(PrepareCommit)
121128
}
122129

123130
/**
@@ -128,6 +135,7 @@ private[transaction] case object CompleteCommit extends TransactionState {
128135
private[transaction] case object CompleteAbort extends TransactionState {
129136
val id: Byte = 5
130137
val name: String = "CompleteAbort"
138+
val validPreviousStates: Set[TransactionState] = Set(PrepareAbort)
131139
}
132140

133141
/**
@@ -136,6 +144,7 @@ private[transaction] case object CompleteAbort extends TransactionState {
136144
private[transaction] case object Dead extends TransactionState {
137145
val id: Byte = 6
138146
val name: String = "Dead"
147+
val validPreviousStates: Set[TransactionState] = Set(Empty, CompleteAbort, CompleteCommit)
139148
}
140149

141150
/**
@@ -145,6 +154,7 @@ private[transaction] case object Dead extends TransactionState {
145154
private[transaction] case object PrepareEpochFence extends TransactionState {
146155
val id: Byte = 7
147156
val name: String = "PrepareEpochFence"
157+
val validPreviousStates: Set[TransactionState] = Set(Ongoing)
148158
}
149159

150160
private[transaction] object TransactionMetadata {
@@ -162,20 +172,6 @@ private[transaction] object TransactionMetadata {
162172
new TransactionMetadata(transactionalId, producerId, lastProducerId, producerEpoch, lastProducerEpoch,
163173
txnTimeoutMs, state, collection.mutable.Set.empty[TopicPartition], timestamp, timestamp)
164174

165-
def isValidTransition(oldState: TransactionState, newState: TransactionState): Boolean =
166-
TransactionMetadata.validPreviousStates(newState).contains(oldState)
167-
168-
private val validPreviousStates: Map[TransactionState, Set[TransactionState]] =
169-
Map(Empty -> Set(Empty, CompleteCommit, CompleteAbort),
170-
Ongoing -> Set(Ongoing, Empty, CompleteCommit, CompleteAbort),
171-
PrepareCommit -> Set(Ongoing),
172-
PrepareAbort -> Set(Ongoing, PrepareEpochFence),
173-
CompleteCommit -> Set(PrepareCommit),
174-
CompleteAbort -> Set(PrepareAbort),
175-
Dead -> Set(Empty, CompleteAbort, CompleteCommit),
176-
PrepareEpochFence -> Set(Ongoing)
177-
)
178-
179175
def isEpochExhausted(producerEpoch: Short): Boolean = producerEpoch >= Short.MaxValue - 1
180176
}
181177

@@ -385,7 +381,7 @@ private[transaction] class TransactionMetadata(val transactionalId: String,
385381
throw new IllegalArgumentException(s"Illegal new producer epoch $newEpoch")
386382

387383
// check that the new state transition is valid and update the pending state if necessary
388-
if (TransactionMetadata.validPreviousStates(newState).contains(state)) {
384+
if (newState.validPreviousStates.contains(state)) {
389385
val transitMetadata = TxnTransitMetadata(newProducerId, producerId, newEpoch, newLastEpoch, newTxnTimeoutMs, newState,
390386
newTopicPartitions, newTxnStartTimestamp, updateTimestamp)
391387
debug(s"TransactionalId $transactionalId prepare transition from $state to $transitMetadata")

0 commit comments

Comments
 (0)