Search in sources :

Example 1 with AutoClosableProcess

use of org.apache.flink.tests.util.AutoClosableProcess in project flink by apache.

the class PrometheusReporterEndToEndITCase method testReporter.

@Test
public void testReporter() throws Exception {
    final Path tmpPrometheusDir = tmp.newFolder().toPath().resolve("prometheus");
    final Path prometheusBinDir = tmpPrometheusDir.resolve(PROMETHEUS_FILE_NAME);
    final Path prometheusConfig = prometheusBinDir.resolve("prometheus.yml");
    final Path prometheusBinary = prometheusBinDir.resolve("prometheus");
    Files.createDirectory(tmpPrometheusDir);
    final Path prometheusArchive = downloadCache.getOrDownload("https://github.com/prometheus/prometheus/releases/download/v" + PROMETHEUS_VERSION + '/' + PROMETHEUS_FILE_NAME + ".tar.gz", tmpPrometheusDir);
    LOG.info("Unpacking Prometheus.");
    runBlocking(CommandLineWrapper.tar(prometheusArchive).extract().zipped().targetDir(tmpPrometheusDir).build());
    LOG.info("Setting Prometheus scrape interval.");
    runBlocking(CommandLineWrapper.sed("s/\\(scrape_interval:\\).*/\\1 1s/", prometheusConfig).inPlace().build());
    try (ClusterController ignored = dist.startCluster(1)) {
        final List<Integer> ports = dist.searchAllLogs(LOG_REPORTER_PORT_PATTERN, matcher -> matcher.group(1)).map(Integer::valueOf).collect(Collectors.toList());
        final String scrapeTargets = ports.stream().map(port -> "'localhost:" + port + "'").collect(Collectors.joining(", "));
        LOG.info("Setting Prometheus scrape targets to {}.", scrapeTargets);
        runBlocking(CommandLineWrapper.sed("s/\\(targets:\\).*/\\1 [" + scrapeTargets + "]/", prometheusConfig).inPlace().build());
        LOG.info("Starting Prometheus server.");
        try (AutoClosableProcess prometheus = runNonBlocking(prometheusBinary.toAbsolutePath().toString(), "--config.file=" + prometheusConfig.toAbsolutePath(), "--storage.tsdb.path=" + prometheusBinDir.resolve("data").toAbsolutePath())) {
            final OkHttpClient client = new OkHttpClient();
            checkMetricAvailability(client, "flink_jobmanager_numRegisteredTaskManagers");
            checkMetricAvailability(client, "flink_taskmanager_Status_Network_TotalMemorySegments");
        }
    }
}
Also used : Path(java.nio.file.Path) LocalStandaloneFlinkResourceFactory(org.apache.flink.tests.util.flink.LocalStandaloneFlinkResourceFactory) REFLECTION(org.apache.flink.metrics.prometheus.tests.PrometheusReporterEndToEndITCase.TestParams.InstantiationType.REFLECTION) Arrays(java.util.Arrays) BeforeClass(org.junit.BeforeClass) ProcessorArchitecture(org.apache.flink.util.ProcessorArchitecture) RunWith(org.junit.runner.RunWith) LoggerFactory(org.slf4j.LoggerFactory) ExceptionUtils(org.apache.flink.util.ExceptionUtils) FlinkResourceSetup(org.apache.flink.tests.util.flink.FlinkResourceSetup) JarLocation(org.apache.flink.tests.util.flink.JarLocation) AutoClosableProcess.runNonBlocking(org.apache.flink.tests.util.AutoClosableProcess.runNonBlocking) AutoClosableProcess(org.apache.flink.tests.util.AutoClosableProcess) ObjectMapper(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper) ConfigConstants(org.apache.flink.configuration.ConfigConstants) TestLogger(org.apache.flink.util.TestLogger) Response(okhttp3.Response) Assume(org.junit.Assume) Path(java.nio.file.Path) Parameterized(org.junit.runners.Parameterized) Request(okhttp3.Request) Logger(org.slf4j.Logger) Files(java.nio.file.Files) FlinkResource(org.apache.flink.tests.util.flink.FlinkResource) Collection(java.util.Collection) Configuration(org.apache.flink.configuration.Configuration) Test(org.junit.Test) OperatingSystem(org.apache.flink.util.OperatingSystem) Collectors(java.util.stream.Collectors) FACTORY(org.apache.flink.metrics.prometheus.tests.PrometheusReporterEndToEndITCase.TestParams.InstantiationType.FACTORY) Consumer(java.util.function.Consumer) List(java.util.List) OkHttpClient(okhttp3.OkHttpClient) Rule(org.junit.Rule) CommandLineWrapper(org.apache.flink.tests.util.CommandLineWrapper) DownloadCache(org.apache.flink.tests.util.cache.DownloadCache) ClusterController(org.apache.flink.tests.util.flink.ClusterController) PrometheusReporterFactory(org.apache.flink.metrics.prometheus.PrometheusReporterFactory) Pattern(java.util.regex.Pattern) PrometheusReporter(org.apache.flink.metrics.prometheus.PrometheusReporter) TemporaryFolder(org.junit.rules.TemporaryFolder) AutoClosableProcess.runBlocking(org.apache.flink.tests.util.AutoClosableProcess.runBlocking) ClusterController(org.apache.flink.tests.util.flink.ClusterController) OkHttpClient(okhttp3.OkHttpClient) AutoClosableProcess(org.apache.flink.tests.util.AutoClosableProcess) Test(org.junit.Test)

