brandboat commented on code in PR #18448: URL: https://github.com/apache/kafka/pull/18448#discussion_r1911996588
########## core/src/test/scala/integration/kafka/coordinator/transaction/ProducerIntegrationTest.scala: ########## @@ -0,0 +1,185 @@ +/** + * 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.coordinator.transaction + +import kafka.network.SocketServer +import kafka.server.IntegrationTestUtils +import org.apache.kafka.clients.admin.{Admin, TransactionState} +import org.apache.kafka.clients.consumer.{Consumer, ConsumerConfig, ConsumerRecords, OffsetAndMetadata} +import org.apache.kafka.clients.producer.{Producer, ProducerConfig, ProducerRecord} +import org.apache.kafka.common.TopicPartition +import org.apache.kafka.common.test.api.{ClusterConfigProperty, ClusterFeature, ClusterInstance, ClusterTest, ClusterTestDefaults, ClusterTestExtensions, ClusterTests, Type} +import org.apache.kafka.common.message.InitProducerIdRequestData +import org.apache.kafka.common.network.ListenerName +import org.apache.kafka.common.protocol.Errors +import org.apache.kafka.common.record.RecordBatch +import org.apache.kafka.common.requests.{InitProducerIdRequest, InitProducerIdResponse} +import org.apache.kafka.common.test.TestUtils +import org.apache.kafka.coordinator.group.GroupCoordinatorConfig +import org.apache.kafka.coordinator.transaction.TransactionLogConfig +import org.apache.kafka.server.common.{Feature, MetadataVersion} +import org.junit.jupiter.api.Assertions.{assertEquals, assertTrue} +import org.junit.jupiter.api.extension.ExtendWith + +import java.time.Duration +import java.util +import java.util.Collections +import java.util.stream.{Collectors, IntStream, StreamSupport} +import scala.concurrent.duration.DurationInt +import scala.jdk.CollectionConverters._ + +@ClusterTestDefaults(types = Array(Type.KRAFT), serverProperties = Array( + new ClusterConfigProperty(key = TransactionLogConfig.TRANSACTIONS_TOPIC_REPLICATION_FACTOR_CONFIG, value = "1"), + new ClusterConfigProperty(key = TransactionLogConfig.TRANSACTIONS_TOPIC_PARTITIONS_CONFIG, value = "1"), + new ClusterConfigProperty(key = TransactionLogConfig.TRANSACTIONS_TOPIC_MIN_ISR_CONFIG, value = "1"), + new ClusterConfigProperty(key = GroupCoordinatorConfig.OFFSETS_TOPIC_REPLICATION_FACTOR_CONFIG, value = "1"), + new ClusterConfigProperty(key = GroupCoordinatorConfig.OFFSETS_TOPIC_PARTITIONS_CONFIG, value = "1"), +)) +@ExtendWith(value = Array(classOf[ClusterTestExtensions])) +class ProducerIntegrationTest { + + @ClusterTests(Array( + new ClusterTest(metadataVersion = MetadataVersion.IBP_3_3_IV0) + )) + def testUniqueProducerIds(clusterInstance: ClusterInstance): Unit = { + verifyUniqueIds(clusterInstance) + } + + @ClusterTests(Array( + new ClusterTest(features = Array( + new ClusterFeature(feature = Feature.TRANSACTION_VERSION, version = 0))), + new ClusterTest(features = Array( + new ClusterFeature(feature = Feature.TRANSACTION_VERSION, version = 1))), + new ClusterTest(features = Array( + new ClusterFeature(feature = Feature.TRANSACTION_VERSION, version = 2))), + )) + def testTransactionWithAndWithoutSend(cluster: ClusterInstance): Unit = { + val properties = new util.HashMap[String, Object] + properties.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, "foobar") + properties.put(ProducerConfig.CLIENT_ID_CONFIG, "test") + properties.put(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, "true") + val producer: Producer[Array[Byte], Array[Byte]] = cluster.producer(properties) + try { + producer.initTransactions() + producer.beginTransaction() + producer.send(new ProducerRecord[Array[Byte], Array[Byte]]("test", "key".getBytes, "value".getBytes)) + producer.commitTransaction() + + producer.beginTransaction() + producer.commitTransaction() + } finally if (producer != null) producer.close() + } + + @ClusterTests(Array( + new ClusterTest(features = Array( + new ClusterFeature(feature = Feature.TRANSACTION_VERSION, version = 0))), + new ClusterTest(features = Array( + new ClusterFeature(feature = Feature.TRANSACTION_VERSION, version = 1))), + new ClusterTest(features = Array( + new ClusterFeature(feature = Feature.TRANSACTION_VERSION, version = 2))), + )) + def testTransactionWithSendOffset(cluster: ClusterInstance): Unit = { Review Comment: > Does this test new behavior/one related to the change? Yes, I want to ensure that if a transaction with sendOffsetToTransaction triggered and no sendRecords are fired, then executing commitTransaction should not raise any errors. And on the server side, the transaction state should be COMPLETE_COMMIT, which means the END_TXN request has been sent to the broker. > The one other thing I was curious about was a test where we try to send a record, it fails to be added and we should be able to abort, but a commit (in the case where a partition was not added) will fail. That one might be tricky to code -- especially the part where we fail to add the partition on the server side. Hmm... I need some time to figure out how to make adding the partition fail on server side. Once that's done, I think I can come up with the test you mentioned. Thanks for the suggestion! -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
