Github user tdas commented on a diff in the pull request:

    https://github.com/apache/spark/pull/4961#discussion_r27717848
  
    --- Diff: 
external/kafka/src/main/scala/org/apache/spark/streaming/kafka/KafkaTestUtils.scala
 ---
    @@ -0,0 +1,272 @@
    +/*
    + * 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.spark.streaming.kafka
    +
    +import java.io.File
    +import java.lang.{Integer => JInt}
    +import java.net.InetSocketAddress
    +import java.util.{Map => JMap}
    +import java.util.Properties
    +import java.util.concurrent.TimeoutException
    +
    +import scala.annotation.tailrec
    +import scala.language.postfixOps
    +import scala.util.Random
    +import scala.util.control.NonFatal
    +
    +import kafka.admin.AdminUtils
    +import kafka.common.KafkaException
    +import kafka.producer.{KeyedMessage, Producer, ProducerConfig}
    +import kafka.serializer.StringEncoder
    +import kafka.server.{KafkaConfig, KafkaServer}
    +import kafka.utils.ZKStringSerializer
    +import org.apache.zookeeper.server.{NIOServerCnxnFactory, ZooKeeperServer}
    +import org.I0Itec.zkclient.ZkClient
    +
    +import org.apache.spark.Logging
    +import org.apache.spark.streaming.Time
    +import org.apache.spark.util.Utils
    +
    +/**
    + * This is a helper class for Kafka test suites. This has the 
functionality to set up
    + * and tear down local Kafka servers, and to push data using Kafka 
producers.
    + *
    + * The reason to put Kafka test utility class in src is to test Python 
related Kafka APIs.
    + */
    +private class KafkaTestUtils extends Logging {
    +
    +  // Zookeeper related configurations
    +  private val zkHost = "localhost"
    +  private var zkPort: Int = 0
    +  private val zkConnectionTimeout = 6000
    +  private val zkSessionTimeout = 6000
    +
    +  private var zookeeper: EmbeddedZookeeper = _
    +
    +  private var zkClient: ZkClient = _
    +
    +  // Kafka broker related configurations
    +  private val brokerHost = "localhost"
    +  private var brokerPort = 9092
    +  private var brokerConf: KafkaConfig = _
    +
    +  // Kafka broker server
    +  private var server: KafkaServer = _
    +
    +  // Kafka producer
    +  private var producer: Producer[String, String] = _
    +
    +  // Flag to test whether the system is correctly started
    +  private var zkReady = false
    +  private var brokerReady = false
    +
    +  def zkAddress: String = {
    +    assert(zkReady, "Zookeeper not setup yet or already torn down, cannot 
get zookeeper address")
    +    s"$zkHost:$zkPort"
    +  }
    +
    +  def brokerAddress: String = {
    +    assert(brokerReady, "Kafka not setup yet or already torn down, cannot 
get broker address")
    +    s"$brokerHost:$brokerPort"
    +  }
    +
    +  def zookeeperClient: ZkClient = {
    +    assert(zkReady, "Zookeeper not setup yet or already torn down, cannot 
get zookeeper client")
    +    Option(zkClient).getOrElse(
    +      throw new IllegalStateException("Zookeeper client is not yet 
initialized"))
    +  }
    +
    +  // Set up the Embedded Zookeeper server and get the proper Zookeeper port
    +  private def setupEmbeddedZookeeper(): Unit = {
    +    // Zookeeper server startup
    +    zookeeper = new EmbeddedZookeeper(s"$zkHost:$zkPort")
    +    // Get the actual zookeeper binding port
    +    zkPort = zookeeper.actualPort
    +    zkClient = new ZkClient(s"$zkHost:$zkPort", zkSessionTimeout, 
zkConnectionTimeout,
    +      ZKStringSerializer)
    +    zkReady = true
    +  }
    +
    +  // Set up the Embedded Kafka server
    +  private def setupEmbeddedKafkaServer(): Unit = {
    +    assert(zkReady, "Zookeeper should be set up beforehand")
    +    // Kafka broker startup
    +    var bindSuccess: Boolean = false
    +    while(!bindSuccess) {
    +      try {
    +        brokerConf = new KafkaConfig(brokerConfigure)
    +        server = new KafkaServer(brokerConf)
    +        server.startup()
    +        bindSuccess = true
    +      } catch {
    +        case e: KafkaException =>
    +          if (e.getMessage != null && e.getMessage.contains("Socket server 
failed to bind to")) {
    +            brokerPort += 1
    +          }
    +        case e: Exception => throw new Exception("Kafka server create 
failed", e)
    +      }
    +    }
    +
    +    Thread.sleep(2000)
    --- End diff --
    
    Good catch. This sleep has been there since before, we should remove it if 
we can. 
    Does it become flaky despite the while loop above it? Is there any thing 
else we should be waiting for rather than blindly waiting for 2 seconds?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to