/**
 * Copyright (C) 2014-2015 LinkedIn Corp. (pinot-core@linkedin.com)
 *
 * Licensed 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 com.linkedin.pinot.common.utils;

import java.io.File;
import java.util.Properties;

import kafka.admin.TopicCommand;
import kafka.server.KafkaConfig;
import kafka.server.KafkaServerStartable;

import org.I0Itec.zkclient.ZkClient;
import org.apache.commons.io.FileUtils;


/**
 * Utilities to start Kafka during unit tests.
 *
 */
public class KafkaStarterUtils {
  public static final int DEFAULT_KAFKA_PORT = 19092;
  public static final int DEFAULT_BROKER_ID = 0;
  public static final String DEFAULT_ZK_STR = ZkStarter.DEFAULT_ZK_STR + "/kafka";
  public static final String DEFAULT_KAFKA_BROKER = "localhost:" + DEFAULT_KAFKA_PORT;

  public static Properties getDefaultKafkaConfiguration() {
    return new Properties();
  }

  public static KafkaServerStartable startServer(final int port, final int brokerId, final String zkStr,
      final Properties configuration) {
    // Create the ZK nodes for Kafka, if needed
    int indexOfFirstSlash = zkStr.indexOf('/');
    if (indexOfFirstSlash != -1) {
      String bareZkUrl = zkStr.substring(0, indexOfFirstSlash);
      String zkNodePath = zkStr.substring(indexOfFirstSlash);
      ZkClient client = new ZkClient(bareZkUrl);
      client.createPersistent(zkNodePath, true);
      client.close();
    }

    File logDir = new File("/tmp/kafka-" + Double.toHexString(Math.random()));
    logDir.mkdirs();

    configuration.put("port", Integer.toString(port));
    configuration.put("zookeeper.connect", zkStr);
    configuration.put("broker.id", Integer.toString(brokerId));
    configuration.put("log.dirs", logDir.getAbsolutePath());
    KafkaConfig config = new KafkaConfig(configuration);

    KafkaServerStartable serverStartable = new KafkaServerStartable(config);
    serverStartable.startup();

    return serverStartable;
  }

  public static void stopServer(KafkaServerStartable serverStartable) {
    serverStartable.shutdown();
    FileUtils.deleteQuietly(new File(serverStartable.serverConfig().logDirs().apply(0)));
  }

  public static void createTopic(String kafkaTopic, String zkStr) {
    TopicCommand
        .main(new String[] { "--create", "--zookeeper", zkStr, "--replication-factor", "1", "--partitions", "10", "--topic", kafkaTopic });
  }
}

@@ -2017,49 +2017,39 @@
 igur
-ation.put(%22port%22, Integer.toString(
+eKafkaPort(configuration, 
 port)
-)
 ;%0A  
@@ -2062,99 +2062,88 @@
 igur
-ation.put(%22zookeeper.connect%22, zkStr);%0A    configuration.put(%22broker.id%22, Integer.toString(
+eZkConnectionString(configuration, zkStr);%0A    configureBrokerId(configuration, 
 brok
@@ -2147,17 +2147,16 @@
 rokerId)
-)
 ;%0A    co
@@ -2165,54 +2165,48 @@
 igur
-ation.put(%22log.dirs%22, logDir.getAbsolutePath()
+eKafkaLogDirectory(configuration, logDir
 );%0A 
@@ -2400,24 +2400,973 @@
 table;%0A  %7D%0A%0A
+  public static void configureSegmentSizeBytes(Properties properties, int segmentSize) %7B%0A    properties.put(%22log.segment.bytes%22, Integer.toString(segmentSize));%0A  %7D%0A%0A  public static void configureLogRetentionSizeBytes(Properties properties, int logRetentionSizeBytes) %7B%0A    properties.put(%22log.retention.bytes%22, Integer.toString(logRetentionSizeBytes));%0A  %7D%0A%0A  public static void configureKafkaLogDirectory(Properties configuration, File logDir) %7B%0A    configuration.put(%22log.dirs%22, logDir.getAbsolutePath());%0A  %7D%0A%0A  public static void configureBrokerId(Properties configuration, int brokerId) %7B%0A    configuration.put(%22broker.id%22, Integer.toString(brokerId));%0A  %7D%0A%0A  public static void configureZkConnectionString(Properties configuration, String zkStr) %7B%0A    configuration.put(%22zookeeper.connect%22, zkStr);%0A  %7D%0A%0A  public static void configureKafkaPort(Properties configuration, int port) %7B%0A    configuration.put(%22port%22, Integer.toString(port));%0A  %7D%0A%0A
   public sta

Add methods to configure Kafka log segment size and log retention size