hachikuji commented on code in PR #13607: URL: https://github.com/apache/kafka/pull/13607#discussion_r1184131575
########## core/src/main/scala/kafka/server/ReplicaManager.scala: ########## @@ -637,17 +637,31 @@ class ReplicaManager(val config: KafkaConfig, if (isValidRequiredAcks(requiredAcks)) { val sTime = time.milliseconds + val transactionalProducerIds = mutable.HashSet[Long]() val (verifiedEntriesPerPartition, notYetVerifiedEntriesPerPartition) = if (transactionStatePartition.isEmpty || !config.transactionPartitionVerificationEnable) (entriesPerPartition, Map.empty) - else + else { entriesPerPartition.partition { case (topicPartition, records) => - getPartitionOrException(topicPartition).hasOngoingTransaction(records.firstBatch().producerId()) + // Produce requests (only requests that require verification) should only have one batch per partition in "batches" but check all just to be safe. + val transactionalBatches = records.batches.asScala.filter(batch => batch.hasProducerId && batch.isTransactional) + transactionalBatches.map(_.producerId()).toSet.foreach(transactionalProducerIds.add(_)) + if (transactionalBatches.nonEmpty) { + getPartitionOrException(topicPartition).hasOngoingTransaction(transactionalBatches.head.producerId) + } else { + // If there is no producer ID in the batches, no need to verify. + true + } } + } + // We should have exactly one producer ID for transactional records + if (transactionalProducerIds.size > 1) { + throw new InvalidRecordException("Transactional records contained more than one producer ID") Review Comment: Would it make sense to return `InvalidProducerIdMapping`? ########## core/src/main/scala/kafka/server/ReplicaManager.scala: ########## @@ -637,17 +637,31 @@ class ReplicaManager(val config: KafkaConfig, if (isValidRequiredAcks(requiredAcks)) { val sTime = time.milliseconds + val transactionalProducerIds = mutable.HashSet[Long]() val (verifiedEntriesPerPartition, notYetVerifiedEntriesPerPartition) = if (transactionStatePartition.isEmpty || !config.transactionPartitionVerificationEnable) (entriesPerPartition, Map.empty) - else + else { entriesPerPartition.partition { case (topicPartition, records) => - getPartitionOrException(topicPartition).hasOngoingTransaction(records.firstBatch().producerId()) + // Produce requests (only requests that require verification) should only have one batch per partition in "batches" but check all just to be safe. + val transactionalBatches = records.batches.asScala.filter(batch => batch.hasProducerId && batch.isTransactional) + transactionalBatches.map(_.producerId()).toSet.foreach(transactionalProducerIds.add(_)) Review Comment: nit: is the `toSet` necessary? Maybe we can simplify: ```scala transactionalBatches.foreach(batch => transactionalProducerIds.add(batch.producerId)) ``` -- 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