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


##########
core/src/main/scala/kafka/server/AddPartitionsToTxnManager.scala:
##########
@@ -0,0 +1,173 @@
+/**
+ * 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.{InvalidRecordException, 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 java.util.Collections
+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 = {
+    // Check if we have already (either node or individual transaction). 
+    val currentNodeAndTransactionDataOpt = nodesToTransactions.get(node)
+    currentNodeAndTransactionDataOpt match {
+      case None =>
+        nodesToTransactions.put(node,
+          new TransactionDataAndCallbacks(new 
AddPartitionsToTxnTransactionCollection(Collections.singletonList(transactionData).iterator()),
+            mutable.Map(transactionData.transactionalId() -> callback)))
+      case Some(currentNodeAndTransactionData) =>
+        // Check if we already have txn ID -- this should only happen in epoch 
bump case. If so, we should return error for old entry and remove from queue.
+        val currentTransactionData = 
currentNodeAndTransactionData.transactionData.find(transactionData.transactionalId)
+        if (currentTransactionData != null) {
+          if (currentTransactionData.producerEpoch() < 
transactionData.producerEpoch()) {
+            val topicPartitionsToError = mutable.Map[TopicPartition, Errors]()
+            currentTransactionData.topics().forEach { topic => 
+              topic.partitions().forEach { partition =>
+                topicPartitionsToError.put(new TopicPartition(topic.name(), 
partition), Errors.INVALID_PRODUCER_EPOCH)
+              }
+            }
+            val oldCallback = 
currentNodeAndTransactionData.callbacks(transactionData.transactionalId())
+            
currentNodeAndTransactionData.transactionData.remove(transactionData)
+            oldCallback(topicPartitionsToError.toMap)
+          } else {
+            // We should never see a request on the same epoch since we 
haven't finished handling the one in queue

Review Comment:
   Yes, that's my understanding based on my reading of the code -- once a 
connection to a broker has at least one timed-out in-flight request, it's 
disconnected and all in-flight requests get a timeout error (which can be then 
retried by the producer on a new connection).  
https://github.com/apache/kafka/blob/trunk/clients/src/main/java/org/apache/kafka/clients/NetworkClient.java#L831
   
   It's also not guaranteed that the request that comes later here is the retry 
(i.e. the assumption that we can just fail the previous request because it 
must've timed out anyway is not true).  Should be unlikely with default 
settings, so we can probably just return some retriable error and document the 
caveat.  Alternatively (I would actually prefer that), we could just support 
multiple requests per transactional.id which would eliminate the need to handle 
this case here altogether: just add all new requests to the pending "batch" and 
let the TC handle different cases.



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