@@ -64,6 +64,8 @@ private[transaction] sealed trait TransactionState {
64
64
* Get the name of this state. This is exposed through the `DescribeTransactions` API.
65
65
*/
66
66
def name : String
67
+
68
+ def validPreviousStates : Set [TransactionState ]
67
69
}
68
70
69
71
/**
@@ -75,6 +77,7 @@ private[transaction] sealed trait TransactionState {
75
77
private [transaction] case object Empty extends TransactionState {
76
78
val id : Byte = 0
77
79
val name : String = " Empty"
80
+ val validPreviousStates : Set [TransactionState ] = Set (Empty , CompleteCommit , CompleteAbort )
78
81
}
79
82
80
83
/**
@@ -88,6 +91,7 @@ private[transaction] case object Empty extends TransactionState {
88
91
private [transaction] case object Ongoing extends TransactionState {
89
92
val id : Byte = 1
90
93
val name : String = " Ongoing"
94
+ val validPreviousStates : Set [TransactionState ] = Set (Ongoing , Empty , CompleteCommit , CompleteAbort )
91
95
}
92
96
93
97
/**
@@ -98,6 +102,7 @@ private[transaction] case object Ongoing extends TransactionState {
98
102
private [transaction] case object PrepareCommit extends TransactionState {
99
103
val id : Byte = 2
100
104
val name : String = " PrepareCommit"
105
+ val validPreviousStates : Set [TransactionState ] = Set (Ongoing )
101
106
}
102
107
103
108
/**
@@ -108,6 +113,7 @@ private[transaction] case object PrepareCommit extends TransactionState {
108
113
private [transaction] case object PrepareAbort extends TransactionState {
109
114
val id : Byte = 3
110
115
val name : String = " PrepareAbort"
116
+ val validPreviousStates : Set [TransactionState ] = Set (Ongoing , PrepareEpochFence )
111
117
}
112
118
113
119
/**
@@ -118,6 +124,7 @@ private[transaction] case object PrepareAbort extends TransactionState {
118
124
private [transaction] case object CompleteCommit extends TransactionState {
119
125
val id : Byte = 4
120
126
val name : String = " CompleteCommit"
127
+ val validPreviousStates : Set [TransactionState ] = Set (PrepareCommit )
121
128
}
122
129
123
130
/**
@@ -128,6 +135,7 @@ private[transaction] case object CompleteCommit extends TransactionState {
128
135
private [transaction] case object CompleteAbort extends TransactionState {
129
136
val id : Byte = 5
130
137
val name : String = " CompleteAbort"
138
+ val validPreviousStates : Set [TransactionState ] = Set (PrepareAbort )
131
139
}
132
140
133
141
/**
@@ -136,6 +144,7 @@ private[transaction] case object CompleteAbort extends TransactionState {
136
144
private [transaction] case object Dead extends TransactionState {
137
145
val id : Byte = 6
138
146
val name : String = " Dead"
147
+ val validPreviousStates : Set [TransactionState ] = Set (Empty , CompleteAbort , CompleteCommit )
139
148
}
140
149
141
150
/**
@@ -145,6 +154,7 @@ private[transaction] case object Dead extends TransactionState {
145
154
private [transaction] case object PrepareEpochFence extends TransactionState {
146
155
val id : Byte = 7
147
156
val name : String = " PrepareEpochFence"
157
+ val validPreviousStates : Set [TransactionState ] = Set (Ongoing )
148
158
}
149
159
150
160
private [transaction] object TransactionMetadata {
@@ -162,20 +172,6 @@ private[transaction] object TransactionMetadata {
162
172
new TransactionMetadata (transactionalId, producerId, lastProducerId, producerEpoch, lastProducerEpoch,
163
173
txnTimeoutMs, state, collection.mutable.Set .empty[TopicPartition ], timestamp, timestamp)
164
174
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
-
179
175
def isEpochExhausted (producerEpoch : Short ): Boolean = producerEpoch >= Short .MaxValue - 1
180
176
}
181
177
@@ -385,7 +381,7 @@ private[transaction] class TransactionMetadata(val transactionalId: String,
385
381
throw new IllegalArgumentException (s " Illegal new producer epoch $newEpoch" )
386
382
387
383
// 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)) {
389
385
val transitMetadata = TxnTransitMetadata (newProducerId, producerId, newEpoch, newLastEpoch, newTxnTimeoutMs, newState,
390
386
newTopicPartitions, newTxnStartTimestamp, updateTimestamp)
391
387
debug(s " TransactionalId $transactionalId prepare transition from $state to $transitMetadata" )
0 commit comments