dajac commented on a change in pull request #9611: URL: https://github.com/apache/kafka/pull/9611#discussion_r531109073
########## File path: core/src/main/resources/common/message/TransactionLogKey.json ########## @@ -0,0 +1,23 @@ +// 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. + +{ + "type": "data", + "name": "TransactionLogKey", + "validVersions": "0", + "fields": [ + { "name": "transactionalId", "type": "string", "versions": "0"} Review comment: nit: We usually capitalize the first letter. We should do it in both schemas. ########## File path: core/src/main/scala/kafka/coordinator/transaction/TransactionLog.scala ########## @@ -146,38 +70,27 @@ object TransactionLog { * @return value payload bytes */ private[transaction] def valueToBytes(txnMetadata: TxnTransitMetadata): Array[Byte] = { - import ValueSchema._ - val value = new Struct(Current) - value.set(ProducerIdField, txnMetadata.producerId) - value.set(ProducerEpochField, txnMetadata.producerEpoch) - value.set(TxnTimeoutField, txnMetadata.txnTimeoutMs) - value.set(TxnStatusField, txnMetadata.txnState.byte) - value.set(TxnEntryTimestampField, txnMetadata.txnLastUpdateTimestamp) - value.set(TxnStartTimestampField, txnMetadata.txnStartTimestamp) - - if (txnMetadata.txnState == Empty) { - if (txnMetadata.topicPartitions.nonEmpty) + if (txnMetadata.txnState == Empty && txnMetadata.topicPartitions.nonEmpty) throw new IllegalStateException(s"Transaction is not expected to have any partitions since its state is ${txnMetadata.txnState}: $txnMetadata") - value.set(TxnPartitionsField, null) - } else { - // first group the topic partitions by their topic names - val topicAndPartitions = txnMetadata.topicPartitions.groupBy(_.topic()) - - val partitionArray = topicAndPartitions.map { case(topic, partitions) => - val topicPartitionsStruct = value.instance(TxnPartitionsField) - val partitionIds: Array[Integer] = partitions.map(topicPartition => Integer.valueOf(topicPartition.partition())).toArray - topicPartitionsStruct.set(PartitionsTopicField, topic) - topicPartitionsStruct.set(PartitionIdsField, partitionIds) - topicPartitionsStruct - } - value.set(TxnPartitionsField, partitionArray.toArray) - } - - val byteBuffer = ByteBuffer.allocate(2 /* version */ + value.sizeOf) - byteBuffer.putShort(CurrentVersion) - value.writeTo(byteBuffer) - byteBuffer.array() + val transactionPartition = if (txnMetadata.txnState == Empty) null + else txnMetadata.topicPartitions + .groupBy(_.topic()) Review comment: nit: Parentheses are not required when accessing an accessor. We could remove them here and in few other places in this file. ########## File path: core/src/main/scala/kafka/coordinator/transaction/TransactionLog.scala ########## @@ -146,38 +70,27 @@ object TransactionLog { * @return value payload bytes */ private[transaction] def valueToBytes(txnMetadata: TxnTransitMetadata): Array[Byte] = { - import ValueSchema._ - val value = new Struct(Current) - value.set(ProducerIdField, txnMetadata.producerId) - value.set(ProducerEpochField, txnMetadata.producerEpoch) - value.set(TxnTimeoutField, txnMetadata.txnTimeoutMs) - value.set(TxnStatusField, txnMetadata.txnState.byte) - value.set(TxnEntryTimestampField, txnMetadata.txnLastUpdateTimestamp) - value.set(TxnStartTimestampField, txnMetadata.txnStartTimestamp) - - if (txnMetadata.txnState == Empty) { - if (txnMetadata.topicPartitions.nonEmpty) + if (txnMetadata.txnState == Empty && txnMetadata.topicPartitions.nonEmpty) throw new IllegalStateException(s"Transaction is not expected to have any partitions since its state is ${txnMetadata.txnState}: $txnMetadata") - value.set(TxnPartitionsField, null) - } else { - // first group the topic partitions by their topic names - val topicAndPartitions = txnMetadata.topicPartitions.groupBy(_.topic()) - - val partitionArray = topicAndPartitions.map { case(topic, partitions) => - val topicPartitionsStruct = value.instance(TxnPartitionsField) - val partitionIds: Array[Integer] = partitions.map(topicPartition => Integer.valueOf(topicPartition.partition())).toArray - topicPartitionsStruct.set(PartitionsTopicField, topic) - topicPartitionsStruct.set(PartitionIdsField, partitionIds) - topicPartitionsStruct - } - value.set(TxnPartitionsField, partitionArray.toArray) - } - - val byteBuffer = ByteBuffer.allocate(2 /* version */ + value.sizeOf) - byteBuffer.putShort(CurrentVersion) - value.writeTo(byteBuffer) - byteBuffer.array() + val transactionPartition = if (txnMetadata.txnState == Empty) null + else txnMetadata.topicPartitions + .groupBy(_.topic()) + .map { case(topic, partitions) => + new TransactionLogValue.PartitionsSchema() + .setTopic(topic) + .setPartitionIds(partitions.map(_.partition()).map(Integer.valueOf).toList.asJava) Review comment: nit: It may be better to combine `map(_.partition()).map(Integer.valueOf)` in one map to avoid intermediate collection. ########## File path: core/src/main/scala/kafka/coordinator/transaction/TransactionLog.scala ########## @@ -146,38 +70,27 @@ object TransactionLog { * @return value payload bytes */ private[transaction] def valueToBytes(txnMetadata: TxnTransitMetadata): Array[Byte] = { - import ValueSchema._ - val value = new Struct(Current) - value.set(ProducerIdField, txnMetadata.producerId) - value.set(ProducerEpochField, txnMetadata.producerEpoch) - value.set(TxnTimeoutField, txnMetadata.txnTimeoutMs) - value.set(TxnStatusField, txnMetadata.txnState.byte) - value.set(TxnEntryTimestampField, txnMetadata.txnLastUpdateTimestamp) - value.set(TxnStartTimestampField, txnMetadata.txnStartTimestamp) - - if (txnMetadata.txnState == Empty) { - if (txnMetadata.topicPartitions.nonEmpty) + if (txnMetadata.txnState == Empty && txnMetadata.topicPartitions.nonEmpty) throw new IllegalStateException(s"Transaction is not expected to have any partitions since its state is ${txnMetadata.txnState}: $txnMetadata") - value.set(TxnPartitionsField, null) - } else { - // first group the topic partitions by their topic names - val topicAndPartitions = txnMetadata.topicPartitions.groupBy(_.topic()) - - val partitionArray = topicAndPartitions.map { case(topic, partitions) => - val topicPartitionsStruct = value.instance(TxnPartitionsField) - val partitionIds: Array[Integer] = partitions.map(topicPartition => Integer.valueOf(topicPartition.partition())).toArray - topicPartitionsStruct.set(PartitionsTopicField, topic) - topicPartitionsStruct.set(PartitionIdsField, partitionIds) - topicPartitionsStruct - } - value.set(TxnPartitionsField, partitionArray.toArray) - } - - val byteBuffer = ByteBuffer.allocate(2 /* version */ + value.sizeOf) - byteBuffer.putShort(CurrentVersion) - value.writeTo(byteBuffer) - byteBuffer.array() + val transactionPartition = if (txnMetadata.txnState == Empty) null + else txnMetadata.topicPartitions + .groupBy(_.topic()) + .map { case(topic, partitions) => Review comment: nit: We usually have a space after `case`. ########## File path: core/src/main/scala/kafka/coordinator/transaction/TransactionLog.scala ########## @@ -146,38 +70,27 @@ object TransactionLog { * @return value payload bytes */ private[transaction] def valueToBytes(txnMetadata: TxnTransitMetadata): Array[Byte] = { - import ValueSchema._ - val value = new Struct(Current) - value.set(ProducerIdField, txnMetadata.producerId) - value.set(ProducerEpochField, txnMetadata.producerEpoch) - value.set(TxnTimeoutField, txnMetadata.txnTimeoutMs) - value.set(TxnStatusField, txnMetadata.txnState.byte) - value.set(TxnEntryTimestampField, txnMetadata.txnLastUpdateTimestamp) - value.set(TxnStartTimestampField, txnMetadata.txnStartTimestamp) - - if (txnMetadata.txnState == Empty) { - if (txnMetadata.topicPartitions.nonEmpty) + if (txnMetadata.txnState == Empty && txnMetadata.topicPartitions.nonEmpty) throw new IllegalStateException(s"Transaction is not expected to have any partitions since its state is ${txnMetadata.txnState}: $txnMetadata") - value.set(TxnPartitionsField, null) - } else { - // first group the topic partitions by their topic names - val topicAndPartitions = txnMetadata.topicPartitions.groupBy(_.topic()) - - val partitionArray = topicAndPartitions.map { case(topic, partitions) => - val topicPartitionsStruct = value.instance(TxnPartitionsField) - val partitionIds: Array[Integer] = partitions.map(topicPartition => Integer.valueOf(topicPartition.partition())).toArray - topicPartitionsStruct.set(PartitionsTopicField, topic) - topicPartitionsStruct.set(PartitionIdsField, partitionIds) - topicPartitionsStruct - } - value.set(TxnPartitionsField, partitionArray.toArray) - } - - val byteBuffer = ByteBuffer.allocate(2 /* version */ + value.sizeOf) - byteBuffer.putShort(CurrentVersion) - value.writeTo(byteBuffer) - byteBuffer.array() + val transactionPartition = if (txnMetadata.txnState == Empty) null + else txnMetadata.topicPartitions + .groupBy(_.topic()) + .map { case(topic, partitions) => + new TransactionLogValue.PartitionsSchema() + .setTopic(topic) + .setPartitionIds(partitions.map(_.partition()).map(Integer.valueOf).toList.asJava) + }.toList.asJava + + MessageUtil.toBytes(TransactionLogValue.HIGHEST_SUPPORTED_VERSION, + new TransactionLogValue() + .setProducerId(txnMetadata.producerId) + .setProducerEpoch(txnMetadata.producerEpoch) + .setTransactionTimeoutMs(txnMetadata.txnTimeoutMs) + .setTransactionStatus(txnMetadata.txnState.byte) + .setTransactionLastUpdateTimestampMs(txnMetadata.txnLastUpdateTimestamp) + .setTransactionStartTimestampMs(txnMetadata.txnStartTimestamp) + .setTransactionPartitions(transactionPartition)) Review comment: nit: `transactionPartition` -> `transactionPartitions`? ---------------------------------------------------------------- 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: us...@infra.apache.org