zentol commented on a change in pull request #7605: [FLINK-11463][PROTOTYPE] 
Port Kafka E2E test to Java
URL: https://github.com/apache/flink/pull/7605#discussion_r269458606
 
 

 ##########
 File path: 
flink-end-to-end-tests/flink-end-to-end-tests-common-kafka/src/main/java/org/apache/flink/tests/util/kafka/LocalStandaloneKafkaResource.java
 ##########
 @@ -0,0 +1,372 @@
+/*
+ * 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.flink.tests.util.kafka;
+
+import org.apache.flink.api.common.time.Deadline;
+import org.apache.flink.tests.util.AutoClosableProcess;
+import org.apache.flink.tests.util.CommandLineWrapper;
+import org.apache.flink.tests.util.activation.OperatingSystemRestriction;
+import org.apache.flink.tests.util.cache.DownloadCache;
+import org.apache.flink.util.OperatingSystem;
+
+import org.junit.rules.TemporaryFolder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.PrintStream;
+import java.io.PrintWriter;
+import java.net.InetSocketAddress;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardOpenOption;
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.Consumer;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import static org.apache.flink.tests.util.AutoClosableProcess.runBlocking;
+import static org.apache.flink.tests.util.AutoClosableProcess.runNonBlocking;
+
+/**
+ * {@link KafkaResource} that downloads kafka and sets up a local kafka 
cluster with the bundled zookeeper.
+ */
+public class LocalStandaloneKafkaResource implements KafkaResource {
+
+       private static final Logger LOG = 
LoggerFactory.getLogger(LocalStandaloneKafkaResource.class);
+       private static final Pattern ZK_DATA_DIR_PATTERN = 
Pattern.compile(".*(dataDir=).*");
+       private static final Pattern KAFKA_LOG_DIR_PATTERN = 
Pattern.compile(".*(log\\.dirs=).*");
+
+       private static final String ZOOKEEPER_HOST = "localhost";
+       private static final int ZOOKEEPER_PORT = 2181;
+       private static final String ZOOKEEPER_ADDRESS = ZOOKEEPER_HOST + ':' + 
ZOOKEEPER_PORT;
+       private static final String KAFKA_HOST = "localhost";
+       private static final int KAFKA_PORT = 9092;
+       private static final String KAFKA_ADDRESS = KAFKA_HOST + ':' + 
KAFKA_PORT;
+
+       private final TemporaryFolder tmp = new TemporaryFolder();
+
+       private final DownloadCache downloadCache = DownloadCache.get();
+       private final String kafkaVersion;
+       private Path kafkaDir;
+
+       LocalStandaloneKafkaResource(final String kafkaVersion) {
+               OperatingSystemRestriction.forbid(
+                       String.format("The %s relies on UNIX utils and shell 
scripts.", getClass().getSimpleName()),
+                       OperatingSystem.WINDOWS);
+               this.kafkaVersion = kafkaVersion;
+       }
+
+       private static String getKafkaDownloadUrl(final String kafkaVersion) {
+               return 
String.format("https://archive.apache.org/dist/kafka/%s/kafka_2.11-%s.tgz";, 
kafkaVersion, kafkaVersion);
+       }
+
+       @Override
+       public void before() throws Exception {
+               tmp.create();
+               downloadCache.before();
+
+               this.kafkaDir = 
tmp.newFolder("kafka").toPath().toAbsolutePath();
+               setupKafkaDist();
+               setKafkaCluster();
+       }
+
+       private void setupKafkaDist() throws IOException {
+               final Path downloadDirectory = 
tmp.newFolder("getOrDownload").toPath();
+               final Path kafkaArchive = 
downloadCache.getOrDownload(getKafkaDownloadUrl(kafkaVersion), 
downloadDirectory);
+
+               LOG.info("Kafka location: {}", kafkaDir.toAbsolutePath());
+               runBlocking(CommandLineWrapper
+                       .tar(kafkaArchive)
+                       .extract()
+                       .zipped()
+                       .strip(1)
+                       .targetDir(kafkaDir)
+                       .build());
+
+               LOG.info("Updating ZooKeeper properties");
+               final Path zookeeperPropertiesFile = 
kafkaDir.resolve(Paths.get("config", "zookeeper.properties"));
+               final List<String> zookeeperPropertiesFileLines = 
Files.readAllLines(zookeeperPropertiesFile);
+               try (PrintWriter pw = new PrintWriter(new 
OutputStreamWriter(Files.newOutputStream(zookeeperPropertiesFile, 
StandardOpenOption.TRUNCATE_EXISTING), StandardCharsets.UTF_8.name()))) {
+                       zookeeperPropertiesFileLines.stream()
+                               .map(line -> 
ZK_DATA_DIR_PATTERN.matcher(line).replaceAll("$1" + 
kafkaDir.resolve("zookeeper").toAbsolutePath()))
+                               .forEachOrdered(pw::println);
+               }
+
+               LOG.info("Updating Kafka properties");
+               final Path kafkaPropertiesFile = 
kafkaDir.resolve(Paths.get("config", "server.properties"));
+               final List<String> kafkaPropertiesFileLines = 
Files.readAllLines(kafkaPropertiesFile);
+               try (PrintWriter pw = new PrintWriter(new 
OutputStreamWriter(Files.newOutputStream(kafkaPropertiesFile, 
StandardOpenOption.TRUNCATE_EXISTING), StandardCharsets.UTF_8.name()))) {
+                       kafkaPropertiesFileLines.stream()
+                               .map(line -> 
KAFKA_LOG_DIR_PATTERN.matcher(line).replaceAll("$1" + 
kafkaDir.resolve("kafka").toAbsolutePath()))
+                               .forEachOrdered(pw::println);
+               }
+       }
+
+       private void setKafkaCluster() throws IOException {
 
 Review comment:
   yes

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to