@@ -29,12 +29,15 @@ import scala.util.Random
2929
3030import kafka .admin .AdminUtils
3131import kafka .api .Request
32- import kafka .common . TopicAndPartition
33- import kafka .server .{ KafkaConfig , KafkaServer , OffsetCheckpoint }
32+ import kafka .server .{ KafkaConfig , KafkaServer }
33+ import kafka .server .checkpoints . OffsetCheckpointFile
3434import kafka .utils .ZkUtils
35+ import org .apache .kafka .clients .CommonClientConfigs
36+ import org .apache .kafka .clients .admin .{AdminClient , CreatePartitionsOptions , NewPartitions }
3537import org .apache .kafka .clients .consumer .KafkaConsumer
3638import org .apache .kafka .clients .producer ._
3739import org .apache .kafka .common .TopicPartition
40+ import org .apache .kafka .common .network .ListenerName
3841import org .apache .kafka .common .serialization .{StringDeserializer , StringSerializer }
3942import org .apache .zookeeper .server .{NIOServerCnxnFactory , ZooKeeperServer }
4043import org .scalatest .concurrent .Eventually ._
@@ -61,6 +64,7 @@ class KafkaTestUtils(withBrokerProps: Map[String, Object] = Map.empty) extends L
6164 private var zookeeper : EmbeddedZookeeper = _
6265
6366 private var zkUtils : ZkUtils = _
67+ private var adminClient : AdminClient = null
6468
6569 // Kafka broker related configurations
6670 private val brokerHost = " localhost"
@@ -113,17 +117,23 @@ class KafkaTestUtils(withBrokerProps: Map[String, Object] = Map.empty) extends L
113117 brokerConf = new KafkaConfig (brokerConfiguration, doLog = false )
114118 server = new KafkaServer (brokerConf)
115119 server.startup()
116- brokerPort = server.boundPort()
120+ brokerPort = server.boundPort(new ListenerName ( " PLAINTEXT " ) )
117121 (server, brokerPort)
118122 }, new SparkConf (), " KafkaBroker" )
119123
120124 brokerReady = true
125+ val props = new Properties ()
126+ props.put(CommonClientConfigs .BOOTSTRAP_SERVERS_CONFIG , s " $brokerHost: $brokerPort" )
127+ adminClient = AdminClient .create(props)
121128 }
122129
123130 /** setup the whole embedded servers, including Zookeeper and Kafka brokers */
124131 def setup (): Unit = {
125132 setupEmbeddedZookeeper()
126133 setupEmbeddedKafkaServer()
134+ eventually(timeout(60 .seconds)) {
135+ assert(zkUtils.getAllBrokersInCluster().nonEmpty, " Broker was not up in 60 seconds" )
136+ }
127137 }
128138
129139 /** Teardown the whole servers, including Kafka broker and Zookeeper */
@@ -203,7 +213,9 @@ class KafkaTestUtils(withBrokerProps: Map[String, Object] = Map.empty) extends L
203213
204214 /** Add new partitions to a Kafka topic */
205215 def addPartitions (topic : String , partitions : Int ): Unit = {
206- AdminUtils .addPartitions(zkUtils, topic, partitions)
216+ adminClient.createPartitions(
217+ Map (topic -> NewPartitions .increaseTo(partitions)).asJava,
218+ new CreatePartitionsOptions )
207219 // wait until metadata is propagated
208220 (0 until partitions).foreach { p =>
209221 waitUntilMetadataIsPropagated(topic, p)
@@ -296,6 +308,8 @@ class KafkaTestUtils(withBrokerProps: Map[String, Object] = Map.empty) extends L
296308 props.put(" replica.socket.timeout.ms" , " 1500" )
297309 props.put(" delete.topic.enable" , " true" )
298310 props.put(" offsets.topic.num.partitions" , " 1" )
311+ props.put(" offsets.topic.replication.factor" , " 1" )
312+ props.put(" group.initial.rebalance.delay.ms" , " 10" )
299313 // Can not use properties.putAll(propsMap.asJava) in scala-2.12
300314 // See https://github.com/scala/bug/issues/10418
301315 withBrokerProps.foreach { case (k, v) => props.put(k, v) }
@@ -327,7 +341,7 @@ class KafkaTestUtils(withBrokerProps: Map[String, Object] = Map.empty) extends L
327341 topic : String ,
328342 numPartitions : Int ,
329343 servers : Seq [KafkaServer ]): Unit = {
330- val topicAndPartitions = (0 until numPartitions).map(TopicAndPartition (topic, _))
344+ val topicAndPartitions = (0 until numPartitions).map(new TopicPartition (topic, _))
331345
332346 import ZkUtils ._
333347 // wait until admin path for delete topic is deleted, signaling completion of topic deletion
@@ -337,16 +351,16 @@ class KafkaTestUtils(withBrokerProps: Map[String, Object] = Map.empty) extends L
337351 assert(! zkUtils.pathExists(getTopicPath(topic)), s " ${getTopicPath(topic)} still exists " )
338352 // ensure that the topic-partition has been deleted from all brokers' replica managers
339353 assert(servers.forall(server => topicAndPartitions.forall(tp =>
340- server.replicaManager.getPartition(tp.topic, tp.partition ) == None )),
354+ server.replicaManager.getPartition(tp) == None )),
341355 s " topic $topic still exists in the replica manager " )
342356 // ensure that logs from all replicas are deleted if delete topic is marked successful
343357 assert(servers.forall(server => topicAndPartitions.forall(tp =>
344358 server.getLogManager().getLog(tp).isEmpty)),
345359 s " topic $topic still exists in log mananger " )
346360 // ensure that topic is removed from all cleaner offsets
347361 assert(servers.forall(server => topicAndPartitions.forall { tp =>
348- val checkpoints = server.getLogManager().logDirs .map { logDir =>
349- new OffsetCheckpoint (new File (logDir, " cleaner-offset-checkpoint" )).read()
362+ val checkpoints = server.getLogManager().liveLogDirs .map { logDir =>
363+ new OffsetCheckpointFile (new File (logDir, " cleaner-offset-checkpoint" )).read()
350364 }
351365 checkpoints.forall(checkpointsPerLogDir => ! checkpointsPerLogDir.contains(tp))
352366 }), s " checkpoint for topic $topic still exists " )
@@ -379,11 +393,9 @@ class KafkaTestUtils(withBrokerProps: Map[String, Object] = Map.empty) extends L
379393 private def waitUntilMetadataIsPropagated (topic : String , partition : Int ): Unit = {
380394 def isPropagated = server.apis.metadataCache.getPartitionInfo(topic, partition) match {
381395 case Some (partitionState) =>
382- val leaderAndInSyncReplicas = partitionState.leaderIsrAndControllerEpoch.leaderAndIsr
383-
384396 zkUtils.getLeaderForPartition(topic, partition).isDefined &&
385- Request .isValidBrokerId(leaderAndInSyncReplicas .leader) &&
386- leaderAndInSyncReplicas.isr.nonEmpty
397+ Request .isValidBrokerId(partitionState.basePartitionState .leader) &&
398+ ! partitionState.basePartitionState.replicas.isEmpty
387399
388400 case _ =>
389401 false
0 commit comments