You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by GitBox <gi...@apache.org> on 2021/02/23 06:00:50 UTC

[GitHub] [kafka] chia7712 commented on a change in pull request #10183: KAFKA-12267; Implement `DescribeTransactions` API

chia7712 commented on a change in pull request #10183:
URL: https://github.com/apache/kafka/pull/10183#discussion_r580779375



##########
File path: clients/src/main/java/org/apache/kafka/common/requests/DescribeTransactionsRequest.java
##########
@@ -0,0 +1,83 @@
+/*
+ * 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 org.apache.kafka.common.requests;
+
+import org.apache.kafka.common.message.DescribeTransactionsRequestData;
+import org.apache.kafka.common.message.DescribeTransactionsResponseData;
+import org.apache.kafka.common.protocol.ApiKeys;
+import org.apache.kafka.common.protocol.ByteBufferAccessor;
+import org.apache.kafka.common.protocol.Errors;
+
+import java.nio.ByteBuffer;
+
+public class DescribeTransactionsRequest extends AbstractRequest {
+    public static class Builder extends AbstractRequest.Builder<DescribeTransactionsRequest> {
+        public final DescribeTransactionsRequestData data;
+
+        public Builder(DescribeTransactionsRequestData data) {
+            super(ApiKeys.DESCRIBE_TRANSACTIONS);
+            this.data = data;
+        }
+
+        @Override
+        public DescribeTransactionsRequest build(short version) {
+            return new DescribeTransactionsRequest(data, version);
+        }
+
+        @Override
+        public String toString() {
+            return data.toString();
+        }
+    }
+
+    private final DescribeTransactionsRequestData data;
+
+    private DescribeTransactionsRequest(DescribeTransactionsRequestData data, short version) {
+        super(ApiKeys.DESCRIBE_TRANSACTIONS, version);
+        this.data = data;
+    }
+
+    @Override
+    public DescribeTransactionsRequestData data() {
+        return data;
+    }
+
+    @Override
+    public DescribeTransactionsResponse getErrorResponse(int throttleTimeMs, Throwable e) {
+        Errors error = Errors.forException(e);
+        DescribeTransactionsResponseData response = new DescribeTransactionsResponseData();

Review comment:
       the `throttleTimeMs` is not added to response

##########
File path: core/src/main/scala/kafka/coordinator/transaction/TransactionCoordinator.scala
##########
@@ -255,6 +258,45 @@ class TransactionCoordinator(brokerId: Int,
     }
   }
 
+  def handleDescribeTransactions(
+    transactionalId: String
+  ): DescribeTransactionsResponseData.TransactionState = {
+    val transactionState = new DescribeTransactionsResponseData.TransactionState()
+      .setTransactionalId(transactionalId)
+
+    if (!isActive.get()) {
+      transactionState.setErrorCode(Errors.COORDINATOR_NOT_AVAILABLE.code)
+    } else if (transactionalId == null || transactionalId.isEmpty) {
+      transactionState.setErrorCode(Errors.INVALID_REQUEST.code)
+    } else {
+      txnManager.getTransactionState(transactionalId) match {
+        case Left(error) =>
+          transactionState.setErrorCode(error.code)
+        case Right(None) =>
+          transactionState.setErrorCode(Errors.TRANSACTIONAL_ID_NOT_FOUND.code)
+        case Right(Some(coordinatorEpochAndMetadata)) =>
+          val txnMetadata = coordinatorEpochAndMetadata.transactionMetadata
+          txnMetadata.inLock {
+            val partitionsByTopic = CollectionUtils.groupPartitionsByTopic(txnMetadata.topicPartitions.asJava)
+            partitionsByTopic.forEach { (topic, partitions) =>
+              val topicData = new DescribeTransactionsResponseData.TopicData()
+                .setTopic(topic)
+                .setPartitions(partitions)
+              transactionState.topics.add(topicData)
+            }
+
+            transactionState
+              .setErrorCode(Errors.NONE.code)
+              .setProducerId(txnMetadata.producerId)
+              .setProducerEpoch(txnMetadata.producerEpoch)
+              .setTransactionState(txnMetadata.state.toString)

Review comment:
       As it is a part of serialized data, should we add constant string to those enums instead of calling `toString`?

##########
File path: core/src/main/scala/kafka/server/KafkaApis.scala
##########
@@ -3271,6 +3272,34 @@ class KafkaApis(val requestChannel: RequestChannel,
       "Apache ZooKeeper mode.")
   }
 
+  def handleDescribeTransactionsRequest(request: RequestChannel.Request): Unit = {
+    val describeTransactionsRequest = request.body[DescribeTransactionsRequest]
+    val response = new DescribeTransactionsResponseData()
+
+    describeTransactionsRequest.data.transactionalIds.forEach { transactionalId =>
+      val transactionState = if (!authHelper.authorize(request.context, DESCRIBE, TRANSACTIONAL_ID, transactionalId)) {
+        new DescribeTransactionsResponseData.TransactionState()
+          .setTransactionalId(transactionalId)
+          .setErrorCode(Errors.TRANSACTIONAL_ID_AUTHORIZATION_FAILED.code)
+      } else {
+        txnCoordinator.handleDescribeTransactions(transactionalId)
+      }
+
+      // Include only partitions which the principal is authorized to describe

Review comment:
       Why not converting to `TOPIC_AUTHORIZATION_FAILED`? Also, the data excluding non-authorized topics is a bit weird as it is in-completed result to callers.

##########
File path: core/src/main/scala/kafka/coordinator/transaction/TransactionCoordinator.scala
##########
@@ -255,6 +258,45 @@ class TransactionCoordinator(brokerId: Int,
     }
   }
 
+  def handleDescribeTransactions(
+    transactionalId: String
+  ): DescribeTransactionsResponseData.TransactionState = {
+    val transactionState = new DescribeTransactionsResponseData.TransactionState()
+      .setTransactionalId(transactionalId)
+
+    if (!isActive.get()) {
+      transactionState.setErrorCode(Errors.COORDINATOR_NOT_AVAILABLE.code)
+    } else if (transactionalId == null || transactionalId.isEmpty) {
+      transactionState.setErrorCode(Errors.INVALID_REQUEST.code)
+    } else {
+      txnManager.getTransactionState(transactionalId) match {
+        case Left(error) =>
+          transactionState.setErrorCode(error.code)
+        case Right(None) =>
+          transactionState.setErrorCode(Errors.TRANSACTIONAL_ID_NOT_FOUND.code)
+        case Right(Some(coordinatorEpochAndMetadata)) =>
+          val txnMetadata = coordinatorEpochAndMetadata.transactionMetadata
+          txnMetadata.inLock {
+            val partitionsByTopic = CollectionUtils.groupPartitionsByTopic(txnMetadata.topicPartitions.asJava)

Review comment:
       How about using `mapKey` to eliminate this re-group collection?




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

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