chia7712 commented on code in PR #20456:
URL: https://github.com/apache/kafka/pull/20456#discussion_r2711313661


##########
core/src/main/scala/kafka/coordinator/transaction/TransactionStateManager.scala:
##########
@@ -495,19 +495,21 @@ class TransactionStateManager(brokerId: Int,
             memRecords.batches.forEach { batch =>
               for (record <- batch.asScala) {
                 require(record.hasKey, "Transaction state log's key should not 
be null")
-                TransactionLog.readTxnRecordKey(record.key) match {
-                  case Left(version) =>
-                    warn(s"Unknown message key with version $version" +
-                      s" while loading transaction state from $topicPartition. 
Ignoring it. " +
-                      "It could be a left over from an aborted upgrade.")
-                  case Right(transactionalId) =>
-                    // load transaction metadata along with transaction state
-                    TransactionLog.readTxnRecordValue(transactionalId, 
record.value) match {
-                      case None =>
-                        loadedTransactions.remove(transactionalId)
-                      case Some(txnMetadata) =>
-                        loadedTransactions.put(transactionalId, txnMetadata)
-                    }
+                val transactionalId = try 
Some(TransactionLog.readTxnRecordKey(record.key))

Review Comment:
   Sure, that works. we can use a sealed interface to avoid explicit error 
handling and match the result in Scala. For example:
   
   ```java
       public sealed interface ReadResult permits TxnRecord, TxnTombstone, 
UnknownVersion {
       }
   
       public record TxnRecord(String transactionId, TransactionMetadata 
metadata) implements ReadResult {}
   
       public record TxnTombstone(String transactionId) implements ReadResult {}
   
       public record UnknownVersion(int version) implements ReadResult {}
   
       public static ReadResult read(ByteBuffer keyBuffer, ByteBuffer 
valueBuffer) {
   ```
   
   ```scala
                 for (record <- batch.asScala) {
                   require(record.hasKey, "Transaction state log's key should 
not be null")
                   TransactionLog.read(record.key(), record.value()) match {
                     case v: TransactionLog.UnknownVersion =>
                       warn(s"Unknown message key with version ${v.version()}" +
                         s" while loading transaction state from 
$topicPartition. Ignoring it. " +
                         "It could be a left over from an aborted upgrade.")
                     case r: TransactionLog.TxnTombstone =>
                       loadedTransactions.remove(r.transactionId())
                     case r: TransactionLog.TxnRecord =>
                       loadedTransactions.put(r.transactionId(), r.metadata())
                   }
                 }
   ```



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