Example 2 with AutoClosableProcess

use of org.apache.flink.tests.util.AutoClosableProcess in project flink by apache.

the class LocalStandaloneKafkaResource method readMessage.

@Override
public List<String> readMessage(int expectedNumMessages, String groupId, String topic) throws IOException {
    final List<String> messages = Collections.synchronizedList(new ArrayList<>(expectedNumMessages));
    try (final AutoClosableProcess kafka = AutoClosableProcess.create(kafkaDir.resolve(Paths.get("bin", "kafka-console-consumer.sh")).toString(), "--bootstrap-server", KAFKA_ADDRESS, "--from-beginning", "--max-messages", String.valueOf(expectedNumMessages), "--topic", topic, "--consumer-property", "group.id=" + groupId).setStdoutProcessor(messages::add).runNonBlocking()) {
        final Deadline deadline = Deadline.fromNow(Duration.ofSeconds(120));
        while (deadline.hasTimeLeft() && messages.size() < expectedNumMessages) {
            try {
                LOG.info("Waiting for messages. Received {}/{}.", messages.size(), expectedNumMessages);
                Thread.sleep(500);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                break;
            }
        }
        if (messages.size() != expectedNumMessages) {
            throw new IOException("Could not read expected number of messages.");
        }
        return messages;
    }
}
Also used : Deadline(org.apache.flink.api.common.time.Deadline) AutoClosableProcess(org.apache.flink.tests.util.AutoClosableProcess) IOException(java.io.IOException)

Aggregations

AutoClosableProcess (org.apache.flink.tests.util.AutoClosableProcess)2 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 List (java.util.List)1 Consumer (java.util.function.Consumer)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 Deadline (org.apache.flink.api.common.time.Deadline)1 ConfigConstants (org.apache.flink.configuration.ConfigConstants)1 Configuration (org.apache.flink.configuration.Configuration)1 PrometheusReporter (org.apache.flink.metrics.prometheus.PrometheusReporter)1 PrometheusReporterFactory (org.apache.flink.metrics.prometheus.PrometheusReporterFactory)1 FACTORY (org.apache.flink.metrics.prometheus.tests.PrometheusReporterEndToEndITCase.TestParams.InstantiationType.FACTORY)1 REFLECTION (org.apache.flink.metrics.prometheus.tests.PrometheusReporterEndToEndITCase.TestParams.InstantiationType.REFLECTION)1