shanthoosh commented on a change in pull request #951: SAMZA-2127: Upgrade to 
Kafka 2.0
URL: https://github.com/apache/samza/pull/951#discussion_r268386095
 
 

 ##########
 File path: 
samza-test/src/test/java/org/apache/samza/test/harness/IntegrationTestHarness.java
 ##########
 @@ -0,0 +1,192 @@
+/*
+ * 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.samza.test.harness;
+
+import com.google.common.collect.ImmutableMap;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.concurrent.TimeUnit;
+import org.apache.kafka.clients.admin.AdminClient;
+import org.apache.kafka.clients.admin.CreatePartitionsResult;
+import org.apache.kafka.clients.admin.CreateTopicsResult;
+import org.apache.kafka.clients.admin.DeleteTopicsResult;
+import org.apache.kafka.clients.admin.NewPartitions;
+import org.apache.kafka.clients.admin.NewTopic;
+import org.apache.kafka.clients.consumer.KafkaConsumer;
+import org.apache.kafka.clients.producer.KafkaProducer;
+import org.apache.kafka.common.serialization.ByteArrayDeserializer;
+import org.apache.kafka.common.serialization.ByteArraySerializer;
+import org.apache.kafka.common.serialization.Deserializer;
+import org.apache.kafka.common.serialization.Serializer;
+import org.apache.kafka.common.serialization.StringDeserializer;
+import org.apache.kafka.common.serialization.StringSerializer;
+import org.apache.samza.config.Config;
+import org.apache.samza.config.JobConfig;
+import org.apache.samza.config.KafkaConsumerConfig;
+import org.apache.samza.config.MapConfig;
+import org.apache.samza.context.ExternalContext;
+import org.apache.samza.runtime.ApplicationRunner;
+import org.apache.samza.system.kafka.KafkaSystemAdmin;
+import org.apache.samza.system.kafka.KafkaSystemConsumer;
+
+import static org.apache.kafka.clients.consumer.ConsumerConfig.*;
+
+
+public class IntegrationTestHarness extends AbstractKafkaServerTestHarness {
+  private static final ByteArraySerializer BYTE_ARRAY_SERIALIZER = new 
ByteArraySerializer();
+  private static final ByteArrayDeserializer BYTE_ARRAY_DESERIALIZER = new 
ByteArrayDeserializer();
+
+  protected static final Serializer<String> STRING_SERIALIZER = new 
StringSerializer();
+  protected static final Deserializer<String> STRING_DESERIALIZER = new 
StringDeserializer();
+
+  private AdminClient adminClient;
+  protected KafkaConsumer consumer;
+  protected KafkaProducer producer;
+  protected KafkaSystemAdmin systemAdmin;
+
+  /**
+   * Starts a single kafka broker, and a single embedded zookeeper server in 
their own threads.
+   * Sub-classes should invoke {@link #zkConnect()} and {@link 
#bootstrapUrl()}s to
+   * obtain the urls (and ports) of the started zookeeper and kafka broker.
+   */
+  @Override
+  public void setUp() {
+    super.setUp();
+    producer = createProducer();
+    consumer = createConsumer();
+
+    Properties kafkaConfig = new Properties();
+    kafkaConfig.setProperty("bootstrap.servers", bootstrapServers());
+    adminClient = AdminClient.create(kafkaConfig);
+
+    systemAdmin = createSystemAdmin("kafka");
+    systemAdmin.start();
+  }
+
+  /**
+   * Shutdown and clear Zookeeper and Kafka broker state.
+   */
+  @Override
+  public void tearDown() {
+    systemAdmin.stop();
+
+   /*
+    * Close joins on AdminClientRunnable thread and at times, it takes longer 
than the test timeouts, resulting
+    * in test failures. Using a bounded close ensures tests passes 
successfully. Note, in the event of timeout,
+    * close notifies AdminClientRunnable thread which in turn closes the 
underlying client quietly and
+    * it shouldn't impact the tests nor have any side effects.
+    */
+    adminClient.close(10000, TimeUnit.MILLISECONDS);
+    consumer.close();
+    producer.close();
+    super.tearDown();
+  }
+
+  /**
+   * Returns the bootstrap servers configuration string to be used by clients.
+   *
+   * @return bootstrap servers string.
+   */
+  protected String bootstrapServers() {
+    return bootstrapUrl();
+  }
+
+  protected KafkaProducer createProducer() {
+    Properties kafkaConfig = new Properties();
+    kafkaConfig.setProperty("bootstrap.servers", bootstrapServers());
+    return new KafkaProducer<>(kafkaConfig, STRING_SERIALIZER, 
BYTE_ARRAY_SERIALIZER);
+  }
+
+  protected KafkaConsumer createConsumer() {
+    Properties consumerProps = new Properties();
+    consumerProps.setProperty("bootstrap.servers", bootstrapServers());
+    consumerProps.setProperty("group.id", "group");
+    consumerProps.setProperty("auto.offset.reset", "earliest");
+    return new KafkaConsumer<>(consumerProps, STRING_DESERIALIZER, 
BYTE_ARRAY_DESERIALIZER);
+  }
+
+  protected void executeRun(ApplicationRunner applicationRunner, Config 
config) {
+    applicationRunner.run(buildExternalContext(config).orElse(null));
+  }
+
+  protected boolean createTopic(String topicName, int numPartitions, int 
replicationFactor) {
+    return createTopics(Collections.singleton(new NewTopic(topicName, 
numPartitions, (short) replicationFactor)));
+  }
+
+  protected boolean createTopics(Collection<NewTopic> newTopics) {
+    boolean createStatus = true;
+
+    try {
+      CreateTopicsResult resultFuture =
+          adminClient.createTopics(newTopics);
+      // wait for 10 seconds to make sure the topic is created if not
+      resultFuture.all().get(10000, TimeUnit.MILLISECONDS);
+    } catch (Exception e) {
+      createStatus = false;
+    }
+
+    return createStatus;
+  }
+
+  protected boolean deleteTopics(Collection<String> topics) {
+    boolean deleteStatus = true;
+
+    try {
+      DeleteTopicsResult resultFutures = adminClient.deleteTopics(topics);
+      resultFutures.all().get(10000, TimeUnit.MILLISECONDS);
+    } catch (Exception e) {
+      deleteStatus = false;
+    }
+
+    return deleteStatus;
+  }
+
+  protected CreatePartitionsResult increasePartitionsTo(String topicName, int 
numPartitions) {
+    return adminClient.createPartitions(ImmutableMap.of(topicName, 
NewPartitions.increaseTo(numPartitions)));
+  }
+
+  private Optional<ExternalContext> buildExternalContext(Config config) {
+    /*
+     * By default, use an empty ExternalContext here. In a custom fork of 
Samza, this can be implemented to pass
+     * a non-empty ExternalContext. Only config should be used to build the 
external context. In the future, components
+     * like the application descriptor may not be available.
+     */
+    return Optional.empty();
+  }
+
+  private KafkaSystemAdmin createSystemAdmin(String system) {
+    String kafkaConsumerPropertyPrefix = "systems." + system + ".consumer.";
+
+    Map<String, String> map = new HashMap<>();
+    map.put(kafkaConsumerPropertyPrefix + BOOTSTRAP_SERVERS_CONFIG, 
brokerList());
+    map.put(JobConfig.JOB_NAME(), "test.job");
+    map.put(kafkaConsumerPropertyPrefix + 
KafkaConsumerConfig.ZOOKEEPER_CONNECT, zkConnect());
 
 Review comment:
   Zookeeper connect is not required by new kafka `AdminClient`, we can remove 
it.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to