artemlivshits commented on code in PR #13391: URL: https://github.com/apache/kafka/pull/13391#discussion_r1152624055
########## core/src/main/scala/kafka/server/AddPartitionsToTxnManager.scala: ########## @@ -0,0 +1,170 @@ +/** + * 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 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 (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 -- this should only happen in epoch bump case. If so, we should return error for old entry and remove from queue. + 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 + throw new InvalidRecordException("Received a second request from the same connection without finishing the first.") + } + } + 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 = { + 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, transactionDataAndCallbacks.transactionData, 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. + error(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 { + val addPartitionsToTxnResponseData = response.responseBody.asInstanceOf[AddPartitionsToTxnResponse].data + if (addPartitionsToTxnResponseData.errorCode != 0) { + error(s"AddPartitionsToTxnRequest for broker ${config.brokerId} returned with error ${Errors.forCode(addPartitionsToTxnResponseData.errorCode)}.") + // TODO: send error back correctly -- we need to verify all possible errors can be handled by the client. + // errors -- versionmismatch --> handled above + // -- clusterauth --> should handle differently + transactionDataAndCallbacks.callbacks.foreach { case (txnId, callback) => + callback(buildErrorMap(txnId, transactionDataAndCallbacks.transactionData, addPartitionsToTxnResponseData.errorCode())) + } + } else { + addPartitionsToTxnResponseData.resultsByTransaction().forEach { transactionResult => Review Comment: The KIP also mentions a race condition where the transaction may be aborted just after we've verified it, but before we got the reply, it doesn't seem like this PR addresses that, do we plan to do it in a different PR? ########## core/src/main/scala/kafka/server/AddPartitionsToTxnManager.scala: ########## @@ -0,0 +1,170 @@ +/** + * 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 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 (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 -- this should only happen in epoch bump case. If so, we should return error for old entry and remove from queue. + 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 + throw new InvalidRecordException("Received a second request from the same connection without finishing the first.") + } + } + 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 = { + inflightNodes.remove(node) Review Comment: If we'll have a thread pool of network threads, we'd need to synchronize this too. So we probably need to add a comment here. ########## core/src/main/scala/kafka/server/AddPartitionsToTxnManager.scala: ########## @@ -0,0 +1,170 @@ +/** + * 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 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 (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 -- this should only happen in epoch bump case. If so, we should return error for old entry and remove from queue. + 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 + throw new InvalidRecordException("Received a second request from the same connection without finishing the first.") + } + } + 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 = { + 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, transactionDataAndCallbacks.transactionData, 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. + error(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 { + val addPartitionsToTxnResponseData = response.responseBody.asInstanceOf[AddPartitionsToTxnResponse].data + if (addPartitionsToTxnResponseData.errorCode != 0) { + error(s"AddPartitionsToTxnRequest for broker ${config.brokerId} returned with error ${Errors.forCode(addPartitionsToTxnResponseData.errorCode)}.") + // TODO: send error back correctly -- we need to verify all possible errors can be handled by the client. + // errors -- versionmismatch --> handled above + // -- clusterauth --> should handle differently + transactionDataAndCallbacks.callbacks.foreach { case (txnId, callback) => + callback(buildErrorMap(txnId, transactionDataAndCallbacks.transactionData, addPartitionsToTxnResponseData.errorCode())) + } + } else { + addPartitionsToTxnResponseData.resultsByTransaction().forEach { transactionResult => + val unverified = mutable.Map[TopicPartition, Errors]() + transactionResult.topicResults().forEach { topicResult => + topicResult.resultsByPartition().forEach { partitionResult => + val tp = new TopicPartition(topicResult.name(), partitionResult.partitionIndex()) + if (partitionResult.partitionErrorCode() != Errors.NONE.code()) { + // Producers expect to handle INVALID_PRODUCER_EPOCH in this scenario. + val code = + if (partitionResult.partitionErrorCode() == Errors.PRODUCER_FENCED.code()) + Errors.INVALID_PRODUCER_EPOCH.code() + else + partitionResult.partitionErrorCode() + unverified.put(tp, Errors.forCode(code)) + } + } + } + val callback = transactionDataAndCallbacks.callbacks(transactionResult.transactionalId()) + callback(unverified.toMap) + } + } + } + wakeup() + } + + private def buildErrorMap(transactionalId: String, addPartitionsToTxnCollection: AddPartitionsToTxnTransactionCollection, errorCode: Short): Map[TopicPartition, Errors] = { + val errors = new mutable.HashMap[TopicPartition, Errors]() + val transactionData = transactionDataAndCallbacks.transactionData.find(transactionalId) + transactionData.topics.forEach { topic => + topic.partitions().forEach { partition => + errors.put(new TopicPartition(topic.name(), partition), Errors.forCode(errorCode)) + } + } + errors.toMap + } + } + + override def shutdown(): Unit = { + super.shutdown() + networkClient.close() + } + + override def generateRequests(): Iterable[RequestAndCompletionHandler] = { + + // build and add requests to queue + val buffer = mutable.Buffer[RequestAndCompletionHandler]() + val currentTimeMs = time.milliseconds() + val removedNodes = mutable.Set[Node]() + nodesToTransactions.foreach { case (node, transactionDataAndCallbacks) => Review Comment: Synchronization? ########## 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 + throw new InvalidRecordException("Received a second request from the same connection without finishing the first.") + } + } + 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 = { Review Comment: Actually, this is a higher level discussion that we should probably have on the KIP thread, as it affects public metric semantics. I think it would be useful to have a separate metric, because it'll help with diagnosing extra latency in transactions that we're introducing with this RPC call. Especially if we provide a feature flag to turn off this check for perf reasons -- we'd need to have some data to help the admin make a decision. -- 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