Search in sources :

Example 36 with JobClient

use of org.apache.flink.core.execution.JobClient in project flink by apache.

the class KafkaTableITCase method testPerPartitionWatermarkWithIdleSource.

@Test
public void testPerPartitionWatermarkWithIdleSource() throws Exception {
    // we always use a different topic name for each parameterized topic,
    // in order to make sure the topic can be created.
    final String topic = "idle_partition_watermark_topic_" + format;
    createTestTopic(topic, 4, 1);
    // ---------- Produce an event time stream into Kafka -------------------
    String groupId = getStandardProps().getProperty("group.id");
    String bootstraps = getBootstrapServers();
    tEnv.getConfig().getConfiguration().set(TABLE_EXEC_SOURCE_IDLE_TIMEOUT, Duration.ofMillis(100));
    final String createTable = String.format("CREATE TABLE kafka (\n" + "  `partition_id` INT,\n" + "  `value` INT,\n" + "  `timestamp` TIMESTAMP(3),\n" + "  WATERMARK FOR `timestamp` AS `timestamp`\n" + ") WITH (\n" + "  'connector' = 'kafka',\n" + "  'topic' = '%s',\n" + "  'properties.bootstrap.servers' = '%s',\n" + "  'properties.group.id' = '%s',\n" + "  'scan.startup.mode' = 'earliest-offset',\n" + "  'sink.partitioner' = '%s',\n" + "  'format' = '%s'\n" + ")", topic, bootstraps, groupId, TestPartitioner.class.getName(), format);
    tEnv.executeSql(createTable);
    // Only two partitions have elements and others are idle.
    // When idle timer triggers, the WatermarkOutputMultiplexer will use the minimum watermark
    // among active partitions as the output watermark.
    // Therefore, we need to make sure the watermark in the each partition is large enough to
    // trigger the window.
    String initialValues = "INSERT INTO kafka\n" + "VALUES\n" + " (0, 0, TIMESTAMP '2020-03-08 13:12:11.123'),\n" + " (0, 1, TIMESTAMP '2020-03-08 13:15:12.223'),\n" + " (0, 2, TIMESTAMP '2020-03-08 16:12:13.323'),\n" + " (1, 3, TIMESTAMP '2020-03-08 13:13:11.123'),\n" + " (1, 4, TIMESTAMP '2020-03-08 13:19:11.133'),\n" + " (1, 5, TIMESTAMP '2020-03-08 16:13:11.143')\n";
    tEnv.executeSql(initialValues).await();
    // ---------- Consume stream from Kafka -------------------
    env.setParallelism(1);
    String createSink = "CREATE TABLE MySink(\n" + "  `id` INT,\n" + "  `cnt` BIGINT\n" + ") WITH (\n" + "  'connector' = 'values'\n" + ")";
    tEnv.executeSql(createSink);
    TableResult tableResult = tEnv.executeSql("INSERT INTO MySink\n" + "SELECT `partition_id` as `id`, COUNT(`value`) as `cnt`\n" + "FROM kafka\n" + "GROUP BY `partition_id`, TUMBLE(`timestamp`, INTERVAL '1' HOUR) ");
    final List<String> expected = Arrays.asList("+I[0, 2]", "+I[1, 2]");
    KafkaTableTestUtils.waitingExpectedResults("MySink", expected, Duration.ofSeconds(5));
    // ------------- cleanup -------------------
    tableResult.getJobClient().ifPresent(JobClient::cancel);
    deleteTestTopic(topic);
}
Also used : TableResult(org.apache.flink.table.api.TableResult) JobClient(org.apache.flink.core.execution.JobClient) Test(org.junit.Test)

Example 37 with JobClient

use of org.apache.flink.core.execution.JobClient in project flink by apache.

the class PerJobMiniClusterFactory method submitJob.

/**
 * Starts a {@link MiniCluster} and submits a job.
 */
public CompletableFuture<JobClient> submitJob(JobGraph jobGraph, ClassLoader userCodeClassloader) throws Exception {
    MiniClusterConfiguration miniClusterConfig = getMiniClusterConfig(jobGraph.getMaximumParallelism());
    MiniCluster miniCluster = miniClusterFactory.apply(miniClusterConfig);
    miniCluster.start();
    return miniCluster.submitJob(jobGraph).thenApplyAsync(FunctionUtils.uncheckedFunction(submissionResult -> {
        org.apache.flink.client.ClientUtils.waitUntilJobInitializationFinished(() -> miniCluster.getJobStatus(submissionResult.getJobID()).get(), () -> miniCluster.requestJobResult(submissionResult.getJobID()).get(), userCodeClassloader);
        return submissionResult;
    })).thenApply(result -> new MiniClusterJobClient(result.getJobID(), miniCluster, userCodeClassloader, MiniClusterJobClient.JobFinalizationBehavior.SHUTDOWN_CLUSTER)).whenComplete((ignored, throwable) -> {
        if (throwable != null) {
            // We failed to create the JobClient and must shutdown to ensure
            // cleanup.
            shutDownCluster(miniCluster);
        }
    }).thenApply(Function.identity());
}
Also used : MiniClusterConfiguration(org.apache.flink.runtime.minicluster.MiniClusterConfiguration) Logger(org.slf4j.Logger) RpcServiceSharing(org.apache.flink.runtime.minicluster.RpcServiceSharing) Configuration(org.apache.flink.configuration.Configuration) JobGraph(org.apache.flink.runtime.jobgraph.JobGraph) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) JobClient(org.apache.flink.core.execution.JobClient) MathUtils(org.apache.flink.util.MathUtils) TaskManagerOptions(org.apache.flink.configuration.TaskManagerOptions) MiniClusterJobClient(org.apache.flink.runtime.minicluster.MiniClusterJobClient) FunctionUtils(org.apache.flink.util.function.FunctionUtils) ConfigConstants(org.apache.flink.configuration.ConfigConstants) MiniCluster(org.apache.flink.runtime.minicluster.MiniCluster) RestOptions(org.apache.flink.configuration.RestOptions) MiniClusterJobClient(org.apache.flink.runtime.minicluster.MiniClusterJobClient) MiniClusterConfiguration(org.apache.flink.runtime.minicluster.MiniClusterConfiguration) MiniCluster(org.apache.flink.runtime.minicluster.MiniCluster)

