junrao commented on code in PR #13391:
URL: https://github.com/apache/kafka/pull/13391#discussion_r1164633122


##########
core/src/main/scala/kafka/server/AddPartitionsToTxnManager.scala:
##########
@@ -0,0 +1,180 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package kafka.server
+
+import kafka.common.{InterBrokerSendThread, RequestAndCompletionHandler}
+import org.apache.kafka.clients.{ClientResponse, NetworkClient, 
RequestCompletionHandler}
+import org.apache.kafka.common.{Node, TopicPartition}
+import 
org.apache.kafka.common.message.AddPartitionsToTxnRequestData.{AddPartitionsToTxnTransaction,
 AddPartitionsToTxnTransactionCollection}
+import org.apache.kafka.common.protocol.Errors
+import org.apache.kafka.common.requests.{AddPartitionsToTxnRequest, 
AddPartitionsToTxnResponse}
+import org.apache.kafka.common.utils.Time
+
+import scala.collection.mutable
+
+object AddPartitionsToTxnManager {
+  type AppendCallback = Map[TopicPartition, Errors] => Unit
+}
+
+
+class TransactionDataAndCallbacks(val transactionData: 
AddPartitionsToTxnTransactionCollection,
+                                  val callbacks: mutable.Map[String, 
AddPartitionsToTxnManager.AppendCallback])
+
+
+class AddPartitionsToTxnManager(config: KafkaConfig, client: NetworkClient, 
time: Time) 
+  extends InterBrokerSendThread("AddPartitionsToTxnSenderThread-" + 
config.brokerId, client, config.requestTimeoutMs, time) {
+  
+  private val inflightNodes = mutable.HashSet[Node]()
+  private val nodesToTransactions = mutable.Map[Node, 
TransactionDataAndCallbacks]()
+  
+  def addTxnData(node: Node, transactionData: AddPartitionsToTxnTransaction, 
callback: AddPartitionsToTxnManager.AppendCallback): Unit = {
+    nodesToTransactions.synchronized {
+      // Check if we have already have either node or individual transaction. 
Add the Node if it isn't there.
+      val currentNodeAndTransactionData = 
nodesToTransactions.getOrElseUpdate(node,
+        new TransactionDataAndCallbacks(
+          new AddPartitionsToTxnTransactionCollection(1),
+          mutable.Map[String, AddPartitionsToTxnManager.AppendCallback]()))
+
+      val currentTransactionData = 
currentNodeAndTransactionData.transactionData.find(transactionData.transactionalId)
+
+      // Check if we already have txn ID -- if the epoch is bumped, return 
invalid producer epoch, otherwise, the client likely disconnected and 
+      // reconnected so return the retriable network exception.
+      if (currentTransactionData != null) {
+        val error = if (currentTransactionData.producerEpoch() < 
transactionData.producerEpoch())
+          Errors.INVALID_PRODUCER_EPOCH
+        else 
+          Errors.NETWORK_EXCEPTION
+        val topicPartitionsToError = mutable.Map[TopicPartition, Errors]()
+        currentTransactionData.topics().forEach { topic =>
+          topic.partitions().forEach { partition =>
+            topicPartitionsToError.put(new TopicPartition(topic.name(), 
partition), error)
+          }
+        }
+        val oldCallback = 
currentNodeAndTransactionData.callbacks(transactionData.transactionalId())
+        currentNodeAndTransactionData.transactionData.remove(transactionData)
+        oldCallback(topicPartitionsToError.toMap)
+      }
+      currentNodeAndTransactionData.transactionData.add(transactionData)
+      
currentNodeAndTransactionData.callbacks.put(transactionData.transactionalId(), 
callback)
+      wakeup()
+    }
+  }
+
+  private class AddPartitionsToTxnHandler(node: Node, 
transactionDataAndCallbacks: TransactionDataAndCallbacks) extends 
RequestCompletionHandler {
+    override def onComplete(response: ClientResponse): Unit = {
+      // Note: Synchronization is not needed on inflightNodes since it is 
always accessed from this thread.
+      inflightNodes.remove(node)
+      if (response.authenticationException() != null) {
+        error(s"AddPartitionsToTxnRequest failed for broker ${config.brokerId} 
with an " +
+          "authentication exception.", response.authenticationException)
+        transactionDataAndCallbacks.callbacks.foreach { case (txnId, callback) 
=>
+          callback(buildErrorMap(txnId, 
Errors.forException(response.authenticationException()).code()))
+        }
+      } else if (response.versionMismatch != null) {
+        // We may see unsupported version exception if we try to send a verify 
only request to a broker that can't handle it. 
+        // In this case, skip verification.
+        warn(s"AddPartitionsToTxnRequest failed for broker ${config.brokerId} 
with invalid version exception. This suggests verification is not supported." +
+          s"Continuing handling the produce request.")
+        transactionDataAndCallbacks.callbacks.values.foreach(_(Map.empty))
+      } else if (response.wasDisconnected() || response.wasTimedOut()) {
+        warn(s"AddPartitionsToTxnRequest failed for broker ${config.brokerId} 
with a network exception.")

Review Comment:
   It's probably used for to log the destination broker on which the request 
fails. Ditto in other logging.



-- 
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: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to