mimaison commented on code in PR #13393: URL: https://github.com/apache/kafka/pull/13393#discussion_r1146437275
########## clients/src/main/java/org/apache/kafka/tools/RecordReader.java: ########## @@ -0,0 +1,51 @@ +/* + * 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.tools; + +import org.apache.kafka.clients.producer.ProducerRecord; +import org.apache.kafka.common.Configurable; + +import java.io.Closeable; +import java.io.InputStream; +import java.util.Iterator; +import java.util.Map; + +/** + * Typical implementations of this interface convert data from an `InputStream` received via `readRecords` into a + * iterator of `ProducerRecord` instance. Noted that the implementations to have a public nullary constructor. Review Comment: `Noted that the implementations to have a public nullary constructor.` -> `Note that implementations must have a public nullary constructor.` ########## clients/src/main/java/org/apache/kafka/tools/RecordReader.java: ########## @@ -0,0 +1,51 @@ +/* + * 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.tools; + +import org.apache.kafka.clients.producer.ProducerRecord; +import org.apache.kafka.common.Configurable; + +import java.io.Closeable; +import java.io.InputStream; +import java.util.Iterator; +import java.util.Map; + +/** + * Typical implementations of this interface convert data from an `InputStream` received via `readRecords` into a Review Comment: Can we use javadoc links instead of backticks? ########## core/src/main/scala/kafka/tools/ConsoleProducer.scala: ########## @@ -24,33 +24,81 @@ import java.util.regex.Pattern import joptsimple.{OptionException, OptionParser, OptionSet} import kafka.common.MessageReader import kafka.utils.Implicits._ -import kafka.utils.{Exit, ToolsUtils} +import kafka.utils.{Exit, Logging, ToolsUtils} import org.apache.kafka.clients.producer.internals.ErrorLoggingCallback -import org.apache.kafka.clients.producer.{KafkaProducer, ProducerConfig, ProducerRecord} +import org.apache.kafka.clients.producer.{KafkaProducer, Producer, ProducerConfig, ProducerRecord} import org.apache.kafka.common.KafkaException import org.apache.kafka.common.record.CompressionType import org.apache.kafka.common.utils.Utils import org.apache.kafka.server.util.{CommandDefaultOptions, CommandLineUtils} +import org.apache.kafka.tools.RecordReader + +import scala.annotation.nowarn + +@nowarn("cat=deprecation") +object ConsoleProducer extends Logging { + + private[tools] def newReader(className: String, prop: Properties): RecordReader = { + val reader = Class.forName(className).getDeclaredConstructor().newInstance() + reader match { + case r: RecordReader => + r.configure(prop.asInstanceOf[java.util.Map[String, _]]) + r + case r: MessageReader => + logger.warn("MessageReader is deprecated. Please use org.apache.kafka.server.tools.RecordReader instead") + new RecordReader { + private[this] var initialized = false + + override def readRecords(inputStream: InputStream): java.util.Iterator[ProducerRecord[Array[Byte], Array[Byte]]] = { + if (initialized) throw new IllegalStateException("It is invalid to call readRecords again when the reader is based on deprecated MessageReader") + if (!initialized) { + r.init(inputStream, prop) + initialized = true + } + new java.util.Iterator[ProducerRecord[Array[Byte], Array[Byte]]] { + private[this] var current: ProducerRecord[Array[Byte], Array[Byte]] = null Review Comment: Can we use `_` instead of null here? ########## core/src/main/scala/kafka/tools/ConsoleProducer.scala: ########## @@ -24,33 +24,81 @@ import java.util.regex.Pattern import joptsimple.{OptionException, OptionParser, OptionSet} import kafka.common.MessageReader import kafka.utils.Implicits._ -import kafka.utils.{Exit, ToolsUtils} +import kafka.utils.{Exit, Logging, ToolsUtils} import org.apache.kafka.clients.producer.internals.ErrorLoggingCallback -import org.apache.kafka.clients.producer.{KafkaProducer, ProducerConfig, ProducerRecord} +import org.apache.kafka.clients.producer.{KafkaProducer, Producer, ProducerConfig, ProducerRecord} import org.apache.kafka.common.KafkaException import org.apache.kafka.common.record.CompressionType import org.apache.kafka.common.utils.Utils import org.apache.kafka.server.util.{CommandDefaultOptions, CommandLineUtils} +import org.apache.kafka.tools.RecordReader + +import scala.annotation.nowarn + +@nowarn("cat=deprecation") +object ConsoleProducer extends Logging { + + private[tools] def newReader(className: String, prop: Properties): RecordReader = { + val reader = Class.forName(className).getDeclaredConstructor().newInstance() + reader match { + case r: RecordReader => + r.configure(prop.asInstanceOf[java.util.Map[String, _]]) + r + case r: MessageReader => + logger.warn("MessageReader is deprecated. Please use org.apache.kafka.server.tools.RecordReader instead") Review Comment: Should we update `LineMessageReader` to implement the new interface? Otherwise this message will print for all users not providing their custom reader (which is most users). ########## core/src/test/scala/unit/kafka/tools/ConsoleProducerTest.scala: ########## @@ -220,4 +227,61 @@ class ConsoleProducerTest { producerConfig.getInt(ProducerConfig.BATCH_SIZE_CONFIG)) } + @Test + def testNewReader(): Unit = { + ConsoleProducerTest.configureCount = 0 + ConsoleProducerTest.closeCount = 0 + val reader = ConsoleProducer.newReader(classOf[ConsoleProducerTest.DumbMessageReader].getName, new Properties()) + // the deprecated MessageReader get configured when creating records + assertEquals(0, ConsoleProducerTest.configureCount) + reader.readRecords(System.in) + assertEquals(1, ConsoleProducerTest.configureCount) + assertEquals(0, ConsoleProducerTest.closeCount) + assertThrows(classOf[IllegalStateException], () => reader.readRecords(System.in)) + reader.close() + assertEquals(1, ConsoleProducerTest.closeCount) + + ConsoleProducerTest.configureCount = 0 + ConsoleProducerTest.closeCount = 0 + + val reader1 = ConsoleProducer.newReader(classOf[ConsoleProducerTest.DumbRecordReader].getName, new Properties()) + assertEquals(1, ConsoleProducerTest.configureCount) + assertEquals(0, ConsoleProducerTest.closeCount) + reader1.close() + assertEquals(1, ConsoleProducerTest.closeCount) + } + + @Test + def testLoopReader(): Unit = { + ConsoleProducerTest.configureCount = 0 + ConsoleProducerTest.closeCount = 0 + val reader = ConsoleProducer.newReader(classOf[ConsoleProducerTest.DumbRecordReader].getName, new Properties()) + + ConsoleProducer.loopReader(Mockito.mock(classOf[Producer[Array[Byte], Array[Byte]]]), + reader, System.in, false) + + assertEquals(1, ConsoleProducerTest.configureCount) + assertEquals(1, ConsoleProducerTest.closeCount) + } +} + +@nowarn("cat=deprecation") +object ConsoleProducerTest { + var configureCount = 0 + var closeCount = 0 + class DumbMessageReader extends MessageReader { + override def init(inputStream: InputStream, props: Properties): Unit = configureCount += 1 + override def readMessage(): ProducerRecord[Array[Byte], Array[Byte]] = null + + override def close(): Unit = closeCount += 1 + + } + + class DumbRecordReader extends RecordReader { Review Comment: Would `TestRecordReader`/`SimpleRecordReader` be more appropriate names? ########## core/src/main/scala/kafka/tools/ConsoleProducer.scala: ########## @@ -24,33 +24,81 @@ import java.util.regex.Pattern import joptsimple.{OptionException, OptionParser, OptionSet} import kafka.common.MessageReader import kafka.utils.Implicits._ -import kafka.utils.{Exit, ToolsUtils} +import kafka.utils.{Exit, Logging, ToolsUtils} import org.apache.kafka.clients.producer.internals.ErrorLoggingCallback -import org.apache.kafka.clients.producer.{KafkaProducer, ProducerConfig, ProducerRecord} +import org.apache.kafka.clients.producer.{KafkaProducer, Producer, ProducerConfig, ProducerRecord} import org.apache.kafka.common.KafkaException import org.apache.kafka.common.record.CompressionType import org.apache.kafka.common.utils.Utils import org.apache.kafka.server.util.{CommandDefaultOptions, CommandLineUtils} +import org.apache.kafka.tools.RecordReader + +import scala.annotation.nowarn + +@nowarn("cat=deprecation") +object ConsoleProducer extends Logging { + + private[tools] def newReader(className: String, prop: Properties): RecordReader = { + val reader = Class.forName(className).getDeclaredConstructor().newInstance() + reader match { + case r: RecordReader => + r.configure(prop.asInstanceOf[java.util.Map[String, _]]) + r + case r: MessageReader => + logger.warn("MessageReader is deprecated. Please use org.apache.kafka.server.tools.RecordReader instead") Review Comment: What about `The MessageReader interface is deprecated.`? The package is `org.apache.kafka.tools`, without `server`. ########## core/src/test/scala/unit/kafka/tools/ConsoleProducerTest.scala: ########## @@ -220,4 +227,61 @@ class ConsoleProducerTest { producerConfig.getInt(ProducerConfig.BATCH_SIZE_CONFIG)) } + @Test + def testNewReader(): Unit = { + ConsoleProducerTest.configureCount = 0 + ConsoleProducerTest.closeCount = 0 + val reader = ConsoleProducer.newReader(classOf[ConsoleProducerTest.DumbMessageReader].getName, new Properties()) + // the deprecated MessageReader get configured when creating records + assertEquals(0, ConsoleProducerTest.configureCount) + reader.readRecords(System.in) + assertEquals(1, ConsoleProducerTest.configureCount) + assertEquals(0, ConsoleProducerTest.closeCount) + assertThrows(classOf[IllegalStateException], () => reader.readRecords(System.in)) + reader.close() + assertEquals(1, ConsoleProducerTest.closeCount) + + ConsoleProducerTest.configureCount = 0 + ConsoleProducerTest.closeCount = 0 + + val reader1 = ConsoleProducer.newReader(classOf[ConsoleProducerTest.DumbRecordReader].getName, new Properties()) + assertEquals(1, ConsoleProducerTest.configureCount) + assertEquals(0, ConsoleProducerTest.closeCount) + reader1.close() + assertEquals(1, ConsoleProducerTest.closeCount) + } + + @Test + def testLoopReader(): Unit = { + ConsoleProducerTest.configureCount = 0 + ConsoleProducerTest.closeCount = 0 + val reader = ConsoleProducer.newReader(classOf[ConsoleProducerTest.DumbRecordReader].getName, new Properties()) + + ConsoleProducer.loopReader(Mockito.mock(classOf[Producer[Array[Byte], Array[Byte]]]), + reader, System.in, false) + + assertEquals(1, ConsoleProducerTest.configureCount) + assertEquals(1, ConsoleProducerTest.closeCount) + } +} + +@nowarn("cat=deprecation") +object ConsoleProducerTest { + var configureCount = 0 + var closeCount = 0 + class DumbMessageReader extends MessageReader { Review Comment: Would `TestMessageReader`/`SimpleMessageReader` be more appropriate names? ########## clients/src/main/java/org/apache/kafka/tools/RecordReader.java: ########## @@ -0,0 +1,51 @@ +/* + * 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.tools; + +import org.apache.kafka.clients.producer.ProducerRecord; +import org.apache.kafka.common.Configurable; + +import java.io.Closeable; +import java.io.InputStream; +import java.util.Iterator; +import java.util.Map; + +/** + * Typical implementations of this interface convert data from an `InputStream` received via `readRecords` into a Review Comment: `a iterator` -> `an iterator` There's 2 more instances below too. -- 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