Example 38 with JobClient

use of org.apache.flink.core.execution.JobClient in project flink by apache.

the class PerJobMiniClusterFactoryTest method testJobClient.

@Test
public void testJobClient() throws Exception {
    PerJobMiniClusterFactory perJobMiniClusterFactory = initializeMiniCluster();
    JobGraph cancellableJobGraph = getCancellableJobGraph();
    JobClient jobClient = perJobMiniClusterFactory.submitJob(cancellableJobGraph, ClassLoader.getSystemClassLoader()).get();
    assertThat(jobClient.getJobID(), is(cancellableJobGraph.getJobID()));
    assertThat(jobClient.getJobStatus().get(), is(JobStatus.RUNNING));
    jobClient.cancel().get();
    assertThrows("Job was cancelled.", ExecutionException.class, () -> jobClient.getJobExecutionResult().get());
    assertThatMiniClusterIsShutdown();
}
Also used : JobGraph(org.apache.flink.runtime.jobgraph.JobGraph) JobClient(org.apache.flink.core.execution.JobClient) Test(org.junit.Test)

Example 39 with JobClient

use of org.apache.flink.core.execution.JobClient in project flink by apache.

the class PerJobMiniClusterFactoryTest method testJobClientSavepoint.

@Test
public void testJobClientSavepoint() throws Exception {
    PerJobMiniClusterFactory perJobMiniClusterFactory = initializeMiniCluster();
    JobClient jobClient = perJobMiniClusterFactory.submitJob(getCancellableJobGraph(), ClassLoader.getSystemClassLoader()).get();
    assertThrows("is not a streaming job.", ExecutionException.class, () -> jobClient.triggerSavepoint(null, SavepointFormatType.DEFAULT).get());
    assertThrows("is not a streaming job.", ExecutionException.class, () -> jobClient.stopWithSavepoint(true, null, SavepointFormatType.DEFAULT).get());
}
Also used : JobClient(org.apache.flink.core.execution.JobClient) Test(org.junit.Test)

Example 40 with JobClient

use of org.apache.flink.core.execution.JobClient in project flink by apache.

the class PerJobMiniClusterFactoryTest method testJobClientInteractionAfterShutdown.

@Test
public void testJobClientInteractionAfterShutdown() throws Exception {
    PerJobMiniClusterFactory perJobMiniClusterFactory = initializeMiniCluster();
    JobClient jobClient = perJobMiniClusterFactory.submitJob(getNoopJobGraph(), ClassLoader.getSystemClassLoader()).get();
    jobClient.getJobExecutionResult().get();
    assertThatMiniClusterIsShutdown();
    assertThrows("MiniCluster is not yet running or has already been shut down.", IllegalStateException.class, jobClient::cancel);
}
Also used : JobClient(org.apache.flink.core.execution.JobClient) Test(org.junit.Test)

Aggregations

JobClient (org.apache.flink.core.execution.JobClient)70 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)36 Test (org.junit.Test)32 JobExecutionResult (org.apache.flink.api.common.JobExecutionResult)16 Configuration (org.apache.flink.configuration.Configuration)16 JobListener (org.apache.flink.core.execution.JobListener)14 ArrayList (java.util.ArrayList)12 List (java.util.List)10 JobID (org.apache.flink.api.common.JobID)10 ExecutionException (java.util.concurrent.ExecutionException)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 DEFAULT_COLLECT_DATA_TIMEOUT (org.apache.flink.connector.testframe.utils.ConnectorTestConstants.DEFAULT_COLLECT_DATA_TIMEOUT)8 DEFAULT_JOB_STATUS_CHANGE_TIMEOUT (org.apache.flink.connector.testframe.utils.ConnectorTestConstants.DEFAULT_JOB_STATUS_CHANGE_TIMEOUT)8 IOException (java.io.IOException)7 DisplayName (org.junit.jupiter.api.DisplayName)7 TestTemplate (org.junit.jupiter.api.TestTemplate)7 Iterator (java.util.Iterator)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 ExecutionEnvironment (org.apache.flink.api.java.ExecutionEnvironment)6 Preconditions.checkNotNull (org.apache.flink.util.Preconditions.checkNotNull)6